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