Error while iterating over callee nodes in CallGraph

Hello,

I’ve written a Module Pass to iterate over the CallGraph using the CallGraph Pass. Here is the code for getAnalysisUsage and runOnModule methods:

void getAnalysisUsage(AnalysisUsage &AU) const {

AU.addRequired();

AU.addPreserved();

}

bool runOnModule(Module &M) override {

CallGraph &cg = getAnalysis();

CallGraphNode *cgm = cg[M.getFunction(“main”)];

for (CallGraphNode::iterator ti = cgm->begin(); ti != cgm->end(); ++ti)

{

CallGraphNode * cgn = ti->second;

if(cgn==NULL)

continue;

errs()<getFunction()<<"\n";

errs()<getFunction()->getName()<<"\n";

}

return false;

}

I’m using llvm3.4.2 to compile and run the pass. I get the following error while iterating through callee nodes of main function:

0 opt 0x00000000011d2302 llvm::sys::PrintStackTrace(_IO_FILE*) + 34

1 opt 0x00000000011d1eb4

2 libpthread.so.0 0x00007f88d72d3330

3 libcgwalk.so 0x00007f88d64d6596

4 opt 0x00000000010ddb45 llvm::legacy::PassManagerImpl::run(llvm::Module&) + 709

5 opt 0x0000000000552724 main + 2452

6 libc.so.6 0x00007f88d66fbf45 __libc_start_main + 245

7 opt 0x000000000056d865

Stack dump:

  1. Program arguments: opt -load build/proj/libcgwalk.so -cgwalk
  1. Running pass ‘Cgwalk’ on module ‘’.

Segmentation fault (core dumped)

I’m running the pass on bitcode of a simple C program where main calls 2 other functions.

I think the function (cgn->getFunction()) can be NULL (to indicate
special edges). Make sure it isn't.