Clarify behavior of context.{get,set} during CABI realloc#680
Conversation
alexcrichton
left a comment
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 👍
* 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
|
One minor test-related note is that in bytecodealliance/wasmtime#13951 I found that some of |
|
Thanks @alexcrichton! Updated |
* Update some required features for tests * Flag `memory64.wast` as now-passing * Temporarily allow `post-return.wast` to fail while bytecodealliance#13949 and WebAssembly/component-model#680 are being aligned. * Fix a debug assert tripping erroneously in an async `subtask.cancel` intrinsic.
This commit is an update to the `cabi_realloc` export configured by wasi-libc, specifically for wasip3 targets. The change here is to change the entrypoint from a C function to a small assembly wrapper which configures the stack pointer and TLS base before calling into C. This is to account for the spec change in WebAssembly/component-model#680 where `realloc` hooks are now going to be invoked with context slots set to 0. This means that `cabi_realloc` runs with no stack, and this would quickly abort in wasi-libc otherwise. The change here is to, when the ABI is using `context` slots for the stack pointer, have an assembly wrapper that configures a known-good stack and TLS block for `cabi_realloc` to use. Once configured the normal C-based `cabi_realloc` routine runs. This is tested against bytecodealliance/wasmtime#13949 to ensure it works on the p3 target with/without coop threading and before/after that Wasmtime change.
* 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
* Update some required features for tests * Flag `memory64.wast` as now-passing * Temporarily allow `post-return.wast` to fail while bytecodealliance#13949 and WebAssembly/component-model#680 are being aligned. * Fix a debug assert tripping erroneously in an async `subtask.cancel` intrinsic.
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.