Retro68/gcc/libgo/go/runtime/gcinfo_test.go

189 lines
5.3 KiB
Go
Raw Normal View History

2015-08-28 15:33:40 +00:00
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2017-04-10 11:32:00 +00:00
// +build ignore
2015-08-28 15:33:40 +00:00
package runtime_test
import (
"bytes"
"runtime"
"testing"
)
2017-04-10 11:32:00 +00:00
const (
typeScalar = 0
typePointer = 1
)
2015-08-28 15:33:40 +00:00
// TestGCInfo tests that various objects in heap, data and bss receive correct GC pointer type info.
func TestGCInfo(t *testing.T) {
t.Skip("skipping on gccgo for now")
2017-04-10 11:32:00 +00:00
verifyGCInfo(t, "stack Ptr", new(Ptr), infoPtr)
2015-08-28 15:33:40 +00:00
verifyGCInfo(t, "stack ScalarPtr", new(ScalarPtr), infoScalarPtr)
verifyGCInfo(t, "stack PtrScalar", new(PtrScalar), infoPtrScalar)
verifyGCInfo(t, "stack BigStruct", new(BigStruct), infoBigStruct())
verifyGCInfo(t, "stack string", new(string), infoString)
verifyGCInfo(t, "stack slice", new([]string), infoSlice)
verifyGCInfo(t, "stack eface", new(interface{}), infoEface)
verifyGCInfo(t, "stack iface", new(Iface), infoIface)
for i := 0; i < 10; i++ {
2017-04-10 11:32:00 +00:00
verifyGCInfo(t, "heap Ptr", escape(new(Ptr)), trimDead(padDead(infoPtr)))
verifyGCInfo(t, "heap PtrSlice", escape(&make([]*byte, 10)[0]), trimDead(infoPtr10))
verifyGCInfo(t, "heap ScalarPtr", escape(new(ScalarPtr)), trimDead(infoScalarPtr))
verifyGCInfo(t, "heap ScalarPtrSlice", escape(&make([]ScalarPtr, 4)[0]), trimDead(infoScalarPtr4))
verifyGCInfo(t, "heap PtrScalar", escape(new(PtrScalar)), trimDead(infoPtrScalar))
verifyGCInfo(t, "heap BigStruct", escape(new(BigStruct)), trimDead(infoBigStruct()))
verifyGCInfo(t, "heap string", escape(new(string)), trimDead(infoString))
verifyGCInfo(t, "heap eface", escape(new(interface{})), trimDead(infoEface))
verifyGCInfo(t, "heap iface", escape(new(Iface)), trimDead(infoIface))
2015-08-28 15:33:40 +00:00
}
}
func verifyGCInfo(t *testing.T, name string, p interface{}, mask0 []byte) {
2017-04-10 11:32:00 +00:00
mask := runtime.GCMask(p)
2015-08-28 15:33:40 +00:00
if bytes.Compare(mask, mask0) != 0 {
t.Errorf("bad GC program for %v:\nwant %+v\ngot %+v", name, mask0, mask)
return
}
}
2017-04-10 11:32:00 +00:00
func padDead(mask []byte) []byte {
// Because the dead bit isn't encoded until the third word,
// and because on 32-bit systems a one-word allocation
// uses a two-word block, the pointer info for a one-word
// object needs to be expanded to include an extra scalar
// on 32-bit systems to match the heap bitmap.
if runtime.PtrSize == 4 && len(mask) == 1 {
return []byte{mask[0], 0}
2015-08-28 15:33:40 +00:00
}
2017-04-10 11:32:00 +00:00
return mask
}
func trimDead(mask []byte) []byte {
for len(mask) > 2 && mask[len(mask)-1] == typeScalar {
mask = mask[:len(mask)-1]
}
return mask
2015-08-28 15:33:40 +00:00
}
var gcinfoSink interface{}
func escape(p interface{}) interface{} {
gcinfoSink = p
return p
}
2017-04-10 11:32:00 +00:00
var infoPtr = []byte{typePointer}
2015-08-28 15:33:40 +00:00
2017-04-10 11:32:00 +00:00
type Ptr struct {
*byte
}
var infoPtr10 = []byte{typePointer, typePointer, typePointer, typePointer, typePointer, typePointer, typePointer, typePointer, typePointer, typePointer}
2015-08-28 15:33:40 +00:00
type ScalarPtr struct {
q int
w *int
e int
r *int
t int
y *int
}
2017-04-10 11:32:00 +00:00
var infoScalarPtr = []byte{typeScalar, typePointer, typeScalar, typePointer, typeScalar, typePointer}
var infoScalarPtr4 = append(append(append(append([]byte(nil), infoScalarPtr...), infoScalarPtr...), infoScalarPtr...), infoScalarPtr...)
2015-08-28 15:33:40 +00:00
type PtrScalar struct {
q *int
w int
e *int
r int
t *int
y int
}
2017-04-10 11:32:00 +00:00
var infoPtrScalar = []byte{typePointer, typeScalar, typePointer, typeScalar, typePointer, typeScalar}
2015-08-28 15:33:40 +00:00
type BigStruct struct {
q *int
w byte
e [17]byte
r []byte
t int
y uint16
u uint64
i string
}
func infoBigStruct() []byte {
switch runtime.GOARCH {
case "386", "arm":
return []byte{
2017-04-10 11:32:00 +00:00
typePointer, // q *int
typeScalar, typeScalar, typeScalar, typeScalar, typeScalar, // w byte; e [17]byte
typePointer, typeScalar, typeScalar, // r []byte
typeScalar, typeScalar, typeScalar, typeScalar, // t int; y uint16; u uint64
typePointer, typeScalar, // i string
2015-08-28 15:33:40 +00:00
}
2017-04-10 11:32:00 +00:00
case "arm64", "amd64", "mips64", "mips64le", "ppc64", "ppc64le":
2015-08-28 15:33:40 +00:00
return []byte{
2017-04-10 11:32:00 +00:00
typePointer, // q *int
typeScalar, typeScalar, typeScalar, // w byte; e [17]byte
typePointer, typeScalar, typeScalar, // r []byte
typeScalar, typeScalar, typeScalar, // t int; y uint16; u uint64
typePointer, typeScalar, // i string
2015-08-28 15:33:40 +00:00
}
case "amd64p32":
return []byte{
2017-04-10 11:32:00 +00:00
typePointer, // q *int
typeScalar, typeScalar, typeScalar, typeScalar, typeScalar, // w byte; e [17]byte
typePointer, typeScalar, typeScalar, // r []byte
typeScalar, typeScalar, typeScalar, typeScalar, typeScalar, // t int; y uint16; u uint64
typePointer, typeScalar, // i string
2015-08-28 15:33:40 +00:00
}
default:
panic("unknown arch")
}
}
type Iface interface {
f()
}
type IfaceImpl int
func (IfaceImpl) f() {
}
var (
// BSS
2017-04-10 11:32:00 +00:00
bssPtr Ptr
2015-08-28 15:33:40 +00:00
bssScalarPtr ScalarPtr
bssPtrScalar PtrScalar
bssBigStruct BigStruct
bssString string
bssSlice []string
bssEface interface{}
bssIface Iface
// DATA
2017-04-10 11:32:00 +00:00
dataPtr = Ptr{new(byte)}
2015-08-28 15:33:40 +00:00
dataScalarPtr = ScalarPtr{q: 1}
dataPtrScalar = PtrScalar{w: 1}
dataBigStruct = BigStruct{w: 1}
dataString = "foo"
dataSlice = []string{"foo"}
dataEface interface{} = 42
dataIface Iface = IfaceImpl(42)
2017-04-10 11:32:00 +00:00
infoString = []byte{typePointer, typeScalar}
infoSlice = []byte{typePointer, typeScalar, typeScalar}
infoEface = []byte{typePointer, typePointer}
infoIface = []byte{typePointer, typePointer}
2015-08-28 15:33:40 +00:00
)