Skip to content

Commit 01141cc

Browse files
committed
Rust: Integrate SSA into data flow
1 parent a36095d commit 01141cc

File tree

3 files changed

+165
-13
lines changed

3 files changed

+165
-13
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Provides subclasses of `CfgNode` that represents different types of nodes in
3+
* the control flow graph.
4+
*/
5+
6+
private import rust
7+
private import ControlFlowGraph
8+
9+
/** A CFG node that corresponds to an element in the AST. */
10+
class AstCfgNode extends CfgNode {
11+
AstCfgNode() { exists(this.getAstNode()) }
12+
}
13+
14+
/** A CFG node that corresponds to an expression in the AST. */
15+
class ExprCfgNode extends AstCfgNode {
16+
ExprCfgNode() { this.getAstNode() instanceof Expr }
17+
18+
/** Gets the underlying expression. */
19+
Expr getExpr() { result = this.getAstNode() }
20+
}

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

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ private import codeql.util.Unit
77
private import codeql.dataflow.DataFlow
88
private import codeql.dataflow.internal.DataFlowImpl
99
private import rust
10+
private import SsaImpl as SsaImpl
1011
private import codeql.rust.controlflow.ControlFlowGraph
12+
private import codeql.rust.controlflow.CfgNodes
1113
private import codeql.rust.dataflow.Ssa
1214

