Skip to content

Truly independent web browser

LibWasm: Reserve room for function results in interpreter frames

Tim Ledbetter7fbaf42cc7b3
diff --git a/Libraries/LibWasm/Interpreter/interpreter.flap b/Libraries/LibWasm/Interpreter/interpreter.flap
index f24f830798c..ae8345197f3 100644
--- a/Libraries/LibWasm/Interpreter/interpreter.flap
+++ b/Libraries/LibWasm/Interpreter/interpreter.flap
@@ -788,10 +788,21 @@ handler Enter() @opcode(0xf0) {
load64(function, [record, ENTRY_FUNCTION]);
let mut param_slots: u64;
load32(param_slots, [function, FUNCTION_PARAM_SLOTS]);
+ let mut result_slots: u64;
+ load32(result_slots, [function, FUNCTION_RESULT_SLOTS]);
+ let mut operand_slots: u64 = param_slots;
+ if result_slots > param_slots {
+ operand_slots = result_slots;
+ }
let entry_sp: u64 = sp;
let runtime_fp = align_down_16(entry_sp - FRAME_HEADER_SIZE - SPILL_AREA_SIZE);
let arguments_top = runtime_fp - FRAME_HEADER_SIZE - (param_slots << 3);
- let new_sp = align_down_16(arguments_top - SPILL_AREA_SIZE);
+ let new_sp = align_down_16(runtime_fp - FRAME_HEADER_SIZE - (operand_slots << 3) - SPILL_AREA_SIZE);
+ let mut limit: u64;
+ load64(limit, [context_pointer, CONTEXT_STACK_LIMIT]);
+ guard new_sp >= limit else @cold {
+ trap_with(TRAP_STACK_EXHAUSTED, context_pointer);
+ };
# The native code that entered the interpreter touched the stack above its frame, but maybe not the page of the
# entry frame's stack pointer.
let _ = atomic_load8([entry_sp, 0]);
diff --git a/Libraries/LibWasm/Module/src/validator.rs b/Libraries/LibWasm/Module/src/validator.rs
index f667a6cbd06..3b74430d29c 100644
--- a/Libraries/LibWasm/Module/src/validator.rs
+++ b/Libraries/LibWasm/Module/src/validator.rs
@@ -2461,7 +2461,7 @@ pub fn validate(module: &mut Module) -> Result<()> {
catch_clauses: validator.catch_clauses.into_boxed_slice(),
local_count: validator.locals.len() as u32 + validator.exception_local_count,
exception_local_count: validator.exception_local_count,
- max_stack_slots: validator.max_slot_height,
+ max_stack_slots: validator.max_slot_height.max(slots_of(&function.results)),
});
}
module.function_infos = infos;
diff --git a/Libraries/LibWasm/Tests/Executor/test-frame-results-reservation.js b/Libraries/LibWasm/Tests/Executor/test-frame-results-reservation.js
new file mode 100644
index 00000000000..083bd98bf88
--- /dev/null
+++ b/Libraries/LibWasm/Tests/Executor/test-frame-results-reservation.js
@@ -0,0 +1,16 @@
+const module = parseWebAssemblyModule(readBinaryWasmFile("Fixtures/Modules/frame-results-reservation.wasm"));
+const call = (name, ...args) => module.invoke(module.getExport(name), ...args);
+const values = Array.from({ length: 64 }, (_, index) => index);
+
+test("an exception caught at the function's own label returns its values", () => {
+ expect(call("catch_values")).toEqual(values);
+});
+
+test("a function continued compiled at a loop returns the results of its tail call", () => {
+ call("call_values", 10000);
+ expect(call("loop_then_tail_call_values")).toEqual(values);
+});
+
+test("a function called from the host returns its results once it runs compiled", () => {
+ for (let round = 0; round < 1000; ++round) expect(call("values")).toEqual(values);
+});
diff --git a/Libraries/LibWasm/Tests/Fixtures/Modules/frame-results-reservation.wasm b/Libraries/LibWasm/Tests/Fixtures/Modules/frame-results-reservation.wasm
new file mode 100644
index 00000000000..a9b78142d85
Binary files /dev/null and b/Libraries/LibWasm/Tests/Fixtures/Modules/frame-results-reservation.wasm differ
diff --git a/Libraries/LibWasm/Tests/Fixtures/Modules/frame-results-reservation.wat b/Libraries/LibWasm/Tests/Fixtures/Modules/frame-results-reservation.wat
new file mode 100644
index 00000000000..6b0762678ad
--- /dev/null
+++ b/Libraries/LibWasm/Tests/Fixtures/Modules/frame-results-reservation.wat
@@ -0,0 +1,47 @@
+(module
+ (type $values (func (result
+ i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
+ i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
+ i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32)))
+ (tag $thrown (param
+ i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
+ i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
+ i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32))
+ (global $exception (mut exnref) (ref.null exn))
+
+ (func $values (export "values") (type $values)
+ (i32.const 0) (i32.const 1) (i32.const 2) (i32.const 3) (i32.const 4) (i32.const 5) (i32.const 6)
+ (i32.const 7) (i32.const 8) (i32.const 9) (i32.const 10) (i32.const 11) (i32.const 12) (i32.const 13)
+ (i32.const 14) (i32.const 15) (i32.const 16) (i32.const 17) (i32.const 18) (i32.const 19) (i32.const 20)
+ (i32.const 21) (i32.const 22) (i32.const 23) (i32.const 24) (i32.const 25) (i32.const 26) (i32.const 27)
+ (i32.const 28) (i32.const 29) (i32.const 30) (i32.const 31) (i32.const 32) (i32.const 33) (i32.const 34)
+ (i32.const 35) (i32.const 36) (i32.const 37) (i32.const 38) (i32.const 39) (i32.const 40) (i32.const 41)
+ (i32.const 42) (i32.const 43) (i32.const 44) (i32.const 45) (i32.const 46) (i32.const 47) (i32.const 48)
+ (i32.const 49) (i32.const 50) (i32.const 51) (i32.const 52) (i32.const 53) (i32.const 54) (i32.const 55)
+ (i32.const 56) (i32.const 57) (i32.const 58) (i32.const 59) (i32.const 60) (i32.const 61) (i32.const 62)
+ (i32.const 63))
+
+ (func $store_exception
+ (block $caught (result exnref)
+ (try_table (catch_all_ref $caught)
+ (throw $thrown (call $values)))
+ (unreachable))
+ (global.set $exception))
+ (start $store_exception)
+
+ (func (export "call_values") (param $count i32)
+ (loop $again
+ (block (call $values) (br 0))
+ (br_if $again (local.tee $count (i32.sub (local.get $count) (i32.const 1))))))
+
+ (func (export "catch_values") (type $values)
+ (try_table (catch $thrown 0)
+ (throw_ref (global.get $exception)))
+ (unreachable))
+
+ (func (export "loop_then_tail_call_values") (type $values)
+ (local $count i32)
+ (local.set $count (i32.const 100000))
+ (loop $again
+ (br_if $again (local.tee $count (i32.sub (local.get $count) (i32.const 1)))))
+ (return_call $values)))