Skip to content

Commit 99f5000

Browse files
Merge pull request #302 from ruby/katei/mitigate-stackoverflow-part2
Mitigate stack-overflow issue take 2
2 parents c5052f2 + d85b9ea commit 99f5000

File tree

6 files changed

+193
-24
lines changed

6 files changed

+193
-24
lines changed

ext/witapi/witapi-core.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,20 @@ rb_eval_string_value_protect(VALUE str, int *pstate) {
6060

6161
__attribute__((import_module("asyncify"), import_name("start_unwind"))) void
6262
asyncify_start_unwind(void *buf);
63+
#define asyncify_start_unwind(buf) \
64+
do { \
65+
extern void *rb_asyncify_unwind_buf; \
66+
rb_asyncify_unwind_buf = (buf); \
67+
asyncify_start_unwind((buf)); \
68+
} while (0)
6369
__attribute__((import_module("asyncify"), import_name("stop_unwind"))) void
6470
asyncify_stop_unwind(void);
71+
#define asyncify_stop_unwind() \
72+
do { \
73+
extern void *rb_asyncify_unwind_buf; \
74+
rb_asyncify_unwind_buf = NULL; \
75+
asyncify_stop_unwind(); \
76+
} while (0)
6577
__attribute__((import_module("asyncify"), import_name("start_rewind"))) void
6678
asyncify_start_rewind(void *buf);
6779
__attribute__((import_module("asyncify"), import_name("stop_rewind"))) void
@@ -114,6 +126,8 @@ rb_wasm_throw_prohibit_rewind_exception(const char *c_msg, size_t msg_len);
114126
void *asyncify_buf = NULL; \
115127
extern void *rb_asyncify_unwind_buf; \
116128
void *asyncify_unwound_buf = rb_asyncify_unwind_buf; \
129+
if (asyncify_unwound_buf == NULL) \
130+
break; \
117131
asyncify_stop_unwind(); \
118132
\
119133
if ((asyncify_buf = rb_wasm_handle_jmp_unwind()) != NULL) { \
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
import { main } from "@ruby/wasm-wasi/dist/browser.script"
22
import * as pkg from "../package.json"
33

4-
main(pkg)
4+
main(pkg, {
5+
env: {
6+
// WORKAROUND(katei): setjmp consumes a LOT of stack in Ruby 3.2,
7+
// so we extend default Fiber stack size as well as main stack
8+
// size allocated by wasm-ld's --stack-size. 8MB is enough for
9+
// most cases. See https://github.com/ruby/ruby.wasm/issues/273
10+
"RUBY_FIBER_MACHINE_STACK_SIZE": "8388608"
11+
}
12+
})

packages/npm-packages/ruby-wasm-wasi/src/browser.script.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { DefaultRubyVM } from "./browser";
22

3-
export const main = async (pkg: { name: string; version: string }) => {
3+
export const main = async (
4+
pkg: { name: string; version: string },
5+
options?: Parameters<typeof DefaultRubyVM>[1],
6+
) => {
47
const response = fetch(
58
`https://cdn.jsdelivr.net/npm/${pkg.name}@${pkg.version}/dist/ruby+stdlib.wasm`,
69
);
710
const module = await compileWebAssemblyModule(response);
8-
const { vm } = await DefaultRubyVM(module);
11+
const { vm } = await DefaultRubyVM(module, options);
912

1013
vm.printVersion();
1114

packages/npm-packages/ruby-wasm-wasi/src/browser.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,23 @@ const consolePrinter = () => {
6262

6363
export const DefaultRubyVM = async (
6464
rubyModule: WebAssembly.Module,
65-
options: { consolePrint: boolean } = { consolePrint: true },
65+
options: {
66+
consolePrint?: boolean;
67+
env?: Record<string, string> | undefined;
68+
} = {},
6669
): Promise<{
6770
vm: RubyVM;
6871
wasi: WASI;
6972
instance: WebAssembly.Instance;
7073
}> => {
7174
await init();
7275

73-
const wasi = new WASI({
74-
env: {
75-
// FIXME(katei): setjmp consumes a LOT of stack now, so we extend
76-
// default Fiber stack size as well as main stack size allocated
77-
// by wasm-ld's --stack-size. The ideal solution is to reduce
78-
// stack consumption in setjmp.
79-
RUBY_FIBER_MACHINE_STACK_SIZE: "16777216",
80-
},
81-
});
76+
const wasi = new WASI({ env: options.env });
8277
const vm = new RubyVM();
8378

8479
const imports = wasi.getImports(rubyModule) as WebAssembly.Imports;
8580
vm.addToImports(imports);
86-
const printer = options.consolePrint ? consolePrinter() : undefined;
81+
const printer = (options.consolePrint ?? true) ? consolePrinter() : undefined;
8782
printer?.addToImports(imports);
8883

8984
const instance = await WebAssembly.instantiate(rubyModule, imports);

packages/npm-packages/ruby-wasm-wasi/src/node.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import { WASI } from "wasi";
22
import { RubyVM } from "./index";
33

4-
export const DefaultRubyVM = async (rubyModule: WebAssembly.Module) => {
5-
const wasi = new WASI({
6-
env: {
7-
// FIXME(katei): setjmp consumes a LOT of stack now, so we extend
8-
// default Fiber stack size as well as main stack size allocated
9-
// by wasm-ld's --stack-size. The ideal solution is to reduce
10-
// stack consumption in setjmp.
11-
RUBY_FIBER_MACHINE_STACK_SIZE: "16777216",
12-
},
13-
});
4+
export const DefaultRubyVM = async (
5+
rubyModule: WebAssembly.Module,
6+
options: { env?: Record<string, string> | undefined } = {},
7+
) => {
8+
const wasi = new WASI({ env: options.env });
149
const vm = new RubyVM();
1510
const imports = {
1611
wasi_snapshot_preview1: wasi.wasiImport,
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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

Comments
 (0)