From 9482a5a36f5c84efc224ec648f28fa7f5e7391bd Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 13:49:29 -0800 Subject: [PATCH] Scripts: an arguments object's map of argument names counts toward the heap limit An `arguments` object keeps a map with one entry per argument, which the heap estimate left out at allocation and at collection, so a script that kept many of them could grow past the heap limit. The object now reserves and charges the map, the collector counts it, and the call's up-front reservation includes it (arrow functions, which make no `arguments` object, reserve none). Same accounting as #9928 gave host object handles. --- crates/js/src/heap.rs | 7 ++++++- crates/js/src/interp.rs | 12 +++++++++--- crates/js/src/interp_tests.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/crates/js/src/heap.rs b/crates/js/src/heap.rs index 29a8a9987a..c8ae5a8c11 100644 --- a/crates/js/src/heap.rs +++ b/crates/js/src/heap.rs @@ -698,6 +698,8 @@ pub(crate) const ENV_BYTES: usize = 64; pub(crate) const ROPE_BYTES: usize = 48; /// The estimated bytes of each argument a bound function holds. pub(crate) const BOUND_ARG_BYTES: usize = 24; +/// The estimated bytes of each entry an `arguments` object holds in its map of argument names. +pub(crate) const ARG_NAME_BYTES: usize = std::mem::size_of::>(); /// The estimated bytes of an XML node without its names and text. pub(crate) const XML_NODE_BYTES: usize = 160; /// The estimated bytes of an XML list without its items. @@ -1097,7 +1099,10 @@ fn trace_object(o: &Object, work: &mut Vec, strings: &mut StringSizes) -> u } extra = BOUND_ARG_BYTES * args.len(); } - Class::Arguments(map) => work.push(map.env.0), + Class::Arguments(map) => { + work.push(map.env.0); + extra = ARG_NAME_BYTES * map.names.len(); + } Class::String(s) => strings.add(s), Class::RegExp(re) => { if strings.seen.insert(Rc::as_ptr(re) as *const () as usize) { diff --git a/crates/js/src/interp.rs b/crates/js/src/interp.rs index 89821e7734..196e9323b3 100644 --- a/crates/js/src/interp.rs +++ b/crates/js/src/interp.rs @@ -561,9 +561,11 @@ fn scope_bytes(code: &Code, i: u32) -> usize { } /// At least the bytes that the `arguments` object of a call with `args` arguments adds to the -/// heap: the object, an element for each argument and its other properties. +/// heap: the object, its map of argument names, an element for each argument and its other +/// properties. fn arguments_bytes(args: usize) -> usize { - OBJECT_BYTES.saturating_add(args.saturating_add(4).saturating_mul(binding_bytes(10))) + let names = crate::heap::ARG_NAME_BYTES.saturating_mul(args); + OBJECT_BYTES.saturating_add(names).saturating_add(args.saturating_add(4).saturating_mul(binding_bytes(10))) } /// A call to a function written in JavaScript, ready to enter; `new_target` is set when it is @@ -3158,7 +3160,8 @@ impl Interp { return Err(Abrupt::Limit(LimitKind::Frames)); } let params = code.params.iter().chain(&code.pattern_names).fold(0usize, |total, p| total.saturating_add(binding_bytes(p.len()))); - let arguments = if code.uses_arguments { arguments_bytes(args.len()) } else { 0 }; + // An arrow function reads the `arguments` object of the function around it. + let arguments = if code.uses_arguments && !code.arrow { arguments_bytes(args.len()) } else { 0 }; // The array a rest parameter gets: its binding and one property for each extra argument. let rest = match &code.rest { Some(name) => { @@ -3373,7 +3376,10 @@ impl Interp { } } let proto = self.intrinsics.object_proto; + let extra = crate::heap::ARG_NAME_BYTES.saturating_mul(names.len()); + self.reserve_heap(crate::heap::OBJECT_BYTES.saturating_add(extra))?; let a = self.heap.alloc_object(Object::new(Class::Arguments(Box::new(ArgMap { env, names })), Some(proto))); + self.heap.bytes = self.heap.bytes.saturating_add(extra); for (i, v) in args.iter().enumerate() { self.define(a, index_key(u32::try_from(i).unwrap_or(u32::MAX)), Prop::Data { value: v.clone(), flags: OPEN })?; } diff --git a/crates/js/src/interp_tests.rs b/crates/js/src/interp_tests.rs index 75f240a6d6..2dc3008630 100644 --- a/crates/js/src/interp_tests.rs +++ b/crates/js/src/interp_tests.rs @@ -600,6 +600,35 @@ fn a_lower_heap_limit_applies_to_what_is_already_live() { assert_eq!(limit(&mut realm, "var more = []; for (var j = 0; j < 20000; j++) more[j] = {j: j};"), LimitKind::Heap); } +#[test] +fn each_arguments_objects_name_map_counts_toward_the_heap() { + let mut realm = Realm::new(); + realm.run("function f() { return arguments; }").unwrap(); + // The growth from 100 objects to 200, each with its elements deleted, so the script's own code + // and strings cancel out and only the map of argument names stays. + let mut per_hundred = |len: usize| { + let mut grown = |count: usize| { + realm.run("var kept = []; var a = null;").unwrap(); + let before = realm.collect_garbage(); + let src = format!( + "var args = []; for (var j = 0; j < {len}; j++) args[j] = 0; \ + for (var i = 0; i < {count}; i++) {{ var a = f.apply(null, args); \ + for (var j = 0; j < {len}; j++) delete a[j]; kept.push(a); }} args = null;" + ); + realm.run(&src).unwrap(); + realm.collect_garbage() - before + }; + grown(200) - grown(100) + }; + // The first runs also leave bindings and names the later ones reuse. + per_hundred(1); + let short = per_hundred(1); + let long = per_hundred(65); + // The longer runs also keep up to a property's worth more per object besides the names. + let names = 100 * 64 * crate::heap::ARG_NAME_BYTES; + assert!((names..names + 100 * crate::heap::PROP_BYTES).contains(&(long - short)), "{} of {names}", long - short); +} + #[test] fn apply_keeps_its_arguments_alive_while_a_native_function_runs() { // The first argument's toString makes 20,000 objects, more than a 1 MiB heap holds, so a