From 09150f35e9e1d70277641221a66221a3e63ca847 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 06:27:56 -0800 Subject: [PATCH] Scripts: host object handles count toward the heap limit Each object of a host class keeps its handle twice, once in its slot and once in the realm's index of the objects scripts hold, but creating one reserved only the object's own bytes, and the collector's live-byte total left the handle out. A host value with a long handle could therefore grow the heap past the script's limit. The object now reserves its handle's bytes before it is made, and the collector counts them for every live object. Tests cover a handle larger than the remaining budget and the bytes 100 retained objects add. --- crates/js/src/heap.rs | 1 + crates/js/src/host_classes.rs | 11 ++++++- crates/js/src/host_tests.rs | 57 +++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/crates/js/src/heap.rs b/crates/js/src/heap.rs index c64973350d..0d0d7acb19 100644 --- a/crates/js/src/heap.rs +++ b/crates/js/src/heap.rs @@ -1185,6 +1185,7 @@ fn trace_object(o: &Object, work: &mut Vec, strings: &mut StringSizes) -> u extra = std::mem::size_of::>() + PROP_BYTES * fields.len(); } Class::UnitValue(_) => extra = UNIT_VALUE_BYTES, + Class::Host(h) => extra = crate::host_classes::handle_bytes(h.handle.len()), Class::Promise(p) => { for v in p.values() { mark_value(&v, work, strings); diff --git a/crates/js/src/host_classes.rs b/crates/js/src/host_classes.rs index d00e569afb..f45f4f079b 100644 --- a/crates/js/src/host_classes.rs +++ b/crates/js/src/host_classes.rs @@ -118,6 +118,13 @@ pub(crate) struct HostObj { pub(crate) handle: Rc<[f64]>, } +/// The estimated bytes an object of a host class holds beyond [`crate::heap::OBJECT_BYTES`] for a +/// handle of `len` numbers: the handle in its slot and the copy in the realm's index of the objects +/// scripts hold. +pub(crate) fn handle_bytes(len: usize) -> usize { + len.saturating_mul(2 * std::mem::size_of::()) +} + /// A class the realm has. #[derive(Clone)] pub(crate) struct Entry { @@ -274,10 +281,12 @@ impl Interp { } return Ok(Value::Object(id)); } - self.reserve_heap(crate::heap::OBJECT_BYTES)?; + let extra = handle_bytes(handle.len()); + self.reserve_heap(crate::heap::OBJECT_BYTES.saturating_add(extra))?; let mut o = Object::new(Class::Host(HostObj { class, handle: handle.into() }), Some(proto)); o.extensible = false; let id = self.heap.alloc_object(o); + self.heap.bytes = self.heap.bytes.saturating_add(extra); self.host_objects.insert(made, id); Ok(Value::Object(id)) } diff --git a/crates/js/src/host_tests.rs b/crates/js/src/host_tests.rs index c3d6ae275c..410489b2c5 100644 --- a/crates/js/src/host_tests.rs +++ b/crates/js/src/host_tests.rs @@ -1081,6 +1081,63 @@ fn an_evaluators_whole_source_must_fit_in_the_heap_before_it_is_parsed() { assert_eq!(r.run("host.evaluate()").unwrap_err().kind, RunErrorKind::Thrown); } +/// A realm with the host class `Big` and `host.make(i, len)`, which gives the object of `Big` whose +/// handle is `i` followed by `len - 1` zeros. +fn big_handles() -> Realm { + let mut r = realm(10_000_000); + let make: HostFunction = Rc::new(|args| { + let (Some(Data::Number(i)), Some(Data::Number(len))) = (args.get(1), args.get(2)) else { panic!("{args:?}") }; + let mut handle = vec![0.0; *len as usize]; + handle[0] = *i; + Ok(host_ref("Big", &handle)) + }); + let big = HostClass { name: "Big".into(), parent: None, members: vec![], info: Vec::new(), index: None, construct: None }; + let maker = HostClass { + name: "Maker".into(), + parent: None, + members: vec![("make".into(), HostMember::Method(make))], + info: Vec::new(), + index: None, + construct: None, + }; + r.define_host_classes(vec![big, maker], vec![("host".into(), host_ref("Maker", &[0.0]))]).unwrap(); + r +} + +#[test] +fn a_host_objects_handle_must_fit_in_the_heap() { + let len = 8192; + let mut r = big_handles(); + r.interp().collect_for_room(); + // Room for the object and the call, and not for its handle. + let heap_bytes = r.interp().heap.bytes + crate::heap::OBJECT_BYTES + (32 << 10); + assert!(crate::host_classes::handle_bytes(len) > 2 * (32 << 10)); + r.set_limits(Limits { fuel: 10_000_000, heap_bytes, ..Limits::default() }); + assert_eq!(r.run(&format!("host.make(1, {len})")).unwrap_err().kind, RunErrorKind::Limit(LimitKind::Heap)); + r.set_limits(Limits { fuel: 10_000_000, ..Limits::default() }); + assert!(r.run(&format!("host.make(1, {len})")).is_ok()); +} + +#[test] +fn each_host_objects_handle_counts_toward_the_heap() { + let mut r = big_handles(); + // The growth from 100 objects to 200, so the script's own code and strings cancel out. + let mut per_hundred = |len: usize| { + let mut grown = |count: usize| { + r.run("var kept = [];").unwrap(); + let before = r.collect_garbage(); + r.run(&format!("for (var i = 0; i < {count}; i++) kept.push(host.make(i, {len}));")).unwrap(); + r.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); + assert_eq!(long - short, 100 * crate::host_classes::handle_bytes(64)); +} + proptest::proptest! { /// A slice size does not change what a script computes. #[test]