Alexandru Ionut Diaconescu
2012-Dec-19 16:19 UTC
[LLVMdev] LLVM segmentation fault / need use Instruction instead of Instruction*
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 BBterminators? for (Function::iterator II = F.begin(), EE = F.end(); II != EE; ++II, ++ii) { ....... // not relevant code ; BasicBlock* BB=(dyn_cast<BasicBlock>(II)); if (BB->getTerminator()) { Instruction* current = BB->getTerminator(); Instruction* previous = current->getPrevNode(); if (current->getOpcode()==Instruction::Br) { BBterminators[ii]=current; ...// not relevant code where Instruction** BBterminators = new Instruction*[100]; Thank you a lot ! Alex -- Best regards, Alexandru Ionut Diaconescu -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20121219/99ccf5e4/attachment.html>
John Criswell
2012-Dec-19 16:32 UTC
[LLVMdev] LLVM segmentation fault / need use Instruction instead of Instruction*
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 > BBterminators?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 something like: BasicBlock * BB = *I; or 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 function. -- John T.> > if (BB->getTerminator()) > { > Instruction* current = BB->getTerminator(); > > Instruction* previous = current->getPrevNode(); > > if (current->getOpcode()==Instruction::Br) > { > > BBterminators[ii]=current; > ...// not relevant code > > where Instruction** BBterminators = new Instruction*[100]; > > Thank you a lot ! > Alex > > > -- > Best regards, > Alexandru Ionut Diaconescu > > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20121219/5c43f15b/attachment.html>
Alexandru Ionut Diaconescu
2012-Dec-20 10:39 UTC
[LLVMdev] LLVM segmentation fault / need use Instruction instead of Instruction*
Hello John, I was following your procedures and I isolated the problem. The problem are represented by the basic blocks with only one elements. for (Function::iterator II = F.begin(), EE = F.end(); II != EE; ++II, ++ii) { BasicBlock* BB=II; if (BB->getTerminator()) { Instruction* current = BB->getTerminator(); Instruction* previous; errs()<<"AAA\n"; if(current->getPrevNode()) { errs()<<"BBB\n"; previous = current->getPrevNode(); ok=1; } if (ok) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20121220/41659d7e/attachment.html>
Alexandru Ionut Diaconescu
2012-Dec-20 10:42 UTC
[LLVMdev] LLVM segmentation fault / need use Instruction instead of Instruction*
Hello John, I was following your procedures and I isolated the problem. The problem are represented by the basic blocks with only one element. for (Function::iterator II = F.begin(), EE = F.end(); II != EE; ++II, ++ii) { BasicBlock* BB=II; if (BB->getTerminator()) { Instruction* current = BB->getTerminator(); Instruction* previous; errs()<<"AAA\n"; if(*current->getPrevNode()*) { errs()<<"BBB\n"; previous = current->getPrevNode(); ok=1; } if (ok){ errs()<<"CCC\n"; ........ It does print AAA, but then I have the segfault. So when I am evaluating the *current->getPrevNode() *condition, I got the segfault. Do you know how can I solve this? Thank you Alex -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20121220/feea7b5a/attachment.html>
Reasonably Related Threads
- [LLVMdev] LLVM segmentation fault / need use Instruction instead of Instruction*
- [LLVMdev] LLVM segmentation fault / need use Instruction instead of Instruction*
- [LLVMdev] LLVM segmentation fault / need use Instruction instead of Instruction*
- [LLVMdev] LLVM segmentation fault / need use Instruction instead of Instruction*
- [LLVMdev] LLVM segmentation fault / need use Instruction instead of Instruction*