From af7c540b2f403cc65a8ac0f376416a5883b7eac0 Mon Sep 17 00:00:00 2001 From: Berrysoft Date: Sun, 4 Apr 2021 22:50:01 +0800 Subject: [PATCH 1/6] Add empty __CxxFrameHandler3 impl in panic_abort --- library/panic_abort/src/lib.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/library/panic_abort/src/lib.rs b/library/panic_abort/src/lib.rs index eb2277d8baacd..0347df2d63717 100644 --- a/library/panic_abort/src/lib.rs +++ b/library/panic_abort/src/lib.rs @@ -116,6 +116,19 @@ pub mod personalities { )))] pub extern "C" fn rust_eh_personality() {} + // On *-pc-windows-msvc we need such a symbol to make linker happy. + #[allow(non_snake_case)] + #[no_mangle] + #[cfg(all(target_os = "windows", target_env = "msvc"))] + pub extern "C" fn __CxxFrameHandler3( + _record: usize, + _frame: usize, + _context: usize, + _dispatcher: usize, + ) -> u32 { + 1 + } + // On x86_64-pc-windows-gnu we use our own personality function that needs // to return `ExceptionContinueSearch` as we're passing on all our frames. #[rustc_std_internal_symbol] From 9cdaea332b407e96476410445329349f5fed1feb Mon Sep 17 00:00:00 2001 From: Berrysoft Date: Sun, 4 Apr 2021 22:50:39 +0800 Subject: [PATCH 2/6] Add tests for abort link to unwind. --- ...nk-to-unwind-msvc-no-std-link-to-libcmt.rs | 32 +++++++++++ ...nk-to-unwind-msvc-no-std-link-to-msvcrt.rs | 32 +++++++++++ .../abort-link-to-unwind-msvc-no-std.rs | 54 +++++++++++++++++++ .../exit-success-if-unwind-msvc-no-std.rs | 24 +++++++++ 4 files changed, 142 insertions(+) create mode 100644 src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std-link-to-libcmt.rs create mode 100644 src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std-link-to-msvcrt.rs create mode 100644 src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std.rs create mode 100644 src/test/ui/panic-runtime/auxiliary/exit-success-if-unwind-msvc-no-std.rs diff --git a/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std-link-to-libcmt.rs b/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std-link-to-libcmt.rs new file mode 100644 index 0000000000000..34347ab80cce0 --- /dev/null +++ b/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std-link-to-libcmt.rs @@ -0,0 +1,32 @@ +// build-pass +// compile-flags: -C panic=abort -C target-feature=+crt-static +// aux-build:exit-success-if-unwind-msvc-no-std.rs +// only-msvc +// Test that `no_std` with `panic=abort` under MSVC toolchain +// doesn't cause error when linking to libcmt. +// We don't run this executable because it will hang in `rust_begin_unwind` + +#![no_std] +#![no_main] + +extern crate exit_success_if_unwind_msvc_no_std; + +use core::panic::PanicInfo; + +#[panic_handler] +fn handle_panic(_: &PanicInfo) -> ! { + loop {} +} + +#[link(name = "libcmt")] +extern "C" {} + +#[no_mangle] +pub extern "C" fn main() -> i32 { + exit_success_if_unwind_msvc_no_std::bar(do_panic); + 0 +} + +fn do_panic() { + panic!(); +} diff --git a/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std-link-to-msvcrt.rs b/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std-link-to-msvcrt.rs new file mode 100644 index 0000000000000..ef071b565fe3e --- /dev/null +++ b/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std-link-to-msvcrt.rs @@ -0,0 +1,32 @@ +// build-pass +// compile-flags: -C panic=abort +// aux-build:exit-success-if-unwind-msvc-no-std.rs +// only-msvc +// Test that `no_std` with `panic=abort` under MSVC toolchain +// doesn't cause error when linking to msvcrt. +// We don't run this executable because it will hang in `rust_begin_unwind` + +#![no_std] +#![no_main] + +extern crate exit_success_if_unwind_msvc_no_std; + +use core::panic::PanicInfo; + +#[panic_handler] +fn handle_panic(_: &PanicInfo) -> ! { + loop {} +} + +#[link(name = "msvcrt")] +extern "C" {} + +#[no_mangle] +pub extern "C" fn main() -> i32 { + exit_success_if_unwind_msvc_no_std::bar(do_panic); + 0 +} + +fn do_panic() { + panic!(); +} diff --git a/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std.rs b/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std.rs new file mode 100644 index 0000000000000..5c90285695db7 --- /dev/null +++ b/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std.rs @@ -0,0 +1,54 @@ +// build-pass +// compile-flags:-C panic=abort +// aux-build:exit-success-if-unwind-msvc-no-std.rs +// no-prefer-dynamic +// only-msvc +// We don't run this executable because it will hang in `rust_begin_unwind` + +#![no_std] +#![no_main] +#![windows_subsystem = "console"] +#![feature(panic_abort)] + +extern crate exit_success_if_unwind_msvc_no_std; +extern crate panic_abort; + +use core::panic::PanicInfo; + +#[panic_handler] +fn handle_panic(_: &PanicInfo) -> ! { + loop {} +} + +#[no_mangle] +pub unsafe extern "C" fn memcpy(dest: *mut u8, _src: *const u8, _n: usize) -> *mut u8 { + dest +} + +#[no_mangle] +pub unsafe extern "C" fn memmove(dest: *mut u8, _src: *const u8, _n: usize) -> *mut u8 { + dest +} + +#[no_mangle] +pub unsafe extern "C" fn memset(mem: *mut u8, _val: i32, _n: usize) -> *mut u8 { + mem +} + +#[no_mangle] +pub unsafe extern "C" fn memcmp(_mem1: *const u8, _mem2: *const u8, _n: usize) -> i32 { + 0 +} + +#[no_mangle] +#[used] +static _fltused: i32 = 0; + +#[no_mangle] +pub extern "C" fn mainCRTStartup() { + exit_success_if_unwind_msvc_no_std::bar(main); +} + +fn main() { + panic!(); +} diff --git a/src/test/ui/panic-runtime/auxiliary/exit-success-if-unwind-msvc-no-std.rs b/src/test/ui/panic-runtime/auxiliary/exit-success-if-unwind-msvc-no-std.rs new file mode 100644 index 0000000000000..7afe4f17e6d7f --- /dev/null +++ b/src/test/ui/panic-runtime/auxiliary/exit-success-if-unwind-msvc-no-std.rs @@ -0,0 +1,24 @@ +// compile-flags:-C panic=unwind +// no-prefer-dynamic + +#![no_std] +#![crate_type = "rlib"] + +struct Bomb; + +impl Drop for Bomb { + fn drop(&mut self) { + #[link(name = "kernel32")] + extern "C" { + fn ExitProcess(code: u32) -> !; + } + unsafe { + ExitProcess(0); + } + } +} + +pub fn bar(f: fn()) { + let _bomb = Bomb; + f(); +} From 48acdb719731ba98a1cc5635d26d08cbe8809e4b Mon Sep 17 00:00:00 2001 From: Berrysoft Date: Sun, 4 Apr 2021 22:52:34 +0800 Subject: [PATCH 3/6] Add tests for unwind no_std. --- .../unwind-msvc-no-std-link-to-libcmt.rs | 47 +++++++++++++++++++ .../unwind-msvc-no-std-link-to-msvcrt.rs | 47 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/test/ui/panic-runtime/unwind-msvc-no-std-link-to-libcmt.rs create mode 100644 src/test/ui/panic-runtime/unwind-msvc-no-std-link-to-msvcrt.rs diff --git a/src/test/ui/panic-runtime/unwind-msvc-no-std-link-to-libcmt.rs b/src/test/ui/panic-runtime/unwind-msvc-no-std-link-to-libcmt.rs new file mode 100644 index 0000000000000..b27b95ee050f9 --- /dev/null +++ b/src/test/ui/panic-runtime/unwind-msvc-no-std-link-to-libcmt.rs @@ -0,0 +1,47 @@ +// build-pass +// compile-flags: -C panic=unwind -C target-feature=+crt-static +// only-msvc +// Test that `no_std` with `panic=unwind` under MSVC toolchain +// doesn't cause error when linking to libcmt. + +#![no_std] +#![no_main] +#![feature(alloc_error_handler)] +#![feature(panic_unwind)] + +use core::alloc::{GlobalAlloc, Layout}; + +struct DummyAllocator; + +unsafe impl GlobalAlloc for DummyAllocator { + unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { + core::ptr::null_mut() + } + + unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} +} + +#[global_allocator] +static ALLOC: DummyAllocator = DummyAllocator; + +#[alloc_error_handler] +fn rust_oom(_layout: Layout) -> ! { + panic!() +} + +extern crate panic_unwind; + +use core::panic::PanicInfo; + +#[panic_handler] +fn handle_panic(_: &PanicInfo) -> ! { + loop {} +} + +#[link(name = "libcmt")] +extern "C" {} + +#[no_mangle] +pub extern "C" fn main() -> i32 { + panic!(); +} diff --git a/src/test/ui/panic-runtime/unwind-msvc-no-std-link-to-msvcrt.rs b/src/test/ui/panic-runtime/unwind-msvc-no-std-link-to-msvcrt.rs new file mode 100644 index 0000000000000..95036e5774cee --- /dev/null +++ b/src/test/ui/panic-runtime/unwind-msvc-no-std-link-to-msvcrt.rs @@ -0,0 +1,47 @@ +// build-pass +// compile-flags: -C panic=unwind +// only-msvc +// Test that `no_std` with `panic=unwind` under MSVC toolchain +// doesn't cause error when linking to msvcrt. + +#![no_std] +#![no_main] +#![feature(alloc_error_handler)] +#![feature(panic_unwind)] + +use core::alloc::{GlobalAlloc, Layout}; + +struct DummyAllocator; + +unsafe impl GlobalAlloc for DummyAllocator { + unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { + core::ptr::null_mut() + } + + unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} +} + +#[global_allocator] +static ALLOC: DummyAllocator = DummyAllocator; + +#[alloc_error_handler] +fn rust_oom(_layout: Layout) -> ! { + panic!() +} + +extern crate panic_unwind; + +use core::panic::PanicInfo; + +#[panic_handler] +fn handle_panic(_: &PanicInfo) -> ! { + loop {} +} + +#[link(name = "msvcrt")] +extern "C" {} + +#[no_mangle] +pub extern "C" fn main() -> i32 { + panic!(); +} From fc4bf6d06b150d5e514726399b5aaa707cf8226d Mon Sep 17 00:00:00 2001 From: Berrysoft Date: Tue, 6 Apr 2021 12:42:29 +0800 Subject: [PATCH 4/6] Fix ExitProcess linkage. --- .../auxiliary/exit-success-if-unwind-msvc-no-std.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/panic-runtime/auxiliary/exit-success-if-unwind-msvc-no-std.rs b/src/test/ui/panic-runtime/auxiliary/exit-success-if-unwind-msvc-no-std.rs index 7afe4f17e6d7f..9a167a9ec6e85 100644 --- a/src/test/ui/panic-runtime/auxiliary/exit-success-if-unwind-msvc-no-std.rs +++ b/src/test/ui/panic-runtime/auxiliary/exit-success-if-unwind-msvc-no-std.rs @@ -9,7 +9,7 @@ struct Bomb; impl Drop for Bomb { fn drop(&mut self) { #[link(name = "kernel32")] - extern "C" { + extern "system" { fn ExitProcess(code: u32) -> !; } unsafe { From c162f8ac4fb9eaeacecaf8070169ed314e66446b Mon Sep 17 00:00:00 2001 From: Berrysoft Date: Tue, 6 Apr 2021 22:21:59 +0800 Subject: [PATCH 5/6] Link to ntdll to fix some crt symbols --- .../ui/panic-runtime/abort-link-to-unwind-msvc-no-std.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std.rs b/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std.rs index 5c90285695db7..ba5c41e4fa888 100644 --- a/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std.rs +++ b/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std.rs @@ -40,10 +40,15 @@ pub unsafe extern "C" fn memcmp(_mem1: *const u8, _mem2: *const u8, _n: usize) - 0 } +// Used by compiler_builtins #[no_mangle] #[used] static _fltused: i32 = 0; +// MSVC always assumes that some functions are avaliable +#[link(name = "ntdll")] +extern "system" {} + #[no_mangle] pub extern "C" fn mainCRTStartup() { exit_success_if_unwind_msvc_no_std::bar(main); From 7a54baa6d3bf4d85c51b1dbe5721880786fbe46a Mon Sep 17 00:00:00 2001 From: Berrysoft Date: Thu, 8 Apr 2021 22:12:15 +0800 Subject: [PATCH 6/6] Run test for abort-link-to-unwind. --- .../abort-link-to-unwind-msvc-no-std-link-to-libcmt.rs | 9 +-------- .../abort-link-to-unwind-msvc-no-std-link-to-msvcrt.rs | 9 +-------- .../ui/panic-runtime/abort-link-to-unwind-msvc-no-std.rs | 9 +-------- .../auxiliary/exit-success-if-unwind-msvc-no-std.rs | 8 ++++++++ 4 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std-link-to-libcmt.rs b/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std-link-to-libcmt.rs index 34347ab80cce0..324bd7e296dab 100644 --- a/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std-link-to-libcmt.rs +++ b/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std-link-to-libcmt.rs @@ -1,4 +1,4 @@ -// build-pass +// run-fail // compile-flags: -C panic=abort -C target-feature=+crt-static // aux-build:exit-success-if-unwind-msvc-no-std.rs // only-msvc @@ -11,13 +11,6 @@ extern crate exit_success_if_unwind_msvc_no_std; -use core::panic::PanicInfo; - -#[panic_handler] -fn handle_panic(_: &PanicInfo) -> ! { - loop {} -} - #[link(name = "libcmt")] extern "C" {} diff --git a/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std-link-to-msvcrt.rs b/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std-link-to-msvcrt.rs index ef071b565fe3e..11c8b1d033d80 100644 --- a/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std-link-to-msvcrt.rs +++ b/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std-link-to-msvcrt.rs @@ -1,4 +1,4 @@ -// build-pass +// run-fail // compile-flags: -C panic=abort // aux-build:exit-success-if-unwind-msvc-no-std.rs // only-msvc @@ -11,13 +11,6 @@ extern crate exit_success_if_unwind_msvc_no_std; -use core::panic::PanicInfo; - -#[panic_handler] -fn handle_panic(_: &PanicInfo) -> ! { - loop {} -} - #[link(name = "msvcrt")] extern "C" {} diff --git a/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std.rs b/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std.rs index ba5c41e4fa888..a738a39d78001 100644 --- a/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std.rs +++ b/src/test/ui/panic-runtime/abort-link-to-unwind-msvc-no-std.rs @@ -1,4 +1,4 @@ -// build-pass +// run-fail // compile-flags:-C panic=abort // aux-build:exit-success-if-unwind-msvc-no-std.rs // no-prefer-dynamic @@ -13,13 +13,6 @@ extern crate exit_success_if_unwind_msvc_no_std; extern crate panic_abort; -use core::panic::PanicInfo; - -#[panic_handler] -fn handle_panic(_: &PanicInfo) -> ! { - loop {} -} - #[no_mangle] pub unsafe extern "C" fn memcpy(dest: *mut u8, _src: *const u8, _n: usize) -> *mut u8 { dest diff --git a/src/test/ui/panic-runtime/auxiliary/exit-success-if-unwind-msvc-no-std.rs b/src/test/ui/panic-runtime/auxiliary/exit-success-if-unwind-msvc-no-std.rs index 9a167a9ec6e85..e2be90f5addb0 100644 --- a/src/test/ui/panic-runtime/auxiliary/exit-success-if-unwind-msvc-no-std.rs +++ b/src/test/ui/panic-runtime/auxiliary/exit-success-if-unwind-msvc-no-std.rs @@ -3,6 +3,7 @@ #![no_std] #![crate_type = "rlib"] +#![feature(core_intrinsics)] struct Bomb; @@ -22,3 +23,10 @@ pub fn bar(f: fn()) { let _bomb = Bomb; f(); } + +use core::panic::PanicInfo; + +#[panic_handler] +fn handle_panic(_: &PanicInfo) -> ! { + core::intrinsics::abort(); +}