diff options
Diffstat (limited to 'libgo/go/runtime/gcinfo_test.go')
-rw-r--r-- | libgo/go/runtime/gcinfo_test.go | 138 |
1 files changed, 66 insertions, 72 deletions
diff --git a/libgo/go/runtime/gcinfo_test.go b/libgo/go/runtime/gcinfo_test.go index 00449929c95..7e345e55a4c 100644 --- a/libgo/go/runtime/gcinfo_test.go +++ b/libgo/go/runtime/gcinfo_test.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build ignore + package runtime_test import ( @@ -10,25 +12,16 @@ import ( "testing" ) +const ( + typeScalar = 0 + typePointer = 1 +) + // 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") - verifyGCInfo(t, "bss ScalarPtr", &bssScalarPtr, nonStackInfo(infoScalarPtr)) - verifyGCInfo(t, "bss PtrScalar", &bssPtrScalar, nonStackInfo(infoPtrScalar)) - verifyGCInfo(t, "bss BigStruct", &bssBigStruct, nonStackInfo(infoBigStruct())) - verifyGCInfo(t, "bss string", &bssString, nonStackInfo(infoString)) - verifyGCInfo(t, "bss slice", &bssSlice, nonStackInfo(infoSlice)) - verifyGCInfo(t, "bss eface", &bssEface, nonStackInfo(infoEface)) - verifyGCInfo(t, "bss iface", &bssIface, nonStackInfo(infoIface)) - - verifyGCInfo(t, "data ScalarPtr", &dataScalarPtr, nonStackInfo(infoScalarPtr)) - verifyGCInfo(t, "data PtrScalar", &dataPtrScalar, nonStackInfo(infoPtrScalar)) - verifyGCInfo(t, "data BigStruct", &dataBigStruct, nonStackInfo(infoBigStruct())) - verifyGCInfo(t, "data string", &dataString, nonStackInfo(infoString)) - verifyGCInfo(t, "data slice", &dataSlice, nonStackInfo(infoSlice)) - verifyGCInfo(t, "data eface", &dataEface, nonStackInfo(infoEface)) - verifyGCInfo(t, "data iface", &dataIface, nonStackInfo(infoIface)) + verifyGCInfo(t, "stack Ptr", new(Ptr), infoPtr) verifyGCInfo(t, "stack ScalarPtr", new(ScalarPtr), infoScalarPtr) verifyGCInfo(t, "stack PtrScalar", new(PtrScalar), infoPtrScalar) verifyGCInfo(t, "stack BigStruct", new(BigStruct), infoBigStruct()) @@ -38,40 +31,43 @@ func TestGCInfo(t *testing.T) { verifyGCInfo(t, "stack iface", new(Iface), infoIface) for i := 0; i < 10; i++ { - verifyGCInfo(t, "heap ScalarPtr", escape(new(ScalarPtr)), nonStackInfo(infoScalarPtr)) - verifyGCInfo(t, "heap PtrScalar", escape(new(PtrScalar)), nonStackInfo(infoPtrScalar)) - verifyGCInfo(t, "heap BigStruct", escape(new(BigStruct)), nonStackInfo(infoBigStruct())) - verifyGCInfo(t, "heap string", escape(new(string)), nonStackInfo(infoString)) - verifyGCInfo(t, "heap eface", escape(new(interface{})), nonStackInfo(infoEface)) - verifyGCInfo(t, "heap iface", escape(new(Iface)), nonStackInfo(infoIface)) + 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)) } - } func verifyGCInfo(t *testing.T, name string, p interface{}, mask0 []byte) { - mask := /* runtime.GCMask(p) */ []byte(nil) - if len(mask) > len(mask0) { - mask0 = append(mask0, BitsDead) - mask = mask[:len(mask0)] - } + mask := runtime.GCMask(p) if bytes.Compare(mask, mask0) != 0 { t.Errorf("bad GC program for %v:\nwant %+v\ngot %+v", name, mask0, mask) return } } -func nonStackInfo(mask []byte) []byte { - // BitsDead is replaced with BitsScalar everywhere except stacks. - mask1 := make([]byte, len(mask)) - mw := false - for i, v := range mask { - if !mw && v == BitsDead { - v = BitsScalar - } - mw = !mw && v == BitsMultiWord - mask1[i] = v +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} } - return mask1 + return mask +} + +func trimDead(mask []byte) []byte { + for len(mask) > 2 && mask[len(mask)-1] == typeScalar { + mask = mask[:len(mask)-1] + } + return mask } var gcinfoSink interface{} @@ -81,19 +77,13 @@ func escape(p interface{}) interface{} { return p } -const ( - BitsDead = iota - BitsScalar - BitsPointer - BitsMultiWord -) +var infoPtr = []byte{typePointer} -const ( - BitsString = iota // unused - BitsSlice // unused - BitsIface - BitsEface -) +type Ptr struct { + *byte +} + +var infoPtr10 = []byte{typePointer, typePointer, typePointer, typePointer, typePointer, typePointer, typePointer, typePointer, typePointer, typePointer} type ScalarPtr struct { q int @@ -104,7 +94,9 @@ type ScalarPtr struct { y *int } -var infoScalarPtr = []byte{BitsScalar, BitsPointer, BitsScalar, BitsPointer, BitsScalar, BitsPointer} +var infoScalarPtr = []byte{typeScalar, typePointer, typeScalar, typePointer, typeScalar, typePointer} + +var infoScalarPtr4 = append(append(append(append([]byte(nil), infoScalarPtr...), infoScalarPtr...), infoScalarPtr...), infoScalarPtr...) type PtrScalar struct { q *int @@ -115,7 +107,7 @@ type PtrScalar struct { y int } -var infoPtrScalar = []byte{BitsPointer, BitsScalar, BitsPointer, BitsScalar, BitsPointer, BitsScalar} +var infoPtrScalar = []byte{typePointer, typeScalar, typePointer, typeScalar, typePointer, typeScalar} type BigStruct struct { q *int @@ -132,27 +124,27 @@ func infoBigStruct() []byte { switch runtime.GOARCH { case "386", "arm": return []byte{ - BitsPointer, // q *int - BitsScalar, BitsScalar, BitsScalar, BitsScalar, BitsScalar, // w byte; e [17]byte - BitsPointer, BitsDead, BitsDead, // r []byte - BitsScalar, BitsScalar, BitsScalar, BitsScalar, // t int; y uint16; u uint64 - BitsPointer, BitsDead, // i string + 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 } - case "amd64": + case "arm64", "amd64", "ppc64", "ppc64le": return []byte{ - BitsPointer, // q *int - BitsScalar, BitsScalar, BitsScalar, // w byte; e [17]byte - BitsPointer, BitsDead, BitsDead, // r []byte - BitsScalar, BitsScalar, BitsScalar, // t int; y uint16; u uint64 - BitsPointer, BitsDead, // i string + 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 } case "amd64p32": return []byte{ - BitsPointer, // q *int - BitsScalar, BitsScalar, BitsScalar, BitsScalar, BitsScalar, // w byte; e [17]byte - BitsPointer, BitsDead, BitsDead, // r []byte - BitsScalar, BitsScalar, BitsDead, BitsScalar, BitsScalar, // t int; y uint16; u uint64 - BitsPointer, BitsDead, // i string + 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 } default: panic("unknown arch") @@ -170,6 +162,7 @@ func (IfaceImpl) f() { var ( // BSS + bssPtr Ptr bssScalarPtr ScalarPtr bssPtrScalar PtrScalar bssBigStruct BigStruct @@ -179,6 +172,7 @@ var ( bssIface Iface // DATA + dataPtr = Ptr{new(byte)} dataScalarPtr = ScalarPtr{q: 1} dataPtrScalar = PtrScalar{w: 1} dataBigStruct = BigStruct{w: 1} @@ -187,8 +181,8 @@ var ( dataEface interface{} = 42 dataIface Iface = IfaceImpl(42) - infoString = []byte{BitsPointer, BitsDead} - infoSlice = []byte{BitsPointer, BitsDead, BitsDead} - infoEface = []byte{BitsMultiWord, BitsEface} - infoIface = []byte{BitsMultiWord, BitsIface} + infoString = []byte{typePointer, typeScalar} + infoSlice = []byte{typePointer, typeScalar, typeScalar} + infoEface = []byte{typePointer, typePointer} + infoIface = []byte{typePointer, typePointer} ) |