Clarify behavior of context.{get,set} during CABI realloc#680
Open
lukewagner wants to merge 1 commit into
Open
Clarify behavior of context.{get,set} during CABI realloc#680lukewagner wants to merge 1 commit into
lukewagner wants to merge 1 commit into
Conversation
This was referenced Jul 23, 2026
alexcrichton
left a comment
Collaborator
There was a problem hiding this comment.
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 👍
alexcrichton
approved these changes
Jul 23, 2026
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
Collaborator
|
One minor test-related note is that in bytecodealliance/wasmtime#13951 I found that some of |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR intends to address #678 which observes that
reallocis underspecified w.r.tcontext.{get,set}. The root (spec) problem is that the corereallocfunction 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 toreallocbehind new, slightly-more-convenientLiftLowerContext.{allocate,reallocate}methods that callreallocas-if it was an export (viacanon_lift) which ends up creating a fresh "thread" for therealloccal. However, as described in the optimization paragraph in this PR, b/c it's a fully-sync call and b/c of themay_leaveguards, 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)tothread.index, so thatrealloc'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 allowthread.indexbut not any of the threading builtins that actually do something with an index thatthread.indexwas added for. Happy to discuss this though.