// pointer into Go memory. If it does, we panic.
// The return values are unused but useful to see in panic tracebacks.
func cgoCheckUnknownPointer(p unsafe.Pointer, msg string) (base, i uintptr) {
- if cgoInRange(p, mheap_.arena_start, mheap_.arena_used) {
- if !inheap(uintptr(p)) {
- // On 32-bit systems it is possible for C's allocated memory
- // to have addresses between arena_start and arena_used.
- // Either this pointer is a stack or an unused span or it's
- // a C allocation. Escape analysis should prevent the first,
- // garbage collection should prevent the second,
- // and the third is completely OK.
- return
- }
-
+ if inheap(uintptr(p)) {
b, span, _ := findObject(uintptr(p), 0, 0)
base = b
if base == 0 {
// 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 heapBits{bitp, uint32(off & 3), uint32(arena), last}
}
-// 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)
-}
-
// findObject returns the base address for the heap object containing
// the address p, the object's span, and the index of the object in s.
// If p does not point into a heap object, it returns base == 0.
b := b0
n := n0
- arena_start := mheap_.arena_start
- arena_used := mheap_.arena_used
-
for i := uintptr(0); i < n; {
// Find bits for the next word.
bits := uint32(*addb(ptrmask, i/(sys.PtrSize*8)))
if bits&1 != 0 {
// Same work as in scanobject; see comments there.
obj := *(*uintptr)(unsafe.Pointer(b + i))
- if obj != 0 && arena_start <= obj && obj < arena_used {
+ if obj != 0 {
if obj, span, objIndex := findObject(obj, b, i); obj != 0 {
greyobject(obj, b, i, span, gcw, objIndex)
}
//
//go:nowritebarrier
func scanobject(b uintptr, gcw *gcWork) {
- // Note that arena_used may change concurrently during
- // scanobject and hence scanobject may encounter a pointer to
- // a newly allocated heap object that is *not* in
- // [start,used). It will not mark this object; however, we
- // know that it was just installed by a mutator, which means
- // that mutator will execute a write barrier and take care of
- // marking it. This is even more pronounced on relaxed memory
- // architectures since we access arena_used without barriers
- // or synchronization, but the same logic applies.
- arena_start := mheap_.arena_start
- arena_used := mheap_.arena_used
-
// Find the bits for b and the size of the object at b.
//
// b is either the beginning of an object, in which case this
obj := *(*uintptr)(unsafe.Pointer(b + i))
// At this point we have extracted the next potential pointer.
- // Check if it points into heap and not back at the current object.
- if obj != 0 && arena_start <= obj && obj < arena_used && obj-b >= n {
- // Mark the object.
+ // Quickly filter out nil and pointers back to the current object.
+ if obj != 0 && obj-b >= n {
+ // Test if obj points into the Go heap and, if so,
+ // mark the object.
+ //
+ // Note that it's possible for findObject to
+ // fail if obj points to a just-allocated heap
+ // object because of a race with growing the
+ // heap. In this case, we know the object was
+ // just allocated and hence will be marked by
+ // allocation itself.
if obj, span, objIndex := findObject(obj, b, i); obj != 0 {
greyobject(obj, b, i, span, gcw, objIndex)
}
// gcDumpObject dumps the contents of obj for debugging and marks the
// field at byte offset off in obj.
func gcDumpObject(label string, obj, off uintptr) {
- if obj < mheap_.arena_start || obj >= mheap_.arena_used {
- print(label, "=", hex(obj), " is not in the Go heap\n")
- return
- }
s := spanOf(obj)
print(label, "=", hex(obj))
if s == nil {
useCheckmark = true
for _, s := range mheap_.allspans {
if s.state == _MSpanInUse {
- heapBitsForSpan(s.base()).initCheckmarkSpan(s.layout())
+ heapBitsForAddr(s.base()).initCheckmarkSpan(s.layout())
}
}
}
useCheckmark = false
for _, s := range mheap_.allspans {
if s.state == _MSpanInUse {
- heapBitsForSpan(s.base()).clearCheckmarkSpan(s.layout())
+ heapBitsForAddr(s.base()).clearCheckmarkSpan(s.layout())
}
}
}