Skip to content

Commit 4df4a1e

Browse files
authored
Merge pull request #17863 from aschackmull/shared/universal-flow
Shared: Add a Universal Flow library and refactor TypeFlow to use it.
2 parents f8058e4 + bae6187 commit 4df4a1e

File tree

5 files changed

+394
-245
lines changed

5 files changed

+394
-245
lines changed

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private module Input implements TypeFlowInput<Location> {
159159
)
160160
}
161161

162-
predicate joinStep(TypeFlowNode n1, TypeFlowNode n2) {
162+
predicate step(TypeFlowNode n1, TypeFlowNode n2) {
163163
// instruction -> phi
164164
getAnUltimateLocalDefinition(n2.asInstruction()) = n1.asInstruction()
165165
or
@@ -179,6 +179,8 @@ private module Input implements TypeFlowInput<Location> {
179179
n1.asInstruction() = arg and
180180
n2.asInstruction() = p
181181
)
182+
or
183+
instructionStep(n1.asInstruction(), n2.asInstruction())
182184
}
183185

184186
/**
@@ -199,10 +201,6 @@ private module Input implements TypeFlowInput<Location> {
199201
i2.(PointerArithmeticInstruction).getLeft() = i1
200202
}
201203

202-
predicate step(TypeFlowNode n1, TypeFlowNode n2) {
203-
instructionStep(n1.asInstruction(), n2.asInstruction())
204-
}
205-
206204
predicate isNullValue(TypeFlowNode n) { n.isNullValue() }
207205

208206
private newtype TType =
@@ -245,11 +243,7 @@ private module Input implements TypeFlowInput<Location> {
245243

246244
pragma[nomagic]
247245
private predicate upcastCand(TypeFlowNode n, Type t1, Type t2) {
248-
exists(TypeFlowNode next |
249-
step(n, next)
250-
or
251-
joinStep(n, next)
252-
|
246+
exists(TypeFlowNode next | step(n, next) |
253247
n.getType() = t1 and
254248
next.getType() = t2 and
255249
t1 != t2

java/ql/lib/semmle/code/java/dataflow/TypeFlow.qll

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ private module Input implements TypeFlowInput<Location> {
8484
}
8585

8686
/**
87-
* Holds if data can flow from `n1` to `n2` in one step, and `n1` is not
88-
* necessarily functionally determined by `n2`.
87+
* Holds if data can flow from `n1` to `n2` in one step.
88+
*
89+
* For a given `n2`, this predicate must include all possible `n1` that can flow to `n2`.
8990
*/
90-
predicate joinStep(TypeFlowNode n1, TypeFlowNode n2) {
91+
predicate step(TypeFlowNode n1, TypeFlowNode n2) {
9192
n2.asExpr().(ChooseExpr).getAResultExpr() = n1.asExpr()
9293
or
9394
exists(Field f, Expr e |
@@ -112,13 +113,7 @@ private module Input implements TypeFlowInput<Location> {
112113
// skip trivial recursion
113114
not arg = n2.asSsa().getAUse()
114115
)
115-
}
116-
117-
/**
118-
* Holds if data can flow from `n1` to `n2` in one step, and `n1` is
119-
* functionally determined by `n2`.
120-
*/
121-
predicate step(TypeFlowNode n1, TypeFlowNode n2) {
116+
or
122117
n2.asExpr() = n1.asField().getAnAccess()
123118
or
124119
n2.asExpr() = n1.asSsa().getAUse()
@@ -143,7 +138,7 @@ private module Input implements TypeFlowInput<Location> {
143138
exists(LocalVariableDeclExpr decl |
144139
n.asSsa().(BaseSsaUpdate).getDefiningExpr() = decl and
145140
not decl.hasImplicitInit() and
146-
not exists(decl.getInit())
141+
not exists(decl.getInitOrPatternSource())
147142
)
148143
}
149144

@@ -169,7 +164,7 @@ private module Input implements TypeFlowInput<Location> {
169164
*/
170165
pragma[nomagic]
171166
private predicate upcastCand(TypeFlowNode n, RefType t1, RefType t1e, RefType t2, RefType t2e) {
172-
exists(TypeFlowNode next | step(n, next) or joinStep(n, next) |
167+
exists(TypeFlowNode next | step(n, next) |
173168
n.getType() = t1 and
174169
next.getType() = t2 and
175170
t1.getErasure() = t1e and

shared/typeflow/codeql/typeflow/TypeFlow.qll

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,9 @@ signature module TypeFlowInput<LocationSig Location> {
2828
}
2929

3030
/**
31-
* Holds if data can flow from `n1` to `n2` in one step, and `n1` is not
32-
* necessarily functionally determined by `n2`.
33-
*/
34-
predicate joinStep(TypeFlowNode n1, TypeFlowNode n2);
35-
36-
/**
37-
* Holds if data can flow from `n1` to `n2` in one step, and `n1` is
38-
* functionally determined by `n2`.
31+
* Holds if data can flow from `n1` to `n2` in one step.
32+
*
33+
* For a given `n2`, this predicate must include all possible `n1` that can flow to `n2`.
3934
*/
4035
predicate step(TypeFlowNode n1, TypeFlowNode n2);
4136

0 commit comments

Comments
 (0)