[Bug #20566] Mention out-of-range argument cases in `String#<<`
[ruby.git] / wasm / setjmp.c
blobebbf8949c1ecf79225c2315b098a68f998d049af
1 /*
2 This is a WebAssembly userland setjmp/longjmp implementation based on Binaryen's Asyncify.
3 Inspired by Alon Zakai's snippet released under the MIT License:
4 * https://github.com/kripken/talks/blob/991fb1e4b6d7e4b0ea6b3e462d5643f11d422771/jmp.c
6 WebAssembly doesn't have context-switching mechanism for now, so emulate it by Asyncify,
7 which transforms WebAssembly binary to unwind/rewind the execution point and store/restore
8 locals.
10 The basic concept of this implementation is:
11 1. setjmp captures the current execution context by unwinding to the root frame, then immediately
12 rewind to the setjmp call using the captured context. The context is saved in jmp_buf.
13 2. longjmp unwinds to the root frame and rewinds to a setjmp call re-using a passed jmp_buf.
15 This implementation also supports switching context across different call stack (non-standard)
17 This approach is good at behavior reproducibility and self-containedness compared to Emscripten's
18 JS exception approach. However this is super expensive because Asyncify inserts many glue code to
19 control execution point in userland.
21 This implementation will be replaced with future stack-switching feature.
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <assert.h>
26 #include <stdbool.h>
27 #include "wasm/asyncify.h"
28 #include "wasm/machine.h"
29 #include "wasm/setjmp.h"
31 #ifdef RB_WASM_ENABLE_DEBUG_LOG
32 # include <wasi/api.h>
33 # include <unistd.h>
34 // NOTE: We can't use printf() and most of library function that are
35 // Asyncified due to the use of them in the application itself.
36 // Use of printf() causes "unreachable" error because Asyncified
37 // function misunderstands Asyncify's internal state during
38 // start_unwind()...stop_unwind() and start_rewind()...stop_rewind().
39 # define RB_WASM_DEBUG_LOG_INTERNAL(msg) do { \
40 const uint8_t *msg_start = (uint8_t *)msg; \
41 const uint8_t *msg_end = msg_start; \
42 for (; *msg_end != '\0'; msg_end++) {} \
43 __wasi_ciovec_t iov = {.buf = msg_start, .buf_len = msg_end - msg_start}; \
44 size_t nwritten; \
45 __wasi_fd_write(STDERR_FILENO, &iov, 1, &nwritten); \
46 } while (0)
47 # define RB_WASM_DEBUG_LOG(msg) \
48 RB_WASM_DEBUG_LOG_INTERNAL(__FILE__ ":" STRINGIZE(__LINE__) ": " msg "\n")
49 #else
50 # define RB_WASM_DEBUG_LOG(msg)
51 #endif
53 enum rb_wasm_jmp_buf_state {
54 // Initial state
55 JMP_BUF_STATE_INITIALIZED = 0,
56 // Unwinding to the root or rewinding to the setjmp call
57 // to capture the current execution context
58 JMP_BUF_STATE_CAPTURING = 1,
59 // Ready for longjmp
60 JMP_BUF_STATE_CAPTURED = 2,
61 // Unwinding to the root or rewinding to the setjmp call
62 // to restore the execution context
63 JMP_BUF_STATE_RETURNING = 3,
66 void
67 async_buf_init(struct __rb_wasm_asyncify_jmp_buf* buf)
69 buf->top = &buf->buffer[0];
70 buf->end = &buf->buffer[WASM_SETJMP_STACK_BUFFER_SIZE];
73 // Global unwinding/rewinding jmpbuf state
74 static rb_wasm_jmp_buf *_rb_wasm_active_jmpbuf;
75 void *rb_asyncify_unwind_buf;
77 __attribute__((noinline))
78 int
79 _rb_wasm_setjmp_internal(rb_wasm_jmp_buf *env)
81 RB_WASM_DEBUG_LOG("enter _rb_wasm_setjmp_internal");
82 switch (env->state) {
83 case JMP_BUF_STATE_INITIALIZED: {
84 RB_WASM_DEBUG_LOG(" JMP_BUF_STATE_INITIALIZED");
85 env->state = JMP_BUF_STATE_CAPTURING;
86 env->payload = 0;
87 env->longjmp_buf_ptr = NULL;
88 _rb_wasm_active_jmpbuf = env;
89 async_buf_init(&env->setjmp_buf);
90 asyncify_start_unwind(&env->setjmp_buf);
91 return -1; // return a dummy value
93 case JMP_BUF_STATE_CAPTURING: {
94 asyncify_stop_rewind();
95 RB_WASM_DEBUG_LOG(" JMP_BUF_STATE_CAPTURING");
96 env->state = JMP_BUF_STATE_CAPTURED;
97 _rb_wasm_active_jmpbuf = NULL;
98 return 0;
100 case JMP_BUF_STATE_RETURNING: {
101 asyncify_stop_rewind();
102 RB_WASM_DEBUG_LOG(" JMP_BUF_STATE_RETURNING");
103 env->state = JMP_BUF_STATE_CAPTURED;
104 _rb_wasm_active_jmpbuf = NULL;
105 return env->payload;
107 default:
108 assert(0 && "unexpected state");
110 return 0;
113 void
114 _rb_wasm_longjmp(rb_wasm_jmp_buf* env, int value)
116 RB_WASM_DEBUG_LOG("enter _rb_wasm_longjmp");
117 assert(env->state == JMP_BUF_STATE_CAPTURED);
118 assert(value != 0);
119 env->state = JMP_BUF_STATE_RETURNING;
120 env->payload = value;
121 // Asyncify buffer built during unwinding for longjmp will not
122 // be used to rewind, so re-use static-variable.
123 static struct __rb_wasm_asyncify_jmp_buf tmp_longjmp_buf;
124 env->longjmp_buf_ptr = &tmp_longjmp_buf;
125 _rb_wasm_active_jmpbuf = env;
126 async_buf_init(env->longjmp_buf_ptr);
127 asyncify_start_unwind(env->longjmp_buf_ptr);
131 enum try_catch_phase {
132 TRY_CATCH_PHASE_MAIN = 0,
133 TRY_CATCH_PHASE_RESCUE = 1,
136 void
137 rb_wasm_try_catch_init(struct rb_wasm_try_catch *try_catch,
138 rb_wasm_try_catch_func_t try_f,
139 rb_wasm_try_catch_func_t catch_f,
140 void *context)
142 try_catch->state = TRY_CATCH_PHASE_MAIN;
143 try_catch->try_f = try_f;
144 try_catch->catch_f = catch_f;
145 try_catch->context = context;
148 // NOTE: This function is not processed by Asyncify due to a call of asyncify_stop_rewind
149 void
150 rb_wasm_try_catch_loop_run(struct rb_wasm_try_catch *try_catch, rb_wasm_jmp_buf *target)
152 extern void *rb_asyncify_unwind_buf;
153 extern rb_wasm_jmp_buf *_rb_wasm_active_jmpbuf;
155 target->state = JMP_BUF_STATE_CAPTURED;
157 switch ((enum try_catch_phase)try_catch->state) {
158 case TRY_CATCH_PHASE_MAIN:
159 // may unwind
160 try_catch->try_f(try_catch->context);
161 break;
162 case TRY_CATCH_PHASE_RESCUE:
163 if (try_catch->catch_f) {
164 // may unwind
165 try_catch->catch_f(try_catch->context);
167 break;
171 // catch longjmp with target jmp_buf
172 while (rb_asyncify_unwind_buf && _rb_wasm_active_jmpbuf == target) {
173 // do similar steps setjmp does when JMP_BUF_STATE_RETURNING
175 // stop unwinding
176 // (but call stop_rewind to update the asyncify state to "normal" from "unwind")
177 asyncify_stop_rewind();
178 // clear the active jmpbuf because it's already stopped
179 _rb_wasm_active_jmpbuf = NULL;
180 // reset jmpbuf state to be able to unwind again
181 target->state = JMP_BUF_STATE_CAPTURED;
182 // move to catch loop phase
183 try_catch->state = TRY_CATCH_PHASE_RESCUE;
184 if (try_catch->catch_f) {
185 try_catch->catch_f(try_catch->context);
188 // no unwind or unrelated unwind, then exit
192 void *
193 rb_wasm_handle_jmp_unwind(void)
195 RB_WASM_DEBUG_LOG("enter rb_wasm_handle_jmp_unwind");
196 if (!_rb_wasm_active_jmpbuf) {
197 return NULL;
200 switch (_rb_wasm_active_jmpbuf->state) {
201 case JMP_BUF_STATE_CAPTURING:
202 RB_WASM_DEBUG_LOG(" JMP_BUF_STATE_CAPTURING");
203 // save the captured Asyncify stack top
204 _rb_wasm_active_jmpbuf->dst_buf_top = _rb_wasm_active_jmpbuf->setjmp_buf.top;
205 break;
206 case JMP_BUF_STATE_RETURNING:
207 RB_WASM_DEBUG_LOG(" JMP_BUF_STATE_RETURNING");
208 // restore the saved Asyncify stack top
209 _rb_wasm_active_jmpbuf->setjmp_buf.top = _rb_wasm_active_jmpbuf->dst_buf_top;
210 break;
211 default:
212 assert(0 && "unexpected state");
214 return &_rb_wasm_active_jmpbuf->setjmp_buf;