1 // An interface value whose concrete type fits in a pointer is stored INLINE (
2 // packed into the value slot), which is the representation the shared runtime
3 // codec and relocator assume: RelocMarkIface only forwards a boxed value when
4 // its type is larger than a pointer word, and codecEncodeValue reads the slot
5 // as the value itself for the same reason.
6 //
7 // stage4 boxed small aggregates by pointer anyway, so a four-byte struct
8 // returned from another package pointed into the returning frame's arena,
9 // which was never forwarded: the receiver arrived as garbage and the call
10 // faulted. legacy's emitPointerPack has always packed by value-size, so this
11 // was a stage4-only divergence - and an unsound one, because the two
12 // representations are indistinguishable at the slot.
13 //
14 // Fixed by deciding the representation from the type's store size, exactly as
15 // legacy and the runtime do, and by unpacking on every consumer side
16 // (dispatch receivers, boxing method arguments, type assertions).
17 package main
18 19 import (
20 "os"
21 "tests/regressions/should_compile/iface_inline_box/lib"
22 )
23 24 func check(name string, got, want uint8) {
25 if got != want {
26 println("FAIL: " | name | ": got ", got, " want ", want)
27 os.Exit(1)
28 }
29 }
30 31 func main() {
32 // Termination: a batch program must still be stoppable, so the entry
33 // handles the channel the runtime closes on SIGINT/SIGTERM.
34 select {
35 case <-shutdown:
36 return
37 default:
38 }
39 40 cs := lib.Box()
41 r, g, b, a := cs[0].RGBA()
42 check("dispatch in caller", r, 1)
43 check("dispatch in caller 2", g, 2)
44 check("dispatch in caller 3", b, 3)
45 check("dispatch in caller 4", a, 4)
46 check("dispatch in defining package", lib.Sum(cs), 6)
47 48 // The value form of a type assertion unpacks the same inline slot, and a
49 // failed assertion must yield the zero value without reading memory.
50 c, ok := cs[1].(lib.Colour)
51 if !ok {
52 println("FAIL: comma-ok assertion")
53 os.Exit(1)
54 }
55 check("asserted value", c.R, 5)
56 check("asserted value 2", c.A, 8)
57 c2 := cs[0].(lib.Colour)
58 check("single assertion", c2.G, 2)
59 // A pointer assertion against a value-boxed interface must fail and
60 // leave the target zero, not read the packed slot as an address.
61 if p, bad := cs[0].(*lib.Colour); bad || p != nil {
62 println("FAIL: pointer assertion against a packed value succeeded")
63 os.Exit(1)
64 }
65 println("ok")
66 }
67