Skip to content

Commit 1f60517

Browse files
Bryan C. Millsianlancetaylor
Bryan C. Mills
authored andcommitted
runtime/cgo: use libc for sigaction syscalls when possible
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. Fixes #17753. Change-Id: I9798bb50291a4b8fa20caa39c02a4465ec40bb8d Reviewed-on: https://go-review.googlesource.com/33142 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
1 parent c69233b commit 1f60517

File tree

10 files changed

+268
-6
lines changed

10 files changed

+268
-6
lines changed

misc/cgo/testsanitizers/test.bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ if test "$tsan" = "yes"; then
144144
testtsan tsan2.go
145145
testtsan tsan3.go
146146
testtsan tsan4.go
147+
testtsan tsan8.go
147148

148149
# These tests are only reliable using clang or GCC version 7 or later.
149150
# Otherwise runtime/cgo/libcgo.h can't tell whether TSAN is in use.

misc/cgo/testsanitizers/tsan8.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
// This program failed when run under the C/C++ ThreadSanitizer. The TSAN
8+
// sigaction function interceptor returned SIG_DFL instead of the Go runtime's
9+
// handler in registerSegvForwarder.
10+
11+
/*
12+
#cgo CFLAGS: -fsanitize=thread
13+
#cgo LDFLAGS: -fsanitize=thread
14+
15+
#include <signal.h>
16+
#include <stdio.h>
17+
#include <stdlib.h>
18+
#include <string.h>
19+
20+
struct sigaction prev_sa;
21+
22+
void forwardSignal(int signo, siginfo_t *info, void *context) {
23+
// One of sa_sigaction and/or sa_handler
24+
if ((prev_sa.sa_flags&SA_SIGINFO) != 0) {
25+
prev_sa.sa_sigaction(signo, info, context);
26+
return;
27+
}
28+
if (prev_sa.sa_handler != SIG_IGN && prev_sa.sa_handler != SIG_DFL) {
29+
prev_sa.sa_handler(signo);
30+
return;
31+
}
32+
33+
fprintf(stderr, "No Go handler to forward to!\n");
34+
abort();
35+
}
36+
37+
void registerSegvFowarder() {
38+
struct sigaction sa;
39+
memset(&sa, 0, sizeof(sa));
40+
sigemptyset(&sa.sa_mask);
41+
sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
42+
sa.sa_sigaction = forwardSignal;
43+
44+
if (sigaction(SIGSEGV, &sa, &prev_sa) != 0) {
45+
perror("failed to register SEGV forwarder");
46+
exit(EXIT_FAILURE);
47+
}
48+
}
49+
*/
50+
import "C"
51+
52+
func main() {
53+
C.registerSegvFowarder()
54+
55+
defer func() {
56+
recover()
57+
}()
58+
var nilp *int
59+
*nilp = 42
60+
}

src/runtime/cgo/gcc_sigaction.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build linux,amd64
6+
7+
#include <errno.h>
8+
#include <stddef.h>
9+
#include <stdint.h>
10+
#include <signal.h>
11+
12+
// go_sigaction_t is a C version of the sigactiont struct from
13+
// defs_linux_amd64.go. This definition — and its conversion to and from struct
14+
// sigaction — are specific to linux/amd64.
15+
typedef struct {
16+
uintptr_t handler;
17+
uint64_t flags;
18+
uintptr_t restorer;
19+
uint64_t mask;
20+
} go_sigaction_t;
21+
22+
int32_t
23+
x_cgo_sigaction(intptr_t signum, const go_sigaction_t *goact, go_sigaction_t *oldgoact) {
24+
int32_t ret;
25+
struct sigaction act;
26+
struct sigaction oldact;
27+
int i;
28+
29+
if (goact) {
30+
if (goact->flags & SA_SIGINFO) {
31+
act.sa_sigaction = (void(*)(int, siginfo_t*, void*))(goact->handler);
32+
} else {
33+
act.sa_handler = (void(*)(int))(goact->handler);
34+
}
35+
sigemptyset(&act.sa_mask);
36+
for (i = 0; i < 8 * sizeof(goact->mask); i++) {
37+
if (goact->mask & ((uint64_t)(1)<<i)) {
38+
sigaddset(&act.sa_mask, i+1);
39+
}
40+
}
41+
act.sa_flags = goact->flags;
42+
}
43+
44+
ret = sigaction(signum, goact ? &act : NULL, oldgoact ? &oldact : NULL);
45+
if (ret == -1) {
46+
/* This is what the Go code expects on failure. */
47+
return errno;
48+
}
49+
50+
if (oldgoact) {
51+
if (oldact.sa_flags & SA_SIGINFO) {
52+
oldgoact->handler = (uintptr_t)(oldact.sa_sigaction);
53+
} else {
54+
oldgoact->handler = (uintptr_t)(oldact.sa_handler);
55+
}
56+
oldgoact->mask = 0;
57+
for (i = 0; i < 8 * sizeof(oldgoact->mask); i++) {
58+
if (sigismember(&act.sa_mask, i+1) == 1) {
59+
oldgoact->mask |= (uint64_t)(1)<<i;
60+
}
61+
}
62+
oldgoact->flags = act.sa_flags;
63+
}
64+
65+
return ret;
66+
}

