diff --git a/src/runtime/mbitmap.go b/src/runtime/mbitmap.go index 4d39c321d8256b..efdcb8fca41fa8 100644 --- a/src/runtime/mbitmap.go +++ b/src/runtime/mbitmap.go @@ -978,8 +978,16 @@ func heapBitsSetType(x, size, dataSize uintptr, typ *_type) { // nb unmodified: we just loaded 8 bits, // and the next iteration will consume 8 bits, // leaving us with the same nb the next time we're here. - b |= uintptr(*p) << nb - p = add1(p) + if nb < 8 { + b |= uintptr(*p) << nb + p = add1(p) + } else { + // Reduce the number of bits in b. + // This is important if we skipped + // over a scalar tail, since nb could + // be larger than the bit width of b. + nb -= 8 + } } else if p == nil { // Almost as fast path: track bit count and refill from pbits. // For short repetitions. diff --git a/test/fixedbugs/issue11286.go b/test/fixedbugs/issue11286.go new file mode 100644 index 00000000000000..560b7d4794251b --- /dev/null +++ b/test/fixedbugs/issue11286.go @@ -0,0 +1,34 @@ +// run + +// Copyright 2015 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. + +// Test that pointer bitmaps of types with large scalar tails are +// correctly repeated when unrolled into the heap bitmap. + +package main + +import "runtime" + +const D = 57 + +type T struct { + a [D]float64 + b map[string]int + c [D]float64 +} + +var ts []T + +func main() { + ts = make([]T, 4) + for i := range ts { + ts[i].b = make(map[string]int) + } + ts[3].b["abc"] = 42 + runtime.GC() + if ts[3].b["abc"] != 42 { + panic("bad field value") + } +}