From fbdfa99246ecbb04954a042a5809c4748415574d Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Wed, 16 Dec 2015 12:16:17 -0800 Subject: runtime: for c-archive/c-shared, don't install unnecessary signal handlers Only install signal handlers for synchronous signals that become run-time panics. Set the SA_ONSTACK flag for other signal handlers as needed. Fixes #13028. Update #12465. Update #13034. Update #13042. Change-Id: I28375e70641f60630e10f3c86e24b6e4f8a35cc9 Reviewed-on: https://go-review.googlesource.com/17903 Reviewed-by: Russ Cox Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- src/runtime/signal1_unix.go | 8 ++++++++ src/runtime/signal2_unix.go | 11 +++++++++-- src/runtime/sys_darwin_amd64.s | 6 +++++- src/runtime/sys_linux_arm.s | 4 ++++ 4 files changed, 26 insertions(+), 3 deletions(-) (limited to 'src/runtime') diff --git a/src/runtime/signal1_unix.go b/src/runtime/signal1_unix.go index 405713b371..2a325bb9fc 100644 --- a/src/runtime/signal1_unix.go +++ b/src/runtime/signal1_unix.go @@ -65,6 +65,14 @@ func initsig() { continue } + // When built using c-archive or c-shared, only + // install signal handlers for synchronous signals. + // Set SA_ONSTACK for other signals if necessary. + if (isarchive || islibrary) && t.flags&_SigPanic == 0 { + setsigstack(i) + continue + } + t.flags |= _SigHandling setsig(i, funcPC(sighandler), true) } diff --git a/src/runtime/signal2_unix.go b/src/runtime/signal2_unix.go index 8b0bd42206..0633a51900 100644 --- a/src/runtime/signal2_unix.go +++ b/src/runtime/signal2_unix.go @@ -16,8 +16,6 @@ func sigfwd(fn uintptr, sig uint32, info *siginfo, ctx unsafe.Pointer) // signal was forwarded. //go:nosplit func sigfwdgo(sig uint32, info *siginfo, ctx unsafe.Pointer) bool { - g := getg() - c := &sigctxt{info, ctx} if sig >= uint32(len(sigtable)) { return false } @@ -28,13 +26,22 @@ func sigfwdgo(sig uint32, info *siginfo, ctx unsafe.Pointer) bool { if fwdFn == _SIG_DFL { return false } + + // If we aren't handling the signal, forward it. + if flags&_SigHandling == 0 { + sigfwd(fwdFn, sig, info, ctx) + return true + } + // Only forward synchronous signals. + c := &sigctxt{info, ctx} if c.sigcode() == _SI_USER || flags&_SigPanic == 0 { return false } // Determine if the signal occurred inside Go code. We test that: // (1) we were in a goroutine (i.e., m.curg != nil), and // (2) we weren't in CGO (i.e., m.curg.syscallsp == 0). + g := getg() if g != nil && g.m != nil && g.m.curg != nil && g.m.curg.syscallsp == 0 { return false } diff --git a/src/runtime/sys_darwin_amd64.s b/src/runtime/sys_darwin_amd64.s index 692dbca580..7b9cf6a3bc 100644 --- a/src/runtime/sys_darwin_amd64.s +++ b/src/runtime/sys_darwin_amd64.s @@ -214,10 +214,14 @@ TEXT runtime·sigaction(SB),NOSPLIT,$0-24 TEXT runtime·sigfwd(SB),NOSPLIT,$0-32 MOVQ fn+0(FP), AX - MOVQ sig+8(FP), DI + MOVL sig+8(FP), DI MOVQ info+16(FP), SI MOVQ ctx+24(FP), DX + MOVQ SP, BP + SUBQ $64, SP + ANDQ $~15, SP // alignment for x86_64 ABI CALL AX + MOVQ BP, SP RET TEXT runtime·sigreturn(SB),NOSPLIT,$0-12 diff --git a/src/runtime/sys_linux_arm.s b/src/runtime/sys_linux_arm.s index 216781ef7a..6a3b924330 100644 --- a/src/runtime/sys_linux_arm.s +++ b/src/runtime/sys_linux_arm.s @@ -339,7 +339,11 @@ TEXT runtime·sigfwd(SB),NOSPLIT,$0-16 MOVW info+8(FP), R1 MOVW ctx+12(FP), R2 MOVW fn+0(FP), R11 + MOVW R13, R4 + SUB $24, R13 + BIC $0x7, R13 // alignment for ELF ABI BL (R11) + MOVW R4, R13 RET TEXT runtime·sigtramp(SB),NOSPLIT,$12 -- cgit v1.3-5-g45d5