Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 123 additions & 25 deletions crates/untwist-deobfuscate/src/control_flow_object/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -116,25 +117,56 @@ fn param_symbols(function: &Function<'_>) -> Option<Vec<SymbolId>> {
.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<Vec<(&'p str, &'p Expression<'p>)>> {
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> {
Expand All @@ -160,12 +192,18 @@ impl<'k, 'p> Known<'k, 'p> {
member: &Expression<'_>,
depth: usize,
) -> Option<Resolved<'k, 'p>> {
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),
Expand Down Expand Up @@ -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)?);
Expand Down Expand Up @@ -271,6 +312,7 @@ fn decide<'p>(
props,
scoping: cx.scoping(),
branches,
builtins_unpatched: cx.opts.unsafe_transforms,
};
known
.value(test, &Env::default(), 0)
Expand Down Expand Up @@ -304,6 +346,61 @@ fn mentioned(object: &ObjectExpression<'_>, scoping: &Scoping) -> FxHashSet<Symb
scan.found
}

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 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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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();
74 changes: 74 additions & 0 deletions crates/untwist/tests/obfuscator_sweep_rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Loading