diff options
author | Yuta Saito <kateinoigakukun@gmail.com> | 2022-01-15 23:10:48 +0900 |
---|---|---|
committer | Yuta Saito <kateinoigakukun@gmail.com> | 2022-01-19 11:19:06 +0900 |
commit | 65f95f26ff0e7b4be4704fedc52344a26d22a4e2 (patch) | |
tree | e4bd17869d8dd479855592f3a571aacae19e964c /wasm/setjmp.h | |
parent | e41b121e94ccce9877824e55f865885bbabe40c3 (diff) | |
download | ruby-65f95f26ff0e7b4be4704fedc52344a26d22a4e2.tar.gz |
[wasm] add asyncify based setjmp, fiber, register scan emulation
configure.ac: setup build tools and register objects
main.c: wrap main with rb_wasm_rt_start to handle asyncify unwinds
tool/m4/ruby_wasm_tools.m4: setup default command based on WASI_SDK_PATH
environment variable. checks wasm-opt which is used for asyncify.
tool/wasm-clangw wasm/wasm-opt: a clang wrapper which replaces real
wasm-opt with do-nothing wasm-opt to avoid misoptimization before
asyncify. asyncify is performed at POSTLINK, but clang linker driver
tries to run optimization by wasm-opt unconditionally. inlining pass
at wasm level breaks asyncify's assumption, so should not optimize
before POSTLIK.
wasm/GNUmakefile.in: wasm specific rules to compile objects
Diffstat (limited to 'wasm/setjmp.h')
-rw-r--r-- | wasm/setjmp.h | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/wasm/setjmp.h b/wasm/setjmp.h new file mode 100644 index 0000000000..30ea23ca12 --- /dev/null +++ b/wasm/setjmp.h @@ -0,0 +1,61 @@ +#ifndef RB_WASM_SUPPORT_SETJMP_H +#define RB_WASM_SUPPORT_SETJMP_H + +#include "ruby/internal/config.h" +#include <stdbool.h> + +#ifndef WASM_SETJMP_STACK_BUFFER_SIZE +# define WASM_SETJMP_STACK_BUFFER_SIZE 6144 +#endif + +struct __rb_wasm_asyncify_jmp_buf { + void* top; + void* end; + char buffer[WASM_SETJMP_STACK_BUFFER_SIZE]; +}; + +typedef struct { + // Internal Asyncify buffer space to save execution context + struct __rb_wasm_asyncify_jmp_buf setjmp_buf; + // Internal Asyncify buffer space used while unwinding from longjmp + // but never used for rewinding. + struct __rb_wasm_asyncify_jmp_buf longjmp_buf; + // Used to save top address of Asyncify stack `setjmp_buf`, which is + // overwritten during first rewind. + void *dst_buf_top; + // A payload value given by longjmp and returned by setjmp for the second time + int payload; + // Internal state field + int state; +} rb_wasm_jmp_buf; + +// noinline to avoid breaking Asyncify assumption +NOINLINE(int _rb_wasm_setjmp(rb_wasm_jmp_buf *env)); +NOINLINE(void _rb_wasm_longjmp(rb_wasm_jmp_buf *env, int payload)); + +#define rb_wasm_setjmp(env) ((env).state = 0, _rb_wasm_setjmp(&(env))) + +// NOTE: Why is `_rb_wasm_longjmp` not `noreturn`? Why put `unreachable` in the call site? +// Asyncify expects that `_rb_wasm_longjmp` returns its control, and Asyncify inserts a return +// for unwinding after the call. This means that "`_rb_wasm_longjmp` returns its control but the +// next line in the caller (C level) won't be executed". +// On the other hand, `noreturn` means the callee won't return its control to the caller, +// so compiler can assume that a function with the attribute won't reach the end of the function. +// Therefore `_rb_wasm_longjmp`'s semantics is not exactly same as `noreturn`. +#define rb_wasm_longjmp(env, payload) (_rb_wasm_longjmp(&env, payload), __builtin_unreachable()) + +// Returns the Asyncify buffer of next rewinding if unwound for setjmp capturing or longjmp. +// Used by the top level Asyncify handling in wasm/runtime.c +void *rb_wasm_handle_jmp_unwind(void); + + +// +// POSIX-compatible declarations +// + +typedef rb_wasm_jmp_buf jmp_buf; + +#define setjmp(env) rb_wasm_setjmp(env) +#define longjmp(env, payload) rb_wasm_longjmp(env, payload) + +#endif |