[llvm-dev] writing llvm pass for external library

sangeeta chowdhary via llvm-dev llvm-dev at lists.llvm.org
Thu Jan 4 08:42:45 PST 2018


Hello All,

I am trying to write llvm pass to instrument function in external library. I am using clang to automatically load my pass. I have test.cpp which calls functions in library testlib.so. I have written llvm pass to instrument all reads and writes in test.cpp and call function(recordRW) in testlib.so. 
this is how my pass looks like -

for (auto &BB : F) {
      for (auto &I : BB) {
        // Is this a load or store? Get the address.
        Value *Ptr = nullptr;
        Value *op_l = nullptr;
        Value *op_s = nullptr;
        if (auto *LI = dyn_cast<LoadInst>(&I)) {
          Ptr = LI->getPointerOperand();
  
            Constant* read = ConstantInt::get(Type::getInt32Ty(M.getContext()), 0);
            instrument_access(Ptr, &(*LI), read, M);
            modified = true;
        } else if (auto *SI = dyn_cast<StoreInst>(&I)) {
          Ptr = SI->getPointerOperand();

            Constant* write = ConstantInt::get(Type::getInt32Ty(M.getContext()), 1);
            instrument_access(Ptr, &(*SI),write, M);
            modified = true;

        }
    }
}

 void instrument_access(Value* op, Instruction* inst, Constant* rdWr, Module& module) {
    SmallVector<Value*, 8> args;
    Instruction* get_tid = CallInst::Create(getThreadId, args, "", inst);
    BitCastInst* bitcast = new BitCastInst(op,
           PointerType::getUnqual(Type::getInt8Ty(module.getContext())),
             "", inst);

    args.push_back(get_tid);
    args.push_back(bitcast);
    args.push_back(rdWr);

    Instruction* recordRWCall = CallInst::Create(recordRW, args, "", inst);
  }

I have run my pass with opt and I could see that recordRWCall has been created but this function is not called when I run test.cpp. How can I integrate all this together? What I want to do is when I run test.cpp, and if there is any read or write instruction, recordRW defined in testlib.so should be called. I have read a lot on Internet but I could find a way to do something like this. Any help would be much appreciated.

Regards,
Sangeeta
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180104/3a6a6ecf/attachment-0001.html>


More information about the llvm-dev mailing list