Skip to content

Fall back to cabi_realloc in the main module - #2606

Open
alexcrichton wants to merge 2 commits into
bytecodealliance:mainfrom
alexcrichton:maybe-more-realloc
Open

Fall back to cabi_realloc in the main module #2606
alexcrichton wants to merge 2 commits into
bytecodealliance:mainfrom
alexcrichton:maybe-more-realloc

Conversation

@alexcrichton

@alexcrichton alexcrichton commented Aug 14, 2026

Copy link
Copy Markdown
Member

This commit updates the behavior of wasm-tools component link to
search for the cabi_realloc function in the main module, if present.
This behavior fixes an issue when linking shared libraries where each
shared library was required to export its own cabi_realloc which
otherwise doesn't happen by default when using wasi-sdk for example. By
falling back to the main module that preserves the originally intended
semantics, one definition of cabi_realloc, and these situations should
work by default now.

Note: this is built on #2605

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.
This commit updates the behavior of `wasm-tools component link` to
search for the `cabi_realloc` function in the main module, if present.
This behavior fixes an issue when linking shared libraries where each
shared library was required to export its own `cabi_realloc` which
otherwise doesn't happen by default when using wasi-sdk for example. By
falling back to the main module that preserves the originally intended
semantics, one definition of `cabi_realloc`, and these situations should
work by default now.
@alexcrichton
alexcrichton requested a review from a team as a code owner August 14, 2026 20:07
@alexcrichton
alexcrichton requested review from fitzgen and removed request for a team August 14, 2026 20:07
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