Skip to content

Commit 80b0765

Browse files
committed
C++: Make IR DataFlow dispatch use non-IR version
This removes code duplication and ensures that the IR version also gets the support for flow across link targets.
1 parent 10ce13d commit 80b0765

File tree

3 files changed

+3
-74
lines changed

3 files changed

+3
-74
lines changed
Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1 @@
1-
private import cpp
2-
private import DataFlowPrivate
3-
4-
Function viableImpl(MethodAccess ma) {
5-
result = ma.getTarget()
6-
}
7-
8-
Function viableCallable(Call call) {
9-
result = call.getTarget()
10-
}
11-
12-
/**
13-
* Holds if the call context `ctx` reduces the set of viable dispatch
14-
* targets of `ma` in `c`.
15-
*/
16-
predicate reducedViableImplInCallContext(MethodAccess ma, Callable c, Call ctx) {
17-
none()
18-
}
19-
20-
/**
21-
* Gets a viable dispatch target of `ma` in the context `ctx`. This is
22-
* restricted to those `ma`s for which a context might make a difference.
23-
*/
24-
private Method viableImplInCallContext(MethodAccess ma, Call ctx) {
25-
// stub implementation
26-
result = viableImpl(ma) and
27-
viableCallable(ctx) = ma.getEnclosingFunction()
28-
}
29-
30-
/**
31-
* Gets a viable dispatch target of `ma` in the context `ctx`. This is
32-
* restricted to those `ma`s for which the context makes a difference.
33-
*/
34-
Method prunedViableImplInCallContext(MethodAccess ma, Call ctx) {
35-
result = viableImplInCallContext(ma, ctx) and
36-
reducedViableImplInCallContext(ma, _, ctx)
37-
}
38-
39-
/**
40-
* Holds if data might flow from `ma` to a return statement in some
41-
* configuration.
42-
*/
43-
private predicate maybeChainedReturn(MethodAccess ma) {
44-
exists(ReturnStmt ret |
45-
exists(ret.getExpr()) and
46-
ret.getEnclosingFunction() = ma.getEnclosingFunction() and
47-
not ma.getParent() instanceof ExprStmt
48-
)
49-
}
50-
51-
/**
52-
* Holds if flow returning from `m` to `ma` might return further and if
53-
* this path restricts the set of call sites that can be returned to.
54-
*/
55-
predicate reducedViableImplInReturn(Method m, MethodAccess ma) {
56-
exists(int tgts, int ctxtgts |
57-
m = viableImpl(ma) and
58-
ctxtgts = count(Call ctx | m = viableImplInCallContext(ma, ctx)) and
59-
tgts = strictcount(Call ctx | viableCallable(ctx) = ma.getEnclosingFunction()) and
60-
ctxtgts < tgts
61-
) and
62-
maybeChainedReturn(ma)
63-
}
64-
65-
/**
66-
* Gets a viable dispatch target of `ma` in the context `ctx`. This is
67-
* restricted to those `ma`s and results for which the return flow from the
68-
* result to `ma` restricts the possible context `ctx`.
69-
*/
70-
Method prunedViableImplInCallContextReverse(MethodAccess ma, Call ctx) {
71-
result = viableImplInCallContext(ma, ctx) and
72-
reducedViableImplInReturn(result, ma)
73-
}
1+
import semmle.code.cpp.dataflow.internal.DataFlowDispatch

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
| acrossLinkTargets.cpp:19:27:19:32 | acrossLinkTargets.cpp:12:8:12:8 | AST only |
21
| test.cpp:66:30:66:36 | test.cpp:71:8:71:9 | AST only |
32
| test.cpp:89:28:89:34 | test.cpp:92:8:92:14 | IR only |
43
| test.cpp:100:13:100:18 | test.cpp:103:10:103:12 | AST only |

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
| acrossLinkTargets.cpp:12:8:12:8 | Convert: (int)... | acrossLinkTargets.cpp:19:27:19:32 | Call: call to source |
2+
| acrossLinkTargets.cpp:12:8:12:8 | Load: x | acrossLinkTargets.cpp:19:27:19:32 | Call: call to source |
13
| test.cpp:7:8:7:9 | Load: t1 | test.cpp:6:12:6:17 | Call: call to source |
24
| test.cpp:9:8:9:9 | Load: t1 | test.cpp:6:12:6:17 | Call: call to source |
35
| test.cpp:10:8:10:9 | Load: t2 | test.cpp:6:12:6:17 | Call: call to source |

0 commit comments

Comments
 (0)