Skip to content

Commit e2c4de6

Browse files
Copilotdata-douser
andauthored
Fix CallGraph queries to use resolved target entities instead of name-based matching
Agent-Logs-Url: https://github.com/advanced-security/codeql-development-mcp-server/sessions/8ce53ee0-dea8-4a8f-b300-436d11463003 Co-authored-by: data-douser <70299490+data-douser@users.noreply.github.com>
1 parent c3e1064 commit e2c4de6

6 files changed

Lines changed: 33 additions & 11 deletions

File tree

server/ql/rust/tools/src/CallGraphFrom/CallGraphFrom.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn source_func() { // Source function for analysis
3333
}
3434
```
3535

36-
Running with `sourceFunction = "source_func"` produces results showing each call site with the message pattern ``Call from `source_func` to `helper1```.
36+
Running with `sourceFunction = "source_func"` produces results showing each call site with the message pattern ``Call from `source_func` to `helper1` ``.
3737

3838
## Output Format
3939

server/ql/rust/tools/src/CallGraphFrom/CallGraphFrom.ql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ string getSourceFunctionName() {
1717
exists(string s | sourceFunction(s) | result = s.splitAt(",").trim())
1818
}
1919

20+
/**
21+
* Gets a function by matching against the selected source function names.
22+
*/
23+
Function getSourceFunction() { result.getName().getText() = getSourceFunctionName() }
24+
2025
/**
2126
* Gets the name of the called function.
2227
*/
@@ -29,5 +34,5 @@ string getCalleeName(CallExpr call) {
2934
from CallExpr call, Function source
3035
where
3136
call.getEnclosingCallable() = source and
32-
source.getName().getText() = getSourceFunctionName()
37+
source = getSourceFunction()
3338
select call, "Call from `" + source.getName().getText() + "` to `" + getCalleeName(call) + "`"

server/ql/rust/tools/src/CallGraphFromTo/CallGraphFromTo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn source() {
3333
}
3434
```
3535

36-
Running with `sourceFunction = "source"` and `targetFunction = "target"` produces results showing each call site on the path with the message pattern ``Reachable call from `intermediate` to `target```.
36+
Running with `sourceFunction = "source"` and `targetFunction = "target"` produces results showing each call site on the path with the message pattern ``Reachable call from `intermediate` to `target` ``.
3737

3838
## Output Format
3939

server/ql/rust/tools/src/CallGraphFromTo/CallGraphFromTo.ql

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,22 @@ string getTargetFunctionName() {
2525
}
2626

2727
/**
28-
* Holds if function `caller` directly calls function `callee` by name.
28+
* Gets a function by matching against the selected source function names.
29+
*/
30+
Function getSourceFunction() { result.getName().getText() = getSourceFunctionName() }
31+
32+
/**
33+
* Gets a function by matching against the selected target function names.
34+
*/
35+
Function getTargetFunction() { result.getName().getText() = getTargetFunctionName() }
36+
37+
/**
38+
* Holds if function `caller` directly calls function `callee`.
2939
*/
3040
predicate calls(Function caller_, Function callee_) {
3141
exists(CallExpr c |
3242
c.getEnclosingCallable() = caller_ and
33-
c.getResolvedTarget().(Function).getName().getText() = callee_.getName().getText()
43+
c.getResolvedTarget().(Function) = callee_
3444
)
3545
}
3646

@@ -47,11 +57,11 @@ from CallExpr call, Function caller
4757
where
4858
call.getEnclosingCallable() = caller and
4959
exists(Function source, Function target |
50-
source.getName().getText() = getSourceFunctionName() and
51-
target.getName().getText() = getTargetFunctionName() and
60+
source = getSourceFunction() and
61+
target = getTargetFunction() and
5262
calls*(source, caller) and
5363
exists(Function callee |
54-
call.getResolvedTarget().(Function).getName().getText() = callee.getName().getText() and
64+
call.getResolvedTarget().(Function) = callee and
5565
calls*(callee, target)
5666
)
5767
)

server/ql/rust/tools/src/CallGraphTo/CallGraphTo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn caller2() {
3232
}
3333
```
3434

35-
Running with `targetFunction = "target_func"` produces results showing each call site with the message pattern ``Call to `target_func` from `caller1```.
35+
Running with `targetFunction = "target_func"` produces results showing each call site with the message pattern ``Call to `target_func` from `caller1` ``.
3636

3737
## Output Format
3838

server/ql/rust/tools/src/CallGraphTo/CallGraphTo.ql

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ string getTargetFunctionName() {
1717
exists(string s | targetFunction(s) | result = s.splitAt(",").trim())
1818
}
1919

20+
/**
21+
* Gets a function by matching against the selected target function names.
22+
*/
23+
Function getTargetFunction() { result.getName().getText() = getTargetFunctionName() }
24+
2025
/**
2126
* Gets the caller name for a call expression.
2227
*/
@@ -35,6 +40,8 @@ string getCalleeName(CallExpr call) {
3540
else result = call.toString()
3641
}
3742

38-
from CallExpr call
39-
where call.getResolvedTarget().(Function).getName().getText() = getTargetFunctionName()
43+
from CallExpr call, Function target
44+
where
45+
target = getTargetFunction() and
46+
call.getResolvedTarget() = target
4047
select call, "Call to `" + getCalleeName(call) + "` from `" + getCallerName(call) + "`"

0 commit comments

Comments
 (0)