Skip to content

Commit f3daf4e

Browse files
committed
Use DataFlowIntegration for local SSA flow
We do not use closure flow or barrier guards.
1 parent a25b55c commit f3daf4e

6 files changed

Lines changed: 242 additions & 27 deletions

File tree

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

Lines changed: 38 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,41 @@ 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+
cached
188+
module DataFlowIntegration {
189+
import DataFlowIntegrationImpl
190+
191+
cached
192+
predicate localFlowStep(SsaSourceVariable v, Node nodeFrom, Node nodeTo, boolean isUseStep) {
193+
DataFlowIntegrationImpl::localFlowStep(v, nodeFrom, nodeTo, isUseStep)
194+
}
195+
}
196+
160197
/** Gets the control-flow root containing `def`. */
161198
ControlFlow::Root getSsaDefinitionRoot(Ssa::SsaDefinition def) {
162199
result = def.getBasicBlock().getScope()

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

Lines changed: 52 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()

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

Lines changed: 4 additions & 26 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

‎go/ql/test/library-tests/semmle/go/dataflow/SSADataFlowIntegration/Flows.expected‎

Whitespace-only changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import go
2+
import utils.test.InlineFlowTest
3+
4+
predicate isBad(DataFlow::Node g, Expr e, boolean branch) {
5+
g.(DataFlow::CallNode).getTarget().getName() = "isBad" and
6+
e = g.(DataFlow::CallNode).getArgument(0).asExpr() and
7+
branch = false
8+
}
9+
10+
module Config implements DataFlow::ConfigSig {
11+
predicate isSource = DefaultFlowConfig::isSource/1;
12+
13+
predicate isSink = DefaultFlowConfig::isSink/1;
14+
15+
predicate fieldFlowBranchLimit = DefaultFlowConfig::fieldFlowBranchLimit/0;
16+
17+
predicate isBarrier(DataFlow::Node node) {
18+
node = DataFlow::BarrierGuard<isBad/3>::getABarrierNode()
19+
}
20+
}
21+
22+
import ValueFlowTest<Config>
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package main
2+
3+
func source() string { return "tainted" }
4+
func sink(string) {}
5+
func isBad(string) bool {
6+
return false
7+
}
8+
9+
func readMerge(b, c bool) {
10+
value := source()
11+
if b {
12+
sink(value) // $ hasValueFlow="value"
13+
} else {
14+
sink(value) // $ hasValueFlow="value"
15+
}
16+
if c {
17+
sink(value) // $ hasValueFlow="value"
18+
} else {
19+
sink(value) // $ hasValueFlow="value"
20+
}
21+
}
22+
23+
func writeMerge(b, c bool) {
24+
value := "safe"
25+
if b {
26+
value = source()
27+
}
28+
if c {
29+
sink(value) // $ hasValueFlow="value"
30+
} else {
31+
sink(value) // $ hasValueFlow="value"
32+
}
33+
value = "safe"
34+
sink(value)
35+
}
36+
37+
func loop(b bool) {
38+
value := source()
39+
for b {
40+
sink(value) // $ hasValueFlow="value"
41+
value = "safe"
42+
}
43+
sink(value) // $ hasValueFlow="value"
44+
}
45+
46+
func namedResult() (value string) {
47+
value = source()
48+
return
49+
}
50+
51+
func implicitRead() {
52+
sink(namedResult()) // $ hasValueFlow="call to namedResult"
53+
}
54+
55+
func capturedWrite() {
56+
value := "safe"
57+
set := func() {
58+
value = source()
59+
}
60+
set()
61+
sink(value) // $ hasValueFlow="value"
62+
}
63+
64+
func capturedRead() {
65+
value := source()
66+
read := func() {
67+
sink(value) // $ hasValueFlow="value"
68+
}
69+
sink(value) // $ hasValueFlow="value"
70+
read()
71+
}
72+
73+
func uncertainUpdate(b bool) {
74+
value := source()
75+
update := func() {
76+
if b {
77+
value = "safe"
78+
}
79+
}
80+
update()
81+
sink(value) // $ hasValueFlow="value"
82+
}
83+
84+
func postUpdate(b, c bool) {
85+
value := []string{"safe"}
86+
update := func(values []string) {
87+
values[0] = source()
88+
}
89+
update(value)
90+
if b {
91+
sink(value[0]) // $ hasValueFlow="index expression"
92+
}
93+
if c {
94+
sink(value[0]) // $ hasValueFlow="index expression"
95+
} else {
96+
sink(value[0]) // $ hasValueFlow="index expression"
97+
}
98+
}
99+
100+
func guardedMerge(b bool) {
101+
value := source()
102+
if isBad(value) {
103+
return
104+
}
105+
if b {
106+
sink(value)
107+
} else {
108+
sink(value)
109+
}
110+
}
111+
112+
type record struct {
113+
value string
114+
}
115+
116+
func guardedFieldMerge(b bool) {
117+
item := record{source()}
118+
if isBad(item.value) {
119+
return
120+
}
121+
if b {
122+
sink(item.value)
123+
} else {
124+
sink(item.value)
125+
}
126+
}

0 commit comments

Comments
 (0)