Skip to content

Truly independent web browser

LibWasm: Keep the execution context while unwinding to a calling frame

Tim Ledbetter5463d5630970
diff --git a/Libraries/LibWasm/Interpreter/interpreter.flap b/Libraries/LibWasm/Interpreter/interpreter.flap
index 1d3eadd6618..f24f830798c 100644
--- a/Libraries/LibWasm/Interpreter/interpreter.flap
+++ b/Libraries/LibWasm/Interpreter/interpreter.flap
@@ -292,14 +292,15 @@ inline fn call_host_function(function: u64, return_ip: u64, context_pointer: u64
store64([context_pointer, CONTEXT_ARGUMENT_1], top);
store64([context_pointer, CONTEXT_ARGUMENT_2], return_ip);
store64([context_pointer, CONTEXT_ARGUMENT_3], fp);
- # The helper may clobber every allocatable register, so nothing but its result is kept across it.
+ # The helper may clobber every allocatable register, so nothing but its result and the context is kept across it.
+ let saved_context = context();
ip = return_ip;
let new_top: u64 = call_helper(wasm_runtime_call_host, context_pointer);
guard new_top != 0 else @cold {
trap(TRAP_RUNTIME);
};
if new_top == 1 @cold {
- resume_at_handler();
+ resume_at_handler(saved_context);
}
vsp = new_top;
dispatch;
@@ -315,14 +316,15 @@ inline fn call_compiled_function(function: u64, return_ip: u64, context_pointer:
store64([context_pointer, CONTEXT_ARGUMENT_1], top);
store64([context_pointer, CONTEXT_ARGUMENT_2], return_ip);
store64([context_pointer, CONTEXT_ARGUMENT_3], fp);
- # The helper may clobber every allocatable register, so nothing but its result is kept across it.
+ # The helper may clobber every allocatable register, so nothing but its result and the context is kept across it.
+ let saved_context = context();
ip = return_ip;
let new_top: u64 = call_helper(wasm_runtime_call_compiled, context_pointer);
guard new_top != 0 else @cold {
trap(TRAP_RUNTIME);
};
if new_top == 1 @cold {
- resume_at_handler();
+ resume_at_handler(saved_context);
}
if new_top == 2 @cold {
let pending_context = context();
@@ -336,8 +338,7 @@ inline fn call_compiled_function(function: u64, return_ip: u64, context_pointer:
}
# Continue at the exception handler the runtime found, which it left in the context's arguments.
-inline fn resume_at_handler() {
- let context_pointer = context();
+inline fn resume_at_handler(context_pointer: u64) {
let mut handler_fp: u64;
load64(handler_fp, [context_pointer, CONTEXT_ARGUMENTS]);
let mut handler_ip: u64;
@@ -371,7 +372,7 @@ inline fn throw_exception(tag: u64, exception: u64) {
guard caught != 0 else @cold {
trap(TRAP_RUNTIME);
};
- resume_at_handler();
+ resume_at_handler(context_pointer);
}
handler Throw() @opcode(0x08) {
@@ -905,7 +906,7 @@ handler Loop() @opcode(0x03) {
# stack, or a tail call it made pending.
let results_top: u64 = call_helper(wasm_runtime_tier_up, context_pointer);
if results_top == 1 {
- resume_at_handler();
+ resume_at_handler(context_pointer);
}
if results_top == 2 {
trap(TRAP_RUNTIME);
diff --git a/Libraries/LibWasm/Tests/Executor/test-exception-caught-by-caller.js b/Libraries/LibWasm/Tests/Executor/test-exception-caught-by-caller.js
new file mode 100644
index 00000000000..c894c40267f
--- /dev/null
+++ b/Libraries/LibWasm/Tests/Executor/test-exception-caught-by-caller.js
@@ -0,0 +1,16 @@
+const module = parseWebAssemblyModule(readBinaryWasmFile("Fixtures/Modules/exception-caught-by-caller.wasm"));
+const call = (name, ...args) => module.invoke(module.getExport(name), ...args);
+const values = Array.from({ length: 16 }, (_, index) => index);
+
+test("a handler in the caller receives the values its callee throws", () => {
+ expect(call("catch_from_callee")).toEqual(values);
+});
+
+test("a handler receives the values a compiled function throws to the caller of its caller", () => {
+ call("compile_thrower", 10000);
+ expect(call("catch_from_compiled_callee")).toEqual(values);
+});
+
+test("a handler in the caller receives the values its callee throws once it continues compiled at a loop", () => {
+ expect(call("catch_from_callee_continued_compiled")).toEqual(values);
+});
diff --git a/Libraries/LibWasm/Tests/Fixtures/Modules/exception-caught-by-caller.wasm b/Libraries/LibWasm/Tests/Fixtures/Modules/exception-caught-by-caller.wasm
new file mode 100644
index 00000000000..a9739d68be6
Binary files /dev/null and b/Libraries/LibWasm/Tests/Fixtures/Modules/exception-caught-by-caller.wasm differ
diff --git a/Libraries/LibWasm/Tests/Fixtures/Modules/exception-caught-by-caller.wat b/Libraries/LibWasm/Tests/Fixtures/Modules/exception-caught-by-caller.wat
new file mode 100644
index 00000000000..e0bcd5c2c76
--- /dev/null
+++ b/Libraries/LibWasm/Tests/Fixtures/Modules/exception-caught-by-caller.wat
@@ -0,0 +1,51 @@
+(module
+ (type $values (func (result
+ 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))
+
+ (func $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))
+
+ (func $throw_values
+ (throw $thrown (call $values)))
+
+ (func (export "catch_from_callee") (type $values)
+ (block $caught (type $values)
+ (try_table (catch $thrown $caught)
+ (call $throw_values))
+ (unreachable)))
+
+ (func $throw_values_compiled
+ (throw $thrown (call $values)))
+
+ (func (export "compile_thrower") (param $count i32)
+ (loop $again
+ (block $caught
+ (try_table (catch_all $caught)
+ (call $throw_values_compiled)))
+ (br_if $again (local.tee $count (i32.sub (local.get $count) (i32.const 1))))))
+
+ (func $call_compiled_thrower
+ (call $throw_values_compiled))
+
+ (func (export "catch_from_compiled_callee") (type $values)
+ (block $caught (type $values)
+ (try_table (catch $thrown $caught)
+ (call $call_compiled_thrower))
+ (unreachable)))
+
+ (func $loop_then_throw_values
+ (local $count i32)
+ (local.set $count (i32.const 1000000))
+ (loop $again
+ (br_if $again (local.tee $count (i32.sub (local.get $count) (i32.const 1)))))
+ (throw $thrown (call $values)))
+
+ (func (export "catch_from_callee_continued_compiled") (type $values)
+ (block $caught (type $values)
+ (try_table (catch $thrown $caught)
+ (call $loop_then_throw_values))
+ (unreachable))))
diff --git a/Tests/LibWeb/Text/expected/Wasm/WebAssembly-exception-from-import-caught-in-outer-frame.txt b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-exception-from-import-caught-in-outer-frame.txt
new file mode 100644
index 00000000000..dd78c1b38cd
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-exception-from-import-caught-in-outer-frame.txt
@@ -0,0 +1 @@
+values received by the handler: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
diff --git a/Tests/LibWeb/Text/input/Wasm/WebAssembly-exception-from-import-caught-in-outer-frame.html b/Tests/LibWeb/Text/input/Wasm/WebAssembly-exception-from-import-caught-in-outer-frame.html
new file mode 100644
index 00000000000..606de06f2ea
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Wasm/WebAssembly-exception-from-import-caught-in-outer-frame.html
@@ -0,0 +1,36 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ // (module
+ // (import "m" "tag" (tag $thrown (param i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32)))
+ // (import "m" "throws" (func $throws))
+ // (func $callThrows (call $throws))
+ // (func (export "catchFromCaller") (result i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32)
+ // (block $handler (result i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32)
+ // (try_table (catch $thrown $handler) (call $callThrows))
+ // (unreachable))))
+ const bytes = new Uint8Array([
+ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x2a, 0x03, 0x60, 0x10, 0x7f, 0x7f, 0x7f,
+ 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x00, 0x60, 0x00,
+ 0x00, 0x60, 0x00, 0x10, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
+ 0x7f, 0x7f, 0x7f, 0x7f, 0x02, 0x15, 0x02, 0x01, 0x6d, 0x03, 0x74, 0x61, 0x67, 0x04, 0x00, 0x00,
+ 0x01, 0x6d, 0x06, 0x74, 0x68, 0x72, 0x6f, 0x77, 0x73, 0x00, 0x01, 0x03, 0x03, 0x02, 0x01, 0x02,
+ 0x07, 0x13, 0x01, 0x0f, 0x63, 0x61, 0x74, 0x63, 0x68, 0x46, 0x72, 0x6f, 0x6d, 0x43, 0x61, 0x6c,
+ 0x6c, 0x65, 0x72, 0x00, 0x02, 0x0a, 0x16, 0x02, 0x04, 0x00, 0x10, 0x00, 0x0b, 0x0f, 0x00, 0x02,
+ 0x02, 0x1f, 0x40, 0x01, 0x00, 0x00, 0x00, 0x10, 0x01, 0x0b, 0x00, 0x0b, 0x0b, 0x00, 0x35, 0x04,
+ 0x6e, 0x61, 0x6d, 0x65, 0x01, 0x15, 0x02, 0x00, 0x06, 0x74, 0x68, 0x72, 0x6f, 0x77, 0x73, 0x01,
+ 0x0a, 0x63, 0x61, 0x6c, 0x6c, 0x54, 0x68, 0x72, 0x6f, 0x77, 0x73, 0x03, 0x0c, 0x01, 0x02, 0x01,
+ 0x00, 0x07, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x0b, 0x09, 0x01, 0x00, 0x06, 0x74, 0x68,
+ 0x72, 0x6f, 0x77, 0x6e,
+ ]);
+
+ test(() => {
+ const tag = new WebAssembly.Tag({ parameters: Array(16).fill("i32") });
+ const values = Array.from({ length: 16 }, (_, index) => index);
+ const exports = new WebAssembly.Instance(new WebAssembly.Module(bytes), {
+ m: { tag, throws: () => { throw new WebAssembly.Exception(tag, values); } },
+ }).exports;
+
+ println(`values received by the handler: ${exports.catchFromCaller().join(", ")}`);
+ });
+</script>