Skip to content

Commit 957259b

Browse files
committed
runtime: protect against external code calling ExitProcess
On Windows, we implement asynchronous preemption using SuspendThread to suspend other threads in our process. However, SuspendThread is itself actually asynchronous (it enqueues a kernel "asynchronous procedure call" and returns). Unfortunately, Windows' ExitProcess API kills all threads except the calling one and then runs APCs. As a result, if SuspendThread and ExitProcess are called simultaneously, the exiting thread can be suspended and the suspending thread can be exited, leaving behind a ghost process consisting of a single thread that's suspended. We've already protected against the runtime's own calls to ExitProcess, but if Go code calls external code, there's nothing stopping that code from calling ExitProcess. For example, in #35775, our own call to racefini leads to C code calling ExitProcess and occasionally causing a deadlock. This CL fixes this by introducing synchronization between calling external code on Windows and preemption. It adds an atomic field to the M that participates in a simple CAS-based synchronization protocol to prevent suspending a thread running external code. We use this to protect cgocall (which is used for both cgo calls and system calls on Windows) and racefini. Tested by running the flag package's TestParse test compiled in race mode in a loop. Before this change, this would reliably deadlock after a few minutes. Fixes #35775. Updates #10958, #24543. Change-Id: I50d847abcdc2688b4f71eee6a75eca0f2fee892c Reviewed-on: https://go-review.googlesource.com/c/go/+/213837 Run-TryBot: Austin Clements <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Cherry Zhang <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent 6dbcc8b commit 957259b

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

