Skip to content

Commit 5250279

Browse files
committed
runtime: detect and print corrupted free lists
Issues #10240, #10541, #10941, #11023, #11027 and possibly others are indicating memory corruption in the runtime. One of the easiest places to both get corruption and detect it is in the allocator's free lists since they appear throughout memory and follow strict invariants. This commit adds a check when sweeping a span that its free list is sane and, if not, it prints the corrupted free list and panics. Hopefully this will help us collect more information on these failures. Change-Id: I6d417bcaeedf654943a5e068bd76b58bb02d4a64 Reviewed-on: https://go-review.googlesource.com/10713 Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Russ Cox <[email protected]> Run-TryBot: Austin Clements <[email protected]>
1 parent 142e434 commit 5250279

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/runtime/mgcsweep.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,13 @@ func mSpan_Sweep(s *mspan, preserve bool) bool {
178178
sweepgenset := false
179179

180180
// Mark any free objects in this span so we don't collect them.
181+
sstart := uintptr(s.start << _PageShift)
181182
for link := s.freelist; link.ptr() != nil; link = link.ptr().next {
183+
if uintptr(link) < sstart || s.limit <= uintptr(link) {
184+
// Free list is corrupted.
185+
dumpFreeList(s)
186+
throw("free list corrupted")
187+
}
182188
heapBitsForAddr(uintptr(link)).setMarkedNonAtomic()
183189
}
184190

@@ -298,3 +304,27 @@ func mSpan_Sweep(s *mspan, preserve bool) bool {
298304
}
299305
return res
300306
}
307+
308+
func dumpFreeList(s *mspan) {
309+
printlock()
310+
print("runtime: free list of span ", s, ":\n")
311+
sstart := uintptr(s.start << _PageShift)
312+
link := s.freelist
313+
for i := 0; i < int(s.npages*_PageSize/s.elemsize); i++ {
314+
if i != 0 {
315+
print(" -> ")
316+
}
317+
print(hex(link))
318+
if link.ptr() == nil {
319+
break
320+
}
321+
if uintptr(link) < sstart || s.limit <= uintptr(link) {
322+
// Bad link. Stop walking before we crash.
323+
print(" (BAD)")
324+
break
325+
}
326+
link = link.ptr().next
327+
}
328+
print("\n")
329+
printunlock()
330+
}

0 commit comments

Comments
 (0)