Skip to content

Commit b1dbce3

Browse files
Richard Miller0intro
Richard Miller
authored andcommitted
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 <[email protected]> Reviewed-by: David du Colombier <[email protected]>
1 parent 07f0f09 commit b1dbce3

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/runtime/mem_plan9.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,15 @@ func sysAlloc(n uintptr, sysStat *uint64) unsafe.Pointer {
149149
func sysFree(v unsafe.Pointer, n uintptr, sysStat *uint64) {
150150
mSysStatDec(sysStat, n)
151151
lock(&memlock)
152-
memFree(v, n)
153-
memCheck()
152+
if uintptr(v)+n == bloc {
153+
// address range being freed is at the end of memory,
154+
// so shrink the address space
155+
bloc -= n
156+
brk_(unsafe.Pointer(bloc))
157+
} else {
158+
memFree(v, n)
159+
memCheck()
160+
}
154161
unlock(&memlock)
155162
}
156163

@@ -171,8 +178,16 @@ func sysFault(v unsafe.Pointer, n uintptr) {
171178

172179
func sysReserve(v unsafe.Pointer, n uintptr) unsafe.Pointer {
173180
lock(&memlock)
174-
p := memAlloc(n)
175-
memCheck()
181+
var p unsafe.Pointer
182+
if uintptr(v) == bloc {
183+
// address hint is the current end of memory,
184+
// so try to extend the address space
185+
p = sbrk(n)
186+
}
187+
if p == nil {
188+
p = memAlloc(n)
189+
memCheck()
190+
}
176191
unlock(&memlock)
177192
return p
178193
}

0 commit comments

Comments
 (0)