JavaScript: keep Symbol keys and converted operands counted while script runs

Object.defineProperties and Object.create now keep the Symbol keys of the
properties object rooted until they are defined, so a getter that deletes
a Symbol-keyed property already read no longer lets a collection drop the
Symbol's description from the heap count.

The + operator and the relational comparisons now keep the first
operand's converted value rooted while the second operand's
@@toPrimitive or valueOf runs, so a fresh Symbol or string from the first
conversion stays counted against the heap limit.
This commit is contained in:
dotCooCoo committed 2026-11-22 02:35:00 -08:00
1 parent e475181329
commit 93df144331
3 files changed
+66 -4

No files matched your search

+9 -1
View File
@@ -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;
+6 -3
View File
@@ -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<Option<bool>> {
// 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.
+51
View File
@@ -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'; };";