]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: when dying from a signal use the previous signal handler
authorElias Naur <elias.naur@gmail.com>
Tue, 18 Jul 2017 12:17:57 +0000 (14:17 +0200)
committerElias Naur <elias.naur@gmail.com>
Thu, 10 Aug 2017 10:08:17 +0000 (10:08 +0000)
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 <elias.naur@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/runtime/crash_cgo_test.go
src/runtime/signal_unix.go
src/runtime/testdata/testprogcgo/catchpanic.go [new file with mode: 0644]

index a5cbbad69bbfd66e0621e54e2a3e6eca86154eb4..2869ac768723e6e1b6ab8efd4d62a1637cf78e18 100644 (file)
@@ -411,3 +411,31 @@ func TestCgoNumGoroutine(t *testing.T) {
                t.Errorf("expected %q got %v", want, got)
        }
 }
+
+func TestCatchPanic(t *testing.T) {
+       t.Parallel()
+       switch runtime.GOOS {
+       case "plan9", "windows":
+               t.Skipf("no signals on %s", runtime.GOOS)
+       case "darwin":
+               if runtime.GOARCH == "amd64" {
+                       t.Skipf("crash() on darwin/amd64 doesn't raise SIGABRT")
+               }
+       }
+
+       testenv.MustHaveGoRun(t)
+
+       exe, err := buildTestProg(t, "testprogcgo")
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       cmd := testEnv(exec.Command(exe, "CgoCatchPanic"))
+       // Make sure a panic results in a crash.
+       cmd.Env = append(cmd.Env, "GOTRACEBACK=crash")
+       // Tell testprogcgo to install an early signal handler for SIGABRT
+       cmd.Env = append(cmd.Env, "CGOCATCHPANIC_INSTALL_HANDLER=1")
+       if out, err := cmd.CombinedOutput(); err != nil {
+               t.Errorf("testprogcgo CgoCatchPanic failed: %v\n%s", err, out)
+       }
+}
index d9a18caa6f66c769b6f039bc04f4a94db2ad84ca..e087e145aa1cbc43fe30d22dd65a4b4cdc68fa8c 100644 (file)
@@ -394,8 +394,11 @@ func sigpanic() {
 //go:nosplit
 //go:nowritebarrierrec
 func dieFromSignal(sig uint32) {
-       setsig(sig, _SIG_DFL)
        unblocksig(sig)
+       // First, try any signal handler installed before the runtime
+       // initialized.
+       fn := atomic.Loaduintptr(&fwdSig[sig])
+       setsig(sig, fn)
        raise(sig)
 
        // That should have killed us. On some systems, though, raise
@@ -407,6 +410,14 @@ func dieFromSignal(sig uint32) {
        osyield()
        osyield()
 
+       // If that didn't work, try _SIG_DFL.
+       setsig(sig, _SIG_DFL)
+       raise(sig)
+
+       osyield()
+       osyield()
+       osyield()
+
        // If we are still somehow running, just exit with the wrong status.
        exit(2)
 }
diff --git a/src/runtime/testdata/testprogcgo/catchpanic.go b/src/runtime/testdata/testprogcgo/catchpanic.go
new file mode 100644 (file)
index 0000000..f03b6d3
--- /dev/null
@@ -0,0 +1,39 @@
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build !plan9,!windows
+
+package main
+
+/*
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+
+static void abrthandler(int signum) {
+       if (signum == SIGABRT) {
+               exit(0);  // success
+       }
+}
+
+static void __attribute__ ((constructor)) sigsetup(void) {
+       struct sigaction act;
+
+       if (getenv("CGOCATCHPANIC_INSTALL_HANDLER") == NULL)
+               return;
+       memset(&act, 0, sizeof act);
+       act.sa_handler = abrthandler;
+       sigaction(SIGABRT, &act, NULL);
+}
+*/
+import "C"
+
+func init() {
+       register("CgoCatchPanic", CgoCatchPanic)
+}
+
+// Test that the SIGABRT raised by panic can be caught by an early signal handler.
+func CgoCatchPanic() {
+       panic("catch me")
+}