-
Hi, I am pretty new to codeql and logic programming in general. I am trying to come up with a query that can find the path (list of calls) between to functions. So essentially the query answers two questions:
In my current solution I am trying to accomplish this through recursion but it's a bit of a hassle to be honest and I have a feeling there might be some built in solution for this already ? I tried looking at Global Data flow and such but it seems like that might not be applicable to this problem ? Since the only thing I am trying to get is if |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Hi @dalao1337, You can use something like what I wrote in this issue: /**
* @kind path-problem
*/
import cpp
import semmle.code.cpp.ir.dataflow.ResolveCall
// `edges(a, b)` holds whenever there's a call inside function `a` that may target function `b`.
query predicate edges(Function a, Function b) {
// resolve any calls inside the `a` function.
resolveCall(any(Call call | call.getEnclosingFunction() = a)) = b
}
// This predicate holds if there's a path of calls from `start` to `end`, where the name of `start` is `startName` and the name of `end` is `endName`.
predicate getCallGraph(Function start, Function end, string startName, string endName) {
edges+(start, end) and
start.hasName(startName) and
end.hasName(endName)
}
from Function start, Function end
where getCallGraph(start, end, "main", end.getName()) // `end` are all the functions reachable from `main`.
select end, start, end, "A sequence that starts at 'main' and ends at " + end.getName() This will render a graph due to the I hope that helps! |
Beta Was this translation helpful? Give feedback.
-
Hi @MathiasVP thanks for help! However I just tried this query and it didn't seem to work for me... It is returning no results, what I have tried is changing the line where getCallGraph(start, end, "main", end.getName()) with where getCallGraph(start, end, "function_a", "function_b") and I am not getting any results although |
Beta Was this translation helpful? Give feedback.
-
Just to clarify what I am trying to accomplish, if we take a look at the sample program below: void func_b(){
func_c();
}
void func_a(){
func_b();
}
void func_c(){
func_d();
}
void func_d(){
}
int main(){
func_a();
return 0;
} I would like to have a query that can give me the path between |
Beta Was this translation helpful? Give feedback.
-
Something like this should work
|
Beta Was this translation helpful? Give feedback.
Just to clarify what I am trying to accomplish, if we take a look at the sample program below:
I would like to have a query that can give me the path between
main()
andfunc_d()
so essentially,main() -> func_a() -> func_b() -> func_c() -> func_d()
. Currently, starting from functionfunc_d()
I am able to back-track all the way tomain()
by using a recursive predicate that usesgetACallToThisFunction()
andgetEnclosingFunction()
. Although this is capable of asserting whetherfunction_d()
is reachable frommain()
, that's all that it does…