1 // A sovereign receiver's arena is copy-compacted under pressure, and the
2 // compacted blocks must keep their descriptors on BOTH compilers. The legacy
3 // compiler passed the TinyGo object-layout bitmap as the allocation's type
4 // descriptor while stage4 passed a RawType; the runtime then walked live nodes
5 // as if a bitmap were a descriptor, dropped every block nested under a
6 // receiver root, and the next method call faulted on the unmapped old arena.
7 //
8 // Four things are pinned here:
9 // (a) live data intact across a compaction (chain, items slice, map value);
10 // (b) the live set is relocated (FirstAddr changes);
11 // (c) a frame holding a kind-9 root (a map) refuses the whole drain, and the
12 // refusal is counted instead of being silent;
13 // (d) an allocation between two copy steps cancels the paused cycle rather
14 // than letting it finish over a stale forwarding map.
15 package main
16 17 import (
18 "runtime"
19 "tests/regressions/should_compile/sovereign_arena/lib"
20 )
21 22 // refuseFrame has a map local before its select, so the frame's root map holds
23 // a kind-9 root and the drain must decline to compact anything at that point.
24 func refuseFrame() (ok bool) {
25 m := map[int32]int32{}
26 m[1] = 2
27 select {
28 case <-shutdown:
29 default:
30 }
31 return runtime.SovRefusals() > 0 && runtime.SovCompactions() == 0
32 }
33 34 func main() {
35 // Termination: a batch program must still be stoppable.
36 select {
37 case <-shutdown:
38 return
39 default:
40 }
41 42 var s lib.Store
43 n := int32(40000)
44 sum := int32(0)
45 for i := int32(0); i < n; i++ {
46 s.Add("k", i)
47 sum += i
48 }
49 // Sum counts the linked chain and the items slice, so 2*sum.
50 if s.Sum() != 2*sum {
51 panic("live data wrong before compaction")
52 }
53 before := s.FirstAddr()
54 55 // (c) A kind-9 frame refuses the drain; the queued group must stay queued.
56 if !refuseFrame() {
57 panic("kind-9 frame did not refuse compaction")
58 }
59 60 // (d) A one-byte budget starts a cycle and pauses it after one move.
61 runtime.SovSetCopyBudget(1)
62 select {
63 case <-shutdown:
64 return
65 default:
66 }
67 if runtime.SovCompactions() != 0 {
68 panic("cycle finished under a one-byte budget")
69 }
70 if runtime.SovPendingCompactions() == 0 {
71 panic("no cycle left queued")
72 }
73 74 // An allocation lands in the old arena between steps. The forwarding map
75 // does not contain it, so the next drain must cancel the cycle.
76 s.Add("k", 7)
77 sum += 7
78 select {
79 case <-shutdown:
80 return
81 default:
82 }
83 if runtime.SovCancels() == 0 {
84 panic("allocation between steps did not cancel the cycle")
85 }
86 if s.Sum() != 2*sum {
87 panic("live data lost when the cycle was cancelled")
88 }
89 90 // (a) and (b): with no budget the queued group compacts to completion.
91 runtime.SovSetCopyBudget(0)
92 select {
93 case <-shutdown:
94 return
95 default:
96 }
97 if runtime.SovCompactions() == 0 {
98 panic("compaction did not complete")
99 }
100 got := s.Sum()
101 println("want:", 2*sum, "got:", got, "key:", s.KeyName("k"))
102 if got != 2*sum {
103 panic("live data lost across compaction")
104 }
105 if s.KeyName("k") != "k" {
106 panic("map value lost across compaction")
107 }
108 after := s.FirstAddr()
109 if after == 0 || after == before {
110 panic("live data was not relocated")
111 }
112 println("sov compaction ok")
113 }
114