Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ jobs:
wasmtime run build/qjs -qd
echo "console.log('hello wasi!');" > t.js
wasmtime run --dir . build/qjs t.js
wasmtime run --dir . build/qjs --stack-size 64K tests/wasi-stack-limit.js
wasmtime run --dir . build/qjs tests/wasi-stack-limit.js
- name: build wasi reactor
run: |
cmake -B build -DQJS_BUILD_WERROR=ON -DCMAKE_TOOLCHAIN_FILE=/opt/wasi-sdk/share/cmake/wasi-sdk.cmake -DQJS_WASI_REACTOR=ON
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
_WASI_EMULATED_SIGNAL
)
add_link_options(
-Wl,-z,stack-size=2097152
-lwasi-emulated-process-clocks
-lwasi-emulated-signal
)
Expand Down
9 changes: 1 addition & 8 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2368,9 +2368,6 @@ JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque)
rt->js_class_id_alloc = JS_CLASS_INIT_COUNT;

rt->stack_size = JS_DEFAULT_STACK_SIZE;
#ifdef __wasi__
rt->stack_size = 0;
#endif

JS_UpdateStackTop(rt);

Expand Down Expand Up @@ -3122,15 +3119,11 @@ JSRuntime *JS_GetRuntime(JSContext *ctx)

static void update_stack_limit(JSRuntime *rt)
{
#if defined(__wasi__)
rt->stack_limit = 0; /* no limit */
#else
if (rt->stack_size == 0) {
if (rt->stack_size == 0 || rt->stack_top < rt->stack_size) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For posterity (and to call it out), the rt->stack_top < rt->stack_size check is correct because if:

  1. rt->stack_top == rt->stack_size then rt->stack_top - rt->stack_size == 0, meaning no limit

  2. rt->stack_top > rt->stack_size then rt->stack_top - rt->stack_size wraps around to some huge value much larger than the actual stack size, which in practice means "no limit" as well

rt->stack_limit = 0; /* no limit */
} else {
rt->stack_limit = rt->stack_top - rt->stack_size;
}
#endif
Comment thread
bnoordhuis marked this conversation as resolved.
}

void JS_SetMaxStackSize(JSRuntime *rt, size_t stack_size)
Expand Down
22 changes: 22 additions & 0 deletions tests/wasi-stack-limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
let overflow;

// Use a large interpreter frame so QuickJS's linear-memory stack guard trips
// before Wasmtime's independent WebAssembly call stack is exhausted.
const args = Array(1024).fill("0").join(",");
const recurse = Function(
`return function recurse() { recurse(${args}); }`
)();

try {
recurse();
} catch (error) {
overflow = error;
}

if (!(overflow instanceof Error) ||
!/stack/i.test(overflow.message)) {
throw new Error(`expected a stack overflow, got ${overflow}`);
}

if (Function("return 20 + 22")() !== 42)
throw new Error("runtime is unusable after stack overflow");
Loading