<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 12/19/12 10:19 AM, Alexandru Ionut
Diaconescu wrote:<br>
</div>
<blockquote
cite="mid:CAPBXs301uphkZJ=s2n3e5V4AeUn4q=j3MoS1UAkFEdnS2vtfag@mail.gmail.com"
type="cite">
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
Hello everyone,<br>
<br>
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?<br>
</blockquote>
<br>
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.<br>
<br>
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).<br>
<br>
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.<br>
<br>
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.<br>
<br>
<br>
<blockquote
cite="mid:CAPBXs301uphkZJ=s2n3e5V4AeUn4q=j3MoS1UAkFEdnS2vtfag@mail.gmail.com"
type="cite">
<br>
<br>
for (Function::iterator II = F.begin(), EE = F.end(); II !=
EE; ++II, ++ii)<br>
{ <br>
....... // not relevant code ; <br>
<br>
BasicBlock* BB=(dyn_cast<BasicBlock>(II));<br>
</blockquote>
<br>
This above line shouldn't require a dyn_cast. You should be able to
use something like:<br>
<br>
BasicBlock * BB = *I;<br>
<br>
or <br>
<br>
BasicBlock * BB = I;<br>
<br>
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.<br>
<br>
-- John T.<br>
<br>
<br>
<blockquote
cite="mid:CAPBXs301uphkZJ=s2n3e5V4AeUn4q=j3MoS1UAkFEdnS2vtfag@mail.gmail.com"
type="cite"><br>
if (BB->getTerminator())<br>
{<br>
Instruction* current = BB->getTerminator();<br>
<br>
Instruction* previous = current->getPrevNode();<br>
<br>
if (current->getOpcode()==Instruction::Br) <br>
{<br>
<br>
BBterminators[ii]=current;<br>
...// not relevant code<br>
<br>
where Instruction** BBterminators = new Instruction*[100];<br>
<br>
Thank you a lot !<br>
Alex<br>
<br clear="all">
<br>
-- <br>
<font
style="background-color:rgb(255,255,255);color:rgb(153,153,153)">Best
regards,</font><br
style="background-color:rgb(255,255,255);color:rgb(153,153,153)">
<font
style="background-color:rgb(255,255,255);color:rgb(153,153,153)">Alexandru
Ionut Diaconescu</font><br>
<br>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a> <a class="moz-txt-link-freetext" href="http://llvm.cs.uiuc.edu">http://llvm.cs.uiuc.edu</a>
<a class="moz-txt-link-freetext" href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a>
</pre>
</blockquote>
<br>
</body>
</html>