Skip to content

Commit b9205b1

Browse files
authored
Merge pull request github#17788 from MathiasVP/better-function-pointer-resolution
C++: Improve function pointer resolution
2 parents 7d62cda + 5e04358 commit b9205b1

File tree

7 files changed

+46
-1
lines changed

7 files changed

+46
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The function call target resolution algorithm has been improved to resolve more calls through function pointers. As a result, dataflow queries may have more results.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: feature
3+
---
4+
* Added a new predicate `DataFlow::getARuntimeTarget` for getting a function that may be invoked by a `Call` expression. Unlike `Call.getTarget` this new predicate may also resolve function pointers.

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,10 @@ predicate lambdaCreation(Node creation, LambdaCallKind kind, DataFlowCallable c)
13281328

13291329
/** Holds if `call` is a lambda call of kind `kind` where `receiver` is the lambda expression. */
13301330
predicate lambdaCall(DataFlowCall call, LambdaCallKind kind, Node receiver) {
1331-
call.(SummaryCall).getReceiver() = receiver.(FlowSummaryNode).getSummaryNode() and
1331+
(
1332+
call.(SummaryCall).getReceiver() = receiver.(FlowSummaryNode).getSummaryNode() or
1333+
call.asCallInstruction().getCallTargetOperand() = receiver.asOperand()
1334+
) and
13321335
exists(kind)
13331336
}
13341337

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ private import SsaInternals as Ssa
1717
private import DataFlowImplCommon as DataFlowImplCommon
1818
private import codeql.util.Unit
1919
private import Node0ToString
20+
private import DataFlowDispatch as DataFlowDispatch
2021
import ExprNodes
2122

2223
/**
@@ -2497,3 +2498,16 @@ class AdditionalCallTarget extends Unit {
24972498
*/
24982499
abstract Declaration viableTarget(Call call);
24992500
}
2501+
2502+
/**
2503+
* Gets a function that may be called by `call`.
2504+
*
2505+
* Note that `call` may be a call to a function pointer expression.
2506+
*/
2507+
Function getARuntimeTarget(Call call) {
2508+
exists(DataFlowCall dfCall | dfCall.asCallInstruction().getUnconvertedResultExpression() = call |
2509+
result = DataFlowDispatch::viableCallable(dfCall).asSourceCallable()
2510+
or
2511+
result = DataFlowImplCommon::viableCallableLambda(dfCall, _).asSourceCallable()
2512+
)
2513+
}

cpp/ql/test/library-tests/dataflow/dataflow-tests/dataflow-consistency.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ uniqueEnclosingCallable
33
| test.cpp:864:47:864:54 | call to source | Node should have one enclosing callable but has 0. |
44
| test.cpp:872:46:872:51 | call to source | Node should have one enclosing callable but has 0. |
55
| test.cpp:872:53:872:56 | 1 | Node should have one enclosing callable but has 0. |
6+
| test.cpp:1126:33:1129:1 | {...} | Node should have one enclosing callable but has 0. |
7+
| test.cpp:1127:3:1127:13 | reads_input | Node should have one enclosing callable but has 0. |
8+
| test.cpp:1128:3:1128:21 | not_does_read_input | Node should have one enclosing callable but has 0. |
69
uniqueCallEnclosingCallable
710
| test.cpp:864:47:864:54 | call to source | Call should have one enclosing callable but has 0. |
811
| test.cpp:872:46:872:51 | call to source | Call should have one enclosing callable but has 0. |

cpp/ql/test/library-tests/dataflow/dataflow-tests/test-source-sink.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ irFlow
323323
| test.cpp:1069:9:1069:14 | call to source | test.cpp:1074:10:1074:10 | i |
324324
| test.cpp:1069:9:1069:14 | call to source | test.cpp:1081:10:1081:10 | i |
325325
| test.cpp:1117:27:1117:34 | call to source | test.cpp:1117:27:1117:34 | call to source |
326+
| test.cpp:1132:11:1132:16 | call to source | test.cpp:1121:8:1121:8 | x |
326327
| true_upon_entry.cpp:9:11:9:16 | call to source | true_upon_entry.cpp:13:8:13:8 | x |
327328
| true_upon_entry.cpp:17:11:17:16 | call to source | true_upon_entry.cpp:21:8:21:8 | x |
328329
| true_upon_entry.cpp:27:9:27:14 | call to source | true_upon_entry.cpp:29:8:29:8 | x |

cpp/ql/test/library-tests/dataflow/dataflow-tests/test.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,4 +1115,20 @@ void indirect_sink_const_ref(const T&);
11151115

11161116
void test_temp_with_conversion_from_materialization() {
11171117
indirect_sink_const_ref(source()); // $ ir MISSING: ast
1118+
}
1119+
1120+
void reads_input(int x) {
1121+
sink(x); // $ ir MISSING: ast
1122+
}
1123+
1124+
void not_does_read_input(int x);
1125+
1126+
void (*dispatch_table[])(int) = {
1127+
reads_input,
1128+
not_does_read_input
1129+
};
1130+
1131+
void test_dispatch_table(int i) {
1132+
int x = source();
1133+
dispatch_table[i](x);
11181134
}

0 commit comments

Comments
 (0)