1 // Return ownership: what a value carries out of a call.
2 //
3 // A parameter is caller-owned, so a result built from it must still POINT AT
4 // the caller's data after the call (identity, not a copy), while anything the
5 // callee allocated itself must survive as a copy in the caller's arena. Every
6 // callee here is in lib/, because package main has no frame arena and no
7 // return codec - the property is only observable across a package boundary.
8 package main
9 10 import (
11 "os"
12 "tests/regressions/should_compile/return_ownership/lib"
13 )
14 15 func bad(msg string) {
16 println("FAIL:", msg)
17 os.Exit(1)
18 }
19 20 func main() {
21 // Termination: a batch program must still be stoppable, so the entry
22 // handles the channel the runtime closes on SIGINT/SIGTERM.
23 select {
24 case <-shutdown:
25 return
26 default:
27 }
28 29 own := []byte{:4}
30 own[0] = 1
31 got := lib.FromParam(own)
32 if int32(len(got.B)) != 4 {
33 bad("parameter return length wrong")
34 }
35 // Identity: a write through the returned slice is visible in the caller's
36 // own array only if the callee aliased it instead of copying it.
37 got.B[1] = 9
38 if own[1] != 9 {
39 bad("return from parameter was copied instead of aliased")
40 }
41 42 loc := lib.FromLocal(3)
43 if loc.N != 3 {
44 bad("local return count wrong")
45 }
46 if int32(len(loc.B)) != 3 {
47 bad("local return length wrong")
48 }
49 if loc.B[0] != 'a' || loc.B[2] != 'c' {
50 bad("local return contents wrong")
51 }
52 // Still writable: the storage must be caller-owned, not the dead frame.
53 loc.B[1] = 'Z'
54 if loc.B[1] != 'Z' {
55 bad("local return storage not writable")
56 }
57 58 r := lib.Ring()
59 if r == nil || r.Next == nil || r.Next.Next == nil {
60 bad("ring lost its links")
61 }
62 if string(r.Next.Name) != "b" {
63 bad("ring node name lost")
64 }
65 // a -> b -> c -> a: the fourth hop must be the first node again. A
66 // relocation that duplicated the head would unroll the cycle here.
67 if r.Next.Next.Next != r {
68 bad("ring cycle broke at the return")
69 }
70 71 ext := []byte("caller")
72 g := lib.Build(ext)
73 if string(g.Head.Next.Name) != "b" {
74 bad("graph node name lost")
75 }
76 if g.Head.Next.Next.Next != g.Head {
77 bad("graph cycle broke through the struct field")
78 }
79 if int32(len(g.Items)) != 2 || string(g.Items[1].Name) != "item" {
80 bad("returned slice of structs lost its contents")
81 }
82 if string(g.Items[1].Tags[2]) != "z" {
83 bad("returned struct field lost its nested slice")
84 }
85 if g.ByKey["k"] != 7 {
86 bad("returned map lost its contents")
87 }
88 // A parameter's bytes are the caller's; a write through the returned field
89 // must reach them.
90 g.Ext[0] = 'C'
91 if ext[0] != 'C' {
92 bad("graph's parameter field was copied instead of aliased")
93 }
94 if string(g.Own) != "callee" {
95 bad("callee-built bytes lost")
96 }
97 g.Own[0] = 'K'
98 if string(g.Own) != "Kallee" {
99 bad("callee-built bytes are not writable")
100 }
101 102 big := lib.Big(1000)
103 if int32(len(big)) != 1000 {
104 bad("big return length wrong")
105 }
106 if big[0] != 0 || big[127] != 127 || big[128] != 0 || big[999] != byte(999&0x7f) {
107 bad("big return contents wrong")
108 }
109 big[500] = 0x5a
110 if big[500] != 0x5a {
111 bad("big return storage not writable")
112 }
113 114 println("return ownership ok")
115 }
116