From 955cc07dde70415489fb2096eb575654181e21fe Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Fri, 29 Jun 2018 14:56:48 -0400 Subject: runtime: remap stack spans with MAP_STACK on OpenBSD OpenBSD 6.4 is going to start requiring that the SP points to memory that was mapped with MAP_STACK on system call entry, traps, and when switching to the alternate signal stack [1]. Currently, Go doesn't map any memory MAP_STACK, so the kernel quickly kills Go processes. Fix this by remapping the memory that backs stack spans with MAP_STACK, and re-remapping it without MAP_STACK when it's returned to the heap. [1] http://openbsd-archive.7691.n7.nabble.com/stack-register-checking-td338238.html Fixes #26142. Change-Id: I656eb84385a22833445d49328bb304f8cdd0e225 Reviewed-on: https://go-review.googlesource.com/121657 Run-TryBot: Austin Clements TryBot-Result: Gobot Gobot Reviewed-by: Matthew Dempsky --- src/runtime/stack.go | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/runtime/stack.go') diff --git a/src/runtime/stack.go b/src/runtime/stack.go index 648603db35..c7bfc0434b 100644 --- a/src/runtime/stack.go +++ b/src/runtime/stack.go @@ -186,6 +186,7 @@ func stackpoolalloc(order uint8) gclinkptr { if s.manualFreeList.ptr() != nil { throw("bad manualFreeList") } + osStackAlloc(s) s.elemsize = _FixedStack << order for i := uintptr(0); i < _StackCacheSize; i += s.elemsize { x := gclinkptr(s.base() + i) @@ -238,6 +239,7 @@ func stackpoolfree(x gclinkptr, order uint8) { // By not freeing, we prevent step #4 until GC is done. stackpool[order].remove(s) s.manualFreeList = 0 + osStackFree(s) mheap_.freeManual(s, &memstats.stacks_inuse) } } @@ -385,6 +387,7 @@ func stackalloc(n uint32) stack { if s == nil { throw("out of memory") } + osStackAlloc(s) s.elemsize = uintptr(n) } v = unsafe.Pointer(s.base()) @@ -463,6 +466,7 @@ func stackfree(stk stack) { if gcphase == _GCoff { // Free the stack immediately if we're // sweeping. + osStackFree(s) mheap_.freeManual(s, &memstats.stacks_inuse) } else { // If the GC is running, we can't return a @@ -1112,6 +1116,7 @@ func freeStackSpans() { if s.allocCount == 0 { list.remove(s) s.manualFreeList = 0 + osStackFree(s) mheap_.freeManual(s, &memstats.stacks_inuse) } s = next @@ -1126,6 +1131,7 @@ func freeStackSpans() { for s := stackLarge.free[i].first; s != nil; { next := s.next stackLarge.free[i].remove(s) + osStackFree(s) mheap_.freeManual(s, &memstats.stacks_inuse) s = next } -- cgit v1.3