diff options
| author | Russ Cox <rsc@golang.org> | 2014-12-22 10:53:51 -0500 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2015-01-06 00:26:35 +0000 |
| commit | dcec123a4923437242c52d2693ace80d2f3c704e (patch) | |
| tree | 9f1d6a2a5bcf75e86e84a657a7aca5b522947ab5 /src/runtime/malloc1.go | |
| parent | 3191a235158233bb6f6d960d7ae0cb925606f817 (diff) | |
| download | go-dcec123a4923437242c52d2693ace80d2f3c704e.tar.xz | |
runtime: add GODEBUG wbshadow for finding missing write barriers
This is the detection code. It works well enough that I know of
a handful of missing write barriers. However, those are subtle
enough that I'll address them in separate followup CLs.
GODEBUG=wbshadow=1 checks for a write that bypassed the
write barrier at the next write barrier of the same word.
If a bug can be detected in this mode it is typically easy to
understand, since the crash says quite clearly what kind of
word has missed a write barrier.
GODEBUG=wbshadow=2 adds a check of the write barrier
shadow copy during garbage collection. Bugs detected at
garbage collection can be difficult to understand, because
there is no context for what the found word means.
Typically you have to reproduce the problem with allocfreetrace=1
in order to understand the type of the badly updated word.
Change-Id: If863837308e7c50d96b5bdc7d65af4969bf53a6e
Reviewed-on: https://go-review.googlesource.com/2061
Reviewed-by: Austin Clements <austin@google.com>
Diffstat (limited to 'src/runtime/malloc1.go')
| -rw-r--r-- | src/runtime/malloc1.go | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/runtime/malloc1.go b/src/runtime/malloc1.go index 50e272c1c1..7c2a4c2f27 100644 --- a/src/runtime/malloc1.go +++ b/src/runtime/malloc1.go @@ -223,6 +223,69 @@ func mallocinit() { _g_.m.mcache = allocmcache() } +func wbshadowinit() { + // Initialize write barrier shadow heap if we were asked for it + // and we have enough address space (not on 32-bit). + if debug.wbshadow == 0 { + return + } + if ptrSize != 8 { + print("runtime: GODEBUG=wbshadow=1 disabled on 32-bit system\n") + return + } + + var reserved bool + p1 := sysReserve(nil, mheap_.arena_end-mheap_.arena_start, &reserved) + if p1 == nil { + throw("cannot map shadow heap") + } + mheap_.shadow_heap = uintptr(p1) - mheap_.arena_start + sysMap(p1, mheap_.arena_used-mheap_.arena_start, reserved, &memstats.other_sys) + memmove(p1, unsafe.Pointer(mheap_.arena_start), mheap_.arena_used-mheap_.arena_start) + + mheap_.shadow_reserved = reserved + start := ^uintptr(0) + end := uintptr(0) + if start > uintptr(unsafe.Pointer(&noptrdata)) { + start = uintptr(unsafe.Pointer(&noptrdata)) + } + if start > uintptr(unsafe.Pointer(&data)) { + start = uintptr(unsafe.Pointer(&data)) + } + if start > uintptr(unsafe.Pointer(&noptrbss)) { + start = uintptr(unsafe.Pointer(&noptrbss)) + } + if start > uintptr(unsafe.Pointer(&bss)) { + start = uintptr(unsafe.Pointer(&bss)) + } + if end < uintptr(unsafe.Pointer(&enoptrdata)) { + end = uintptr(unsafe.Pointer(&enoptrdata)) + } + if end < uintptr(unsafe.Pointer(&edata)) { + end = uintptr(unsafe.Pointer(&edata)) + } + if end < uintptr(unsafe.Pointer(&enoptrbss)) { + end = uintptr(unsafe.Pointer(&enoptrbss)) + } + if end < uintptr(unsafe.Pointer(&ebss)) { + end = uintptr(unsafe.Pointer(&ebss)) + } + start &^= _PageSize - 1 + end = round(end, _PageSize) + mheap_.data_start = start + mheap_.data_end = end + reserved = false + p1 = sysReserve(nil, end-start, &reserved) + if p1 == nil { + throw("cannot map shadow data") + } + mheap_.shadow_data = uintptr(p1) - start + sysMap(p1, end-start, reserved, &memstats.other_sys) + memmove(p1, unsafe.Pointer(start), end-start) + + mheap_.shadow_enabled = true +} + func mHeap_SysAlloc(h *mheap, n uintptr) unsafe.Pointer { if n > uintptr(h.arena_end)-uintptr(h.arena_used) { // We are in 32-bit mode, maybe we didn't use all possible address space yet. @@ -260,6 +323,9 @@ func mHeap_SysAlloc(h *mheap, n uintptr) unsafe.Pointer { if raceenabled { racemapshadow((unsafe.Pointer)(p), n) } + if mheap_.shadow_enabled { + sysMap(unsafe.Pointer(p+mheap_.shadow_heap), n, h.shadow_reserved, &memstats.other_sys) + } if uintptr(p)&(_PageSize-1) != 0 { throw("misrounded allocation in MHeap_SysAlloc") |
