Source file
test/fixedbugs/issue27518b.go
1
2
3
4
5
6
7 package main
8
9 import "runtime"
10
11 var finalized bool
12 var err string
13
14 type HeapObj [8]int64
15
16 const filler int64 = 0x123456789abcdef0
17
18 func (h *HeapObj) init() {
19 for i := 0; i < len(*h); i++ {
20 h[i] = filler
21 }
22 }
23 func (h *HeapObj) check() {
24 for i := 0; i < len(*h); i++ {
25 if h[i] != filler {
26 err = "filler overwritten"
27 }
28 }
29 }
30
31 type StackObj struct {
32 h *HeapObj
33 }
34
35 func gc(shouldFinalize bool) {
36 runtime.GC()
37 runtime.GC()
38 runtime.GC()
39 if shouldFinalize != finalized {
40 err = "heap object finalized at the wrong time"
41 }
42 }
43
44 func main() {
45 var s StackObj
46 s.h = new(HeapObj)
47 s.h.init()
48 runtime.SetFinalizer(s.h, func(h *HeapObj) {
49 finalized = true
50 })
51 gc(false)
52 h := g(&s)
53 gc(false)
54 h.check()
55 gc(true)
56 if err != "" {
57 panic(err)
58 }
59 }
60
61 func g(p *StackObj) (v *HeapObj) {
62 gc(false)
63 v = p.h
64 gc(false)
65 defer func() {
66 gc(false)
67 recover()
68 gc(false)
69 }()
70 *(*int)(nil) = 0
71 return
72 }
73
View as plain text