testsuite: Skip 90020.c on AIX.
[official-gcc.git] / libgo / go / runtime / gcinfo_test.go
blobddbe5dd5feadd5bba698a8ef9e54a6c7970fef2d
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build ignore
7 package runtime_test
9 import (
10 "bytes"
11 "runtime"
12 "testing"
15 const (
16 typeScalar = 0
17 typePointer = 1
20 // TestGCInfo tests that various objects in heap, data and bss receive correct GC pointer type info.
21 func TestGCInfo(t *testing.T) {
22 t.Skip("skipping on gccgo for now")
25 var x Ptr
26 verifyGCInfo(t, "stack Ptr", &x, infoPtr)
27 runtime.KeepAlive(x)
30 var x ScalarPtr
31 verifyGCInfo(t, "stack ScalarPtr", &x, infoScalarPtr)
32 runtime.KeepAlive(x)
35 var x PtrScalar
36 verifyGCInfo(t, "stack PtrScalar", &x, infoPtrScalar)
37 runtime.KeepAlive(x)
40 var x BigStruct
41 verifyGCInfo(t, "stack BigStruct", &x, infoBigStruct())
42 runtime.KeepAlive(x)
45 var x string
46 verifyGCInfo(t, "stack string", &x, infoString)
47 runtime.KeepAlive(x)
50 var x []string
51 verifyGCInfo(t, "stack slice", &x, infoSlice)
52 runtime.KeepAlive(x)
55 var x interface{}
56 verifyGCInfo(t, "stack eface", &x, infoEface)
57 runtime.KeepAlive(x)
60 var x Iface
61 verifyGCInfo(t, "stack iface", &x, infoIface)
62 runtime.KeepAlive(x)
65 for i := 0; i < 10; i++ {
66 verifyGCInfo(t, "heap Ptr", escape(new(Ptr)), trimDead(padDead(infoPtr)))
67 verifyGCInfo(t, "heap PtrSlice", escape(&make([]*byte, 10)[0]), trimDead(infoPtr10))
68 verifyGCInfo(t, "heap ScalarPtr", escape(new(ScalarPtr)), trimDead(infoScalarPtr))
69 verifyGCInfo(t, "heap ScalarPtrSlice", escape(&make([]ScalarPtr, 4)[0]), trimDead(infoScalarPtr4))
70 verifyGCInfo(t, "heap PtrScalar", escape(new(PtrScalar)), trimDead(infoPtrScalar))
71 verifyGCInfo(t, "heap BigStruct", escape(new(BigStruct)), trimDead(infoBigStruct()))
72 verifyGCInfo(t, "heap string", escape(new(string)), trimDead(infoString))
73 verifyGCInfo(t, "heap eface", escape(new(interface{})), trimDead(infoEface))
74 verifyGCInfo(t, "heap iface", escape(new(Iface)), trimDead(infoIface))
78 func verifyGCInfo(t *testing.T, name string, p interface{}, mask0 []byte) {
79 mask := runtime.GCMask(p)
80 if !bytes.Equal(mask, mask0) {
81 t.Errorf("bad GC program for %v:\nwant %+v\ngot %+v", name, mask0, mask)
82 return
86 func padDead(mask []byte) []byte {
87 // Because the dead bit isn't encoded in the second word,
88 // and because on 32-bit systems a one-word allocation
89 // uses a two-word block, the pointer info for a one-word
90 // object needs to be expanded to include an extra scalar
91 // on 32-bit systems to match the heap bitmap.
92 if runtime.PtrSize == 4 && len(mask) == 1 {
93 return []byte{mask[0], 0}
95 return mask
98 func trimDead(mask []byte) []byte {
99 for len(mask) > 2 && mask[len(mask)-1] == typeScalar {
100 mask = mask[:len(mask)-1]
102 if len(mask) == 2 && mask[0] == typeScalar && mask[1] == typeScalar {
103 mask = mask[:0]
105 return mask
108 var gcinfoSink interface{}
110 func escape(p interface{}) interface{} {
111 gcinfoSink = p
112 return p
115 var infoPtr = []byte{typePointer}
117 type Ptr struct {
118 *byte
121 var infoPtr10 = []byte{typePointer, typePointer, typePointer, typePointer, typePointer, typePointer, typePointer, typePointer, typePointer, typePointer}
123 type ScalarPtr struct {
124 q int
125 w *int
126 e int
127 r *int
128 t int
129 y *int
132 var infoScalarPtr = []byte{typeScalar, typePointer, typeScalar, typePointer, typeScalar, typePointer}
134 var infoScalarPtr4 = append(append(append(append([]byte(nil), infoScalarPtr...), infoScalarPtr...), infoScalarPtr...), infoScalarPtr...)
136 type PtrScalar struct {
137 q *int
138 w int
139 e *int
140 r int
141 t *int
142 y int
145 var infoPtrScalar = []byte{typePointer, typeScalar, typePointer, typeScalar, typePointer, typeScalar}
147 type BigStruct struct {
148 q *int
149 w byte
150 e [17]byte
151 r []byte
152 t int
153 y uint16
154 u uint64
155 i string
158 func infoBigStruct() []byte {
159 switch runtime.GOARCH {
160 case "386", "arm", "mips", "mipsle":
161 return []byte{
162 typePointer, // q *int
163 typeScalar, typeScalar, typeScalar, typeScalar, typeScalar, // w byte; e [17]byte
164 typePointer, typeScalar, typeScalar, // r []byte
165 typeScalar, typeScalar, typeScalar, typeScalar, // t int; y uint16; u uint64
166 typePointer, typeScalar, // i string
168 case "arm64", "amd64", "mips64", "mips64le", "ppc64", "ppc64le", "riscv64", "s390x", "wasm":
169 return []byte{
170 typePointer, // q *int
171 typeScalar, typeScalar, typeScalar, // w byte; e [17]byte
172 typePointer, typeScalar, typeScalar, // r []byte
173 typeScalar, typeScalar, typeScalar, // t int; y uint16; u uint64
174 typePointer, typeScalar, // i string
176 default:
177 panic("unknown arch")
181 type Iface interface {
185 type IfaceImpl int
187 func (IfaceImpl) f() {
190 var (
191 // BSS
192 bssPtr Ptr
193 bssScalarPtr ScalarPtr
194 bssPtrScalar PtrScalar
195 bssBigStruct BigStruct
196 bssString string
197 bssSlice []string
198 bssEface interface{}
199 bssIface Iface
201 // DATA
202 dataPtr = Ptr{new(byte)}
203 dataScalarPtr = ScalarPtr{q: 1}
204 dataPtrScalar = PtrScalar{w: 1}
205 dataBigStruct = BigStruct{w: 1}
206 dataString = "foo"
207 dataSlice = []string{"foo"}
208 dataEface interface{} = 42
209 dataIface Iface = IfaceImpl(42)
211 infoString = []byte{typePointer, typeScalar}
212 infoSlice = []byte{typePointer, typeScalar, typeScalar}
213 infoEface = []byte{typeScalar, typePointer}
214 infoIface = []byte{typeScalar, typePointer}