Issue |
136547
|
Summary |
Crashed when use clang to compile with a ir debug location printing pass plugin
|
Labels |
clang
|
Assignees |
|
Reporter |
Wakotu
|
I write a pass plugin like following and use clang to compile with that plugin. And then clang crashed.
### Pass Plugin
My Pass Plugin to print Debug line and column location for each IR Instruction:
```cpp
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Passes/OptimizationLevel.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/PassPlugin.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
void print_ir_loc(Instruction &Inst, int idx) {
const DebugLoc &DL = Inst.getDebugLoc();
errs() << "Before" << "\n";
unsigned line = DL.getLine();
errs() << "After" << "\n";
unsigned col = DL.getCol();
outs() << "Instruction " << idx << "\n";
outs() << "line: " << line << ", col: " << col << "\n";
outs() << "\n";
}
void print_ir_loc_for_module(Module &M) {
int idx = 0;
for (Function &F : M) {
for (auto &BB : F) {
for (auto &I : BB) {
idx += 1;
print_ir_loc(I, idx);
}
}
}
}
struct MyPass : PassInfoMixin<MyPass> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM) {
print_ir_loc_for_module(M);
return PreservedAnalyses::all();
}
};
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, "MyPass", "v0.1", [](PassBuilder &PB) {
PB.registerOptimizerEarlyEPCallback(
[](ModulePassManager &MPM, OptimizationLevel) {
MPM.addPass(MyPass());
});
}};
}
```
### Terminal Output
Terminal Output as following. Note that the `Before` text was printed while `After` was not, which means clang crashed at `unsigned line = DL.getLine();` .
```text
Before
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: /usr/lib/llvm-18/bin/clang -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -dumpdir a- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name hello.c -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 -debug-info-kind=constructor -dwarf-version=5 -debugger-tuning=gdb -fdebug-compilation-dir=/home/yqy/toys/cc/pass_plugin -fcoverage-compilation-dir=/home/yqy/toys/cc/pass_plugin -resource-dir /usr/lib/llvm-18/lib/clang/18 -internal-isystem /usr/lib/llvm-18/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/13/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcolor-diagnostics -fpass-plugin=MyPass.so -dwarf-debug-flags "/usr/lib/llvm-18/bin/clang -fpass-plugin=MyPass.so -g ./examples/hello.c -dumpdir a-" -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/hello-7eeb7e.o -x c ./examples/hello.c
1. <eof> parser at end of file
2. Optimizer
#0 0x00007f37089e73ff llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/lib/x86_64-linux-gnu/libLLVM-18.so.18.1+0xda63ff)
#1 0x00007f37089e5539 llvm::sys::RunSignalHandlers() (/lib/x86_64-linux-gnu/libLLVM-18.so.18.1+0xda4539)
#2 0x00007f37089e7b40 (/lib/x86_64-linux-gnu/libLLVM-18.so.18.1+0xda6b40)
#3 0x00007f37077f6330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
#4 0x00007f3708ac1f7b llvm::DebugLoc::getLine() const (/lib/x86_64-linux-gnu/libLLVM-18.so.18.1+0xe80f7b)
#5 0x00007f3705345f58 print_ir_loc(llvm::Instruction&, int) (./MyPass.so+0xaf58)
#6 0x00007f3705346123 print_ir_loc_for_module(llvm::Module&) (./MyPass.so+0xb123)
#7 0x00007f370534861c MyPass::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (./MyPass.so+0xd61c)
#8 0x00007f3705348524 llvm::detail::PassModel<llvm::Module, MyPass, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (./MyPass.so+0xd524)
#9 0x00007f3708b6f341 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/lib/x86_64-linux-gnu/libLLVM-18.so.18.1+0xf2e341)
#10 0x00007f3710e76730 (/usr/lib/llvm-18/lib/libclang-cpp.so.18.1+0x1c1f730)
#11 0x00007f3710e6e093 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>, clang::BackendConsumer*) (/usr/lib/llvm-18/lib/libclang-cpp.so.18.1+0x1c17093)
#12 0x00007f37111f6c57 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/usr/lib/llvm-18/lib/libclang-cpp.so.18.1+0x1f9fc57)
#13 0x00007f370fdee546 clang::ParseAST(clang::Sema&, bool, bool) (/usr/lib/llvm-18/lib/libclang-cpp.so.18.1+0xb97546)
#14 0x00007f3711c5dcdc clang::FrontendAction::Execute() (/usr/lib/llvm-18/lib/libclang-cpp.so.18.1+0x2a06cdc)
#15 0x00007f3711bda764 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/lib/llvm-18/lib/libclang-cpp.so.18.1+0x2983764)
#16 0x00007f3711cd9ced clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/llvm-18/lib/libclang-cpp.so.18.1+0x2a82ced)
#17 0x000056378c5003fe cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/lib/llvm-18/bin/clang+0x143fe)
#18 0x000056378c4fd864 (/usr/lib/llvm-18/bin/clang+0x11864)
#19 0x000056378c4fc9f6 clang_main(int, char**, llvm::ToolContext const&) (/usr/lib/llvm-18/bin/clang+0x109f6)
#20 0x000056378c50a353 main (/usr/lib/llvm-18/bin/clang+0x1e353)
#21 0x00007f37077db1ca __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#22 0x00007f37077db28b call_init ./csu/../csu/libc-start.c:128:20
#23 0x00007f37077db28b __libc_start_main ./csu/../csu/libc-start.c:347:5
#24 0x000056378c4fa225 _start (/usr/lib/llvm-18/bin/clang+0xe225)
clang: error: unable to execute command: Segmentation fault (core dumped)
clang: error: clang frontend command failed due to signal (use -v to see invocation)
Ubuntu clang version 18.1.8 (++20240731025043+3b5b5c1ec4a3-1~exp1~20240731145144.92)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-18/bin
clang: note: diagnostic msg:
```
### Source
Source file to compile, named `hello.c`:
```cpp
#include <stdio.h>
#include <stdlib.h>
int add(int a, int b) {
if (a + b < 20) {
return a + b;
} else {
return a - b;
}
}
int main(int argc, char *argv[]) {
if (3 != argc) {
fprintf(stderr, "Usage: %s <a> <b>\n", argv[0]);
exit(1);
}
int a = atoi(argv[1]);
int b = atoi(argv[2]);
add(a, b);
return 0;
}
```
### Attached File
Preprocessed sources:
[hello-1432ad.c.log](https://github.com/user-attachments/files/19831758/hello-1432ad.c.log)
associated run scripts:
[hello-1432ad.sh.log](https://github.com/user-attachments/files/19831766/hello-1432ad.sh.log)
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs