From: Daniel Morsing Date: Sun, 28 Dec 2025 09:29:47 +0000 (+0000) Subject: runtime/secret: make tests more sturdy X-Git-Tag: go1.26rc2~7^2~12 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1843cfbcd6bd0e1f7650c663c6c4608bc1cd8829;p=gostls13.git runtime/secret: make tests more sturdy Technically, the garbage collector can take an arbitrary long time until it finds a given value unreachable. Account for this fact in our zeroing tests. Updates #76586. Change-Id: Ieaf3cec99afb9204a32524ea559c5f1a280a59e6 Reviewed-on: https://go-review.googlesource.com/c/go/+/732980 Reviewed-by: Keith Randall Reviewed-by: Cherry Mui Reviewed-by: Filippo Valsorda Auto-Submit: Keith Randall Reviewed-by: Keith Randall LUCI-TryBot-Result: Go LUCI --- diff --git a/src/runtime/secret/secret_test.go b/src/runtime/secret/secret_test.go index 98d67cf8a4..e2f78c53a0 100644 --- a/src/runtime/secret/secret_test.go +++ b/src/runtime/secret/secret_test.go @@ -19,6 +19,7 @@ import ( "testing" "time" "unsafe" + "weak" ) type secretType int64 @@ -63,28 +64,33 @@ func heapSTiny() *secretType { // are freed. // See runtime/mheap.go:freeSpecial. func TestHeap(t *testing.T) { - var u uintptr + var addr uintptr + var p weak.Pointer[S] Do(func() { - u = uintptr(unsafe.Pointer(heapS())) + sp := heapS() + addr = uintptr(unsafe.Pointer(sp)) + p = weak.Make(sp) }) - - runtime.GC() + waitCollected(t, p) // Check that object got zeroed. - checkRangeForSecret(t, u, u+unsafe.Sizeof(S{})) + checkRangeForSecret(t, addr, addr+unsafe.Sizeof(S{})) // Also check our stack, just because we can. checkStackForSecret(t) } func TestHeapTiny(t *testing.T) { - var u uintptr + var addr uintptr + var p weak.Pointer[secretType] Do(func() { - u = uintptr(unsafe.Pointer(heapSTiny())) + sp := heapSTiny() + addr = uintptr(unsafe.Pointer(sp)) + p = weak.Make(sp) }) - runtime.GC() + waitCollected(t, p) // Check that object got zeroed. - checkRangeForSecret(t, u, u+unsafe.Sizeof(secretType(0))) + checkRangeForSecret(t, addr, addr+unsafe.Sizeof(secretType(0))) // Also check our stack, just because we can. checkStackForSecret(t) } @@ -240,6 +246,20 @@ func checkRangeForSecret(t *testing.T, lo, hi uintptr) { } } +func waitCollected[P any](t *testing.T, ptr weak.Pointer[P]) { + t.Helper() + i := 0 + for ptr.Value() != nil { + runtime.GC() + i++ + // 20 seems like a decent number of times to try + if i > 20 { + t.Errorf("value was never collected") + } + } + t.Logf("number of cycles until collection: %d", i) +} + func TestRegisters(t *testing.T) { Do(func() { s := makeS()