-
Notifications
You must be signed in to change notification settings - Fork 49
cl,ssa,runtime: safely clear dead local finalizer roots #2036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cpunion
wants to merge
9
commits into
xgo-dev:main
Choose a base branch
from
cpunion:codex/stage5-finalizer-liveness
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3e0c645
cl,ssa,runtime: stack-object finalizer liveness
cpunion f698915
test(cl): cover malformed liveness referrers
cpunion 2607255
cl,runtime: make finalizer liveness clearing fail closed
cpunion b2a33ae
cl,runtime: address finalizer liveness review
cpunion 85f7eea
cl,runtime: fail closed on hidden stack aliases
cpunion 878e07b
cl,runtime: clarify liveness safety invariants
cpunion 5e9b6cd
cl,runtime: reject unscheduled liveness referrers
cpunion e538d81
cl: fail closed on unknown liveness instructions
cpunion af71577
cl: reuse liveness order per block
cpunion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,276 @@ | ||
| /* | ||
| * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package gotest | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| const finalizerLivenessProbe = `package main | ||
|
|
||
| import ( | ||
| "os" | ||
| "runtime" | ||
| "time" | ||
| "unsafe" | ||
| ) | ||
|
|
||
| type Box struct { | ||
| p *int | ||
| } | ||
|
|
||
| type HeapObject [8]int64 | ||
|
|
||
| type StackSlots [8]*HeapObject | ||
|
|
||
| var ( | ||
| savedClosure func() | ||
| savedBox *Box | ||
| expected uintptr | ||
| evalSlot *int | ||
| ) | ||
|
|
||
| func loopCase() { | ||
| x := 42 | ||
| var box Box | ||
| box.p = &x | ||
| for i := 0; i < 3; i++ { | ||
| if box.p == nil || *box.p != 42 { | ||
| panic("box was cleared while live across loop backedge") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func closureCase() { | ||
| x := 42 | ||
| box := Box{p: &x} | ||
| savedClosure = func() { | ||
| if box.p == nil || *box.p != 42 { | ||
| panic("captured heap allocation was cleared") | ||
| } | ||
| } | ||
| savedClosure() | ||
| } | ||
|
|
||
| func globalEscapeCase() { | ||
| x := 42 | ||
| savedBox = &Box{p: &x} | ||
| if savedBox.p == nil || *savedBox.p != 42 { | ||
| panic("globally escaped heap allocation was cleared") | ||
| } | ||
| } | ||
|
|
||
| //go:noinline | ||
| func checkDeferred(box *Box) { | ||
| if box.p == nil || *box.p != 42 { | ||
| panic("deferred argument was cleared before RunDefers") | ||
| } | ||
| } | ||
|
|
||
| func deferCase() { | ||
| x := 42 | ||
| box := Box{p: &x} | ||
| defer checkDeferred(&box) | ||
| } | ||
|
|
||
| //go:noinline | ||
| func consumeBox(*Box) {} | ||
|
|
||
| //go:noinline | ||
| func checkAlias(p *int) { | ||
| if p == nil || *p != 42 { | ||
| panic("independent live alias was cleared") | ||
| } | ||
| } | ||
|
|
||
| func aliasCase() { | ||
| h := new(int) | ||
| *h = 42 | ||
| box := Box{p: h} | ||
| alias := h | ||
| consumeBox(&box) | ||
| checkAlias(alias) | ||
| } | ||
|
|
||
| func goroutineCase() { | ||
| x := 42 | ||
| box := Box{p: &x} | ||
| start := make(chan struct{}) | ||
| done := make(chan struct{}) | ||
| go func(p *Box) { | ||
| <-start | ||
| if p.p == nil || *p.p != 42 { | ||
| panic("goroutine argument was cleared before use") | ||
| } | ||
| close(done) | ||
| }(&box) | ||
| close(start) | ||
| <-done | ||
| } | ||
|
|
||
| func uintptrCase() { | ||
| h := new(int) | ||
| box := Box{p: h} | ||
| bits := uintptr(unsafe.Pointer(h)) | ||
| expected = bits | ||
| consumeBox(&box) | ||
| if bits != expected { | ||
| panic("live uintptr bits were rewritten by stack scan") | ||
| } | ||
| } | ||
|
|
||
| func clearEvalSlot() any { | ||
| evalSlot = nil | ||
| return func(*int) {} | ||
| } | ||
|
|
||
| //go:noinline | ||
| func loadEvalSlot() *int { | ||
| return evalSlot | ||
| } | ||
|
|
||
| func evalOrderCase() { | ||
| p := new(int) | ||
| evalSlot = p | ||
| runtime.SetFinalizer(loadEvalSlot(), clearEvalSlot()) | ||
| runtime.KeepAlive(p) | ||
| } | ||
|
|
||
| //go:noinline | ||
| func sameBlockFinalizationCase(writeIndex, readIndex int) { | ||
| finalized := make(chan struct{}, 1) | ||
| var slots StackSlots | ||
| // Keep the dynamic store and load distinct in SSA while the caller supplies | ||
| // the same index, so this exercises clearing the exact dead stack allocation. | ||
| slots[writeIndex] = new(HeapObject) | ||
| runtime.SetFinalizer(slots[readIndex], func(*HeapObject) { | ||
| finalized <- struct{}{} | ||
| }) | ||
|
|
||
| for i := 0; i < 100; i++ { | ||
| runtime.GC() | ||
| select { | ||
| case <-finalized: | ||
| return | ||
| default: | ||
| } | ||
| runtime.Gosched() | ||
| time.Sleep(10 * time.Millisecond) | ||
| } | ||
| panic("same-block dead stack slot kept finalizer object alive") | ||
| } | ||
|
|
||
| func storedAliasCase() { | ||
| x := 42 | ||
| var box Box | ||
| var alias **int | ||
| box.p = &x | ||
| aliasSlot := &alias | ||
| *aliasSlot = &box.p | ||
| if **aliasSlot == nil || ***aliasSlot != 42 { | ||
| panic("stack slot was cleared before a stored alias read") | ||
| } | ||
| } | ||
|
|
||
| func main() { | ||
| if len(os.Args) != 2 { | ||
| panic("missing case name") | ||
| } | ||
| activation := new(int) | ||
| runtime.SetFinalizer(activation, func(*int) {}) | ||
| switch os.Args[1] { | ||
| case "loop": | ||
| loopCase() | ||
| case "closure": | ||
| closureCase() | ||
| case "global-escape": | ||
| globalEscapeCase() | ||
| case "defer": | ||
| deferCase() | ||
| case "alias": | ||
| aliasCase() | ||
| case "goroutine": | ||
| goroutineCase() | ||
| case "uintptr": | ||
| uintptrCase() | ||
| case "eval-order": | ||
| evalOrderCase() | ||
| case "same-block-finalization": | ||
| index := len(os.Args[1]) & (len(StackSlots{}) - 1) | ||
| sameBlockFinalizationCase(index, index) | ||
| case "stored-alias": | ||
| storedAliasCase() | ||
| default: | ||
| panic("unknown case") | ||
| } | ||
| runtime.KeepAlive(activation) | ||
| } | ||
| ` | ||
|
|
||
| func buildFinalizerLivenessProbe(t *testing.T) (hostBin, llgoBin string) { | ||
| t.Helper() | ||
| dir := t.TempDir() | ||
| mainFile := filepath.Join(dir, "main.go") | ||
| if err := os.WriteFile(mainFile, []byte(finalizerLivenessProbe), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module example.com/finalizerprobe\n\ngo 1.24\n"), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| hostBin = filepath.Join(dir, "host-probe") | ||
| runGoCmd(t, dir, "build", "-o", hostBin, ".") | ||
|
|
||
| llgoBin = filepath.Join(dir, "llgo-probe") | ||
| out, err := runLLGoInModule(t, dir, "build", "-o", llgoBin, ".") | ||
| if err != nil { | ||
| t.Fatalf("llgo build failed: %v\n%s", err, out) | ||
| } | ||
| return hostBin, llgoBin | ||
| } | ||
|
|
||
| func runFinalizerLivenessProbe(t *testing.T, bin, caseName string) { | ||
| t.Helper() | ||
| out, err := exec.Command(bin, caseName).CombinedOutput() | ||
| if err != nil { | ||
| t.Fatalf("%s failed: %v\n%s", filepath.Base(bin), err, out) | ||
| } | ||
| } | ||
|
|
||
| func TestRuntimeSetFinalizerPreservesLiveValues(t *testing.T) { | ||
| hostBin, llgoBin := buildFinalizerLivenessProbe(t) | ||
| for _, caseName := range []string{ | ||
| "loop", | ||
| "closure", | ||
| "global-escape", | ||
| "defer", | ||
| "alias", | ||
| "goroutine", | ||
| "uintptr", | ||
| "eval-order", | ||
| "same-block-finalization", | ||
| "stored-alias", | ||
| } { | ||
| t.Run(caseName, func(t *testing.T) { | ||
| runFinalizerLivenessProbe(t, hostBin, caseName) | ||
| runFinalizerLivenessProbe(t, llgoBin, caseName) | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.