]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/mfinal.go
runtime: avoid write barriers to uninitialized finalizer frame memory
[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         "runtime/internal/atomic"
11         "runtime/internal/sys"
12         "unsafe"
13 )
14
15 // finblock is allocated from non-GC'd memory, so any heap pointers
16 // must be specially handled.
17 //
18 //go:notinheap
19 type finblock struct {
20         alllink *finblock
21         next    *finblock
22         cnt     int32
23         _       int32
24         fin     [(_FinBlockSize - 2*sys.PtrSize - 2*4) / unsafe.Sizeof(finalizer{})]finalizer
25 }
26
27 var finlock mutex  // protects the following variables
28 var fing *g        // goroutine that runs finalizers
29 var finq *finblock // list of finalizers that are to be executed
30 var finc *finblock // cache of free blocks
31 var finptrmask [_FinBlockSize / sys.PtrSize / 8]byte
32 var fingwait bool
33 var fingwake bool
34 var allfin *finblock // list of all blocks
35
36 // NOTE: Layout known to queuefinalizer.
37 type finalizer struct {
38         fn   *funcval       // function to call (may be a heap pointer)
39         arg  unsafe.Pointer // ptr to object (may be a heap pointer)
40         nret uintptr        // bytes of return values from fn
41         fint *_type         // type of first argument of fn
42         ot   *ptrtype       // type of ptr to object (may be a heap pointer)
43 }
44
45 var finalizer1 = [...]byte{
46         // Each Finalizer is 5 words, ptr ptr INT ptr ptr (INT = uintptr here)
47         // Each byte describes 8 words.
48         // Need 8 Finalizers described by 5 bytes before pattern repeats:
49         //      ptr ptr INT ptr ptr
50         //      ptr ptr INT ptr ptr
51         //      ptr ptr INT ptr ptr
52         //      ptr ptr INT ptr ptr
53         //      ptr ptr INT ptr ptr
54         //      ptr ptr INT ptr ptr
55         //      ptr ptr INT ptr ptr
56         //      ptr ptr INT ptr ptr
57         // aka
58         //
59         //      ptr ptr INT ptr ptr ptr ptr INT
60         //      ptr ptr ptr ptr INT ptr ptr ptr
61         //      ptr INT ptr ptr ptr ptr INT ptr
62         //      ptr ptr ptr INT ptr ptr ptr ptr
63         //      INT ptr ptr ptr ptr INT ptr ptr
64         //
65         // Assumptions about Finalizer layout checked below.
66         1<<0 | 1<<1 | 0<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 0<<7,
67         1<<0 | 1<<1 | 1<<2 | 1<<3 | 0<<4 | 1<<5 | 1<<6 | 1<<7,
68         1<<0 | 0<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 0<<6 | 1<<7,
69         1<<0 | 1<<1 | 1<<2 | 0<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7,
70         0<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4 | 0<<5 | 1<<6 | 1<<7,
71 }
72
73 func queuefinalizer(p unsafe.Pointer, fn *funcval, nret uintptr, fint *_type, ot *ptrtype) {
74         lock(&finlock)
75         if finq == nil || finq.cnt == int32(len(finq.fin)) {
76                 if finc == nil {
77                         finc = (*finblock)(persistentalloc(_FinBlockSize, 0, &memstats.gc_sys))
78                         finc.alllink = allfin
79                         allfin = finc
80                         if finptrmask[0] == 0 {
81                                 // Build pointer mask for Finalizer array in block.
82                                 // Check assumptions made in finalizer1 array above.
83                                 if (unsafe.Sizeof(finalizer{}) != 5*sys.PtrSize ||
84                                         unsafe.Offsetof(finalizer{}.fn) != 0 ||
85                                         unsafe.Offsetof(finalizer{}.arg) != sys.PtrSize ||
86                                         unsafe.Offsetof(finalizer{}.nret) != 2*sys.PtrSize ||
87                                         unsafe.Offsetof(finalizer{}.fint) != 3*sys.PtrSize ||
88                                         unsafe.Offsetof(finalizer{}.ot) != 4*sys.PtrSize) {
89                                         throw("finalizer out of sync")
90                                 }
91                                 for i := range finptrmask {
92                                         finptrmask[i] = finalizer1[i%len(finalizer1)]
93                                 }
94                         }
95                 }
96                 block := finc
97                 finc = block.next
98                 block.next = finq
99                 finq = block
100         }
101         f := &finq.fin[finq.cnt]
102         finq.cnt++
103         f.fn = fn
104         f.nret = nret
105         f.fint = fint
106         f.ot = ot
107         f.arg = p
108         fingwake = true
109         unlock(&finlock)
110 }
111
112 //go:nowritebarrier
113 func iterate_finq(callback func(*funcval, unsafe.Pointer, uintptr, *_type, *ptrtype)) {
114         for fb := allfin; fb != nil; fb = fb.alllink {
115                 for i := int32(0); i < fb.cnt; i++ {
116                         f := &fb.fin[i]
117                         callback(f.fn, f.arg, f.nret, f.fint, f.ot)
118                 }
119         }
120 }
121
122 func wakefing() *g {
123         var res *g
124         lock(&finlock)
125         if fingwait && fingwake {
126                 fingwait = false
127                 fingwake = false
128                 res = fing
129         }
130         unlock(&finlock)
131         return res
132 }
133
134 var (
135         fingCreate  uint32
136         fingRunning bool
137 )
138
139 func createfing() {
140         // start the finalizer goroutine exactly once
141         if fingCreate == 0 && atomic.Cas(&fingCreate, 0, 1) {
142                 go runfinq()
143         }
144 }
145
146 // This is the goroutine that runs all of the finalizers
147 func runfinq() {
148         var (
149                 frame    unsafe.Pointer
150                 framecap uintptr
151         )
152
153         for {
154                 lock(&finlock)
155                 fb := finq
156                 finq = nil
157                 if fb == nil {
158                         gp := getg()
159                         fing = gp
160                         fingwait = true
161                         goparkunlock(&finlock, "finalizer wait", traceEvGoBlock, 1)
162                         continue
163                 }
164                 unlock(&finlock)
165                 if raceenabled {
166                         racefingo()
167                 }
168                 for fb != nil {
169                         for i := fb.cnt; i > 0; i-- {
170                                 f := &fb.fin[i-1]
171
172                                 framesz := unsafe.Sizeof((interface{})(nil)) + f.nret
173                                 if framecap < framesz {
174                                         // The frame does not contain pointers interesting for GC,
175                                         // all not yet finalized objects are stored in finq.
176                                         // If we do not mark it as FlagNoScan,
177                                         // the last finalized object is not collected.
178                                         frame = mallocgc(framesz, nil, true)
179                                         framecap = framesz
180                                 }
181
182                                 if f.fint == nil {
183                                         throw("missing type in runfinq")
184                                 }
185                                 // frame is effectively uninitialized
186                                 // memory. That means we have to clear
187                                 // it before writing to it to avoid
188                                 // confusing the write barrier.
189                                 *(*[2]uintptr)(frame) = [2]uintptr{}
190                                 switch f.fint.kind & kindMask {
191                                 case kindPtr:
192                                         // direct use of pointer
193                                         *(*unsafe.Pointer)(frame) = f.arg
194                                 case kindInterface:
195                                         ityp := (*interfacetype)(unsafe.Pointer(f.fint))
196                                         // set up with empty interface
197                                         (*eface)(frame)._type = &f.ot.typ
198                                         (*eface)(frame).data = f.arg
199                                         if len(ityp.mhdr) != 0 {
200                                                 // convert to interface with methods
201                                                 // this conversion is guaranteed to succeed - we checked in SetFinalizer
202                                                 assertE2I(ityp, *(*eface)(frame), (*iface)(frame))
203                                         }
204                                 default:
205                                         throw("bad kind in runfinq")
206                                 }
207                                 fingRunning = true
208                                 reflectcall(nil, unsafe.Pointer(f.fn), frame, uint32(framesz), uint32(framesz))
209                                 fingRunning = false
210
211                                 // drop finalizer queue references to finalized object
212                                 f.fn = nil
213                                 f.arg = nil
214                                 f.ot = nil
215                                 fb.cnt = i - 1
216                         }
217                         next := fb.next
218                         lock(&finlock)
219                         fb.next = finc
220                         finc = fb
221                         unlock(&finlock)
222                         fb = next
223                 }
224         }
225 }
226
227 // SetFinalizer sets the finalizer associated with obj to the provided
228 // finalizer function. When the garbage collector finds an unreachable block
229 // with an associated finalizer, it clears the association and runs
230 // finalizer(obj) in a separate goroutine. This makes obj reachable again,
231 // but now without an associated finalizer. Assuming that SetFinalizer
232 // is not called again, the next time the garbage collector sees
233 // that obj is unreachable, it will free obj.
234 //
235 // SetFinalizer(obj, nil) clears any finalizer associated with obj.
236 //
237 // The argument obj must be a pointer to an object allocated by calling
238 // new, by taking the address of a composite literal, or by taking the
239 // address of a local variable.
240 // The argument finalizer must be a function that takes a single argument
241 // to which obj's type can be assigned, and can have arbitrary ignored return
242 // values. If either of these is not true, SetFinalizer may abort the
243 // program.
244 //
245 // Finalizers are run in dependency order: if A points at B, both have
246 // finalizers, and they are otherwise unreachable, only the finalizer
247 // for A runs; once A is freed, the finalizer for B can run.
248 // If a cyclic structure includes a block with a finalizer, that
249 // cycle is not guaranteed to be garbage collected and the finalizer
250 // is not guaranteed to run, because there is no ordering that
251 // respects the dependencies.
252 //
253 // The finalizer for obj is scheduled to run at some arbitrary time after
254 // obj becomes unreachable.
255 // There is no guarantee that finalizers will run before a program exits,
256 // so typically they are useful only for releasing non-memory resources
257 // associated with an object during a long-running program.
258 // For example, an os.File object could use a finalizer to close the
259 // associated operating system file descriptor when a program discards
260 // an os.File without calling Close, but it would be a mistake
261 // to depend on a finalizer to flush an in-memory I/O buffer such as a
262 // bufio.Writer, because the buffer would not be flushed at program exit.
263 //
264 // It is not guaranteed that a finalizer will run if the size of *obj is
265 // zero bytes.
266 //
267 // It is not guaranteed that a finalizer will run for objects allocated
268 // in initializers for package-level variables. Such objects may be
269 // linker-allocated, not heap-allocated.
270 //
271 // A finalizer may run as soon as an object becomes unreachable.
272 // In order to use finalizers correctly, the program must ensure that
273 // the object is reachable until it is no longer required.
274 // Objects stored in global variables, or that can be found by tracing
275 // pointers from a global variable, are reachable. For other objects,
276 // pass the object to a call of the KeepAlive function to mark the
277 // last point in the function where the object must be reachable.
278 //
279 // For example, if p points to a struct that contains a file descriptor d,
280 // and p has a finalizer that closes that file descriptor, and if the last
281 // use of p in a function is a call to syscall.Write(p.d, buf, size), then
282 // p may be unreachable as soon as the program enters syscall.Write. The
283 // finalizer may run at that moment, closing p.d, causing syscall.Write
284 // to fail because it is writing to a closed file descriptor (or, worse,
285 // to an entirely different file descriptor opened by a different goroutine).
286 // To avoid this problem, call runtime.KeepAlive(p) after the call to
287 // syscall.Write.
288 //
289 // A single goroutine runs all finalizers for a program, sequentially.
290 // If a finalizer must run for a long time, it should do so by starting
291 // a new goroutine.
292 func SetFinalizer(obj interface{}, finalizer interface{}) {
293         if debug.sbrk != 0 {
294                 // debug.sbrk never frees memory, so no finalizers run
295                 // (and we don't have the data structures to record them).
296                 return
297         }
298         e := efaceOf(&obj)
299         etyp := e._type
300         if etyp == nil {
301                 throw("runtime.SetFinalizer: first argument is nil")
302         }
303         if etyp.kind&kindMask != kindPtr {
304                 throw("runtime.SetFinalizer: first argument is " + etyp.string() + ", not pointer")
305         }
306         ot := (*ptrtype)(unsafe.Pointer(etyp))
307         if ot.elem == nil {
308                 throw("nil elem type!")
309         }
310
311         // find the containing object
312         _, base, _ := findObject(e.data)
313
314         if base == nil {
315                 // 0-length objects are okay.
316                 if e.data == unsafe.Pointer(&zerobase) {
317                         return
318                 }
319
320                 // Global initializers might be linker-allocated.
321                 //      var Foo = &Object{}
322                 //      func main() {
323                 //              runtime.SetFinalizer(Foo, nil)
324                 //      }
325                 // The relevant segments are: noptrdata, data, bss, noptrbss.
326                 // We cannot assume they are in any order or even contiguous,
327                 // due to external linking.
328                 for datap := &firstmoduledata; datap != nil; datap = datap.next {
329                         if datap.noptrdata <= uintptr(e.data) && uintptr(e.data) < datap.enoptrdata ||
330                                 datap.data <= uintptr(e.data) && uintptr(e.data) < datap.edata ||
331                                 datap.bss <= uintptr(e.data) && uintptr(e.data) < datap.ebss ||
332                                 datap.noptrbss <= uintptr(e.data) && uintptr(e.data) < datap.enoptrbss {
333                                 return
334                         }
335                 }
336                 throw("runtime.SetFinalizer: pointer not in allocated block")
337         }
338
339         if e.data != base {
340                 // As an implementation detail we allow to set finalizers for an inner byte
341                 // of an object if it could come from tiny alloc (see mallocgc for details).
342                 if ot.elem == nil || ot.elem.kind&kindNoPointers == 0 || ot.elem.size >= maxTinySize {
343                         throw("runtime.SetFinalizer: pointer not at beginning of allocated block")
344                 }
345         }
346
347         f := efaceOf(&finalizer)
348         ftyp := f._type
349         if ftyp == nil {
350                 // switch to system stack and remove finalizer
351                 systemstack(func() {
352                         removefinalizer(e.data)
353                 })
354                 return
355         }
356
357         if ftyp.kind&kindMask != kindFunc {
358                 throw("runtime.SetFinalizer: second argument is " + ftyp.string() + ", not a function")
359         }
360         ft := (*functype)(unsafe.Pointer(ftyp))
361         if ft.dotdotdot() {
362                 throw("runtime.SetFinalizer: cannot pass " + etyp.string() + " to finalizer " + ftyp.string() + " because dotdotdot")
363         }
364         if ft.inCount != 1 {
365                 throw("runtime.SetFinalizer: cannot pass " + etyp.string() + " to finalizer " + ftyp.string())
366         }
367         fint := ft.in()[0]
368         switch {
369         case fint == etyp:
370                 // ok - same type
371                 goto okarg
372         case fint.kind&kindMask == kindPtr:
373                 if (fint.uncommon() == nil || etyp.uncommon() == nil) && (*ptrtype)(unsafe.Pointer(fint)).elem == ot.elem {
374                         // ok - not same type, but both pointers,
375                         // one or the other is unnamed, and same element type, so assignable.
376                         goto okarg
377                 }
378         case fint.kind&kindMask == kindInterface:
379                 ityp := (*interfacetype)(unsafe.Pointer(fint))
380                 if len(ityp.mhdr) == 0 {
381                         // ok - satisfies empty interface
382                         goto okarg
383                 }
384                 if assertE2I2(ityp, *efaceOf(&obj), nil) {
385                         goto okarg
386                 }
387         }
388         throw("runtime.SetFinalizer: cannot pass " + etyp.string() + " to finalizer " + ftyp.string())
389 okarg:
390         // compute size needed for return parameters
391         nret := uintptr(0)
392         for _, t := range ft.out() {
393                 nret = round(nret, uintptr(t.align)) + uintptr(t.size)
394         }
395         nret = round(nret, sys.PtrSize)
396
397         // make sure we have a finalizer goroutine
398         createfing()
399
400         systemstack(func() {
401                 if !addfinalizer(e.data, (*funcval)(f.data), nret, fint, ot) {
402                         throw("runtime.SetFinalizer: finalizer already set")
403                 }
404         })
405 }
406
407 // Look up pointer v in heap. Return the span containing the object,
408 // the start of the object, and the size of the object. If the object
409 // does not exist, return nil, nil, 0.
410 func findObject(v unsafe.Pointer) (s *mspan, x unsafe.Pointer, n uintptr) {
411         c := gomcache()
412         c.local_nlookup++
413         if sys.PtrSize == 4 && c.local_nlookup >= 1<<30 {
414                 // purge cache stats to prevent overflow
415                 lock(&mheap_.lock)
416                 purgecachedstats(c)
417                 unlock(&mheap_.lock)
418         }
419
420         // find span
421         arena_start := mheap_.arena_start
422         arena_used := mheap_.arena_used
423         if uintptr(v) < arena_start || uintptr(v) >= arena_used {
424                 return
425         }
426         p := uintptr(v) >> pageShift
427         q := p - arena_start>>pageShift
428         s = mheap_.spans[q]
429         if s == nil {
430                 return
431         }
432         x = unsafe.Pointer(s.base())
433
434         if uintptr(v) < uintptr(x) || uintptr(v) >= uintptr(unsafe.Pointer(s.limit)) || s.state != mSpanInUse {
435                 s = nil
436                 x = nil
437                 return
438         }
439
440         n = s.elemsize
441         if s.sizeclass != 0 {
442                 x = add(x, (uintptr(v)-uintptr(x))/n*n)
443         }
444         return
445 }
446
447 // Mark KeepAlive as noinline so that the current compiler will ensure
448 // that the argument is alive at the point of the function call.
449 // If it were inlined, it would disappear, and there would be nothing
450 // keeping the argument alive. Perhaps a future compiler will recognize
451 // runtime.KeepAlive specially and do something more efficient.
452 //go:noinline
453
454 // KeepAlive marks its argument as currently reachable.
455 // This ensures that the object is not freed, and its finalizer is not run,
456 // before the point in the program where KeepAlive is called.
457 //
458 // A very simplified example showing where KeepAlive is required:
459 //      type File struct { d int }
460 //      d, err := syscall.Open("/file/path", syscall.O_RDONLY, 0)
461 //      // ... do something if err != nil ...
462 //      p := &File{d}
463 //      runtime.SetFinalizer(p, func(p *File) { syscall.Close(p.d) })
464 //      var buf [10]byte
465 //      n, err := syscall.Read(p.d, buf[:])
466 //      // Ensure p is not finalized until Read returns.
467 //      runtime.KeepAlive(p)
468 //      // No more uses of p after this point.
469 //
470 // Without the KeepAlive call, the finalizer could run at the start of
471 // syscall.Read, closing the file descriptor before syscall.Read makes
472 // the actual system call.
473 func KeepAlive(interface{}) {}