Ruby  3.4.0dev (2024-11-22 revision 0989400a925cd201defdca9eb28eb87200b30785)
setjmp.h
1 #ifndef RB_WASM_SUPPORT_SETJMP_H
2 #define RB_WASM_SUPPORT_SETJMP_H
3 
4 #include "ruby/internal/config.h"
5 #include <stdbool.h>
6 
7 #ifndef WASM_SETJMP_STACK_BUFFER_SIZE
8 # define WASM_SETJMP_STACK_BUFFER_SIZE 6144
9 #endif
10 
12  void* top;
13  void* end;
14  char buffer[WASM_SETJMP_STACK_BUFFER_SIZE];
15 };
16 
17 typedef struct {
18  // Internal Asyncify buffer space to save execution context
19  struct __rb_wasm_asyncify_jmp_buf setjmp_buf;
20  // Internal Asyncify buffer space used while unwinding from longjmp
21  // but never used for rewinding.
22  struct __rb_wasm_asyncify_jmp_buf *longjmp_buf_ptr;
23  // Used to save top address of Asyncify stack `setjmp_buf`, which is
24  // overwritten during first rewind.
25  void *dst_buf_top;
26  // A payload value given by longjmp and returned by setjmp for the second time
27  int payload;
28  // Internal state field
29  int state;
31 
32 // noinline to avoid breaking Asyncify assumption
33 NOINLINE(int _rb_wasm_setjmp(rb_wasm_jmp_buf *env));
34 NOINLINE(void _rb_wasm_longjmp(rb_wasm_jmp_buf *env, int payload));
35 
36 #define rb_wasm_setjmp(env) ((env).state = 0, _rb_wasm_setjmp(&(env)))
37 
38 // NOTE: Why is `_rb_wasm_longjmp` not `noreturn`? Why put `unreachable` in the call site?
39 // Asyncify expects that `_rb_wasm_longjmp` returns its control, and Asyncify inserts a return
40 // for unwinding after the call. This means that "`_rb_wasm_longjmp` returns its control but the
41 // next line in the caller (C level) won't be executed".
42 // On the other hand, `noreturn` means the callee won't return its control to the caller,
43 // so compiler can assume that a function with the attribute won't reach the end of the function.
44 // Therefore `_rb_wasm_longjmp`'s semantics is not exactly same as `noreturn`.
45 #define rb_wasm_longjmp(env, payload) (_rb_wasm_longjmp(&env, payload), __builtin_unreachable())
46 
47 // Returns the Asyncify buffer of next rewinding if unwound for setjmp capturing or longjmp.
48 // Used by the top level Asyncify handling in wasm/runtime.c
49 void *rb_wasm_handle_jmp_unwind(void);
50 
51 
52 //
53 // POSIX-compatible declarations
54 //
55 
56 typedef rb_wasm_jmp_buf jmp_buf;
57 
58 #define setjmp(env) rb_wasm_setjmp(env)
59 #define longjmp(env, payload) rb_wasm_longjmp(env, payload)
60 
61 
62 typedef void (*rb_wasm_try_catch_func_t)(void *ctx);
63 
65  rb_wasm_try_catch_func_t try_f;
66  rb_wasm_try_catch_func_t catch_f;
67  void *context;
68  int state;
69 };
70 
71 //
72 // Lightweight try-catch API without unwinding to root frame.
73 //
74 
75 void
76 rb_wasm_try_catch_init(struct rb_wasm_try_catch *try_catch,
77  rb_wasm_try_catch_func_t try_f,
78  rb_wasm_try_catch_func_t catch_f,
79  void *context);
80 
81 // Run, catch longjmp thrown by run, and re-catch longjmp thrown by catch, ...
82 //
83 // 1. run try_f of try_catch struct
84 // 2. catch longjmps with the given target jmp_buf or exit
85 // 3. run catch_f if not NULL, otherwise exit
86 // 4. catch longjmps with the given target jmp_buf or exit
87 // 5. repeat from step 3
88 //
89 // NOTICE: This API assumes that all longjmp targeting the given jmp_buf are NOT called
90 // after the function that called this function has exited.
91 //
92 void
93 rb_wasm_try_catch_loop_run(struct rb_wasm_try_catch *try_catch, rb_wasm_jmp_buf *target);
94 
95 #endif
C99 shim for <stdbool.h>