<div dir="ltr">Hi, <span style="font-family:arial,sans-serif;font-size:13px">John Criswell</span><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><font face="arial, sans-serif">Thank you for your detail notes, which give me much invaluable information. </font></div>
</div><div class="gmail_extra"><br clear="all"><div><br>--------------------------------------------<br>Qiuping Yi<br>Institute Of Software<br>Chinese Academy of Sciences</div>
<br><br><div class="gmail_quote">On Tue, May 13, 2014 at 9:40 PM, John Criswell <span dir="ltr"><<a href="mailto:criswell@illinois.edu" target="_blank">criswell@illinois.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000"><div class="">
<div>On 5/13/14, 3:56 AM, Qiuping Yi wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<div>Hi everyone, </div>
<div><br>
</div>
<div>I have some trouble in instrumenting load instructions. I
want to instrument load instructions as follow: Firstly, I
judge whether the loaded pointer(<b>any type is possible</b>)
is NULL. If so, I want to explicitly allocate the
corresponding address space of its type to the pointer. </div>
<div><br>
</div>
<div>For example, in source code level I want to translate the
next statement </div>
<div><br>
</div>
<div>*p = 1; </div>
<div><br>
</div>
<div>into the next statements</div>
<div><br>
</div>
<div>if (p == NULL) </div>
<div> *p = malloc(sizeof(*p));</div>
<div>*p = 1;</div>
<div><br>
</div>
<div>For simplicity, I want to wrapper the first two statements
into function init. And then I can implement as follow:</div>
<div><br>
</div>
<div>init((void*)p, sizeof(*p));</div>
<div>*p = 1;</div>
<div><br>
</div>
<div>where</div>
<div><br>
</div>
<div>void init(void *p, int size) {</div>
<div> if (p == NULL)</div>
<div> p = malloc(size);</div>
<div>}</div>
<br clear="all">
<div>I am trying to use the next pass for instrumentation:</div>
</div>
</blockquote>
<br></div>
Just three notes:<br>
<br>
1) You may want to ensure that you're not invalidating the
BasicBlock::iterator variable i by inserting the Call instruction.
If you are invalidating the iterator, then your code may skip over
load instructions or instrument a single load twice.<br>
<br>
2) It may be better make your class derive from the InstVisitor
class.<br>
<br>
3) You may want to instrument atomic operations as well as the Load
Instruction. This is because the atomic operations also perform a
load (as well as a store). The same applies to some of the
intrinsics (e.g., the memcpy/memcmp intrinsics).<div class=""><br>
<br>
<blockquote type="cite">
<div dir="ltr">
<div><br>
</div>
<div>for (Module::iterator f=M.begin(), fe=M.end(); f!=fe; ++f)
{</div>
<div> for (Function::iterator b=f->begin(), be=f->end();
b!=be; ++b) {</div>
<div> for (BasicBlock::iterator i=b->begin(),
ie=b->end()l i!=ie; ++i) {</div>
<div> if (i->getOpcode() == Instruction::Load) {</div>
<div> </div>
<div><b> CallInst::create(....); // add a call inst
before inst i to invoke function init</b></div>
<div><br>
</div>
<div>
<br>
</div>
<div> }</div>
<div> }</div>
<div> }</div>
<div>}</div>
<div><br>
</div>
<div>So my question is How should I create the previous call
inst to execute invocation: init((void*)p, sizeof(p)). Because
any pointer type is possible, so I let the first parameter of
function init as 'void*'. </div>
</div>
</blockquote>
<br></div>
A "void *" in the LLVM IR is a pointer to an integer of size 1
(i.e., a char *).<div class=""><br>
<br>
<blockquote type="cite">
<div dir="ltr">
<div>Furthermore, how should I get the size of *p? I check
Type.h, and found class Type only provide function
getPrimitiveSizeInBits() to return the size of the primitive
types. How can I know the size of other types, eg. the size of
a structure type.</div>
</div>
</blockquote>
<br></div>
You can use the DataLayout analysis pass to find the size of various
types. You can read the doxygen documentation on it at
<a href="http://llvm.org/doxygen/classllvm_1_1DataLayout.html" target="_blank">http://llvm.org/doxygen/classllvm_1_1DataLayout.html</a>.<br>
<br>
Regards,<br>
<br>
John Criswell<br>
<br>
</div>
</blockquote></div><br></div>