Skip to content

Commit 1ee0eeb

Browse files
owen-mcCopilot
andcommitted
Go: Remove uncertain exit reads for captured variables
Drop synthetic uncertain exit reads now that shared capture builds its own SSA graph, and exclude captured variables from DeadStoreOfLocal to avoid false positives when escaped closures observe later assignments. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 92c30c1 commit 1ee0eeb

19 files changed

Lines changed: 137 additions & 172 deletions

File tree

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,31 +97,10 @@ private module Internal {
9797

9898
/**
9999
* Holds if the `i`th node of basic block `bb` reads source variable `v`.
100-
*
101-
* We add a synthetic uncertain read at the exit node of every function
102-
* that references a captured variable `v`. This ensures that definitions
103-
* of captured variables are included in the SSA graph even when the
104-
* variable is not locally read in that function scope (but may be read
105-
* by another function sharing the same closure).
106100
*/
107101
cached
108102
predicate variableRead(BasicBlock bb, int i, SourceVariable v, boolean certain) {
109103
useAt(bb, i, v) and certain = true
110-
or
111-
v.isCaptured() and
112-
exists(FuncDef f |
113-
f = bb.getScope() and
114-
bb.getLastNode().(ControlFlow::Node).isExitNode() and
115-
i = bb.length() - 1 and
116-
certain = false
117-
|
118-
// The declaring function: captures may be read after calls to closures
119-
f = v.getDeclaringFunction()
120-
or
121-
// Any function that writes `v`: the write may be observed by the
122-
// declaring function or another closure sharing the same variable
123-
any(IR::Instruction def | def.writes(v, _)).getRoot() = f
124-
)
125104
}
126105

127106
cached

‎go/ql/src/RedundantCode/DeadStoreOfLocal.qhelp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ A value is assigned to a variable, but either it is never read, or its value is
99
always overwritten before being read. This means that the original assignment
1010
has no effect, and could indicate a logic error or incomplete code.
1111
</p>
12+
<p>
13+
Variables captured by closures are excluded because escaped closures may read
14+
values assigned after the closure was created.
15+
</p>
1216
</overview>
1317

1418
<recommendation>

‎go/ql/src/RedundantCode/DeadStoreOfLocal.ql‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ from IR::WriteInstruction def, SsaSourceVariable target, IR::Instruction rhs, Ex
3333
where
3434
def.writes(target, rhs) and
3535
not exists(SsaExplicitWrite ssa | ssa.getDefinition() = def) and
36+
// Escaped closures may observe writes that have no corresponding SSA read.
37+
not target.isCaptured() and
3638
// exclude assignments in dead code
3739
def.getBasicBlock() instanceof ReachableBasicBlock and
3840
// exclude assignments with default values or simple expressions
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The query `go/useless-assignment-to-local` ("Useless assignment to local variable") no longer reports assignments to variables captured by closures, avoiding false positives when escaped closures observe later assignments.

‎go/ql/test/experimental/CWE-942/CONSISTENCY/SsaConsistency.expected‎

Lines changed: 0 additions & 4 deletions
This file was deleted.

‎go/ql/test/library-tests/semmle/go/controlflow/ControlFlowGraph/CONSISTENCY/SsaConsistency.expected‎

Lines changed: 0 additions & 4 deletions
This file was deleted.

‎go/ql/test/library-tests/semmle/go/dataflow/Defer/CONSISTENCY/SsaConsistency.expected‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎go/ql/test/library-tests/semmle/go/dataflow/FlowSteps/LocalFlowStep.expected‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
| main.go:10:12:10:12 | y | main.go:10:17:10:17 | y |
1818
| main.go:10:17:10:27 | ...>=... | main.go:10:7:10:27 | After ...&&... |
1919
| main.go:11:14:11:14 | z | main.go:11:9:11:15 | type conversion |
20-
| main.go:15:9:15:9 | 0 | main.go:15:2:15:9 | SSA def(acc) |
2120
| main.go:16:9:19:2 | SSA implicit def(acc) | main.go:17:3:17:5 | acc |
2221
| main.go:17:3:17:7 | SSA def(acc) | main.go:18:10:18:12 | acc |
2322
| main.go:17:3:17:7 | increment statement | main.go:17:3:17:7 | SSA def(acc) |

‎go/ql/test/library-tests/semmle/go/dataflow/GlobalValueNumbering/GlobalValueNumber.expected‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
| main.go:33:24:39:1 | zero-init:0 block statement | main.go:10:7:10:7 | 0 |
3535
| main.go:34:8:34:8 | 7 | main.go:34:8:34:8 | 7 |
3636
| main.go:35:8:37:4 | defer-invoke function call | main.go:35:8:37:4 | function call |
37-
| main.go:36:3:36:9 | SSA def(res) | main.go:36:9:36:9 | 8 |
3837
| main.go:36:9:36:9 | 8 | main.go:36:9:36:9 | 8 |
3938
| main.go:38:2:38:9 | SSA def(res) | main.go:38:9:38:9 | 9 |
4039
| main.go:38:9:38:9 | 9 | main.go:38:9:38:9 | 9 |

‎go/ql/test/library-tests/semmle/go/dataflow/SSA/SsaDefinition.expected‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
| main.go:95:22:95:28 | SSA param(wrapper) |
3636
| main.go:96:2:96:7 | SSA def(x) |
3737
| main.go:97:2:99:3 | SSA implicit def(x) |
38-
| main.go:98:3:98:7 | SSA def(x) |
3938
| main.go:103:20:103:26 | SSA param(wrapper) |
4039
| main.go:104:2:104:7 | SSA def(x) |
4140
| main.go:105:16:108:2 | SSA implicit def(x) |
@@ -45,7 +44,6 @@
4544
| main.go:114:2:117:3 | SSA implicit def(x) |
4645
| main.go:114:16:117:2 | SSA implicit def(x) |
4746
| main.go:115:3:115:12 | SSA def(y) |
48-
| main.go:116:3:116:7 | SSA def(x) |
4947
| main.go:129:2:133:2 | SSA phi(p) |
5048
| main.go:130:3:130:24 | SSA def(p) |
5149
| main.go:132:3:132:24 | SSA def(p) |

0 commit comments

Comments
 (0)