Skip to content

Commit c7127cb

Browse files
committed
Don't call rust_begin_unwind directly
`builtins-test-intrinsics` has long included a call to an unmangled `rust_begin_unwind` (the name `rustc` gives the `#[panic_handler]`), since [1], which I believe was intended to ensure panic machinery links correctly. However, since [2], `rust_begin_unwind` is mangled and unavailable for calling directly, which explains the recent CI failures. Instead of calling the function directly, we can just panic; do so here. Additionally, put this call behind `black_box(false)` rather than unconditional, which means we can run the binary and ensure there are no runtime issues. [1]: #360 [2]: rust-lang/rust#127173
1 parent 45007cc commit c7127cb

File tree

4 files changed

+43
-20
lines changed

4 files changed

+43
-20
lines changed

builtins-test-intrinsics/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55
publish = false
66

77
[dependencies]
8-
compiler_builtins = { path = "../", features = ["compiler-builtins"]}
8+
compiler_builtins = { path = "../", features = ["compiler-builtins", "mem"]}
99
panic-handler = { path = '../crates/panic-handler' }
1010

1111
[features]

builtins-test-intrinsics/build.rs

+5
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ fn main() {
88
let target = builtins_configure::Target::from_env();
99
builtins_configure::configure_f16_f128(&target);
1010
builtins_configure::configure_aliases(&target);
11+
12+
if target.os == "windows" {
13+
// Needed for using the `mainCRTStartup` entrypoint
14+
println!("cargo::rustc-link-arg=/subsystem:console");
15+
}
1116
}

builtins-test-intrinsics/src/main.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#![no_std]
1414
#![no_main]
1515

16+
// Ensure this repo's version of `compiler_builtins` gets used, rather than what gets injected from
17+
// the sysroot.
18+
extern crate compiler_builtins;
1619
extern crate panic_handler;
1720

1821
#[cfg(all(not(thumb), not(windows), not(target_arch = "wasm32")))]
@@ -626,12 +629,9 @@ fn run() {
626629

627630
something_with_a_dtor(&|| assert_eq!(bb(1), 1));
628631

629-
extern "C" {
630-
fn rust_begin_unwind(x: usize);
631-
}
632-
633-
unsafe {
634-
rust_begin_unwind(0);
632+
// Ensure panic machinery gets linked, but still allow this to run to completion.
633+
if bb(false) {
634+
panic!();
635635
}
636636
}
637637

@@ -648,15 +648,23 @@ fn something_with_a_dtor(f: &dyn Fn()) {
648648
}
649649

650650
#[no_mangle]
651-
#[cfg(not(thumb))]
652-
fn main(_argc: core::ffi::c_int, _argv: *const *const u8) -> core::ffi::c_int {
651+
#[cfg(not(any(thumb, windows)))]
652+
extern "C" fn main(_argc: core::ffi::c_int, _argv: *const *const u8) -> core::ffi::c_int {
653+
run();
654+
0
655+
}
656+
657+
#[no_mangle]
658+
#[cfg(windows)]
659+
#[allow(non_snake_case)]
660+
extern "C" fn mainCRTStartup() -> core::ffi::c_int {
653661
run();
654662
0
655663
}
656664

657665
#[no_mangle]
658666
#[cfg(thumb)]
659-
pub fn _start() -> ! {
667+
extern "C" fn _start() -> ! {
660668
run();
661669
loop {}
662670
}
@@ -678,6 +686,12 @@ pub fn __aeabi_unwind_cpp_pr1() {}
678686
#[no_mangle]
679687
pub fn _Unwind_Resume() {}
680688

689+
#[cfg(windows)]
690+
#[unsafe(no_mangle)]
691+
extern "win64" fn __CxxFrameHandler3() -> ! {
692+
unimplemented!()
693+
}
694+
681695
#[cfg(not(any(windows, target_os = "cygwin")))]
682696
#[lang = "eh_personality"]
683697
#[no_mangle]

ci/run.sh

+14-10
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,26 @@ done
120120

121121
rm -f "${rlib_paths[@]}"
122122

123-
build_intrinsics_test() {
124-
cargo build --target "$target" -v --package builtins-test-intrinsics "$@"
123+
run_intrinsics_test() {
124+
if [ "${NO_STD:-}" = "1" ]; then
125+
cmd=build
126+
else
127+
cmd=run
128+
fi
129+
130+
cargo "$cmd" --target "$target" -v --package builtins-test-intrinsics "$@"
125131
}
126132

127133
# Verify that we haven't dropped any intrinsics/symbols
128-
build_intrinsics_test
129-
build_intrinsics_test --release
130-
build_intrinsics_test --features c
131-
build_intrinsics_test --features c --release
134+
run_intrinsics_test
135+
run_intrinsics_test --release
136+
run_intrinsics_test --features c
137+
run_intrinsics_test --features c --release
132138

133139
# Verify that there are no undefined symbols to `panic` within our
134140
# implementations
135-
CARGO_PROFILE_DEV_LTO=true \
136-
cargo build --target "$target" --package builtins-test-intrinsics
137-
CARGO_PROFILE_RELEASE_LTO=true \
138-
cargo build --target "$target" --package builtins-test-intrinsics --release
141+
CARGO_PROFILE_DEV_LTO=true run_intrinsics_test
142+
CARGO_PROFILE_RELEASE_LTO=true run_intrinsics_test --release
139143

140144
# Ensure no references to any symbols from core
141145
update_rlib_paths

0 commit comments

Comments
 (0)