rustc: assertion failiure in llvm when defining __CxxFrameHandler3

fb20859
Opened by Jonathan Nilsson at 2023-08-15 19:40:45

I'm building a 64k demo on windows so i'm building without std and without libc. When writing some code a reference to __CxxFrameHandler3 is emitted even though i'm building with panic=abort. I suspect this is because libcore is built with unwinding. In release builds the symbol __CxxFrameHandler3 is optimized away so the program links successfully. If i try to define __CxxFrameHandler3 i get an assertion failure in llvm.

I tried this code:

Build with rustc --crate-type bin -C panic=abort main.rs

#![feature(lang_items)]
#![feature(link_args)]

#![no_std]
#![no_main]

#[link_args = "/SUBSYSTEM:WINDOWS"]
extern "C" {}

#[no_mangle]
pub extern "system" fn __CxxFrameHandler3(_: *mut u8, _: *mut u8, _: *mut u8, _: *mut u8) -> i32 { 0 }

#[no_mangle]
pub extern "system" fn WinMainCRTStartup() {

    for _ in 0..1 {}
}

#[lang = "panic_fmt"]
#[no_mangle]
pub extern "C" fn rust_begin_panic(_: core::fmt::Arguments, _: &'static str, _: u32) -> ! { loop {} }

I expected to see this happen: Preferably the symbol __CxxFrameHandler3 would not be emitted. But being able to define it without a crash is ok as well.

Instead, this happened: The symbol __CxxFrameHandler3 is emitted and when i try to define it i get a crash.

Current workaround is to pass /FORCE to the linker to ignore the unresolved symbol.

Meta

rustc --version --verbose:

rustc 1.15.0-nightly (71c06a56a 2016-12-18)
binary: rustc
commit-hash: 71c06a56a120a0d5e3b224105ee3e6754f83e5fa
commit-date: 2016-12-18
host: x86_64-pc-windows-msvc
release: 1.15.0-nightly
LLVM version: 3.9

Output from rustc

Assertion failed: isa<X>(Val) && "cast<Ty>() argument of incompatible type!", file C:\bot\slave\nightly-dist-rustc-win-msvc-64\build\src\llvm\include\llvm/Support/Casting.h, line 237

Backtrace

msvcr120.dll!_wassert(const wchar_t * expr, const wchar_t * filename, unsigned int lineno) Line 142
rustc_llvm-039c192262ead9ec.dll!00007ff969b9b87c()
rustc_trans-ba0c504011b26d52.dll!00007ff970296ccb()
rustc_trans-ba0c504011b26d52.dll!00007ff97026d1b4()
rustc_trans-ba0c504011b26d52.dll!00007ff9702b673b()
rustc_trans-ba0c504011b26d52.dll!00007ff9702debd2()
rustc_trans-ba0c504011b26d52.dll!00007ff97023b9fd()
rustc_driver-9b39ebb875ea3d3d.dll!00007ff9754b8383()
rustc_driver-9b39ebb875ea3d3d.dll!00007ff97548a519()
rustc_driver-9b39ebb875ea3d3d.dll!00007ff9754b5580()
rustc_driver-9b39ebb875ea3d3d.dll!00007ff975404ec9()
rustc_driver-9b39ebb875ea3d3d.dll!00007ff975486553()
rustc_driver-9b39ebb875ea3d3d.dll!00007ff9754d6f77()
rustc_driver-9b39ebb875ea3d3d.dll!00007ff9753bd69f()
std-efe4996faa4fd772.dll!00007ff974114612()
rustc_driver-9b39ebb875ea3d3d.dll!00007ff9753e6bc5()
std-efe4996faa4fd772.dll!00007ff97410c2ff()
  1. Thanks for the detailed report @JoNil !

    cc @vadimcn this might interest you.

    Brian Anderson at 2016-12-29 01:52:37

  2. anyway to resolve this issue?

    Yonggang Luo at 2020-10-10 17:38:54

  3. I haven't checked myself but the newish -Zbuild-std=core cargo flag might clear this up, assuming it really is a problem with libcore being compiled with unwinding.

    Lokathor at 2020-10-10 17:43:57

  4. Still actual - I tried to build a Windows kernel driver using a stable Rust and it failed due to unresolved __CxxFrameHandler3 in libcore for profile.dev.

    Александр at 2023-08-15 19:40:11