// Copyright 2009 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. // Garbage collector: type and heap bitmaps. // // Stack, data, and bss bitmaps // // Stack frames and global variables in the data and bss sections are described // by 1-bit bitmaps in which 0 means uninteresting and 1 means live pointer // to be visited during GC. The bits in each byte are consumed starting with // the low bit: 1<<0, 1<<1, and so on. // // Heap bitmap // // The allocated heap comes from a subset of the memory in the range [start, used), // where start == mheap_.arena_start and used == mheap_.arena_used. // The heap bitmap comprises 2 bits for each pointer-sized word in that range, // stored in bytes indexed backward in memory from start. // That is, the byte at address start-1 holds the 2-bit entries for the four words // start through start+3*ptrSize, the byte at start-2 holds the entries for // start+4*ptrSize through start+7*ptrSize, and so on. // // In each 2-bit entry, the lower bit holds the same information as in the 1-bit // bitmaps: 0 means uninteresting and 1 means live pointer to be visited during GC. // The meaning of the high bit depends on the position of the word being described // in its allocated object. In all words *except* the second word, the // high bit indicates that the object is still being described. In // these words, if a bit pair with a high bit 0 is encountered, the // low bit can also be assumed to be 0, and the object description is // over. This 00 is called the ``dead'' encoding: it signals that the // rest of the words in the object are uninteresting to the garbage // collector. // // In the second word, the high bit is the GC ``checkmarked'' bit (see below). // // The 2-bit entries are split when written into the byte, so that the top half // of the byte contains 4 high bits and the bottom half contains 4 low (pointer) // bits. // This form allows a copy from the 1-bit to the 4-bit form to keep the // pointer bits contiguous, instead of having to space them out. // // The code makes use of the fact that the zero value for a heap bitmap // has no live pointer bit set and is (depending on position), not used, // not checkmarked, and is the dead encoding. // These properties must be preserved when modifying the encoding. // // The bitmap for noscan spans is not maintained. Code must ensure // that an object is scannable before consulting its bitmap by // checking either the noscan bit in the span or by consulting its // type's information. // // Checkmarks // // In a concurrent garbage collector, one worries about failing to mark // a live object due to mutations without write barriers or bugs in the // collector implementation. As a sanity check, the GC has a 'checkmark' // mode that retraverses the object graph with the world stopped, to make // sure that everything that should be marked is marked. // In checkmark mode, in the heap bitmap, the high bit of the 2-bit entry // for the second word of the object holds the checkmark bit. // When not in checkmark mode, this bit is set to 1. // // The smallest possible allocation is 8 bytes. On a 32-bit machine, that // means every allocated object has two words, so there is room for the // checkmark bit. On a 64-bit machine, however, the 8-byte allocation is // just one word, so the second bit pair is not available for encoding the // checkmark. However, because non-pointer allocations are combined // into larger 16-byte (maxTinySize) allocations, a plain 8-byte allocation // must be a pointer, so the type bit in the first word is not actually needed. // It is still used in general, except in checkmark the type bit is repurposed // as the checkmark bit and then reinitialized (to 1) as the type bit when // finished. // package runtime import ( "runtime/internal/atomic" "runtime/internal/sys" "unsafe" ) const ( bitPointer = 1 << 0 bitScan = 1 << 4 heapBitsShift = 1 // shift offset between successive bitPointer or bitScan entries heapBitmapScale = sys.PtrSize * (8 / 2) // number of data bytes described by one heap bitmap byte // all scan/pointer bits in a byte bitScanAll = bitScan | bitScan<= n { return } sysMap(unsafe.Pointer(h.bitmap-n), n-h.bitmap_mapped, h.arena_reserved, &memstats.gc_sys) h.bitmap_mapped = n } // heapBits provides access to the bitmap bits for a single heap word. // The methods on heapBits take value receivers so that the compiler // can more easily inline calls to those methods and registerize the // struct fields independently. type heapBits struct { bitp *uint8 shift uint32 } // markBits provides access to the mark bit for an object in the heap. // bytep points to the byte holding the mark bit. // mask is a byte with a single bit set that can be &ed with *bytep // to see if the bit has been set. // *m.byte&m.mask != 0 indicates the mark bit is set. // index can be used along with span information to generate // the address of the object in the heap. // We maintain one set of mark bits for allocation and one for // marking purposes. type markBits struct { bytep *uint8 mask uint8 index uintptr } //go:nosplit func (s *mspan) allocBitsForIndex(allocBitIndex uintptr) markBits { bytep, mask := s.allocBits.bitp(allocBitIndex) return markBits{bytep, mask, allocBitIndex} } // refillaCache takes 8 bytes s.allocBits starting at whichByte // and negates them so that ctz (count trailing zeros) instructions // can be used. It then places these 8 bytes into the cached 64 bit // s.allocCache. func (s *mspan) refillAllocCache(whichByte uintptr) { bytes := (*[8]uint8)(unsafe.Pointer(s.allocBits.bytep(whichByte))) aCache := uint64(0) aCache |= uint64(bytes[0]) aCache |= uint64(bytes[1]) << (1 * 8) aCache |= uint64(bytes[2]) << (2 * 8) aCache |= uint64(bytes[3]) << (3 * 8) aCache |= uint64(bytes[4]) << (4 * 8) aCache |= uint64(bytes[5]) << (5 * 8) aCache |= uint64(bytes[6]) << (6 * 8) aCache |= uint64(bytes[7]) << (7 * 8) s.allocCache = ^aCache } // nextFreeIndex returns the index of the next free object in s at // or after s.freeindex. // There are hardware instructions that can be used to make this // faster if profiling warrants it. func (s *mspan) nextFreeIndex() uintptr { sfreeindex := s.freeindex snelems := s.nelems if sfreeindex == snelems { return sfreeindex } if sfreeindex > snelems { throw("s.freeindex > s.nelems") } aCache := s.allocCache bitIndex := sys.Ctz64(aCache) for bitIndex == 64 { // Move index to start of next cached bits. sfreeindex = (sfreeindex + 64) &^ (64 - 1) if sfreeindex >= snelems { s.freeindex = snelems return snelems } whichByte := sfreeindex / 8 // Refill s.allocCache with the next 64 alloc bits. s.refillAllocCache(whichByte) aCache = s.allocCache bitIndex = sys.Ctz64(aCache) // nothing available in cached bits // grab the next 8 bytes and try again. } result := sfreeindex + uintptr(bitIndex) if result >= snelems { s.freeindex = snelems return snelems } s.allocCache >>= uint(bitIndex + 1) sfreeindex = result + 1 if sfreeindex%64 == 0 && sfreeindex != snelems { // We just incremented s.freeindex so it isn't 0. // As each 1 in s.allocCache was encountered and used for allocation // it was shifted away. At this point s.allocCache contains all 0s. // Refill s.allocCache so that it corresponds // to the bits at s.allocBits starting at s.freeindex. whichByte := sfreeindex / 8 s.refillAllocCache(whichByte) } s.freeindex = sfreeindex return result } // isFree returns whether the index'th object in s is unallocated. func (s *mspan) isFree(index uintptr) bool { if index < s.freeindex { return false } bytep, mask := s.allocBits.bitp(index) return *bytep&mask == 0 } func (s *mspan) objIndex(p uintptr) uintptr { byteOffset := p - s.base() if byteOffset == 0 { return 0 } if s.baseMask != 0 { // s.baseMask is 0, elemsize is a power of two, so shift by s.divShift return byteOffset >> s.divShift } return uintptr(((uint64(byteOffset) >> s.divShift) * uint64(s.divMul)) >> s.divShift2) } func markBitsForAddr(p uintptr) markBits { s := spanOf(p) objIndex := s.objIndex(p) return s.markBitsForIndex(objIndex) } func (s *mspan) markBitsForIndex(objIndex uintptr) markBits { bytep, mask := s.gcmarkBits.bitp(objIndex) return markBits{bytep, mask, objIndex} } func (s *mspan) markBitsForBase() markBits { return markBits{(*uint8)(s.gcmarkBits), uint8(1), 0} } // isMarked reports whether mark bit m is set. func (m markBits) isMarked() bool { return *m.bytep&m.mask != 0 } // setMarked sets the marked bit in the markbits, atomically. Some compilers // are not able to inline atomic.Or8 function so if it appears as a hot spot consider // inlining it manually. func (m markBits) setMarked() { // Might be racing with other updates, so use atomic update always. // We used to be clever here and use a non-atomic update in certain // cases, but it's not worth the risk. atomic.Or8(m.bytep, m.mask) } // setMarkedNonAtomic sets the marked bit in the markbits, non-atomically. func (m markBits) setMarkedNonAtomic() { *m.bytep |= m.mask } // clearMarked clears the marked bit in the markbits, atomically. func (m markBits) clearMarked() { // Might be racing with other updates, so use atomic update always. // We used to be clever here and use a non-atomic update in certain // cases, but it's not worth the risk. atomic.And8(m.bytep, ^m.mask) } // markBitsForSpan returns the markBits for the span base address base. func markBitsForSpan(base uintptr) (mbits markBits) { if base < mheap_.arena_start || base >= mheap_.arena_used { throw("markBitsForSpan: base out of range") } mbits = markBitsForAddr(base) if mbits.mask != 1 { throw("markBitsForSpan: unaligned start") } return mbits } // advance advances the markBits to the next object in the span. func (m *markBits) advance() { if m.mask == 1<<7 { m.bytep = (*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(m.bytep)) + 1)) m.mask = 1 } else { m.mask = m.mask << 1 } m.index++ } // heapBitsForAddr returns the heapBits for the address addr. // The caller must have already checked that addr is in the range [mheap_.arena_start, mheap_.arena_used). // // nosplit because it is used during write barriers and must not be preempted. //go:nosplit func heapBitsForAddr(addr uintptr) heapBits { // 2 bits per work, 4 pairs per byte, and a mask is hard coded. off := (addr - mheap_.arena_start) / sys.PtrSize return heapBits{(*uint8)(unsafe.Pointer(mheap_.bitmap - off/4 - 1)), uint32(off & 3)} } // heapBitsForSpan returns the heapBits for the span base address base. func heapBitsForSpan(base uintptr) (hbits heapBits) { if base < mheap_.arena_start || base >= mheap_.arena_used { print("runtime: base ", hex(base), " not in range [", hex(mheap_.arena_start), ",", hex(mheap_.arena_used), ")\n") throw("heapBitsForSpan: base out of range") } return heapBitsForAddr(base) } // heapBitsForObject returns the base address for the heap object // containing the address p, the heapBits for base, // the object's span, and of the index of the object in s. // If p does not point into a heap object, // return base == 0 // otherwise return the base of the object. // // For gccgo, the forStack parameter is true if the value came from the stack. // The stack is collected conservatively and may contain invalid pointers. // // refBase and refOff optionally give the base address of the object // in which the pointer p was found and the byte offset at which it // was found. These are used for error reporting. func heapBitsForObject(p, refBase, refOff uintptr, forStack bool) (base uintptr, hbits heapBits, s *mspan, objIndex uintptr) { arenaStart := mheap_.arena_start if p < arenaStart || p >= mheap_.arena_used { return } off := p - arenaStart idx := off >> _PageShift // p points into the heap, but possibly to the middle of an object. // Consult the span table to find the block beginning. s = mheap_.spans[idx] if s == nil || p < s.base() || p >= s.limit || s.state != mSpanInUse { if s == nil || s.state == _MSpanManual || forStack { // If s is nil, the virtual address has never been part of the heap. // This pointer may be to some mmap'd region, so we allow it. // Pointers into stacks are also ok, the runtime manages these explicitly. return } // The following ensures that we are rigorous about what data // structures hold valid pointers. if debug.invalidptr != 0 { // Typically this indicates an incorrect use // of unsafe or cgo to store a bad pointer in // the Go heap. It may also indicate a runtime // bug. // // TODO(austin): We could be more aggressive // and detect pointers to unallocated objects // in allocated spans. printlock() print("runtime: pointer ", hex(p)) if s.state != mSpanInUse { print(" to unallocated span") } else { print(" to unused region of span") } print(" idx=", hex(idx), " span.base()=", hex(s.base()), " span.limit=", hex(s.limit), " span.state=", s.state, "\n") if refBase != 0 { print("runtime: found in object at *(", hex(refBase), "+", hex(refOff), ")\n") gcDumpObject("object", refBase, refOff) } getg().m.traceback = 2 throw("found bad pointer in Go heap (incorrect use of unsafe or cgo?)") } return } if forStack { // A span can be entered in mheap_.spans, and be set // to mSpanInUse, before it is fully initialized. // All we need in practice is allocBits and gcmarkBits, // so make sure they are set. if s.allocBits == nil || s.gcmarkBits == nil { return } } // If this span holds object of a power of 2 size, just mask off the bits to // the interior of the object. Otherwise use the size to get the base. if s.baseMask != 0 { // optimize for power of 2 sized objects. base = s.base() base = base + (p-base)&uintptr(s.baseMask) objIndex = (base - s.base()) >> s.divShift // base = p & s.baseMask is faster for small spans, // but doesn't work for large spans. // Overall, it's faster to use the more general computation above. } else { base = s.base() if p-base >= s.elemsize { // n := (p - base) / s.elemsize, using division by multiplication objIndex = uintptr(p-base) >> s.divShift * uintptr(s.divMul) >> s.divShift2 base += objIndex * s.elemsize } } // Now that we know the actual base, compute heapBits to return to caller. hbits = heapBitsForAddr(base) return } // next returns the heapBits describing the next pointer-sized word in memory. // That is, if h describes address p, h.next() describes p+ptrSize. // Note that next does not modify h. The caller must record the result. // // nosplit because it is used during write barriers and must not be preempted. //go:nosplit func (h heapBits) next() heapBits { if h.shift < 3*heapBitsShift { return heapBits{h.bitp, h.shift + heapBitsShift} } return heapBits{subtract1(h.bitp), 0} } // forward returns the heapBits describing n pointer-sized words ahead of h in memory. // That is, if h describes address p, h.forward(n) describes p+n*ptrSize. // h.forward(1) is equivalent to h.next(), just slower. // Note that forward does not modify h. The caller must record the result. // bits returns the heap bits for the current word. func (h heapBits) forward(n uintptr) heapBits { n += uintptr(h.shift) / heapBitsShift return heapBits{subtractb(h.bitp, n/4), uint32(n%4) * heapBitsShift} } // The caller can test morePointers and isPointer by &-ing with bitScan and bitPointer. // The result includes in its higher bits the bits for subsequent words // described by the same bitmap byte. // // nosplit because it is used during write barriers and must not be preempted. //go:nosplit func (h heapBits) bits() uint32 { // The (shift & 31) eliminates a test and conditional branch // from the generated code. return uint32(*h.bitp) >> (h.shift & 31) } // morePointers returns true if this word and all remaining words in this object // are scalars. // h must not describe the second word of the object. func (h heapBits) morePointers() bool { return h.bits()&bitScan != 0 } // isPointer reports whether the heap bits describe a pointer word. // // nosplit because it is used during write barriers and must not be preempted. //go:nosplit func (h heapBits) isPointer() bool { return h.bits()&bitPointer != 0 } // isCheckmarked reports whether the heap bits have the checkmarked bit set. // It must be told how large the object at h is, because the encoding of the // checkmark bit varies by size. // h must describe the initial word of the object. func (h heapBits) isCheckmarked(size uintptr) bool { if size == sys.PtrSize { return (*h.bitp>>h.shift)&bitPointer != 0 } // All multiword objects are 2-word aligned, // so we know that the initial word's 2-bit pair // and the second word's 2-bit pair are in the // same heap bitmap byte, *h.bitp. return (*h.bitp>>(heapBitsShift+h.shift))&bitScan != 0 } // setCheckmarked sets the checkmarked bit. // It must be told how large the object at h is, because the encoding of the // checkmark bit varies by size. // h must describe the initial word of the object. func (h heapBits) setCheckmarked(size uintptr) { if size == sys.PtrSize { atomic.Or8(h.bitp, bitPointer<> 1 } if bits&1 != 0 { dstx := (*uintptr)(unsafe.Pointer(dst + i)) srcx := (*uintptr)(unsafe.Pointer(src + i)) writebarrierptr_prewrite(dstx, *srcx) } } } // The methods operating on spans all require that h has been returned // by heapBitsForSpan and that size, n, total are the span layout description // returned by the mspan's layout method. // If total > size*n, it means that there is extra leftover memory in the span, // usually due to rounding. // // TODO(rsc): Perhaps introduce a different heapBitsSpan type. // initSpan initializes the heap bitmap for a span. // It clears all checkmark bits. // If this is a span of pointer-sized objects, it initializes all // words to pointer/scan. // Otherwise, it initializes all words to scalar/dead. func (h heapBits) initSpan(s *mspan) { size, n, total := s.layout() // Init the markbit structures s.freeindex = 0 s.allocCache = ^uint64(0) // all 1s indicating all free. s.nelems = n s.allocBits = nil s.gcmarkBits = nil s.gcmarkBits = newMarkBits(s.nelems) s.allocBits = newAllocBits(s.nelems) // Clear bits corresponding to objects. if total%heapBitmapScale != 0 { throw("initSpan: unaligned length") } nbyte := total / heapBitmapScale if sys.PtrSize == 8 && size == sys.PtrSize { end := h.bitp bitp := subtractb(end, nbyte-1) for { *bitp = bitPointerAll | bitScanAll if bitp == end { break } bitp = add1(bitp) } return } memclrNoHeapPointers(unsafe.Pointer(subtractb(h.bitp, nbyte-1)), nbyte) } // initCheckmarkSpan initializes a span for being checkmarked. // It clears the checkmark bits, which are set to 1 in normal operation. func (h heapBits) initCheckmarkSpan(size, n, total uintptr) { // The ptrSize == 8 is a compile-time constant false on 32-bit and eliminates this code entirely. if sys.PtrSize == 8 && size == sys.PtrSize { // Checkmark bit is type bit, bottom bit of every 2-bit entry. // Only possible on 64-bit system, since minimum size is 8. // Must clear type bit (checkmark bit) of every word. // The type bit is the lower of every two-bit pair. bitp := h.bitp for i := uintptr(0); i < n; i += 4 { *bitp &^= bitPointerAll bitp = subtract1(bitp) } return } for i := uintptr(0); i < n; i++ { *h.bitp &^= bitScan << (heapBitsShift + h.shift) h = h.forward(size / sys.PtrSize) } } // clearCheckmarkSpan undoes all the checkmarking in a span. // The actual checkmark bits are ignored, so the only work to do // is to fix the pointer bits. (Pointer bits are ignored by scanobject // but consulted by typedmemmove.) func (h heapBits) clearCheckmarkSpan(size, n, total uintptr) { // The ptrSize == 8 is a compile-time constant false on 32-bit and eliminates this code entirely. if sys.PtrSize == 8 && size == sys.PtrSize { // Checkmark bit is type bit, bottom bit of every 2-bit entry. // Only possible on 64-bit system, since minimum size is 8. // Must clear type bit (checkmark bit) of every word. // The type bit is the lower of every two-bit pair. bitp := h.bitp for i := uintptr(0); i < n; i += 4 { *bitp |= bitPointerAll bitp = subtract1(bitp) } } } // oneBitCount is indexed by byte and produces the // number of 1 bits in that byte. For example 128 has 1 bit set // and oneBitCount[128] will holds 1. var oneBitCount = [256]uint8{ 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8} // countAlloc returns the number of objects allocated in span s by // scanning the allocation bitmap. // TODO:(rlh) Use popcount intrinsic. func (s *mspan) countAlloc() int { count := 0 maxIndex := s.nelems / 8 for i := uintptr(0); i < maxIndex; i++ { mrkBits := *s.gcmarkBits.bytep(i) count += int(oneBitCount[mrkBits]) } if bitsInLastByte := s.nelems % 8; bitsInLastByte != 0 { mrkBits := *s.gcmarkBits.bytep(maxIndex) mask := uint8((1 << bitsInLastByte) - 1) bits := mrkBits & mask count += int(oneBitCount[bits]) } return count } // heapBitsSetType records that the new allocation [x, x+size) // holds in [x, x+dataSize) one or more values of type typ. // (The number of values is given by dataSize / typ.size.) // If dataSize < size, the fragment [x+dataSize, x+size) is // recorded as non-pointer data. // It is known that the type has pointers somewhere; // malloc does not call heapBitsSetType when there are no pointers, // because all free objects are marked as noscan during // heapBitsSweepSpan. // // There can only be one allocation from a given span active at a time, // and the bitmap for a span always falls on byte boundaries, // so there are no write-write races for access to the heap bitmap. // Hence, heapBitsSetType can access the bitmap without atomics. // // There can be read-write races between heapBitsSetType and things // that read the heap bitmap like scanobject. However, since // heapBitsSetType is only used for objects that have not yet been // made reachable, readers will ignore bits being modified by this // function. This does mean this function cannot transiently modify // bits that belong to neighboring objects. Also, on weakly-ordered // machines, callers must execute a store/store (publication) barrier // between calling this function and making the object reachable. func heapBitsSetType(x, size, dataSize uintptr, typ *_type) { const doubleCheck = false // slow but helpful; enable to test modifications to this code // dataSize is always size rounded up to the next malloc size class, // except in the case of allocating a defer block, in which case // size is sizeof(_defer{}) (at least 6 words) and dataSize may be // arbitrarily larger. // // The checks for size == sys.PtrSize and size == 2*sys.PtrSize can therefore // assume that dataSize == size without checking it explicitly. if sys.PtrSize == 8 && size == sys.PtrSize { // It's one word and it has pointers, it must be a pointer. // Since all allocated one-word objects are pointers // (non-pointers are aggregated into tinySize allocations), // initSpan sets the pointer bits for us. Nothing to do here. if doubleCheck { h := heapBitsForAddr(x) if !h.isPointer() { throw("heapBitsSetType: pointer bit missing") } if !h.morePointers() { throw("heapBitsSetType: scan bit missing") } } return } h := heapBitsForAddr(x) ptrmask := typ.gcdata // start of 1-bit pointer mask (or GC program, handled below) // Heap bitmap bits for 2-word object are only 4 bits, // so also shared with objects next to it. // This is called out as a special case primarily for 32-bit systems, // so that on 32-bit systems the code below can assume all objects // are 4-word aligned (because they're all 16-byte aligned). if size == 2*sys.PtrSize { if typ.size == sys.PtrSize { // We're allocating a block big enough to hold two pointers. // On 64-bit, that means the actual object must be two pointers, // or else we'd have used the one-pointer-sized block. // On 32-bit, however, this is the 8-byte block, the smallest one. // So it could be that we're allocating one pointer and this was // just the smallest block available. Distinguish by checking dataSize. // (In general the number of instances of typ being allocated is // dataSize/typ.size.) if sys.PtrSize == 4 && dataSize == sys.PtrSize { // 1 pointer object. On 32-bit machines clear the bit for the // unused second word. *h.bitp &^= (bitPointer | bitScan | ((bitPointer | bitScan) << heapBitsShift)) << h.shift *h.bitp |= (bitPointer | bitScan) << h.shift } else { // 2-element slice of pointer. *h.bitp |= (bitPointer | bitScan | bitPointer<= nw { goto Phase3 } *hbitp = uint8(hb) hbitp = subtract1(hbitp) b >>= 4 nb -= 4 case sys.PtrSize == 8 && h.shift == 2: // Ptrmask and heap bitmap are misaligned. // The bits for the first two words are in a byte shared // with another object, so we must be careful with the bits // already there. // We took care of 1-word and 2-word objects above, // so this is at least a 6-word object. hb = (b & (bitPointer | bitPointer<>= 2 nb -= 2 // Note: no bitScan for second word because that's // the checkmark. *hbitp &^= uint8((bitPointer | bitScan | (bitPointer << heapBitsShift)) << (2 * heapBitsShift)) *hbitp |= uint8(hb) hbitp = subtract1(hbitp) if w += 2; w >= nw { // We know that there is more data, because we handled 2-word objects above. // This must be at least a 6-word object. If we're out of pointer words, // mark no scan in next bitmap byte and finish. hb = 0 w += 4 goto Phase3 } } // Phase 2: Full bytes in bitmap, up to but not including write to last byte (full or partial) in bitmap. // The loop computes the bits for that last write but does not execute the write; // it leaves the bits in hb for processing by phase 3. // To avoid repeated adjustment of nb, we subtract out the 4 bits we're going to // use in the first half of the loop right now, and then we only adjust nb explicitly // if the 8 bits used by each iteration isn't balanced by 8 bits loaded mid-loop. nb -= 4 for { // Emit bitmap byte. // b has at least nb+4 bits, with one exception: // if w+4 >= nw, then b has only nw-w bits, // but we'll stop at the break and then truncate // appropriately in Phase 3. hb = b & bitPointerAll hb |= bitScanAll if w += 4; w >= nw { break } *hbitp = uint8(hb) hbitp = subtract1(hbitp) b >>= 4 // Load more bits. b has nb right now. if p != endp { // Fast path: keep reading from ptrmask. // 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. 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. if nb < 8 { b |= pbits << nb nb += endnb } nb -= 8 // for next iteration } else { // Slow path: reached end of ptrmask. // Process final partial byte and rewind to start. b |= uintptr(*p) << nb nb += endnb if nb < 8 { b |= uintptr(*ptrmask) << nb p = add1(ptrmask) } else { nb -= 8 p = ptrmask } } // Emit bitmap byte. hb = b & bitPointerAll hb |= bitScanAll if w += 4; w >= nw { break } *hbitp = uint8(hb) hbitp = subtract1(hbitp) b >>= 4 } Phase3: // Phase 3: Write last byte or partial byte and zero the rest of the bitmap entries. if w > nw { // Counting the 4 entries in hb not yet written to memory, // there are more entries than possible pointer slots. // Discard the excess entries (can't be more than 3). mask := uintptr(1)<<(4-(w-nw)) - 1 hb &= mask | mask<<4 // apply mask to both pointer bits and scan bits } // Change nw from counting possibly-pointer words to total words in allocation. nw = size / sys.PtrSize // Write whole bitmap bytes. // The first is hb, the rest are zero. if w <= nw { *hbitp = uint8(hb) hbitp = subtract1(hbitp) hb = 0 // for possible final half-byte below for w += 4; w <= nw; w += 4 { *hbitp = 0 hbitp = subtract1(hbitp) } } // Write final partial bitmap byte if any. // We know w > nw, or else we'd still be in the loop above. // It can be bigger only due to the 4 entries in hb that it counts. // If w == nw+4 then there's nothing left to do: we wrote all nw entries // and can discard the 4 sitting in hb. // But if w == nw+2, we need to write first two in hb. // The byte is shared with the next object, so be careful with // existing bits. if w == nw+2 { *hbitp = *hbitp&^(bitPointer|bitScan|(bitPointer|bitScan)<> h.shift) & (bitPointer | bitScan) if i >= totalptr { want = 0 // deadmarker if typ.kind&kindGCProg != 0 && i < (totalptr+3)/4*4 { want = bitScan } } else { if j < nptr && (*addb(ptrmask, j/8)>>(j%8))&1 != 0 { want |= bitPointer } if i != 1 { want |= bitScan } else { have &^= bitScan } } if have != want { println("mismatch writing bits for", *typ.string, "x", dataSize/typ.size) print("typ.size=", typ.size, " typ.ptrdata=", typ.ptrdata, " dataSize=", dataSize, " size=", size, "\n") print("kindGCProg=", typ.kind&kindGCProg != 0, "\n") print("w=", w, " nw=", nw, " b=", hex(b), " nb=", nb, " hb=", hex(hb), "\n") h0 := heapBitsForAddr(x) print("initial bits h0.bitp=", h0.bitp, " h0.shift=", h0.shift, "\n") print("current bits h.bitp=", h.bitp, " h.shift=", h.shift, " *h.bitp=", hex(*h.bitp), "\n") print("ptrmask=", ptrmask, " p=", p, " endp=", endp, " endnb=", endnb, " pbits=", hex(pbits), " b=", hex(b), " nb=", nb, "\n") println("at word", i, "offset", i*sys.PtrSize, "have", have, "want", want) if typ.kind&kindGCProg != 0 { println("GC program:") dumpGCProg(addb(typ.gcdata, 4)) } throw("bad heapBitsSetType") } h = h.next() } if ptrmask == debugPtrmask.data { unlock(&debugPtrmask.lock) } } } var debugPtrmask struct { lock mutex data *byte } // heapBitsSetTypeGCProg implements heapBitsSetType using a GC program. // progSize is the size of the memory described by the program. // elemSize is the size of the element that the GC program describes (a prefix of). // dataSize is the total size of the intended data, a multiple of elemSize. // allocSize is the total size of the allocated memory. // // GC programs are only used for large allocations. // heapBitsSetType requires that allocSize is a multiple of 4 words, // so that the relevant bitmap bytes are not shared with surrounding // objects. func heapBitsSetTypeGCProg(h heapBits, progSize, elemSize, dataSize, allocSize uintptr, prog *byte) { if sys.PtrSize == 8 && allocSize%(4*sys.PtrSize) != 0 { // Alignment will be wrong. throw("heapBitsSetTypeGCProg: small allocation") } var totalBits uintptr if elemSize == dataSize { totalBits = runGCProg(prog, nil, h.bitp, 2) if totalBits*sys.PtrSize != progSize { println("runtime: heapBitsSetTypeGCProg: total bits", totalBits, "but progSize", progSize) throw("heapBitsSetTypeGCProg: unexpected bit count") } } else { count := dataSize / elemSize // Piece together program trailer to run after prog that does: // literal(0) // repeat(1, elemSize-progSize-1) // zeros to fill element size // repeat(elemSize, count-1) // repeat that element for count // This zero-pads the data remaining in the first element and then // repeats that first element to fill the array. var trailer [40]byte // 3 varints (max 10 each) + some bytes i := 0 if n := elemSize/sys.PtrSize - progSize/sys.PtrSize; n > 0 { // literal(0) trailer[i] = 0x01 i++ trailer[i] = 0 i++ if n > 1 { // repeat(1, n-1) trailer[i] = 0x81 i++ n-- for ; n >= 0x80; n >>= 7 { trailer[i] = byte(n | 0x80) i++ } trailer[i] = byte(n) i++ } } // repeat(elemSize/ptrSize, count-1) trailer[i] = 0x80 i++ n := elemSize / sys.PtrSize for ; n >= 0x80; n >>= 7 { trailer[i] = byte(n | 0x80) i++ } trailer[i] = byte(n) i++ n = count - 1 for ; n >= 0x80; n >>= 7 { trailer[i] = byte(n | 0x80) i++ } trailer[i] = byte(n) i++ trailer[i] = 0 i++ runGCProg(prog, &trailer[0], h.bitp, 2) // Even though we filled in the full array just now, // record that we only filled in up to the ptrdata of the // last element. This will cause the code below to // memclr the dead section of the final array element, // so that scanobject can stop early in the final element. totalBits = (elemSize*(count-1) + progSize) / sys.PtrSize } endProg := unsafe.Pointer(subtractb(h.bitp, (totalBits+3)/4)) endAlloc := unsafe.Pointer(subtractb(h.bitp, allocSize/heapBitmapScale)) memclrNoHeapPointers(add(endAlloc, 1), uintptr(endProg)-uintptr(endAlloc)) } // progToPointerMask returns the 1-bit pointer mask output by the GC program prog. // size the size of the region described by prog, in bytes. // The resulting bitvector will have no more than size/sys.PtrSize bits. func progToPointerMask(prog *byte, size uintptr) bitvector { n := (size/sys.PtrSize + 7) / 8 x := (*[1 << 30]byte)(persistentalloc(n+1, 1, &memstats.buckhash_sys))[:n+1] x[len(x)-1] = 0xa1 // overflow check sentinel n = runGCProg(prog, nil, &x[0], 1) if x[len(x)-1] != 0xa1 { throw("progToPointerMask: overflow") } return bitvector{int32(n), &x[0]} } // Packed GC pointer bitmaps, aka GC programs. // // For large types containing arrays, the type information has a // natural repetition that can be encoded to save space in the // binary and in the memory representation of the type information. // // The encoding is a simple Lempel-Ziv style bytecode machine // with the following instructions: // // 00000000: stop // 0nnnnnnn: emit n bits copied from the next (n+7)/8 bytes // 10000000 n c: repeat the previous n bits c times; n, c are varints // 1nnnnnnn c: repeat the previous n bits c times; c is a varint // runGCProg executes the GC program prog, and then trailer if non-nil, // writing to dst with entries of the given size. // If size == 1, dst is a 1-bit pointer mask laid out moving forward from dst. // If size == 2, dst is the 2-bit heap bitmap, and writes move backward // starting at dst (because the heap bitmap does). In this case, the caller guarantees // that only whole bytes in dst need to be written. // // runGCProg returns the number of 1- or 2-bit entries written to memory. func runGCProg(prog, trailer, dst *byte, size int) uintptr { dstStart := dst // Bits waiting to be written to memory. var bits uintptr var nbits uintptr p := prog Run: for { // Flush accumulated full bytes. // The rest of the loop assumes that nbits <= 7. for ; nbits >= 8; nbits -= 8 { if size == 1 { *dst = uint8(bits) dst = add1(dst) bits >>= 8 } else { v := bits&bitPointerAll | bitScanAll *dst = uint8(v) dst = subtract1(dst) bits >>= 4 v = bits&bitPointerAll | bitScanAll *dst = uint8(v) dst = subtract1(dst) bits >>= 4 } } // Process one instruction. inst := uintptr(*p) p = add1(p) n := inst & 0x7F if inst&0x80 == 0 { // Literal bits; n == 0 means end of program. if n == 0 { // Program is over; continue in trailer if present. if trailer != nil { //println("trailer") p = trailer trailer = nil continue } //println("done") break Run } //println("lit", n, dst) nbyte := n / 8 for i := uintptr(0); i < nbyte; i++ { bits |= uintptr(*p) << nbits p = add1(p) if size == 1 { *dst = uint8(bits) dst = add1(dst) bits >>= 8 } else { v := bits&0xf | bitScanAll *dst = uint8(v) dst = subtract1(dst) bits >>= 4 v = bits&0xf | bitScanAll *dst = uint8(v) dst = subtract1(dst) bits >>= 4 } } if n %= 8; n > 0 { bits |= uintptr(*p) << nbits p = add1(p) nbits += n } continue Run } // Repeat. If n == 0, it is encoded in a varint in the next bytes. if n == 0 { for off := uint(0); ; off += 7 { x := uintptr(*p) p = add1(p) n |= (x & 0x7F) << off if x&0x80 == 0 { break } } } // Count is encoded in a varint in the next bytes. c := uintptr(0) for off := uint(0); ; off += 7 { x := uintptr(*p) p = add1(p) c |= (x & 0x7F) << off if x&0x80 == 0 { break } } c *= n // now total number of bits to copy // If the number of bits being repeated is small, load them // into a register and use that register for the entire loop // instead of repeatedly reading from memory. // Handling fewer than 8 bits here makes the general loop simpler. // The cutoff is sys.PtrSize*8 - 7 to guarantee that when we add // the pattern to a bit buffer holding at most 7 bits (a partial byte) // it will not overflow. src := dst const maxBits = sys.PtrSize*8 - 7 if n <= maxBits { // Start with bits in output buffer. pattern := bits npattern := nbits // If we need more bits, fetch them from memory. if size == 1 { src = subtract1(src) for npattern < n { pattern <<= 8 pattern |= uintptr(*src) src = subtract1(src) npattern += 8 } } else { src = add1(src) for npattern < n { pattern <<= 4 pattern |= uintptr(*src) & 0xf src = add1(src) npattern += 4 } } // We started with the whole bit output buffer, // and then we loaded bits from whole bytes. // Either way, we might now have too many instead of too few. // Discard the extra. if npattern > n { pattern >>= npattern - n npattern = n } // Replicate pattern to at most maxBits. if npattern == 1 { // One bit being repeated. // If the bit is 1, make the pattern all 1s. // If the bit is 0, the pattern is already all 0s, // but we can claim that the number of bits // in the word is equal to the number we need (c), // because right shift of bits will zero fill. if pattern == 1 { pattern = 1<8 bits, there will be full bytes to flush // on each iteration. for ; c >= npattern; c -= npattern { bits |= pattern << nbits nbits += npattern if size == 1 { for nbits >= 8 { *dst = uint8(bits) dst = add1(dst) bits >>= 8 nbits -= 8 } } else { for nbits >= 4 { *dst = uint8(bits&0xf | bitScanAll) dst = subtract1(dst) bits >>= 4 nbits -= 4 } } } // Add final fragment to bit buffer. if c > 0 { pattern &= 1< nbits because n > maxBits and nbits <= 7 if size == 1 { // Leading src fragment. src = subtractb(src, (off+7)/8) if frag := off & 7; frag != 0 { bits |= uintptr(*src) >> (8 - frag) << nbits src = add1(src) nbits += frag c -= frag } // Main loop: load one byte, write another. // The bits are rotating through the bit buffer. for i := c / 8; i > 0; i-- { bits |= uintptr(*src) << nbits src = add1(src) *dst = uint8(bits) dst = add1(dst) bits >>= 8 } // Final src fragment. if c %= 8; c > 0 { bits |= (uintptr(*src) & (1<> (4 - frag) << nbits src = subtract1(src) nbits += frag c -= frag } // Main loop: load one byte, write another. // The bits are rotating through the bit buffer. for i := c / 4; i > 0; i-- { bits |= (uintptr(*src) & 0xf) << nbits src = subtract1(src) *dst = uint8(bits&0xf | bitScanAll) dst = subtract1(dst) bits >>= 4 } // Final src fragment. if c %= 4; c > 0 { bits |= (uintptr(*src) & (1< 0; nbits -= 8 { *dst = uint8(bits) dst = add1(dst) bits >>= 8 } } else { totalBits = (uintptr(unsafe.Pointer(dstStart))-uintptr(unsafe.Pointer(dst)))*4 + nbits nbits += -nbits & 3 for ; nbits > 0; nbits -= 4 { v := bits&0xf | bitScanAll *dst = uint8(v) dst = subtract1(dst) bits >>= 4 } } return totalBits } func dumpGCProg(p *byte) { nptr := 0 for { x := *p p = add1(p) if x == 0 { print("\t", nptr, " end\n") break } if x&0x80 == 0 { print("\t", nptr, " lit ", x, ":") n := int(x+7) / 8 for i := 0; i < n; i++ { print(" ", hex(*p)) p = add1(p) } print("\n") nptr += int(x) } else { nbit := int(x &^ 0x80) if nbit == 0 { for nb := uint(0); ; nb += 7 { x := *p p = add1(p) nbit |= int(x&0x7f) << nb if x&0x80 == 0 { break } } } count := 0 for nb := uint(0); ; nb += 7 { x := *p p = add1(p) count |= int(x&0x7f) << nb if x&0x80 == 0 { break } } print("\t", nptr, " repeat ", nbit, " × ", count, "\n") nptr += nbit * count } } } // Testing. // gcbits returns the GC type info for x, for testing. // The result is the bitmap entries (0 or 1), one entry per byte. //go:linkname reflect_gcbits reflect.gcbits func reflect_gcbits(x interface{}) []byte { ret := getgcmask(x) typ := (*ptrtype)(unsafe.Pointer(efaceOf(&x)._type)).elem nptr := typ.ptrdata / sys.PtrSize for uintptr(len(ret)) > nptr && ret[len(ret)-1] == 0 { ret = ret[:len(ret)-1] } return ret } // Returns GC type info for object p for testing. func getgcmask(ep interface{}) (mask []byte) { e := *efaceOf(&ep) p := e.data t := e._type // data or bss roots := gcRoots for roots != nil { for i := 0; i < roots.count; i++ { pr := roots.roots[i] addr := uintptr(pr.decl) if addr <= uintptr(p) && uintptr(p) < addr+pr.size { n := (*ptrtype)(unsafe.Pointer(t)).elem.size mask = make([]byte, n/sys.PtrSize) copy(mask, (*[1 << 29]uint8)(unsafe.Pointer(pr.gcdata))[:pr.ptrdata]) } return } roots = roots.next } // heap var n uintptr var base uintptr if mlookup(uintptr(p), &base, &n, nil) != 0 { mask = make([]byte, n/sys.PtrSize) for i := uintptr(0); i < n; i += sys.PtrSize { hbits := heapBitsForAddr(base + i) if hbits.isPointer() { mask[i/sys.PtrSize] = 1 } if i != 1*sys.PtrSize && !hbits.morePointers() { mask = mask[:i/sys.PtrSize] break } } return } // otherwise, not something the GC knows about. // possibly read-only data, like malloc(0). // must not have pointers // For gccgo, may live on the stack, which is collected conservatively. return }