Skip to content

Commit 334e222

Browse files
owen-mcCopilot
andcommitted
Go: Use DataFlowIntegration for local SSA flow
Use the shared DataFlowIntegration library for local SSA flow while preserving Go's field-sensitive barrier guards and closure flow, hiding synthetic SSA merge nodes from paths, and defining SsaNode.getBasicBlock. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ba05e20 commit 334e222

9 files changed

Lines changed: 355 additions & 28 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Local SSA data flow now uses the shared `DataFlowIntegration` library. `DataFlow::localFlowStep` and `TaintTracking::localTaintStep` may now step through intermediate SSA phi and synthetic merge-read nodes at control-flow joins rather than directly between uses across branches; `DataFlow::localFlow` reachability and global path explanations are unchanged.
5+
* `DataFlow::SsaNode.getBasicBlock()` now returns the basic block of the underlying SSA definition, and `IR::EvalInstruction.reads` no longer treats `select` receive assignment targets as variable reads.

‎go/ql/lib/semmle/go/dataflow/SsaImpl.qll‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ private module Internal {
125125
}
126126

127127
cached
128-
predicate ssaCachedStageRevRef() { Ssa::ssaCachedStageRevRef1() }
128+
predicate ssaCachedStageRevRef() {
129+
Ssa::ssaCachedStageRevRef1() and DataFlowIntegrationImpl::ssaCachedStageRevRef2()
130+
}
129131
}
130132
}
131133

