Skip to content

Commit 682a1d2

Browse files
committed
runtime: detect errors in DuplicateHandle
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 #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]>
1 parent 9f83418 commit 682a1d2

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/runtime/os_windows.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,10 @@ func sigblock(exiting bool) {
893893
// Called on the new thread, cannot allocate memory.
894894
func minit() {
895895
var thandle uintptr
896-
stdcall7(_DuplicateHandle, currentProcess, currentThread, currentProcess, uintptr(unsafe.Pointer(&thandle)), 0, 0, _DUPLICATE_SAME_ACCESS)
896+
if stdcall7(_DuplicateHandle, currentProcess, currentThread, currentProcess, uintptr(unsafe.Pointer(&thandle)), 0, 0, _DUPLICATE_SAME_ACCESS) == 0 {
897+
print("runtime.minit: duplicatehandle failed; errno=", getlasterror(), "\n")
898+
throw("runtime.minit: duplicatehandle failed")
899+
}
897900

898901
// Configure usleep timer, if possible.
899902
var timer uintptr
@@ -1134,8 +1137,12 @@ func profileloop1(param uintptr) uint32 {
11341137
}
11351138
// Acquire our own handle to the thread.
11361139
var thread uintptr
1137-
stdcall7(_DuplicateHandle, currentProcess, mp.thread, currentProcess, uintptr(unsafe.Pointer(&thread)), 0, 0, _DUPLICATE_SAME_ACCESS)
1140+
if stdcall7(_DuplicateHandle, currentProcess, mp.thread, currentProcess, uintptr(unsafe.Pointer(&thread)), 0, 0, _DUPLICATE_SAME_ACCESS) == 0 {
1141+
print("runtime.profileloop1: duplicatehandle failed; errno=", getlasterror(), "\n")
1142+
throw("runtime.profileloop1: duplicatehandle failed")
1143+
}
11381144
unlock(&mp.threadLock)
1145+
11391146
// mp may exit between the DuplicateHandle
11401147
// above and the SuspendThread. The handle
11411148
// will remain valid, but SuspendThread may
@@ -1214,7 +1221,10 @@ func preemptM(mp *m) {
12141221
return
12151222
}
12161223
var thread uintptr
1217-
stdcall7(_DuplicateHandle, currentProcess, mp.thread, currentProcess, uintptr(unsafe.Pointer(&thread)), 0, 0, _DUPLICATE_SAME_ACCESS)
1224+
if stdcall7(_DuplicateHandle, currentProcess, mp.thread, currentProcess, uintptr(unsafe.Pointer(&thread)), 0, 0, _DUPLICATE_SAME_ACCESS) == 0 {
1225+
print("runtime.preemptM: duplicatehandle failed; errno=", getlasterror(), "\n")
1226+
throw("runtime.preemptM: duplicatehandle failed")
1227+
}
12181228
unlock(&mp.threadLock)
12191229

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

0 commit comments

Comments
 (0)