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>
On 5/13/14, 3:56 AM, Qiuping Yi wrote:> 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:Just three notes: 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. 2) It may be better make your class derive from the InstVisitor class. 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).> > 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*'.A "void *" in the LLVM IR is a pointer to an integer of size 1 (i.e., a char *).> 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.You can use the DataLayout analysis pass to find the size of various types. You can read the doxygen documentation on it at http://llvm.org/doxygen/classllvm_1_1DataLayout.html. Regards, John Criswell -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140513/d0c86e89/attachment.html>
Hi Qiuping, IIRC, there's a wrapper API for easy use of all type definitions; however I cannot remember exactly now. For a void* type, is the size i8? I don't think you can get the size of *p though since this formal parameter of a function in C is no more than an address. Another tip: you can try some C++11 features(such as range-based for loops) in order to avoid "ugly code", or customize some macros yourself. Thanks and Regards, Hongxu On Tue, May 13, 2014 at 5:01 PM, Qiuping Yi-2 [via LLVM] < ml-node+s1065342n68544h55 at n5.nabble.com> wrote:> 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 > > _______________________________________________ > LLVM Developers mailing list > [hidden email] <http://user/SendEmail.jtp?type=node&node=68544&i=0> > http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > > > ------------------------------ > If you reply to this email, your message will be added to the discussion > below: > http://llvm.1065342.n5.nabble.com/Problems-in-instrumentation-tp68544.html > To start a new topic under LLVM - Dev, email > ml-node+s1065342n3h84 at n5.nabble.com > To unsubscribe from LLVM, click here<http://llvm.1065342.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=2&code=bGVmdGNvcHkuY2h4QGdtYWlsLmNvbXwyfC0xMjc2Njc5OTI2> > . > NAML<http://llvm.1065342.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> >-- View this message in context: http://llvm.1065342.n5.nabble.com/Problems-in-instrumentation-tp68544p68554.html Sent from the LLVM - Dev mailing list archive at Nabble.com. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140513/a2f6b785/attachment.html>
Hi, John Criswell Thank you for your detail notes, which give me much invaluable information. -------------------------------------------- Qiuping Yi Institute Of Software Chinese Academy of Sciences On Tue, May 13, 2014 at 9:40 PM, John Criswell <criswell at illinois.edu>wrote:> On 5/13/14, 3:56 AM, Qiuping Yi wrote: > > 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: > > > Just three notes: > > 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. > > 2) It may be better make your class derive from the InstVisitor class. > > 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). > > > > 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*'. > > > A "void *" in the LLVM IR is a pointer to an integer of size 1 (i.e., a > char *). > > > 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. > > > You can use the DataLayout analysis pass to find the size of various > types. You can read the doxygen documentation on it at > http://llvm.org/doxygen/classllvm_1_1DataLayout.html. > > Regards, > > John Criswell > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140514/29386b17/attachment.html>
Hi, Hongxu Thank you for your advice. Now I resolve to the DataLayout (TargetData in lower llvm version) analysis pass to get the size of each type. Best Regards! Qiuping 2014-05-14 1:01 GMT+08:00 Hongxu Chen <leftcopy.chx at gmail.com>:> Hi Qiuping, > > IIRC, there's a wrapper API for easy use of all type definitions; > however I cannot remember exactly now. > For a void* type, is the size i8? I don't think you can get the size > of *p though since this formal parameter of a function in C is no more than > an address. > Another tip: you can try some C++11 features(such as range-based for > loops) in order to avoid "ugly code", or customize some macros yourself. > > > Thanks and Regards, > Hongxu > > > On Tue, May 13, 2014 at 5:01 PM, Qiuping Yi-2 [via LLVM] <[hidden email]<http://user/SendEmail.jtp?type=node&node=68554&i=0> > > wrote: > >> 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 >> >> _______________________________________________ >> LLVM Developers mailing list >> [hidden email] <http://user/SendEmail.jtp?type=node&node=68544&i=0> >> http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >> >> >> ------------------------------ >> If you reply to this email, your message will be added to the >> discussion below: >> http://llvm.1065342.n5.nabble.com/Problems-in-instrumentation-tp68544.html >> To start a new topic under LLVM - Dev, email [hidden email]<http://user/SendEmail.jtp?type=node&node=68554&i=1> >> To unsubscribe from LLVM, click here. >> NAML<http://llvm.1065342.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> >> > > > ------------------------------ > View this message in context: Re: Problems in instrumentation<http://llvm.1065342.n5.nabble.com/Problems-in-instrumentation-tp68544p68554.html> > Sent from the LLVM - Dev mailing list archive<http://llvm.1065342.n5.nabble.com/LLVM-Dev-f3.html>at Nabble.com. > > _______________________________________________ > 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/20140514/2641fa2f/attachment.html>
Reasonably Related Threads
- [LLVMdev] internal compiler error when compiling llvm-gcc-4.2-2.9
- [LLVMdev] a bug in Kaleidoscope code
- [LLVMdev] internal compiler error when compiling llvm-gcc-4.2-2.9
- [LLVMdev] Add call printf instructions problems
- [LLVMdev] Add call printf instructions problems