@@ -157,6 +159,48 @@ private module PublicSsaInput implements Impl::SsaInputSig {
157159

158160
module Ssa = Impl::MakeSsa<PublicSsaInput>;
159161

162+
private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInputSig {
163+
private import codeql.util.Void
164+
165+
predicate ssaDefHasSource(Impl::WriteDefinition def) { def instanceof Ssa::SsaExplicitWrite }
166+
167+
predicate allowFlowIntoUncertainDef(Impl::UncertainWriteDefinition def) { any() }
168+
169+
// Keep Go's field-sensitive barrier guards rather than adding definition-level barriers.
170+
class GuardValue = Void;
171+
172+
class Guard extends Void {
173+
string toString() { none() }
174+
175+
predicate hasValueBranchEdge(BasicBlock bb1, BasicBlock bb2, GuardValue val) { none() }
176+
177+
predicate valueControlsBranchEdge(BasicBlock bb1, BasicBlock bb2, GuardValue val) { none() }
178+
}
179+
180+
predicate guardDirectlyControlsBlock(Guard guard, BasicBlock bb, GuardValue val) { none() }
181+
182+
predicate supportBarrierGuardsOnPhiEdges() { none() }
183+
}
184+
185+
private module DataFlowIntegrationImpl = Impl::DataFlowIntegration<DataFlowIntegrationInput>;
186+
187+
/** Provides shared SSA nodes and local flow steps for Go data-flow analysis. */
188+
cached
189+
module DataFlowIntegration {
190+
import DataFlowIntegrationImpl
191+
192+
/**
193+
* Holds if the value of `v` flows from `nodeFrom` to `nodeTo` in one local SSA step.
194+
*
195+
* `isUseStep` is `true` when `nodeFrom` is a (post-update) read node and
196+
* `nodeTo` is a read node or phi (read) node.
197+
*/
198+
cached
199+
predicate localFlowStep(SsaSourceVariable v, Node nodeFrom, Node nodeTo, boolean isUseStep) {
200+
DataFlowIntegrationImpl::localFlowStep(v, nodeFrom, nodeTo, isUseStep)
201+
}
202+
}
203+
160204
/** Gets the control-flow root containing `def`. */
161205
ControlFlow::Root getSsaDefinitionRoot(Ssa::SsaDefinition def) {
162206
result = def.getBasicBlock().getScope()

‎go/ql/lib/semmle/go/dataflow/internal/DataFlowNodes.qll‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ private import DataFlowPrivate
99
private import FlowSummaryImpl as FlowSummaryImpl
1010
private import semmle.go.dataflow.ExternalFlow
1111

12+
private module SsaFlow = SsaImpl::DataFlowIntegration;
13+
1214
cached
1315
private newtype TNode =
1416
MkInstructionNode(IR::Instruction insn) or
1517
MkSsaNode(SsaDefinition ssa) or
18+
MkSsaSynthReadNode(SsaFlow::SsaSynthReadNode ssa) or
1619
MkGlobalFunctionNode(Function f) or
1720
MkImplicitVarargsSlice(IR::EvalInstruction ins) {
1821
// We only use CallExprs with an EvalInstruction to guarantee reachability.
@@ -26,6 +29,55 @@ private newtype TNode =
2629
module Private {
2730
private import DataFlowDispatch
2831

32+
/** A synthetic SSA read at a control-flow merge. */
33+
class SsaSynthReadNode extends Node, MkSsaSynthReadNode {
34+
private SsaFlow::SsaSynthReadNode ssa;
35+
36+
SsaSynthReadNode() { this = MkSsaSynthReadNode(ssa) }
37+
38+
SsaFlow::SsaSynthReadNode getSsaNode() { result = ssa }
39+
40+
override ControlFlow::Root getRoot() { result = ssa.getBasicBlock().getScope() }
41+
42+
override BasicBlock getBasicBlock() { result = ssa.getBasicBlock() }
43+
44+
override Type getType() { result = ssa.getSourceVariable().getType() }
45+
46+
override string getNodeKind() { result = "SSA synthetic read" }
47+
48+
override string toString() { result = ssa.toString() }
49+
50+
override Location getLocation() { result = ssa.getLocation() }
51+
}
52+
53+
/** Gets the Go data-flow node corresponding to `n`. */
54+
Node getNodeFromSsaNode(SsaFlow::Node n) {
55+
result.(SsaNode).getDefinition() = n.(SsaFlow::SsaDefinitionNode).getDefinition()
56+
or
57+
result.(SsaSynthReadNode).getSsaNode() = n
58+
or
59+
exists(IR::Instruction insn, SsaSourceVariable v |
60+
n.(SsaFlow::ReadNode).readsAt(insn, v) and
61+
insn.reads(v) and
62+
result.asInstruction() = insn
63+
)
64+
or
65+
exists(IR::Instruction insn, SsaSourceVariable v |
66+
n.(SsaFlow::PostUpdateReadNode).readsAt(insn, v) and
67+
insn.reads(v) and
68+
result.(PostUpdateNode).getPreUpdateNode().asInstruction() = insn
69+
)
70+
or
71+
exists(IR::Instruction rhs |
72+
rhs =
73+
n.(SsaFlow::WriteDefSourceNode).getDefinition().(SsaExplicitWrite).getDefinition().getRhs()
74+
|
75+
result.asInstruction() = rhs
76+
or
77+
result.(PostUpdateNode).getPreUpdateNode().asInstruction() = rhs
78+
)
79+
}
80+
2981
/** Gets the callable in which this node occurs. */
3082
DataFlowCallable nodeGetEnclosingCallable(Node n) {
3183
result.asCallable() = n.getEnclosingCallable()
@@ -309,6 +361,8 @@ module Public {
309361

310362
override ControlFlow::Root getRoot() { result = SsaImpl::getSsaDefinitionRoot(ssa) }
311363

364+
override BasicBlock getBasicBlock() { result = ssa.getBasicBlock() }
365+
312366
override Type getType() { result = ssa.getSourceVariable().getType() }
313367

314368
override string getNodeKind() { result = "SSA variable " + ssa.getSourceVariable().getName() }

‎go/ql/lib/semmle/go/dataflow/internal/DataFlowPrivate.qll‎

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,10 @@ predicate basicLocalFlowStep(Node nodeFrom, Node nodeTo) {
7070
else nodeTo.asInstruction() = evalAssert
7171
)
7272
or
73-
// Instruction -> SSA defn
74-
exists(IR::Instruction pred, SsaExplicitWrite succ |
75-
succ.getDefinition().getRhs() = pred and
76-
(
77-
nodeFrom = instructionNode(pred) or
78-
nodeFrom.(PostUpdateNode).getPreUpdateNode() = instructionNode(pred)
79-
) and
80-
nodeTo = ssaNode(succ)
81-
)
82-
or
83-
// SSA defn -> first SSA use
84-
exists(SsaDefinition pred, IR::Instruction succ | SsaImpl::firstUse(pred, succ) |
85-
(pred instanceof SsaExplicitWrite or pred instanceof SsaUncertainWrite) and
86-
nodeFrom = ssaNode(pred) and
87-
nodeTo = instructionNode(succ)
88-
)
89-
or
90-
// SSA use -> successive SSA use
91-
// Note this case includes Phi node traversal
92-
exists(IR::Instruction pred, IR::Instruction succ | succ = getAnAdjacentUse(pred) |
93-
(
94-
nodeFrom = instructionNode(pred) or
95-
nodeFrom.(PostUpdateNode).getPreUpdateNode() = instructionNode(pred)
96-
) and
97-
nodeTo = instructionNode(succ) and
98-
nodeTo != nodeFrom
73+
exists(SsaImpl::DataFlowIntegration::Node pred, SsaImpl::DataFlowIntegration::Node succ |
74+
SsaImpl::DataFlowIntegration::localFlowStep(_, pred, succ, _) and
75+
nodeFrom = getNodeFromSsaNode(pred) and
76+
nodeTo = getNodeFromSsaNode(succ)
9977
)
10078
}
10179

@@ -448,7 +426,13 @@ Node getArgument(CallNode c, int i) {
448426
}
449427

450428
/** Holds if `n` should be hidden from path explanations. */
451-
predicate nodeIsHidden(Node n) { n instanceof FlowSummaryNode }
429+
predicate nodeIsHidden(Node n) {
430+
n instanceof FlowSummaryNode
431+
or
432+
n instanceof SsaSynthReadNode
433+
or
434+
n.(SsaNode).getDefinition() instanceof SsaImpl::PhiNode
435+
}
452436

453437
class LambdaCallKind = Unit;
454438

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
testFailures
2+
unscopedSsaNode
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import go
2+
import utils.test.InlineFlowTest
3+
private import semmle.go.dataflow.internal.DataFlowNodes::Private as Nodes
4+
private import semmle.go.dataflow.internal.DataFlowPrivate as PrivateFlow
5+
6+
predicate isBad(DataFlow::Node g, Expr e, boolean branch) {
7+
g.(DataFlow::CallNode).getTarget().getName() = "isBad" and
8+
e = g.(DataFlow::CallNode).getArgument(0).asExpr() and
9+
branch = false
10+
}
11+
12+
module Config implements DataFlow::ConfigSig {
13+
predicate isSource = DefaultFlowConfig::isSource/1;
14+
15+
predicate isSink = DefaultFlowConfig::isSink/1;
16+
17+
predicate fieldFlowBranchLimit = DefaultFlowConfig::fieldFlowBranchLimit/0;
18+
19+
predicate isBarrier(DataFlow::Node node) {
20+
node = DataFlow::BarrierGuard<isBad/3>::getABarrierNode()
21+
}
22+
}
23+
24+
import ValueFlowTest<Config>
25+
26+
query predicate unscopedSsaNode(DataFlow::Node node) {
27+
(node instanceof DataFlow::SsaNode or node instanceof Nodes::SsaSynthReadNode) and
28+
not exists(PrivateFlow::NodeRegion region |
29+
region.contains(node) and
30+
region.(BasicBlock).getScope() = node.getRoot()
31+
)
32+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| neverSkip | 8 | 0 | 0 |
2+
| stateful | 8 | 0 | 0 |
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import go
2+
import semmle.go.dataflow.internal.DataFlowNodes as Nodes
3+
import semmle.go.dataflow.SsaImpl
4+
5+
private predicate sourceNode(DataFlow::Node node) {
6+
node.(DataFlow::CallNode).getTarget().getName() = "source" and
7+
node.getEnclosingCallable().getName() = ["readMerge", "writeMerge", "loop"]
8+
}
9+
10+
private predicate sinkNode(DataFlow::Node node) {
11+
node = any(DataFlow::CallNode call | call.getTarget().getName() = "sink").getArgument(0) and
12+
node.getEnclosingCallable().getName() = ["readMerge", "writeMerge", "loop"]
13+
}
14+
15+
module NeverSkipConfig implements DataFlow::ConfigSig {
16+
predicate isSource(DataFlow::Node node) { sourceNode(node) }
17+
18+
predicate isSink(DataFlow::Node node) { sinkNode(node) }
19+
20+
predicate neverSkip(DataFlow::Node node) { any() }
21+
}
22+
23+
module NeverSkipFlow = DataFlow::Global<NeverSkipConfig>;
24+
25+
module StatefulConfig implements DataFlow::StateConfigSig {
26+
class FlowState extends string {
27+
FlowState() { this = ["initial", "marked"] }
28+
}
29+
30+
predicate isSource(DataFlow::Node node, FlowState state) {
31+
sourceNode(node) and state = "initial"
32+
}
33+
34+
predicate isSink(DataFlow::Node node, FlowState state) { sinkNode(node) and state = "marked" }
35+
36+
predicate isAdditionalFlowStep(
37+
DataFlow::Node node1, FlowState state1, DataFlow::Node node2, FlowState state2
38+
) {
39+
node1 = node2 and
40+
node1.getType().getUnderlyingType() instanceof StringType and
41+
state1 = "initial" and
42+
state2 = "marked"
43+
}
44+
}
45+
46+
module StatefulFlow = DataFlow::GlobalWithState<StatefulConfig>;
47+
48+
private predicate pathNode(string mode, DataFlow::Node node) {
49+
exists(NeverSkipFlow::PathNode p |
50+
(
51+
NeverSkipFlow::PathGraph::edges(p, _, _, _) or
52+
NeverSkipFlow::PathGraph::edges(_, p, _, _)
53+
) and
54+
mode = "neverSkip" and
55+
node = p.getNode()
56+
)
57+
or
58+
exists(StatefulFlow::PathNode p |
59+
(
60+
StatefulFlow::PathGraph::edges(p, _, _, _) or
61+
StatefulFlow::PathGraph::edges(_, p, _, _)
62+
) and
63+
mode = "stateful" and
64+
node = p.getNode()
65+
)
66+
}
67+
68+
private predicate flow(string mode, DataFlow::Node source, DataFlow::Node sink) {
69+
mode = "neverSkip" and NeverSkipFlow::flow(source, sink)
70+
or
71+
mode = "stateful" and StatefulFlow::flow(source, sink)
72+
}
73+
74+
from string mode
75+
where mode = ["neverSkip", "stateful"]
76+
select mode, count(DataFlow::Node source, DataFlow::Node sink | flow(mode, source, sink)) as flows,
77+
count(DataFlow::Node n | pathNode(mode, n) and n instanceof Nodes::Private::SsaSynthReadNode) as syntheticReads,
78+
count(DataFlow::SsaNode n | pathNode(mode, n) and n.getDefinition() instanceof PhiNode) as phis

0 commit comments

Comments
 (0)