aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-17 12:48:53 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-10-17 12:48:53 +0200
commit1aa6444759c2e564f5b99c0cd65f7c2bb134758a (patch)
treea6095c80eed46cc62abf8a709d0a89ff05410466
parentaa5f6a215d3676a184a5d199acfbe7ee08ba7378 (diff)
Add Trap Handling
We implemented trap handing with setjmp/longjmp. This _should_ be very widely supported, but it was _one_ nicety of WAMR: it did not rely on this mechanism and would instead return errors on runtime calls in case of a trap. The implementation is fairly straightforward: we set a global `jmp_buf` when installing a handler and longjmp in case of a trap. Note that the handler also keeps the trap code, because the C standard only requires `if (setjmp(buf))` to be supported, i.e. "the entire controlling expression of `if`", meaning even if it does return the code passed to `longjmp`, we can't really do anything with it. Added a `Esc` key handler in the PICO-8 stub to check traps work, and they do :).
-rw-r--r--src/game.c12
-rw-r--r--src/picow.c3
-rw-r--r--src/wasmrt.c63
-rw-r--r--src/wasmrt.h38
-rw-r--r--stub/pico8.wat7
5 files changed, 116 insertions, 7 deletions
diff --git a/src/game.c b/src/game.c
index 4803fc0..fa031a0 100644
--- a/src/game.c
+++ b/src/game.c
@@ -126,9 +126,21 @@ bool pw_game_iterate(struct pw_game *game) {
pw_fb_iterate(game->fb);
pw_input_iterate(game->input, game->fb);
+ struct wasm_rt_trap_handler_t handler;
+ if (!wasm_rt_trap_register(&handler)) {
+ return false;
+ }
+
+ if (wasm_rt_trapped(&handler)) {
+ return SDL_SetError("PICO-8 execution trapped: %s",
+ wasm_rt_trap_str(handler.code));
+ }
+
w2c_pico8_dynCall_v(&game->instance, game->env->main_loop);
w2c_pico8_0x5Fmix_0(&game->instance, 42, 1337);
+ wasm_rt_trap_unregister();
+
return true;
}
diff --git a/src/picow.c b/src/picow.c
index 32e54b4..ef4b726 100644
--- a/src/picow.c
+++ b/src/picow.c
@@ -106,7 +106,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
return failf("failed to initialize cart selection state");
}
- state->game = pw_game_init(state->renderer, state->config->mode, state->config->memory);
+ state->game =
+ pw_game_init(state->renderer, state->config->mode, state->config->memory);
if (!state->game) {
return failf("failed to initialize PICO-8 game state");
}
diff --git a/src/wasmrt.c b/src/wasmrt.c
index 23e0c16..831b0ca 100644
--- a/src/wasmrt.c
+++ b/src/wasmrt.c
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <SDL3/SDL_endian.h>
+#include <SDL3/SDL_error.h>
#include <SDL3/SDL_log.h>
#include <SDL3/SDL_stdinc.h>
@@ -12,10 +13,64 @@ bool wasm_rt_is_initialized(void) {
return true;
}
-void wasm_rt_trap(enum wasm_rt_trap_t trap) {
- (void)trap;
- SDL_LogCritical(0, "trap!\n");
- abort();
+static struct wasm_rt_trap_handler_t *wasm_rt_trap_handler = NULL;
+
+void wasm_rt_trap(enum wasm_rt_trap_t code) {
+ struct wasm_rt_trap_handler_t *handler = wasm_rt_trap_handler;
+ if (!handler) {
+ SDL_LogCritical(0, "trapped without registering a handler: %s",
+ wasm_rt_trap_str(code));
+ abort();
+ }
+
+ if (!code) {
+ SDL_LogWarn(0, "attempted to trap without a code");
+ code = WASM_RT_TRAP_UNREACHABLE;
+ }
+
+ handler->code = code;
+ longjmp(handler->env, code);
+}
+
+char const *wasm_rt_trap_str(enum wasm_rt_trap_t code) {
+ switch (code) {
+ case WASM_RT_TRAP_OOB:
+ return "out-of-bounds memory access";
+ case WASM_RT_TRAP_INT_OVERFLOW:
+ return "arithmetic overflow";
+ case WASM_RT_TRAP_DIV_BY_ZERO:
+ return "division by zero";
+ case WASM_RT_TRAP_INVALID_CONVERSION:
+ return "invalid value conversion";
+ case WASM_RT_TRAP_UNREACHABLE:
+ return "unreachable";
+ case WASM_RT_TRAP_CALL_INDIRECT:
+ return "invalid indirect call";
+ default:
+ return "";
+ }
+}
+
+bool wasm_rt_trap_register(struct wasm_rt_trap_handler_t *handler) {
+ if (!handler) {
+ return SDL_InvalidParamError("handler");
+ }
+
+ if (wasm_rt_trap_handler) {
+ return SDL_SetError("Wasm runtime trap handler already set");
+ }
+
+ wasm_rt_trap_handler = handler;
+ wasm_rt_trap_handler->code = WASM_RT_TRAP_NONE;
+ return true;
+}
+
+void wasm_rt_trap_unregister(void) {
+ if (!wasm_rt_trap_handler) {
+ SDL_SetError("Wasm runtime trap handler not set");
+ }
+
+ wasm_rt_trap_handler = NULL;
}
void wasm_rt_memory_check(struct wasm_rt_memory_t const *mem, uint32_t addr,
diff --git a/src/wasmrt.h b/src/wasmrt.h
index 85cc6ca..aaf2624 100644
--- a/src/wasmrt.h
+++ b/src/wasmrt.h
@@ -1,6 +1,8 @@
#ifndef PW_WASMRT_H
#define PW_WASMRT_H
+#include <setjmp.h>
+
#include <SDL3/SDL_stdinc.h>
#if SDL_HAS_BUILTIN(__builtin_expect)
@@ -131,6 +133,16 @@ typedef struct wasm_rt_externref_table_t {
} wasm_rt_externref_table_t;
/**
+ * A trap handling context.
+ *
+ * This is created by the caller for catching traps during wasm execution.
+ */
+typedef struct wasm_rt_trap_handler_t {
+ jmp_buf env;
+ volatile wasm_rt_trap_t code;
+} wasm_rt_trap_handler_t;
+
+/**
* Returns whether Web Assembly runtime is initialized.
*/
bool wasm_rt_is_initialized(void);
@@ -138,7 +150,31 @@ bool wasm_rt_is_initialized(void);
/**
* IT'S A TRAP!
*/
-WASM_RT_NO_RETURN void wasm_rt_trap(wasm_rt_trap_t code);
+WASM_RT_NO_RETURN void wasm_rt_trap(enum wasm_rt_trap_t code);
+
+/**
+ * Returns a string name for the trap.
+ */
+char const *wasm_rt_trap_str(enum wasm_rt_trap_t code);
+
+/**
+ * Registers a trap handler for a call, returning false if another handler is
+ * already registered.
+ */
+bool wasm_rt_trap_register(struct wasm_rt_trap_handler_t *handler);
+
+/**
+ * Unregisters the trap handler. Sets an error if no handler was registered.
+ */
+void wasm_rt_trap_unregister(void);
+
+/**
+ * Returns whether or not a call trapped.
+ *
+ * It is undefined behaviour to call this macro without first registering the
+ * trap handler.
+ */
+#define wasm_rt_trapped(h) setjmp((h)->env)
#define wasm_rt_memcpy SDL_memcpy
#define wasm_rt_funcref_null_value \
diff --git a/stub/pico8.wat b/stub/pico8.wat
index db28ce5..8029559 100644
--- a/stub/pico8.wat
+++ b/stub/pico8.wat
@@ -93,7 +93,12 @@
(if (i32.eq (local.get $sym) (i32.const 120))
(then
(global.set $key_x
- (i32.eq (local.get $type) (i32.const 0x300)))))))
+ (i32.eq (local.get $type) (i32.const 0x300)))))
+ (if
+ (i32.and
+ (i32.eq (local.get $sym) (i32.const 27))
+ (i32.eq (local.get $type) (i32.const 0x300)))
+ (then unreachable))))
(br $poll_loop)))
(local.set $buttons