1315
module Node {
@@ -52,18 +54,43 @@ module Node {
5254
override Location getLocation() { none() }
5355
}
5456

57+
/**
58+
* A node in the data flow graph that corresponds to an expression in the
59+
* AST.
60+
*
61+
* Note that because of control-flow splitting, one `Expr` may correspond
62+
* to multiple `ExprNode`s, just like it may correspond to multiple
63+
* `ControlFlow::Node`s.
64+
*/
65+
final class ExprNode extends Node, TExprNode {
66+
ExprCfgNode n;
67+
68+
ExprNode() { this = TExprNode(n) }
69+
70+
override Location getLocation() { result = n.getExpr().getLocation() }
71+
72+
override string toString() { result = n.getExpr().toString() }
73+
74+
override Expr asExpr() { result = n.getExpr() }
75+
76+
override CfgNode getCfgNode() { result = n }
77+
}
78+
5579
/**
5680
* The value of a parameter at function entry, viewed as a node in a data
5781
* flow graph.
5882
*/
59-
final class ParameterNode extends Node {
60-
Param param;
83+
final class ParameterNode extends Node, TParameterNode {
84+
Param parameter;
85+
86+
ParameterNode() { this = TParameterNode(parameter) }
6187

62-
ParameterNode() { this = TSourceParameterNode(param) }
88+
override Location getLocation() { result = parameter.getLocation() }
6389

64-
override Location getLocation() { result = param.getLocation() }
90+
override string toString() { result = parameter.toString() }
6591

66-
override string toString() { result = param.toString() }
92+
/** Gets the parameter in the AST that this node corresponds to. */
93+
Param getParameter() { result = parameter }
6794
}
6895

6996
final class ArgumentNode = NaNode;
@@ -93,6 +120,32 @@ module Node {
93120
final class CastNode = NaNode;
94121
}
95122

123+
final class Node = Node::Node;
124+
125+
/** Provides logic related to SSA. */
126+
module SsaFlow {
127+
private module Impl = SsaImpl::DataFlowIntegration;
128+
129+
private Node::ParameterNode toParameterNode(Param p) { result = TParameterNode(p) }
130+
131+
/** Converts a control flow node into an SSA control flow node. */
132+
Impl::Node asNode(Node n) {
133+
n = TSsaNode(result)
134+
or
135+
result.(Impl::ExprNode).getExpr() = n.(Node::ExprNode).getCfgNode()
136+
or
137+
n = toParameterNode(result.(Impl::ParameterNode).getParameter())
138+
}
139+
140+
predicate localFlowStep(SsaImpl::DefinitionExt def, Node nodeFrom, Node nodeTo, boolean isUseStep) {
141+
Impl::localFlowStep(def, asNode(nodeFrom), asNode(nodeTo), isUseStep)
142+
}
143+
144+
predicate localMustFlowStep(SsaImpl::DefinitionExt def, Node nodeFrom, Node nodeTo) {
145+
Impl::localMustFlowStep(def, asNode(nodeFrom), asNode(nodeTo))
146+
}
147+
}
148+
96149
module RustDataFlow implements InputSig<Location> {
97150
/**
98151
* An element, viewed as a node in a data flow graph. Either an expression
@@ -122,10 +175,10 @@ module RustDataFlow implements InputSig<Location> {
122175

123176
predicate nodeIsHidden(Node node) { none() }
124177

125-
class DataFlowExpr = Void;
178+
class DataFlowExpr = ExprCfgNode;
126179

127180
/** Gets the node corresponding to `e`. */
128-
Node exprNode(DataFlowExpr e) { none() }
181+
Node exprNode(DataFlowExpr e) { result.getCfgNode() = e }
129182

130183
final class DataFlowCall extends TNormalCall {
131184
private CallExpr c;
@@ -191,7 +244,7 @@ module RustDataFlow implements InputSig<Location> {
191244
* Holds if there is a simple local flow step from `node1` to `node2`. These
192245
* are the value-preserving intra-callable flow steps.
193246
*/
194-
predicate simpleLocalFlowStep(Node node1, Node node2, string model) { none() }
247+
predicate simpleLocalFlowStep(Node nodeFrom, Node nodeTo, string model) { none() }
195248

196249
/**
197250
* Holds if data can flow from `node1` to `node2` through a non-local step
@@ -256,7 +309,9 @@ module RustDataFlow implements InputSig<Location> {
256309
* `node2` must be visited along a flow path, then any type known for `node2`
257310
* must also apply to `node1`.
258311
*/
259-
predicate localMustFlowStep(Node node1, Node node2) { none() }
312+
predicate localMustFlowStep(Node node1, Node node2) {
313+
SsaFlow::localMustFlowStep(_, node1, node2)
314+
}
260315

261316
class LambdaCallKind = Void;
262317

@@ -267,7 +322,7 @@ module RustDataFlow implements InputSig<Location> {
267322
/** Holds if `call` is a lambda call of kind `kind` where `receiver` is the lambda expression. */
268323
predicate lambdaCall(DataFlowCall call, LambdaCallKind kind, Node receiver) { none() }
269324

270-
/** Extra data-flow steps needed for lambda flow analysis. */
325+
/** Extra data flow steps needed for lambda flow analysis. */
271326
predicate additionalLambdaFlowStep(Node nodeFrom, Node nodeTo, boolean preservesValue) { none() }
272327

273328
predicate knownSourceModel(Node source, string model) { none() }
@@ -286,8 +341,9 @@ cached
286341
private module Cached {
287342
cached
288343
newtype TNode =
289-
TExprNode(CfgNode n, Expr e) { n.getAstNode() = e } or
290-
TSourceParameterNode(Param param)
344+
TExprNode(ExprCfgNode n) or
345+
TParameterNode(Param p) or
346+
TSsaNode(SsaImpl::DataFlowIntegration::SsaNode node)
291347

292348
cached
293349
newtype TDataFlowCall = TNormalCall(CallExpr c)
@@ -302,7 +358,9 @@ private module Cached {
302358

303359
/** This is the local flow predicate that is exposed. */
304360
cached
305-
predicate localFlowStepImpl(Node::Node nodeFrom, Node::Node nodeTo) { none() }
361+
predicate localFlowStepImpl(Node::Node nodeFrom, Node::Node nodeTo) {
362+
SsaFlow::localFlowStep(_, nodeFrom, nodeTo, _)
363+
}
306364
}
307365

308366
import Cached

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ private import rust
22
private import codeql.rust.controlflow.BasicBlocks as BasicBlocks
33
private import BasicBlocks
44
private import codeql.rust.controlflow.ControlFlowGraph as Cfg
5+
private import codeql.rust.controlflow.CfgNodes as CfgNodes
56
private import Cfg
67
private import codeql.rust.controlflow.internal.ControlFlowGraphImpl as ControlFlowGraphImpl
78
private import codeql.ssa.Ssa as SsaImplCommon
@@ -395,6 +396,38 @@ private module Cached {
395396
Definition uncertainWriteDefinitionInput(UncertainWriteDefinition def) {
396397
Impl::uncertainWriteDefinitionInput(def, result)
397398
}
399+
400+
cached
401+
module DataFlowIntegration {
402+
import DataFlowIntegrationImpl
403+
404+
cached
405+
predicate localFlowStep(DefinitionExt def, Node nodeFrom, Node nodeTo, boolean isUseStep) {
406+
DataFlowIntegrationImpl::localFlowStep(def, nodeFrom, nodeTo, isUseStep)
407+
}
408+
409+
cached
410+
predicate localMustFlowStep(DefinitionExt def, Node nodeFrom, Node nodeTo) {
411+
DataFlowIntegrationImpl::localMustFlowStep(def, nodeFrom, nodeTo)
412+
}
413+
414+
signature predicate guardChecksSig(CfgNodes::AstCfgNode g, Cfg::CfgNode e, boolean branch);
415+
416+
cached // nothing is actually cached
417+
module BarrierGuard<guardChecksSig/3 guardChecks> {
418+
private predicate guardChecksAdjTypes(
419+
DataFlowIntegrationInput::Guard g, DataFlowIntegrationInput::Expr e, boolean branch
420+
) {
421+
guardChecks(g, e, branch)
422+
}
423+
424+
private Node getABarrierNodeImpl() {
425+
result = DataFlowIntegrationImpl::BarrierGuard<guardChecksAdjTypes/3>::getABarrierNode()
426+
}
427+
428+
predicate getABarrierNode = getABarrierNodeImpl/0;
429+
}
430+
}
398431
}
399432

400433
import Cached
@@ -426,3 +459,44 @@ class PhiReadNode extends DefinitionExt, Impl::PhiReadNode {
426459

427460
override Location getLocation() { result = Impl::PhiReadNode.super.getLocation() }
428461
}
462+
463+
private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInputSig {
464+
class Expr extends CfgNodes::ExprCfgNode {
465+
predicate hasCfgNode(SsaInput::BasicBlock bb, int i) { this = bb.getNode(i) }
466+
}
467+
468+
Expr getARead(Definition def) { result = Cached::getARead(def) }
469+
470+
/** Holds if SSA definition `def` assigns `value` to the underlying variable. */
471+
predicate ssaDefAssigns(WriteDefinition def, Expr value) { none() }
472+
473+
class Parameter = Param;
474+
475+
/** Holds if SSA definition `def` initializes parameter `p` at function entry. */
476+
predicate ssaDefInitializesParam(WriteDefinition def, Parameter p) {
477+
exists(BasicBlock bb, int i | bb.getNode(i).getAstNode() = p and def.definesAt(_, bb, i))
478+
}
479+
480+
class Guard extends CfgNodes::AstCfgNode {
481+
predicate hasCfgNode(SsaInput::BasicBlock bb, int i) { this = bb.getNode(i) }
482+
}
483+
484+
/** Holds if the guard `guard` controls block `bb` upon evaluating to `branch`. */
485+
predicate guardControlsBlock(Guard guard, SsaInput::BasicBlock bb, boolean branch) {
486+
exists(ConditionBlock conditionBlock, ConditionalSuccessor s |
487+
guard = conditionBlock.getLastNode() and
488+
s.getValue() = branch and
489+
conditionBlock.controls(bb, s)
490+
)
491+
}
492+
493+
/** Gets an immediate conditional successor of basic block `bb`, if any. */
494+
SsaInput::BasicBlock getAConditionalBasicBlockSuccessor(SsaInput::BasicBlock bb, boolean branch) {
495+
exists(Cfg::ConditionalSuccessor s |
496+
result = bb.getASuccessor(s) and
497+
s.getValue() = branch
498+
)
499+
}
500+
}
501+
502+
private module DataFlowIntegrationImpl = Impl::DataFlowIntegration<DataFlowIntegrationInput>;

0 commit comments

Comments
 (0)