Stream: git-wasmtime

Topic: wasmtime / PR #15358 Encapsulate Wasm-is-running state, m...


view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 21:40):

cfallin requested alexcrichton for a review on PR #15358.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 21:41):

cfallin requested wasmtime-compiler-reviewers for a review on PR #15358.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 21:41):

cfallin requested wasmtime-core-reviewers for a review on PR #15358.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 21:41):

cfallin opened PR #15358 from cfallin:encapsulate-running-state to bytecodealliance:main:

Fixes #15281.

This PR makes use of the EpochState<T> introduced in #15191 for encapsulation of GC-heap state mutations (for unrooted refs). The idea is that we want to have the same "cannot mutate without bumping the counter" enforced guarantee for all state that could be mutated by Wasm functions, such as their stack frames (i.e., FrameHandles, the subject of the bug above).

The idea is to introduce an enum Running { Yes, No } and hold an EpochState<Running> in the Store. We then change this (to "yes" or "no") on entry/exit to Wasm from the host, and on exit/entry to the host from Wasm (respectively). Ideally we'd actually put "the real Wasm state" inside T, but that's not possible (e.g. the actual machine stack) so instead we have this stand-in.

We assert that when we set it to "yes" or "no", it was previously the opposite state. This way, we should catch e.g. an entry or return path that we forgot about; if we make it to host code and the state is still "running", that's a bug and we'll hit the assert.

The PR puts enter_wasm and exit_wasm invocations at the narrow waist where all Wasm entry/exit should go through in traphandlers.rs. It fixes the hole that led to #15281 as well (namely, exception raise is also a re-entry).

We can then refactor FrameHandle on top of this: rather than keep its own execution-version snapshot and check it, we can use an EpochStateHandle and check its validity.

In theory we can use this framework for other kinds of "views of frozen state" in the future too, potentially.

<!--
Please make sure you include the following information:

Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.html

Please review the Bytecode Alliance's AI tool usage policy at
https://github.com/bytecodealliance/governance/blob/main/AI_TOOL_POLICY.md

Please ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 21:59):

alexcrichton commented on PR #15358:

lgtm, but in the context of https://github.com/bytecodealliance/wasmtime/issues/15342 I'm curious to review sightglass numbers before landing this as well. I'm attempting to run locally and it says 28m55s remaining, so I'll see what happens...

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 22:13):

cfallin updated PR #15358.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 22:27):

cfallin commented on PR #15358:

I see these results:

  ┌───────────────────┬───────────────────────────────┐
  │     Benchmark     │ Execution: baseline faster by │
  ├───────────────────┼───────────────────────────────┤
  │ hoot-earley-boyer │ 1.18–1.22x                    │
  ├───────────────────┼───────────────────────────────┤
  │ dart-deltablue    │ 1.10–1.13x                    │
  ├───────────────────┼───────────────────────────────┤
  │ rust-wasmprinter  │ 1.07–1.11x                    │
  ├───────────────────┼───────────────────────────────┤
  │ splay             │ 1.01–1.09x (noise ~16%)       │
  ├───────────────────┼───────────────────────────────┤
  │ tinygo-regex      │ 1.02–1.04x                    │
  ├───────────────────┼───────────────────────────────┤
  │ shootout-memmove  │ 1.02–1.04x                    │
  ├───────────────────┼───────────────────────────────┤
  │ dotnet-raytracer  │ 1.01–1.03x                    │
  ├───────────────────┼───────────────────────────────┤
  │ kotlin-richards   │ 1.01–1.04x                    │
  ├───────────────────┼───────────────────────────────┤
  │ hashset           │ 1.00–1.04x                    │
  └───────────────────┴───────────────────────────────┘

The two biggest slowdowns are both GC benchmarks, so more hostcalls hurt more.

Honestly I'm surprised the overhead is this high -- I'll profile a bit and also assuming it's the "epoch bump", I'll see if I can add some more gating (e.g. on guest-debug being enabled)...

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 23:13):

cfallin commented on PR #15358:

So it looks like:

I'll play around with things a bit more; a fallback idea is to do Running-tracking only in debug builds, and have cheaper bumps (as per above) in release builds, but I don't like that kind of divergence in general...

view this post on Zulip Wasmtime GitHub notifications bot (Nov 19 2026 at 00:51):

github-actions[bot] added the label wasmtime:api on PR #15358.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 19 2026 at 03:38):

cfallin updated PR #15358.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 19 2026 at 03:58):

cfallin updated PR #15358.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 19 2026 at 04:02):

cfallin edited PR #15358:

Fixes #15281.

This PR makes use of the EpochState<T> introduced in #15191 for encapsulation of GC-heap state mutations (for unrooted refs). The idea is that we want to have the same "cannot mutate without bumping the counter" enforced guarantee for all state that could be mutated by Wasm functions, such as their stack frames (i.e., FrameHandles, the subject of the bug above).

The idea is to introduce a struct WasmState; unit struct that we "mutate" whenever control transitions from Wasm to the host. Ideally we'd actually put "the real Wasm state" inside T, but that's not possible (e.g. the actual machine stack) so instead we have this stand-in -- just a unit value, and a mutation right after the real Wasm state is updated. The PR inserts a hook at all points where Wasm-to-host transitions occur in traphandlers.rs, and fixes the hole that led to #15281 as well (namely, exception raise is also a re-entry).

We can then refactor FrameHandle on top of this: rather than keep its own execution-version snapshot and check it, we can use an EpochStateHandle and check its validity.

In theory we can use this framework for other kinds of "views of frozen state" in the future too, potentially.

(A previous version of this PR had an enum Running { Yes, No } and hold an EpochState<Running> in the Store. We then changed this (to "yes" or "no") on entry/exit to Wasm from the host, and on exit/entry to the host from Wasm (respectively), and asserted that when we set it to "yes" or "no", it was previously the opposite state. This way, we would catch e.g. an entry or return path that we forgot about; if we make it to host code and the state is still "running", that's a bug and we'll hit the assert. Unfortunately, this was too expensive: the action on return from a hostcall back to Wasm both inhibited tail-calls and also kept more state alive, increasing register pressure and causing spills in compiled libcall runtime code in practice.)

<!--
Please make sure you include the following information:

Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.html

Please review the Bytecode Alliance's AI tool usage policy at
https://github.com/bytecodealliance/governance/blob/main/AI_TOOL_POLICY.md

Please ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->

view this post on Zulip Wasmtime GitHub notifications bot (Nov 19 2026 at 04:04):

cfallin commented on PR #15358:

OK, updated (and updated description): we now avoid bumps on host-to-Wasm return from libcalls, which were causing the above slowdowns. No slowdowns observed with Sightglass now. We lose the proper-pairing asserts, which is unfortunate, but the one single hook is now centralized and conspicuous enough that hopefully this will let us keep it up-to-date and correct more easily.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 19 2026 at 04:14):

cfallin updated PR #15358.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 19 2026 at 15:29):

:thumbs_up: alexcrichton submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 19 2026 at 15:29):

alexcrichton added PR #15358 Encapsulate Wasm-is-running state, making epoch counters safer. to the merge queue

view this post on Zulip Wasmtime GitHub notifications bot (Nov 19 2026 at 15:51):

:check: alexcrichton merged PR #15358.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 19 2026 at 15:51):

alexcrichton removed PR #15358 Encapsulate Wasm-is-running state, making epoch counters safer. from the merge queue


Last updated: Nov 19 2026 at 20:05 UTC