Source file src/runtime/testdata/testprog/checkfinalizers.go

     1  // Copyright 2024 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  package main
     6  
     7  import (
     8  	"internal/asan"
     9  	"internal/race"
    10  	"runtime"
    11  	"runtime/debug"
    12  	"unsafe"
    13  )
    14  
    15  func init() {
    16  	register("DetectFinalizerAndCleanupLeaks", DetectFinalizerAndCleanupLeaks)
    17  }
    18  
    19  type tiny uint8
    20  
    21  var tinySink *tiny
    22  
    23  // Intended to be run only with `GODEBUG=checkfinalizers=1`.
    24  func DetectFinalizerAndCleanupLeaks() {
    25  	type T *int
    26  
    27  	defer debug.SetGCPercent(debug.SetGCPercent(-1))
    28  
    29  	// Leak a cleanup.
    30  	cLeak := new(T)
    31  
    32  	// Use an extra closure to avoid the simple
    33  	// checking done by AddCleanup.
    34  	var closeOverCLeak func(int)
    35  	closeOverCLeak = func(x int) {
    36  		// Use recursion to avoid inlining.
    37  		if x <= 0 {
    38  			**cLeak = x
    39  		} else {
    40  			closeOverCLeak(x - 1)
    41  		}
    42  	}
    43  
    44  	runtime.AddCleanup(cLeak, func(x int) {
    45  		closeOverCLeak(x)
    46  	}, int(0))
    47  
    48  	// Have a regular cleanup to make sure it doesn't trip the detector.
    49  	cNoLeak := new(T)
    50  	runtime.AddCleanup(cNoLeak, func(_ int) {}, int(0))
    51  
    52  	// Like closeOverCLeak.
    53  	var closeOverCNoLeak func(int)
    54  	closeOverCNoLeak = func(x int) {
    55  		if x <= 0 {
    56  			**cNoLeak = x
    57  		} else {
    58  			closeOverCNoLeak(x - 1)
    59  		}
    60  	}
    61  
    62  	// Add a cleanup that only temporarily leaks cNoLeak.
    63  	runtime.AddCleanup(cNoLeak, func(x int) {
    64  		closeOverCNoLeak(x)
    65  	}, int(0)).Stop()
    66  
    67  	if !asan.Enabled && !race.Enabled {
    68  		// Ensure we create an allocation into a tiny block that shares space among several values.
    69  		//
    70  		// Don't do this with ASAN and in race mode, where the tiny allocator is disabled.
    71  		// We might just loop forever here in that case.
    72  		var ctLeak *tiny
    73  		for {
    74  			tinySink = ctLeak
    75  			ctLeak = new(tiny)
    76  			*ctLeak = tiny(55)
    77  			// Make sure the address is an odd value. This is sufficient to
    78  			// be certain that we're sharing a block with another value and
    79  			// trip the detector.
    80  			if uintptr(unsafe.Pointer(ctLeak))%2 != 0 {
    81  				break
    82  			}
    83  		}
    84  		runtime.AddCleanup(ctLeak, func(_ struct{}) {}, struct{}{})
    85  	}
    86  
    87  	// Leak a finalizer.
    88  	fLeak := new(T)
    89  	runtime.SetFinalizer(fLeak, func(_ *T) {
    90  		**fLeak = 12
    91  	})
    92  
    93  	// Have a regular finalizer to make sure it doesn't trip the detector.
    94  	fNoLeak := new(T)
    95  	runtime.SetFinalizer(fNoLeak, func(x *T) {
    96  		**x = 51
    97  	})
    98  
    99  	// runtime.GC here should crash.
   100  	runtime.GC()
   101  	println("OK")
   102  }
   103  

View as plain text