mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-24 18:32:23 +00:00
JavaScript: spreading a string counts its flattened copy against the heap
Spreading a concatenated string (`[...s]`, `f(...s)`) flattened it before any limit was checked, so a script could make the host build the whole copy before the per-element fuel and heap accounting ran. The spread now flattens through the same counted helper the string built-ins use, which ends the run with the heap limit before the copy is made.
This commit is contained in:
@@ -364,6 +364,23 @@ fn spread_and_templates_stay_within_the_limits() {
|
||||
throws("var a = []; a.length = 4294967294; [0, ...a];", "RangeError");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spreading_a_rope_counts_its_flattened_length_first() {
|
||||
// `mk` returns a rope whose left half is a flat string of about half the heap, held by
|
||||
// nothing else. Its flattened copy is the other half, so the two don't fit together. Making
|
||||
// the left half takes about 16,400 fuel, and the elements read before the heap fills would
|
||||
// take more than the rest, so only the check before the copy ends the run with the heap limit.
|
||||
let build = "var s = 'x'; for (var i = 0; i < 18; i++) s = s + s; function f(c) { return s + c; } \
|
||||
function mk() { var a = f('a'); a[0]; return a + 'b'; }";
|
||||
let limits = Limits { heap_bytes: 4 * 262_146, fuel: 20_000, ..Limits::default() };
|
||||
for spread in ["[...mk()]", "Math.max(...mk())"] {
|
||||
let e = Realm::with_options(es_options(), limits.clone()).run(&format!("{build} {spread}; 0")).unwrap_err();
|
||||
assert_eq!(e.kind, RunErrorKind::Limit(LimitKind::Heap), "{spread}: {e:?}");
|
||||
}
|
||||
let made = Realm::with_options(es_options(), limits).run(&format!("{build} mk(); 0"));
|
||||
assert_eq!(made, Ok(JsValue::Number(0.0)), "the rope is made within the fuel");
|
||||
}
|
||||
|
||||
#[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
|
||||
|
||||
@@ -2292,13 +2292,14 @@ impl Interp {
|
||||
|
||||
/// Appends the elements of the value on top to the array under it: an array's elements by
|
||||
/// index (holes read as `undefined`), a string's characters by code point, and an object with
|
||||
/// a `length` by index. Any other value is a `TypeError`. Each element takes a unit of fuel.
|
||||
/// a `length` by index. Any other value is a `TypeError`. Each element takes a unit of fuel,
|
||||
/// and a concatenated string's flattened copy is counted against the heap before it is made.
|
||||
fn op_array_spread(&mut self) -> JsResult<()> {
|
||||
let v = self.peek(0)?;
|
||||
let Value::Object(a) = self.peek(1)? else { return internal("spread into something that is not an array") };
|
||||
let items: Vec<Value> = match &v {
|
||||
Value::String(s) => {
|
||||
let units = s.flat();
|
||||
let units = crate::string_builtins::flat_charged(self, s)?;
|
||||
let mut out = Vec::new();
|
||||
let mut held = HeldBytes::new(self);
|
||||
let mut i = 0;
|
||||
|
||||
Reference in New Issue
Block a user