|
| 1 | +From 253a1835b07a291a84348ae812e61c8fde222962 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Yuta Saito < [email protected]> |
| 3 | +Date: Sun, 12 Nov 2023 07:18:01 +0900 |
| 4 | +Subject: [PATCH] [wasm] allocate Asyncify setjmp buffer in heap |
| 5 | + |
| 6 | +`rb_jmpbuf_t` type is considerably large due to inline-allocated |
| 7 | +Asyncify buffer, and it leads to stack overflow even with small number |
| 8 | +of C-method call frames. This commit allocates the Asyncify buffer used |
| 9 | +by `rb_wasm_setjmp` in heap to mitigate the issue. |
| 10 | + |
| 11 | +This patch introduces a new type `rb_vm_tag_jmpbuf_t` to abstract the |
| 12 | +representation of a jump buffer, and init/deinit hook points to manage |
| 13 | +lifetime of the buffer. These changes are effectively NFC for non-wasm |
| 14 | +platforms. |
| 15 | +--- |
| 16 | + eval_intern.h | 6 ++++-- |
| 17 | + vm.c | 2 +- |
| 18 | + vm_core.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++++-- |
| 19 | + 3 files changed, 62 insertions(+), 5 deletions(-) |
| 20 | + |
| 21 | +diff --git a/eval_intern.h b/eval_intern.h |
| 22 | +index 778b63e0ea..d008b17ca1 100644 |
| 23 | +--- a/eval_intern.h |
| 24 | ++++ b/eval_intern.h |
| 25 | +@@ -110,9 +110,11 @@ extern int select_large_fdset(int, fd_set *, fd_set *, fd_set *, struct timeval |
| 26 | + _tag.tag = Qundef; \ |
| 27 | + _tag.prev = _ec->tag; \ |
| 28 | + _tag.lock_rec = rb_ec_vm_lock_rec(_ec); \ |
| 29 | ++ rb_vm_tag_jmpbuf_init(&_tag.buf); \ |
| 30 | + |
| 31 | + #define EC_POP_TAG() \ |
| 32 | + _ec->tag = _tag.prev; \ |
| 33 | ++ rb_vm_tag_jmpbuf_deinit(&_tag.buf); \ |
| 34 | + } while (0) |
| 35 | + |
| 36 | + #define EC_TMPPOP_TAG() \ |
| 37 | +@@ -161,7 +163,7 @@ rb_ec_tag_jump(const rb_execution_context_t *ec, enum ruby_tag_type st) |
| 38 | + { |
| 39 | + RUBY_ASSERT(st != TAG_NONE); |
| 40 | + ec->tag->state = st; |
| 41 | +- ruby_longjmp(ec->tag->buf, 1); |
| 42 | ++ ruby_longjmp(RB_VM_TAG_JMPBUF_GET(ec->tag->buf), 1); |
| 43 | + } |
| 44 | + |
| 45 | + /* |
| 46 | +@@ -169,7 +171,7 @@ rb_ec_tag_jump(const rb_execution_context_t *ec, enum ruby_tag_type st) |
| 47 | + [ISO/IEC 9899:1999] 7.13.1.1 |
| 48 | + */ |
| 49 | + #define EC_EXEC_TAG() \ |
| 50 | +- (UNLIKELY(ruby_setjmp(_tag.buf)) ? rb_ec_tag_state(VAR_FROM_MEMORY(_ec)) : (EC_REPUSH_TAG(), 0)) |
| 51 | ++ (UNLIKELY(ruby_setjmp(RB_VM_TAG_JMPBUF_GET(_tag.buf))) ? rb_ec_tag_state(VAR_FROM_MEMORY(_ec)) : (EC_REPUSH_TAG(), 0)) |
| 52 | + |
| 53 | + #define EC_JUMP_TAG(ec, st) rb_ec_tag_jump(ec, st) |
| 54 | + |
| 55 | +diff --git a/vm.c b/vm.c |
| 56 | +index 7f43484905..789e0956be 100644 |
| 57 | +--- a/vm.c |
| 58 | ++++ b/vm.c |
| 59 | +@@ -2462,7 +2462,7 @@ vm_exec(rb_execution_context_t *ec) |
| 60 | + |
| 61 | + rb_wasm_try_catch_init(&try_catch, vm_exec_bottom_main, vm_exec_bottom_rescue, &ctx); |
| 62 | + |
| 63 | +- rb_wasm_try_catch_loop_run(&try_catch, &_tag.buf); |
| 64 | ++ rb_wasm_try_catch_loop_run(&try_catch, &RB_VM_TAG_JMPBUF_GET(_tag.buf)); |
| 65 | + |
| 66 | + result = ctx.result; |
| 67 | + #else |
| 68 | +diff --git a/vm_core.h b/vm_core.h |
| 69 | +index acad6280be..45290c21f7 100644 |
| 70 | +--- a/vm_core.h |
| 71 | ++++ b/vm_core.h |
| 72 | +@@ -884,6 +884,61 @@ typedef RUBY_JMP_BUF rb_jmpbuf_t; |
| 73 | + typedef void *rb_jmpbuf_t[5]; |
| 74 | + #endif |
| 75 | + |
| 76 | ++/* |
| 77 | ++ `rb_vm_tag_jmpbuf_t` type represents a buffer used to |
| 78 | ++ long jump to a C frame associated with `rb_vm_tag`. |
| 79 | ++ |
| 80 | ++ Use-site of `rb_vm_tag_jmpbuf_t` is responsible for calling the |
| 81 | ++ following functions: |
| 82 | ++ - `rb_vm_tag_jmpbuf_init` once `rb_vm_tag_jmpbuf_t` is allocated. |
| 83 | ++ - `rb_vm_tag_jmpbuf_deinit` once `rb_vm_tag_jmpbuf_t` is no longer necessary. |
| 84 | ++ |
| 85 | ++ `RB_VM_TAG_JMPBUF_GET` transforms a `rb_vm_tag_jmpbuf_t` into a |
| 86 | ++ `rb_jmpbuf_t` to be passed to `rb_setjmp/rb_longjmp`. |
| 87 | ++*/ |
| 88 | ++#if defined(__wasm__) && !defined(__EMSCRIPTEN__) |
| 89 | ++/* |
| 90 | ++ WebAssembly target with Asyncify-based SJLJ needs |
| 91 | ++ to capture the execution context by unwind/rewind-ing |
| 92 | ++ call frames into a jump buffer. The buffer space tends |
| 93 | ++ to be considerably large unlike other architectures' |
| 94 | ++ register-based buffers. |
| 95 | ++ Therefore, we allocates the buffer on the heap on such |
| 96 | ++ environments. |
| 97 | ++*/ |
| 98 | ++typedef rb_jmpbuf_t *rb_vm_tag_jmpbuf_t; |
| 99 | ++ |
| 100 | ++#define RB_VM_TAG_JMPBUF_GET(buf) (*buf) |
| 101 | ++ |
| 102 | ++inline void |
| 103 | ++rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf) |
| 104 | ++{ |
| 105 | ++ *jmpbuf = malloc(sizeof(rb_jmpbuf_t)); |
| 106 | ++} |
| 107 | ++ |
| 108 | ++inline void |
| 109 | ++rb_vm_tag_jmpbuf_deinit(const rb_vm_tag_jmpbuf_t *jmpbuf) |
| 110 | ++{ |
| 111 | ++ free(*jmpbuf); |
| 112 | ++} |
| 113 | ++#else |
| 114 | ++typedef rb_jmpbuf_t rb_vm_tag_jmpbuf_t; |
| 115 | ++ |
| 116 | ++#define RB_VM_TAG_JMPBUF_GET(buf) (buf) |
| 117 | ++ |
| 118 | ++inline void |
| 119 | ++rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf) |
| 120 | ++{ |
| 121 | ++ // no-op |
| 122 | ++} |
| 123 | ++ |
| 124 | ++inline void |
| 125 | ++rb_vm_tag_jmpbuf_deinit(const rb_vm_tag_jmpbuf_t *jmpbuf) |
| 126 | ++{ |
| 127 | ++ // no-op |
| 128 | ++} |
| 129 | ++#endif |
| 130 | ++ |
| 131 | + /* |
| 132 | + the members which are written in EC_PUSH_TAG() should be placed at |
| 133 | + the beginning and the end, so that entire region is accessible. |
| 134 | +@@ -891,7 +946,7 @@ typedef void *rb_jmpbuf_t[5]; |
| 135 | + struct rb_vm_tag { |
| 136 | + VALUE tag; |
| 137 | + VALUE retval; |
| 138 | +- rb_jmpbuf_t buf; |
| 139 | ++ rb_vm_tag_jmpbuf_t buf; |
| 140 | + struct rb_vm_tag *prev; |
| 141 | + enum ruby_tag_type state; |
| 142 | + unsigned int lock_rec; |
| 143 | +@@ -899,7 +954,7 @@ struct rb_vm_tag { |
| 144 | + |
| 145 | + STATIC_ASSERT(rb_vm_tag_buf_offset, offsetof(struct rb_vm_tag, buf) > 0); |
| 146 | + STATIC_ASSERT(rb_vm_tag_buf_end, |
| 147 | +- offsetof(struct rb_vm_tag, buf) + sizeof(rb_jmpbuf_t) < |
| 148 | ++ offsetof(struct rb_vm_tag, buf) + sizeof(rb_vm_tag_jmpbuf_t) < |
| 149 | + sizeof(struct rb_vm_tag)); |
| 150 | + |
| 151 | + struct rb_unblock_callback { |
| 152 | +-- |
| 153 | +2.39.3 (Apple Git-145) |
| 154 | + |
0 commit comments