diff --git a/crates/untwist-cli/tests/browserify_review.rs b/crates/untwist-cli/tests/browserify_review.rs index c2877c13..68a8752c 100644 --- a/crates/untwist-cli/tests/browserify_review.rs +++ b/crates/untwist-cli/tests/browserify_review.rs @@ -26,13 +26,13 @@ const PREMISE_ROWS: [(&str, &[&str]); 16] = [ ("r5esbargv", UNSAFE), ("r5esbargvs", UNSAFE), ]; -const RUNNER_PREMISE_ROWS: [&str; 59] = [ +const RUNNER_PREMISE_ROWS: [&str; 61] = [ "r6bsc", "r6c1", "r6c2", "r6c3", "r6c4", "r6c5", "r6c6", "r6c7", "r6d1", "r6d2", "r6d3", "r6d4", "r6d5", "r6d6", "r6d7", "r6d8", "r6d9", "r6e1", "r6e10", "r6e11", "r6e12", "r6e5", "r6e6", "r6escs", "r6g1", "r6g10", "r6g2", "r6g3", "r6g4", "r6g5", "r6g6", "r6g7", "r6g8", "r6g9", "r6k1", "r6k2", "r6k3", "r6k5", "r6k6", "r6k7", "r6k8", "r6k9", "r6rdc", "r6t1", - "r6t2", "r6t3", "r6t4", "r6t5", "r7hv1", "r7n1", "r7n2", "r7n3", "r7rb1", "r7rb2", "r7rd1", - "r7rd2", "r7rd3", "r7sp1", "r7sp2", + "r6t2", "r6t3", "r6t4", "r6t5", "r7hv1", "r7l1", "r7l2", "r7n1", "r7n2", "r7n3", "r7rb1", + "r7rb2", "r7rd1", "r7rd2", "r7rd3", "r7sp1", "r7sp2", ]; const DISPATCH_PREMISE_ROWS: [&str; 7] = ["e1", "e2", "r4n10", "r5pr1", "r5pr2", "r5pr4", "r5pr11"]; const PROTOTYPE_PREMISE_ROWS: [&str; 9] = ["a27", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8"]; @@ -237,7 +237,7 @@ fn host_route_probes_run_like_their_bundles_in_both_lanes() { return; }; let inputs = inputs(ROUTE_INPUTS); - assert!(inputs.len() >= 162, "found {} inputs", inputs.len()); + assert!(inputs.len() >= 164, "found {} inputs", inputs.len()); let wrong = parity_failures(&node, &inputs); assert!(wrong.is_empty(), "{}", wrong.join("\n")); } diff --git a/crates/untwist-cli/tests/host_routes_review/r7l1.js b/crates/untwist-cli/tests/host_routes_review/r7l1.js new file mode 100644 index 00000000..553c6148 --- /dev/null +++ b/crates/untwist-cli/tests/host_routes_review/r7l1.js @@ -0,0 +1,108 @@ +"use strict"; +/******/ (function(modules) { // webpackBootstrap +/******/ // The module cache +/******/ var installedModules = {}; +/******/ +/******/ // object to store loaded chunks +/******/ // "0" means "already loaded" +/******/ var installedChunks = { +/******/ 0: 0 +/******/ }; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ +/******/ // Check if module is in cache +/******/ if(installedModules[moduleId]) { +/******/ return installedModules[moduleId].exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = installedModules[moduleId] = { +/******/ i: moduleId, +/******/ l: false, +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); +/******/ +/******/ // Flag the module as loaded +/******/ module.l = true; +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/******/ // The chunk loading function for additional chunks +/******/ // Since all referenced chunks are already included +/******/ // in this file, this function is empty here. +/******/ __webpack_require__.e = function requireEnsure() { +/******/ return Promise.resolve(); +/******/ }; +/******/ +/******/ // expose the modules object (__webpack_modules__) +/******/ __webpack_require__.m = modules; +/******/ +/******/ // expose the module cache +/******/ __webpack_require__.c = installedModules; +/******/ +/******/ // define getter function for harmony exports +/******/ __webpack_require__.d = function(exports, name, getter) { +/******/ if(!__webpack_require__.o(exports, name)) { +/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); +/******/ } +/******/ }; +/******/ +/******/ // define __esModule on exports +/******/ __webpack_require__.r = function(exports) { +/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { +/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); +/******/ } +/******/ Object.defineProperty(exports, '__esModule', { value: true }); +/******/ }; +/******/ +/******/ // create a fake namespace object +/******/ // mode & 1: value is a module id, require it +/******/ // mode & 2: merge all properties of value into the ns +/******/ // mode & 4: return value when already ns object +/******/ // mode & 8|1: behave like require +/******/ __webpack_require__.t = function(value, mode) { +/******/ if(mode & 1) value = __webpack_require__(value); +/******/ if(mode & 8) return value; +/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; +/******/ var ns = Object.create(null); +/******/ __webpack_require__.r(ns); +/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); +/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); +/******/ return ns; +/******/ }; +/******/ +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __webpack_require__.n = function(module) { +/******/ var getter = module && module.__esModule ? +/******/ function getDefault() { return module['default']; } : +/******/ function getModuleExports() { return module; }; +/******/ __webpack_require__.d(getter, 'a', getter); +/******/ return getter; +/******/ }; +/******/ +/******/ // Object.prototype.hasOwnProperty.call +/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; +/******/ +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = ""; +/******/ +/******/ // uncaught error handler for webpack runtime +/******/ __webpack_require__.oe = function(err) { +/******/ process.nextTick(function() { +/******/ throw err; // catch this error by using import().catch() +/******/ }); +/******/ }; +/******/ +/******/ +/******/ // Load entry module and return exports +return __webpack_require__(__webpack_require__.s = 0); +})([ +function(module, exports, __webpack_require__){ console.log(__webpack_require__(1)); var P = Object.getPrototypeOf(function(){}); var K = Object.getOwnPropertyNames(P).filter(function(n){return n.length==11})[0]; var C = "return import(\"./1.js\")"; var p = new Proxy({constructor: 1}, {get: (t, k) => P[k]}); Object.values(p)[0](C)().then(function(m){console.log("got", m.default||m)}, function(e){console.log("threw", e.code||e.message)}); }, +function(module, exports, __webpack_require__){ module.exports = "one"; } +]); diff --git a/crates/untwist-cli/tests/host_routes_review/r7l2.js b/crates/untwist-cli/tests/host_routes_review/r7l2.js new file mode 100644 index 00000000..3e6ae38d --- /dev/null +++ b/crates/untwist-cli/tests/host_routes_review/r7l2.js @@ -0,0 +1,108 @@ +"use strict"; +/******/ (function(modules) { // webpackBootstrap +/******/ // The module cache +/******/ var installedModules = {}; +/******/ +/******/ // object to store loaded chunks +/******/ // "0" means "already loaded" +/******/ var installedChunks = { +/******/ 0: 0 +/******/ }; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ +/******/ // Check if module is in cache +/******/ if(installedModules[moduleId]) { +/******/ return installedModules[moduleId].exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = installedModules[moduleId] = { +/******/ i: moduleId, +/******/ l: false, +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); +/******/ +/******/ // Flag the module as loaded +/******/ module.l = true; +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/******/ // The chunk loading function for additional chunks +/******/ // Since all referenced chunks are already included +/******/ // in this file, this function is empty here. +/******/ __webpack_require__.e = function requireEnsure() { +/******/ return Promise.resolve(); +/******/ }; +/******/ +/******/ // expose the modules object (__webpack_modules__) +/******/ __webpack_require__.m = modules; +/******/ +/******/ // expose the module cache +/******/ __webpack_require__.c = installedModules; +/******/ +/******/ // define getter function for harmony exports +/******/ __webpack_require__.d = function(exports, name, getter) { +/******/ if(!__webpack_require__.o(exports, name)) { +/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); +/******/ } +/******/ }; +/******/ +/******/ // define __esModule on exports +/******/ __webpack_require__.r = function(exports) { +/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { +/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); +/******/ } +/******/ Object.defineProperty(exports, '__esModule', { value: true }); +/******/ }; +/******/ +/******/ // create a fake namespace object +/******/ // mode & 1: value is a module id, require it +/******/ // mode & 2: merge all properties of value into the ns +/******/ // mode & 4: return value when already ns object +/******/ // mode & 8|1: behave like require +/******/ __webpack_require__.t = function(value, mode) { +/******/ if(mode & 1) value = __webpack_require__(value); +/******/ if(mode & 8) return value; +/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; +/******/ var ns = Object.create(null); +/******/ __webpack_require__.r(ns); +/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); +/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); +/******/ return ns; +/******/ }; +/******/ +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __webpack_require__.n = function(module) { +/******/ var getter = module && module.__esModule ? +/******/ function getDefault() { return module['default']; } : +/******/ function getModuleExports() { return module; }; +/******/ __webpack_require__.d(getter, 'a', getter); +/******/ return getter; +/******/ }; +/******/ +/******/ // Object.prototype.hasOwnProperty.call +/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; +/******/ +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = ""; +/******/ +/******/ // uncaught error handler for webpack runtime +/******/ __webpack_require__.oe = function(err) { +/******/ process.nextTick(function() { +/******/ throw err; // catch this error by using import().catch() +/******/ }); +/******/ }; +/******/ +/******/ +/******/ // Load entry module and return exports +return __webpack_require__(__webpack_require__.s = 0); +})([ +function(module, exports, __webpack_require__){ console.log(__webpack_require__(1)); var P = Object.getPrototypeOf(function(){}); var K = Object.getOwnPropertyNames(P).filter(function(n){return n.length==11})[0]; var C = "return import(\"./1.js\")"; var t = {}; Object.defineProperty(t, K, {enumerable: true, get: () => P[K]}); Object.values(t)[0](C)().then(function(m){console.log("got", m.default||m)}, function(e){console.log("threw", e.code||e.message)}); }, +function(module, exports, __webpack_require__){ module.exports = "one"; } +]); diff --git a/crates/untwist-unpack/src/host_keys.rs b/crates/untwist-unpack/src/host_keys.rs index 9ba634a3..6647cb32 100644 --- a/crates/untwist-unpack/src/host_keys.rs +++ b/crates/untwist-unpack/src/host_keys.rs @@ -98,7 +98,7 @@ impl<'a> Writes<'a> { } } - fn settled(&self, scoping: &Scoping, symbol: SymbolId) -> Option<&[Write<'a>]> { + pub fn settled(&self, scoping: &Scoping, symbol: SymbolId) -> Option<&[Write<'a>]> { if !self.declared.contains(&symbol) || self.loose.contains(&symbol) { return None; } @@ -358,7 +358,7 @@ impl<'a> Keys<'_, 'a> { }) } - fn lister(&self, callee: &Expression<'_>, alias: bool) -> bool { + pub fn lister(&self, callee: &Expression<'_>, alias: bool) -> bool { match callee.without_parentheses() { Expression::Identifier(id) if alias => self .symbol(id) diff --git a/crates/untwist-unpack/src/host_reach/carry.rs b/crates/untwist-unpack/src/host_reach/carry.rs new file mode 100644 index 00000000..72535b21 --- /dev/null +++ b/crates/untwist-unpack/src/host_reach/carry.rs @@ -0,0 +1,255 @@ +use oxc_ast::AstKind; +use oxc_syntax::operator::{AssignmentOperator, LogicalOperator, UnaryOperator}; +use oxc_syntax::symbol::SymbolId; + +use super::*; + +const ENUMERABLE: &str = "enumerable"; + +impl<'a> Reach<'_, 'a> { + pub(super) fn identifier_symbol(&self, expr: &Expression<'_>) -> Option { + match expr.without_parentheses() { + Expression::Identifier(id) => self.symbol(id), + _ => None, + } + } + + pub(super) fn read_source(&self, arrow: &ArrowFunctionExpression<'a>) -> Option { + match arrow.get_expression()?.without_parentheses() { + Expression::ComputedMemberExpression(member) => self.identifier_symbol(&member.object), + _ => None, + } + } + + pub(super) fn source_carries(&self, source: Option) -> Option> { + source.and_then(|symbol| self.tainted.get(&symbol).copied()) + } + + pub(super) fn carry( + &mut self, + target: Option<&'a Expression<'a>>, + carried: Option>, + ) -> bool { + match carried { + None => true, + Some(held) => target.is_some_and(|target| self.carries(target, held)), + } + } + + pub(super) fn carries(&mut self, target: &Expression<'a>, held: Held<'a>) -> bool { + match self.identifier_symbol(target) { + Some(symbol) => { + self.taint(symbol, held); + true + } + None => false, + } + } + + pub(super) fn getter_carries( + &mut self, + call: &'a CallExpression<'a>, + descriptor: &'a ObjectExpression<'a>, + source: Option, + key: &'a Expression<'a>, + ) -> bool { + let kept = source.is_some_and(|source| { + self.writes.stable(self.scoping, source) + && self.keeps_enumerability(descriptor, source, key) + }); + let carried = match kept { + true => self.source_carries(source), + false => Some(Held::Read(Some(key))), + }; + let target = call.arguments.first().and_then(Argument::as_expression); + self.carry(target, carried) + } + + fn keeps_enumerability( + &self, + descriptor: &ObjectExpression<'a>, + source: SymbolId, + key: &Expression<'a>, + ) -> bool { + let mut enumerable = None; + for property in &descriptor.properties { + let ObjectPropertyKind::ObjectProperty(property) = property else { + return false; + }; + if property.computed { + return false; + } + let named = property + .key + .static_name() + .is_some_and(|name| name.as_str() == Some(ENUMERABLE)); + if named { + enumerable = Some(&property.value); + } + } + let Some(value) = enumerable else { + return true; + }; + let hidden = + matches!(value.without_parentheses(), Expression::BooleanLiteral(flag) if !flag.value); + hidden || (self.own_flag(value, source, key) && self.own_key(key, source)) + } + + fn own_flag(&self, value: &Expression<'a>, source: SymbolId, key: &Expression<'a>) -> bool { + let Expression::LogicalExpression(logical) = value.without_parentheses() else { + return false; + }; + let Expression::UnaryExpression(not) = logical.left.without_parentheses() else { + return false; + }; + let Expression::AssignmentExpression(assign) = not.argument.without_parentheses() else { + return false; + }; + let AssignmentTarget::AssignmentTargetIdentifier(desc) = &assign.left else { + return false; + }; + let Expression::StaticMemberExpression(flag) = logical.right.without_parentheses() else { + return false; + }; + let desc = self.symbol(desc); + logical.operator == LogicalOperator::Or + && not.operator == UnaryOperator::LogicalNot + && assign.operator == AssignmentOperator::Assign + && desc.is_some() + && flag.property.name == ENUMERABLE + && self.identifier_symbol(&flag.object) == desc + && self.reads_own(&assign.right, source, key) + } + + fn reads_own(&self, read: &Expression<'a>, source: SymbolId, key: &Expression<'a>) -> bool { + let Expression::CallExpression(read) = read.without_parentheses() else { + return false; + }; + let reader = self.builtin_member(&read.callee, &[OBJECT], &[DESCRIPTOR]) + || self.alias(&read.callee, &self.writes.readers); + let arguments = match read.arguments.as_slice() { + [object, named] => object + .as_expression() + .zip(named.as_expression()) + .is_some_and(|(object, named)| { + self.identifier_symbol(object) == Some(source) + && self.keys().same_key(key, named) + }), + _ => false, + }; + reader && arguments + } + + fn own_key(&self, key: &Expression<'a>, source: SymbolId) -> bool { + let Some(symbol) = self.identifier_symbol(key) else { + return false; + }; + self.listed_by_loop(symbol, source) || self.listed_by_index(symbol, source) + } + + fn listed_by_loop(&self, symbol: SymbolId, source: SymbolId) -> bool { + let unwritten = self + .scoping + .get_resolved_reference_ids(symbol) + .iter() + .all(|reference| !self.scoping.get_reference(*reference).is_write()); + unwritten + && self.stack.iter().any(|kind| { + matches!(kind, AstKind::ForOfStatement(each) + if !each.r#await + && declares(&each.left, symbol) + && self.lists(&each.right, source)) + }) + } + + fn listed_by_index(&self, symbol: SymbolId, source: SymbolId) -> bool { + self.writes + .settled(self.scoping, symbol) + .is_some_and(|writes| { + !writes.is_empty() + && writes.iter().all(|write| match write { + Write::Value(value) => self.indexes_list(value, source), + _ => false, + }) + }) + } + + fn indexes_list(&self, value: &Expression<'_>, source: SymbolId) -> bool { + let Expression::ComputedMemberExpression(member) = value.without_parentheses() else { + return false; + }; + self.identifier_symbol(&member.object) + .and_then(|list| self.writes.settled(self.scoping, list)) + .is_some_and( + |writes| matches!(writes, [Write::Value(init)] if self.lists(init, source)), + ) + } + + fn lists(&self, expr: &Expression<'_>, source: SymbolId) -> bool { + let Expression::CallExpression(call) = expr.without_parentheses() else { + return false; + }; + let listed = match call.arguments.as_slice() { + [object] => object + .as_expression() + .is_some_and(|object| self.identifier_symbol(object) == Some(source)), + _ => false, + }; + listed && self.keys().lister(&call.callee, true) + } + + pub(super) fn sink_carries(&mut self, call: &'a CallExpression<'a>, held: Held<'a>) -> bool { + let descriptor = call.arguments.get(2).and_then(Argument::as_expression); + let read = descriptor.and_then(|descriptor| match descriptor.without_parentheses() { + Expression::CallExpression(read) => Some(read), + _ => None, + }); + let direct = read.filter(|read| { + self.builtin_member(&read.callee, &[OBJECT], &[DESCRIPTOR]) + || self.alias(&read.callee, &self.writes.readers) + }); + let carried = match direct { + Some(read) => { + let source = read + .arguments + .first() + .and_then(Argument::as_expression) + .and_then(|object| self.identifier_symbol(object)); + self.source_carries(source) + } + None => Some(Held::Read(held.key())), + }; + let target = call.arguments.first().and_then(Argument::as_expression); + self.carry(target, carried) + } + + pub(super) fn copies_carry( + &mut self, + call: &'a CallExpression<'a>, + descriptors: &'a CallExpression<'a>, + ) -> bool { + let source = descriptors + .arguments + .first() + .and_then(Argument::as_expression) + .and_then(|object| self.identifier_symbol(object)); + let carried = self.source_carries(source); + let defines = self.builtin_member(&call.callee, &[OBJECT], &[COPIERS[0]]); + match defines { + true => { + let target = call.arguments.first().and_then(Argument::as_expression); + self.carry(target, carried) + } + false => carried.is_none(), + } + } +} + +fn declares(left: &ForStatementLeft<'_>, symbol: SymbolId) -> bool { + let ForStatementLeft::VariableDeclaration(declaration) = left else { + return false; + }; + matches!(declaration.declarations.as_slice(), [declarator] + if matches!(&declarator.id, BindingPattern::BindingIdentifier(id) + if id.symbol_id.get() == Some(symbol))) +} diff --git a/crates/untwist-unpack/src/host_reach/copies.rs b/crates/untwist-unpack/src/host_reach/copies.rs index 0de5dac4..d3274dc6 100644 --- a/crates/untwist-unpack/src/host_reach/copies.rs +++ b/crates/untwist-unpack/src/host_reach/copies.rs @@ -6,7 +6,7 @@ use oxc_syntax::symbol::SymbolId; use super::*; impl<'a> Reach<'_, 'a> { - pub(super) fn getter(&self, at: usize, held: Held<'a>) -> bool { + pub(super) fn getter(&mut self, at: usize, held: Held<'a>) -> bool { let (Held::Read(Some(key)), AstKind::ArrowFunctionExpression(arrow)) = (held, self.stack[at]) else { @@ -17,7 +17,8 @@ impl<'a> Reach<'_, 'a> { }; match self.stack[up] { AstKind::ObjectProperty(_) => { - self.getter_target(up, child, key) || self.proxy_trap(up, arrow, key) + let source = self.read_source(arrow); + self.getter_target(up, child, key, source) || self.proxy_trap(up, arrow, key) } AstKind::StaticMemberExpression(member) if member.property.name == "bind" => { self.bound_getter(up, arrow, key) @@ -27,7 +28,7 @@ impl<'a> Reach<'_, 'a> { } pub(super) fn bound_getter( - &self, + &mut self, at: usize, arrow: &ArrowFunctionExpression<'a>, key: &Expression<'a>, @@ -65,12 +66,13 @@ impl<'a> Reach<'_, 'a> { let Some((property, child)) = self.parent(up) else { return false; }; + let source = self.read_source(arrow); matches!(self.stack[property], AstKind::ObjectProperty(_)) - && self.getter_target(property, child, bound) + && self.getter_target(property, child, bound, source) } pub(super) fn proxy_trap( - &self, + &mut self, at: usize, arrow: &ArrowFunctionExpression<'a>, key: &Expression<'a>, @@ -100,22 +102,40 @@ impl<'a> Reach<'_, 'a> { let Some((up, _)) = self.parent(object_at) else { return false; }; - let proxy = match self.stack[up] { - AstKind::NewExpression(call) => { - matches!(call.callee.without_parentheses(), Expression::Identifier(id) - if id.name == PROXY && self.symbol(id).is_none()) - && call - .arguments - .get(1) - .and_then(Argument::as_expression) - .is_some_and(|handler| same(handler, object)) - } - _ => false, + let AstKind::NewExpression(call) = self.stack[up] else { + return false; }; - keyed && named_get && proxy + let proxy = matches!(call.callee.without_parentheses(), Expression::Identifier(id) + if id.name == PROXY && self.symbol(id).is_none()) + && call + .arguments + .get(1) + .and_then(Argument::as_expression) + .is_some_and(|handler| same(handler, object)); + let first = arrow + .params + .items + .first() + .and_then(|param| match ¶m.pattern { + BindingPattern::BindingIdentifier(id) => id.symbol_id.get(), + _ => None, + }); + let reads_target = first.is_some() && self.read_source(arrow) == first; + let target = call + .arguments + .first() + .and_then(Argument::as_expression) + .and_then(|target| self.identifier_symbol(target)); + keyed && named_get && proxy && reads_target && self.source_carries(target).is_none() } - pub(super) fn getter_target(&self, at: usize, child: Address, key: &Expression<'_>) -> bool { + pub(super) fn getter_target( + &mut self, + at: usize, + child: Address, + key: &'a Expression<'a>, + source: Option, + ) -> bool { let AstKind::ObjectProperty(property) = self.stack[at] else { return false; }; @@ -148,10 +168,13 @@ impl<'a> Reach<'_, 'a> { .get(1) .and_then(Argument::as_expression) .is_some_and(|named| self.keys().same_key(key, named)); - matches!(self.stack[object_at], AstKind::ObjectExpression(_)) - && descriptor + let AstKind::ObjectExpression(object) = self.stack[object_at] else { + return false; + }; + descriptor && same_key && self.defines(call) + && self.getter_carries(call, object, source, key) } pub(super) fn passed_on( @@ -204,7 +227,12 @@ impl<'a> Reach<'_, 'a> { } } - pub(super) fn sink(&self, call: &CallExpression<'a>, child: Address, held: Held<'a>) -> bool { + pub(super) fn sink( + &mut self, + call: &'a CallExpression<'a>, + child: Address, + held: Held<'a>, + ) -> bool { let free_callee = |names: &[&str], expr: &Expression<'_>| { matches!(expr.without_parentheses(), Expression::Identifier(id) if names.contains(&id.name.as_str()) && self.symbol(id).is_none()) @@ -233,7 +261,7 @@ impl<'a> Reach<'_, 'a> { (Held::Descriptor(Some(key)), Some(named)) => self.keys().same_key(key, named), _ => false, }; - defines && at_value && same_key + defines && at_value && same_key && self.sink_carries(call, held) } pub(super) fn assigned( @@ -267,19 +295,6 @@ impl<'a> Reach<'_, 'a> { } } - fn carries(&mut self, target: &Expression<'a>, held: Held<'a>) -> bool { - let Expression::Identifier(id) = target.without_parentheses() else { - return false; - }; - match self.symbol(id) { - Some(symbol) => { - self.taint(symbol, held); - true - } - None => false, - } - } - pub(super) fn hold(&mut self, pattern: &BindingPattern<'a>, held: Held<'a>) -> bool { let BindingPattern::BindingIdentifier(id) = pattern else { return false; diff --git a/crates/untwist-unpack/src/host_reach/mod.rs b/crates/untwist-unpack/src/host_reach/mod.rs index e6e6411c..32365c1e 100644 --- a/crates/untwist-unpack/src/host_reach/mod.rs +++ b/crates/untwist-unpack/src/host_reach/mod.rs @@ -10,6 +10,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; use crate::host_keys::{const_key, literal_data, runner_key, Keys, Write, Writes, NAME_LISTERS}; use crate::host_text::CONSTRUCTOR; +mod carry; mod collect; mod copies; #[cfg(test)] @@ -480,7 +481,7 @@ impl<'a> Reach<'_, 'a> { || (name == GET && (call.arguments.len() >= 2 || self.reflective(member.object()))); if reads { self.check_read_call(at, call, name == DESCRIPTOR); - } else if name == DESCRIPTORS && !self.copied(at) { + } else if name == DESCRIPTORS && !self.copied(at, call) { self.refuse(DESCRIPTORS_TAKEN); } } @@ -513,21 +514,21 @@ impl<'a> Reach<'_, 'a> { } } - fn copied(&self, at: usize) -> bool { - self.parent(at) - .is_some_and(|(up, child)| match self.stack[up] { - AstKind::CallExpression(call) => { - let copier = self.builtin_member(&call.callee, &[OBJECT], &COPIERS) - && !call.arguments.iter().any(Argument::is_spread); - let second = call - .arguments - .get(1) - .and_then(Argument::as_expression) - .is_some_and(|argument| same(argument, child)); - copier && second - } - _ => false, - }) + fn copied(&mut self, at: usize, descriptors: &'a CallExpression<'a>) -> bool { + let Some((up, child)) = self.parent(at) else { + return false; + }; + let AstKind::CallExpression(call) = self.stack[up] else { + return false; + }; + let copier = self.builtin_member(&call.callee, &[OBJECT], &COPIERS) + && !call.arguments.iter().any(Argument::is_spread); + let second = call + .arguments + .get(1) + .and_then(Argument::as_expression) + .is_some_and(|argument| same(argument, child)); + copier && second && self.copies_carry(call, descriptors) } fn check_pattern_key(&mut self, key: &PropertyKey<'a>, computed: bool) { diff --git a/crates/untwist-unpack/src/host_reach/tests.rs b/crates/untwist-unpack/src/host_reach/tests.rs index 8e2e3da5..3a36995a 100644 --- a/crates/untwist-unpack/src/host_reach/tests.rs +++ b/crates/untwist-unpack/src/host_reach/tests.rs @@ -33,7 +33,11 @@ fn keys_that_cannot_name_a_runner_pass() { "var __gn = Object.getOwnPropertyNames; var __c = (cb, mod) => function r() { return mod || (0, cb[__gn(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; var a = __c({ 'a.js'(e, m) {} });", "var __dp = Object.defineProperty; var __gd = Object.getOwnPropertyDescriptor; var __cp = (to, from, desc) => { for (let key of Object.keys(from)) __dp(to, key, { get: () => from[key], enumerable: !(desc = __gd(from, key)) || desc.enumerable }); return to; };", "var __dp = Object.defineProperty; var __cp = (to, from) => { for (var keys = Object.keys(from), i = 0, key; i < keys.length; i++) { key = keys[i]; __dp(to, key, { get: ((k) => from[k]).bind(null, key) }); } return to; };", - "var __r = ((x) => typeof require !== 'undefined' ? require : typeof Proxy !== 'undefined' ? new Proxy(x, { get: (a, b) => (typeof require !== 'undefined' ? require : a)[b] }) : x)(function (x) { throw Error(x); });", + "var p = new Proxy(x, { get: (a, b) => a[b] });", + "var __dp = Object.defineProperty; var __gd = Object.getOwnPropertyDescriptor; var __gn = Object.getOwnPropertyNames; var __cp = (to, from, except, desc) => { if (from && typeof from === 'object' || typeof from === 'function') { for (let key of __gn(from)) if (key !== except) __dp(to, key, { get: () => from[key], enumerable: !(desc = __gd(from, key)) || desc.enumerable }); } return to; }; var m = __cp({}, require_a());", + "var __dp = Object.defineProperty; var __gd = Object.getOwnPropertyDescriptor; var __gn = Object.getOwnPropertyNames; var __cp = (to, from, except, desc) => { for (var keys = __gn(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (key !== except) __dp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __gd(from, key)) || desc.enumerable }); } return to; }; __cp({}, require_a());", + "function c(t, s, k) { Object.defineProperty(t, k, Object.getOwnPropertyDescriptor(s, k)); return t; } c({}, a, b);", + "function c(t, s, k) { Object.defineProperty(t, k, { get: () => s[k] }); return t; } c({}, a, b);", "function l(e, t) { var n = e[t]; e[t] = n; }", "var __ea = (all) => { let t = Object.setPrototypeOf({}, null); for (var n in all) Object.defineProperty(t, n, { get: all[n] }); return t; }; __ea({ a: () => 1 });", "var __c = (cb) => cb[k]('x');", @@ -160,6 +164,13 @@ fn the_sixth_review_shapes_refuse() { "var t = {}; var v = P[K]; t[K] = v; f(t);", "a.b[K] = P[K];", "for (var k in s) t[k] = s[k];", + "var t = {}; Object.defineProperty(t, K, { enumerable: true, get: () => P[K] }); Object.values(t)[0]('x')();", + "var p = new Proxy({ constructor: 1 }, { get: (t, k) => P[k] }); Object.values(p)[0]('x')();", + "var __dp = Object.defineProperty; var __gd = Object.getOwnPropertyDescriptor; function cp(to, from, key, desc) { __dp(to, key, { get: () => from[key], enumerable: !(desc = __gd(from, key)) || desc.enumerable }); return to; } cp({}, P, K);", + "var __dp = Object.defineProperty; var __gd = Object.getOwnPropertyDescriptor; var t = {}; t[K] = P[K]; var cp = (to, from, desc) => { for (let key of Object.getOwnPropertyNames(from)) __dp(to, key, { get: () => from[key], enumerable: !(desc = __gd(from, key)) || desc.enumerable }); return to; }; cp({}, t);", + "var __dp = Object.defineProperty; var __gd = Object.getOwnPropertyDescriptor; var cp = (to, from, desc) => { for (let key of Object.getOwnPropertyNames(from)) { key = 'constructor'; __dp(to, key, { get: () => from[key], enumerable: !(desc = __gd(from, key)) || desc.enumerable }); } return to; }; cp({}, P);", + "var d = Object.getOwnPropertyDescriptor(P, K); d.enumerable = true; var t = {}; Object.defineProperty(t, K, d); Object.values(t)[0]('x')();", + "var __r = ((x) => typeof require !== 'undefined' ? require : typeof Proxy !== 'undefined' ? new Proxy(x, { get: (a, b) => (typeof require !== 'undefined' ? require : a)[b] }) : x)(function (x) { throw Error(x); });", "var n = {}; var a = Object.defineProperty; for (c in e) { if ((r = a && Object.getOwnPropertyDescriptor(e, c)) && (r.get || r.set)) { a(n, c, r); } else { n[c] = e[c]; } } var r;", "Symbol.foo = K; (function(){})[Symbol.foo]('x')();", "Math.foo = function() { return K; }; (function(){})[Math.foo()]('x')();", diff --git a/docs/known-divergences/unpack.md b/docs/known-divergences/unpack.md index 5fbb4fd6..ce1764c1 100644 --- a/docs/known-divergences/unpack.md +++ b/docs/known-divergences/unpack.md @@ -21,7 +21,7 @@ Untwist's unpack stage is verified against the reference deobfuscator by `cargo - **A local or parameter named `require` is not the runtime.** A by-name rewrite of `require()`, `require.n(...)` and `require.t(...)` also rewrites `function load(require) { return require("./utils") + require(7); }` inside a module, whose arguments then become module paths the function was never given. Untwist asks one question, `scan::is_free`, in the require-path rewriter, the `.n` conversion, the `.t` conversion and the host-`require` shield: a reference that scoping resolves to a binding of the module is left alone, and a node without a reference (one Untwist built itself) counts as the runtime. This holds in both modes, so the conservative comparison mode differs from a by-name rewrite only inside a module that declares its own `require`. - **A webpack var injection is spliced only where it is proven.** The reference deobfuscator turns every module statement of the form `(function (a, b) { ... }).call(this, x, y)` (or `.call(exports, ...)`) into `var a = x; var b = y;` followed by the body. That changes the program when the call is not the whole module (its declarations join the module's, a `return` now ends the module), when the counts differ, when the body has a directive, a `return` at its own level, `arguments`, `new.target` or `eval`, when a name the body declares at its own level collides with a parameter or with `module`/`exports`/`require`/`__filename`/`__dirname` (a function declaration is hoisted above the new `var` and loses to it), when an argument reads a parameter name or a name the body declares, or when the receiver is `exports` and the body reads `this`. By default Untwist splices only webpack's own wrapper: an anonymous plain function called as the last statement of a module whose other statements are `require.r(exports)`, with as many arguments as plain, distinct parameters and none of the cases above. Any other call stays as written. The conservative comparison mode keeps the reference deobfuscator's splice. - **A webpack or browserify bundle is taken only when every factory is closed.** The reference deobfuscator finds these bundle shapes anywhere in the program and lifts each factory body into its own file. A factory that reads a binding of the scope around the bundle (a variable beside a nested bundle call, a parameter of the function around it, the `__WEBPACK_EXTERNAL_MODULE_x__` parameters of a `libraryTarget: "umd"` wrapper with externals, a name of the bundle's own prelude), its own `arguments`, a parameter the runtime never passes, or that calls `eval` directly (any call spelled `eval(...)`, also through a local binding named `eval`) or holds a `with` statement (the module rewrites cannot be proven inside one; a direct `eval` of a constant string that declares nothing and reads only names no binding holds at the call, such as `eval("Math;")`, is allowed, and those names are then kept free in the written file: a factory parameter is renamed around them, and a bundle whose rewrites would bind one is not written; such an eval whose names include `require`, `module`, `exports`, `__filename` or `__dirname` is refused, since it reads the name the bundle file has and the written file would answer for itself: `eval("require('./b')")` counts as a free-require read, `it reads require, which every module has for itself`, except that in a webpack factory an eval whose whole payload is `require` (`eval("require;")("worker_threads")`, which real bundles use to hide node's require from the bundler) is the host's `require` and is judged by the webpack rule below; a factory that reads a free `module` or `exports` in its code while its own parameter in that place has another name is refused too, since the written file would bind that name: `it reads exports, which every module has for itself`; so is a factory that reads a free `__filename` or `__dirname`, in both lanes: in the bundle every module sees the bundle file and its directory, in the tree each written file sees itself, so two modules that shared one directory would disagree (`it reads __dirname, which every module has for itself`); a webpack factory may use the host's free `require` only as `typeof require` or as the callee of a call with one literal request naming a node built-in other than `module` and `process`, or a package (webpack's externals, also through a choice such as `(typeof require == "undefined" ? __webpack_require__(125) : require)("worker_threads")`); every other appearance, in the body or a parameter, is refused: a relative request, any member of `require` (`require.main`, `require.resolve`, `require.cache`, `require.valueOf()`, which hands the function back, `require.__defineGetter__` and `require.prototype`, which write to it, a computed member), a write, `require` as a value or an alias, since node answers them from the written file's path or gives each written file its own function: `it uses the host's require (require.valueOf), which a written file answers for its own path`; a package request kept this way is resolved from the written file like browserify's left requests, and a request node would answer with a file of the written tree refuses the bundle), then runs in a file where none of that exists. By default Untwist checks every factory with the rule the other bundlers already follow (`closed.rs`: only globals, the mapped parameters and the factory's own declarations) and, when one fails, takes no bundle from that shape: the program is output whole, with the note `module is not closed over its own scope; no bundle is written: it reads , a binding of the bundle's scope`. A later shape in the same file whose factories are closed is still found, and is written only under the rule for code beside the bundle below. A UMD wrapper with externals is refused this way for now rather than having its externals turned into requires. The conservative comparison mode keeps the reference deobfuscator's behaviour. -- **A request a module leaves to node must resolve alike from the bundle and from the written tree.** node resolves `require(...)` and `import(...)` from the file that holds the call, so a relative request, or any route whose answer depends on the module's own file or the main module's, gives the bundle's answer in the bundle and the written file's in the tree; a route relative to the working directory is the same on both sides. In every bundle kind, a host `require("x")` a module keeps (webpack externals, esbuild's and Bun's `require` and `__require`, rolldown, UMD factories, Rollup, Parcel's hoisted output, AMD, Closure, Metro, Turbopack, SystemJS) and every `import("x")`, static `import … from "x"` and `export … from "x"` in a module or in the code kept beside the bundle, also an `import()` in the text of a constant `eval`, must name a node built-in other than `module` and `process` (which hand out `createRequire`, `_load`, `_resolveFilename` and `mainModule`), or a package; the one exception is the recorded runtime line `import { createRequire } from "node:module"` whose binding only feeds `createRequire(import.meta.url)` (rolldown's and esbuild's `require` shim). An `import()` of a non-literal specifier refuses the bundle, and in the default lane so does every relative request in an `import()`, a static `import` or an `export … from` (`module 0 uses import("./1.js"), which node answers from the file that holds it, so the written file would not load what the bundle loads; no bundle is written`), also when it names an input of the run: a mirrored input is a second copy, so a chunk that imports the bundle's input back by name evaluates the whole bundle again, node reads the specifier as a URL (`%6C`, `?x`, `#x`) and through symbolic links, and the target's own imports are not followed. The vite 8, rolldown, esbuild and Bun ESM splits given with their chunks are therefore kept whole in the default lane. Under `--unsafe` such a relative request is kept when the bundle is one input of a run whose other inputs are mirrored, the module that holds it is written in the directory of that input, the specifier holds no `%`, `?`, `#` or `\`, and it names a mirrored input other than the bundle's own; and the bundle is refused when any other input of the run names the bundle's input in a relative request. The lane's premise is that the inputs given beside a bundle are the files its relative imports load, and that the bundle's input is not imported back by name through a route that check does not see. In the default lane a module or the code beside the bundle that holds a code runner (`Function`, `new Function`, indirect `eval`, a timer given a string) refuses the bundle unless the runner's text is constant and parses free of `import()`, `import.meta`, `require`, `module`, `exports`, `process`, `__filename`, `__dirname`, `globalThis`, `global`, a computed member and other runners (`Function("return this")` is kept); a runner taken as a value refuses, and so does a request for the `vm` built-in. Any `.constructor` member (`x.constructor`, `x["constructor"]`) refuses too, called or read, whatever the receiver, since the receiver may be a function reached through an alias (`var f = function () {}; f.constructor(text)`); only `typeof x.constructor` is kept. A runner can be reached without its name, so in the default lane every route to one is checked against a closed list as well (`host_reach.rs`): a free `Function` may be a member object only as `Function.prototype`, `.name` or `.length` (`Function.call(null, text)` refuses); a member named `eval`, `Function` or `constructor` refuses on any receiver (`g.eval`, `this.Function`, `globalThis[`eval`]`, a key that folds to the name), as does a destructuring key of those names or a computed one (`var {constructor: F} = f`, `var {eval: e} = globalThis`), a free `constructor` and a `with` statement; a computed member whose key is not a constant is kept only when the key cannot be one of those names (a number, a loop counter whose writes are numbers, a string whose cooked text has a prefix or suffix no runner name has such as `'on' + type`, and `Symbol.iterator`, `Math.max(a, b)`, `Date.now()` or a RegExp literal only while the built-in scan finds no write to `Symbol`, `Math`, `Date`, `RegExp`, `Number`, `parseInt`, `parseFloat` or `Object.prototype` in any input), when the receiver is an array or object literal or a local that only ever holds one and is given only primitive values under computed keys (`test2['_' + c] = i`), or when the value read is only tested, compared, written back under the same key into a local (`t[k] = s[k]`, or `t[ks[i]] = s[ks[i]]` where `ks` only ever holds the result of `Object.keys`, `Object.getOwnPropertyNames` or `Object.getOwnPropertySymbols`; the local `t` then carries the value, so its own uses must follow the same rule and `return t` or `Object.values(t)` keeps the bundle whole), read for a member other than `call`, `apply` or `bind`, called with no text (`o[k]()`), or held in a local whose own uses are all of these; `Reflect` may be used only as the object of a member and `Reflect.get`, `getOwnPropertyDescriptor` and `getOwnPropertyDescriptors` only as callees, with a constant key or a key held under the same rule and no spread argument (`Object.defineProperty(t, k, Object.getOwnPropertyDescriptor(s, k))` and `Object.defineProperties(t, Object.getOwnPropertyDescriptors(s))` are copies and kept when the callee is a member of the free `Object` or `Reflect`). The global object needs no list of its own: it reaches a runner only through those keys, so aliases such as `var g = globalThis` or `Function("return this")()` are kept. A computed read the check cannot follow keeps the bundle whole in the default lane: a value that flows into a function from another module (lodash's `isIterateeCall` hands `object[index]` to `eq`, which it takes from `requireEq()` through a lazy require wrapper), a value passed to a function that uses it for more than the uses above (svelte's `attr(name, attrs[name])`), a read keyed by a parameter (`const pick = (name) => colors[name]`), and property descriptors kept as a value (solid's `desc = Object.getOwnPropertyDescriptors(value)`); `--unsafe` splits them. A code text that holds a `with` statement refuses too. A direct `eval` of a constant text gets the same scan as a runner's text (`eval("Function(…)()")` and `eval("process.argv[1]")` refuse), and code texts also refuse `Reflect`, `Object` and every route above. Under `--unsafe` the premise is that no code text reaches `import()`, node's module objects or loader. In the default lane the own file's and the main file's names are closed off by an allow-list: `import.meta` may appear only under `typeof` (`typeof import.meta`, `typeof import.meta.url`) or inside the recorded runtime line's `createRequire(import.meta.url)`; a member of a free `process` only from a fixed list of members that name no file (`env`, `nextTick`, `platform`, `version`, `versions`, `cwd`, `exit`, the event methods, the standard streams, …; not `argv`, `mainModule` or `getBuiltinModule`); a member of a free `module` only as `exports`, `hot` or `loaded`; a member of the host's free `require` only as `apply`; `__filename` and `__dirname` not at all. Anything else (`import.meta.url`, `import.meta.filename`, `require.main`, `module.filename`, `module.id`, `module.parent`, `process.argv`) refuses the bundle. Under `--unsafe` the premise is that the program does not read its own file name or the main module's path. A stack trace names the file that holds each frame (`new Error().stack`, `Error.captureStackTrace`), and neither lane checks for it: both assume no code reads file names out of a stack trace. `require("module")` refuses the bundle too. Each kept package request is resolved like browserify's left requests (`crates/untwist-unpack/src/browserify/resolve.rs`): it refuses the bundle when a file of the written tree or any input of the run lies on node's lookup path for it, looked up from the written file and also from the bundle's own input as named under the directory the inputs share, which is where a partial run mirrors the other inputs (`app/bundle.js` beside `app/node_modules/foo/index.js` refuses: `module 1: require("foo") is left as written, which node would resolve to a file of the written tree`). A module that names `process.mainModule`, `process.getBuiltinModule` or a computed member of `process` is refused in both lanes. Turbopack's server runtime is not checked: it is an input written whole at its own path (a fixed path, which refuses the bundle if anything would move it), so its chunk loads run from the same place on both sides. `process` taken as a value is not followed, nor is it reached through the global object (`var p = process; p.mainModule.require("./1.js")` is the premise row `r4n10`; `globalThis.process.mainModule` and `global.process.mainModule` are the same premise): bundles pass `process` to their polyfills and destructure it (`const { env } = process`), so the README states the premise that no code reaches node's module objects or loader that way. A factory or module wrapper that is async, a generator, or takes a pattern, rest or default parameter is refused in every kind (`its factory is async, a generator or takes a pattern, rest or default parameter, so it does not run like a file`), since the default runs when the bundle calls the factory and the async body would become a file with a top-level `await`. Rows: `crates/untwist-cli/tests/host_routes_review`, `host_routes_mirrored` and `host_routes_split` (`host_route_splits.rs`), both lanes, node parity. +- **A request a module leaves to node must resolve alike from the bundle and from the written tree.** node resolves `require(...)` and `import(...)` from the file that holds the call, so a relative request, or any route whose answer depends on the module's own file or the main module's, gives the bundle's answer in the bundle and the written file's in the tree; a route relative to the working directory is the same on both sides. In every bundle kind, a host `require("x")` a module keeps (webpack externals, esbuild's and Bun's `require` and `__require`, rolldown, UMD factories, Rollup, Parcel's hoisted output, AMD, Closure, Metro, Turbopack, SystemJS) and every `import("x")`, static `import … from "x"` and `export … from "x"` in a module or in the code kept beside the bundle, also an `import()` in the text of a constant `eval`, must name a node built-in other than `module` and `process` (which hand out `createRequire`, `_load`, `_resolveFilename` and `mainModule`), or a package; the one exception is the recorded runtime line `import { createRequire } from "node:module"` whose binding only feeds `createRequire(import.meta.url)` (rolldown's and esbuild's `require` shim). An `import()` of a non-literal specifier refuses the bundle, and in the default lane so does every relative request in an `import()`, a static `import` or an `export … from` (`module 0 uses import("./1.js"), which node answers from the file that holds it, so the written file would not load what the bundle loads; no bundle is written`), also when it names an input of the run: a mirrored input is a second copy, so a chunk that imports the bundle's input back by name evaluates the whole bundle again, node reads the specifier as a URL (`%6C`, `?x`, `#x`) and through symbolic links, and the target's own imports are not followed. The vite 8, rolldown, esbuild and Bun ESM splits given with their chunks are therefore kept whole in the default lane. Under `--unsafe` such a relative request is kept when the bundle is one input of a run whose other inputs are mirrored, the module that holds it is written in the directory of that input, the specifier holds no `%`, `?`, `#` or `\`, and it names a mirrored input other than the bundle's own; and the bundle is refused when any other input of the run names the bundle's input in a relative request. The lane's premise is that the inputs given beside a bundle are the files its relative imports load, and that the bundle's input is not imported back by name through a route that check does not see. In the default lane a module or the code beside the bundle that holds a code runner (`Function`, `new Function`, indirect `eval`, a timer given a string) refuses the bundle unless the runner's text is constant and parses free of `import()`, `import.meta`, `require`, `module`, `exports`, `process`, `__filename`, `__dirname`, `globalThis`, `global`, a computed member and other runners (`Function("return this")` is kept); a runner taken as a value refuses, and so does a request for the `vm` built-in. Any `.constructor` member (`x.constructor`, `x["constructor"]`) refuses too, called or read, whatever the receiver, since the receiver may be a function reached through an alias (`var f = function () {}; f.constructor(text)`); only `typeof x.constructor` is kept. A runner can be reached without its name, so in the default lane every route to one is checked against a closed list as well (`host_reach.rs`): a free `Function` may be a member object only as `Function.prototype`, `.name` or `.length` (`Function.call(null, text)` refuses); a member named `eval`, `Function` or `constructor` refuses on any receiver (`g.eval`, `this.Function`, `globalThis[`eval`]`, a key that folds to the name), as does a destructuring key of those names or a computed one (`var {constructor: F} = f`, `var {eval: e} = globalThis`), a free `constructor` and a `with` statement; a computed member whose key is not a constant is kept only when the key cannot be one of those names (a number, a loop counter whose writes are numbers, a string whose cooked text has a prefix or suffix no runner name has such as `'on' + type`, and `Symbol.iterator`, `Math.max(a, b)`, `Date.now()` or a RegExp literal only while the built-in scan finds no write to `Symbol`, `Math`, `Date`, `RegExp`, `Number`, `parseInt`, `parseFloat` or `Object.prototype` in any input), when the receiver is an array or object literal or a local that only ever holds one and is given only primitive values under computed keys (`test2['_' + c] = i`), or when the value read is only tested, compared, written back under the same key into a local (`t[k] = s[k]`, or `t[ks[i]] = s[ks[i]]` where `ks` only ever holds the result of `Object.keys`, `Object.getOwnPropertyNames` or `Object.getOwnPropertySymbols`; the local `t` then carries the value, so its own uses must follow the same rule and `return t` or `Object.values(t)` keeps the bundle whole), read for a member other than `call`, `apply` or `bind`, called with no text (`o[k]()`), or held in a local whose own uses are all of these; `Reflect` may be used only as the object of a member and `Reflect.get`, `getOwnPropertyDescriptor` and `getOwnPropertyDescriptors` only as callees, with a constant key or a key held under the same rule and no spread argument (`Object.defineProperty(t, k, Object.getOwnPropertyDescriptor(s, k))` and `Object.defineProperties(t, Object.getOwnPropertyDescriptors(s))` are copies and kept when the callee is a member of the free `Object` or `Reflect`). A copy that keeps the source's enumerability (a descriptor copy, `Object.defineProperties(t, Object.getOwnPropertyDescriptors(s))`, a getter `{ get: () => s[k] }` with no `enumerable` or `enumerable: false`, and esbuild's and rolldown's `__copyProps`, whose `enumerable: !(d = __getOwnPropDesc(from, key)) || d.enumerable` runs over `__getOwnPropNames(from)`) makes `t` carry what `s` carries; any other getter copy, a descriptor held in a local, and a Proxy `get` trap that reads anything but its own target (`(a, b) => a[b]`) make the target carry the read, so `Object.values(t)` or `return t` keeps the bundle whole. esbuild's `__require` shim reads the free `require` in its Proxy trap and so keeps an esbuild bundle that holds it whole in the default lane. The global object needs no list of its own: it reaches a runner only through those keys, so aliases such as `var g = globalThis` or `Function("return this")()` are kept. A computed read the check cannot follow keeps the bundle whole in the default lane: a value that flows into a function from another module (lodash's `isIterateeCall` hands `object[index]` to `eq`, which it takes from `requireEq()` through a lazy require wrapper), a value passed to a function that uses it for more than the uses above (svelte's `attr(name, attrs[name])`), a read keyed by a parameter (`const pick = (name) => colors[name]`), and property descriptors kept as a value (solid's `desc = Object.getOwnPropertyDescriptors(value)`); `--unsafe` splits them. A code text that holds a `with` statement refuses too. A direct `eval` of a constant text gets the same scan as a runner's text (`eval("Function(…)()")` and `eval("process.argv[1]")` refuse), and code texts also refuse `Reflect`, `Object` and every route above. Under `--unsafe` the premise is that no code text reaches `import()`, node's module objects or loader. In the default lane the own file's and the main file's names are closed off by an allow-list: `import.meta` may appear only under `typeof` (`typeof import.meta`, `typeof import.meta.url`) or inside the recorded runtime line's `createRequire(import.meta.url)`; a member of a free `process` only from a fixed list of members that name no file (`env`, `nextTick`, `platform`, `version`, `versions`, `cwd`, `exit`, the event methods, the standard streams, …; not `argv`, `mainModule` or `getBuiltinModule`); a member of a free `module` only as `exports`, `hot` or `loaded`; a member of the host's free `require` only as `apply`; `__filename` and `__dirname` not at all. Anything else (`import.meta.url`, `import.meta.filename`, `require.main`, `module.filename`, `module.id`, `module.parent`, `process.argv`) refuses the bundle. Under `--unsafe` the premise is that the program does not read its own file name or the main module's path. A stack trace names the file that holds each frame (`new Error().stack`, `Error.captureStackTrace`), and neither lane checks for it: both assume no code reads file names out of a stack trace. `require("module")` refuses the bundle too. Each kept package request is resolved like browserify's left requests (`crates/untwist-unpack/src/browserify/resolve.rs`): it refuses the bundle when a file of the written tree or any input of the run lies on node's lookup path for it, looked up from the written file and also from the bundle's own input as named under the directory the inputs share, which is where a partial run mirrors the other inputs (`app/bundle.js` beside `app/node_modules/foo/index.js` refuses: `module 1: require("foo") is left as written, which node would resolve to a file of the written tree`). A module that names `process.mainModule`, `process.getBuiltinModule` or a computed member of `process` is refused in both lanes. Turbopack's server runtime is not checked: it is an input written whole at its own path (a fixed path, which refuses the bundle if anything would move it), so its chunk loads run from the same place on both sides. `process` taken as a value is not followed, nor is it reached through the global object (`var p = process; p.mainModule.require("./1.js")` is the premise row `r4n10`; `globalThis.process.mainModule` and `global.process.mainModule` are the same premise): bundles pass `process` to their polyfills and destructure it (`const { env } = process`), so the README states the premise that no code reaches node's module objects or loader that way. A factory or module wrapper that is async, a generator, or takes a pattern, rest or default parameter is refused in every kind (`its factory is async, a generator or takes a pattern, rest or default parameter, so it does not run like a file`), since the default runs when the bundle calls the factory and the async body would become a file with a top-level `await`. Rows: `crates/untwist-cli/tests/host_routes_review`, `host_routes_mirrored` and `host_routes_split` (`host_route_splits.rs`), both lanes, node parity. - **A bundle is split in the safe lane only when no module can see its caller.** In the bundle a function called by a module's top-level code has that module's factory as its caller, with the bundle's `module`, `exports` and `require` as the caller's arguments; in a written file the caller is node's module wrapper, with five other arguments. The same holds for every bundle kind: a factory or module wrapper (esbuild's `__commonJS`, Parcel, Metro, Turbopack, AMD, UMD, SystemJS, Closure) or the outer function of a split Rollup bundle is the caller in the bundle and not in the written files. No scan of property names can prove that nothing reads that frame (a computed key, a reader passed around as a value, text built at run time, `Error.prepareStackTrace` with `getFunction()`, `with (f) caller` in another input), so by default Untwist relies on how V8 treats strict code instead: the `caller` of a function whose caller is strict code is `null`, reading `caller` or `arguments` of a strict function throws, and `getFunction()` and `getThis()` are `undefined` for strict frames (and for every frame once a strict one is on the stack). This was checked in node 22 for `f.caller`, `arguments.callee.caller`, `with (f) caller`, `({ __proto__: f }).caller`, `getFunction()` from `Error.prepareStackTrace` and from `Error.captureStackTrace`. A bundle is therefore split only when every function in the input that holds it is strict code (the input is an ES module or starts with `"use strict"`, or each function sits in a function or class that is strict) and every written module file is strict code (it has `import` or `export`, is `.mjs`, or starts with `"use strict"`). Untwist adds `"use strict"` to a written file only when the module's code was already strict in the bundle (see below), never to code that was sloppy there. Otherwise no bundle is written, with the note `the bundle is not strict code, is sloppy: a function the modules call could read its caller (...), which is the bundle's own function in the bundle and node's module wrapper in the written files; no bundle is written (--unsafe assumes no such read reaches a module)`, or `module would be written as a sloppy file (...)` with the same reason. Most webpack 4 bundles, and every bundle with a CommonJS module that is not strict, are kept whole in the safe lane this way. Under `--unsafe`, no read of a function's caller or arguments, in any input of the run, is assumed to reach a bundled module's function, and such bundles are unpacked anyway (Krunker's `kpdData.caller` is game data, but that cannot be proven). In every lane, a module whose code is strict in the bundle (all of it: through its own directive, a `"use strict"` of the file or of a function around it, or an ES module input; a class or a `"use strict"` function among a module's top-level statements makes only its own body strict, never the module) and that would otherwise be written as a sloppy CommonJS file is written with a `"use strict"` directive, as esbuild, AMD and Turbopack modules already were; webpack's and browserify's module files used to lose a strictness they had only from the code around them (webpack 5 node chunks and `output.module` chunks start with `"use strict"` and their factories have none), and a factory's own directives now stay in its written file (browserify's were dropped). The input counts as an ES module when it is named `.mjs` or its original text, before any pass runs, does not parse as a script but parses as a module (`import`/`export`, `import.meta` or a top-level `await x`: node 22's syntax detection; `await (x)`, a call of a global named `await`, keeps a script), even when a later pass removes that code (`if (0) { import.meta; }`); every bundle kind in such an input then writes strict module files (browserify's proof refuses it instead). The detection matched is node 22.23's (older 22.x releases report a SyntaxError for some module-only syntax in a script, e.g. `class A { [await 0]() {} }`). A bundle whose written CommonJS module file (one without `import`/`export`, or the written runtime) would hold `import.meta` or a top-level `await` is not written, in every lane, with the note `module would be written with import.meta, which node runs only in an ES module and the written file cannot keep as the input had it`: node runs a `.js` file with that syntax as an ES module, where `module`, `exports` and `require` do not exist. esbuild's `__commonJS` factory check refuses `import.meta` too. A file written as an ES module keeps its `import.meta` (the `createRequire(import.meta.url)` banner of esbuild and Rolldown ESM output, the one Untwist writes for an ES module entry); its `url` then names the written file, as `__filename` does in a CommonJS file. With several inputs this is decided for each input by its own text and name. A runtime helper that calls `eval` directly, holds a `with` statement, or names the written runtime's own `__webpack_module_paths__` is not moved to `_runtime/webpack.js`, and a module that reads `__dirname` or `__filename` stays CommonJS. - **A member of `module` used as a call or tag target keeps the bundle whole in every wrapper format.** `module.exports()`, `module["exports"]()`, `(module.exports)()`, `module?.exports()` and `` module.exports`x` `` hand the module object over as `this`: the bundler's record (`{ exports }`) in the bundle, node's `Module` in a written file. Besides webpack and browserify (below), esbuild's and rolldown's `__commonJS` factories, Parcel, Metro and Turbopack modules that do this are not split. `module.exports.f()`, `(0, module.exports)()` and `new module.exports()` hand over no module and are not affected. An AMD factory never gets a module record (a `"module"` dependency is refused), a UMD wrapper and Bun hand over node's own module, Rollup's CommonJS wrappers keep their `{ exports }` holder in the same written file, and SystemJS and Closure modules have no module object. - **Code beside a webpack or browserify bundle goes into the written entry, or no bundle is written.** When the bundle is one top-level statement of a file that has others, the reference deobfuscator writes the modules and loses the rest, so `node index.js` does not run it. Untwist puts the statements before the bundle at the top of the entry module's file and those after it at its end, which runs them in the same order. That needs the statements not to declare anything at the top level (a `var`, `let`, `const`, function or class, also a `var` or function in a block), not to read `this`, `arguments`, `module`, `exports`, `require`, `__filename`, `__dirname` or a binding of the bundle's own statement, not to call `eval` directly or hold a `with` statement, the bundle to come from one input with one entry, and the entry module to be CommonJS (an ES module's imports would run first), written exactly as strict as that code (the entry file's strictness is decided first, from the bundle, and code from a sloppy top level never goes into a `"use strict"` entry file, nor strict code into a sloppy one), without a `return` at its top level and without a top-level binding of a name that code reads as a global. Otherwise no bundle is written (`the program runs code beside the bundle (line N) that the written files cannot hold: it `, or `... that the written entry cannot hold: `). A file with a second, refused bundle beside a closed one is therefore kept whole too. The bundle must also be the whole of its top-level statement: the statement may wrap it only in `!` or `void` (which run none of the bundle value's code, unlike `+`, `-` or `~`), parentheses, or a function called on the spot whose body holds nothing but the bundle (and directives): a plain function or arrow (not `async`, not a generator) without parameters, called with no arguments or only `this` and literals, directly or with `.call`, or with `.apply` and at most one argument. The exceptions are a webpack 5 library build whose wrapper equals a recorded library wrapper (above), browserify's standalone frame, and a UMD wrapper (webpack 4 `umd`) whose parameters are plain names without defaults and whose CommonJS branch, `module.exports = factory()` with a factory without parameters, is taken before any `typeof define` test, since code before the bundle can define `define`, and only when nothing before the wrapper can run (directives, function declarations and `var` declarations whose initialisers neither call nor write), since such code could change what the wrapper's tests and arguments read (`Object.prototype.nodeName`, a getter on `self`). A bundle inside an `if`, a block, a function that is only declared, any other UMD or library wrapper (an AMD test first), an assignment or a declaration, or a wrapper with other statements beside the bundle (`(function () { f(); })()`), means no bundle (`the bundle sits inside a top-level statement (line N) that runs other code too, which the written files cannot hold`), in every lane. A sequence `f(), ` is split into statements before the unpack stage and follows the rule for code beside the bundle. Not covered: a webpack 5 runtime written at the top level of the file. diff --git a/web/scripts/size.ts b/web/scripts/size.ts index 21265c71..9d0fba2c 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_355_000; +const WASM_BASELINE_BYTES = 14_360_000; const WASM_GROWTH = 0.1; const gzip = (bytes: Buffer): number => gzipSync(bytes, { level: 9 }).length;