Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 28e549d

Browse files
committedOct 6, 2020
runtime: use sigaltstack on macOS/ARM64
Currently we don't use sigaltstack on darwin/arm64, as is not supported on iOS. However, it is supported on macOS. Use it. (iOS remains unchanged.) Change-Id: Icc154c5e2edf2dbdc8ca68741ad9157fc15a72ee Reviewed-on: https://go-review.googlesource.com/c/go/+/256917 Trust: Cherry Zhang <cherryyz@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
1 parent 04c7e32 commit 28e549d

File tree

7 files changed

+27
-19
lines changed

7 files changed

+27
-19
lines changed
 

‎misc/cgo/test/sigaltstack.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import (
6262

6363
func testSigaltstack(t *testing.T) {
6464
switch {
65-
case runtime.GOOS == "solaris", runtime.GOOS == "illumos", (runtime.GOOS == "darwin" || runtime.GOOS == "ios") && runtime.GOARCH == "arm64":
65+
case runtime.GOOS == "solaris", runtime.GOOS == "illumos", runtime.GOOS == "ios" && runtime.GOARCH == "arm64":
6666
t.Skipf("switching signal stack not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
6767
}
6868

‎src/cmd/internal/obj/arm64/obj7.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ func preprocess(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
589589
q1.To.Reg = REGSP
590590
q1.Spadj = c.autosize
591591

592-
if c.ctxt.Headtype == objabi.Hdarwin {
592+
if objabi.GOOS == "ios" {
593593
// iOS does not support SA_ONSTACK. We will run the signal handler
594594
// on the G stack. If we write below SP, it may be clobbered by
595595
// the signal handler. So we save LR after decrementing SP.

‎src/runtime/mkpreempt.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,9 @@ func genARM64() {
340340
p("MOVD R29, -8(RSP)") // save frame pointer (only used on Linux)
341341
p("SUB $8, RSP, R29") // set up new frame pointer
342342
p("#endif")
343-
// On darwin, save the LR again after decrementing SP. We run the
344-
// signal handler on the G stack (as it doesn't support SA_ONSTACK),
343+
// On iOS, save the LR again after decrementing SP. We run the
344+
// signal handler on the G stack (as it doesn't support sigaltstack),
345345
// so any writes below SP may be clobbered.
346-
p("#ifdef GOOS_darwin")
347-
p("MOVD R30, (RSP)")
348-
p("#endif")
349346
p("#ifdef GOOS_ios")
350347
p("MOVD R30, (RSP)")
351348
p("#endif")

‎src/runtime/os_darwin.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ func mpreinit(mp *m) {
289289
// Called to initialize a new m (including the bootstrap m).
290290
// Called on the new thread, cannot allocate memory.
291291
func minit() {
292-
// The alternate signal stack is buggy on arm64.
292+
// iOS does not support alternate signal stack.
293293
// The signal handler handles it directly.
294-
if GOARCH != "arm64" {
294+
if !(GOOS == "ios" && GOARCH == "arm64") {
295295
minitSignalStack()
296296
}
297297
minitSignalMask()
@@ -301,9 +301,9 @@ func minit() {
301301
// Called from dropm to undo the effect of an minit.
302302
//go:nosplit
303303
func unminit() {
304-
// The alternate signal stack is buggy on arm64.
304+
// iOS does not support alternate signal stack.
305305
// See minit.
306-
if GOARCH != "arm64" {
306+
if !(GOOS == "ios" && GOARCH == "arm64") {
307307
unminitSignals()
308308
}
309309
}

‎src/runtime/preempt_arm64.s

-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ TEXT ·asyncPreempt(SB),NOSPLIT|NOFRAME,$0-0
1010
MOVD R29, -8(RSP)
1111
SUB $8, RSP, R29
1212
#endif
13-
#ifdef GOOS_darwin
14-
MOVD R30, (RSP)
15-
#endif
1613
#ifdef GOOS_ios
1714
MOVD R30, (RSP)
1815
#endif

‎src/runtime/stack.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const (
6666
// to each stack below the usual guard area for OS-specific
6767
// purposes like signal handling. Used on Windows, Plan 9,
6868
// and iOS because they do not use a separate stack.
69-
_StackSystem = sys.GoosWindows*512*sys.PtrSize + sys.GoosPlan9*512 + (sys.GoosDarwin+sys.GoosIos)*sys.GoarchArm64*1024
69+
_StackSystem = sys.GoosWindows*512*sys.PtrSize + sys.GoosPlan9*512 + sys.GoosIos*sys.GoarchArm64*1024
7070

7171
// The minimum size of stack used by Go code
7272
_StackMin = 2048

‎src/runtime/sys_darwin_arm64.s

+18-4
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ TEXT runtime·sigtramp(SB),NOSPLIT,$192
202202
BEQ 2(PC)
203203
BL runtime·load_g(SB)
204204

205+
#ifdef GOOS_ios
205206
MOVD RSP, R6
206207
CMP $0, g
207208
BEQ nog
@@ -226,16 +227,21 @@ nog:
226227
// Switch to gsignal stack.
227228
MOVD R6, RSP
228229

229-
// Call sigtrampgo.
230+
// Save arguments.
230231
MOVW R0, (8*1)(RSP)
231232
MOVD R1, (8*2)(RSP)
232233
MOVD R2, (8*3)(RSP)
234+
#endif
235+
236+
// Call sigtrampgo.
233237
MOVD $runtime·sigtrampgo(SB), R11
234238
BL (R11)
235239

240+
#ifdef GOOS_ios
236241
// Switch to old stack.
237242
MOVD (8*4)(RSP), R5
238243
MOVD R5, RSP
244+
#endif
239245

240246
// Restore callee-save registers.
241247
MOVD (8*4)(RSP), R19
@@ -329,12 +335,20 @@ TEXT runtime·fcntl_trampoline(SB),NOSPLIT,$0
329335
ADD $16, RSP
330336
RET
331337

332-
// sigaltstack on iOS is not supported and will always
333-
// run the signal handler on the main stack, so our sigtramp has
334-
// to do the stack switch ourselves.
335338
TEXT runtime·sigaltstack_trampoline(SB),NOSPLIT,$0
339+
#ifdef GOOS_ios
340+
// sigaltstack on iOS is not supported and will always
341+
// run the signal handler on the main stack, so our sigtramp has
342+
// to do the stack switch ourselves.
336343
MOVW $43, R0
337344
BL libc_exit(SB)
345+
#else
346+
MOVD 8(R0), R1 // arg 2 old
347+
MOVD 0(R0), R0 // arg 1 new
348+
CALL libc_sigaltstack(SB)
349+
CBZ R0, 2(PC)
350+
BL notok<>(SB)
351+
#endif
338352
RET
339353

340354
// Thread related functions

0 commit comments

Comments
 (0)
Failed to load comments.