cfallin opened PR #15187 from cfallin:raw-fibers to bytecodealliance:main:
In order to support record/replay with reversible debugging, we need the ability to snapshot and resume to earlier execution states.
The general strategy I've taken in my prototype branch is to snapshot the whole execution state of a Wasm store by literally memcpy'ing its activation stacks, and memcpy'ing those snapshots back.
In order to do this without creating UB in Rust, however, we need to run the Wasm on a stack with no Rust frames. Later commits will introduce new kinds of trampolines that actually do this, by using Cranelift-compiled code both at the "entry end" (the initial trampoline that calls whatever Wasm entry point) and the "exit end" (trampolines for hostcalls, by yielding back out with a special code indicating we're doing a call). However, a prerequisite for this is a new kind of fiber that does not take a Rust closure as its entry point.
This PR introduces exactly that: a "raw fiber" is one that takes a raw function address as an entry point (an
unsafe "C" fn), with a fixed defined signature, and invokes that. The entry-point function must never return; instead, it should terminate its execution by yielding one last time with a user-defined protocol that then causes the yieldee to drop the raw fiber, thus freeing the stack.This PR then provides a snapshot/resume API. The idea is that the snapshot captures the state of the stack and the saved stack pointer. It is the responsibility of the user to ensure that the semantics of resuming multiple times to a raw fiber snapshot are sensible (e.g. by controlling the code entirely, i.e. as Cranelift-compiled JIT code, and by snapshotting/restoring all other state that that code uses).
This PR supports Windows as well; this is a little trickier than the Unix case because we use Windows' kernel-level fibers in this fiber library. We need to interpose a special frame with a known shape in the Windows case so that we can save and restore the stack pointer for arbitrary snapshots.
<!--
Please make sure you include the following information:
If this work has been discussed elsewhere, please include a link to that
conversation. If it was discussed in an issue, just mention "issue #...".Explain why this change is needed. If the details are in an issue already,
this can be brief.Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.htmlPlease review the Bytecode Alliance's AI tool usage policy at
https://github.com/bytecodealliance/governance/blob/main/AI_TOOL_POLICY.mdPlease ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->
cfallin requested dicej for a review on PR #15187.
cfallin removed the review request for wasmtime-core-reviewers from PR #15187.
cfallin requested wasmtime-core-reviewers for a review on PR #15187.
cfallin removed the review request for dicej from PR #15187.
cfallin requested fitzgen for a review on PR #15187.
:memo: fitzgen submitted PR review:
LGTM modulo a couple questions/comments
:speech_balloon: fitzgen created PR review comment:
Should this assert that the state is not
Terminal?
:speech_balloon: fitzgen created PR review comment:
u64identifiers so we don't have to worry about overflow on 32-bit? Also aRawFiberIdnewtype would be nice.
:speech_balloon: fitzgen created PR review comment:
Can we have
RawFiberSnapshothold aTryVecinstead of aVecand stop doing thismap_errdance here?
:thumbs_up: fitzgen submitted PR review.
cfallin updated PR #15187.
:memo: cfallin submitted PR review.
:speech_balloon: cfallin created PR review comment:
Done!
:memo: cfallin submitted PR review.
:speech_balloon: cfallin created PR review comment:
Done!
:memo: cfallin submitted PR review.
:speech_balloon: cfallin created PR review comment:
Done!
cfallin has enabled auto merge for PR #15187.
cfallin added PR #15187 wasmtime-fiber: add raw fibers with snapshots. to the merge queue
github-merge-queue[bot] removed PR #15187 wasmtime-fiber: add raw fibers with snapshots. from the merge queue
cfallin updated PR #15187.
cfallin has enabled auto merge for PR #15187.
cfallin added PR #15187 wasmtime-fiber: add raw fibers with snapshots. to the merge queue
:check: cfallin merged PR #15187.
cfallin removed PR #15187 wasmtime-fiber: add raw fibers with snapshots. from the merge queue
:memo: alexcrichton submitted PR review.
:speech_balloon: alexcrichton created PR review comment:
How come this is a function as opposed to a
use thing as switch_routine?
:speech_balloon: alexcrichton created PR review comment:
Instead if
cfg!here could this somehow be in the platform-specific modules themselves?
:speech_balloon: alexcrichton created PR review comment:
FWIW this would probably be best modeled with a
-> !in the function type to reflect the noreturn-nature
:speech_balloon: alexcrichton created PR review comment:
I'm personally a bit wary of nested trees of
cfg_select!or disparate ones. Would it be possible to move all thecfg_select!s added here into, in theory, one top-level version at the crate level?
:speech_balloon: alexcrichton created PR review comment:
How is this supposed to work when one of the documented conditions is "no rust frames on the stack" but the tests here are only dealing with Rust frames on the stack?
I was naively expecting to see a #[cfg]'d version of tests that only ran on x86_64 or something like that. But otherwise isn't this pretty brittle and likely to break in the future?
:memo: cfallin submitted PR review.
:speech_balloon: cfallin created PR review comment:
Well, I guess the condition is a bit more nuanced in reality, and we're doing whitebox testing here, so we can "get away with it". The real conditions are that (i) when doing snapshots/restores, don't resume more than once from a snapshot because that's UB in Rust, and (ii) don't expect destructors to be run in the function that never actually returns.
I'm happy to delete the test entirely if you'd prefer though? It just seemed better to have some coverage, here, before we get all the way to end-to-end tests with reversible debugging...
:memo: alexcrichton submitted PR review.
:speech_balloon: alexcrichton created PR review comment:
I do think this is a good test to have, and I'd want to keep it tested for sure.
Would it be possible to have the "on the stack" part here be a small inline assembly function, and then that would delegate to some Rust helper? For example the inline asm would do something like call a phase1, phase2, phase3, ... host function and the inline asm itself would be pretty simple while the phase/host functions do the semi-complicated things. That way we can reflect the guarantee that there are zero Rust frames on the stack without too much onerous maintenace here. I'd expect the test to be allow-listed on what architectures it runs on, and I think it'd be reasonable to basically just support x64/aarch64 and then full-breadth testing would come from the integration RR tests later across all platforms.
:memo: cfallin submitted PR review.
:speech_balloon: cfallin created PR review comment:
I guess so, but that still seems like quite a lot of complexity vs. altering our safety requirement: what if we simply declare what I noted above (no multi-shot resume from one snapshot, destructors will not be run; the latter I guess also implied by the
-> !signature you suggest below) to be the requirement for safety? That also matches reality: there is no physical difference with the stack that a raw fiber runs on that prevents them from actually running Rust.Said another way: we describe the safety conditions so that we can snapshot raw fibers' stacks, but here we are not yet snapshotting and resuming their stacks; so these are just normal fibers with a weird entry signature. We are choosing to impose the conditions, we are not forced into them with what we have so far. So we might as well move the safety conditions to the "multi-shot resume" (aka snapshot/rewind) bit (which comes later).
And I guess overall I'd rather not write inline assembly unless I have to as it seems to me to have brittleness risk (since it is not typechecked against what we call) and obscures what's being tested.
Please do let me know if I'm missing something about the contours of the UB risk here though!
:memo: cfallin submitted PR review.
:speech_balloon: cfallin created PR review comment:
Ah, wait, I missed that I actually segmented the patch stack so this does include rewinds; so everything I said above about that safety-condition segmentation is null and void. Sorry about that!
This is then indeed textbook UB; I'll rework it to have an inline-assembly implementation.
:speech_balloon: cfallin edited PR review comment.
cfallin commented on PR #15187:
Thanks @alexcrichton for the post-merge review notes -- addressed in #15229.
Last updated: Nov 19 2026 at 20:05 UTC