diff --git a/crates/untwist-deobfuscate/src/control_flow_object/dead.rs b/crates/untwist-deobfuscate/src/control_flow_object/dead.rs index 3c9c7c1d..b3ed920f 100644 --- a/crates/untwist-deobfuscate/src/control_flow_object/dead.rs +++ b/crates/untwist-deobfuscate/src/control_flow_object/dead.rs @@ -9,13 +9,15 @@ use rustc_hash::{FxHashMap, FxHashSet}; use untwist_core::ast_util::global::{references_are_global, references_are_global_or}; use untwist_core::ast_util::inline::{is_plain_function, param_names, returned_expression}; use untwist_core::ast_util::prop::member_parts; -use untwist_core::ast_util::readonly::{readonly_binding_uses, ObjectUses}; use untwist_core::pass::Cx; use untwist_eval::fold::{fold_binary, fold_to_const, fold_unary, Const, FoldOptions}; use std::borrow::Cow; -use std::cell::RefCell; +use std::cell::{OnceCell, RefCell}; +use super::nested_proof::{ + annex_b_written, builtins_proven_intact, proven_readonly, static_property, touches_builtins, +}; use super::CffValue; fn truthy(value: &Const) -> bool { @@ -117,58 +119,6 @@ fn param_symbols(function: &Function<'_>) -> Option> { .collect() } -const PROTO_KEY: &str = "__proto__"; - -const OBJECT_PROTOTYPE_KEYS: [&str; 12] = [ - "constructor", - "hasOwnProperty", - "isPrototypeOf", - "propertyIsEnumerable", - "toLocaleString", - "toString", - "valueOf", - PROTO_KEY, - "__defineGetter__", - "__defineSetter__", - "__lookupGetter__", - "__lookupSetter__", -]; - -fn static_key<'p>(property: &'p ObjectProperty<'p>) -> Option<&'p str> { - if property.method || property.computed || property.kind != PropertyKind::Init { - return None; - } - let key = match &property.key { - PropertyKey::StaticIdentifier(id) => id.name.as_str(), - PropertyKey::StringLiteral(text) => text.value.as_str()?, - _ => return None, - }; - (key != PROTO_KEY).then_some(key) -} - -fn static_properties<'p>( - object: &'p ObjectExpression<'p>, -) -> Option)>> { - object - .properties - .iter() - .map(|property| match property { - ObjectPropertyKind::ObjectProperty(property) => { - Some((static_key(property)?, &property.value)) - } - ObjectPropertyKind::SpreadProperty(_) => None, - }) - .collect() -} - -fn static_property<'p>(object: &'p ObjectExpression<'p>, name: &str) -> Option<&'p Expression<'p>> { - static_properties(object)? - .into_iter() - .rev() - .find(|(key, _)| *key == name) - .map(|(_, value)| value) -} - impl<'k, 'p> Known<'k, 'p> { fn own(&self, member: &Expression<'_>) -> Option> { let (_, name) = member_parts(member)?; @@ -232,7 +182,7 @@ impl<'k, 'p> Known<'k, 'p> { match inner { Expression::Identifier(id) if !env.is_empty() => match symbol_of(id, self.scoping) { Some(symbol) => env.get(&symbol).cloned(), - None => fold_to_const(inner, &FoldOptions::default()), + None => self.folded(inner, &FoldOptions::default()), }, Expression::CallExpression(call) if !call.optional => self.call(call, env, depth), Expression::BinaryExpression(binary) => fold_binary( @@ -247,12 +197,39 @@ impl<'k, 'p> Known<'k, 'p> { ) } _ if references_are_global(inner, self.scoping) => { - fold_to_const(inner, &FoldOptions::default()) + self.folded(inner, &FoldOptions::default()) } _ => None, } } + fn folded(&self, expr: &Expression<'_>, options: &FoldOptions) -> Option { + if !self.builtins_unpatched + && touches_builtins(expr, self.scoping) + && !self.branches.builtins_intact(self.scoping) + { + return None; + } + fold_to_const(expr, options) + } + + fn folded_own_body(&self, function: &Function<'_>, arguments: &[Const]) -> Option { + let names = param_names(function)?; + if names.len() != arguments.len() + || names.iter().any(|name| FOLDED_GLOBALS.contains(name)) + || !reads_only_own_or_globals(function, self.scoping) + { + return None; + } + let mut options = FoldOptions::default(); + for (name, argument) in names.iter().zip(arguments) { + options + .bindings + .insert((*name).to_string(), argument.clone()); + } + self.folded(returned_expression(function)?, &options) + } + fn call(&self, call: &CallExpression<'_>, env: &Env, depth: usize) -> Option { let Resolved::Function(function) = self.member(&call.callee, depth)? else { return None; @@ -264,7 +241,7 @@ impl<'k, 'p> Known<'k, 'p> { for argument in &call.arguments { arguments.push(self.value(argument.as_expression()?, env, depth + 1)?); } - if let Some(value) = folded_own_body(function, &arguments, self.scoping) { + if let Some(value) = self.folded_own_body(function, &arguments) { return Some(value); } let params = param_symbols(function)?; @@ -279,27 +256,6 @@ impl<'k, 'p> Known<'k, 'p> { } } -fn folded_own_body( - function: &Function<'_>, - arguments: &[Const], - scoping: &Scoping, -) -> Option { - let names = param_names(function)?; - if names.len() != arguments.len() - || names.iter().any(|name| FOLDED_GLOBALS.contains(name)) - || !reads_only_own_or_globals(function, scoping) - { - return None; - } - let mut options = FoldOptions::default(); - for (name, argument) in names.iter().zip(arguments) { - options - .bindings - .insert((*name).to_string(), argument.clone()); - } - fold_to_const(returned_expression(function)?, &options) -} - fn decide<'p>( test: &Expression<'p>, symbol: SymbolId, @@ -346,61 +302,6 @@ fn mentioned(object: &ObjectExpression<'_>, scoping: &Scoping) -> FxHashSet VisitJs<'a> for ReceiverUses { - fn visit_this_expression(&mut self, _: &ThisExpression) { - self.found = true; - } - - fn visit_super(&mut self, _: &Super) { - self.found = true; - } - - fn visit_identifier_reference(&mut self, it: &IdentifierReference<'a>) { - if matches!(it.name.as_str(), "arguments" | "eval") { - self.found = true; - } - } -} - -fn reaches_its_receiver(object: &ObjectExpression<'_>) -> bool { - let mut scan = ReceiverUses { found: false }; - scan.visit_object_expression(object); - scan.found -} - -fn reads_only_own_or_absent_keys( - object: &ObjectExpression<'_>, - uses: &ObjectUses, - builtins_unpatched: bool, -) -> bool { - let Some(keys) = static_properties(object) else { - return false; - }; - let own: FxHashSet<&str> = keys.into_iter().map(|(key, _)| key).collect(); - uses.reads.iter().all(|read| { - own.contains(read.name.as_str()) - || (builtins_unpatched && !OBJECT_PROTOTYPE_KEYS.contains(&read.name.as_str())) - }) -} - -fn proven_readonly( - program: &Program<'_>, - object: &ObjectExpression<'_>, - scoping: &Scoping, - receiver: SymbolId, - builtins_unpatched: bool, -) -> bool { - if reaches_its_receiver(object) { - return false; - } - readonly_binding_uses(program, scoping, receiver) - .is_some_and(|uses| reads_only_own_or_absent_keys(object, &uses, builtins_unpatched)) -} - #[derive(Clone, Copy)] pub(super) struct DeadPart { node: usize, @@ -437,6 +338,7 @@ pub(super) struct Branches<'p> { program: Option<&'p Program<'p>>, readonly: RefCell>, reached: RefCell>, + builtins: OnceCell, } impl<'p> Branches<'p> { @@ -461,6 +363,13 @@ impl<'p> Branches<'p> { readonly.then_some(object) } + fn builtins_intact(&self, scoping: &Scoping) -> bool { + *self.builtins.get_or_init(|| { + self.program + .is_some_and(|program| builtins_proven_intact(program, scoping)) + }) + } + fn reaches_owner(&self, receiver: SymbolId, owner: SymbolId, scoping: &Scoping) -> bool { if let Some(known) = self.reached.borrow().get(&(receiver, owner)).copied() { return known; @@ -629,6 +538,11 @@ pub(super) fn predicate_branches<'p>(program: &'p Program<'p>, scoping: &Scoping found: Branches::default(), }; scan.visit_program(program); + if let Some(writes) = annex_b_written(program, scoping) { + scan.found + .objects + .retain(|symbol, _| !writes.may_write(*symbol)); + } scan.found.program = Some(program); scan.found } diff --git a/crates/untwist-deobfuscate/src/control_flow_object/mod.rs b/crates/untwist-deobfuscate/src/control_flow_object/mod.rs index 9812ee40..4bbb398e 100644 --- a/crates/untwist-deobfuscate/src/control_flow_object/mod.rs +++ b/crates/untwist-deobfuscate/src/control_flow_object/mod.rs @@ -31,6 +31,7 @@ mod hoisted; mod inner_first; mod keys; mod nested; +mod nested_proof; mod reach; mod sequenced; diff --git a/crates/untwist-deobfuscate/src/control_flow_object/nested_proof.rs b/crates/untwist-deobfuscate/src/control_flow_object/nested_proof.rs new file mode 100644 index 00000000..539847b7 --- /dev/null +++ b/crates/untwist-deobfuscate/src/control_flow_object/nested_proof.rs @@ -0,0 +1,268 @@ +use oxc_ast::ast::*; +use oxc_ast_visit::{walk_js, VisitJs}; +use oxc_semantic::Scoping; +use oxc_span::{GetSpan, Span}; +use oxc_syntax::symbol::SymbolId; +use rustc_hash::FxHashSet; +use untwist_core::ast_util::readonly::{readonly_binding_uses, ObjectUses}; +use untwist_core::semantic::builtins::{intact, Reliance}; +use untwist_core::semantic::{has_module_syntax, AnnexBWrites}; + +use std::sync::OnceLock; + +const PROTO_KEY: &str = "__proto__"; + +const OBJECT_PROTOTYPE_KEYS: [&str; 12] = [ + "constructor", + "hasOwnProperty", + "isPrototypeOf", + "propertyIsEnumerable", + "toLocaleString", + "toString", + "valueOf", + PROTO_KEY, + "__defineGetter__", + "__defineSetter__", + "__lookupGetter__", + "__lookupSetter__", +]; + +const FOLDED_GLOBALS: [&str; 3] = ["undefined", "NaN", "Infinity"]; + +fn is_plain_own_key(key: &str) -> bool { + !OBJECT_PROTOTYPE_KEYS.contains(&key) +} + +fn static_key<'p>(property: &'p ObjectProperty<'p>) -> Option<&'p str> { + if property.method || property.computed || property.kind != PropertyKind::Init { + return None; + } + let key = match &property.key { + PropertyKey::StaticIdentifier(id) => id.name.as_str(), + PropertyKey::StringLiteral(text) => text.value.as_str()?, + _ => return None, + }; + is_plain_own_key(key).then_some(key) +} + +pub(super) fn static_properties<'p>( + object: &'p ObjectExpression<'p>, +) -> Option)>> { + object + .properties + .iter() + .map(|property| match property { + ObjectPropertyKind::ObjectProperty(property) => { + Some((static_key(property)?, &property.value)) + } + ObjectPropertyKind::SpreadProperty(_) => None, + }) + .collect() +} + +pub(super) fn static_property<'p>( + object: &'p ObjectExpression<'p>, + name: &str, +) -> Option<&'p Expression<'p>> { + static_properties(object)? + .into_iter() + .rev() + .find(|(key, _)| *key == name) + .map(|(_, value)| value) +} + +struct ReceiverUses { + found: bool, +} + +impl<'a> VisitJs<'a> for ReceiverUses { + fn visit_this_expression(&mut self, _: &ThisExpression) { + self.found = true; + } + + fn visit_super(&mut self, _: &Super) { + self.found = true; + } + + fn visit_identifier_reference(&mut self, it: &IdentifierReference<'a>) { + if matches!(it.name.as_str(), "arguments" | "eval") { + self.found = true; + } + } +} + +fn reaches_its_receiver(object: &ObjectExpression<'_>) -> bool { + let mut scan = ReceiverUses { found: false }; + scan.visit_object_expression(object); + scan.found +} + +fn is_written_function(value: &Expression<'_>) -> bool { + matches!( + value.without_parentheses(), + Expression::FunctionExpression(_) | Expression::ArrowFunctionExpression(_) + ) +} + +struct Callees<'s> { + scoping: &'s Scoping, + receiver: SymbolId, + spans: FxHashSet, +} + +impl Callees<'_> { + fn record(&mut self, callee: &Expression<'_>) { + let callee = callee.without_parentheses(); + let Some(member) = callee.as_member_expression() else { + return; + }; + let Expression::Identifier(object) = member.object().without_parentheses() else { + return; + }; + let symbol = object + .reference_id + .get() + .and_then(|reference| self.scoping.get_reference(reference).symbol_id()); + if symbol == Some(self.receiver) { + self.spans.insert(callee.span()); + } + } +} + +impl<'a> VisitJs<'a> for Callees<'_> { + fn visit_call_expression(&mut self, it: &CallExpression<'a>) { + self.record(&it.callee); + walk_js::walk_call_expression(self, it); + } + + fn visit_new_expression(&mut self, it: &NewExpression<'a>) { + self.record(&it.callee); + walk_js::walk_new_expression(self, it); + } + + fn visit_tagged_template_expression(&mut self, it: &TaggedTemplateExpression<'a>) { + self.record(&it.tag); + walk_js::walk_tagged_template_expression(self, it); + } +} + +fn callee_spans(program: &Program<'_>, scoping: &Scoping, receiver: SymbolId) -> FxHashSet { + let mut scan = Callees { + scoping, + receiver, + spans: FxHashSet::default(), + }; + scan.visit_program(program); + scan.spans +} + +fn reads_are_proven( + object: &ObjectExpression<'_>, + uses: &ObjectUses, + callees: &FxHashSet, + builtins_unpatched: bool, +) -> bool { + let Some(keys) = static_properties(object) else { + return false; + }; + let value_of = |name: &str| { + keys.iter() + .rev() + .find(|(key, _)| *key == name) + .map(|(_, value)| *value) + }; + uses.reads.iter().all(|read| { + let name = read.name.as_str(); + match value_of(name) { + Some(value) => !callees.contains(&read.member_span) || is_written_function(value), + None => builtins_unpatched && is_plain_own_key(name), + } + }) +} + +pub(super) fn proven_readonly( + program: &Program<'_>, + object: &ObjectExpression<'_>, + scoping: &Scoping, + receiver: SymbolId, + builtins_unpatched: bool, +) -> bool { + if reaches_its_receiver(object) { + return false; + } + let Some(uses) = readonly_binding_uses(program, scoping, receiver) else { + return false; + }; + let callees = callee_spans(program, scoping, receiver); + reads_are_proven(object, &uses, &callees, builtins_unpatched) +} + +pub(super) fn annex_b_written(program: &Program<'_>, scoping: &Scoping) -> Option { + if crate::mutants::annex_b_writes_trusted() { + return None; + } + let writes = AnnexBWrites::of(program, scoping); + (!writes.is_empty()).then_some(writes) +} + +struct BuiltinTouch<'s> { + scoping: &'s Scoping, + found: bool, +} + +impl<'a> VisitJs<'a> for BuiltinTouch<'_> { + fn visit_expression(&mut self, it: &Expression<'a>) { + match it { + Expression::BooleanLiteral(_) + | Expression::NullLiteral(_) + | Expression::NumericLiteral(_) + | Expression::StringLiteral(_) + | Expression::TemplateLiteral(_) + | Expression::Identifier(_) + | Expression::ParenthesizedExpression(_) + | Expression::UnaryExpression(_) + | Expression::BinaryExpression(_) + | Expression::LogicalExpression(_) + | Expression::ConditionalExpression(_) + | Expression::SequenceExpression(_) => walk_js::walk_expression(self, it), + _ => self.found = true, + } + } + + fn visit_identifier_reference(&mut self, it: &IdentifierReference<'a>) { + let unresolved = it + .reference_id + .get() + .and_then(|reference| self.scoping.get_reference(reference).symbol_id()) + .is_none(); + if unresolved && !FOLDED_GLOBALS.contains(&it.name.as_str()) { + self.found = true; + } + } +} + +pub(super) fn touches_builtins(expr: &Expression<'_>, scoping: &Scoping) -> bool { + let mut scan = BuiltinTouch { + scoping, + found: false, + }; + scan.visit_expression(expr); + scan.found +} + +fn every_builtin() -> &'static Reliance { + static RELIANCE: OnceLock = OnceLock::new(); + RELIANCE.get_or_init(|| Reliance { + roots: untwist_eval::builtins::all_root_names(), + members: &[], + }) +} + +pub(super) fn builtins_proven_intact(program: &Program<'_>, scoping: &Scoping) -> bool { + intact( + program, + scoping, + has_module_syntax(program), + every_builtin(), + ) +} diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-annexb-if.js b/crates/untwist/tests/data/obfuscator_sweep/r5-annexb-if.js new file mode 100644 index 00000000..7d3096ba --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-annexb-if.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + var o = { k: U.a }; + if (U.a) { function o() {} } + if (o.k !== "x") { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-annexb-nested-call.js b/crates/untwist/tests/data/obfuscator_sweep/r5-annexb-nested-call.js new file mode 100644 index 00000000..9659fcee --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-annexb-nested-call.js @@ -0,0 +1,9 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + var o = { k: U.a }; + function h() { if (o.k !== "x") { U.b = "changed"; } } + { function o() {} } + h(); + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-annexb-switch.js b/crates/untwist/tests/data/obfuscator_sweep/r5-annexb-switch.js new file mode 100644 index 00000000..b62a55fa --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-annexb-switch.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + var o = { k: U.a }; + switch (1) { case 1: function o() {} } + if (o.k !== "x") { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-b-alias-patch.js b/crates/untwist/tests/data/obfuscator_sweep/r5-b-alias-patch.js new file mode 100644 index 00000000..8c9247b2 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-b-alias-patch.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + var S = "".constructor; S.prototype.trim = function () { return "q"; }; + const o = { k: U.a, t: function (p) { return p.trim() === "x"; } }; + if (o.t(o.k)) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-b-atob.js b/crates/untwist/tests/data/obfuscator_sweep/r5-b-atob.js new file mode 100644 index 00000000..305d56f3 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-b-atob.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + globalThis.atob = function () { return "q"; }; + const o = { k: U.a, t: function (p) { return atob("eA==") === p; } }; + if (o.t(o.k)) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-b-escape.js b/crates/untwist/tests/data/obfuscator_sweep/r5-b-escape.js new file mode 100644 index 00000000..db8a3a19 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-b-escape.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + globalThis.escape = function () { return "q"; }; + const o = { k: U.a, t: function (p) { return escape(p) === "x"; } }; + if (o.t(o.k)) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-b-fromcc.js b/crates/untwist/tests/data/obfuscator_sweep/r5-b-fromcc.js new file mode 100644 index 00000000..8f403466 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-b-fromcc.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + String.fromCharCode = function () { return "q"; }; + const o = { k: U.a, t: function (p) { return String.fromCharCode(120) === p; } }; + if (o.t(o.k)) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-b-json.js b/crates/untwist/tests/data/obfuscator_sweep/r5-b-json.js new file mode 100644 index 00000000..227a6489 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-b-json.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + JSON.stringify = function () { return "q"; }; + const o = { k: U.a, t: function (p) { return JSON.stringify(p) === "\"x\""; } }; + if (o.t(o.k)) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-b-math.js b/crates/untwist/tests/data/obfuscator_sweep/r5-b-math.js new file mode 100644 index 00000000..a2f2ace3 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-b-math.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + Math.floor = function () { return 7; }; + const o = { k: U.a, t: function (p) { return Math.floor(1.5) === 1 && p === "x"; } }; + if (o.t(o.k)) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-b-parseint.js b/crates/untwist/tests/data/obfuscator_sweep/r5-b-parseint.js new file mode 100644 index 00000000..0b46f9ec --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-b-parseint.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + parseInt = function () { return 7; }; + const o = { k: U.a, t: function (p) { return parseInt("1" + p) === 1; } }; + if (o.t(o.k)) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-b-string-fn.js b/crates/untwist/tests/data/obfuscator_sweep/r5-b-string-fn.js new file mode 100644 index 00000000..2dfa0376 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-b-string-fn.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + String = function () { return "q"; }; + const o = { k: U.a, t: function (p) { return String(p) === "x"; } }; + if (o.t(o.k)) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-body-proto-read.js b/crates/untwist/tests/data/obfuscator_sweep/r5-body-proto-read.js new file mode 100644 index 00000000..a2482fea --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-body-proto-read.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + String.prototype.foo = "x"; + const o = { k: U.a, t: function (p) { return p.foo === undefined; } }; + if (o.t(o.k)) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-body-valueof.js b/crates/untwist/tests/data/obfuscator_sweep/r5-body-valueof.js new file mode 100644 index 00000000..4f5a96a3 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-body-valueof.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + Number.prototype.toString = function () { return "q"; }; + const o = { k: U.a, t: function (p) { return (1).toString() + p === "1x"; } }; + if (o.t(o.k)) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-chain-three.js b/crates/untwist/tests/data/obfuscator_sweep/r5-chain-three.js new file mode 100644 index 00000000..93044055 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-chain-three.js @@ -0,0 +1,11 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + function g() { this.v = "z"; } + const p = { v: U.a, t: g }; + const o = { k: p.v }; + p.t(); + const o2 = { k: p.v }; + if (o2.k !== "x") { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-f1.js b/crates/untwist/tests/data/obfuscator_sweep/r5-f1.js new file mode 100644 index 00000000..b42d44c2 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-f1.js @@ -0,0 +1,9 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + function g() { this.k = "z"; } + const o = { k: U.a, t: g }; + o.t(); + if (o.k !== "x") { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-f2a.js b/crates/untwist/tests/data/obfuscator_sweep/r5-f2a.js new file mode 100644 index 00000000..5f01bb3f --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-f2a.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + String.prototype.charAt = function () { return "q"; }; + const o = { k: U.a, t: function (p) { return p.charAt(0) === "x"; } }; + if (o.t(o.k)) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-f2b.js b/crates/untwist/tests/data/obfuscator_sweep/r5-f2b.js new file mode 100644 index 00000000..e84d349f --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-f2b.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + String.prototype.foo = "x"; + const o = { k: U.a, j: "q".foo }; + if (o.j === undefined) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-f3.js b/crates/untwist/tests/data/obfuscator_sweep/r5-f3.js new file mode 100644 index 00000000..4458f8f0 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-f3.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + var o = { k: U.a }; + { function o() {} } + if (o.k !== "x") { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-owner-member-undef.js b/crates/untwist/tests/data/obfuscator_sweep/r5-owner-member-undef.js new file mode 100644 index 00000000..e13f8f78 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-owner-member-undef.js @@ -0,0 +1,7 @@ +function f() { + String.prototype.foo = "x"; + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + if (U.eq("q".foo, undefined)) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-t-cond.js b/crates/untwist/tests/data/obfuscator_sweep/r5-t-cond.js new file mode 100644 index 00000000..ff443fce --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-t-cond.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + String.prototype.foo = "x"; + const o = { k: U.a, e: function (p, q) { return p === q; } }; + if (o.e("q".foo ? 1 : 2, 2)) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-t-idx.js b/crates/untwist/tests/data/obfuscator_sweep/r5-t-idx.js new file mode 100644 index 00000000..517f0fa9 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-t-idx.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + String.prototype[5] = "x"; + const o = { k: U.a, e: function (p, q) { return p === q; } }; + if (o.e("q"[5], undefined)) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-t-member-undef.js b/crates/untwist/tests/data/obfuscator_sweep/r5-t-member-undef.js new file mode 100644 index 00000000..254f0766 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-t-member-undef.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + String.prototype.foo = "x"; + const o = { k: U.a, e: function (p, q) { return p === q; } }; + if (o.e("q".foo, undefined) ) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-t-num-member.js b/crates/untwist/tests/data/obfuscator_sweep/r5-t-num-member.js new file mode 100644 index 00000000..df7f5b44 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-t-num-member.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + Number.prototype.foo = "x"; + const o = { k: U.a, e: function (p, q) { return p === q; } }; + if (o.e((1).foo, undefined)) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-t-typeof.js b/crates/untwist/tests/data/obfuscator_sweep/r5-t-typeof.js new file mode 100644 index 00000000..6fa18333 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-t-typeof.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + String.prototype.foo = "x"; + const o = { k: U.a, e: function (p, q) { return p === q; } }; + if (o.e(typeof "q".foo, "undefined")) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-tagged.js b/crates/untwist/tests/data/obfuscator_sweep/r5-tagged.js new file mode 100644 index 00000000..a884b4f9 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-tagged.js @@ -0,0 +1,9 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + function g() { this.k = "z"; } + const o = { k: U.a, t: g }; + o.t``; + if (o.k !== "x") { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-this-after-test.js b/crates/untwist/tests/data/obfuscator_sweep/r5-this-after-test.js new file mode 100644 index 00000000..fa6cee6e --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-this-after-test.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + function g() { this.k = "z"; } + const o = { k: U.a, t: g }; + for (var i = 0; i < 2; i++) { if (o.k !== "x") { U.b = "changed"; } o.t(); } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-this-builtin.js b/crates/untwist/tests/data/obfuscator_sweep/r5-this-builtin.js new file mode 100644 index 00000000..fbecb7cc --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-this-builtin.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + const o = { k: U.a, t: Object.prototype.__defineGetter__ }; + o.t("k", function () { return "z"; }); + if (o.k !== "x") { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-this-delete.js b/crates/untwist/tests/data/obfuscator_sweep/r5-this-delete.js new file mode 100644 index 00000000..23c116fa --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-this-delete.js @@ -0,0 +1,9 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + function g() { delete this.k; } + const o = { k: U.a, t: g }; + o.t(); + if (o.k === "x") {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-this-factory.js b/crates/untwist/tests/data/obfuscator_sweep/r5-this-factory.js new file mode 100644 index 00000000..bcc5f83b --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-this-factory.js @@ -0,0 +1,9 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + function mkf() { return function () { this.k = "z"; }; } + const o = { k: U.a, t: mkf() }; + o.t(); + if (o.k !== "x") { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-this-member.js b/crates/untwist/tests/data/obfuscator_sweep/r5-this-member.js new file mode 100644 index 00000000..df71bf98 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-this-member.js @@ -0,0 +1,9 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + const G = { s: function () { this.k = "z"; } }; + const o = { k: U.a, t: G.s }; + o.t(); + if (o.k !== "x") { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-this-param.js b/crates/untwist/tests/data/obfuscator_sweep/r5-this-param.js new file mode 100644 index 00000000..0d9f78d2 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-this-param.js @@ -0,0 +1,10 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + (function (g) { + const o = { k: U.a, t: g }; + o.t(); + if (o.k !== "x") { U.b = "changed"; } + })(function () { this.k = "z"; }); + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-this-ternary.js b/crates/untwist/tests/data/obfuscator_sweep/r5-this-ternary.js new file mode 100644 index 00000000..fe8b4674 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-this-ternary.js @@ -0,0 +1,9 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + function g() { this.k = "z"; } + const o = { k: U.a, t: g }; + o.t(); + console.log(o.k !== "x" ? (U.b = "changed") : "kept"); + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-v-member.js b/crates/untwist/tests/data/obfuscator_sweep/r5-v-member.js new file mode 100644 index 00000000..e84d349f --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-v-member.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + String.prototype.foo = "x"; + const o = { k: U.a, j: "q".foo }; + if (o.j === undefined) {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-v-typeof.js b/crates/untwist/tests/data/obfuscator_sweep/r5-v-typeof.js new file mode 100644 index 00000000..3cd57703 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-v-typeof.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + String.prototype.foo = "x"; + const o = { k: U.a, j: typeof "q".foo }; + if (o.j === "undefined") {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/r5-valueof-own.js b/crates/untwist/tests/data/obfuscator_sweep/r5-valueof-own.js new file mode 100644 index 00000000..23eecf7e --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/r5-valueof-own.js @@ -0,0 +1,8 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + const o = { k: U.a, t: Object.prototype.valueOf }; + o.t().k = "z"; + if (o.k !== "x") { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/obfuscator_sweep_rows.rs b/crates/untwist/tests/obfuscator_sweep_rows.rs index feab2c36..7c909e29 100644 --- a/crates/untwist/tests/obfuscator_sweep_rows.rs +++ b/crates/untwist/tests/obfuscator_sweep_rows.rs @@ -431,3 +431,73 @@ fn a_call_of_a_key_no_object_has_still_decides_the_branch_under_unsafe() { assert!(!output.contains("missing"), "{output}"); assert!(default_code(&source).contains("missing")); } + +const NESTED_HAZARDS_BOTH_LANES: &[&str] = &[ + "r5-f1", + "r5-this-factory", + "r5-this-member", + "r5-this-param", + "r5-this-builtin", + "r5-valueof-own", + "r5-this-delete", + "r5-tagged", + "r5-this-after-test", + "r5-chain-three", + "r5-this-ternary", + "r5-f3", + "r5-annexb-if", + "r5-annexb-switch", + "r5-annexb-nested-call", +]; + +const NESTED_HAZARDS_PATCHED_BUILTINS: &[&str] = &[ + "r5-f2a", + "r5-f2b", + "r5-owner-member-undef", + "r5-b-math", + "r5-b-parseint", + "r5-b-string-fn", + "r5-b-fromcc", + "r5-b-json", + "r5-b-atob", + "r5-b-escape", + "r5-b-alias-patch", + "r5-body-valueof", + "r5-body-proto-read", + "r5-t-typeof", + "r5-t-cond", + "r5-t-idx", + "r5-t-num-member", + "r5-t-member-undef", + "r5-v-typeof", + "r5-v-member", +]; + +fn prints_like_node_in(labels: &[&str], lanes: &[fn(&str) -> String]) { + let Some(node) = node_or_skip() else { + return; + }; + let dir = tempfile::tempdir().unwrap(); + let mut wrong = Vec::new(); + for label in labels { + let source = read(&format!("{label}.js")); + let expected = printed(&node, dir.path(), &source); + for lane in lanes { + let actual = printed(&node, dir.path(), &lane(&source)); + if expected.is_err() || expected != actual { + wrong.push(format!("{label}: {expected:?} vs {actual:?}")); + } + } + } + assert!(wrong.is_empty(), "{}", wrong.join("\n")); +} + +#[test] +fn a_nested_object_a_call_or_a_block_function_can_change_prints_like_node_in_both_lanes() { + prints_like_node_in(NESTED_HAZARDS_BOTH_LANES, &[default_code, unsafe_lane]); +} + +#[test] +fn a_fold_through_a_built_in_the_program_patches_prints_like_node_in_the_default_lane() { + prints_like_node_in(NESTED_HAZARDS_PATCHED_BUILTINS, &[default_code]); +} diff --git a/web/scripts/size.ts b/web/scripts/size.ts index f2c55191..41eb41df 100644 --- a/web/scripts/size.ts +++ b/web/scripts/size.ts @@ -12,7 +12,7 @@ const INITIAL_JS_GZIP = 190_000; const CSS_GZIP = 15_000; const FONT_BYTES = 120_000; const SW_GZIP = 4_000; -const WASM_BASELINE_BYTES = 14_720_000; +const WASM_BASELINE_BYTES = 14_740_000; const WASM_GROWTH = 0.1; const gzip = (bytes: Buffer): number => gzipSync(bytes, { level: 9 }).length;