Debug LLVM pass seg fault

I am writing a toy compiler based on a tutorial on the LLVM website , but now I get an error message when generating code (I believe). The program crashed with a seg error in the llvm::TypeFinder::incorporateValue method, and I can not understand what is happening.

This is the output from LLDB when a failure occurs.

 * thread #1: tid = 0x2dfc, 0x0000000000a2a2d0 wtf`llvm::TypeFinder::incorporateValue(llvm::Value const*) + 16, stop reason = invalid address frame #0: 0x0000000000a2a2d0 wtf`llvm::TypeFinder::incorporateValue(llvm::Value const*) + 16 wtf`llvm::TypeFinder::incorporateValue(llvm::Value const*) + 16: -> 0xa2a2d0: movb 8(%r14), %al 0xa2a2d4: cmpb $17, %al 0xa2a2d6: jne 0xa2a332 ; llvm::TypeFinder::incorporateValue(llvm::Value const*) + 114 0xa2a2d8: movq %r14, (%rsp) 

This happens when I try to reset the IR code of a function, when verifyFunction is running on it, or when the FunctionPassManager tries to optimize it. When FPM works, it fails with

 Stack dump: 0. Running pass 'Module Verifier' on function '@foo' [1] 11485 segmentation fault (core dumped) bin/wtf examples/simple.wtf 

And this is the code that generates the error

 Function *Codegen::Generate(FunctionAST *funcAst) { NamedValues.clear(); Function *func = this->Generate(funcAst->GetPrototype()); if (func == 0) return 0; BasicBlock *block = BasicBlock::Create(getGlobalContext(), "entry", func); Builder.SetInsertPoint(block); // iterate and codegen all expressions in body Value *retVal = this->Generate(funcAst->GetBody()); if (retVal) { Builder.CreateRet(retVal); verifyFunction(*func); TheFPM->run(*func); // <----- crash happens here return func; } func->eraseFromParent(); return 0; }; 

Iโ€™m either looking for the cause of the failure, or somehow narrowing it down , because going through LLDB doesnโ€™t help me much (maybe because I donโ€™t understand the inside of LLVM)

This is backtrace when LLDB fails on verifyFunction :

 frame #0: 0x0000000000a2a225 wtf`llvm::TypeFinder::incorporateType(llvm::Type*) + 53 frame #1: 0x0000000000a29e1b wtf`llvm::TypeFinder::run(llvm::Module const&, bool) + 91 frame #2: 0x000000000098d91b wtf`(anonymous namespace)::TypePrinting::incorporateTypes(llvm::Module const&) + 27 frame #3: 0x00000000009907f5 wtf`llvm::Value::print(llvm::raw_ostream&, llvm::AssemblyAnnotationWriter*) const + 2421 frame #4: 0x0000000000a316cf wtf`(anonymous namespace)::Verifier::CheckFailed(llvm::Twine const&, llvm::Value const*, llvm::Type*, llvm::Value const*) + 143 frame #5: 0x0000000000a30ca5 wtf`(anonymous namespace)::Verifier::visitFunction(llvm::Function&) + 421 frame #6: 0x0000000000a2fd84 wtf`(anonymous namespace)::Verifier::runOnFunction(llvm::Function&) + 132 frame #7: 0x0000000000a1d99e wtf`llvm::FPPassManager::runOnFunction(llvm::Function&) + 318 frame #8: 0x0000000000a1d243 wtf`llvm::FunctionPassManagerImpl::run(llvm::Function&) + 195 frame #9: 0x0000000000a1d11d wtf`llvm::FunctionPassManager::run(llvm::Function&) + 93 frame #10: 0x0000000000a2f98a wtf`llvm::verifyFunction(llvm::Function const&, llvm::VerifierFailureAction) + 538 frame #11: 0x00000000004cef02 wtf`Codegen::Generate(this=<unavailable>, funcAst=<unavailable>) + 226 at Codegen.cpp:325 frame #12: 0x00000000004da96b wtf`Driver::Go(std::string) [inlined] Driver::HandleDefinition(this=<unavailable>) + 27 at Driver.cpp:5 frame #13: 0x00000000004da950 wtf`Driver::Go(this=<unavailable>, file=<unavailable>) + 208 at Driver.cpp:61 frame #14: 0x00000000004db71c wtf`main(argc=<unavailable>, argv=<unavailable>) + 364 at main.cpp:30 frame #15: 0x00007ff648bb476d libc.so.6`__libc_start_main + 237 frame #16: 0x00000000004cb9bd wtf`_start + 41 
+4
source share

Source: https://habr.com/ru/post/1490067/


All Articles