1 // A []byte built at RUN TIME must cross a spawn channel byte-exact.
2 //
3 // The boundary copies raw element bytes into a shared ring, so a slice element
4 // used to hand the child an ADDRESS in the sender's arena space. The child is a
5 // fork, so it read whatever its copy-on-write view held there: measured as the
6 // right length and the bytes [4, 0, 0, ...] - the push allocator's first-batch
7 // marker. This fixture is the boundary's regression test: lengths 0, 1, a frame
8 // that fills exactly one 8-byte ring slot, one byte past it, and a body spanning
9 // many slots, each byte chosen so a shifted or truncated read changes the sum.
10 //
11 // The element is a struct with a cap tag because a spawn channel's ring is
12 // sized from the element type before the fork: an untagged body would be one
13 // 4096-byte page, and the 100000-byte case would be over its own declared
14 // budget (a loud panic, not a silent wrap). The tag states 128 KiB, so the ring
15 // carries every case here.
16 //
17 // A scalar control rides the same spawn on a chan int32: if that ever fails, the
18 // boundary itself is broken and this fixture is not measuring the slice path.
19 //
20 // usage: spawnbytes exit 0 when every case is byte-exact
21 package main
22 23 import (
24 "fmt"
25 "os"
26 )
27 28 // Body is the channel element: the cap tag is the frame budget the ring is
29 // built from.
30 type Body struct {
31 Data []byte `cap:"131072"`
32 }
33 34 // sum is an order sensitive checksum: a shifted read changes it.
35 func sum(b []byte) (s int32) {
36 for i := int32(0); i < int32(len(b)); i++ {
37 s = s*31 + int32(b[i]) + i
38 }
39 return s
40 }
41 42 // make builds a body of n bytes at run time, through push - the path that puts
43 // the payload in the arena rather than in a literal in the image.
44 func make(n int32) (b []byte) {
45 b = []byte{}
46 for i := int32(0); i < n; i++ {
47 b = push(b, byte((i*7+3)&0xff))
48 }
49 return b
50 }
51 52 // child echoes the length and checksum of every body it receives, then the
53 // scalar control's value, then exits.
54 func child(bodies chan Body, scalars chan int32, back chan int32) (closed chan struct{}) {
55 select {
56 case <-closed:
57 return closed
58 default:
59 }
60 n := <-scalars
61 s := <-scalars
62 back <- s
63 for i := int32(0); i < n; i++ {
64 select {
65 case <-closed:
66 return closed
67 default:
68 }
69 b := <-bodies
70 back <- int32(len(b.Data))
71 back <- sum(b.Data)
72 }
73 return closed
74 }
75 76 func main() {
77 // Termination: a batch program must still be stoppable, so the entry
78 // handles the channel the runtime closes on SIGINT/SIGTERM.
79 select {
80 case <-shutdown:
81 return
82 default:
83 }
84 85 var fails int32
86 bodies := chan Body{}
87 scalars := chan int32{}
88 back := chan int32{}
89 spawn(child, bodies, scalars, back)
90 91 // The shapes, smallest first. 4 payload bytes frame to exactly one 8-byte
92 // aligned ring slot (a 4-byte length prefix plus the payload); 5 is one
93 // byte past that slot.
94 sizes := []int32{0, 1, 4, 5, 100000}
95 labels := []string{"empty", "one", "full-slot", "over-slot", "many-slots"}
96 nCases := int32(len(sizes))
97 98 scalars <- nCases
99 scalars <- int32(4242)
100 gotScalar := <-back
101 if gotScalar != 4242 {
102 fmt.Printf("FAIL scalar got %d want 4242\n", gotScalar)
103 fails = fails + 1
104 } else {
105 fmt.Printf("ok scalar %d\n", gotScalar)
106 }
107 108 for c := int32(0); c < nCases; c++ {
109 body := make(sizes[c])
110 wantLen := int32(len(body))
111 wantSum := sum(body)
112 bodies <- Body{Data: body}
113 gotLen := <-back
114 gotSum := <-back
115 if gotLen != wantLen || gotSum != wantSum {
116 fmt.Printf("FAIL %-12s len=%d want %d, sum=%d want %d\n", labels[c], gotLen, wantLen, gotSum, wantSum)
117 fails = fails + 1
118 } else {
119 fmt.Printf("ok %-12s len=%d sum=%d\n", labels[c], gotLen, gotSum)
120 }
121 }
122 123 if fails != 0 {
124 fmt.Printf("spawnbytes: %d case(s) not byte-exact\n", fails)
125 os.Exit(1)
126 }
127 fmt.Printf("spawnbytes ok\n")
128 }
129