Skip to content

Clarify behavior of context.{get,set} during CABI realloc#680

Open
lukewagner wants to merge 1 commit into
mainfrom
fix-realloc
Open

Clarify behavior of context.{get,set} during CABI realloc#680
lukewagner wants to merge 1 commit into
mainfrom
fix-realloc

Conversation

@lukewagner

Copy link
Copy Markdown
Member

This PR intends to address #678 which observes that realloc is underspecified w.r.t context.{get,set}. The root (spec) problem is that the core realloc function is being called "directly" during lowering without going through the usual coop-threads machinery that defines the current thread. To fix this, this PR puts calls to realloc behind new, slightly-more-convenient LiftLowerContext.{allocate,reallocate} methods that call realloc as-if it was an export (via canon_lift) which ends up creating a fresh "thread" for the realloc cal. However, as described in the optimization paragraph in this PR, b/c it's a fully-sync call and b/c of the may_leave guards, an engine can unobservably use whatever thread it wants as long as it sets/restores the 2 thread-local-storage cells and, if necessary, the current instance.

There is one extra choice in this PR that could go either way and that is to add a trap_if(not may_leave) to thread.index, so that realloc's thread index is unobservable, which I thought might simplify optimization. It's also just a weird omission in the original coop threads PR to allow thread.index but not any of the threading builtins that actually do something with an index that thread.index was added for. Happy to discuss this though.

@alexcrichton alexcrichton left a comment

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.

Ok I've implemented this in Wasmtime and wasi-libc (linked above) and this looks good to me. The wrapper in wasi-libc wasn't too too hard to get right and none of the tests are currently failing (e.g. free never tries to get the current thread id). I'm not 100% confident this'll continue to slide through uneventfully once more interesting ABI situations are exercised in wit-bindgen for example, but I'm also pretty confident that should this situation arise it'd be best handled in wasi-libc.

Basically, lgtm 👍

github-merge-queue Bot pushed a commit to bytecodealliance/wasmtime that referenced this pull request Jul 24, 2026
* Configure async task context on `realloc` calls

This commit is an implementation of WebAssembly/component-model#680
which is a semantic change for invocations of the `realloc` canonical
ABI option. Previously when this function was invoked the configured
context-storage was whatever happened to run last in the store and in
general wasn't well-defined. Additionally the context of the thread
being run in was whatever happened to run last. The goal of the upstream
spec change, and this commit, is to fully define what happens in this
situation. Specifically:

* Invocations of `realloc` always start with `context` slots set to 0.
* The `thread.index` intrinsics, part of the component-model-threading
  proposal, is now no longer exempt from may-leave checks.

These changes combined mean that it's not actually possible for
`realloc` to witness anything about its thread identity. This means that
we don't actually have to allocate anything, all that's necessary is to
save/restore context around invocation of `cabi_realloc`.

While this is a breaking change it's not expected to be observable in
the ecosystem. This only affects `context` slots which are primarily
only used for the WASIp3 ABI as the stack pointer and TLS base. There is
no shipping WASIp3 target anywhere right now, so this breakage should
not be observable.

* Fix conditional builds

* Add a note about restoration
@alexcrichton

Copy link
Copy Markdown
Collaborator

One minor test-related note is that in bytecodealliance/wasmtime#13951 I found that some of post-return.wast will need updating to expect traps for thread.index now

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