Skip to content

Commit

Permalink
runtime: detect errors in DuplicateHandle
Browse files Browse the repository at this point in the history
These functions rely on DuplicateHandle succeeding, but they don't check
the return value, which might be masking subtle bugs that cause other
problems down the line.

Updates golang#43720.

Change-Id: I77f0e6645affa534777ffc173144a52e4afa5f81
Reviewed-on: https://go-review.googlesource.com/c/go/+/284135
Run-TryBot: Jason A. Donenfeld <[email protected]>
Reviewed-by: Alex Brainman <[email protected]>
Reviewed-by: Austin Clements <[email protected]>
Trust: Alex Brainman <[email protected]>
Trust: Jason A. Donenfeld <[email protected]>
  • Loading branch information
zx2c4 committed Jan 15, 2021
1 parent 9f83418 commit 682a1d2
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/runtime/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,10 @@ func sigblock(exiting bool) {
// Called on the new thread, cannot allocate memory.
func minit() {
var thandle uintptr
stdcall7(_DuplicateHandle, currentProcess, currentThread, currentProcess, uintptr(unsafe.Pointer(&thandle)), 0, 0, _DUPLICATE_SAME_ACCESS)
if stdcall7(_DuplicateHandle, currentProcess, currentThread, currentProcess, uintptr(unsafe.Pointer(&thandle)), 0, 0, _DUPLICATE_SAME_ACCESS) == 0 {
print("runtime.minit: duplicatehandle failed; errno=", getlasterror(), "\n")
throw("runtime.minit: duplicatehandle failed")
}

// Configure usleep timer, if possible.
var timer uintptr
Expand Down Expand Up @@ -1134,8 +1137,12 @@ func profileloop1(param uintptr) uint32 {
}
// Acquire our own handle to the thread.
var thread uintptr
stdcall7(_DuplicateHandle, currentProcess, mp.thread, currentProcess, uintptr(unsafe.Pointer(&thread)), 0, 0, _DUPLICATE_SAME_ACCESS)
if stdcall7(_DuplicateHandle, currentProcess, mp.thread, currentProcess, uintptr(unsafe.Pointer(&thread)), 0, 0, _DUPLICATE_SAME_ACCESS) == 0 {
print("runtime.profileloop1: duplicatehandle failed; errno=", getlasterror(), "\n")
throw("runtime.profileloop1: duplicatehandle failed")
}
unlock(&mp.threadLock)

// mp may exit between the DuplicateHandle
// above and the SuspendThread. The handle
// will remain valid, but SuspendThread may
Expand Down Expand Up @@ -1214,7 +1221,10 @@ func preemptM(mp *m) {
return
}
var thread uintptr
stdcall7(_DuplicateHandle, currentProcess, mp.thread, currentProcess, uintptr(unsafe.Pointer(&thread)), 0, 0, _DUPLICATE_SAME_ACCESS)
if stdcall7(_DuplicateHandle, currentProcess, mp.thread, currentProcess, uintptr(unsafe.Pointer(&thread)), 0, 0, _DUPLICATE_SAME_ACCESS) == 0 {
print("runtime.preemptM: duplicatehandle failed; errno=", getlasterror(), "\n")
throw("runtime.preemptM: duplicatehandle failed")
}
unlock(&mp.threadLock)

// Prepare thread context buffer. This must be aligned to 16 bytes.
Expand Down

0 comments on commit 682a1d2

Please sign in to comment.