Skip to content

Commit 5500c9c

Browse files
author
Elias Naur
committed
runtime: when dying from a signal use the previous signal handler
Before this CL, whenever the Go runtime wanted to kill its own process with a signal dieFromSignal would reset the signal handler to _SIG_DFL. Unfortunately, if any signal handler were installed before the Go runtime initialized, it wouldn't be invoked either. Instead, use whatever signal handler was installed before initialization. The motivating use case is Crashlytics on Android. Before this CL, Crashlytics would not consider a crash from a panic() since the corresponding SIGABRT never reached its signal handler. Updates #11382 Updates #20392 (perhaps even fixes it) Fixes #19389 Change-Id: I0c8633329433b45cbb3b16571bea227e38e8be2e Reviewed-on: https://go-review.googlesource.com/49590 Run-TryBot: Elias Naur <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 7d80a2e commit 5500c9c

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

src/runtime/crash_cgo_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,31 @@ func TestCgoNumGoroutine(t *testing.T) {
411411
t.Errorf("expected %q got %v", want, got)
412412
}
413413
}
414+
415+
func TestCatchPanic(t *testing.T) {
416+
t.Parallel()
417+
switch runtime.GOOS {
418+
case "plan9", "windows":
419+
t.Skipf("no signals on %s", runtime.GOOS)
420+
case "darwin":
421+
if runtime.GOARCH == "amd64" {
422+
t.Skipf("crash() on darwin/amd64 doesn't raise SIGABRT")
423+
}
424+
}
425+
426+
testenv.MustHaveGoRun(t)
427+
428+
exe, err := buildTestProg(t, "testprogcgo")
429+
if err != nil {
430+
t.Fatal(err)
431+
}
432+
433+
cmd := testEnv(exec.Command(exe, "CgoCatchPanic"))
434+
// Make sure a panic results in a crash.
435+
cmd.Env = append(cmd.Env, "GOTRACEBACK=crash")
436+
// Tell testprogcgo to install an early signal handler for SIGABRT
437+
cmd.Env = append(cmd.Env, "CGOCATCHPANIC_INSTALL_HANDLER=1")
438+
if out, err := cmd.CombinedOutput(); err != nil {
439+
t.Errorf("testprogcgo CgoCatchPanic failed: %v\n%s", err, out)
440+
}
441+
}

src/runtime/signal_unix.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,11 @@ func sigpanic() {
394394
//go:nosplit
395395
//go:nowritebarrierrec
396396
func dieFromSignal(sig uint32) {
397-
setsig(sig, _SIG_DFL)
398397
unblocksig(sig)
398+
// First, try any signal handler installed before the runtime
399+
// initialized.
400+
fn := atomic.Loaduintptr(&fwdSig[sig])
401+
setsig(sig, fn)
399402
raise(sig)
400403

401404
// That should have killed us. On some systems, though, raise
@@ -407,6 +410,14 @@ func dieFromSignal(sig uint32) {
407410
osyield()
408411
osyield()
409412

413+
// If that didn't work, try _SIG_DFL.
414+
setsig(sig, _SIG_DFL)
415+
raise(sig)
416+
417+
osyield()
418+
osyield()
419+
osyield()
420+
410421
// If we are still somehow running, just exit with the wrong status.
411422
exit(2)
412423
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2017 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 !plan9,!windows
6+
7+
package main
8+
9+
/*
10+
#include <signal.h>
11+
#include <stdlib.h>
12+
#include <string.h>
13+
14+
static void abrthandler(int signum) {
15+
if (signum == SIGABRT) {
16+
exit(0); // success
17+
}
18+
}
19+
20+
static void __attribute__ ((constructor)) sigsetup(void) {
21+
struct sigaction act;
22+
23+
if (getenv("CGOCATCHPANIC_INSTALL_HANDLER") == NULL)
24+
return;
25+
memset(&act, 0, sizeof act);
26+
act.sa_handler = abrthandler;
27+
sigaction(SIGABRT, &act, NULL);
28+
}
29+
*/
30+
import "C"
31+
32+
func init() {
33+
register("CgoCatchPanic", CgoCatchPanic)
34+
}
35+
36+
// Test that the SIGABRT raised by panic can be caught by an early signal handler.
37+
func CgoCatchPanic() {
38+
panic("catch me")
39+
}

0 commit comments

Comments
 (0)