Skip to content

Commit 420544a

Browse files
committed
Move wasm throw intrinsic back to unwind
rustc assumes that regular `extern "Rust"` functions unwind only if the `unwind` panic runtime is linked. `throw` was annotated as such, but unwound unconditionally. This could cause UB when a crate built with `-C panic=abort` called `throw` from `core` built with `-C panic=unwind`, since no terminator was added to handle the panic arising from calling an allegedly non-unwinding `extern "Rust"` function. rustc was taught to recognize this condition since #144225 and prevented such linkage, but this caused regressions in #148246, since this meant that Emscripten projects could not be built with `-C panic=abort` without recompiling std. The most straightforward solution would be to move `throw` into the `panic_unwind` crate, so that it's only compiled if the panic runtime is guaranteed to be `unwind`, but this is messy due to our architecture. Instead, move it into `unwind::wasm`, which is only compiled for bare-metal targets that default to `panic = "abort"`, rendering the issue moot.
1 parent 292be5c commit 420544a

File tree

5 files changed

+38
-45
lines changed

5 files changed

+38
-45
lines changed

library/stdarch/crates/core_arch/src/wasm32/mod.rs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -173,35 +173,3 @@ pub fn f64_nearest(a: f64) -> f64 {
173173
pub fn f64_sqrt(a: f64) -> f64 {
174174
crate::intrinsics::sqrtf64(a)
175175
}
176-
177-
unsafe extern "C-unwind" {
178-
#[link_name = "llvm.wasm.throw"]
179-
fn wasm_throw(tag: i32, ptr: *mut u8) -> !;
180-
}
181-
182-
/// Generates the [`throw`] instruction from the [exception-handling proposal] for WASM.
183-
///
184-
/// This function is unlikely to be stabilized until codegen backends have better support.
185-
///
186-
/// [`throw`]: https://webassembly.github.io/exception-handling/core/syntax/instructions.html#syntax-instr-control
187-
/// [exception-handling proposal]: https://github.com/WebAssembly/exception-handling
188-
// FIXME: wasmtime does not currently support exception-handling, so cannot execute
189-
// a wasm module with the throw instruction in it. once it does, we can
190-
// reenable this attribute.
191-
// #[cfg_attr(test, assert_instr(throw, TAG = 0, ptr = core::ptr::null_mut()))]
192-
#[inline]
193-
#[unstable(feature = "wasm_exception_handling_intrinsics", issue = "122465")]
194-
// FIXME: Since this instruction unwinds, `core` built with `-C panic=unwind`
195-
// cannot be linked with `-C panic=abort` programs. But that's not
196-
// entirely supported anyway, because runtimes without EH support won't
197-
// be able to handle `try` blocks in `-C panic=unwind` crates either.
198-
// We ship `-C panic=abort` `core`, so this doesn't affect users
199-
// directly. Resolving this will likely require patching out both `try`
200-
// and `throw` instructions, at which point we can look into whitelisting
201-
// this function in the compiler to allow linking.
202-
// See https://github.com/rust-lang/rust/issues/118168.
203-
#[allow(ffi_unwind_calls)]
204-
pub unsafe fn throw<const TAG: i32>(ptr: *mut u8) -> ! {
205-
static_assert!(TAG == 0); // LLVM only supports tag 0 == C++ right now.
206-
wasm_throw(TAG, ptr)
207-
}

library/unwind/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#![cfg_attr(not(target_env = "msvc"), feature(libc))]
88
#![cfg_attr(
99
all(target_family = "wasm", any(not(target_os = "emscripten"), emscripten_wasm_eh)),
10-
feature(simd_wasm64, wasm_exception_handling_intrinsics)
10+
feature(link_llvm_intrinsics, simd_wasm64)
1111
)]
1212
#![allow(internal_features)]
1313
#![deny(unsafe_op_in_unsafe_fn)]

library/unwind/src/wasm.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,40 @@ pub unsafe fn _Unwind_DeleteException(exception: *mut _Unwind_Exception) {
4040
}
4141

