15102925731
2012-Apr-09 07:50 UTC
[LLVMdev] How to instrument a this function using insertBefore instruction???
Hi all, Im trying to instrument this hello function right before the instruction that call the "puts" function(the source code is as follow). Now I can compile the pass without errors, but when run the pass with opt tool, it broke down. The diagnose is something like Referencing function in another module! %CallCheck = call i32 @fib() Broken module found, compilation aborted! Does it mean I fail to wrap the function into a module?? How to actually insert the the "hello function" before the calling instruction ?? Im waiting for your help. Thank you!! //******=========================================================================================================================================******// //******=========================================================================================================================================******//// FPSFI: a Function Pass Based Idea for Software Fault Isolation // // //This file involves the main work of SFI. It will source the certain point in any programme(mainly modules in our context)and insert our manully made// //check function to provide API integrity. Thanks to the pass mechanism that LLVM provide, we can wrap our fix and optimization idea into a function pass // and act it on every function the clang has analysed. // //******=========================================================================================================================================******// //******=========================================================================================================================================******// #include "llvm/Pass.h" #include "llvm/Function.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Instruction.h" #include "llvm/Transforms/Utils/UnrollLoop.h" #include "llvm/BasicBlock.h" #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/LoopIterator.h" #include "llvm/Analysis/LoopPass.h" #include "llvm/Analysis/ScalarEvolution.h" #include "llvm/Analysis/ScalarEvolutionExpander.h" #include "llvm/Support/Debug.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/Cloning.h" #include "llvm/Type.h" #include "llvm/LLVMContext.h" #include "llvm/Support/Casting.h" #include "stdio.h" #include "llvm/Module.h" using namespace llvm; int check() { printf("Hello me!!\n"); return 0; } Module * M; LLVMContext Context; FunctionType *STy=FunctionType::get(Type::getInt32Ty(Context), false); Function *check = Function::Create(STy, Function::InternalLinkage, "check" ,&M); CallInst *callcheck = CallInst::Create(FibF,"CallCheck"); namespace { struct Hello : public FunctionPass { static char ID; Hello() : FunctionPass(ID) {} virtual bool runOnFunction(Function &F) { errs() << "Hello: "; errs().write_escaped(F.getName()) <<'\n'; // run through all the instruction and convert all the callinst to ... for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) { for(BasicBlock::iterator II = BI->begin(),IE = BI->end();II != IE; ++II) { // errs() <<"between instructions! \n"; // CallInst * III = CallInst::Create(&F,"InsttoCallInst",II); if(CallInst * III = dyn_cast<CallInst>(II)) { if(III->getCalledFunction()!=NULL&&III->getCalledFunction()->getName()=="puts") { errs() <<III->getCalledFunction()->getName()<<" function found!\n"; callcheck->insertBefore(II); errs() <<"INSERT SUCCEEDED!!!!!!!!\n"; } else { errs() <<"it's not main function!\n"<<"it is:"<<III->getCalledFunction()->getName()<<'\n'; } }/**/ } } } return true; } }; } char Hello::ID = 0; static RegisterPass<Hello> X("hello", "ZHello Korld Pass", false, false); -- 祝好! 甄凯 ------------------------------------------------------------------------------------------------------ 2012-04-09 ------------------------------------------------------------------------------------------------------ Name: 甄凯(ZhenKai) Homepage:http://www.renren.com/262729393 Email: zhenkaixd at 126.com or 846227103 at qq.com TEL: 15810729006(Beijing) Address: Room I-406, Central Building, Tsinghua University, Beijing, China. 100084. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120409/234ea1d0/attachment.html>
15102925731
2012-Apr-09 08:53 UTC
[LLVMdev] How to instrument a this function using insertBefore instruction???
Hi, I got upset.. What does “Broken module found, compilation aborted!” mean really?? what‘s “broken module“?? -- 祝好! 甄凯 ------------------------------------------------------------------------------------------------------ 2012-04-09 ------------------------------------------------------------------------------------------------------ Name: 甄凯(ZhenKai) Homepage:http://www.renren.com/262729393 Email: zhenkaixd at 126.com or 846227103 at qq.com TEL: 15810729006(Beijing) Address: Room I-406, Central Building, Tsinghua University, Beijing, China. 100084. 在 2012-04-09 15:50:00,15102925731 <zhenkaixd at 126.com> 写道: Hi all, Im trying to instrument this hello function right before the instruction that call the "puts" function(the source code is as follow). Now I can compile the pass without errors, but when run the pass with opt tool, it broke down. The diagnose is something like Referencing function in another module! %CallCheck = call i32 @fib() Broken module found, compilation aborted! Does it mean I fail to wrap the function into a module?? How to actually insert the the "hello function" before the calling instruction ?? Im waiting for your help. Thank you!! //******=========================================================================================================================================******// //******=========================================================================================================================================******//// FPSFI: a Function Pass Based Idea for Software Fault Isolation // // //This file involves the main work of SFI. It will source the certain point in any programme(mainly modules in our context)and insert our manully made// //check function to provide API integrity. Thanks to the pass mechanism that LLVM provide, we can wrap our fix and optimization idea into a function pass // and act it on every function the clang has analysed. // //******=========================================================================================================================================******// //******=========================================================================================================================================******// #include "llvm/Pass.h" #include "llvm/Function.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Instruction.h" #include "llvm/Transforms/Utils/UnrollLoop.h" #include "llvm/BasicBlock.h" #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/LoopIterator.h" #include "llvm/Analysis/LoopPass.h" #include "llvm/Analysis/ScalarEvolution.h" #include "llvm/Analysis/ScalarEvolutionExpander.h" #include "llvm/Support/Debug.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/Cloning.h" #include "llvm/Type.h" #include "llvm/LLVMContext.h" #include "llvm/Support/Casting.h" #include "stdio.h" #include "llvm/Module.h" using namespace llvm; int check() { printf("Hello me!!\n"); return 0; } Module * M; LLVMContext Context; FunctionType *STy=FunctionType::get(Type::getInt32Ty(Context), false); Function *check = Function::Create(STy, Function::InternalLinkage, "check" ,&M); CallInst *callcheck = CallInst::Create(FibF,"CallCheck"); namespace { struct Hello : public FunctionPass { static char ID; Hello() : FunctionPass(ID) {} virtual bool runOnFunction(Function &F) { errs() << "Hello: "; errs().write_escaped(F.getName()) <<'\n'; // run through all the instruction and convert all the callinst to ... for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) { for(BasicBlock::iterator II = BI->begin(),IE = BI->end();II != IE; ++II) { // errs() <<"between instructions! \n"; // CallInst * III = CallInst::Create(&F,"InsttoCallInst",II); if(CallInst * III = dyn_cast<CallInst>(II)) { if(III->getCalledFunction()!=NULL&&III->getCalledFunction()->getName()=="puts") { errs() <<III->getCalledFunction()->getName()<<" function found!\n"; callcheck->insertBefore(II); errs() <<"INSERT SUCCEEDED!!!!!!!!\n"; } else { errs() <<"it's not main function!\n"<<"it is:"<<III->getCalledFunction()->getName()<<'\n'; } }/**/ } } } return true; } }; } char Hello::ID = 0; static RegisterPass<Hello> X("hello", "ZHello Korld Pass", false, false); -- 祝好! 甄凯 ------------------------------------------------------------------------------------------------------ 2012-04-09 ------------------------------------------------------------------------------------------------------ Name: 甄凯(ZhenKai) Homepage:http://www.renren.com/262729393 Email: zhenkaixd at 126.com or 846227103 at qq.com TEL: 15810729006(Beijing) Address: Room I-406, Central Building, Tsinghua University, Beijing, China. 100084. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120409/fcddc5a8/attachment.html>
Joey Gouly
2012-Apr-09 09:16 UTC
[LLVMdev] How to instrument a this function using insertBefore instruction???
Hi, I don't think the code you pasted can be the correct code, where does FibF come from? Anyway, the problem is that you're calling the FibF from Module A, however you defined it for Module B. You need to insert the FibF function into the Module that you're running. To do this override "virtual bool doInitialization(Module &M);" and insert FibF into M. Joey 2012/4/9 15102925731 <zhenkaixd at 126.com>> Hi, > I got upset.. What does “Broken module found, compilation aborted!” mean > really?? what‘s “broken module“?? > > > > -- > 祝好! > > 甄凯 > > ------------------------------------------------------------------------------------------------------ > 2012-04-09 > > ------------------------------------------------------------------------------------------------------ > Name: 甄凯(ZhenKai) > Homepage:http://www.renren.com/262729393 > Email: zhenkaixd at 126.com or 846227103 at qq.com > TEL: 15810729006(Beijing) > Address: Room I-406, Central Building, Tsinghua University, Beijing, > China. 100084. > > 在 2012-04-09 15:50:00,15102925731 <zhenkaixd at 126.com> 写道: > > Hi all, > Im trying to instrument this hello function right before the instruction > that call the "puts" function(the source code is as follow). > > Now I can compile the pass without errors, but when run the pass with opt > tool, it broke down. The diagnose is something like > > Referencing function in another module! > %CallCheck = call i32 @fib() > Broken module found, compilation aborted! > > Does it mean I fail to wrap the function into a module?? How to actually > insert the the "hello function" before the calling instruction ?? Im > waiting for your help. > Thank you!! > > > //******=========================================================================================================================================******// > //******=========================================================================================================================================******//// > FPSFI: a Function Pass Based Idea for Software Fault Isolation > // > // > //This file involves the main work of SFI. It will source the certain > point in any programme(mainly modules in our context)and insert our manully > made// > //check function to provide API integrity. Thanks to the pass mechanism > that LLVM provide, we can wrap our fix and optimization idea into a > function pass > // and act it on every function the clang has analysed. > // > > //******=========================================================================================================================================******// > > //******=========================================================================================================================================******// > #include "llvm/Pass.h" > #include "llvm/Function.h" > #include "llvm/Support/raw_ostream.h" > #include "llvm/Instruction.h" > #include "llvm/Transforms/Utils/UnrollLoop.h" > #include "llvm/BasicBlock.h" > #include "llvm/ADT/Statistic.h" > #include "llvm/Analysis/LoopIterator.h" > #include "llvm/Analysis/LoopPass.h" > #include "llvm/Analysis/ScalarEvolution.h" > #include "llvm/Analysis/ScalarEvolutionExpander.h" > #include "llvm/Support/Debug.h" > #include "llvm/Transforms/Utils/BasicBlockUtils.h" > #include "llvm/Transforms/Utils/Cloning.h" > #include "llvm/Type.h" > #include "llvm/LLVMContext.h" > #include "llvm/Support/Casting.h" > #include "stdio.h" > > #include "llvm/Module.h" > using namespace llvm; > > > int check() > { > printf("Hello me!!\n"); > return 0; > } > > > > Module * M; > LLVMContext Context; > FunctionType *STy=FunctionType::get(Type::getInt32Ty(Context), false); > Function *check = Function::Create(STy, Function::InternalLinkage, > "check" ,&M); > > CallInst *callcheck = CallInst::Create(FibF,"CallCheck"); > > namespace { > > struct Hello : public FunctionPass > { > > static char ID; > Hello() : FunctionPass(ID) {} > virtual bool runOnFunction(Function &F) > { > errs() << "Hello: "; > errs().write_escaped(F.getName()) > <<'\n'; > // run through all the instruction and convert all the callinst to ... > for (Function::iterator BI = F.begin(), BE > F.end(); BI != BE; ++BI) > { > for(BasicBlock::iterator II > BI->begin(),IE = BI->end();II != IE; ++II) > { > // errs() <<"between instructions! \n"; > // CallInst * III > CallInst::Create(&F,"InsttoCallInst",II); > if(CallInst * III > dyn_cast<CallInst>(II)) > { > > > if(III->getCalledFunction()!=NULL&&III->getCalledFunction()->getName()=="puts") > { > errs() > <<III->getCalledFunction()->getName()<<" function found!\n"; > > callcheck->insertBefore(II); > errs() <<"INSERT > SUCCEEDED!!!!!!!!\n"; > } > else > { > errs() <<"it's not main > function!\n"<<"it is:"<<III->getCalledFunction()->getName()<<'\n'; > } > }/**/ > } > > } > > } > return true; > } > }; > } > > char Hello::ID = 0; > static RegisterPass<Hello> X("hello", "ZHello Korld Pass", false, false); > > -- > 祝好! > > 甄凯 > > ------------------------------------------------------------------------------------------------------ > 2012-04-09 > > ------------------------------------------------------------------------------------------------------ > Name: 甄凯(ZhenKai) > Homepage:http://www.renren.com/262729393 > Email: zhenkaixd at 126.com or 846227103 at qq.com > TEL: 15810729006(Beijing) > Address: Room I-406, Central Building, Tsinghua University, Beijing, > China. 100084. > > > > > > _______________________________________________ > 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/20120409/cb0ed592/attachment.html>
Alexander Potapenko
2012-Apr-09 09:19 UTC
[LLVMdev] How to instrument a this function using insertBefore instruction???
You'll need to declare the function in a separate module, not in the function pass. That module needs to be compiled independently and linked to the program instrumented by your pass. In order to add a function declaration to the program, use getOrInsertFunction() You may want to have a look at how AddressSanitizer works (http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp), it does essentially the same. HTH, Alex
Maybe Matching Threads
- [LLVMdev] How to instrument a this function using insertBefore instruction???
- [LLVMdev] How to instrument a this function using insertBefore instruction???
- [LLVMdev] How to instrument a this function using insertBefore instruction???
- [LLVMdev] How to instrument a this function using insertBefore instruction???
- [LLVMdev] How to explain this weird phenomenon????????