]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: tidy Context allocation
authorAustin Clements <austin@google.com>
Tue, 19 Nov 2019 19:48:17 +0000 (14:48 -0500)
committerAustin Clements <austin@google.com>
Thu, 16 Apr 2020 13:02:31 +0000 (13:02 +0000)
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 <austin@google.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
src/runtime/os_windows.go

index 1298a140179b83dea599f8e6fea621d42fb9350f..7baba83817f0d1ff4daf62be468b9a40d94c71dd 100644 (file)
@@ -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