4242
pub unsafe fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code {
43-
// The wasm `throw` instruction takes a "tag", which differentiates certain
44-
// types of exceptions from others. LLVM currently just identifies these
45-
// via integers, with 0 corresponding to C++ exceptions and 1 to C setjmp()/longjmp().
46-
// Ideally, we'd be able to choose something unique for Rust, but for now,
47-
// we pretend to be C++ and implement the Itanium exception-handling ABI.
43+
// This implementation is only used for `wasm*-unknown-unknown` targets. Such targets are not
44+
// guaranteed to support exceptions, and they default to `-C panic=abort`. Because an unknown
45+
// instruction is a load-time error on wasm, instead of a runtime error like on traditional
46+
// architectures, we never want to codegen a `throw` instruction unless the user explicitly
47+
// enabled exceptions via `-Z build-std` with `-C panic=unwind`.
4848
cfg_select! {
49-
// panic=abort is default for wasm targets. Because an unknown instruction is a load-time
50-
// error on wasm, instead of a runtime error like on traditional architectures, we never
51-
// want to codegen a `throw` instruction, as that would break users using runtimes that
52-
// don't yet support exceptions. The only time this first branch would be selected is if
53-
// the user explicitly opts in to wasm exceptions, via -Zbuild-std with -Cpanic=unwind.
5449
panic = "unwind" => {
50+
// It's important that this intrinsic is defined here rather than in `core`. Since it
51+
// unwinds, invoking it from Rust code compiled with `-C panic=unwind` immediately
52+
// forces `panic_unwind` as the required panic runtime.
53+
//
54+
// We ship unwinding `core` on Emscripten, so making this intrinsic part of `core` would
55+
// prevent linking precompiled `core` into `-C panic=abort` binaries. Unlike `core`,
56+
// this particular module is never precompiled with `-C panic=unwind` because it's only
57+
// used for bare-metal targets, so an error can only arise if the user both manually
58+
// recompiles `std` with `-C panic=unwind` and manually compiles the binary crate with
59+
// `-C panic=abort`, which we don't care to support.
60+
//
61+
// See https://github.com/rust-lang/rust/issues/148246.
62+
unsafe extern "C-unwind" {
63+
/// LLVM lowers this intrinsic to the `throw` instruction.
64+
#[link_name = "llvm.wasm.throw"]
65+
fn wasm_throw(tag: i32, ptr: *mut u8) -> !;
66+
}
67+
68+
// The wasm `throw` instruction takes a "tag", which differentiates certain types of
69+
// exceptions from others. LLVM currently just identifies these via integers, with 0
70+
// corresponding to C++ exceptions and 1 to C setjmp()/longjmp(). Ideally, we'd be able
71+
// to choose something unique for Rust, but for now, we pretend to be C++ and implement
72+
// the Itanium exception-handling ABI.
5573
// corresponds with llvm::WebAssembly::Tag::CPP_EXCEPTION
5674
// in llvm-project/llvm/include/llvm/CodeGen/WasmEHFuncInfo.h
5775
const CPP_EXCEPTION_TAG: i32 = 0;
58-
core::arch::wasm::throw::<CPP_EXCEPTION_TAG>(exception.cast())
76+
wasm_throw(CPP_EXCEPTION_TAG, exception.cast())
5977
}
6078
_ => {
6179
let _ = exception;

tests/codegen-llvm/wasm_exceptions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//@ [WASMEXN] compile-flags: -C panic=unwind -Z emscripten-wasm-eh
44

55
#![crate_type = "lib"]
6-
#![feature(core_intrinsics, wasm_exception_handling_intrinsics, link_llvm_intrinsics)]
6+
#![feature(core_intrinsics, link_llvm_intrinsics)]
77

88
extern "C-unwind" {
99
fn may_panic();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//@ only-wasm32
2+
//@ compile-flags: -C panic=abort
3+
//@ build-pass
4+
5+
// Test that a `-C panic=abort` binary crate can link to a `-C panic=unwind` core.
6+
7+
fn main() {}

0 commit comments

Comments
 (0)