[LLVMdev] LLVM segmentation fault / need use Instruction instead of Instruction*
criswell at illinois.edu
Wed Dec 19 08:32:01 PST 2012
On 12/19/12 10:19 AM, Alexandru Ionut Diaconescu wrote:
> Hello everyone,
> I have a segmentation fault while running an LLVM pass. I need to use
> BBterminators array outside the iterating "for" loop for basic blocks.
> It seems that LLVM does not protect the addresses ( note:
> TerminatorInst *BasicBlock::getTerminator() ) when iterating through
> the loop, so I need to keep in BBterminators "Instruction" type
> elements, not "Instruction* ". How can I copy entire Instructions into
1) Make sure that the rest of the code isn't inserting or remove
instructions or basic blocks as you iterate. That can invalidate the
iterators. Using invalidated iterators can cause a segmentation fault.
2) Make sure that you're not writing past the end of BBterminators. My
recommendation is to either allocate BBterminators with sufficient size
(you can find the number of basic blocks and then use that to allocate
the size) or to use a dynamically sized container (std::vector,
std::set, or one of the LLVM classes that provide a more efficient
implementation of these).
3) Make sure you're not leaking memory by not freeing BBterminators. If
the call to new returns a NULL pointer due to memory exhaustion, that
could cause the segfault.
4) You may just have to roll up your sleeves and figure out what is
causing the invalid memory reference. Tools like Address Sanitizer and
SAFECode may help you narrow down the problem. Clang's static analyzer
may help, too.
> for (Function::iterator II = F.begin(), EE = F.end(); II != EE;
> ++II, ++ii)
> ....... // not relevant code ;
> BasicBlock* BB=(dyn_cast<BasicBlock>(II));
This above line shouldn't require a dyn_cast. You should be able to use
BasicBlock * BB = *I;
BasicBlock * BB = I;
If you're going to use a cast, use cast<> instead of dyn_cast<>. If the
cast fails, you'll get an assertion if asserts are enabled. You should
never get anything other than a BasicBlock when iterating through a
-- John T.
> if (BB->getTerminator())
> Instruction* current = BB->getTerminator();
> Instruction* previous = current->getPrevNode();
> if (current->getOpcode()==Instruction::Br)
> ...// not relevant code
> where Instruction** BBterminators = new Instruction*;
> Thank you a lot !
> Best regards,
> Alexandru Ionut Diaconescu
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the llvm-dev