aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/mheap.go
diff options
context:
space:
mode:
authorMichael Anthony Knyszek <mknyszek@google.com>2025-06-18 17:42:16 +0000
committerGopher Robot <gobot@golang.org>2025-06-18 11:34:51 -0700
commit4c7567290ced9c4dc629f2386f2eebfebba95ce6 (patch)
tree3a7ee7e73a2d91a592c38cc3942857a4a457239e /src/runtime/mheap.go
parentc6ac7362888c25dd1251adaa11e1503cf78ec26d (diff)
downloadgo-4c7567290ced9c4dc629f2386f2eebfebba95ce6.tar.xz
runtime: set mspan limit field early and eagerly
Currently the mspan limit field is set after allocSpan returns, *after* the span has already been published to the GC (including the conservative scanner). But the limit field is load-bearing, because it's checked to filter out invalid pointers. A stale limit value could cause a crash by having the conservative scanner access allocBits out of bounds. Fix this by setting the mspan limit field before publishing the span. For large objects and arena chunks, we adjust the limit down after allocSpan because we don't have access to the true object's size from allocSpan. However this is safe, since we first initialize the limit to something definitely safe (the actual span bounds) and only adjust it down after. Adjusting it down has the benefit of more precise debug output, but the window in which it's imprecise is also fine because a single object (logically, with arena chunks) occupies the whole span, so the 'invalid' part of the memory will just safely point back to that object. We can't do this for smaller objects because the limit will include space that does *not* contain any valid objects. Fixes #74288. Change-Id: I0a22e5b9bccc1bfdf51d2b73ea7130f1b99c0c7c Reviewed-on: https://go-review.googlesource.com/c/go/+/682655 Reviewed-by: Keith Randall <khr@google.com> Auto-Submit: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'src/runtime/mheap.go')
-rw-r--r--src/runtime/mheap.go5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/runtime/mheap.go b/src/runtime/mheap.go
index 9361089b80..3965d6eb52 100644
--- a/src/runtime/mheap.go
+++ b/src/runtime/mheap.go
@@ -1445,7 +1445,6 @@ func (h *mheap) initSpan(s *mspan, typ spanAllocType, spanclass spanClass, base,
if typ.manual() {
s.manualFreeList = 0
s.nelems = 0
- s.limit = s.base() + s.npages*pageSize
s.state.set(mSpanManual)
} else {
// We must set span properties before the span is published anywhere
@@ -1486,6 +1485,9 @@ func (h *mheap) initSpan(s *mspan, typ spanAllocType, spanclass spanClass, base,
s.gcmarkBits = newMarkBits(uintptr(s.nelems))
s.allocBits = newAllocBits(uintptr(s.nelems))
+ // Adjust s.limit down to the object-containing part of the span.
+ s.limit = s.base() + uintptr(s.elemsize)*uintptr(s.nelems)
+
// It's safe to access h.sweepgen without the heap lock because it's
// only ever updated with the world stopped and we run on the
// systemstack which blocks a STW transition.
@@ -1785,6 +1787,7 @@ func (span *mspan) init(base uintptr, npages uintptr) {
span.list = nil
span.startAddr = base
span.npages = npages
+ span.limit = base + npages*gc.PageSize // see go.dev/issue/74288; adjusted later for heap spans
span.allocCount = 0
span.spanclass = 0
span.elemsize = 0