<div style="line-height:1.7;color:#000000;font-size:14px;font-family:arial">Hi all£¬<br>Im trying to instrument this hello function right before the instruction that call the "puts" function(the source code is as follow).<br><br>Now I can compile the pass without errors, but when run the pass with opt tool, it broke down. The diagnose is something like<br> <br>Referencing function in another module!<br>  %CallCheck = call i32 @fib()<br>Broken module found, compilation aborted!<br><br>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.<br>Thank you!!<br><br>//******=========================================================================================================================================******//<br>//******=========================================================================================================================================******////                                     FPSFI: a Function Pass Based Idea for Software Fault Isolation<br>//<br>//<br>//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//<br>//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<br>// and  act it on every function the clang has analysed.<br>//<br>//******=========================================================================================================================================******//<br>//******=========================================================================================================================================******//<br>#include "llvm/Pass.h"<br>#include "llvm/Function.h"<br>#include "llvm/Support/raw_ostream.h"<br>#include "llvm/Instruction.h"<br>#include "llvm/Transforms/Utils/UnrollLoop.h"<br>#include "llvm/BasicBlock.h"<br>#include "llvm/ADT/Statistic.h"<br>#include "llvm/Analysis/LoopIterator.h"<br>#include "llvm/Analysis/LoopPass.h"<br>#include "llvm/Analysis/ScalarEvolution.h"<br>#include "llvm/Analysis/ScalarEvolutionExpander.h"<br>#include "llvm/Support/Debug.h"<br>#include "llvm/Transforms/Utils/BasicBlockUtils.h"<br>#include "llvm/Transforms/Utils/Cloning.h"<br>#include "llvm/Type.h"<br>#include "llvm/LLVMContext.h"<br>#include "llvm/Support/Casting.h"<br>#include "stdio.h"<br><br>#include "llvm/Module.h"<br>using namespace llvm;<br><br><br>int check()<br>{<br>    printf("Hello me!!\n");<br>    return 0;<br>}<br><br> <br><br> Module * M;<br> LLVMContext Context;<br>  FunctionType *STy=FunctionType::get(Type::getInt32Ty(Context), false);<br>  Function *check = Function::Create(STy, Function::InternalLinkage, "check" ,&M); <br><br>  CallInst *callcheck = CallInst::Create(FibF,"CallCheck");<br><br>namespace {<br><br>struct Hello : public FunctionPass<br>    {<br><br>                      static char ID;<br>                      Hello() : FunctionPass(ID) {}<br>                      virtual bool runOnFunction(Function &F)<br>                      {<br>                              errs() << "Hello: ";<br>                                  errs().write_escaped(F.getName()) <<'\n';                                 <br>// run through all the instruction and convert all the callinst to ...<br>                            for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI)<br>                             {<br>                                for(BasicBlock::iterator II = BI->begin(),IE = BI->end();II != IE; ++II)<br>                                 {<br>//                                     errs() <<"between instructions! \n";<br>//                                     CallInst * III = CallInst::Create(&F,"InsttoCallInst",II);<br>                                        if(CallInst * III = dyn_cast<CallInst>(II))<br>                                         {<br><br>                                             if(III->getCalledFunction()!=NULL&&III->getCalledFunction()->getName()=="puts")<br>                                             {<br>                                                     errs() <<III->getCalledFunction()->getName()<<" function found!\n";<br>                                                callcheck->insertBefore(II);<br>                                                      errs() <<"INSERT SUCCEEDED!!!!!!!!\n";<br>                                              }<br>                                              else<br>                                             {<br>                                                  errs() <<"it's not main function!\n"<<"it is:"<<III->getCalledFunction()->getName()<<'\n';<br>                                              }<br>                                        }/**/  <br>                                }<br>                                <br>                            }<br>         <br>                                }  <br>                              return true;<br>                        }<br>       };<br> }<br><br>char Hello::ID = 0;<br>static RegisterPass<Hello> X("hello", "ZHello Korld Pass", false, false);<br><br><div>--<br><div>
<div>
<div><font color="#c0c0c0" size="2">
<div><font color="#c0c0c0"><font size="3">  <span style="COLOR: #000000">          ×£ºÃ£¡</span></font></font></div><span style="COLOR: #000000">
<div> </div>
<div><font size="3">  Õç¿­</font></div>
<div>
<div><font color="#c0c0c0"><font size="3">------------------------------------------------------------------------------------------------------</font></font></div></div>
<div><font color="#c0c0c0"><font size="3">2012-04-09</font></font></div></span></font></div>
<div><font color="#c0c0c0"><font size="3">------------------------------------------------------------------------------------------------------</font></font></div>
<div><font color="#c0c0c0"><font size="3">Name: Õç¿­(ZhenKai)<br>Homepage:http://www.renren.com/262729393</font></font></div>
<div><font color="#c0c0c0"><font size="3">Email: </font><a href="mailto:zhenkaixd@126.com" target="_blank" _listener="1901"><font color="#0066cc" size="3">zhenkaixd@126.com</font></a><font size="3"> or <a href="mailto:846227103@qq.com">846227103@qq.com</a></font></font></div>
<div><font color="#c0c0c0" size="3">TEL: 15810729006(Beijing)<br>Address: Room I-406, Central Building, Tsinghua University, Beijing, China. 100084.</font></div></div></div></div></div><br><br><span title="neteasefooter"><span id="netease_mail_footer"></span></span>