Source file src/runtime/secret/alloc_test.go

     1  // Copyright 2025 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.runtimesecret && (arm64 || amd64) && linux
     6  
     7  package secret_test
     8  
     9  import (
    10  	"runtime"
    11  	"runtime/secret"
    12  	"testing"
    13  )
    14  
    15  func TestInterleavedAllocFrees(t *testing.T) {
    16  	// Interleave heap objects that are kept alive beyond secret.Do
    17  	// with heap objects that do not live past secret.Do.
    18  	// The intent is for the clearing of one object (with the wrong size)
    19  	// to clobber the type header of the next slot. If the GC sees a nil type header
    20  	// when it expects to find one, it can throw.
    21  	type T struct {
    22  		p *int
    23  		x [1024]byte
    24  	}
    25  	for range 10 {
    26  		var s []*T
    27  		secret.Do(func() {
    28  			for i := range 100 {
    29  				t := &T{}
    30  				if i%2 == 0 {
    31  					s = append(s, t)
    32  				}
    33  			}
    34  		})
    35  		runtime.GC()
    36  		runtime.GC()
    37  		runtime.KeepAlive(s)
    38  	}
    39  }
    40  

View as plain text