Skip to content

Commit 453a309

Browse files
owen-mcCopilot
andcommitted
Go: Synchronize deferred captures at invocation
Preserve registration-time callee and argument evaluation while synchronizing captured values and write-back at deferred invocation for closure callees, arguments, interface-boxed closures, and method receivers. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1ee0eeb commit 453a309

8 files changed

Lines changed: 645 additions & 13 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
22
category: minorAnalysis
33
---
4-
* Data flow through captured variables now uses the shared variable capture library, keeping captures associated with their closure objects rather than using context-insensitive jumps.
4+
* Data flow through captured variables now uses the shared variable capture library, keeping captures associated with their closure objects rather than using context-insensitive jumps, and synchronizing captured variables for deferred closures at deferred invocation while preserving registration-time argument evaluation.
55
* `DataFlow::ArgumentNode` now includes the closure argument at position `-2` for calls through function values.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,11 @@ module Public {
11131113
}
11141114

11151115
private predicate insnDirectlyHasDeferredPostUpdateNode(IR::Instruction insn) {
1116+
exists(DeferStmt d |
1117+
isLambdaCall(d.getCall()) and
1118+
insn = IR::deferReadCalleeInstruction(d)
1119+
)
1120+
or
11161121
exists(AddressExpr ae |
11171122
isOnDeferSavedAccessPath(_, IR::evalExprInstruction(ae)) and
11181123
(

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,14 @@ predicate deferredPostUpdateFlowStepForDefer(DeferStmt d, Node nodeFrom, Node no
217217
laterRead.reads(rv)
218218
)
219219
)
220+
or
221+
exists(IR::Instruction anc, IR::Instruction laterRead, boolean inPlace |
222+
DataFlow::isOnDeferSavedAccessPath(d, anc) and
223+
nodeFrom.(DataFlow::PostUpdateNode).getPreUpdateNode().asInstruction() = anc and
224+
laterRead = epilogueNodeAfterDefer(d) and
225+
(if deferMutatesInPlace(d, anc) then inPlace = true else inPlace = false) and
226+
VariableCapture::deferredPostUpdateReachesCaptureTarget(d, anc, inPlace, laterRead, nodeTo)
227+
)
220228
}
221229

222230
/**

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

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ private predicate closureDirectInstructionStep(IR::Instruction nodeFrom, IR::Ins
7070
nodeFrom = IR::evalExprInstruction(conv.getOperand().stripParens()) and
7171
nodeTo = IR::evalExprInstruction(conv)
7272
)
73+
or
74+
SsaImpl::tempDefUseStep(nodeFrom, nodeTo)
7375
)
7476
}
7577

@@ -177,7 +179,10 @@ private Flow::ClosureNode asClosureNode(Node n) {
177179
or
178180
exists(IR::Instruction insn |
179181
insn = n.(PostUpdateNode).getPreUpdateNode().asInstruction() and
180-
DataFlow::insnHasSyncPostUpdateNode(insn) and
182+
(
183+
DataFlow::insnHasSyncPostUpdateNode(insn) or
184+
insn instanceof IR::DeferReadInstruction
185+
) and
181186
result.(Flow::ExprPostUpdateNode).getExpr() = insn
182187
)
183188
or
@@ -206,3 +211,91 @@ predicate clearsContent(Node n, CapturedVariableContent c) {
206211
predicate localFlowStep(Node nodeFrom, Node nodeTo) {
207212
Flow::localFlowStep(asClosureNode(nodeFrom), asClosureNode(nodeTo))
208213
}
214+
215+
private predicate deferCaptureSeed(DeferStmt d, IR::Instruction anc, CapturedVariable v) {
216+
DataFlow::isOnDeferSavedAccessPath(d, anc) and
217+
anc.reads(v)
218+
}
219+
220+
private predicate directCaptureWriteAt(CapturedVariable v, BasicBlock bb, int i) {
221+
exists(CaptureInput::VariableWrite vw |
222+
vw.writes(v, _) and
223+
vw.hasCfgNode(bb, i)
224+
)
225+
}
226+
227+
private predicate deferSeedReachesBlockStart(
228+
DeferStmt d, IR::Instruction anc, CapturedVariable v, BasicBlock bb
229+
) {
230+
exists(BasicBlock startBb, int startIdx |
231+
deferCaptureSeed(d, anc, v) and
232+
anc = startBb.getNode(startIdx) and
233+
not exists(int writeIdx |
234+
writeIdx > startIdx and
235+
directCaptureWriteAt(v, startBb, writeIdx)
236+
) and
237+
bb = startBb.getASuccessor()
238+
)
239+
or
240+
exists(BasicBlock pred |
241+
deferSeedReachesBlockStart(d, anc, v, pred) and
242+
not directCaptureWriteAt(v, pred, _) and
243+
bb = pred.getASuccessor()
244+
)
245+
}
246+
247+
private predicate deferSeedReachesDeferCallWithoutWrite(
248+
DeferStmt d, IR::Instruction anc, CapturedVariable v
249+
) {
250+
exists(IR::Instruction deferCall | deferCall = IR::evalExprInstruction(d.getCall()) |
251+
exists(BasicBlock bb, int startIdx, int endIdx |
252+
deferCaptureSeed(d, anc, v) and
253+
anc = bb.getNode(startIdx) and
254+
deferCall = bb.getNode(endIdx) and
255+
endIdx > startIdx and
256+
not exists(int writeIdx |
257+
writeIdx > startIdx and
258+
writeIdx < endIdx and
259+
directCaptureWriteAt(v, bb, writeIdx)
260+
)
261+
)
262+
or
263+
exists(BasicBlock bb, int endIdx |
264+
deferSeedReachesBlockStart(d, anc, v, bb) and
265+
deferCall = bb.getNode(endIdx) and
266+
not exists(int writeIdx |
267+
writeIdx < endIdx and
268+
directCaptureWriteAt(v, bb, writeIdx)
269+
)
270+
)
271+
)
272+
}
273+
274+
/**
275+
* Holds if `target` reads a captured variable `v` at `laterRead` (either into
276+
* a closure's `CaptureNode` or at a named `ReadResultInstruction`), and `anc`
277+
* on `d`'s saved access path updates `v` without `v` being overwritten before
278+
* `d` runs (unless `inPlace` holds, in which case `d` mutates `v` in place).
279+
*/
280+
predicate deferredPostUpdateReachesCaptureTarget(
281+
DeferStmt d, IR::Instruction anc, boolean inPlace, IR::Instruction laterRead, Node target
282+
) {
283+
exists(CapturedVariable v | deferCaptureSeed(d, anc, v) |
284+
exists(Flow::ExprNode closureNode |
285+
closureNode.getExpr() = laterRead and
286+
Flow::storeStep(target.(CaptureNode).getSynthesizedCaptureNode(), v, closureNode)
287+
|
288+
inPlace = true
289+
or
290+
inPlace = false and
291+
deferSeedReachesDeferCallWithoutWrite(d, anc, v)
292+
)
293+
or
294+
inPlace = false and
295+
laterRead instanceof IR::ReadResultInstruction and
296+
not laterRead instanceof IR::ReadUnnamedResultInstruction and
297+
laterRead.reads(v) and
298+
deferSeedReachesDeferCallWithoutWrite(d, anc, v) and
299+
target = instructionNode(laterRead)
300+
)
301+
}

‎go/ql/test/library-tests/semmle/go/dataflow/VariableCapture/ArgumentNodes.ql‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,29 @@ private import semmle.go.dataflow.internal.DataFlowPrivate as Impl
33
private import semmle.go.dataflow.internal.DataFlowNodes::Private as Nodes
44
import utils.test.InlineExpectationsTest
55

6+
private class ModelCopyToFirstArg extends DataFlow::FunctionModel {
7+
ModelCopyToFirstArg() { this.getName() = "modelCopyToFirstArg" }
8+
9+
override predicate hasDataFlow(FunctionInput inp, FunctionOutput outp) {
10+
inp.isParameter(1) and outp.isParameter(0)
11+
}
12+
}
13+
14+
private class ModelTaintToFirstArg extends TaintTracking::FunctionModel {
15+
ModelTaintToFirstArg() { this.getName() = "modelTaintToFirstArg" }
16+
17+
override predicate hasTaintFlow(FunctionInput inp, FunctionOutput outp) {
18+
inp.isParameter(1) and outp.isParameter(0)
19+
}
20+
}
21+
622
module ArgumentNodesTest implements TestSig {
723
string getARelevantTag() {
824
result =
925
[
1026
"closure-args", "callee-post", "closure-post", "argument-post", "syntactic-arg",
11-
"receiver-post", "closure-params", "receiver-node", "function-receiver", "input-receiver",
12-
"receiver-agreement"
27+
"receiver-post", "model-effective-input", "closure-params", "receiver-node",
28+
"function-receiver", "input-receiver", "receiver-agreement"
1329
]
1430
}
1531

@@ -34,13 +50,13 @@ module ArgumentNodesTest implements TestSig {
3450
.toString()
3551
or
3652
tag = "argument-post" and
37-
call.getTarget().getName() = "callback" and
53+
call.getTarget().getName() = ["callback", "callbackAny"] and
3854
value =
3955
count(DataFlow::PostUpdateNode post | post.getPreUpdateNode() = Impl::getArgument(call, 0))
4056
.toString()
4157
or
4258
tag = "syntactic-arg" and
43-
call.getTarget().getName() = "callback" and
59+
call.getTarget().getName() = ["callback", "callbackAny"] and
4460
value =
4561
count(DataFlow::ArgumentNode argument |
4662
argument.argumentOf(call.asExpr(), 0) and
@@ -53,6 +69,18 @@ module ArgumentNodesTest implements TestSig {
5369
count(DataFlow::PostUpdateNode post |
5470
post.getPreUpdateNode() = call.(DataFlow::MethodCallNode).getReceiver()
5571
).toString()
72+
or
73+
tag = "model-effective-input" and
74+
call.getTarget().getName() = ["modelCopyToFirstArg", "modelTaintToFirstArg"] and
75+
value =
76+
count(DataFlow::Node inp |
77+
(
78+
exists(DataFlow::FunctionModel m | inp = m.getAnInputNode(call))
79+
or
80+
exists(TaintTracking::FunctionModel m | inp = m.getAnInputNode(call))
81+
) and
82+
inp = Impl::getArgument(call, 1)
83+
).toString()
5684
)
5785
or
5886
exists(FuncLit function |

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,48 @@
11
import utils.test.InlineFlowTest
2-
import ValueFlowTest<DefaultFlowConfig>
32
private import semmle.go.dataflow.internal.VariableCapture as Capture
43
private import semmle.go.dataflow.internal.DataFlowUtil as Util
54
private import semmle.go.dataflow.internal.DataFlowNodes::Private as Nodes
65
private import semmle.go.dataflow.internal.DataFlowPrivate as PrivateFlow
76

7+
module TaintModelFlowConfig implements DataFlow::ConfigSig {
8+
predicate isSource(DataFlow::Node source) {
9+
DefaultFlowConfig::isSource(source) and
10+
source
11+
.asExpr()
12+
.getEnclosingFunction*()
13+
.(FuncDecl)
14+
.getName()
15+
.matches("deferredTaintFunctionModel%")
16+
}
17+
18+
predicate isSink(DataFlow::Node sink) {
19+
DefaultFlowConfig::isSink(sink) and
20+
sink.asExpr()
21+
.getEnclosingFunction*()
22+
.(FuncDecl)
23+
.getName()
24+
.matches("deferredTaintFunctionModel%")
25+
}
26+
}
27+
28+
import FlowTest<DefaultFlowConfig, TaintModelFlowConfig>
29+
30+
private class ModelCopyToFirstArg extends DataFlow::FunctionModel {
31+
ModelCopyToFirstArg() { this.getName() = "modelCopyToFirstArg" }
32+
33+
override predicate hasDataFlow(FunctionInput inp, FunctionOutput outp) {
34+
inp.isParameter(1) and outp.isParameter(0)
35+
}
36+
}
37+
38+
private class ModelTaintToFirstArg extends TaintTracking::FunctionModel {
39+
ModelTaintToFirstArg() { this.getName() = "modelTaintToFirstArg" }
40+
41+
override predicate hasTaintFlow(FunctionInput inp, FunctionOutput outp) {
42+
inp.isParameter(1) and outp.isParameter(0)
43+
}
44+
}
45+
846
query predicate spuriousConstructionClearing(DataFlow::Node construction, string variable) {
947
construction.asExpr() instanceof FuncLit and
1048
exists(Util::CapturedVariableContent content |

0 commit comments

Comments
 (0)