llvm / llvm/llvm-project

[clang] Crash in clang when compiling IR program

Open
#219,223 8 comments 0 reactions 0 assignees View on GitHub
backend:X86 confirmed crash-on-valid
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Compiling the attached IR with clang aborts during code generation.

### Input Program

```llvm
target triple = "x86_64-pc-linux-gnu"

declare i32 @__CxxFrameHandler3(...)
declare void @f()

define void @foo() personality ptr @__CxxFrameHandler3 {
entry:
invoke void @f() to label %cont unwind label %dispatch

dispatch:
%cs = catchswitch within none [label %catch] unwind to caller

catch:
%pad = catchpad within %cs []
catchret from %pad to label %cont

cont:
ret void
}
```

### Stack Trace

```
warning: overriding the module target triple with x86_64-unknown-linux-gnu [-Woverride-module]
clang-23: /llvm/include/llvm/Support/Casting.h:572: decltype(auto) llvm::cast(From*) [with To = Constant; From = Value]: Assertion `isa(Val) && "cast() argument of incompatible type!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and dumped files.
Stack dump:
0. Program arguments: /build/bin/clang-23 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -dumpdir test.out- -disable-free -clear-ast-before-backend -main-file-name test.ll -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir= -fcoverage-compilation-dir= -resource-dir /build/lib/clang/23 -ferror-limit 19 -fmessage-length=130 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -faddrsig -fdwarf2-cfi-asm -o /tmp/test-b4c23b.o -x ir ./test.ll
1. Code generation
2. Running pass 'Function Pass Manager' on module './test.ll'.
3. Running pass 'X86 DAG->DAG Instruction Selection' on function '@foo'
#0 0x0000581f14812dac llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/build/bin/clang-23+0x28f7dac)
#1 0x0000581f1480fb44 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#2 0x0000581f1480fc2c SignalHandlerTerminate(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0x000072aa1fa45cb0 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x45cb0)
#4 0x000072aa1faa648c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
#5 0x000072aa1faa648c __pthread_kill_internal ./nptl/pthread_kill.c:89:10
#6 0x000072aa1faa648c pthread_kill ./nptl/pthread_kill.c:100:10
#7 0x000072aa1fa45b7e raise ./signal/../sysdeps/posix/raise.c:27:6
#8 0x000072aa1fa288ec internal_signal_block_all ./stdlib/../sysdeps/unix/sysv/linux/internal-signals.h:81:3
#9 0x000072aa1fa288ec __abort_lock_wrlock ./stdlib/abort.c:62:3
#10 0x000072aa1fa288ec abort ./stdlib/abort.c:85:3
#11 0x000072aa1fa29979 _IO_acquire_lock_fct ./libio/libioP.h:991:6
#12 0x000072aa1fa29979 _IO_peekc_locked ./libio/peekc.c:37:3
#13 0x000072aa1fa3bf75 __libc_message_wrapper ./assert/../include/stdio.h:203:3
#14 0x000072aa1fa3bf75 __assert_fail ./assert/assert.c:37:3
#15 0x0000581f12ba6738 addTryBlockMapEntry(llvm::WinEHFuncInfo&, int, int, int, llvm::ArrayRef) (.cold) WinEHPrepare.cpp:0:0
#16 0x0000581f13e5fb03 calculateCXXStateNumbers(llvm::WinEHFuncInfo&, llvm::Instruction const*, int) WinEHPrepare.cpp:0:0
#17 0x0000581f13e5fddf llvm::calculateWinCXXEHStateNumbers(llvm::Function const*, llvm::WinEHFuncInfo&) (/build/bin/clang-23+0x1f44ddf)
#18 0x0000581f15bff299 llvm::FunctionLoweringInfo::set(llvm::Function const&, llvm::MachineFunction&, llvm::SelectionDAG*) (/build/bin/clang-23+0x3ce4299)
#19 0x0000581f15d3ffd8 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (/build/bin/clang-23+0x3e24fd8)
#20 0x0000581f15d38c18 llvm::SelectionDAGISelLegacy::runOnMachineFunction(llvm::MachineFunction&) (/build/bin/clang-23+0x3e1dc18)
#21 0x0000581f13b9a8cc llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) MachineFunctionPass.cpp:0:0
#22 0x0000581f142183b1 llvm::FPPassManager::runOnFunction(llvm::Function&) (/build/bin/clang-23+0x22fd3b1)
#23 0x0000581f14218975 llvm::FPPassManager::runOnModule(llvm::Module&) (/build/bin/clang-23+0x22fd975)
#24 0x0000581f1421946f (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) LegacyPassManager.cpp:0:0
#25 0x0000581f14219b5b llvm::legacy::PassManagerImpl::run(llvm::Module&) (/build/bin/clang-23+0x22feb5b)
#26 0x0000581f14a84a33 (anonymous namespace)::EmitAssemblyHelper::TimeCodegenPasses(llvm::function_ref) BackendUtil.cpp:0:0
#27 0x0000581f14a9ac38 (anonymous namespace)::EmitAssemblyHelper::emitAssembly(clang::BackendAction, std::unique_ptr>, clang::BackendConsumer*) BackendUtil.cpp:0:0
#28 0x0000581f14a9b294 clang::emitBackendOutput(clang::CompilerInstance&, clang::CodeGenOptions&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::IntrusiveRefCntPtr, std::unique_ptr>, clang::BackendConsumer*) (/build/bin/clang-23+0x2b80294)
#29 0x0000581f15105a22 clang::CodeGenAction::ExecuteAction() (/build/bin/clang-23+0x31eaa22)
#30 0x0000581f1548e31f clang::FrontendAction::Execute() (/build/bin/clang-23+0x357331f)
#31 0x0000581f15413d32 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/build/bin/clang-23+0x34f8d32)
#32 0x0000581f1556463a clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/build/bin/clang-23+0x364963a)
#33 0x0000581f1327ab34 cc1_main(llvm::ArrayRef, char const*, void*) (/build/bin/clang-23+0x135fb34)
#34 0x0000581f13272338 ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#35 0x0000581f13275fb7 clang_main(int, char**, llvm::ToolContext const&) (/build/bin/clang-23+0x135afb7)
#36 0x0000581f131baa0e main (/build/bin/clang-23+0x129fa0e)
#37 0x000072aa1fa2a601 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:83:3
#38 0x000072aa1fa2a718 call_init ./csu/../csu/libc-start.c:128:20
#39 0x000072aa1fa2a718 __libc_start_main ./csu/../sysdeps/nptl/libc-start.c:347:5
#40 0x0000581f13271565 _start (/build/bin/clang-23+0x1356565)
clang-23: error: unable to execute command: Aborted
clang-23: error: clang frontend command failed with exit code -2 (use -v to see invocation)
clang version 23.0.0git (https://github.com/llvm/llvm-project.git fff064e93d144a6499efcf091e1f226d144e514f)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /build/bin
Build config: +assertions
clang-23: note: diagnostic msg:
********************

PLEASE ATTACH THE FOLLOWING CRASH REPRODUCER FILES TO THE BUG REPORT:
clang-23: note: diagnostic msg: /tmp/test-eea5d7.ll
clang-23: note: diagnostic msg: /tmp/test-eea5d7.sh
clang-23: note: diagnostic msg:

********************
Aborted /build/bin/clang-23 ./test.ll -o test.out
```

Version: 23.0.0
Commit id: fff064e93d144a6499efcf091e1f226d144e514f

Contributor guide

Open the contributing guide

Research direction

Reproduce the crash with the provided LLVM IR and the clang command, then inspect the WinEHPrepare.cpp frames in the stack trace, especially calculateCXXStateNumbers and addTryBlockMapEntry. Done means this input no longer aborts during X86 DAG-to-DAG instruction selection and the regression is covered by an appropriate test.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.