Skip to content

Commit 3fb8758

Browse files
authored
Merge pull request #19886 from hvitved/rust/dataflow-caching
Rust: Cache `DataFlow::Node.{toString,getLocation}`
2 parents 2f208bd + b70aa80 commit 3fb8758

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@ private import codeql.rust.dataflow.FlowSummary
1717
private import Node as Node
1818
private import DataFlowImpl
1919
private import FlowSummaryImpl as FlowSummaryImpl
20+
private import codeql.rust.internal.CachedStages
2021

2122
/** An element, viewed as a node in a data flow graph. */
22-
abstract class NodePublic extends TNode {
23+
// It is important to not make this class `abstract`, as it otherwise results in
24+
// a needless charpred, which will result in recomputation of internal non-cached
25+
// predicates
26+
class NodePublic extends TNode {
2327
/** Gets the location of this node. */
28+
cached
2429
abstract Location getLocation();
2530

2631
/** Gets a textual representation of this node. */
32+
cached
2733
abstract string toString();
2834

2935
/**
@@ -55,17 +61,6 @@ abstract class Node extends NodePublic {
5561
CfgNode getCfgNode() { none() }
5662
}
5763

58-
/** A node type that is not implemented. */
59-
final class NaNode extends Node {
60-
NaNode() { none() }
61-
62-
override CfgScope getCfgScope() { none() }
63-
64-
override string toString() { result = "N/A" }
65-
66-
override Location getLocation() { none() }
67-
}
68-
6964
/** A data flow node used to model flow summaries. */
7065
class FlowSummaryNode extends Node, TFlowSummaryNode {
7166
FlowSummaryImpl::Private::SummaryNode getSummaryNode() { this = TFlowSummaryNode(result) }
@@ -108,6 +103,7 @@ class FlowSummaryNode extends Node, TFlowSummaryNode {
108103
}
109104

110105
override Location getLocation() {
106+
Stages::DataFlowStage::ref() and
111107
exists(this.getSummarizedCallable()) and
112108
result instanceof EmptyLocation
113109
or
@@ -116,7 +112,10 @@ class FlowSummaryNode extends Node, TFlowSummaryNode {
116112
result = this.getSinkElement().getLocation()
117113
}
118114

119-
override string toString() { result = this.getSummaryNode().toString() }
115+
override string toString() {
116+
Stages::DataFlowStage::ref() and
117+
result = this.getSummaryNode().toString()
118+
}
120119
}
121120

122121
/** A data flow node that corresponds directly to a CFG node for an AST node. */
@@ -440,9 +439,9 @@ private class CapturePostUpdateNode extends PostUpdateNode, CaptureNode {
440439
final override string toString() { result = PostUpdateNode.super.toString() }
441440
}
442441

443-
final class CastNode = NaNode;
444-
445-
private import codeql.rust.internal.CachedStages
442+
final class CastNode extends ExprNode {
443+
CastNode() { none() }
444+
}
446445

447446
cached
448447
newtype TNode =

rust/ql/lib/codeql/rust/internal/CachedStages.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ module Stages {
186186
predicate backref() {
187187
1 = 1
188188
or
189-
exists(Node n)
189+
exists(any(Node n).toString())
190+
or
191+
exists(any(Node n).getLocation())
190192
or
191193
RustTaintTracking::defaultAdditionalTaintStep(_, _, _)
192194
or

0 commit comments

Comments
 (0)