Skip to content

Commit c7b4e7c

Browse files
committed
Rust: Small improvements to data flow
The predicate `ssaDefAssigns` was definitely broken before
1 parent d9636b3 commit c7b4e7c

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

rust/ql/lib/codeql/rust/controlflow/CfgNodes.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ private import ControlFlowGraph
99
/** A CFG node that corresponds to an element in the AST. */
1010
class AstCfgNode extends CfgNode {
1111
AstNode node;
12-
12+
1313
AstCfgNode() { node = this.getAstNode() }
1414
}
1515

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,18 @@ private predicate propagatesValue(Expr e) {
183183
e instanceof MatchExpr
184184
}
185185

186+
/**
187+
* Gets a node that may execute last in `n`, and which, when it executes last,
188+
* will be the value of `n`.
189+
*/
190+
private ExprCfgNode getALastEvalNode(ExprCfgNode n) {
191+
propagatesValue(n.getExpr()) and result.getASuccessor() = n
192+
}
193+
186194
module LocalFlow {
187195
pragma[nomagic]
188196
predicate localFlowStepCommon(Node nodeFrom, Node nodeTo) {
189-
propagatesValue(nodeTo.(Node::ExprNode).asExpr()) and
190-
nodeFrom.getCfgNode().getASuccessor() = nodeTo.getCfgNode()
197+
nodeFrom.getCfgNode() = getALastEvalNode(nodeTo.getCfgNode())
191198
}
192199
}
193200

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,9 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
469469

470470
/** Holds if SSA definition `def` assigns `value` to the underlying variable. */
471471
predicate ssaDefAssigns(WriteDefinition def, Expr value) {
472-
exists(CfgNode node, BasicBlock bb, int i |
473-
def.definesAt(_, bb, i) and value.getAstNode() = node.getAstNode().(AssignmentExpr).getLhs()
472+
exists(BasicBlock bb, int i |
473+
def.definesAt(_, bb, i) and
474+
value.getAstNode() = bb.getNode(i).getAstNode().(AssignmentExpr).getRhs()
474475
)
475476
}
476477

0 commit comments

Comments
 (0)