Skip to content

Commit 5c75f14

Browse files
authored
Rollup merge of rust-lang#136200 - purplesyringa:wasm-eh-fixes, r=bjorn3
Generate correct terminate block under Wasm EH This fixes failing LLVM assertions during insnsel. Improves rust-lang#135665. r? bjorn3 ^ you reviewed the PR bringing Wasm EH in, I assume this is within your area of expertise?
2 parents 985973c + a983b58 commit 5c75f14

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

compiler/rustc_codegen_ssa/src/mir/block.rs

+23-6
Original file line numberDiff line numberDiff line change
@@ -1708,15 +1708,32 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
17081708
let mut cs_bx = Bx::build(self.cx, llbb);
17091709
let cs = cs_bx.catch_switch(None, None, &[cp_llbb]);
17101710

1711-
// The "null" here is actually a RTTI type descriptor for the
1712-
// C++ personality function, but `catch (...)` has no type so
1713-
// it's null. The 64 here is actually a bitfield which
1714-
// represents that this is a catch-all block.
17151711
bx = Bx::build(self.cx, cp_llbb);
17161712
let null =
17171713
bx.const_null(bx.type_ptr_ext(bx.cx().data_layout().instruction_address_space));
1718-
let sixty_four = bx.const_i32(64);
1719-
funclet = Some(bx.catch_pad(cs, &[null, sixty_four, null]));
1714+
1715+
// The `null` in first argument here is actually a RTTI type
1716+
// descriptor for the C++ personality function, but `catch (...)`
1717+
// has no type so it's null.
1718+
let args = if base::wants_msvc_seh(self.cx.sess()) {
1719+
// This bitmask is a single `HT_IsStdDotDot` flag, which
1720+
// represents that this is a C++-style `catch (...)` block that
1721+
// only captures programmatic exceptions, not all SEH
1722+
// exceptions. The second `null` points to a non-existent
1723+
// `alloca` instruction, which an LLVM pass would inline into
1724+
// the initial SEH frame allocation.
1725+
let adjectives = bx.const_i32(0x40);
1726+
&[null, adjectives, null] as &[_]
1727+
} else {
1728+
// Specifying more arguments than necessary usually doesn't
1729+
// hurt, but the `WasmEHPrepare` LLVM pass does not recognize
1730+
// anything other than a single `null` as a `catch (...)` block,
1731+
// leading to problems down the line during instruction
1732+
// selection.
1733+
&[null] as &[_]
1734+
};
1735+
1736+
funclet = Some(bx.catch_pad(cs, args));
17201737
} else {
17211738
llbb = Bx::append_block(self.cx, self.llfn, "terminate");
17221739
bx = Bx::build(self.cx, llbb);

tests/codegen/terminating-catchpad.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//@ revisions: emscripten wasi seh
2+
//@[emscripten] compile-flags: --target wasm32-unknown-emscripten -Z emscripten-wasm-eh
3+
//@[wasi] compile-flags: --target wasm32-wasip1 -C panic=unwind
4+
//@[seh] compile-flags: --target x86_64-pc-windows-msvc
5+
//@[emscripten] needs-llvm-components: webassembly
6+
//@[wasi] needs-llvm-components: webassembly
7+
//@[seh] needs-llvm-components: x86
8+
9+
// Ensure a catch-all generates:
10+
// - `catchpad ... [ptr null]` on Wasm (otherwise LLVM gets confused)
11+
// - `catchpad ... [ptr null, i32 64, ptr null]` on Windows (otherwise we catch SEH exceptions)
12+
13+
#![feature(no_core, lang_items, rustc_attrs)]
14+
#![crate_type = "lib"]
15+
#![no_std]
16+
#![no_core]
17+
18+
#[lang = "sized"]
19+
trait Sized {}
20+
21+
unsafe extern "C-unwind" {
22+
safe fn unwinds();
23+
}
24+
25+
#[lang = "panic_cannot_unwind"]
26+
fn panic_cannot_unwind() -> ! {
27+
loop {}
28+
}
29+
30+
#[no_mangle]
31+
#[rustc_nounwind]
32+
pub fn doesnt_unwind() {
33+
// emscripten: %catchpad = catchpad within %catchswitch [ptr null]
34+
// wasi: %catchpad = catchpad within %catchswitch [ptr null]
35+
// seh: %catchpad = catchpad within %catchswitch [ptr null, i32 64, ptr null]
36+
unwinds();
37+
}

0 commit comments

Comments
 (0)