Hello, everyone<br>I am new to LLVM, now I got a problem<br><br>I want to add a function call before sleep(int a, int b)<br><br>code below<br><br>#include <stdio.h><br><br>int sleep(int a, int b)<br>{<br> <br>
return a+b; <br>}<br>int main(int argc, char **argv)<br>{<br> sleep(1,2);<br><br>}<br><br><br><br>after use opt -load ../llvm-2.8/Release+Asserts/lib/bishe_insert.so -bishe_insert <1.bc> 2.bc<br>I want get the code <br>
<br>#include <stdio.h><br><br>int sleep(int a, int b)<br>{<br> <br> return a+b; <br>}<br>int main(int argc, char **argv)<br>{<br> hook() //void *hook(void )<br> sleep(1,2);<br>}<br><br><br>after Using apt................................................<br>
<br>ERROR Occured<br><br>[kain@localhost 4]$ opt -load /home/kain/Documents/bishe/llvm-2.8/Release+Asserts/lib/bishe_insert.so -bishe_insert <1.bc> 2.bc<br>opt: Type.cpp:456: llvm::FunctionType::FunctionType(const llvm::Type*, const std::vector<const llvm::Type*, std::allocator<const llvm::Type*> >&, bool): Assertion `isValidArgumentType(Params[i]) && "Not a valid type for function argument!"' failed.<br>
0 opt 0x000000000082f5cf<br>1 opt 0x000000000083181a<br>2 libpthread.so.0 0x00000033f460f4c0<br>3 libc.so.6 0x00000033f42329a5 gsignal + 53<br>4 libc.so.6 0x00000033f4234185 abort + 373<br>
5 libc.so.6 0x00000033f422b935 __assert_fail + 245<br>6 opt 0x00000000007cb477 llvm::FunctionType::FunctionType(llvm::Type const*, std::vector<llvm::Type const*, std::allocator<llvm::Type const*> > const&, bool) + 407<br>
7 opt 0x00000000007cdf94 llvm::FunctionType::get(llvm::Type const*, std::vector<llvm::Type const*, std::allocator<llvm::Type const*> > const&, bool) + 324<br>8 opt 0x00000000007babad llvm::Module::getOrInsertFunction(llvm::StringRef, llvm::Type const*, ...) + 253<br>
9 bishe_insert.so 0x00007fb4ec5838b0<br>10 opt 0x00000000007c47d7 llvm::MPPassManager::runOnModule(llvm::Module&) + 503<br>11 opt 0x00000000007c4957 llvm::PassManagerImpl::run(llvm::Module&) + 167<br>
12 opt 0x00000000004a43be main + 2734<br>13 libc.so.6 0x00000033f421ec5d __libc_start_main + 253<br>14 opt 0x0000000000499d19<br>Stack dump:<br>0. Program arguments: opt -load /home/kain/Documents/bishe/llvm-2.8/Release+Asserts/lib/bishe_insert.so -bishe_insert <br>
1. Running pass 'test function exist' on module '<stdin>'.<br>Aborted (core dumped)<br><br><br><br><br><br>Below is my PASS code<br><br><br><br>#include "llvm/Pass.h"<br>#include "llvm/Module.h"<br>
#include "llvm/Function.h"<br>#include "llvm/Support/raw_ostream.h"<br>#include "llvm/Type.h"<br>#include "llvm/Instructions.h"<br>#include "llvm/Instruction.h"<br><br>#include "llvm/Support/IRBuilder.h"<br>
<br><br><br>using namespace llvm;<br><br>namespace{<br> struct bishe_insert : public ModulePass{<br> static char ID; <br> Function *hook;<br><br> bishe_insert() : ModulePass(ID) {}<br><br> virtual bool runOnModule(Module &M)<br>
{<br>// Function *F = M.getFunction("sleep");<br>// hook = Function::Create(F->getFunctionType(), GlobalValue::ExternalLinkage, "hook", F->getParent());<br> Constant *hookFunc;<br>
hookFunc = M.getOrInsertFunction("hook", Type::getVoidTy(M.getContext()), Type::getVoidTy(M.getContext()), (Type*)0);<br> <br> hook= cast<Function>(hookFunc);<br><br><br>
for(Module::iterator F = M.begin(), E = M.end(); F!= E; ++F)<br> {<br><br> for(Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)<br> {<br> bishe_insert::runOnBasicBlock(BB);<br>
}<br> }<br><br> return false;<br> }<br> virtual bool runOnBasicBlock(Function::iterator &BB)<br> {<br> for(BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI) <br>
{<br> if(isa<CallInst>(&(*BI)) || isa<InvokeInst>(&(*BI)))<br> {<br> CallInst *CI = dyn_cast<CallInst>(BI);<br><br> std::vector<Value *> Args;<br>
for(Function::arg_iterator i = hook->arg_begin(), e = hook->arg_end(); i != e; ++i)<br> {<br> Args.push_back(i); <br> }<br>
<br> if("sleep" == CI->getCalledFunction()->getName())<br> {<br><br> CallInst *NewCall = CallInst::Create(hook,Args.begin(), Args.end(), "hook", (Instruction *)BI);<br>
}<br> <br> }<br> <br> }<br> return true;<br> }<br> };<br>}<br>char bishe_insert::ID = 0;<br>static RegisterPass<bishe_insert> X("bishe_insert", "test function exist", false, false);<br>
<br><br><br><br><br><br><br><br><br><br><br><br><br>I think CallInst *NewCall = CallInst::Create(hook,Args.begin(), Args.end(), "hook", (Instruction *)BI)<br>can Insert hook() before sleep(1,2), but the Args may be wrong,<br>
I wirte the PASS code by learning Miscompilation.cpp<br><br><br>Thank you very much<br> <br>