File size: 2,904 Bytes
de452ad | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | // Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"os"
"runtime"
"runtime/debug"
"runtime/secret"
"sync"
"syscall"
"time"
_ "unsafe"
"weak"
)
// Same secret as in ../../crash_test.go
var secretStore = [8]byte{
0x00,
0x81,
0xa0,
0xc6,
0xb3,
0x01,
0x66,
0x53,
}
func main() {
enableCore()
useSecretProc()
// clear out secret. That way we don't have
// to figure out which secret is the allowed
// source
clear(secretStore[:])
panic("terminate")
}
// Copied from runtime/runtime-gdb_unix_test.go
func enableCore() {
debug.SetTraceback("crash")
var lim syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_CORE, &lim)
if err != nil {
panic(fmt.Sprintf("error getting rlimit: %v", err))
}
lim.Cur = lim.Max
fmt.Fprintf(os.Stderr, "Setting RLIMIT_CORE = %+#v\n", lim)
err = syscall.Setrlimit(syscall.RLIMIT_CORE, &lim)
if err != nil {
panic(fmt.Sprintf("error setting rlimit: %v", err))
}
}
// useSecretProc does 5 seconds of work, using the secret value
// inside secret.Do in a bunch of ways.
func useSecretProc() {
stop := make(chan bool)
var wg sync.WaitGroup
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
time.Sleep(1 * time.Second)
for {
select {
case <-stop:
wg.Done()
return
default:
secret.Do(func() {
// Copy key into a variable-sized heap allocation.
// This both puts secrets in heap objects,
// and more generally just causes allocation,
// which forces garbage collection, which
// requires interrupts and the like.
s := bytes.Repeat(secretStore[:], 1+i*2)
// Also spam the secret across all registers.
useSecret(s)
})
}
}
}()
}
// Send some allocations over a channel. This does 2 things:
// 1) forces some GCs to happen
// 2) causes more scheduling noise (Gs moving between Ms, etc.)
c := make(chan []byte)
wg.Add(2)
go func() {
for {
select {
case <-stop:
wg.Done()
return
case c <- make([]byte, 256):
}
}
}()
go func() {
for {
select {
case <-stop:
wg.Done()
return
case <-c:
}
}
}()
time.Sleep(5 * time.Second)
close(stop)
wg.Wait()
// use a weak reference for ensuring that the GC has cleared everything
// Use a large value to avoid the tiny allocator.
w := weak.Make(new([2048]byte))
// 20 seems like a decent amount?
for i := 0; i < 20; i++ {
runtime.GC() // GC should clear any secret heap objects and clear out scheduling buffers.
if w.Value() == nil {
fmt.Fprintf(os.Stderr, "number of GCs %v\n", i+1)
return
}
}
fmt.Fprintf(os.Stderr, "GC didn't clear out in time\n")
// This will cause the core dump to happen with the sentinel value still in memory
// so we will detect the fault.
panic("fault")
}
|