src/runtime/cgo/sigaction.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build linux,amd64
6+
7+
package cgo
8+
9+
// Import "unsafe" because we use go:linkname.
10+
import _ "unsafe"
11+
12+
// When using cgo, call the C library for sigaction, so that we call into
13+
// any sanitizer interceptors. This supports using the memory
14+
// sanitizer with Go programs. The memory sanitizer only applies to
15+
// C/C++ code; this permits that code to see the Go runtime's existing signal
16+
// handlers when registering new signal handlers for the process.
17+
18+
//go:cgo_import_static x_cgo_sigaction
19+
//go:linkname x_cgo_sigaction x_cgo_sigaction
20+
//go:linkname _cgo_sigaction _cgo_sigaction
21+
var x_cgo_sigaction byte
22+
var _cgo_sigaction = &x_cgo_sigaction

src/runtime/cgo_mmap.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) uns
3535
// sysMmap calls the mmap system call. It is implemented in assembly.
3636
func sysMmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) unsafe.Pointer
3737

38-
// cgoMmap calls the mmap function in the runtime/cgo package on the
3938
// callCgoMmap calls the mmap function in the runtime/cgo package
4039
// using the GCC calling convention. It is implemented in assembly.
4140
func callCgoMmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) uintptr

src/runtime/cgo_sigaction.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Support for memory sanitizer. See runtime/cgo/sigaction.go.
6+
7+
// +build linux,amd64
8+
9+
package runtime
10+
11+
import "unsafe"
12+
13+
// _cgo_sigaction is filled in by runtime/cgo when it is linked into the
14+
// program, so it is only non-nil when using cgo.
15+
//go:linkname _cgo_sigaction _cgo_sigaction
16+
var _cgo_sigaction unsafe.Pointer
17+
18+
//go:nosplit
19+
//go:nowritebarrierrec
20+
func rt_sigaction(sig uintptr, new, old *sigactiont, size uintptr) int32 {
21+
// The runtime package is explicitly blacklisted from sanitizer
22+
// instrumentation in racewalk.go, but we might be calling into instrumented C
23+
// functions here — so we need the pointer parameters to be properly marked.
24+
//
25+
// Mark the input as having been written before the call and the output as
26+
// read after.
27+
if msanenabled && new != nil {
28+
msanwrite(unsafe.Pointer(new), unsafe.Sizeof(*new))
29+
}
30+
31+
var ret int32
32+
33+
if _cgo_sigaction == nil {
34+
ret = sysSigaction(sig, new, old, size)
35+
} else {
36+
// We need to call _cgo_sigaction, which means we need a big enough stack
37+
// for C. To complicate matters, we may be in libpreinit (before the
38+
// runtime has been initialized) or in an asynchronous signal handler (with
39+
// the current thread in transition between goroutines, or with the g0
40+
// system stack already in use).
41+
42+
g := getg()
43+
sp := uintptr(unsafe.Pointer(&sig))
44+
switch {
45+
case g == nil:
46+
// No g: we're on a C stack or a signal stack.
47+
ret = callCgoSigaction(sig, new, old)
48+
case sp < g.stack.lo || sp >= g.stack.hi:
49+
// We're no longer on g's stack, so we must be handling a signal. It's
50+
// possible that we interrupted the thread during a transition between g
51+
// and g0, so we should stay on the current stack to avoid corrupting g0.
52+
ret = callCgoSigaction(sig, new, old)
53+
default:
54+
// We're running on g's stack, so either we're not in a signal handler or
55+
// the signal handler has set the correct g. If we're on gsignal or g0,
56+
// systemstack will make the call directly; otherwise, it will switch to
57+
// g0 to ensure we have enough room to call a libc function.
58+
//
59+
// The function literal that we pass to systemstack is not nosplit, but
60+
// that's ok: we'll be running on a fresh, clean system stack so the stack
61+
// check will always succeed anyway.
62+
systemstack(func() {
63+
ret = callCgoSigaction(sig, new, old)
64+
})
65+
}
66+
67+
const EINVAL = 22
68+
if ret == EINVAL {
69+
// libc reserves certain signals — normally 32-33 — for pthreads, and
70+
// returns EINVAL for sigaction calls on those signals. If we get EINVAL,
71+
// fall back to making the syscall directly.
72+
ret = sysSigaction(sig, new, old, size)
73+
}
74+
}
75+
76+
if msanenabled && old != nil && ret == 0 {
77+
msanread(unsafe.Pointer(old), unsafe.Sizeof(*old))
78+
}
79+
return ret
80+
}
81+
82+
// sysSigaction calls the rt_sigaction system call. It is implemented in assembly.
83+
//go:noescape
84+
func sysSigaction(sig uintptr, new, old *sigactiont, size uintptr) int32
85+
86+
// callCgoSigaction calls the sigaction function in the runtime/cgo package
87+
// using the GCC calling convention. It is implemented in assembly.
88+
//go:noescape
89+
func callCgoSigaction(sig uintptr, new, old *sigactiont) int32

