2

I am newbie to clang, and working on parsing of C/C++ code. I am giving below code as input, but not able to find actual function name using clang.

#include <stdio.h>

typedef int (*ExampleCallback)(void* something, int status);

//This will be in a separate file
void moduleAPI_start(ExampleCallback onSomething);
int moduleAPI_run();

static ExampleCallback storedCallback;

void moduleAPI_start(ExampleCallback onSomething)
{
    storedCallback = onSomething;
}

int moduleAPI_run()
{
    return storedCallback(NULL, '0');
}

//Callback implementing ExampleCallback signature
int myCallback(void* something, int status)
{
    return status;
}

int main(void)
{
    moduleAPI_start(myCallback);
    printf("%d\n", moduleAPI_run());
    return 0;
}

My question is, how do I know that, whenever I call moduleAPI_run, it is actually calling myCallback via storedCallback.

In short, how do I get below sequence, so that I can use myCallback for further processing. storedCallback->onSomething->myCallback

Note: I have gone through this post, but unable to get clear solution.

Edit: On bigger side, we are trying to solve function pointer identity. We have different functions written with same signature having different purpose (to register appropriate function as callback at run time). With this clang analysis tool, we want to visualize the actual function called.

Community
  • 1
  • 1
S S
  • 920
  • 1
  • 17
  • 34

1 Answers1

2

My question is, how do I know that, whenever I call moduleAPI_run, it is actually calling myCallback via storedCallback.

That's not a question the parser or abstract syntax tree can answer.

The callback isn't a fixed function. It is a variable which holds a pointer to a function that can be assigned and reassigned. So it can carry many different values over the course of the run of the program. For a software tool to be able to answer questions about that without running the program is called (tag added to question):

https://en.wikipedia.org/wiki/Static_program_analysis

You may be able to look at this simple one-file example and yourself see that storedCallback is assigned in one place...and that this place is executed exactly once, with no paths avoiding it. But it's actually impossible for an analysis tool to tell you how an arbitrary input program will behave at runtime, due to the Halting Problem--if you're not already aware of that:

https://en.wikipedia.org/wiki/Halting_problem

I don't know what exactly the class of queries it is you are looking to cover, just from your one example. But I will say that static analysis tools tend to be fairly special purpose--mostly for finding bugs. Many are commercial and expensive.

UPDATE: It turns out Clang itself has a static analyzer (and apparently a pretty good one). But I don't see any "API" for it at first glance, just how to use the tool. So if you wanted access to its information that it uses to generate the reports, you'd probably have to crack open the code and adapt it. But someone else may know better than I.

  • Thanks for the clear picture. I edited the question for better understanding of the problem that I am trying to solve. Yes, I am trying to do overall static analysis, but I think it should be possible, in worst case by storing all function pointer in table and keeping track of all. Another way might be to use `sourcelocation` to find actual function. Isn't it? – S S Aug 27 '15 at 06:17
  • 1
    @SaumyaSuhagiya I haven't used clang for this (I am old, it is new). So I don't know the specific API. But what you should probably do when looking at this type of problem is draw up some scenarios you expect it to be able to detect *(for whatever you are detecting)* and then some scenarios that you think are too hard for it to detect. Then explain to yourself concretely what it is that differentiates the easy cases from the too hard ones. If you can very clearly know that, you have a chance of making a tool that can know it too. Which is how these things work. – HostileFork says dont trust SE Aug 27 '15 at 06:23