Skip to content

Commit 02d42b3

Browse files
committed
Add documentation about unwinding to wasm targets
This commit adds some documentation about the state of `-Cpanic=unwind` for the following wasm targets: * `wasm32-unknown-unknown` * `wasm32-wasip1` * `wasm32-wasip2` * `wasm32v1-none` Notably it's possible to use `-Cpanic=unwind` with `-Zbuild-std` and it's also mentioned that there are no concrete proposals at this time to adding a new set of targets which support unwinding. My hunch is that in a few years' time it would make sense to enable it by default on these targets (except for `wasm32v1-none`) but that's a problem for future folks to debate. For now this is an attempt to document the status quo.
1 parent f437c86 commit 02d42b3

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

src/doc/rustc/src/platform-support/wasm32-unknown-unknown.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ $ cargo +nightly build -Zbuild-std=panic_abort,std --target wasm32-unknown-unkno
157157
```
158158

159159
Here the `mvp` "cpu" is a placeholder in LLVM for disabling all supported
160-
features by default. Cargo's `-Zbuild-std` feature, a Nightly Rust feature, is
160+
features by default. Cargo's [`-Zbuild-std`] feature, a Nightly Rust feature, is
161161
then used to recompile the standard library in addition to your own code. This
162162
will produce a binary that uses only the original WebAssembly features by
163163
default and no proposals since its inception.
@@ -207,3 +207,63 @@ conditionally compile code instead. This is notably different to the way native
207207
platforms such as x86\_64 work, and this is due to the fact that WebAssembly
208208
binaries must only contain code the engine understands. Native binaries work so
209209
long as the CPU doesn't execute unknown code dynamically at runtime.
210+
211+
## Unwinding
212+
213+
By default the `wasm32-unknown-unknown` target is compiled with `-Cpanic=abort`.
214+
Historically this was due to the fact that there was no way to catch panics in
215+
wasm, but since mid-2025 the WebAssembly [`exception-handling`
216+
proposal](https://github.com/WebAssembly/exception-handling) reached
217+
stabilization. LLVM has support for this proposal as well and when this is all
218+
combined together it's possible to enable `-Cpanic=unwind` on wasm targets.
219+
220+
Compiling wasm targets with `-Cpanic=unwind` is not as easy as just passing
221+
`-Cpanic=unwind`, however:
222+
223+
```
224+
$ rustc foo.rs -Cpanic=unwind --target wasm32-unknown-unknown
225+
error: the crate `panic_unwind` does not have the panic strategy `unwind`
226+
```
227+
228+
Notably the precompiled standard library that is shipped through Rustup is
229+
compiled with `-Cpanic=abort`, not `-Cpanic=unwind`. While this is the case
230+
you're going to be required to use Cargo's [`-Zbuild-std`] feature to build with
231+
unwinding support:
232+
233+
```
234+
$ RUSTFLAGS='-Cpanic=unwind' cargo +nightly build --target wasm32-unknown-unknown -Zbuild-std
235+
```
236+
237+
Note, however, that as of 2025-10-03 LLVM is still using the "legacy exception
238+
instructions" by default, not the officially standard version of the
239+
exception-handling proposal:
240+
241+
```
242+
$ wasm-tools validate target/wasm32-unknown-unknown/debug/foo.wasm
243+
error: <sysroot>/library/std/src/sys/backtrace.rs:161:5
244+
function `std::sys::backtrace::__rust_begin_short_backtrace` failed to validate
245+
246+
Caused by:
247+
0: func 2 failed to validate
248+
1: legacy_exceptions feature required for try instruction (at offset 0x880)
249+
```
250+
251+
Fixing this requires passing `-Cllvm-args=-wasm-use-legacy-eh=false` to the Rust
252+
compiler as well:
253+
254+
```
255+
$ RUSTFLAGS='-Cpanic=unwind -Cllvm-args=-wasm-use-legacy-eh=false' cargo +nightly build --target wasm32-unknown-unknown -Zbuild-std
256+
$ wasm-tools validate target/wasm32-unknown-unknown/debug/foo.wasm
257+
```
258+
259+
At this time there are no concrete plans for adding new targets to the Rust
260+
compiler which have `-Cpanic=unwind` enabled-by-default. The most likely route
261+
to having this enabled is that in a few years when the `exception-handling`
262+
target feature is enabled by default in LLVM (due to browsers/runtime support
263+
propagating widely enough) the targets will switch to using `-Cpanic=unwind` by
264+
default. This is not for certain, however, and will likely be accompanied with
265+
either an MCP or an RFC about changing all wasm targets in the same manner. In
266+
the meantime using `-Cpanic=unwind` will require using [`-Zbuild-std`] and
267+
passing the appropriate flags to rustc.
268+
269+
[`-Zbuild-std`]: ../../cargo/reference/unstable.html#build-std

src/doc/rustc/src/platform-support/wasm32-wasip1.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,9 @@ to Rust 1.80 the `target_env` condition was not set.
133133
The default set of WebAssembly features enabled for compilation is currently the
134134
same as [`wasm32-unknown-unknown`](./wasm32-unknown-unknown.md). See the
135135
documentation there for more information.
136+
137+
## Unwinding
138+
139+
This target is compiled with `-Cpanic=abort` by default. For information on
140+
using `-Cpanic=unwind` see the [documentation about unwinding for
141+
`wasm32-unknown-unknown`](./wasm32-unknown-unknown.md#unwinding).

src/doc/rustc/src/platform-support/wasm32-wasip2.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,9 @@ It's recommended to conditionally compile code for this target with:
6767
The default set of WebAssembly features enabled for compilation is currently the
6868
same as [`wasm32-unknown-unknown`](./wasm32-unknown-unknown.md). See the
6969
documentation there for more information.
70+
71+
## Unwinding
72+
73+
This target is compiled with `-Cpanic=abort` by default. For information on
74+
using `-Cpanic=unwind` see the [documentation about unwinding for
75+
`wasm32-unknown-unknown`](./wasm32-unknown-unknown.md#unwinding).

src/doc/rustc/src/platform-support/wasm32v1-none.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,14 @@ $ cargo +nightly build -Zbuild-std=panic_abort,std --target wasm32-unknown-unkno
107107
Which not only rebuilds `std`, `core` and `alloc` (which is somewhat costly and annoying) but more importantly requires the use of nightly Rust toolchains (for the `-Zbuild-std` flag). This is very undesirable for the target audience, which consists of people targeting WebAssembly implementations that prioritize stability, simplicity and/or security over feature support.
108108

109109
This `wasm32v1-none` target exists as an alternative option that works on stable Rust toolchains, without rebuilding the stdlib.
110+
111+
## Unwinding
112+
113+
This target is compiled with `-Cpanic=abort` by default. Using `-Cpanic=unwind`
114+
would require using the WebAssembly exception-handling proposal stabilized
115+
mid-2025, and if that's desired then you most likely don't want to use this
116+
target and instead want to use `wasm32-unknown-unknown` instead. It's unlikely
117+
that this target will ever support unwinding with the precompiled artifacts
118+
shipped through rustup. For documentation about using `-Zbuild-std` to enable
119+
using `-Cpanic=unwind` see the [documentation of
120+
`wasm32-unknown-unknown`](./wasm32-unknown-unknown.md#unwinding).

0 commit comments

Comments
 (0)