Debian Codegraph @

src/runtime/abi_test.go

raw
1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build goexperiment.regabiargs
6
7 // This file contains tests specific to making sure the register ABI
8 // works in a bunch of contexts in the runtime.
9
10 package runtime_test
11
12 import (
13 "internal/abi"
14 "internal/runtime/atomic"
15 "internal/testenv"
16 "os"
17 "os/exec"
18 "runtime"
19 "strings"
20 "testing"
21 "time"
22 )
23
24 var regConfirmRun atomic.Int32
25
26 //go:registerparams
27 func regFinalizerPointer(v *TintPointer) (int, float32, [10]byte) {
28 regConfirmRun.Store(int32(*(*int)(v.p)))
29 return 5151, 4.0, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
30 }
31
32 //go:registerparams
33 func regFinalizerIface(v Tinter) (int, float32, [10]byte) {
34 regConfirmRun.Store(int32(*(*int)(v.(*TintPointer).p)))
35 return 5151, 4.0, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
36 }
37
38 // TintPointer has a pointer member to make sure that it isn't allocated by the
39 // tiny allocator, so we know when its finalizer will run
40 type TintPointer struct {
41 p *Tint
42 }
43
44 func (*TintPointer) m() {}
45
46 func TestFinalizerRegisterABI(t *testing.T) {
47 // Actually run the test in a subprocess because we don't want
48 // finalizers from other tests interfering.
49 if os.Getenv("TEST_FINALIZER_REGABI") != "1" {
50 cmd := testenv.CleanCmdEnv(exec.Command(testenv.Executable(t), "-test.run=^TestFinalizerRegisterABI$", "-test.v"))
51 cmd.Env = append(cmd.Env, "TEST_FINALIZER_REGABI=1")
52 out, err := cmd.CombinedOutput()
53 if !strings.Contains(string(out), "PASS\n") || err != nil {
54 t.Fatalf("%s\n(exit status %v)", string(out), err)
55 }
56 return
57 }
58
59 // Optimistically clear any latent finalizers from e.g. the testing
60 // package before continuing.
61 //
62 // It's possible that a finalizer only becomes available to run
63 // after this point, which would interfere with the test and could
64 // cause a crash, but because we're running in a separate process
65 // it's extremely unlikely.
66 runtime.GC()
67 runtime.GC()
68
69 // Make sure the finalizer goroutine is running.
70 runtime.SetFinalizer(new(TintPointer), func(_ *TintPointer) {})
71
72 // fing will only pick the new IntRegArgs up if it's currently
73 // sleeping and wakes up, so wait for it to go to sleep.
74 success := false
75 for i := 0; i < 100; i++ {
76 if runtime.FinalizerGAsleep() {
77 success = true
78 break
79 }
80 time.Sleep(20 * time.Millisecond)
81 }
82 if !success {
83 t.Fatal("finalizer not asleep?")
84 }
85
86 argRegsBefore := runtime.SetIntArgRegs(abi.IntArgRegs)
87 defer runtime.SetIntArgRegs(argRegsBefore)
88
89 tests := []struct {
90 name string
91 fin any
92 confirmValue int
93 }{
94 {"Pointer", regFinalizerPointer, -1},
95 {"Interface", regFinalizerIface, -2},
96 }
97 for i := range tests {
98 test := &tests[i]
99 t.Run(test.name, func(t *testing.T) {
100 x := &TintPointer{p: new(Tint)}
101 *x.p = (Tint)(test.confirmValue)
102 runtime.SetFinalizer(x, test.fin)
103
104 runtime.KeepAlive(x)
105
106 // Queue the finalizer.
107 runtime.GC()
108 runtime.GC()
109
110 if !runtime.BlockUntilEmptyFinalizerQueue(int64(time.Second)) {
111 t.Fatal("finalizer failed to execute")
112 }
113 if got := int(regConfirmRun.Load()); got != test.confirmValue {
114 t.Fatalf("wrong finalizer executed? got %d, want %d", got, test.confirmValue)
115 }
116 })
117 }
118 }