Skip to content

Refactor stack management on wasip3 - #870

Open
alexcrichton wants to merge 1 commit into
WebAssembly:mainfrom
alexcrichton:refactor-stack-management
Open

Refactor stack management on wasip3#870
alexcrichton wants to merge 1 commit into
WebAssembly:mainfrom
alexcrichton:refactor-stack-management

Conversation

@alexcrichton

Copy link
Copy Markdown
Collaborator

This commit is a refactor of how wasip3 works specifically with respect to management of the stack and intrinsics used. The end result of this work is that a number of bugs, present in wasi-libc today, are all fixed:

  • Sync-reentrant calls, allowed in the component model, previously used the same stack and stomped over each other.
  • Resource destructors, executed with fresh task state, did not correctly setup a stack to execute on.

The fix for the first issue here is particularly gnarly because it means that stacks are required to be dynamically allocated in some situations. This in turn means that those same stacks need to be deallocated, and prior to this commit there was no great place to doing that. The fix for the second issue is a bit broader than wasi-libc as well as it couldn't be solved exclusively here.

To resolve these problems this commit updates the wasm-tools version required, notably including recent PRs to adjust how linking works to transform __wasm_init_task and __wasm_init_async_task into a more-generic __wasm_task_hook function. This hook function is invoked in more locations, such as resource destructors, and additionally subsumes previous custom logic with cabi_realloc. Effectively __wasm_task_hook is now the sole location concerned with configuring a stack and TLS for the task-to-run. This handles all situations such as sync/async tasks, _initialize, cabi_realloc, resource destructors, and post-return. This was implemented in bytecodealliance/wasm-tools#2603 and bytecodealliance/wasm-tools#2605.

To get existing tests all passing after updating wasm-tools many of the changes here were needed no matter what. To fully exercise the new functionality, however, I've also started adding a composition-based test suite which creates two components, composes them together, and runs that as a test. This creates the ability to test situations such as resource destructors and reentrant behavior purely within this repository. My hope is that as we discover bugs/issues elsewhere it'll be possible to add a small reduced test case to this repo as a composition. For now the composition tests are pretty simple and only cover the cases that failed before this PR and now fail after this PR.

This commit is a refactor of how wasip3 works specifically with
respect to management of the stack and intrinsics used. The end result
of this work is that a number of bugs, present in wasi-libc today, are
all fixed:

* Sync-reentrant calls, allowed in the component model, previously used
  the same stack and stomped over each other.
* Resource destructors, executed with fresh task state, did not
  correctly setup a stack to execute on.

The fix for the first issue here is particularly gnarly because it means
that stacks are required to be dynamically allocated in some situations.
This in turn means that those same stacks need to be deallocated, and
prior to this commit there was no great place to doing that. The fix for
the second issue is a bit broader than wasi-libc as well as it couldn't
be solved exclusively here.

To resolve these problems this commit updates the `wasm-tools` version
required, notably including recent PRs to adjust how linking works to
transform `__wasm_init_task` and `__wasm_init_async_task` into a
more-generic `__wasm_task_hook` function. This hook function is invoked
in more locations, such as resource destructors, and additionally
subsumes previous custom logic with `cabi_realloc`. Effectively
`__wasm_task_hook` is now the sole location concerned with configuring a
stack and TLS for the task-to-run. This handles all situations such as
sync/async tasks, `_initialize`, `cabi_realloc`, resource destructors,
and post-return. This was implemented in bytecodealliance/wasm-tools#2603
and bytecodealliance/wasm-tools#2605.

To get existing tests all passing after updating `wasm-tools` many of
the changes here were needed no matter what. To fully exercise the new
functionality, however, I've also started adding a composition-based
test suite which creates two components, composes them together, and
runs that as a test. This creates the ability to test situations such as
resource destructors and reentrant behavior purely within this
repository. My hope is that as we discover bugs/issues elsewhere it'll
be possible to add a small reduced test case to this repo as a
composition. For now the composition tests are pretty simple and only
cover the cases that failed before this PR and now fail after this PR.
@alexcrichton
alexcrichton requested a review from dicej August 14, 2026 20:09
@alexcrichton

Copy link
Copy Markdown
Collaborator Author

I'll note that like with #867 this will require an updated wasm-tools, not yet published, to pass CI. Locally, however, everything's passing.

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.

1 participant