Skip to content

Commit c5acdaa

Browse files
committed
unified: Use proper CFG nodes for assignments in data flow
1 parent 43298bf commit c5acdaa

3 files changed

Lines changed: 16 additions & 13 deletions

File tree

unified/ql/lib/codeql/unified/internal/dataflow/DataFlowNode.qll

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ private import unified
22
private import AllDataFlow
33
private import codeql.unified.internal.ExprPositions
44

5-
private predicate hasPostUpdate(Expr expr) {
5+
private predicate hasIncomingValueAtCfgNode(Expr expr, ControlFlowNode cfgNode) {
6+
exists(AstNode declOrAssignment |
7+
hasIncomingValue(expr, declOrAssignment) and
8+
cfgNode.injects(declOrAssignment)
9+
)
10+
}
11+
12+
private predicate hasPostUpdate(Expr expr, ControlFlowNode cfgNode) {
613
exists(MemberAccessExpr member |
7-
(hasIncomingValue(member, _) or hasPostUpdate(member)) and
14+
(hasIncomingValueAtCfgNode(member, cfgNode) or hasPostUpdate(member, cfgNode)) and
815
expr = member.getBase()
916
)
1017
}
@@ -16,28 +23,27 @@ predicate performsVariableAccess(
1623
Expr expr, LocalVariable var, VariableRefKind kind, ControlFlowNode cfgNode
1724
) {
1825
exists(LocalVariableAccess access | var = access.getLocalVariable() and expr = access |
19-
hasResultValue(access) and kind.isRead() and cfgNode.isAfter(expr)
26+
hasResultValue(access) and kind.isRead() and cfgNode.asExpr() = expr
2027
or
21-
hasIncomingValue(access, _) and kind.isWrite() and cfgNode.asExpr() = expr // TODO: use more precise CFG node
28+
hasIncomingValueAtCfgNode(access, cfgNode) and kind.isWrite()
2229
or
23-
hasPostUpdate(access) and kind.isPostUpdate() and cfgNode.asExpr() = expr // TODO: use more precise CFG node
30+
hasPostUpdate(access, cfgNode) and kind.isPostUpdate()
2431
)
2532
or
2633
exists(UnqualifiedMemberAccess access |
2734
access.isInstanceAccess() and var = access.getImplicitQualifierVariable() and expr = access
2835
|
2936
kind.isRead() and cfgNode.isBefore(access)
3037
or
31-
(hasIncomingValue(access, _) or hasPostUpdate(access)) and
32-
kind.isPostUpdate() and
33-
cfgNode.asExpr() = access // TODO: use more precise CFG node
38+
(hasIncomingValueAtCfgNode(access, cfgNode) or hasPostUpdate(access, cfgNode)) and
39+
kind.isPostUpdate()
3440
)
3541
}
3642

3743
newtype TDataFlowNode =
3844
TValueNode(Expr expr) { hasResultValue(expr) or hasIncomingValue(expr, _) } or
3945
TStrictlyIncomingValue(Expr expr) { hasResultValue(expr) and hasIncomingValue(expr, _) } or
40-
TExprPostUpdateNode(Expr expr) { hasPostUpdate(expr) } or
46+
TExprPostUpdateNode(Expr expr) { hasPostUpdate(expr, _) } or
4147
TLocalVariableRefNode(Expr expr, LocalVariable var, VariableRefKind kind) {
4248
performsVariableAccess(expr, var, kind, _)
4349
} or

unified/ql/test/library-tests/dataflow/test.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ edges
9595
| test.swift:120:12:120:12 | b | test.swift:126:10:126:10 | b | provenance | |
9696
| test.swift:120:17:120:21 | tuple [0] | test.swift:120:9:120:13 | TupleExpr [0] | provenance | |
9797
| test.swift:120:17:120:21 | tuple [1] | test.swift:120:9:120:13 | TupleExpr [1] | provenance | |
98-
| test.swift:131:5:131:5 | a | test.swift:131:14:131:14 | a | provenance | |
9998
| test.swift:131:5:131:5 | a | test.swift:132:10:132:10 | a | provenance | |
10099
| test.swift:131:19:131:33 | CallExpr | test.swift:131:5:131:5 | a | provenance | |
101100
nodes
@@ -224,7 +223,6 @@ nodes
224223
| test.swift:125:10:125:10 | a | semmle.label | a |
225224
| test.swift:126:10:126:10 | b | semmle.label | b |
226225
| test.swift:131:5:131:5 | a | semmle.label | a |
227-
| test.swift:131:14:131:14 | a | semmle.label | a |
228226
| test.swift:131:19:131:33 | CallExpr | semmle.label | CallExpr |
229227
| test.swift:132:10:132:10 | a | semmle.label | a |
230228
subpaths
@@ -261,5 +259,4 @@ testFailures
261259
| test.swift:112:10:112:16 | MemberAccessExpr | test.swift:107:19:107:33 | CallExpr | test.swift:112:10:112:16 | MemberAccessExpr | $@ | test.swift:107:19:107:33 | CallExpr | CallExpr |
262260
| test.swift:125:10:125:10 | a | test.swift:117:18:117:33 | CallExpr | test.swift:125:10:125:10 | a | $@ | test.swift:117:18:117:33 | CallExpr | CallExpr |
263261
| test.swift:126:10:126:10 | b | test.swift:117:35:117:49 | CallExpr | test.swift:126:10:126:10 | b | $@ | test.swift:117:35:117:49 | CallExpr | CallExpr |
264-
| test.swift:131:14:131:14 | a | test.swift:131:19:131:33 | CallExpr | test.swift:131:14:131:14 | a | $@ | test.swift:131:19:131:33 | CallExpr | CallExpr |
265262
| test.swift:132:10:132:10 | a | test.swift:131:19:131:33 | CallExpr | test.swift:132:10:132:10 | a | $@ | test.swift:131:19:131:33 | CallExpr | CallExpr |

unified/ql/test/library-tests/dataflow/test.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func t13() {
128128

129129
func t14() {
130130
var a = "safe";
131-
a = sink(a) + source("t14.1"); // $ SPURIOUS: hasTaintFlow=t14.1
131+
a = sink(a) + source("t14.1");
132132
sink(a); // $ hasTaintFlow=t14.1
133133
}
134134

0 commit comments

Comments
 (0)