A memory error was encountered executing np in the main function.
Below is my code, I am sure that the pass in the runTimer function is being executed properly, but on return to the main function is a memory error. And the memory error is different for different IR files (sometimes malloc(): unsorted double linked list corrupted
, sometimes Segmentation fault (core dumped)
).
extern "C" void runTimer(llvm::Module &M){
llvm::legacy::PassManager lpm;
lpm.add(llvm::createLowerSwitchPass());
lpm.run(M);
PassBuilder PB;
FunctionPassManager FPM;
ModulePassManager MPM;
ModuleAnalysisManager MAM;
FunctionAnalysisManager FAM;
CGSCCAnalysisManager CGAM;
LoopAnalysisManager LAM;
// Register analysis passes with the managers
PB.registerModuleAnalyses(MAM);
PB.registerFunctionAnalyses(FAM);
PB.registerCGSCCAnalyses(CGAM);
PB.registerLoopAnalyses(LAM);
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
FPM.addPass(timer::LowerSelect());
FPM.addPass(timer::BasicBlockTimer());
MPM.addPass(llvm::createModuleToFunctionPassAdaptor(std::move(FPM)));
MPM.run(M, MAM);
}
int main(int argc, char **argv) {
// Initialize LLVM
InitLLVM X(argc, argv);
cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input IR file>"), cl::init("-"));
cl::ParseCommandLineOptions(argc, argv, "LLVM IR File Reader\n");
LLVMContext Context;
SMDiagnostic Err;
std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
if (!M) {
WithColor::error(errs(), argv[0]) << "Error reading IR file: " << InputFilename << "\n";
Err.print(argv[0], errs());
return 1;
}
runTimer(*M);
std::cout << "Timer pass finished!" << std::endl;
return 0;
}
Below is the bt from my gdb output:
Program received signal SIGSEGV, Segmentation fault.
0x000000000000e391 in ?? ()
(gdb) bt
#0 0x000000000000e391 in ?? ()
#1 0x0000555555bc6d75 in llvm::detail::AnalysisResultModel<llvm::Module, llvm::InnerAnalysisManagerProxy<llvm::AnalysisManager<llvm::Function>, llvm::Module>, llvm::InnerAnalysisManagerProxy<llvm::AnalysisManager<llvm::Function>, llvm::Module>::Result, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator, true>::~AnalysisResultModel() ()
#2 0x00005555556ba1b5 in std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> >::operator() (
__ptr=<optimized out>, this=<optimized out>) at /usr/include/c++/12/bits/unique_ptr.h:89
#3 std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > >::~unique_ptr (this=0x5555576de148, __in_chrg=<optimized out>) at /usr/include/c++/12/bits/unique_ptr.h:396
#4 std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >::~pair (this=0x5555576de140, __in_chrg=<optimized out>)
at /usr/include/c++/12/bits/stl_pair.h:185
#5 std::__new_allocator<std::_List_node<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > >::destroy<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > (__p=0x5555576de140, this=0x5555577da548) at /usr/include/c++/12/bits/new_allocator.h:181
#6 std::allocator_traits<std::allocator<std::_List_node<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > > >::destroy<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > (__p=0x5555576de140, __a=...) at /usr/include/c++/12/bits/alloc_traits.h:535
#7 std::__cxx11::_List_base<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > >::_M_clear (this=0x5555577da548) at /usr/include/c++/12/bits/list.tcc:77
#8 std::__cxx11::_List_base<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > >::~_List_base (this=0x5555577da548, __in_chrg=<optimized out>) at /usr/include/c++/12/bits/stl_list.h:575
#9 std::__cxx11::list<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > >::~list (this=0x5555577da548, __in_chrg=<optimized out>) at /usr/include/c++/12/bits/stl_list.h:903
#10 llvm::DenseMapBase<llvm::DenseMap<llvm::Module*, std::__cxx11::list<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > >, llvm::DenseMapInfo<llvm::Module*, void>, llvm::detail::DenseMapPair<llvm::Module*, std::__cxx11::list<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > > > >, llvm::Module*, std::__cxx11::list<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > >, llvm::DenseMapInfo<llvm::Module*, void>, llvm::detail::DenseMapPair<llvm::Module*, std::__cxx11::list<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > > > >::destroyAll (this=0x7fffffffca08)
--Type <RET> for more, q to quit, c to continue without paging--
at /usr/local/include/llvm/ADT/DenseMap.h:368
#11 llvm::DenseMap<llvm::Module*, std::__cxx11::list<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > >, llvm::DenseMapInfo<llvm::Module*, void>, llvm::detail::DenseMapPair<llvm::Module*, std::__cxx11::list<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > > > >::~DenseMap (this=0x7fffffffca08, __in_chrg=<optimized out>) at /usr/local/include/llvm/ADT/DenseMap.h:754
#12 llvm::AnalysisManager<llvm::Module>::~AnalysisManager (this=0x7fffffffc9f0, __in_chrg=<optimized out>) at /usr/local/include/llvm/IR/PassManager.h:620
#13 runTimer (M=...) at /home/lqs66/Desktop/model-checking-flight-control/TimeConstraints/src/timer.cpp:52
#14 0x0000555555647a5d in main (argc=<optimized out>, argv=<optimized out>) at /home/lqs66/Desktop/model-checking-flight-control/TimeConstraints/src/timer.cpp:78
This error occurs when I use llvm::createModuleToFunctionPassAdaptor
, without llvm::createModuleToFunctionPassAdaptor
everything works fine.