From 813d8e886208e5e001df3b55e8bc1c65ca4249ff Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Wed, 23 Oct 2019 14:28:55 +0700 Subject: [PATCH] runtime: factor out debug.invalidptr case in findObject This helps keeping findObject's frame small. Updates #35068 Change-Id: I1b8c1fcc5831944c86f1a30ed2f2d867a5f2b242 Reviewed-on: https://go-review.googlesource.com/c/go/+/202797 Run-TryBot: Cuong Manh Le TryBot-Result: Gobot Gobot Reviewed-by: Austin Clements --- src/runtime/mbitmap.go | 50 +++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/runtime/mbitmap.go b/src/runtime/mbitmap.go index 7f9f71842d..68a22690d2 100644 --- a/src/runtime/mbitmap.go +++ b/src/runtime/mbitmap.go @@ -349,6 +349,32 @@ func heapBitsForAddr(addr uintptr) (h heapBits) { return } +// badPointer throws bad pointer in heap panic. +func badPointer(s *mspan, p, refBase, refOff uintptr) { + // 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(" 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?)") +} + // 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. @@ -372,32 +398,10 @@ func findObject(p, refBase, refOff uintptr) (base uintptr, s *mspan, objIndex ui if s.state == mSpanManual { 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(" 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?)") + badPointer(s, p, refBase, refOff) } return } -- 2.48.1