Source file src/runtime/mfinal.go

     1  // Copyright 2009 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  // Garbage collector: finalizers and block profiling.
     6  
     7  package runtime
     8  
     9  import (
    10  	"internal/abi"
    11  	"internal/goarch"
    12  	"internal/runtime/atomic"
    13  	"internal/runtime/sys"
    14  	"unsafe"
    15  )
    16  
    17  // finblock is an array of finalizers to be executed. finblocks are
    18  // arranged in a linked list for the finalizer queue.
    19  //
    20  // finblock is allocated from non-GC'd memory, so any heap pointers
    21  // must be specially handled. GC currently assumes that the finalizer
    22  // queue does not grow during marking (but it can shrink).
    23  type finblock struct {
    24  	_       sys.NotInHeap
    25  	alllink *finblock
    26  	next    *finblock
    27  	cnt     uint32
    28  	_       int32
    29  	fin     [(_FinBlockSize - 2*goarch.PtrSize - 2*4) / unsafe.Sizeof(finalizer{})]finalizer
    30  }
    31  
    32  var fingStatus atomic.Uint32
    33  
    34  // finalizer goroutine status.
    35  const (
    36  	fingUninitialized uint32 = iota
    37  	fingCreated       uint32 = 1 << (iota - 1)
    38  	fingRunningFinalizer
    39  	fingWait
    40  	fingWake
    41  )
    42  
    43  // This runs durring the GC sweep phase. Heap memory can't be allocated while sweep is running.
    44  var (
    45  	finlock    mutex     // protects the following variables
    46  	fing       *g        // goroutine that runs finalizers
    47  	finq       *finblock // list of finalizers that are to be executed
    48  	finc       *finblock // cache of free blocks
    49  	finptrmask [_FinBlockSize / goarch.PtrSize / 8]byte
    50  )
    51  
    52  var allfin *finblock // list of all blocks
    53  
    54  // NOTE: Layout known to queuefinalizer.
    55  type finalizer struct {
    56  	fn   *funcval       // function to call (may be a heap pointer)
    57  	arg  unsafe.Pointer // ptr to object (may be a heap pointer)
    58  	nret uintptr        // bytes of return values from fn
    59  	fint *_type         // type of first argument of fn
    60  	ot   *ptrtype       // type of ptr to object (may be a heap pointer)
    61  }
    62  
    63  var finalizer1 = [...]byte{
    64  	// Each Finalizer is 5 words, ptr ptr INT ptr ptr (INT = uintptr here)
    65  	// Each byte describes 8 words.
    66  	// Need 8 Finalizers described by 5 bytes before pattern repeats:
    67  	//	ptr ptr INT ptr ptr
    68  	//	ptr ptr INT ptr ptr
    69  	//	ptr ptr INT ptr ptr
    70  	//	ptr ptr INT ptr ptr
    71  	//	ptr ptr INT ptr ptr
    72  	//	ptr ptr INT ptr ptr
    73  	//	ptr ptr INT ptr ptr
    74  	//	ptr ptr INT ptr ptr
    75  	// aka
    76  	//
    77  	//	ptr ptr INT ptr ptr ptr ptr INT
    78  	//	ptr ptr ptr ptr INT ptr ptr ptr
    79  	//	ptr INT ptr ptr ptr ptr INT ptr
    80  	//	ptr ptr ptr INT ptr ptr ptr ptr
    81  	//	INT ptr ptr ptr ptr INT ptr ptr
    82  	//
    83  	// Assumptions about Finalizer layout checked below.
    84  	1<<0 | 1<<1 | 0<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 0<<7,
    85  	1<<0 | 1<<1 | 1<<2 | 1<<3 | 0<<4 | 1<<5 | 1<<6 | 1<<7,
    86  	1<<0 | 0<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 0<<6 | 1<<7,
    87  	1<<0 | 1<<1 | 1<<2 | 0<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7,
    88  	0<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4 | 0<<5 | 1<<6 | 1<<7,
    89  }
    90  
    91  // lockRankMayQueueFinalizer records the lock ranking effects of a
    92  // function that may call queuefinalizer.
    93  func lockRankMayQueueFinalizer() {
    94  	lockWithRankMayAcquire(&finlock, getLockRank(&finlock))
    95  }
    96  
    97  func queuefinalizer(p unsafe.Pointer, fn *funcval, nret uintptr, fint *_type, ot *ptrtype) {
    98  	if gcphase != _GCoff {
    99  		// Currently we assume that the finalizer queue won't
   100  		// grow during marking so we don't have to rescan it
   101  		// during mark termination. If we ever need to lift
   102  		// this assumption, we can do it by adding the
   103  		// necessary barriers to queuefinalizer (which it may
   104  		// have automatically).
   105  		throw("queuefinalizer during GC")
   106  	}
   107  
   108  	lock(&finlock)
   109  	if finq == nil || finq.cnt == uint32(len(finq.fin)) {
   110  		if finc == nil {
   111  			finc = (*finblock)(persistentalloc(_FinBlockSize, 0, &memstats.gcMiscSys))
   112  			finc.alllink = allfin
   113  			allfin = finc
   114  			if finptrmask[0] == 0 {
   115  				// Build pointer mask for Finalizer array in block.
   116  				// Check assumptions made in finalizer1 array above.
   117  				if (unsafe.Sizeof(finalizer{}) != 5*goarch.PtrSize ||
   118  					unsafe.Offsetof(finalizer{}.fn) != 0 ||
   119  					unsafe.Offsetof(finalizer{}.arg) != goarch.PtrSize ||
   120  					unsafe.Offsetof(finalizer{}.nret) != 2*goarch.PtrSize ||
   121  					unsafe.Offsetof(finalizer{}.fint) != 3*goarch.PtrSize ||
   122  					unsafe.Offsetof(finalizer{}.ot) != 4*goarch.PtrSize) {
   123  					throw("finalizer out of sync")
   124  				}
   125  				for i := range finptrmask {
   126  					finptrmask[i] = finalizer1[i%len(finalizer1)]
   127  				}
   128  			}
   129  		}
   130  		block := finc
   131  		finc = block.next
   132  		block.next = finq
   133  		finq = block
   134  	}
   135  	f := &finq.fin[finq.cnt]
   136  	atomic.Xadd(&finq.cnt, +1) // Sync with markroots
   137  	f.fn = fn
   138  	f.nret = nret
   139  	f.fint = fint
   140  	f.ot = ot
   141  	f.arg = p
   142  	unlock(&finlock)
   143  	fingStatus.Or(fingWake)
   144  }
   145  
   146  //go:nowritebarrier
   147  func iterate_finq(callback func(*funcval, unsafe.Pointer, uintptr, *_type, *ptrtype)) {
   148  	for fb := allfin; fb != nil; fb = fb.alllink {
   149  		for i := uint32(0); i < fb.cnt; i++ {
   150  			f := &fb.fin[i]
   151  			callback(f.fn, f.arg, f.nret, f.fint, f.ot)
   152  		}
   153  	}
   154  }
   155  
   156  func wakefing() *g {
   157  	if ok := fingStatus.CompareAndSwap(fingCreated|fingWait|fingWake, fingCreated); ok {
   158  		return fing
   159  	}
   160  	return nil
   161  }
   162  
   163  func createfing() {
   164  	// start the finalizer goroutine exactly once
   165  	if fingStatus.Load() == fingUninitialized && fingStatus.CompareAndSwap(fingUninitialized, fingCreated) {
   166  		go runfinq()
   167  	}
   168  }
   169  
   170  func finalizercommit(gp *g, lock unsafe.Pointer) bool {
   171  	unlock((*mutex)(lock))
   172  	// fingStatus should be modified after fing is put into a waiting state
   173  	// to avoid waking fing in running state, even if it is about to be parked.
   174  	fingStatus.Or(fingWait)
   175  	return true
   176  }
   177  
   178  // This is the goroutine that runs all of the finalizers and cleanups.
   179  func runfinq() {
   180  	var (
   181  		frame    unsafe.Pointer
   182  		framecap uintptr
   183  		argRegs  int
   184  	)
   185  
   186  	gp := getg()
   187  	lock(&finlock)
   188  	fing = gp
   189  	unlock(&finlock)
   190  
   191  	for {
   192  		lock(&finlock)
   193  		fb := finq
   194  		finq = nil
   195  		if fb == nil {
   196  			gopark(finalizercommit, unsafe.Pointer(&finlock), waitReasonFinalizerWait, traceBlockSystemGoroutine, 1)
   197  			continue
   198  		}
   199  		argRegs = intArgRegs
   200  		unlock(&finlock)
   201  		if raceenabled {
   202  			racefingo()
   203  		}
   204  		for fb != nil {
   205  			for i := fb.cnt; i > 0; i-- {
   206  				f := &fb.fin[i-1]
   207  
   208  				// arg will only be nil when a cleanup has been queued.
   209  				if f.arg == nil {
   210  					var cleanup func()
   211  					fn := unsafe.Pointer(f.fn)
   212  					cleanup = *(*func())(unsafe.Pointer(&fn))
   213  					fingStatus.Or(fingRunningFinalizer)
   214  					cleanup()
   215  					fingStatus.And(^fingRunningFinalizer)
   216  
   217  					f.fn = nil
   218  					f.arg = nil
   219  					f.ot = nil
   220  					atomic.Store(&fb.cnt, i-1)
   221  					continue
   222  				}
   223  
   224  				var regs abi.RegArgs
   225  				// The args may be passed in registers or on stack. Even for
   226  				// the register case, we still need the spill slots.
   227  				// TODO: revisit if we remove spill slots.
   228  				//
   229  				// Unfortunately because we can have an arbitrary
   230  				// amount of returns and it would be complex to try and
   231  				// figure out how many of those can get passed in registers,
   232  				// just conservatively assume none of them do.
   233  				framesz := unsafe.Sizeof((any)(nil)) + f.nret
   234  				if framecap < framesz {
   235  					// The frame does not contain pointers interesting for GC,
   236  					// all not yet finalized objects are stored in finq.
   237  					// If we do not mark it as FlagNoScan,
   238  					// the last finalized object is not collected.
   239  					frame = mallocgc(framesz, nil, true)
   240  					framecap = framesz
   241  				}
   242  				// cleanups also have a nil fint. Cleanups should have been processed before
   243  				// reaching this point.
   244  				if f.fint == nil {
   245  					throw("missing type in runfinq")
   246  				}
   247  				r := frame
   248  				if argRegs > 0 {
   249  					r = unsafe.Pointer(&regs.Ints)
   250  				} else {
   251  					// frame is effectively uninitialized
   252  					// memory. That means we have to clear
   253  					// it before writing to it to avoid
   254  					// confusing the write barrier.
   255  					*(*[2]uintptr)(frame) = [2]uintptr{}
   256  				}
   257  				switch f.fint.Kind_ & abi.KindMask {
   258  				case abi.Pointer:
   259  					// direct use of pointer
   260  					*(*unsafe.Pointer)(r) = f.arg
   261  				case abi.Interface:
   262  					ityp := (*interfacetype)(unsafe.Pointer(f.fint))
   263  					// set up with empty interface
   264  					(*eface)(r)._type = &f.ot.Type
   265  					(*eface)(r).data = f.arg
   266  					if len(ityp.Methods) != 0 {
   267  						// convert to interface with methods
   268  						// this conversion is guaranteed to succeed - we checked in SetFinalizer
   269  						(*iface)(r).tab = assertE2I(ityp, (*eface)(r)._type)
   270  					}
   271  				default:
   272  					throw("bad kind in runfinq")
   273  				}
   274  				fingStatus.Or(fingRunningFinalizer)
   275  				reflectcall(nil, unsafe.Pointer(f.fn), frame, uint32(framesz), uint32(framesz), uint32(framesz), &regs)
   276  				fingStatus.And(^fingRunningFinalizer)
   277  
   278  				// Drop finalizer queue heap references
   279  				// before hiding them from markroot.
   280  				// This also ensures these will be
   281  				// clear if we reuse the finalizer.
   282  				f.fn = nil
   283  				f.arg = nil
   284  				f.ot = nil
   285  				atomic.Store(&fb.cnt, i-1)
   286  			}
   287  			next := fb.next
   288  			lock(&finlock)
   289  			fb.next = finc
   290  			finc = fb
   291  			unlock(&finlock)
   292  			fb = next
   293  		}
   294  	}
   295  }
   296  
   297  func isGoPointerWithoutSpan(p unsafe.Pointer) bool {
   298  	// 0-length objects are okay.
   299  	if p == unsafe.Pointer(&zerobase) {
   300  		return true
   301  	}
   302  
   303  	// Global initializers might be linker-allocated.
   304  	//	var Foo = &Object{}
   305  	//	func main() {
   306  	//		runtime.SetFinalizer(Foo, nil)
   307  	//	}
   308  	// The relevant segments are: noptrdata, data, bss, noptrbss.
   309  	// We cannot assume they are in any order or even contiguous,
   310  	// due to external linking.
   311  	for datap := &firstmoduledata; datap != nil; datap = datap.next {
   312  		if datap.noptrdata <= uintptr(p) && uintptr(p) < datap.enoptrdata ||
   313  			datap.data <= uintptr(p) && uintptr(p) < datap.edata ||
   314  			datap.bss <= uintptr(p) && uintptr(p) < datap.ebss ||
   315  			datap.noptrbss <= uintptr(p) && uintptr(p) < datap.enoptrbss {
   316  			return true
   317  		}
   318  	}
   319  	return false
   320  }
   321  
   322  // blockUntilEmptyFinalizerQueue blocks until either the finalizer
   323  // queue is emptied (and the finalizers have executed) or the timeout
   324  // is reached. Returns true if the finalizer queue was emptied.
   325  // This is used by the runtime and sync tests.
   326  func blockUntilEmptyFinalizerQueue(timeout int64) bool {
   327  	start := nanotime()
   328  	for nanotime()-start < timeout {
   329  		lock(&finlock)
   330  		// We know the queue has been drained when both finq is nil
   331  		// and the finalizer g has stopped executing.
   332  		empty := finq == nil
   333  		empty = empty && readgstatus(fing) == _Gwaiting && fing.waitreason == waitReasonFinalizerWait
   334  		unlock(&finlock)
   335  		if empty {
   336  			return true
   337  		}
   338  		Gosched()
   339  	}
   340  	return false
   341  }
   342  
   343  // SetFinalizer sets the finalizer associated with obj to the provided
   344  // finalizer function. When the garbage collector finds an unreachable block
   345  // with an associated finalizer, it clears the association and runs
   346  // finalizer(obj) in a separate goroutine. This makes obj reachable again,
   347  // but now without an associated finalizer. Assuming that SetFinalizer
   348  // is not called again, the next time the garbage collector sees
   349  // that obj is unreachable, it will free obj.
   350  //
   351  // SetFinalizer(obj, nil) clears any finalizer associated with obj.
   352  //
   353  // New Go code should consider using [AddCleanup] instead, which is much
   354  // less error-prone than SetFinalizer.
   355  //
   356  // The argument obj must be a pointer to an object allocated by calling
   357  // new, by taking the address of a composite literal, or by taking the
   358  // address of a local variable.
   359  // The argument finalizer must be a function that takes a single argument
   360  // to which obj's type can be assigned, and can have arbitrary ignored return
   361  // values. If either of these is not true, SetFinalizer may abort the
   362  // program.
   363  //
   364  // Finalizers are run in dependency order: if A points at B, both have
   365  // finalizers, and they are otherwise unreachable, only the finalizer
   366  // for A runs; once A is freed, the finalizer for B can run.
   367  // If a cyclic structure includes a block with a finalizer, that
   368  // cycle is not guaranteed to be garbage collected and the finalizer
   369  // is not guaranteed to run, because there is no ordering that
   370  // respects the dependencies.
   371  //
   372  // The finalizer is scheduled to run at some arbitrary time after the
   373  // program can no longer reach the object to which obj points.
   374  // There is no guarantee that finalizers will run before a program exits,
   375  // so typically they are useful only for releasing non-memory resources
   376  // associated with an object during a long-running program.
   377  // For example, an [os.File] object could use a finalizer to close the
   378  // associated operating system file descriptor when a program discards
   379  // an os.File without calling Close, but it would be a mistake
   380  // to depend on a finalizer to flush an in-memory I/O buffer such as a
   381  // [bufio.Writer], because the buffer would not be flushed at program exit.
   382  //
   383  // It is not guaranteed that a finalizer will run if the size of *obj is
   384  // zero bytes, because it may share same address with other zero-size
   385  // objects in memory. See https://go.dev/ref/spec#Size_and_alignment_guarantees.
   386  //
   387  // It is not guaranteed that a finalizer will run for objects allocated
   388  // in initializers for package-level variables. Such objects may be
   389  // linker-allocated, not heap-allocated.
   390  //
   391  // Note that because finalizers may execute arbitrarily far into the future
   392  // after an object is no longer referenced, the runtime is allowed to perform
   393  // a space-saving optimization that batches objects together in a single
   394  // allocation slot. The finalizer for an unreferenced object in such an
   395  // allocation may never run if it always exists in the same batch as a
   396  // referenced object. Typically, this batching only happens for tiny
   397  // (on the order of 16 bytes or less) and pointer-free objects.
   398  //
   399  // A finalizer may run as soon as an object becomes unreachable.
   400  // In order to use finalizers correctly, the program must ensure that
   401  // the object is reachable until it is no longer required.
   402  // Objects stored in global variables, or that can be found by tracing
   403  // pointers from a global variable, are reachable. A function argument or
   404  // receiver may become unreachable at the last point where the function
   405  // mentions it. To make an unreachable object reachable, pass the object
   406  // to a call of the [KeepAlive] function to mark the last point in the
   407  // function where the object must be reachable.
   408  //
   409  // For example, if p points to a struct, such as os.File, that contains
   410  // a file descriptor d, and p has a finalizer that closes that file
   411  // descriptor, and if the last use of p in a function is a call to
   412  // syscall.Write(p.d, buf, size), then p may be unreachable as soon as
   413  // the program enters [syscall.Write]. The finalizer may run at that moment,
   414  // closing p.d, causing syscall.Write to fail because it is writing to
   415  // a closed file descriptor (or, worse, to an entirely different
   416  // file descriptor opened by a different goroutine). To avoid this problem,
   417  // call KeepAlive(p) after the call to syscall.Write.
   418  //
   419  // A single goroutine runs all finalizers for a program, sequentially.
   420  // If a finalizer must run for a long time, it should do so by starting
   421  // a new goroutine.
   422  //
   423  // In the terminology of the Go memory model, a call
   424  // SetFinalizer(x, f) “synchronizes before” the finalization call f(x).
   425  // However, there is no guarantee that KeepAlive(x) or any other use of x
   426  // “synchronizes before” f(x), so in general a finalizer should use a mutex
   427  // or other synchronization mechanism if it needs to access mutable state in x.
   428  // For example, consider a finalizer that inspects a mutable field in x
   429  // that is modified from time to time in the main program before x
   430  // becomes unreachable and the finalizer is invoked.
   431  // The modifications in the main program and the inspection in the finalizer
   432  // need to use appropriate synchronization, such as mutexes or atomic updates,
   433  // to avoid read-write races.
   434  func SetFinalizer(obj any, finalizer any) {
   435  	e := efaceOf(&obj)
   436  	etyp := e._type
   437  	if etyp == nil {
   438  		throw("runtime.SetFinalizer: first argument is nil")
   439  	}
   440  	if etyp.Kind_&abi.KindMask != abi.Pointer {
   441  		throw("runtime.SetFinalizer: first argument is " + toRType(etyp).string() + ", not pointer")
   442  	}
   443  	ot := (*ptrtype)(unsafe.Pointer(etyp))
   444  	if ot.Elem == nil {
   445  		throw("nil elem type!")
   446  	}
   447  	if inUserArenaChunk(uintptr(e.data)) {
   448  		// Arena-allocated objects are not eligible for finalizers.
   449  		throw("runtime.SetFinalizer: first argument was allocated into an arena")
   450  	}
   451  	if debug.sbrk != 0 {
   452  		// debug.sbrk never frees memory, so no finalizers run
   453  		// (and we don't have the data structures to record them).
   454  		return
   455  	}
   456  
   457  	// find the containing object
   458  	base, span, _ := findObject(uintptr(e.data), 0, 0)
   459  
   460  	if base == 0 {
   461  		if isGoPointerWithoutSpan(e.data) {
   462  			return
   463  		}
   464  		throw("runtime.SetFinalizer: pointer not in allocated block")
   465  	}
   466  
   467  	// Move base forward if we've got an allocation header.
   468  	if !span.spanclass.noscan() && !heapBitsInSpan(span.elemsize) && span.spanclass.sizeclass() != 0 {
   469  		base += mallocHeaderSize
   470  	}
   471  
   472  	if uintptr(e.data) != base {
   473  		// As an implementation detail we allow to set finalizers for an inner byte
   474  		// of an object if it could come from tiny alloc (see mallocgc for details).
   475  		if ot.Elem == nil || ot.Elem.Pointers() || ot.Elem.Size_ >= maxTinySize {
   476  			throw("runtime.SetFinalizer: pointer not at beginning of allocated block")
   477  		}
   478  	}
   479  
   480  	f := efaceOf(&finalizer)
   481  	ftyp := f._type
   482  	if ftyp == nil {
   483  		// switch to system stack and remove finalizer
   484  		systemstack(func() {
   485  			removefinalizer(e.data)
   486  		})
   487  		return
   488  	}
   489  
   490  	if ftyp.Kind_&abi.KindMask != abi.Func {
   491  		throw("runtime.SetFinalizer: second argument is " + toRType(ftyp).string() + ", not a function")
   492  	}
   493  	ft := (*functype)(unsafe.Pointer(ftyp))
   494  	if ft.IsVariadic() {
   495  		throw("runtime.SetFinalizer: cannot pass " + toRType(etyp).string() + " to finalizer " + toRType(ftyp).string() + " because dotdotdot")
   496  	}
   497  	if ft.InCount != 1 {
   498  		throw("runtime.SetFinalizer: cannot pass " + toRType(etyp).string() + " to finalizer " + toRType(ftyp).string())
   499  	}
   500  	fint := ft.InSlice()[0]
   501  	switch {
   502  	case fint == etyp:
   503  		// ok - same type
   504  		goto okarg
   505  	case fint.Kind_&abi.KindMask == abi.Pointer:
   506  		if (fint.Uncommon() == nil || etyp.Uncommon() == nil) && (*ptrtype)(unsafe.Pointer(fint)).Elem == ot.Elem {
   507  			// ok - not same type, but both pointers,
   508  			// one or the other is unnamed, and same element type, so assignable.
   509  			goto okarg
   510  		}
   511  	case fint.Kind_&abi.KindMask == abi.Interface:
   512  		ityp := (*interfacetype)(unsafe.Pointer(fint))
   513  		if len(ityp.Methods) == 0 {
   514  			// ok - satisfies empty interface
   515  			goto okarg
   516  		}
   517  		if itab := assertE2I2(ityp, efaceOf(&obj)._type); itab != nil {
   518  			goto okarg
   519  		}
   520  	}
   521  	throw("runtime.SetFinalizer: cannot pass " + toRType(etyp).string() + " to finalizer " + toRType(ftyp).string())
   522  okarg:
   523  	// compute size needed for return parameters
   524  	nret := uintptr(0)
   525  	for _, t := range ft.OutSlice() {
   526  		nret = alignUp(nret, uintptr(t.Align_)) + t.Size_
   527  	}
   528  	nret = alignUp(nret, goarch.PtrSize)
   529  
   530  	// make sure we have a finalizer goroutine
   531  	createfing()
   532  
   533  	systemstack(func() {
   534  		if !addfinalizer(e.data, (*funcval)(f.data), nret, fint, ot) {
   535  			throw("runtime.SetFinalizer: finalizer already set")
   536  		}
   537  	})
   538  }
   539  
   540  // Mark KeepAlive as noinline so that it is easily detectable as an intrinsic.
   541  //
   542  //go:noinline
   543  
   544  // KeepAlive marks its argument as currently reachable.
   545  // This ensures that the object is not freed, and its finalizer is not run,
   546  // before the point in the program where KeepAlive is called.
   547  //
   548  // A very simplified example showing where KeepAlive is required:
   549  //
   550  //	type File struct { d int }
   551  //	d, err := syscall.Open("/file/path", syscall.O_RDONLY, 0)
   552  //	// ... do something if err != nil ...
   553  //	p := &File{d}
   554  //	runtime.SetFinalizer(p, func(p *File) { syscall.Close(p.d) })
   555  //	var buf [10]byte
   556  //	n, err := syscall.Read(p.d, buf[:])
   557  //	// Ensure p is not finalized until Read returns.
   558  //	runtime.KeepAlive(p)
   559  //	// No more uses of p after this point.
   560  //
   561  // Without the KeepAlive call, the finalizer could run at the start of
   562  // [syscall.Read], closing the file descriptor before syscall.Read makes
   563  // the actual system call.
   564  //
   565  // Note: KeepAlive should only be used to prevent finalizers from
   566  // running prematurely. In particular, when used with [unsafe.Pointer],
   567  // the rules for valid uses of unsafe.Pointer still apply.
   568  func KeepAlive(x any) {
   569  	// Introduce a use of x that the compiler can't eliminate.
   570  	// This makes sure x is alive on entry. We need x to be alive
   571  	// on entry for "defer runtime.KeepAlive(x)"; see issue 21402.
   572  	if cgoAlwaysFalse {
   573  		println(x)
   574  	}
   575  }
   576  

View as plain text