[LLVMdev] Problems in instrumentation

Qiuping Yi yiqiuping at gmail.com
Tue May 13 01:56:51 PDT 2014


Hi everyone,

I have some trouble in instrumenting load instructions. I want to
instrument load instructions as follow: Firstly, I judge whether the loaded
pointer(*any type is possible*) is NULL. If so, I want to explicitly
allocate the corresponding address space of its type to the pointer.

For example, in source code level I want to translate the next statement

*p = 1;

into the next statements

if (p == NULL)
    *p = malloc(sizeof(*p));
*p = 1;

For simplicity, I want to wrapper the first two statements into function
init. And then I can implement as follow:

init((void*)p, sizeof(*p));
*p = 1;

where

void init(void *p, int size) {
     if (p == NULL)
         p = malloc(size);
}

I am trying to use the next pass for instrumentation:

for (Module::iterator f=M.begin(), fe=M.end(); f!=fe; ++f) {
   for (Function::iterator b=f->begin(), be=f->end(); b!=be; ++b) {
      for (BasicBlock::iterator i=b->begin(), ie=b->end()l i!=ie; ++i)  {
          if (i->getOpcode() == Instruction::Load) {

*             CallInst::create(....); // add a call inst before inst i to
invoke function init*


          }
      }
   }
}

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*'.
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.

Any Suggestions are welcome. Thank you all in advance.

Best Regards!

--------------------------------------------
Qiuping Yi
Institute Of Software
Chinese Academy of Sciences
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140513/77fcf8ac/attachment.html>


More information about the llvm-dev mailing list