Debian Codegraph @

libgo/go/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.regabireflect
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/testenv"
14 "os"
15 "os/exec"
16 "runtime"
17 "strings"
18 "testing"
19 "time"
20 )
21
22 var regConfirmRun chan int
23
24 //go:registerparams
25 func regFinalizerPointer(v *Tint) (int, float32, [10]byte) {
26 regConfirmRun <- *(*int)(v)
27 return 5151, 4.0, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
28 }
29
30 //go:registerparams
31 func regFinalizerIface(v Tinter) (int, float32, [10]byte) {
32 regConfirmRun <- *(*int)(v.(*Tint))
33 return 5151, 4.0, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
34 }
35
36 func TestFinalizerRegisterABI(t *testing.T) {
37 testenv.MustHaveExec(t)
38
39 // Actually run the test in a subprocess because we don't want
40 // finalizers from other tests interfering.
41 if os.Getenv("TEST_FINALIZER_REGABI") != "1" {
42 cmd := testenv.CleanCmdEnv(exec.Command(os.Args[0], "-test.run=TestFinalizerRegisterABI", "-test.v"))
43 cmd.Env = append(cmd.Env, "TEST_FINALIZER_REGABI=1")
44 out, err := cmd.CombinedOutput()
45 if !strings.Contains(string(out), "PASS\n") || err != nil {
46 t.Fatalf("%s\n(exit status %v)", string(out), err)
47 }
48 return
49 }
50
51 // Optimistically clear any latent finalizers from e.g. the testing
52 // package before continuing.
53 //
54 // It's possible that a finalizer only becomes available to run
55 // after this point, which would interfere with the test and could
56 // cause a crash, but because we're running in a separate process
57 // it's extremely unlikely.
58 runtime.GC()
59 runtime.GC()
60
61 // fing will only pick the new IntRegArgs up if it's currently
62 // sleeping and wakes up, so wait for it to go to sleep.
63 success := false
64 for i := 0; i < 100; i++ {
65 if runtime.FinalizerGAsleep() {
66 success = true
67 break
68 }
69 time.Sleep(20 * time.Millisecond)
70 }
71 if !success {
72 t.Fatal("finalizer not asleep?")
73 }
74
75 // argRegsBefore := runtime.SetIntArgRegs(abi.IntArgRegs)
76 // defer runtime.SetIntArgRegs(argRegsBefore)
77
78 tests := []struct {
79 name string
80 fin any
81 confirmValue int
82 }{
83 {"Pointer", regFinalizerPointer, -1},
84 {"Interface", regFinalizerIface, -2},
85 }
86 for i := range tests {
87 test := &tests[i]
88 t.Run(test.name, func(t *testing.T) {
89 regConfirmRun = make(chan int)
90
91 x := new(Tint)
92 *x = (Tint)(test.confirmValue)
93 runtime.SetFinalizer(x, test.fin)
94
95 runtime.KeepAlive(x)
96
97 // Queue the finalizer.
98 runtime.GC()
99 runtime.GC()
100
101 select {
102 case <-time.After(time.Second):
103 t.Fatal("finalizer failed to execute")
104 case gotVal := <-regConfirmRun:
105 if gotVal != test.confirmValue {
106 t.Fatalf("wrong finalizer executed? got %d, want %d", gotVal, test.confirmValue)
107 }
108 }
109 })
110 }
111 }