diff --git a/crates/js/src/builtins/object.rs b/crates/js/src/builtins/object.rs index 48fdbb6eda..42947c7d7a 100644 --- a/crates/js/src/builtins/object.rs +++ b/crates/js/src/builtins/object.rs @@ -198,7 +198,15 @@ fn define_all(interp: &mut Interp, o: ObjId, props: &Value) -> JsResult<()> { interp.temp_roots.push(Value::Object(p)); let r = (|| { let mut descs = Vec::new(); - for k in own_prop_keys(interp, p)? { + let keys = own_prop_keys(interp, p)?; + // A getter can delete a Symbol-keyed property already read, so the Symbols stay rooted + // until they are defined on `o`. + for k in &keys { + if let PropKey::Sym(s) = k { + interp.temp_roots.push(Value::Symbol(s.clone())); + } + } + for k in keys { interp.use_fuel()?; if !interp.get_own_pk(p, &k)?.is_some_and(|prop| prop.enumerable()) { continue; diff --git a/crates/js/src/interp.rs b/crates/js/src/interp.rs index 3c6f73cb64..405874dc96 100644 --- a/crates/js/src/interp.rs +++ b/crates/js/src/interp.rs @@ -2993,7 +2993,8 @@ impl Interp { return self.xml_concat(a, b); } let pa = self.to_primitive(a, Hint::Default)?; - let pb = self.to_primitive(b, Hint::Default)?; + // The right side's conversion can run script, so the left side's result stays counted. + let pb = self.rooted(std::slice::from_ref(&pa), |i| i.to_primitive(b, Hint::Default))?; if matches!(pa, Value::String(_)) || matches!(pb, Value::String(_)) { // Both are primitives, so this converts them without running script code; a Symbol // throws. @@ -3007,12 +3008,14 @@ impl Interp { /// The abstract relational comparison `x < y` (section 11.8.5): `None` when either side is /// NaN. fn less_than(&mut self, x: &Value, y: &Value, left_first: bool) -> JsResult> { + // The second conversion can run script, so the first one's result stays counted. let (px, py) = if left_first { let px = self.to_primitive(x, Hint::Number)?; - (px, self.to_primitive(y, Hint::Number)?) + let py = self.rooted(std::slice::from_ref(&px), |i| i.to_primitive(y, Hint::Number))?; + (px, py) } else { let py = self.to_primitive(y, Hint::Number)?; - (self.to_primitive(x, Hint::Number)?, py) + (self.rooted(std::slice::from_ref(&py), |i| i.to_primitive(x, Hint::Number))?, py) }; if let (Value::String(a), Value::String(b)) = (&px, &py) { // Both stay rooted, so the first copy is still counted while the second is made. diff --git a/crates/js/src/symbol_tests.rs b/crates/js/src/symbol_tests.rs index a11c105107..6ea28e1dee 100644 --- a/crates/js/src/symbol_tests.rs +++ b/crates/js/src/symbol_tests.rs @@ -283,6 +283,57 @@ fn a_symbol_key_from_to_primitive_counts_against_the_heap_while_the_property_is_ } } +#[test] +fn define_properties_keeps_a_symbol_key_counted_after_a_getter_deletes_it() { + // The second Symbol-keyed descriptor's getter deletes the first property, drops `s` and makes + // a string of 1,500,000 units. With `keep` (1,000,000 units) and the first Symbol's + // description of 2,000,000 units still counted, that string does not fit; with a short + // description it does. + let src = |description: &str, define: &str| { + format!( + "function mk(n) {{ return new Array(n + 1).join('x'); }} \ + var keep = mk(1000000), made = null, s = Symbol({description}), props = {{}}; \ + props[s] = {{ value: 1, enumerable: true }}; \ + Object.defineProperty(props, Symbol('t'), {{ enumerable: true, get: function () {{ \ + delete props[s]; s = null; made = mk(1500000); return {{ value: 2 }}; }} }}); \ + var o = {define}; o[Object.getOwnPropertySymbols(o)[0]] + ' ' + made.length" + ) + }; + let limits = || Limits { heap_bytes: 8 << 20, fuel: 50_000_000, ..Limits::default() }; + for define in ["Object.defineProperties({}, props)", "Object.create(null, props)"] { + assert_eq!(es2015(limits()).run(&src("'d'", define)), Ok(JsValue::String("1 1500000".into())), "{define}"); + match es2015(limits()).run(&src("mk(2000000)", define)) { + Err(RunError { kind: RunErrorKind::Limit(LimitKind::Heap), .. }) => {} + other => panic!("{define}: {other:?}"), + } + } +} + +#[test] +fn an_operator_keeps_the_first_operand_converted_while_it_converts_the_second() { + // `a` converts to a Symbol with a description of 2,000,000 units that nothing else holds, and + // `b`'s conversion makes a string of 1,500,000 units. With `keep` (1,000,000 units) and the + // description still counted, that string does not fit; when `a` gives a number it does. + let src = |first: &str, op: &str| { + format!( + "function mk(n) {{ return new Array(n + 1).join('x'); }} \ + var keep = mk(1000000), made = null; \ + var a = {{}}; a[Symbol.toPrimitive] = function () {{ return {first}; }}; \ + var b = {{}}; b[Symbol.toPrimitive] = function () {{ made = mk(1500000); return 1; }}; \ + try {{ a {op} b; }} catch (e) {{ if (!(e instanceof TypeError)) throw e; }} \ + made.length === 1500000" + ) + }; + let limits = || Limits { heap_bytes: 8 << 20, fuel: 50_000_000, ..Limits::default() }; + for op in ["+", "<", ">", "<=", ">="] { + assert_eq!(es2015(limits()).run(&src("(Symbol(mk(2000000)), 0)", op)), Ok(JsValue::Bool(true)), "{op}"); + match es2015(limits()).run(&src("Symbol(mk(2000000))", op)) { + Err(RunError { kind: RunErrorKind::Limit(LimitKind::Heap), .. }) => {} + other => panic!("{op}: {other:?}"), + } + } +} + #[test] fn date_objects_convert_through_their_to_primitive_method() { let date = "var d = new Date(0); d.valueOf = function () { return 7; }; d.toString = function () { return 'date'; };";