Skip to content

Commit 82076ee

Browse files
committed
Rust: Propagate data flow through a few expression types
1 parent 2bab29d commit 82076ee

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

rust/ql/lib/codeql/rust/dataflow/internal/DataFlowImpl.qll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,31 @@ module SsaFlow {
146146
}
147147
}
148148

149+
/**
150+
* Holds for expressions `e` that evaluate to the value of any last (in
151+
* evaluation order) subexpressions within it. E.g., expressions that propagate
152+
* a values from a subexpression.
153+
*
154+
* For instance, the predicate holds for if expressions as `if b { e1 } else {
155+
* e2 }` evalates to the value of one of the subexpressions `e1` or `e2`.
156+
*/
157+
predicate propagatesValue(Expr e) {
158+
e instanceof IfExpr or
159+
e instanceof LoopExpr or
160+
e instanceof ReturnExpr or
161+
e instanceof BreakExpr or
162+
e.(BlockExpr).getStmtList().hasTailExpr() or
163+
e instanceof MatchExpr
164+
}
165+
166+
module LocalFlow {
167+
pragma[nomagic]
168+
predicate localFlowStepCommon(Node nodeFrom, Node nodeTo) {
169+
propagatesValue(nodeTo.(Node::ExprNode).asExpr()) and
170+
nodeFrom.getCfgNode().getASuccessor() = nodeTo.getCfgNode()
171+
}
172+
}
173+
149174
module RustDataFlow implements InputSig<Location> {
150175
/**
151176
* An element, viewed as a node in a data flow graph. Either an expression
@@ -359,6 +384,8 @@ private module Cached {
359384
/** This is the local flow predicate that is exposed. */
360385
cached
361386
predicate localFlowStepImpl(Node::Node nodeFrom, Node::Node nodeTo) {
387+
LocalFlow::localFlowStepCommon(nodeFrom, nodeTo)
388+
or
362389
SsaFlow::localFlowStep(_, nodeFrom, nodeTo, _)
363390
}
364391
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
| main.rs:9:21:11:5 | BlockExpr | main.rs:9:13:13:5 | IfExpr |
2+
| main.rs:10:9:10:9 | a | main.rs:9:21:11:5 | BlockExpr |
3+
| main.rs:11:12:13:5 | BlockExpr | main.rs:9:13:13:5 | IfExpr |
4+
| main.rs:12:9:12:9 | b | main.rs:11:12:13:5 | BlockExpr |
5+
| main.rs:14:5:14:5 | c | main.rs:6:37:15:1 | BlockExpr |
6+
| main.rs:20:9:20:15 | BreakExpr | main.rs:19:13:21:5 | LoopExpr |
7+
| main.rs:20:15:20:15 | a | main.rs:20:9:20:15 | BreakExpr |
8+
| main.rs:22:5:22:5 | b | main.rs:17:29:23:1 | BlockExpr |
9+
| main.rs:26:5:29:5 | MatchExpr | main.rs:25:60:30:1 | BlockExpr |
10+
| main.rs:27:20:27:20 | a | main.rs:26:5:29:5 | MatchExpr |
11+
| main.rs:28:17:28:17 | b | main.rs:26:5:29:5 | MatchExpr |

0 commit comments

Comments
 (0)