S S
S S

Reputation: 1020

Clang - Get actual function pointer declaration traceback

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.

Upvotes: 3

Views: 940

Answers (1)

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.

Upvotes: 3

Related Questions