diff --git a/crates/untwist-deobfuscate/src/control_flow_object/dead.rs b/crates/untwist-deobfuscate/src/control_flow_object/dead.rs index 3f826be1..3c9c7c1d 100644 --- a/crates/untwist-deobfuscate/src/control_flow_object/dead.rs +++ b/crates/untwist-deobfuscate/src/control_flow_object/dead.rs @@ -7,9 +7,9 @@ use oxc_syntax::operator::UnaryOperator; use oxc_syntax::symbol::SymbolId; use rustc_hash::{FxHashMap, FxHashSet}; use untwist_core::ast_util::global::{references_are_global, references_are_global_or}; -use untwist_core::ast_util::inline::{param_names, returned_expression}; +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; +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}; @@ -84,6 +84,7 @@ struct Known<'k, 'p> { props: &'k [(Cow<'k, str>, CffValue<'k, 'p>)], scoping: &'k Scoping, branches: &'k Branches<'p>, + builtins_unpatched: bool, } enum Resolved<'k, 'p> { @@ -116,25 +117,56 @@ fn param_symbols(function: &Function<'_>) -> Option> { .collect() } -fn static_property<'p>(object: &'p ObjectExpression<'p>, name: &str) -> Option<&'p Expression<'p>> { - let mut found = None; - for property in &object.properties { - let ObjectPropertyKind::ObjectProperty(property) = property else { - return None; - }; - if property.method || property.kind != PropertyKind::Init { - return None; - } - let key = match &property.key { - PropertyKey::StaticIdentifier(id) if !property.computed => id.name.as_str(), - PropertyKey::StringLiteral(text) => text.value.as_str()?, - _ => return None, - }; - if key == name { - found = Some(&property.value); - } +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; } - found + 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> { @@ -160,12 +192,18 @@ impl<'k, 'p> Known<'k, 'p> { member: &Expression<'_>, depth: usize, ) -> Option> { - let object = self - .branches - .readonly_object(receiver, self.symbol, self.scoping)?; + let object = self.branches.readonly_object( + receiver, + self.symbol, + self.scoping, + self.builtins_unpatched, + )?; let (_, name) = member_parts(member)?; match static_property(object, name.as_ref())?.without_parentheses() { - Expression::FunctionExpression(function) => Some(Resolved::Function(function)), + Expression::FunctionExpression(function) if is_plain_function(function) => { + Some(Resolved::Function(function)) + } + Expression::FunctionExpression(_) => None, value => self .value(value, &Env::default(), depth + 1) .map(Resolved::Value), @@ -219,6 +257,9 @@ impl<'k, 'p> Known<'k, 'p> { let Resolved::Function(function) = self.member(&call.callee, depth)? else { return None; }; + if !is_plain_function(function) { + return None; + } let mut arguments = Vec::with_capacity(call.arguments.len()); for argument in &call.arguments { arguments.push(self.value(argument.as_expression()?, env, depth + 1)?); @@ -271,6 +312,7 @@ fn decide<'p>( props, scoping: cx.scoping(), branches, + builtins_unpatched: cx.opts.unsafe_transforms, }; known .value(test, &Env::default(), 0) @@ -304,6 +346,61 @@ 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, @@ -348,6 +445,7 @@ impl<'p> Branches<'p> { receiver: SymbolId, owner: SymbolId, scoping: &Scoping, + builtins_unpatched: bool, ) -> Option<&'p ObjectExpression<'p>> { let object = *self.objects.get(&receiver)?; if !self.reaches_owner(receiver, owner, scoping) { @@ -356,7 +454,7 @@ impl<'p> Branches<'p> { let program = self.program?; let known = self.readonly.borrow().get(&receiver).copied(); let readonly = known.unwrap_or_else(|| { - let readonly = readonly_binding_uses(program, scoping, receiver).is_some(); + let readonly = proven_readonly(program, object, scoping, receiver, builtins_unpatched); self.readonly.borrow_mut().insert(receiver, readonly); readonly }); diff --git a/crates/untwist/tests/data/obfuscator_sweep/nested-absent-key-call.js b/crates/untwist/tests/data/obfuscator_sweep/nested-absent-key-call.js new file mode 100644 index 00000000..c3160c5e --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/nested-absent-key-call.js @@ -0,0 +1,7 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + const o = { k: U.a }; + if (o.k === "x") {} else { U.b = String.fromCharCode(o.missing(46)); } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/nested-async-property.js b/crates/untwist/tests/data/obfuscator_sweep/nested-async-property.js new file mode 100644 index 00000000..8aeeac4f --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/nested-async-property.js @@ -0,0 +1,7 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + const o = { k: U.a, t: async function (p, q) { return p === q; } }; + if (o.t(U.a, "nope")) { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/nested-generator-property.js b/crates/untwist/tests/data/obfuscator_sweep/nested-generator-property.js new file mode 100644 index 00000000..7a85a0f4 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/nested-generator-property.js @@ -0,0 +1,7 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + const o = { k: U.a, t: function* (p, q) { return p === q; } }; + if (o.t(U.a, "nope")) { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/nested-inherited-definegetter.js b/crates/untwist/tests/data/obfuscator_sweep/nested-inherited-definegetter.js new file mode 100644 index 00000000..09fb38d1 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/nested-inherited-definegetter.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 }; + o.__defineGetter__("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/nested-inherited-valueof.js b/crates/untwist/tests/data/obfuscator_sweep/nested-inherited-valueof.js new file mode 100644 index 00000000..8c6cf19e --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/nested-inherited-valueof.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 }; + o.valueOf().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/data/obfuscator_sweep/nested-patched-prototype.js b/crates/untwist/tests/data/obfuscator_sweep/nested-patched-prototype.js new file mode 100644 index 00000000..17e742e1 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/nested-patched-prototype.js @@ -0,0 +1,9 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + Object.prototype.zz = function () { this.k = "z"; }; + const o = { k: U.a }; + o.zz(); + 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/nested-plain-decided.js b/crates/untwist/tests/data/obfuscator_sweep/nested-plain-decided.js new file mode 100644 index 00000000..efc6ca23 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/nested-plain-decided.js @@ -0,0 +1,7 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + const o = { k: U.a, t: function (p, q) { return p === q; } }; + if (o.t(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/nested-proto-identifier-key.js b/crates/untwist/tests/data/obfuscator_sweep/nested-proto-identifier-key.js new file mode 100644 index 00000000..e72facdb --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/nested-proto-identifier-key.js @@ -0,0 +1,7 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + const o = { __proto__: "x", k: U.a }; + if (o.__proto__ === "x") {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/nested-proto-string-key.js b/crates/untwist/tests/data/obfuscator_sweep/nested-proto-string-key.js new file mode 100644 index 00000000..5ab60cc8 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/nested-proto-string-key.js @@ -0,0 +1,7 @@ +function f() { + const U = { a: "x", b: "y", eq: function (p, q) { return p === q; } }; + const o = { "__proto__": "x", k: U.a }; + if (o.__proto__ === "x") {} else { U.b = "changed"; } + console.log(U.b, U.eq(U.a, "x")); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/nested-this-arrow.js b/crates/untwist/tests/data/obfuscator_sweep/nested-this-arrow.js new file mode 100644 index 00000000..6af73b28 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/nested-this-arrow.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, set: function () { const g = () => { this.k = "z"; }; g(); } }; + o.set(); + 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/nested-this-returned.js b/crates/untwist/tests/data/obfuscator_sweep/nested-this-returned.js new file mode 100644 index 00000000..a870a9f3 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/nested-this-returned.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, self: function () { return this; } }; + o.self().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/data/obfuscator_sweep/nested-this-write.js b/crates/untwist/tests/data/obfuscator_sweep/nested-this-write.js new file mode 100644 index 00000000..d427f293 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/nested-this-write.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, set: function () { this.k = "z"; } }; + o.set(); + 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 e3535259..feab2c36 100644 --- a/crates/untwist/tests/obfuscator_sweep_rows.rs +++ b/crates/untwist/tests/obfuscator_sweep_rows.rs @@ -357,3 +357,77 @@ fn an_outer_string_array_with_one_decoded_entry_is_decoded_in_the_default_lane() assert!(!output.contains("_0x3980"), "{output}"); prints_the_same(&read("one-entry-outer-array.source.js"), &output); } + +const NESTED_OBJECT_HAZARDS: &[&str] = &[ + "nested-this-write", + "nested-this-returned", + "nested-this-arrow", + "nested-inherited-valueof", + "nested-inherited-definegetter", + "nested-async-property", + "nested-generator-property", + "nested-proto-string-key", + "nested-proto-identifier-key", +]; + +#[test] +fn a_nested_object_that_can_change_or_is_not_plain_keeps_the_branch_in_both_lanes() { + for label in NESTED_OBJECT_HAZARDS { + let source = read(&format!("{label}.js")); + for output in [default_code(&source), unsafe_lane(&source)] { + assert!(output.contains("\"changed\""), "{label}:\n{output}"); + } + } +} + +#[test] +fn a_nested_object_that_can_change_or_is_not_plain_prints_what_node_prints_in_both_lanes() { + let Some(node) = node_or_skip() else { + return; + }; + let dir = tempfile::tempdir().unwrap(); + for label in NESTED_OBJECT_HAZARDS { + let source = read(&format!("{label}.js")); + let expected = printed(&node, dir.path(), &source); + for output in [default_code(&source), unsafe_lane(&source)] { + let actual = printed(&node, dir.path(), &output); + assert!( + expected.is_ok() && expected == actual, + "{label}: {expected:?} vs {actual:?}" + ); + } + } +} + +#[test] +fn a_plain_read_only_nested_object_still_decides_the_branch_in_both_lanes() { + let source = read("nested-plain-decided.js"); + for output in [default_code(&source), unsafe_lane(&source)] { + assert!(!output.contains("\"changed\""), "{output}"); + } +} + +#[test] +fn a_nested_object_whose_prototype_the_program_may_patch_keeps_the_branch_in_the_default_lane() { + let source = read("nested-patched-prototype.js"); + let output = default_code(&source); + assert!(output.contains("\"changed\""), "{output}"); + let Some(node) = node_or_skip() else { + return; + }; + let dir = tempfile::tempdir().unwrap(); + let expected = printed(&node, dir.path(), &source); + let actual = printed(&node, dir.path(), &output); + assert!( + expected.is_ok() && expected == actual, + "{expected:?} vs {actual:?}" + ); +} + +#[test] +fn a_call_of_a_key_no_object_has_still_decides_the_branch_under_unsafe() { + let source = read("nested-absent-key-call.js"); + let output = unsafe_lane(&source); + assert!(!output.contains("missing"), "{output}"); + assert!(default_code(&source).contains("missing")); +} diff --git a/web/scripts/size.ts b/web/scripts/size.ts index 316425ed..1b7205e7 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_655_000; +const WASM_BASELINE_BYTES = 14_680_000; const WASM_GROWTH = 0.1; const gzip = (bytes: Buffer): number => gzipSync(bytes, { level: 9 }).length;