mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-24 11:23:43 +01:00
JavaScript: a function body reserves room for its arguments object
The call reserves room for the declarations and the arguments object before the body's let and const environment takes some of it. The body then reserved room for its declarations again, but not for the arguments object, so making the object could take the heap estimate past the limit until the next check. The body now reserves both, with the arguments size from one helper that the call also uses.
This commit is contained in:
1 parent
d61d494bea
commit
54b9504cb5
2 files changed
+53
-5
No files matched your search
@@ -704,6 +704,48 @@ fn declarations_are_checked_after_the_let_and_const_names_take_their_room() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_arguments_object_is_checked_after_the_let_and_const_names_take_their_room() {
|
||||
// The `let` names and the `arguments` object of 1,000 arguments each fit in the heap, but not
|
||||
// always together. Each run stops for lack of fuel at a different point while the variables
|
||||
// after the object are made, before a collection could correct the estimate, and the
|
||||
// estimate is within the limit at every one of them.
|
||||
let names = |prefix: &str, n: usize| (0..n).map(|i| format!("{prefix}{i:0>60}")).collect::<Vec<_>>().join(", ");
|
||||
let (lets, vars) = (names("l", 300), names("v", 100));
|
||||
let zeros = vec!["0"; 1000].join(", ");
|
||||
let setup = format!("function f() {{ let {lets}; var {vars}; return arguments.length; }} function g() {{ return f({zeros}); }} 0");
|
||||
let call = "g()";
|
||||
let mut measure = Realm::with_options(es_options(), Limits::default());
|
||||
assert_eq!(measure.run(&setup), Ok(JsValue::Number(0.0)));
|
||||
measure.interp().collect_garbage();
|
||||
let base = measure.interp().heap.bytes;
|
||||
assert_eq!(measure.run(call), Ok(JsValue::Number(1000.0)));
|
||||
let fuel = Limits::default().fuel - measure.interp().fuel;
|
||||
let mut stopped = 0;
|
||||
for extra in (0..150).map(|k| k * 2048) {
|
||||
let heap_bytes = base + extra;
|
||||
let mut realm = Realm::with_options(es_options(), Limits { heap_bytes, ..Limits::default() });
|
||||
if realm.run(&setup).is_err() {
|
||||
continue;
|
||||
}
|
||||
// The variables are made before the body runs its 300 `let` declarations, which take
|
||||
// about 700 units of fuel, so every third stop in the last 800 units finds them.
|
||||
for f in (fuel.saturating_sub(800).max(1)..=fuel).step_by(3) {
|
||||
realm.interp().collect_garbage();
|
||||
realm.set_limits(Limits { heap_bytes, fuel: f, ..Limits::default() });
|
||||
match realm.run(call) {
|
||||
Ok(v) => assert_eq!(v, JsValue::Number(1000.0), "{heap_bytes}, {f}"),
|
||||
Err(e) => {
|
||||
assert!(matches!(e.kind, RunErrorKind::Limit(LimitKind::Heap | LimitKind::Fuel)), "{heap_bytes}, {f}: {e:?}");
|
||||
stopped += usize::from(e.kind == RunErrorKind::Limit(LimitKind::Heap));
|
||||
}
|
||||
}
|
||||
assert!(realm.interp().heap.bytes <= heap_bytes, "{heap_bytes}, {f}: {}", realm.interp().heap.bytes);
|
||||
}
|
||||
}
|
||||
assert!(stopped > 0, "no run reached the heap limit");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_scopes_survive_collections_while_they_are_made() {
|
||||
// Heaps just big enough make the collector run while function bodies and blocks set up their
|
||||
|
||||
+11
-5
@@ -463,6 +463,12 @@ fn declaration_bytes(code: &Code) -> usize {
|
||||
code.vars.iter().fold(funs, |total, v| total.saturating_add(binding_bytes(v.len())))
|
||||
}
|
||||
|
||||
/// 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.
|
||||
fn arguments_bytes(args: usize) -> usize {
|
||||
OBJECT_BYTES.saturating_add(args.saturating_add(4).saturating_mul(binding_bytes(10)))
|
||||
}
|
||||
|
||||
/// A call to a function written in JavaScript, ready to enter.
|
||||
struct Entry {
|
||||
callee: ObjId,
|
||||
@@ -2818,8 +2824,7 @@ impl Interp {
|
||||
return Err(Abrupt::Limit(LimitKind::Frames));
|
||||
}
|
||||
let params = code.params.iter().fold(0usize, |total, p| total.saturating_add(binding_bytes(p.len())));
|
||||
let arguments =
|
||||
if code.uses_arguments { OBJECT_BYTES.saturating_add(args.len().saturating_add(4).saturating_mul(binding_bytes(10))) } else { 0 };
|
||||
let arguments = if code.uses_arguments { 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) => {
|
||||
@@ -2930,9 +2935,10 @@ impl Interp {
|
||||
let has = |interp: &Self, name: &[u16]| matches!(interp.heap.env(env), Some(Env::Decl { bindings, .. }) if bindings.contains(name));
|
||||
let lex = self.top_env(code, env)?;
|
||||
self.temp_envs.push(lex);
|
||||
// The call reserved room for the declarations before the `let` and `const` environment
|
||||
// took some of it.
|
||||
self.reserve_heap(declaration_bytes(code))?;
|
||||
// The call reserved room for the declarations and the `arguments` object before the `let`
|
||||
// and `const` environment took some of it.
|
||||
let arguments = if needs_arguments && !has(self, &key("arguments")) { arguments_bytes(args.len()) } else { 0 };
|
||||
self.reserve_heap(declaration_bytes(code).saturating_add(arguments))?;
|
||||
for (name, fi) in &code.fun_decls {
|
||||
self.use_fuel()?;
|
||||
let Some(fcode) = code.functions.get(*fi as usize).cloned() else { return internal("function out of range") };
|
||||
|
||||
Reference in new issue
Block a user