From 180795ceac69b57b9a13f51c87f0be9bf9af9050 Mon Sep 17 00:00:00 2001 From: limeidan Date: Wed, 5 Mar 2025 15:07:07 +0800 Subject: [PATCH] runtime, runtime/cgo: use libc for sigaction syscalls when cgo is enabled on loong64 This ensures that runtime's signal handlers pass through the TSAN and MSAN libc interceptors and subsequent calls to the intercepted sigaction function from C will correctly see them. Change-Id: I243a70d9dcb6d95a65c8494d5f9f9f09a316c693 Reviewed-on: https://go-review.googlesource.com/c/go/+/654995 Reviewed-by: Ian Lance Taylor Reviewed-by: Cherry Mui Reviewed-by: abner chenc LUCI-TryBot-Result: Go LUCI --- src/runtime/cgo/gcc_sigaction.c | 11 ++++++++--- src/runtime/cgo/sigaction.go | 2 +- src/runtime/cgo_sigaction.go | 2 +- src/runtime/sigaction.go | 2 +- src/runtime/sys_linux_loong64.s | 10 ++++++++++ 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/runtime/cgo/gcc_sigaction.c b/src/runtime/cgo/gcc_sigaction.c index 374909bf97..7cbef7db11 100644 --- a/src/runtime/cgo/gcc_sigaction.c +++ b/src/runtime/cgo/gcc_sigaction.c @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build linux && (amd64 || arm64 || ppc64le) +//go:build linux && (amd64 || arm64 || loong64 || ppc64le) #include #include @@ -13,13 +13,18 @@ #include "libcgo.h" // go_sigaction_t is a C version of the sigactiont struct from -// defs_linux_amd64.go. This definition — and its conversion to and from struct -// sigaction — are specific to linux/amd64. +// defs_${goos}_${goarch}.go. This definition — and its conversion +// to and from struct sigaction — are specific to ${goos}/${goarch}. typedef struct { uintptr_t handler; uint64_t flags; +#ifdef __loongarch__ + uint64_t mask; + uintptr_t restorer; +#else uintptr_t restorer; uint64_t mask; +#endif } go_sigaction_t; // SA_RESTORER is part of the kernel interface. diff --git a/src/runtime/cgo/sigaction.go b/src/runtime/cgo/sigaction.go index dc714f7ef4..dc3f5fd255 100644 --- a/src/runtime/cgo/sigaction.go +++ b/src/runtime/cgo/sigaction.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build (linux && amd64) || (freebsd && amd64) || (linux && arm64) || (linux && ppc64le) +//go:build (linux && (amd64 || arm64 || loong64 || ppc64le)) || (freebsd && amd64) package cgo diff --git a/src/runtime/cgo_sigaction.go b/src/runtime/cgo_sigaction.go index 9500c52205..5c644587f0 100644 --- a/src/runtime/cgo_sigaction.go +++ b/src/runtime/cgo_sigaction.go @@ -4,7 +4,7 @@ // Support for sanitizers. See runtime/cgo/sigaction.go. -//go:build (linux && amd64) || (freebsd && amd64) || (linux && arm64) || (linux && ppc64le) +//go:build (linux && (amd64 || arm64 || loong64 || ppc64le)) || (freebsd && amd64) package runtime diff --git a/src/runtime/sigaction.go b/src/runtime/sigaction.go index 05f44f65db..2027ae80bf 100644 --- a/src/runtime/sigaction.go +++ b/src/runtime/sigaction.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build (linux && !amd64 && !arm64 && !ppc64le) || (freebsd && !amd64) +//go:build (linux && !amd64 && !arm64 && !loong64 && !ppc64le) || (freebsd && !amd64) package runtime diff --git a/src/runtime/sys_linux_loong64.s b/src/runtime/sys_linux_loong64.s index 1121b3c991..84630c16d8 100644 --- a/src/runtime/sys_linux_loong64.s +++ b/src/runtime/sys_linux_loong64.s @@ -378,6 +378,16 @@ TEXT runtime·rt_sigaction(SB),NOSPLIT,$0 SYSCALL RET +// Call the function stored in _cgo_sigaction using the GCC calling convention. +TEXT runtime·callCgoSigaction(SB),NOSPLIT,$0 + // R4: sig, R5: new, R6: old + MOVV _cgo_sigaction(SB), R7 + SUBV $16, R3 // reserve 16 bytes for sp-8 where fp may be saved. + JAL (R7) + ADDV $16, R3 + MOVW R4, R4 + RET + // func sigfwd(fn uintptr, sig uint32, info *siginfo, ctx unsafe.Pointer) TEXT runtime·sigfwd(SB),NOSPLIT,$0 // before: -- 2.51.0