src/runtime/cgocall.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ import (
9090
type cgoCallers [32]uintptr
9191

9292
// Call from Go to C.
93+
//
94+
// This must be nosplit because it's used for syscalls on some
95+
// platforms. Syscalls may have untyped arguments on the stack, so
96+
// it's not safe to grow or scan the stack.
97+
//
9398
//go:nosplit
9499
func cgocall(fn, arg unsafe.Pointer) int32 {
95100
if !iscgo && GOOS != "solaris" && GOOS != "illumos" && GOOS != "windows" {
@@ -127,6 +132,13 @@ func cgocall(fn, arg unsafe.Pointer) int32 {
127132
// saved by entersyscall here.
128133
entersyscall()
129134

135+
// Tell asynchronous preemption that we're entering external
136+
// code. We do this after entersyscall because this may block
137+
// and cause an async preemption to fail, but at this point a
138+
// sync preemption will succeed (though this is not a matter
139+
// of correctness).
140+
osPreemptExtEnter(mp)
141+
130142
mp.incgo = true
131143
errno := asmcgocall(fn, arg)
132144

@@ -135,6 +147,8 @@ func cgocall(fn, arg unsafe.Pointer) int32 {
135147
mp.incgo = false
136148
mp.ncgo--
137149

150+
osPreemptExtExit(mp)
151+
138152
exitsyscall()
139153

140154
// Note that raceacquire must be called only after exitsyscall has
@@ -188,12 +202,16 @@ func cgocallbackg(ctxt uintptr) {
188202
exitsyscall() // coming out of cgo call
189203
gp.m.incgo = false
190204

205+
osPreemptExtExit(gp.m)
206+
191207
cgocallbackg1(ctxt)
192208

193209
// At this point unlockOSThread has been called.
194210
// The following code must not change to a different m.
195211
// This is enforced by checking incgo in the schedule function.
196212

213+
osPreemptExtEnter(gp.m)
214+
197215
gp.m.incgo = true
198216
// going back to cgo call
199217
reentersyscall(savedpc, uintptr(savedsp))
@@ -352,6 +370,7 @@ func unwindm(restore *bool) {
352370
if mp.ncgo > 0 {
353371
mp.incgo = false
354372
mp.ncgo--
373+
osPreemptExtExit(mp)
355374
}
356375

357376
releasem(mp)

src/runtime/os_windows.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,29 @@ type mOS struct {
151151

152152
waitsema uintptr // semaphore for parking on locks
153153
resumesema uintptr // semaphore to indicate suspend/resume
154+
155+
// preemptExtLock synchronizes preemptM with entry/exit from
156+
// external C code.
157+
//
158+
// This protects against races between preemptM calling
159+
// SuspendThread and external code on this thread calling
160+
// ExitProcess. If these happen concurrently, it's possible to
161+
// exit the suspending thread and suspend the exiting thread,
162+
// leading to deadlock.
163+
//
164+
// 0 indicates this M is not being preempted or in external
165+
// code. Entering external code CASes this from 0 to 1. If
166+
// this fails, a preemption is in progress, so the thread must
167+
// wait for the preemption. preemptM also CASes this from 0 to
168+
// 1. If this fails, the preemption fails (as it would if the
169+
// PC weren't in Go code). The value is reset to 0 when
170+
// returning from external code or after a preemption is
171+
// complete.
172+
//
173+
// TODO(austin): We may not need this if preemption were more
174+
// tightly synchronized on the G/P status and preemption
175+
// blocked transition into _Gsyscall/_Psyscall.
176+
preemptExtLock uint32
154177
}
155178

156179
//go:linkname os_sigpipe os.sigpipe
@@ -1121,11 +1144,20 @@ func preemptM(mp *m) {
11211144
throw("self-preempt")
11221145
}
11231146

1147+
// Synchronize with external code that may try to ExitProcess.
1148+
if !atomic.Cas(&mp.preemptExtLock, 0, 1) {
1149+
// External code is running. Fail the preemption
1150+
// attempt.
1151+
atomic.Xadd(&mp.preemptGen, 1)
1152+
return
1153+
}
1154+
11241155
// Acquire our own handle to mp's thread.
11251156
lock(&mp.threadLock)
11261157
if mp.thread == 0 {
11271158
// The M hasn't been minit'd yet (or was just unminit'd).
11281159
unlock(&mp.threadLock)
1160+
atomic.Store(&mp.preemptExtLock, 0)
11291161
atomic.Xadd(&mp.preemptGen, 1)
11301162
return
11311163
}
@@ -1151,6 +1183,7 @@ func preemptM(mp *m) {
11511183
if int32(stdcall1(_SuspendThread, thread)) == -1 {
11521184
unlock(&suspendLock)
11531185
stdcall1(_CloseHandle, thread)
1186+
atomic.Store(&mp.preemptExtLock, 0)
11541187
// The thread no longer exists. This shouldn't be
11551188
// possible, but just acknowledge the request.
11561189
atomic.Xadd(&mp.preemptGen, 1)
@@ -1191,9 +1224,43 @@ func preemptM(mp *m) {
11911224
stdcall2(_SetThreadContext, thread, uintptr(unsafe.Pointer(c)))
11921225
}
11931226

1227+
atomic.Store(&mp.preemptExtLock, 0)
1228+
11941229
// Acknowledge the preemption.
11951230
atomic.Xadd(&mp.preemptGen, 1)
11961231

11971232
stdcall1(_ResumeThread, thread)
11981233
stdcall1(_CloseHandle, thread)
11991234
}
1235+
1236+
// osPreemptExtEnter is called before entering external code that may
1237+
// call ExitProcess.
1238+
//
1239+
// This must be nosplit because it may be called from a syscall with
1240+
// untyped stack slots, so the stack must not be grown or scanned.
1241+
//
1242+
//go:nosplit
1243+
func osPreemptExtEnter(mp *m) {
1244+
for !atomic.Cas(&mp.preemptExtLock, 0, 1) {
1245+
// An asynchronous preemption is in progress. It's not
1246+
// safe to enter external code because it may call
1247+
// ExitProcess and deadlock with SuspendThread.
1248+
// Ideally we would do the preemption ourselves, but
1249+
// can't since there may be untyped syscall arguments
1250+
// on the stack. Instead, just wait and encourage the
1251+
// SuspendThread APC to run. The preemption should be
1252+
// done shortly.
1253+
osyield()
1254+
}
1255+
// Asynchronous preemption is now blocked.
1256+
}
1257+
1258+
// osPreemptExtExit is called after returning from external code that
1259+
// may call ExitProcess.
1260+
//
1261+
// See osPreemptExtEnter for why this is nosplit.
1262+
//
1263+
//go:nosplit
1264+
func osPreemptExtExit(mp *m) {
1265+
atomic.Store(&mp.preemptExtLock, 0)
1266+
}

src/runtime/preempt_nonwindows.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !windows
6+
7+
package runtime
8+
9+
//go:nosplit
10+
func osPreemptExtEnter(mp *m) {}
11+
12+
//go:nosplit
13+
func osPreemptExtExit(mp *m) {}

src/runtime/race.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@ func racefini() {
403403
// already held it's assumed that the first caller exits the program
404404
// so other calls can hang forever without an issue.
405405
lock(&raceFiniLock)
406+
// We're entering external code that may call ExitProcess on
407+
// Windows.
408+
osPreemptExtEnter(getg().m)
406409
racecall(&__tsan_fini, 0, 0, 0, 0)
407410
}
408411

0 commit comments

Comments
 (0)