[llvm-dev] Trace calls to memcpy

Matt Fysh via llvm-dev llvm-dev at lists.llvm.org
Thu Mar 12 06:15:29 PDT 2020


Hello,

I'm trying to use an LLVM Pass to trace calls to memcpy via
instrumentation, but finding it quite difficult without having much
knowledge of sound IR structure. Here's my FunctionPass thus far:

// at the beginning of runOnFunction
FunctionCallee hookFunc = M->getOrInsertFunction("_Z12trace_memcpyPvS_m",
Type::getVoidTy(M->getContext()));

// further in the same function
for (BasicBlock &BB : F) {
  for (Instruction &I : BB) {
    if (isa<CallInst>(&I)) {
      CallInst *CI = dyn_cast<CallInst>(&I);
      Function* FC = CI->getCalledFunction();
      StringRef SR = FC->getName();
      if (SR == "llvm.memcpy.p0i8.p0i8.i64") {
        CallInst* newInstr = dyn_cast<CallInst>(CI->clone());
        newInstr->setCalledFunction(hookFunc);
        newInstr->insertBefore(CI);
      }
    }
  }
}

The issue arises when using `setCalledFunction`. If I remove that, it runs
successfully and I end up with duplicate calls to memcpy - as expected.

But when I leave it in, and run the pass with `opt`, I get the following
error:

LLVM ERROR: Broken function found, compilation aborted!

Looking for any clues on how to fix... and would also appreciate  pointers
in general about how I might learn the general concepts at play here.

Thanks all,.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200313/fe0c4981/attachment.html>


More information about the llvm-dev mailing list