[llvm-dev] Finding callees of a function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[llvm-dev] Finding callees of a function

Jonas Paulsson via llvm-dev
I searched the doxygen documentation and could not find a solution to my
task:

In a ModulePass running at EP_OptimizerLast, if I have a function F like in:

  bool Foo:runOnModule(Module &M) {
    LLVMContext &C = M.getContext();
    for (auto &F : M) {
      // magic here

if I want to know from which function (callee) each function is called -
how can I do this?
(so that I e.g. have a "Function *callee" result or a list if it is
called from several other functions - or even better, the specific basic
blocks  where the calls are.)

I am aware that this will not work for calls that are made outside of
the analyzed module.

Thanks!

Regards,
Marc

--
Marc Heuse
www.mh-sec.de

PGP: AF3D 1D4C D810 F0BB 977D  3807 C7EE D0A0 6BE9 F573
_______________________________________________
LLVM Developers mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Finding callees of a function

Jonas Paulsson via llvm-dev
Hi Marc,

On Wed, 15 Jan 2020 at 09:52, Marc via llvm-dev <[hidden email]> wrote:
>   bool Foo:runOnModule(Module &M) {
>     LLVMContext &C = M.getContext();
>     for (auto &F : M) {
>       // magic here
>
> if I want to know from which function (callee) each function is called -
> how can I do this?

To get the direct callers you would iterate through the users of F,
and check whether it's being used as the callee operand in a CallInst
or InvokeInst. Fortunately there's a CallSite class that abstracts
away many of the differences. So something like:

    for (auto &U : F.getUsers()) {
      if (auto CS = CallSite(U)) {
        if (CS->getCalledFunction() == F)
          doStuff(CS);
      }
    }

> I am aware that this will not work for calls that are made outside of
> the analyzed module.

Also indirect calls.

Cheers.

Tim.
_______________________________________________
LLVM Developers mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Finding callees of a function

Jonas Paulsson via llvm-dev
You should use getCalledValue()->stripPointerCasts() instead

Zhang

> 在 2020年1月15日,17:58,Tim Northover via llvm-dev <[hidden email]> 写道:
>
> Hi Marc,
>
>> On Wed, 15 Jan 2020 at 09:52, Marc via llvm-dev <[hidden email]> wrote:
>>  bool Foo:runOnModule(Module &M) {
>>    LLVMContext &C = M.getContext();
>>    for (auto &F : M) {
>>      // magic here
>>
>> if I want to know from which function (callee) each function is called -
>> how can I do this?
>
> To get the direct callers you would iterate through the users of F,
> and check whether it's being used as the callee operand in a CallInst
> or InvokeInst. Fortunately there's a CallSite class that abstracts
> away many of the differences. So something like:
>
>    for (auto &U : F.getUsers()) {
>      if (auto CS = CallSite(U)) {
>        if (CS->getCalledFunction() == F)
>          doStuff(CS);
>      }
>    }
>
>> I am aware that this will not work for calls that are made outside of
>> the analyzed module.
>
> Also indirect calls.
>
> Cheers.
>
> Tim.
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



_______________________________________________
LLVM Developers mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Finding callees of a function

Jonas Paulsson via llvm-dev
In reply to this post by Jonas Paulsson via llvm-dev
——-If you need to handle indirect calls

Zhang

> 在 2020年1月15日,17:58,Tim Northover via llvm-dev <[hidden email]> 写道:
>
> Hi Marc,
>
>> On Wed, 15 Jan 2020 at 09:52, Marc via llvm-dev <[hidden email]> wrote:
>>  bool Foo:runOnModule(Module &M) {
>>    LLVMContext &C = M.getContext();
>>    for (auto &F : M) {
>>      // magic here
>>
>> if I want to know from which function (callee) each function is called -
>> how can I do this?
>
> To get the direct callers you would iterate through the users of F,
> and check whether it's being used as the callee operand in a CallInst
> or InvokeInst. Fortunately there's a CallSite class that abstracts
> away many of the differences. So something like:
>
>    for (auto &U : F.getUsers()) {
>      if (auto CS = CallSite(U)) {
>        if (CS->getCalledFunction() == F)
>          doStuff(CS);
>      }
>    }
>
>> I am aware that this will not work for calls that are made outside of
>> the analyzed module.
>
> Also indirect calls.
>
> Cheers.
>
> Tim.
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



_______________________________________________
LLVM Developers mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Finding callees of a function

Jonas Paulsson via llvm-dev
In reply to this post by Jonas Paulsson via llvm-dev
On 01/15, Tim Northover via llvm-dev wrote:

> Hi Marc,
>
> On Wed, 15 Jan 2020 at 09:52, Marc via llvm-dev <[hidden email]> wrote:
> >   bool Foo:runOnModule(Module &M) {
> >     LLVMContext &C = M.getContext();
> >     for (auto &F : M) {
> >       // magic here
> >
> > if I want to know from which function (callee) each function is called -
> > how can I do this?
>
> To get the direct callers you would iterate through the users of F,
> and check whether it's being used as the callee operand in a CallInst
> or InvokeInst. Fortunately there's a CallSite class that abstracts
> away many of the differences. So something like:
>
>     for (auto &U : F.getUsers()) {
>       if (auto CS = CallSite(U)) {
>         if (CS->getCalledFunction() == F)
>           doStuff(CS);
>       }
>     }
Nit: This might visit the same call site multiple times if the function
is passed as a function pointer argument to recursive call `f(&f, &f)`.

    for (auto &U : F.getUses()) {
      if (auto CS = CallSite(U.getUser())) {
        if (CS->isCallee(&U))
          doStuff(CS);
      }
    }

or, if you also want to deal with callback calls [0, 1, 2], you can do:

    for (auto &U : F.getUses()) {
      if (auto ACS = AbstractCallSite(U))
        doStuff(ACS);  // or ACS.getCallSite()
    }

Cheers,
  Johannes

[0] https://clang.llvm.org/docs/AttributeReference.html#callback
[1] https://llvm.org/docs/LangRef.html#callback-metadata
[2] https://www.youtube.com/watch?v=zfiHaPaoQPc

> > I am aware that this will not work for calls that are made outside of
> > the analyzed module.
>
> Also indirect calls.
>
> Cheers.
>
> Tim.
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
--

Johannes Doerfert
Researcher

Argonne National Laboratory
Lemont, IL 60439, USA

[hidden email]

_______________________________________________
LLVM Developers mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

signature.asc (235 bytes) Download Attachment