Skip to content

Commit 7f688d1

Browse files
committed
runtime: mlock signal stack on macOS/ARM64
Apparently, the macOS ARM64 kernel has a bug where when a signal arrives and the signal stack is not currently faulted in, it may kill the program with a SIGILL. Work around it by mlock the signal stacks. Fixes #42774. Change-Id: I99a4b3fdb6d8af1c945725ddc2c25568d81c510a Reviewed-on: https://go-review.googlesource.com/c/go/+/273686 Trust: Cherry Zhang <[email protected]> Reviewed-by: Austin Clements <[email protected]> Run-TryBot: Austin Clements <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent d2b436d commit 7f688d1

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

src/runtime/os_darwin.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@ func libpreinit() {
283283
func mpreinit(mp *m) {
284284
mp.gsignal = malg(32 * 1024) // OS X wants >= 8K
285285
mp.gsignal.m = mp
286+
if GOOS == "darwin" && GOARCH == "arm64" {
287+
// mlock the signal stack to work around a kernel bug where it may
288+
// SIGILL when the signal stack is not faulted in while a signal
289+
// arrives. See issue 42774.
290+
mlock(unsafe.Pointer(mp.gsignal.stack.hi-physPageSize), physPageSize)
291+
}
286292
}
287293

288294
// Called to initialize a new m (including the bootstrap m).

src/runtime/sys_darwin.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,13 @@ func madvise(addr unsafe.Pointer, n uintptr, flags int32) {
226226
}
227227
func madvise_trampoline()
228228

229+
//go:nosplit
230+
//go:cgo_unsafe_args
231+
func mlock(addr unsafe.Pointer, n uintptr) {
232+
libcCall(unsafe.Pointer(funcPC(mlock_trampoline)), unsafe.Pointer(&addr))
233+
}
234+
func mlock_trampoline()
235+
229236
//go:nosplit
230237
//go:cgo_unsafe_args
231238
func read(fd int32, p unsafe.Pointer, n int32) int32 {
@@ -465,6 +472,7 @@ func setNonblock(fd int32) {
465472
//go:cgo_import_dynamic libc_mmap mmap "/usr/lib/libSystem.B.dylib"
466473
//go:cgo_import_dynamic libc_munmap munmap "/usr/lib/libSystem.B.dylib"
467474
//go:cgo_import_dynamic libc_madvise madvise "/usr/lib/libSystem.B.dylib"
475+
//go:cgo_import_dynamic libc_mlock mlock "/usr/lib/libSystem.B.dylib"
468476
//go:cgo_import_dynamic libc_error __error "/usr/lib/libSystem.B.dylib"
469477
//go:cgo_import_dynamic libc_usleep usleep "/usr/lib/libSystem.B.dylib"
470478

src/runtime/sys_darwin_amd64.s

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ TEXT runtime·madvise_trampoline(SB), NOSPLIT, $0
105105
POPQ BP
106106
RET
107107

108+
TEXT runtime·mlock_trampoline(SB), NOSPLIT, $0
109+
UNDEF // unimplemented
110+
108111
GLOBL timebase<>(SB),NOPTR,$(machTimebaseInfo__size)
109112

110113
TEXT runtime·nanotime_trampoline(SB),NOSPLIT,$0

src/runtime/sys_darwin_arm64.s

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ TEXT runtime·madvise_trampoline(SB),NOSPLIT,$0
120120
BL libc_madvise(SB)
121121
RET
122122

123+
TEXT runtime·mlock_trampoline(SB),NOSPLIT,$0
124+
MOVD 8(R0), R1 // arg 2 len
125+
MOVD 0(R0), R0 // arg 1 addr
126+
BL libc_mlock(SB)
127+
RET
128+
123129
TEXT runtime·setitimer_trampoline(SB),NOSPLIT,$0
124130
MOVD 8(R0), R1 // arg 2 new
125131
MOVD 16(R0), R2 // arg 3 old

0 commit comments

Comments
 (0)