From b1dbce31d7bbd7c3fdff1679d6f08c107d0be9a5 Mon Sep 17 00:00:00 2001 From: Richard Miller Date: Fri, 16 Feb 2018 15:20:04 +0000 Subject: [PATCH] runtime: don't ignore address hint for sysReserve in Plan 9 On Plan 9, sysReserve was ignoring the address hint and allocating memory wherever it is available. This causes the new TestArenaCollision test to fail on 32-bit Plan 9. We now use the address hint in the specific case where sysReserve is extending the process address space at its end, and similarly we contract the address space in the case where sysFree is releasing memory at the end. Fixes #23860 Change-Id: Ia5254779ba8f1698c999832720a88de400b5f91a Reviewed-on: https://go-review.googlesource.com/94776 Reviewed-by: Austin Clements Reviewed-by: David du Colombier <0intro@gmail.com> --- src/runtime/mem_plan9.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/runtime/mem_plan9.go b/src/runtime/mem_plan9.go index ca8c437d1a..b80d030b24 100644 --- a/src/runtime/mem_plan9.go +++ b/src/runtime/mem_plan9.go @@ -149,8 +149,15 @@ func sysAlloc(n uintptr, sysStat *uint64) unsafe.Pointer { func sysFree(v unsafe.Pointer, n uintptr, sysStat *uint64) { mSysStatDec(sysStat, n) lock(&memlock) - memFree(v, n) - memCheck() + if uintptr(v)+n == bloc { + // address range being freed is at the end of memory, + // so shrink the address space + bloc -= n + brk_(unsafe.Pointer(bloc)) + } else { + memFree(v, n) + memCheck() + } unlock(&memlock) } @@ -171,8 +178,16 @@ func sysFault(v unsafe.Pointer, n uintptr) { func sysReserve(v unsafe.Pointer, n uintptr) unsafe.Pointer { lock(&memlock) - p := memAlloc(n) - memCheck() + var p unsafe.Pointer + if uintptr(v) == bloc { + // address hint is the current end of memory, + // so try to extend the address space + p = sbrk(n) + } + if p == nil { + p = memAlloc(n) + memCheck() + } unlock(&memlock) return p } -- 2.50.0