简体   繁体   English

如何在LLVM IR中获取引用函数指针?

[英]How to get the referencing function pointer in LLVM IR?

I am writing a LLVM module pass that will get the third argument in pthread_create .我正在编写一个 LLVM 模块传递,它将获得pthread_create的第三个参数。 According to the man page the function prototype of pthread_create is根据手册页pthread_create的函数原型是

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);

For example my target C source is as following.例如我的目标 C 源代码如下。

  void *pthread_task(void *args) {
    sleep(4);
    return NULL;
  }

  int main(int argc, char *argv[]) {
    pthread_t tid;
    pthread_create(&tid, NULL, pthread_task, NULL);
    pthread_join(tid, NULL);
    return 0;
  }

I would like to have the output "pthread_task".我想要输出“pthread_task”。 I was able to access the right function and list all arguments.我能够访问正确的函数并列出所有参数。 However, since the third parameter is a function pointer, I don't know how to access the function which it points to.但是,由于第三个参数是函数指针,我不知道如何访问它指向的函数。

Also I attach how I implement my module pass as following.我还附上了我如何实现我的模块传递如下。

  namespace {
~   int32_t indexOfPthreadCreate(CallGraphNode *node) {
~     for (int32_t i = 0; i < node -> size(); i++) {
~       Function *f = (*node)[i] -> getFunction();
~       if (f && f -> getName().compare("pthread_create") == 0) {
+         for (auto &A: f->args()) {

            // I would like to access the third parameter of pthread_create here.

+           A.print(errs());
+         }
+         return i;
+       }
      }
~     return -1;
    }
    struct PthreadScopeDetectPass : public ModulePass {
      static char ID;
      PthreadScopeDetectPass() : ModulePass(ID) { }

      bool runOnModule(Module &M) override {
        CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
        uint32_t nSCC  = 0;
        for (scc_iterator<CallGraph *> iterSCC = scc_begin(&CG); !iterSCC.isAtEnd(); ++iterSCC) {
          auto nodes = *iterSCC;
          for (CallGraphNode *node: nodes) {
            Function *currFunc = node -> getFunction();
~           int32_t target_i = indexOfPthreadCreate(node);
~           if (target_i > 0 && currFunc) {
+             insertTimer(currFunc);
            }
          }
        }
        return true;
      }

      void getAnalysisUsage(AnalysisUsage &AU) const override {
        AU.setPreservesAll();
        AU.addRequired<CallGraphWrapperPass>();
      }
    };
  }


Thanks for @arnt's comment.感谢@arnt 的评论。 I already solve the issue.我已经解决了这个问题。 Here's how I solve it.这是我解决它的方法。

~   int32_t indexOfPthreadCreate(CallGraphNode *node) {
~     for (int32_t i = 0; i < node -> size(); i++) {
~       Function *f = (*node)[i] -> getFunction();
~       if (f && f -> getName().compare("pthread_create") == 0) {
+         for (auto &inst: instructions(f)) {
~           if (inst.getOpcode() == 56) {
~             CallInst *ci = dyn_cast<CallInst>(&inst);
~             if (ci && ci->getCalledFunction()->getName().compare("pthread_create") == 0) {
~               Function *pthread_task = dyn_cast<Function>(ci->getArgOperand(2));
~_              errs() << "pthread is going to execute " << pthread_task->getName() << "function \n";
              }
+           }
+           A.print(errs());
+         }
+         return i;
+       }
      }
~     return -1;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM