From f07cbc7f88e5e15e41ec8b9c2b850d2179e0834e Mon Sep 17 00:00:00 2001 From: Cherry Zhang Date: Thu, 31 Oct 2019 10:32:31 -0400 Subject: runtime: don't fetch G from signal stack when using cgo When using cgo, we save G to TLS, and when a signal happens, we load G from TLS in sigtramp. This should give us a valid G. Don't try to fetch from the signal stack. In particular, C code may change the signal stack or call our signal handler directly (e.g. TSAN), so we are not necessarily running on the original gsignal stack where we saved G. Also skip saving G on the signal stack when using cgo. Updates #35249. Change-Id: I40749ce6682709bd4ebfdfd9f23bd0f317fc197d Reviewed-on: https://go-review.googlesource.com/c/go/+/204519 Reviewed-by: Ian Lance Taylor --- src/runtime/sys_linux_arm.s | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'src/runtime/sys_linux_arm.s') diff --git a/src/runtime/sys_linux_arm.s b/src/runtime/sys_linux_arm.s index 9ef8c9258b..b8dc202d4c 100644 --- a/src/runtime/sys_linux_arm.s +++ b/src/runtime/sys_linux_arm.s @@ -277,21 +277,28 @@ noswitch: // during VDSO code we can find the g. // If we don't have a signal stack, we won't receive signal, // so don't bother saving g. + // When using cgo, we already saved g on TLS, also don't save + // g here. + MOVB runtime·iscgo(SB), R6 + CMP $0, R6 + BNE nosaveg MOVW m_gsignal(R5), R6 // g.m.gsignal CMP $0, R6 - BEQ 3(PC) + BEQ nosaveg MOVW (g_stack+stack_lo)(R6), R6 // g.m.gsignal.stack.lo MOVW g, (R6) BL (R11) - CMP $0, R6 // R6 is unchanged by C code - BEQ 3(PC) MOVW $0, R1 - MOVW R1, (R6) // clear g slot + MOVW R1, (R6) // clear g slot, R6 is unchanged by C code JMP finish +nosaveg: + BL (R11) + JMP finish + fallback: MOVW $SYS_clock_gettime, R7 SWI $0 @@ -344,21 +351,28 @@ noswitch: // during VDSO code we can find the g. // If we don't have a signal stack, we won't receive signal, // so don't bother saving g. + // When using cgo, we already saved g on TLS, also don't save + // g here. + MOVB runtime·iscgo(SB), R6 + CMP $0, R6 + BNE nosaveg MOVW m_gsignal(R5), R6 // g.m.gsignal CMP $0, R6 - BEQ 3(PC) + BEQ nosaveg MOVW (g_stack+stack_lo)(R6), R6 // g.m.gsignal.stack.lo MOVW g, (R6) BL (R11) - CMP $0, R6 // R6 is unchanged by C code - BEQ 3(PC) MOVW $0, R1 - MOVW R1, (R6) // clear g slot + MOVW R1, (R6) // clear g slot, R6 is unchanged by C code JMP finish +nosaveg: + BL (R11) + JMP finish + fallback: MOVW $SYS_clock_gettime, R7 SWI $0 -- cgit v1.3