Why clang is crashing while using the pass, which is injecting print function inside code

I wrote a pass that is injecting print function inside a CPP code. The pass is working fine with opt but while I am running the pass with clang, it is crashing at the end.

This works fine with the IR generated using clang:

opt -load <llvm path>/build/lib/LLVMmodule_dir.so -enable-new-pm=0 -legacy-inject-func-call bb.ll -o bb.bin

But not while directly using the clang to inject print,

/path/to/llvm-project/build/bin/clang++  -flegacy-pass-manager -g -Xclang -load -Xclang /path/to//llvm-project/build/lib/libInjectFuncCallPass.so hello.cpp

Error:

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: /home/ubuntu-18/llvm-project-14.0.6.src/build/bin/clang-14 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -main-file-name hello.cpp -mrelocation-model static -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debug-info-kind=constructor -dwarf-version=5 -debugger-tuning=gdb -fcoverage-compilation-dir=/home/ubuntu-18 -resource-dir /home/ubuntu-18/llvm-project-14.0.6.src/build/lib/clang/14.0.6 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/backward -internal-isystem /home/ubuntu-18/llvm-project-14.0.6.src/build/lib/clang/14.0.6/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.5.0/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir=/home/ubuntu-18 -ferror-limit 19 -fgnuc-version=4.2.1 -flegacy-pass-manager -fcxx-exceptions -fexceptions -fcolor-diagnostics -load /home/ubuntu-18/llvm-project-14.0.6.src/build/lib/libInjectFuncCallPass.so -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/hello-75f35c.o -x c++ hello.cpp
1.	<eof> parser at end of file
2.	Per-function optimization
3.	Running pass 'LegacyInjectFuncCall' on function '@__cxx_global_var_init'
 #0 0x00007f758901dc5f PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x00007f758901ab2d SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f75884caf10 (/lib/x86_64-linux-gnu/libc.so.6+0x3ef10)
 #3 0x00007f7589f03268 llvm::PointerType::get(llvm::Type*, unsigned int) (/home/ubuntu-18/llvm-project-14.0.6.src/build/bin/../lib/libLLVMCore.so.14+0x2b4268)
 #4 0x00007f757ddc0b92 InjectFuncCall::runOnModule(llvm::Module&) (/home/ubuntu-18/llvm-project-14.0.6.src/build/lib/libInjectFuncCallPass.so+0x2b92)
 #5 0x00007f7589e9596f llvm::FPPassManager::runOnFunction(llvm::Function&) (/home/ubuntu-18/llvm-project-14.0.6.src/build/bin/../lib/libLLVMCore.so.14+0x24696f)
 #6 0x00007f7589e95eac llvm::legacy::FunctionPassManagerImpl::run(llvm::Function&) (/home/ubuntu-18/llvm-project-14.0.6.src/build/bin/../lib/libLLVMCore.so.14+0x246eac)
 #7 0x00007f7589e962cc llvm::legacy::FunctionPassManager::run(llvm::Function&) (/home/ubuntu-18/llvm-project-14.0.6.src/build/bin/../lib/libLLVMCore.so.14+0x2472cc)
 #8 0x00007f758f3617e5 (anonymous namespace)::EmitAssemblyHelper::EmitAssemblyWithLegacyPassManager(clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream> >) BackendUtil.cpp:0:0
 #9 0x00007f758f366079 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream> >) (/home/ubuntu-18/llvm-project-14.0.6.src/build/bin/../lib/libclangCodeGen.so.14+0xfd079)
#10 0x00007f758f71adc2 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/home/ubuntu-18/llvm-project-14.0.6.src/build/bin/../lib/libclangCodeGen.so.14+0x4b1dc2)
#11 0x00007f7582b93e11 clang::ParseAST(clang::Sema&, bool, bool) (/home/ubuntu-18/llvm-project-14.0.6.src/build/bin/../lib/../lib/libclangParse.so.14+0x35e11)
#12 0x00007f758f719b28 clang::CodeGenAction::ExecuteAction() (/home/ubuntu-18/llvm-project-14.0.6.src/build/bin/../lib/libclangCodeGen.so.14+0x4b0b28)
#13 0x00007f758d2a8a99 clang::FrontendAction::Execute() (/home/ubuntu-18/llvm-project-14.0.6.src/build/bin/../lib/libclangFrontend.so.14+0x15ea99)
#14 0x00007f758d20c79a clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/home/ubuntu-18/llvm-project-14.0.6.src/build/bin/../lib/libclangFrontend.so.14+0xc279a)
#15 0x00007f7590321d11 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/home/ubuntu-18/llvm-project-14.0.6.src/build/bin/../lib/libclangFrontendTool.so.14+0x4d11)
#16 0x0000557aca6b4605 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/home/ubuntu-18/llvm-project-14.0.6.src/build/bin/clang-14+0x15605)
#17 0x0000557aca6b1389 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#18 0x0000557aca6ac65b main (/home/ubuntu-18/llvm-project-14.0.6.src/build/bin/clang-14+0xd65b)
#19 0x00007f75884adc87 __libc_start_main /build/glibc-CVJwZb/glibc-2.27/csu/../csu/libc-start.c:344:0
#20 0x0000557aca6ae81a _start (/home/ubuntu-18/llvm-project-14.0.6.src/build/bin/clang-14+0xf81a)
clang-14: error: unable to execute command: Segmentation fault (core dumped)
clang-14: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 14.0.6
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/ubuntu-18/llvm-project-14.0.6.src/build/bin
clang-14: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang-14: note: diagnostic msg: /tmp/hello-557f1e.cpp
clang-14: note: diagnostic msg: /tmp/hello-557f1e.sh
clang-14: note: diagnostic msg: 

Here is my pass
Also available at github
CMakeList

You are most likely trying to create a pointertype with incorrect parameters (pointerType::get()).
Nothing you shouldn’t be able to debug with a debugger or some print outs.

  1. Debbug. Build in debug mode, fire up the debugger, run the program until it crashes. You will have to walk up the call stack till you get to your code
  2. Add some dump calls to the type parameter and what else might be intersting to verify