Skip to content

Go: implement shared variable capture library - #22863

Draft
owen-mc wants to merge 17 commits into
github:mainfrom
owen-mc:go/shared-variable-capture
Draft

owen-mc wants to merge 17 commits into
github:mainfrom
owen-mc:go/shared-variable-capture

Conversation

@owen-mc

@owen-mc owen-mc commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@owen-mc owen-mc added the Go label Oct 21, 2026
@owen-mc
owen-mc force-pushed the go/shared-variable-capture branch 2 times, most recently from b48acf2 to e7131eb Compare October 21, 2026 13:33
@owen-mc
owen-mc force-pushed the go/shared-variable-capture branch from e7131eb to d678416 Compare November 27, 2026 13:49
@github-actions

Copy link
Copy Markdown
Contributor

QHelp previews:

go/ql/src/RedundantCode/DeadStoreOfLocal.qhelp

Useless assignment to local variable

A value is assigned to a variable, but either it is never read, or its value is always overwritten before being read. This means that the original assignment has no effect, and could indicate a logic error or incomplete code.

Variables captured by closures are excluded because escaped closures may read values assigned after the closure was created.

Recommendation

Remove assignments to variables that are immediately overwritten, or use the blank identifier _ as a placeholder for return values that are never used.

Example

In the following example, a value is assigned to a, but then immediately overwritten, a value is assigned to b and never used, and finally, the results of a call to fmt.Println are assigned to two temporary variables, which are then immediately overwritten by a call to function.

package main

import "fmt"

func main() {
	a := calculateValue()
	a = 2

	b := calculateValue()

	ignore, ignore1 := fmt.Println(a)

	ignore, ignore1, err := function()
	if err != nil {
		panic(err)
	}

	fmt.Println(a)
}

The result of calculateValue is never used, and if calculateValue is a side-effect free function, those assignments can be removed. To ignore all the return values of fmt.Println, you can simply not assign it to any variables. To ignore only certain return values, use _.

package main

import "fmt"

func main() {
	a := 2

	fmt.Println(a)

	_, _, err := function()
	if err != nil {
		panic(err)
	}

	fmt.Println(a)
}

References

/** Holds if `call` is a lambda call of kind `kind` where `receiver` is the lambda expression. */
predicate lambdaCall(DataFlowCall call, LambdaCallKind kind, Node receiver) { none() }
predicate lambdaCall(DataFlowCall call, LambdaCallKind kind, Node receiver) {
isLambdaCall(call.(CallExpr)) and
owen-mc and others added 17 commits December 4, 2026 12:12
Model Go's runtime defer and return evaluation order directly in the
CFG and IR:

- Evaluate a deferred call's callee, receiver, and arguments at defer
  registration and save them into synthetic TempVariables via
  DeferWriteInstructions.
- Read saved callee, receiver, and arguments via DeferReadInstructions
  before each exit-time defer-invoke node in LIFO order, propagating
  panic/recovery state across the defer chain and routing process-exit
  calls (os.Exit, log.Fatal) directly to ExitSuccessor.
- In functions containing defer, save unnamed return expressions into
  synthetic result temporaries before running deferred calls and read
  them at function exit via ReadUnnamedResultInstructions.
- Unwrap parenthesized and generic-instantiated callees in
  CallExpr.getTarget() and propagate constant values onto
  DeferReadInstructions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Instantiate shared SSA for synthetic temporaries and route deferred
callees, receivers, arguments, and unnamed results through their saved
values. Adapt summary typing, constants, and path presentation.

Include inline tests for registration-time evaluation, call forms,
conditional and loop registrations, and constant branch pruning.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Separate synchronous and deferred post-update nodes so deferred
mutations do not appear in the function body. Propagate updates to
later-running defers, results, and captures along baseline access paths.

Include end-to-end tests for LIFO updates, named and unnamed returns,
nested access paths, capture isolation, and accepted alias limitations.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Reuse value numbers for single-source saved defer inputs and unnamed
results where types and intervening side effects permit it. Detect
recovered panic paths that can leave unnamed results at zero values.

Keep the focused value-numbering fixtures and expected output here.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Do not treat deferred calls as synchronous exception guards. Account
for deferred result mutations and recovery when inferring return
properties, and recognize suitable post-registration guards.

Include guard, wrapper-inference, and deferred barrier tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Track the slice or map header saved by deferred clear calls. Use
instruction ordering and SSA result-path reachability to sanitize
epilogue uses without clearing earlier body uses or bypassing returns.

Include clearing tests for conditional paths, reassigned headers,
named-result phi nodes, and accepted in-place re-tainting limitations.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Represent Fmt and Bun scan destinations as post-update sources so
deferred input operations taint their destinations only when invoked.

Include scanner source tests and the deferred-input OAuth state case.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Read regexp patterns through node constants and inspect saved deferred
inputs in allocation-size modeling and barrier propagation.

Include the matching regexp and allocation-overflow regression tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Exclude synthetic saved-value copies where crypto models and constant
sources would otherwise count the same value or operation twice.

Include deferred crypto, key-size, and hardcoded-credential fixtures.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Check handled Sync ordering from defer registration. Unwrap saved
receivers and direct SSA copies without following phi inputs, so
syncing one possible handle does not exempt another unsynced handle.

Include ordinary and deferred receiver-merge regressions and controls.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Trace local-flow roots into synthetic result reads when identifying
callbacks that always return nil. Exclude results with uncertain
capture writes or indirect references that may be changed by defers.

Include named, unnamed, captured, and pointer-mutated result tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Document registration-time saving, exit-time invocation, and synthetic
call/result nodes in the library pack. Note precision improvements in
the SSH callback and writable-file-close queries in the query pack.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Allow synthetic data-flow nodes such as DeferReadInstructions to act as
XPath injection sinks and sanitizers, matching XPath::XPathExpressionString,
XPath::Sanitizer, and the other Go security customization modules.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use the shared variable capture library for inter-procedural capture flow while keeping internal capture nodes hidden and preserving ordinary SSA local flow. Recognize named, generic, and summarized function values and method values as lambda targets, and track closure aliases through local SSA, nested captured variables, conversions, and type assertions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Recognize both SSA-backed and instruction-backed receiver parameters so receiver lookup works when a receiver is unused or read only inside a closure. Document the hierarchy change and test that receiver lookup APIs agree without changing captured-receiver flow.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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>
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>
@owen-mc
owen-mc force-pushed the go/shared-variable-capture branch from d678416 to 453a309 Compare December 4, 2026 15:13

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants