From 2a029b3f26169be7c89cb2cdcc3db4b5d097a8b8 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Tue, 19 Nov 2019 14:48:17 -0500 Subject: [PATCH] runtime: tidy Context allocation The Context object we pass to GetThreadContext on Windows must be 16 byte-aligned. We also can't allocate in the contexts where we create these, so they must be stack-allocated. There's no great way to do this, but this CL makes the code at least a little clearer, and makes profilem and preemptM more consistent with each other. Change-Id: I5ec47a27d7580ed6003030bf953e668e8cae2cef Reviewed-on: https://go-review.googlesource.com/c/go/+/207967 Run-TryBot: Austin Clements Reviewed-by: Cherry Zhang Reviewed-by: Alex Brainman --- src/runtime/os_windows.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/runtime/os_windows.go b/src/runtime/os_windows.go index 1298a14017..7baba83817 100644 --- a/src/runtime/os_windows.go +++ b/src/runtime/os_windows.go @@ -1027,17 +1027,17 @@ func callbackasm1() var profiletimer uintptr func profilem(mp *m, thread uintptr) { - var r *context - rbuf := make([]byte, unsafe.Sizeof(*r)+15) + // Align Context to 16 bytes. + var c *context + var cbuf [unsafe.Sizeof(*c) + 15]byte + c = (*context)(unsafe.Pointer((uintptr(unsafe.Pointer(&cbuf[15]))) &^ 15)) - // align Context to 16 bytes - r = (*context)(unsafe.Pointer((uintptr(unsafe.Pointer(&rbuf[15]))) &^ 15)) - r.contextflags = _CONTEXT_CONTROL - stdcall2(_GetThreadContext, thread, uintptr(unsafe.Pointer(r))) + c.contextflags = _CONTEXT_CONTROL + stdcall2(_GetThreadContext, thread, uintptr(unsafe.Pointer(c))) gp := gFromTLS(mp) - sigprof(r.ip(), r.sp(), r.lr(), gp, mp) + sigprof(c.ip(), c.sp(), c.lr(), gp, mp) } func gFromTLS(mp *m) *g { @@ -1153,10 +1153,9 @@ func preemptM(mp *m) { stdcall7(_DuplicateHandle, currentProcess, mp.thread, currentProcess, uintptr(unsafe.Pointer(&thread)), 0, 0, _DUPLICATE_SAME_ACCESS) unlock(&mp.threadLock) - // Prepare thread context buffer. + // Prepare thread context buffer. This must be aligned to 16 bytes. var c *context - cbuf := make([]byte, unsafe.Sizeof(*c)+15) - // Align Context to 16 bytes. + var cbuf [unsafe.Sizeof(*c) + 15]byte c = (*context)(unsafe.Pointer((uintptr(unsafe.Pointer(&cbuf[15]))) &^ 15)) c.contextflags = _CONTEXT_CONTROL