llvm / llvm/llvm-project

Clang generates a segmentation fault when compiling RISC-V assembly

Open
#193,443 9 comments 0 reactions 0 assignees View on GitHub
crash llvm:mc
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The `clang` executable has requested that I file a bug report regarding the segmentation fault that was generated during compilation. Please let me know if you need further information for reproduction.

The following message is produced when compiling a RISC-V assembly file:

```
Stack dump:
0. Program arguments: /usr/lib/llvm-18/bin/clang -cc1as -triple riscv32-unknown-unknown-elf -filetype obj -main-file-name id_stage_test.S -target-cpu generic-rv32 -target-feature -a -target-feature -c -target-feature -d -target-feature -e -target-feature -f -target-feature -h -target-feature -m -target-feature -smaia -target-feature -smepmp -target-feature -ssaia -target-feature -svinval -target-feature -svnapot -target-feature -svpbmt -target-feature -v -target-feature -xcvalu -target-feature -xcvbi -target-feature -xcvbitmanip -target-feature -xcvelw -target-feature -xcvmac -target-feature -xcvmem -target-feature -xcvsimd -target-feature -xsfvcp -target-feature -xsfvfnrclipxfqf -target-feature -xsfvfwmaccqqq -target-feature -xsfvqmaccdod -target-feature -xsfvqmaccqoq -target-feature -xtheadba -target-feature -xtheadbb -target-feature -xtheadbs -target-feature -xtheadcmo -target-feature -xtheadcondmov -target-feature -xtheadfmemidx -target-feature -xtheadmac -target-feature -xtheadmemidx -target-feature -xtheadmempair -target-feature -xtheadsync -target-feature -xtheadvdot -target-feature -xventanacondops -target-feature -za128rs -target-feature -za64rs -target-feature -zawrs -target-feature -zba -target-feature -zbb -target-feature -zbc -target-feature -zbkb -target-feature -zbkc -target-feature -zbkx -target-feature -zbs -target-feature -zca -target-feature -zcb -target-feature -zcd -target-feature -zce -target-feature -zcf -target-feature -zcmp -target-feature -zcmt -target-feature -zdinx -target-feature -zfa -target-feature -zfh -target-feature -zfhmin -target-feature -zfinx -target-feature -zhinx -target-feature -zhinxmin -target-feature -zic64b -target-feature -zicbom -target-feature -zicbop -target-feature -zicboz -target-feature -ziccamoa -target-feature -ziccif -target-feature -zicclsm -target-feature -ziccrse -target-feature -zicntr -target-feature -zicond -target-feature -zicsr -target-feature -zifencei -target-feature -zihintntl -target-feature -zihintpause -target-feature -zihpm -target-feature -zk -target-feature -zkn -target-feature -zknd -target-feature -zkne -target-feature -zknh -target-feature -zkr -target-feature -zks -target-feature -zksed -target-feature -zksh -target-feature -zkt -target-feature -zmmul -target-feature -zvbb -target-feature -zvbc -target-feature -zve32f -target-feature -zve32x -target-feature -zve64d -target-feature -zve64f -target-feature -zve64x -target-feature -zvfh -target-feature -zvfhmin -target-feature -zvkb -target-feature -zvkg -target-feature -zvkn -target-feature -zvknc -target-feature -zvkned -target-feature -zvkng -target-feature -zvknha -target-feature -zvknhb -target-feature -zvks -target-feature -zvksc -target-feature -zvksed -target-feature -zvksg -target-feature -zvksh -target-feature -zvkt -target-feature -zvl1024b -target-feature -zvl128b -target-feature -zvl16384b -target-feature -zvl2048b -target-feature -zvl256b -target-feature -zvl32768b -target-feature -zvl32b -target-feature -zvl4096b -target-feature -zvl512b -target-feature -zvl64b -target-feature -zvl65536b -target-feature -zvl8192b -target-feature -experimental-zacas -target-feature -experimental-zcmop -target-feature -experimental-zfbfmin -target-feature -experimental-zicfilp -target-feature -experimental-zicfiss -target-feature -experimental-zimop -target-feature -experimental-ztso -target-feature -experimental-zvfbfmin -target-feature -experimental-zvfbfwma -target-feature +relax -fdebug-compilation-dir=/home/rantanv2/Projects/apina -dwarf-debug-producer "Ubuntu clang version 18.1.3 (1ubuntu1)" -dwarf-version=4 -mrelocation-model static -target-abi ilp32 -mllvm -riscv-add-build-attributes -o /tmp/id_stage_test-403897.o /tmp/id_stage_test-060ccc.s
#0 0x00007387fe5a63bf llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) build-llvm/tools/clang/stage2-bins/llvm/lib/Support/Unix/Signals.inc:723:13
#1 0x00007387fe5a44f9 llvm::sys::RunSignalHandlers() build-llvm/tools/clang/stage2-bins/llvm/lib/Support/Signals.cpp:106:18
#2 0x00007387fe5a6b00 SignalHandler build-llvm/tools/clang/stage2-bins/llvm/lib/Support/Unix/Signals.inc:413:1
#3 0x00007387fd045330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
#4 0x00007387ffc14efd recordRelocation build-llvm/tools/clang/stage2-bins/llvm/lib/MC/ELFObjectWriter.cpp:1454:60
#5 0x00007388010fb1a8 _M_ptr /usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/unique_ptr.h:199:51
#6 0x00007388010fb1a8 get /usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/unique_ptr.h:470:21
#7 0x00007388010fb1a8 operator* /usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/unique_ptr.h:454:10
#8 0x00007388010fb1a8 getWriter build-llvm/tools/clang/stage2-bins/llvm/include/llvm/MC/MCAssembler.h:336:46
#9 0x00007388010fb1a8 handleAddSubRelocations build-llvm/tools/clang/stage2-bins/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp:629:7
#10 0x00007387ffc2caba handleFixup build-llvm/tools/clang/stage2-bins/llvm/lib/MC/MCAssembler.cpp:806:7
#11 0x00007387ffc2caba llvm::MCAssembler::layout(llvm::MCAsmLayout&) build-llvm/tools/clang/stage2-bins/llvm/lib/MC/MCAssembler.cpp:940:13
#12 0x00007387ffc2cdef _M_ptr /usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/unique_ptr.h:199:51
#13 0x00007387ffc2cdef get /usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/unique_ptr.h:470:21
#14 0x00007387ffc2cdef operator* /usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/unique_ptr.h:454:10
#15 0x00007387ffc2cdef getWriter build-llvm/tools/clang/stage2-bins/llvm/include/llvm/MC/MCAssembler.h:336:46
#16 0x00007387ffc2cdef llvm::MCAssembler::Finish() build-llvm/tools/clang/stage2-bins/llvm/lib/MC/MCAssembler.cpp:954:25
#17 0x00007387ffc4c2ea llvm::MCELFStreamer::finishImpl() build-llvm/tools/clang/stage2-bins/llvm/lib/MC/MCELFStreamer.cpp:721:1
#18 0x00007387ffcab1ac Run build-llvm/tools/clang/stage2-bins/llvm/lib/MC/MCParser/AsmParser.cpp:0:9
#19 0x0000651271621a44 ExecuteAssemblerImpl build-llvm/tools/clang/stage2-bins/clang/tools/driver/cc1as_main.cpp:588:22
#20 0x0000651271621a44 ExecuteAssembler build-llvm/tools/clang/stage2-bins/clang/tools/driver/cc1as_main.cpp:596:17
#21 0x0000651271621a44 cc1as_main(llvm::ArrayRef, char const*, void*) build-llvm/tools/clang/stage2-bins/clang/tools/driver/cc1as_main.cpp:675:45
#22 0x000065127161992d ExecuteCC1Tool build-llvm/tools/clang/stage2-bins/clang/tools/driver/driver.cpp:367:12
#23 0x0000651271618a26 clang_main(int, char**, llvm::ToolContext const&) build-llvm/tools/clang/stage2-bins/clang/tools/driver/driver.cpp:405:12
#24 0x0000651271626383 main build-llvm/tools/clang/stage2-bins/build-llvm/tools/clang/stage2-bins/tools/clang/tools/driver/clang-driver.cpp:17:10
#25 0x00007387fd02a1ca __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#26 0x00007387fd02a28b call_init ./csu/../csu/libc-start.c:128:20
#27 0x00007387fd02a28b __libc_start_main ./csu/../csu/libc-start.c:347:5
#28 0x0000651271616255 _start (/usr/lib/llvm-18/bin/clang+0xe255)
clang: error: unable to execute command: Segmentation fault (core dumped)
clang: error: clang integrated assembler command failed due to signal (use -v to see invocation)
Ubuntu clang version 18.1.3 (1ubuntu1)
Target: riscv32-unknown-unknown-elf
Thread model: posix
InstalledDir: /usr/bin
clang: note: diagnostic msg:
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/id_stage_test-880778.S
clang: note: diagnostic msg: /tmp/id_stage_test-880778.sh
clang: note: diagnostic msg:

********************
```

The files requested:

[id_stage_test-880778.sh](https://github.com/user-attachments/files/26961025/id_stage_test-880778.sh)

[id_stage_test-880778.txt](https://github.com/user-attachments/files/26961052/id_stage_test-880778.txt) (Note change in extension)

Contributor guide

Open the contributing guide

Research direction

Start by running the attached id_stage_test-880778.sh with id_stage_test-880778.txt using the reported Clang configuration. Read RISCVAsmBackend.cpp at handleAddSubRelocations and ELFObjectWriter.cpp at recordRelocation, then trace the failing assembler path. Done means the supplied RISC-V assembly no longer causes clang to segfault.

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
Quiet
Clarity
Needs clarification
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.