Skip to content

Configure a new stack in cabi_realloc - #854

Merged
alexcrichton merged 1 commit into
WebAssembly:mainfrom
alexcrichton:configure-stack-poitner-in-cabi-realloc
Jul 27, 2026
Merged

Configure a new stack in cabi_realloc#854
alexcrichton merged 1 commit into
WebAssembly:mainfrom
alexcrichton:configure-stack-poitner-in-cabi-realloc

Conversation

@alexcrichton

Copy link
Copy Markdown
Collaborator

This commit is an update to the cabi_realloc export configured by wasi-libc, specifically for wasip3 targets. The change here is to change the entrypoint from a C function to a small assembly wrapper which configures the stack pointer and TLS base before calling into C. This is to account for the spec change in WebAssembly/component-model#680 where realloc hooks are now going to be invoked with context slots set to 0. This means that cabi_realloc runs with no stack, and this would quickly abort in wasi-libc otherwise.

The change here is to, when the ABI is using context slots for the stack pointer, have an assembly wrapper that configures a known-good stack and TLS block for cabi_realloc to use. Once configured the normal C-based cabi_realloc routine runs. This is tested against bytecodealliance/wasmtime#13949 to ensure it works on the p3 target with/without coop threading and before/after that Wasmtime change.

@alexcrichton
alexcrichton deleted the configure-stack-poitner-in-cabi-realloc branch July 23, 2026 23:15
@alexcrichton
alexcrichton restored the configure-stack-poitner-in-cabi-realloc branch July 23, 2026 23:15
@alexcrichton alexcrichton reopened this Jul 23, 2026
@alexcrichton

Copy link
Copy Markdown
Collaborator Author

Wrong button...

@alexcrichton
alexcrichton requested a review from dicej July 23, 2026 23:16
@alexcrichton

Copy link
Copy Markdown
Collaborator Author

Oh right, I should also mention that I've updated the implementation of __wasm_init_async_task here to be the same as __wasm_init_task. After some discussion earlier this morning it was concluded that everything, sync and async, can all use the same stack because of reentrancy rules in the component model. Async tasks will return from the call stack when blocking to allow reentrance and otherwise do not allow reentrance while they're on the stack

This commit is an update to the `cabi_realloc` export configured by
wasi-libc, specifically for wasip3 targets. The change here is to change
the entrypoint from a C function to a small assembly wrapper which
configures the stack pointer and TLS base before calling into C. This is
to account for the spec change in WebAssembly/component-model#680 where
`realloc` hooks are now going to be invoked with context slots set to 0.
This means that `cabi_realloc` runs with no stack, and this would
quickly abort in wasi-libc otherwise.

The change here is to, when the ABI is using `context` slots for the
stack pointer, have an assembly wrapper that configures a known-good
stack and TLS block for `cabi_realloc` to use. Once configured the
normal C-based `cabi_realloc` routine runs. This is tested against
bytecodealliance/wasmtime#13949 to ensure it works on the p3 target
with/without coop threading and before/after that Wasmtime change.
@alexcrichton
alexcrichton force-pushed the configure-stack-poitner-in-cabi-realloc branch from 339af6a to ffedbd4 Compare July 24, 2026 23:58
@@ -0,0 +1,21 @@
// This is a "surgical" augmentation for the generated `wasip{2,3}.c` files in
// wasi-libc which changes the behavior of the generated `cabi_realloc`. Ideally
// `wit-bindgen` would have a mode that doesn't generate `cabi_realloc`, but that

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is there an issue for this? It would be nice to remove the need for this "surgery" sooner rather than later.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not currently, no

@alexcrichton
alexcrichton merged commit f3e8728 into WebAssembly:main Jul 27, 2026
36 checks passed
@alexcrichton
alexcrichton deleted the configure-stack-poitner-in-cabi-realloc branch July 27, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants