diff --git a/crates/untwist-deobfuscate/src/control_flow_object/assembled.rs b/crates/untwist-deobfuscate/src/control_flow_object/assembled.rs index b05bfde7..900f0055 100644 --- a/crates/untwist-deobfuscate/src/control_flow_object/assembled.rs +++ b/crates/untwist-deobfuscate/src/control_flow_object/assembled.rs @@ -233,6 +233,7 @@ fn this_free_function(value: &Expression<'_>) -> bool { struct AliasUses<'s, 'k, 'p> { scoping: &'s Scoping, objects: FxHashMap>, + growing_bases: FxHashSet, dead: &'k FxHashSet, builtins_unpatched: bool, depth: usize, @@ -297,9 +298,12 @@ impl<'a> VisitJs<'a> for AliasUses<'_, '_, '_> { fn visit_call_expression(&mut self, it: &CallExpression<'a>) { let callee = it.callee.without_parentheses(); if let Some((id, assembled)) = self.receiver(callee) { - let safe = member_parts(callee) - .and_then(|(_, name)| assembled.property(&name)) - .is_some_and(this_free_function); + let growing = resolved(id, self.scoping) + .is_some_and(|symbol| self.growing_bases.contains(&symbol)); + let safe = !growing + && member_parts(callee) + .and_then(|(_, name)| assembled.property(&name)) + .is_some_and(this_free_function); if !safe { self.risky.insert(address_of(id)); } @@ -354,6 +358,11 @@ where for (_, assembled) in &used { objects.entry(assembled.base).or_insert(*assembled); } + let growing_bases = used + .iter() + .filter(|(_, assembled)| assembled.adds_keys()) + .map(|(_, assembled)| assembled.base) + .collect(); let mut roots: Vec<&FunctionBody<'_>> = Vec::new(); let mut whole = false; for assembled in objects.values() { @@ -366,6 +375,7 @@ where let mut uses = AliasUses { scoping, objects, + growing_bases, dead, builtins_unpatched, depth: 0, diff --git a/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1a.js b/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1a.js new file mode 100644 index 00000000..246965ee --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1a.js @@ -0,0 +1,11 @@ +function setter() { this.kkkkk = "y"; } +function f() { + const d = { abcde: "x", fghij: function (a, b) { return a === b; } }; + const m = { kkkkk: d.abcde, fffff: setter }; + m.ggggg = m.fffff(); + m.fffff = function () { return 1; }; + const v = m; + if (v.kkkkk === "x") { console.log("then"); } else { d.abcde = "changed"; } + console.log(d.abcde); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1b.js b/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1b.js new file mode 100644 index 00000000..c2650caa --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1b.js @@ -0,0 +1,13 @@ +function setter() { this.kkkkk = "y"; } +function f() { + const d = { abcde: "x", fghij: function (a, b) { return a === b; } }; + const m = {}; + m.kkkkk = d.abcde; + m.fffff = setter; + m.ggggg = m.fffff(); + m.fffff = function () { return 1; }; + const v = m; + if (v.kkkkk === "x") { console.log("then"); } else { d.abcde = "changed"; } + console.log(d.abcde); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1c.js b/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1c.js new file mode 100644 index 00000000..46833a22 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1c.js @@ -0,0 +1,13 @@ +function setter() { this.kkkkk = "y"; return 1; } +function f() { + const d = { abcde: "x", fghij: function (a, b) { return a === b; } }; + const m = {}; + m.kkkkk = d.abcde; + m.fffff = setter; + m.ggggg = (function () { return m["fffff"](); })(); + m.fffff = function () { return 1; }; + const v = m; + if (d.fghij(v.kkkkk, "x")) { console.log("then"); } else { d.abcde = "changed"; } + console.log(d.abcde); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1d.js b/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1d.js new file mode 100644 index 00000000..0bc1e532 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1d.js @@ -0,0 +1,12 @@ +function setter() { this.kkkkk = "y"; return 1; } +function f() { + const d = { abcde: "x", fghij: function (a, b) { return a === b; } }; + const m = {}; + m.kkkkk = d.abcde; + m.fffff = setter; + m.ggggg = m.fffff(); + const v = m; + if (v.kkkkk === "x") { console.log("then"); } else { d.abcde = "changed"; } + console.log(d.abcde); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1e.js b/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1e.js new file mode 100644 index 00000000..eef9e646 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1e.js @@ -0,0 +1,15 @@ +function setter() { this.kkkkk = "y"; return 1; } +function f() { + const d = { abcde: "x", fghij: function (a, b) { return a === b; } }; + function call() { return m.fffff(); } + const m = {}; + m.kkkkk = d.abcde; + m.fffff = setter; + m.ggggg = call(); + m.fffff = function () { return 1; }; + const v = m; + call(); + if (v.kkkkk === "x") { console.log("then"); } else { d.abcde = "changed"; } + console.log(d.abcde); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1f.js b/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1f.js new file mode 100644 index 00000000..49af5589 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1f.js @@ -0,0 +1,13 @@ +function setter() { this.kkkkk = "y"; return 1; } +function f() { + const d = { abcde: "x", fghij: function (a, b) { return a === b; } }; + const m = {}; + m.kkkkk = d.abcde; + m.fffff = setter; + m.ggggg = m.fffff?.(); + m.fffff = function () { return 1; }; + const v = m; + v.kkkkk === "x" ? console.log("then") : (d.abcde = "changed"); + console.log(d.abcde); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1g.js b/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1g.js new file mode 100644 index 00000000..931e8651 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1g.js @@ -0,0 +1,13 @@ +function setter() { this.kkkkk = "y"; return 1; } +function f() { + const d = { abcde: "x", fghij: function (a, b) { return a === b; } }; + const m = {}; + m.kkkkk = d.abcde; + m.fffff = setter; + m.ggggg = m.fffff(); + m.fffff = function () { return this; }; + const v = m; + if (v.kkkkk === "x") { console.log("then"); } else { d.abcde = "changed"; } + console.log(d.abcde); +} +f(); diff --git a/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1h.js b/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1h.js new file mode 100644 index 00000000..e4332656 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/cfo-r7-h1h.js @@ -0,0 +1,12 @@ +function f(g) { + const d = { abcde: "x", fghij: function (a, b) { return a === b; } }; + const m = {}; + m.kkkkk = d.abcde; + m.fffff = g; + m.ggggg = m.fffff(); + m.fffff = function () { return 1; }; + const v = m; + if (v.kkkkk === "x") { console.log("then"); } else { console.log("else"); d.abcde = "changed"; } + console.log(d.abcde, v.kkkkk); +} +f(function () { delete this.kkkkk; }); diff --git a/crates/untwist/tests/obfuscator_sweep_rows.rs b/crates/untwist/tests/obfuscator_sweep_rows.rs index 83456fca..e86c4cf4 100644 --- a/crates/untwist/tests/obfuscator_sweep_rows.rs +++ b/crates/untwist/tests/obfuscator_sweep_rows.rs @@ -726,6 +726,35 @@ fn an_assembled_base_read_or_an_optional_method_call_prints_like_node_in_both_la ); } +const ASSEMBLED_BASE_CALLS_DURING_THE_RUN: &[&str] = &[ + "cfo-r7-h1a", + "cfo-r7-h1b", + "cfo-r7-h1c", + "cfo-r7-h1d", + "cfo-r7-h1e", + "cfo-r7-h1f", + "cfo-r7-h1g", + "cfo-r7-h1h", +]; + +#[test] +fn a_method_call_on_an_assembled_base_prints_like_node_in_both_lanes() { + prints_like_node_in( + ASSEMBLED_BASE_CALLS_DURING_THE_RUN, + &[default_code, unsafe_lane], + ); +} + +#[test] +fn a_method_call_on_an_assembled_base_keeps_the_branch_in_both_lanes() { + for label in ASSEMBLED_BASE_CALLS_DURING_THE_RUN { + let source = read(&format!("{label}.js")); + for output in [default_code(&source), unsafe_lane(&source)] { + assert!(output.contains("\"changed\""), "{label}:\n{output}"); + } + } +} + const BLOCK_ROTATOR_ESCAPES: &[&str] = &[ "block-rotator-return", "block-rotator-loop-var",