From 4c7567290ced9c4dc629f2386f2eebfebba95ce6 Mon Sep 17 00:00:00 2001 From: Michael Anthony Knyszek Date: Wed, 18 Jun 2025 17:42:16 +0000 Subject: 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 Auto-Submit: Michael Knyszek LUCI-TryBot-Result: Go LUCI Reviewed-by: Keith Randall --- src/runtime/mcentral.go | 3 --- 1 file changed, 3 deletions(-) (limited to 'src/runtime/mcentral.go') diff --git a/src/runtime/mcentral.go b/src/runtime/mcentral.go index c71ecbbcd5..ec27ce25a8 100644 --- a/src/runtime/mcentral.go +++ b/src/runtime/mcentral.go @@ -250,13 +250,10 @@ func (c *mcentral) uncacheSpan(s *mspan) { // grow allocates a new empty span from the heap and initializes it for c's size class. func (c *mcentral) grow() *mspan { npages := uintptr(gc.SizeClassToNPages[c.spanclass.sizeclass()]) - size := uintptr(gc.SizeClassToSize[c.spanclass.sizeclass()]) - s := mheap_.alloc(npages, c.spanclass) if s == nil { return nil } - s.limit = s.base() + size*uintptr(s.nelems) s.initHeapBits() return s } -- cgit v1.3