<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 5/13/14, 3:56 AM, Qiuping Yi wrote:<br>
    </div>
    <blockquote
cite="mid:CAJSQ9qfrygV-e4SSpd3ZLBo7rq1p4+SF2zZh9S2ECzdmavJ7+g@mail.gmail.com"
      type="cite">
      <meta http-equiv="Content-Type" content="text/html;
        charset=ISO-8859-1">
      <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>
    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).<br>
    <br>
    <blockquote
cite="mid:CAJSQ9qfrygV-e4SSpd3ZLBo7rq1p4+SF2zZh9S2ECzdmavJ7+g@mail.gmail.com"
      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>
    A "void *" in the LLVM IR is a pointer to an integer of size 1
    (i.e., a char *).<br>
    <br>
    <blockquote
cite="mid:CAJSQ9qfrygV-e4SSpd3ZLBo7rq1p4+SF2zZh9S2ECzdmavJ7+g@mail.gmail.com"
      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>
    You can use the DataLayout analysis pass to find the size of various
    types.  You can read the doxygen documentation on it at
    <a class="moz-txt-link-freetext" href="http://llvm.org/doxygen/classllvm_1_1DataLayout.html">http://llvm.org/doxygen/classllvm_1_1DataLayout.html</a>.<br>
    <br>
    Regards,<br>
    <br>
    John Criswell<br>
    <br>
  </body>
</html>