src/runtime/msan_amd64.s

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,16 @@ TEXT runtime·msanfree(SB), NOSPLIT, $0-16
6262
TEXT msancall<>(SB), NOSPLIT, $0-0
6363
get_tls(R12)
6464
MOVQ g(R12), R14
65+
MOVQ SP, R12 // callee-saved, preserved across the CALL
66+
CMPQ R14, $0
67+
JE call // no g; still on a system stack
68+
6569
MOVQ g_m(R14), R13
6670
// Switch to g0 stack.
67-
MOVQ SP, R12 // callee-saved, preserved across the CALL
6871
MOVQ m_g0(R13), R10
6972
CMPQ R10, R14
7073
JE call // already on g0
74+
7175
MOVQ (g_sched+gobuf_sp)(R10), SP
7276
call:
7377
ANDQ $~15, SP // alignment for gcc ABI

src/runtime/os_linux.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,6 @@ func sigreturn()
311311
func sigtramp(sig uint32, info *siginfo, ctx unsafe.Pointer)
312312
func cgoSigtramp()
313313

314-
//go:noescape
315-
func rt_sigaction(sig uintptr, new, old *sigactiont, size uintptr) int32
316-
317314
//go:noescape
318315
func sigaltstack(new, old *stackt)
319316

src/runtime/sigaction_linux.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !amd64
6+
7+
package runtime
8+
9+
// rt_sigaction calls the rt_sigaction system call. It is implemented in assembly.
10+
//go:noescape
11+
func rt_sigaction(sig uintptr, new, old *sigactiont, size uintptr) int32

src/runtime/sys_linux_amd64.s

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ TEXT runtime·rtsigprocmask(SB),NOSPLIT,$0-28
208208
MOVL $0xf1, 0xf1 // crash
209209
RET
210210

211-
TEXT runtime·rt_sigaction(SB),NOSPLIT,$0-36
211+
TEXT runtime·sysSigaction(SB),NOSPLIT,$0-36
212212
MOVQ sig+0(FP), DI
213213
MOVQ new+8(FP), SI
214214
MOVQ old+16(FP), DX
@@ -218,6 +218,19 @@ TEXT runtime·rt_sigaction(SB),NOSPLIT,$0-36
218218
MOVL AX, ret+32(FP)
219219
RET
220220

221+
// Call the function stored in _cgo_sigaction using the GCC calling convention.
222+
TEXT runtime·callCgoSigaction(SB),NOSPLIT,$16
223+
MOVQ sig+0(FP), DI
224+
MOVQ new+8(FP), SI
225+
MOVQ old+16(FP), DX
226+
MOVQ _cgo_sigaction(SB), AX
227+
MOVQ SP, BX // callee-saved
228+
ANDQ $~15, SP // alignment as per amd64 psABI
229+
CALL AX
230+
MOVQ BX, SP
231+
MOVL AX, ret+24(FP)
232+
RET
233+
221234
TEXT runtime·sigfwd(SB),NOSPLIT,$0-32
222235
MOVQ fn+0(FP), AX
223236
MOVL sig+8(FP), DI

0 commit comments

Comments
 (0)