Skip to content

Use __wasm_task_hook for realloc options - #2605

Merged
alexcrichton merged 2 commits into
bytecodealliance:mainfrom
alexcrichton:hook-cabi-realloc
Aug 17, 2026
Merged

alexcrichton merged 2 commits into
bytecodealliance:mainfrom
alexcrichton:hook-cabi-realloc

Conversation

@alexcrichton

Copy link
Copy Markdown
Member

This commit updates the __wasm_task_hook intrinsic, added in #2603, to additionally get called for the realloc function, typically exported as cabi_realloc today. The goal of this commit is to uniformly use this hook for wasip3 task initialization/configuration as opposed to the current state of affairs in wasi-libc where cabi_realloc is special.

The implementation here is more involved than #2603 realloc options are required to create the fixup module and thus come from the original instance raw. When using task hooks, however, the desired state is that the lowerings created use the hooked version of realloc. As with all problems in computer science, this is solved with another layer of indirection.

Specifically the shim module may not contain shims for realloc options if used for lowerings. These are then hooked up through the fixup module so it's instantiated with both lowered functions and the "real" implementation so everything can get wired up at the same time.

While implementing this I went ahead an implemented a minor optimization where for modules that import linear memory they no longer need an shim-per-import-using-realloc and instead just have a single shim for realloc. This means that when importing memory generated components generally have even fewer shims than before.

This commit then attempts to add a variety of tests for various shapes of this new hook and various other options. This was developed in tandem with changes to wasi-libc and additionally verified against those.

This commit updates the `__wasm_task_hook` intrinsic, added in bytecodealliance#2603, to
additionally get called for the `realloc` function, typically exported
as `cabi_realloc` today. The goal of this commit is to uniformly use
this hook for wasip3 task initialization/configuration as opposed to the
current state of affairs in wasi-libc where `cabi_realloc` is special.

The implementation here is more involved than bytecodealliance#2603 `realloc` options
are required to create the fixup module and thus come from the original
instance raw. When using task hooks, however, the desired state is that
the lowerings created use the hooked version of `realloc`. As with all
problems in computer science, this is solved with another layer of
indirection.

Specifically the shim module may not contain shims for `realloc` options
if used for lowerings. These are then hooked up through the fixup module
so it's instantiated with both lowered functions and the "real"
implementation so everything can get wired up at the same time.

While implementing this I went ahead an implemented a minor optimization
where for modules that import linear memory they no longer need an
shim-per-import-using-realloc and instead just have a single shim for
realloc. This means that when importing memory generated components
generally have even fewer shims than before.

This commit then attempts to add a variety of tests for various shapes
of this new hook and various other options. This was developed in tandem
with changes to wasi-libc and additionally verified against those.
Comment thread crates/wit-component/src/encoding/world.rs Outdated
Co-authored-by: Joel Dice <joel.dice@akamai.com>
@alexcrichton
alexcrichton enabled auto-merge August 17, 2026 17:43
@alexcrichton
alexcrichton added this pull request to the merge queue Aug 17, 2026
Merged via the queue into bytecodealliance:main with commit 908c4ac Aug 17, 2026
37 checks passed
@alexcrichton
alexcrichton deleted the hook-cabi-realloc branch August 17, 2026 18:04
alexcrichton added a commit to WebAssembly/wasi-libc that referenced this pull request Aug 19, 2026
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.
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