Skip to content

Commit

Permalink
runtime: detect and print corrupted free lists
Browse files Browse the repository at this point in the history
Issues golang#10240, golang#10541, golang#10941, golang#11023, golang#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]>
  • Loading branch information
aclements committed Jun 16, 2015
1 parent 142e434 commit 5250279
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/runtime/mgcsweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,13 @@ func mSpan_Sweep(s *mspan, preserve bool) bool {
sweepgenset := false

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

Expand Down Expand Up @@ -298,3 +304,27 @@ func mSpan_Sweep(s *mspan, preserve bool) bool {
}
return res
}

func dumpFreeList(s *mspan) {
printlock()
print("runtime: free list of span ", s, ":\n")
sstart := uintptr(s.start << _PageShift)
link := s.freelist
for i := 0; i < int(s.npages*_PageSize/s.elemsize); i++ {
if i != 0 {
print(" -> ")
}
print(hex(link))
if link.ptr() == nil {
break
}
if uintptr(link) < sstart || s.limit <= uintptr(link) {
// Bad link. Stop walking before we crash.
print(" (BAD)")
break
}
link = link.ptr().next
}
print("\n")
printunlock()
}

0 comments on commit 5250279

Please sign in to comment.