aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/crash_test.go
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2018-06-28 16:45:28 -0700
committerIan Lance Taylor <iant@golang.org>2018-06-29 21:29:17 +0000
commitf43aa1df701d7190eeaf301d3a41e1714c516c45 (patch)
tree452f2ee9d5e13b5de4c3a2de36d8dff850cf9488 /src/runtime/crash_test.go
parent955cc07dde70415489fb2096eb575654181e21fe (diff)
downloadgo-f43aa1df701d7190eeaf301d3a41e1714c516c45.tar.gz
go-f43aa1df701d7190eeaf301d3a41e1714c516c45.zip
runtime: throw if the runtime panics with out of bounds index
If the runtime code panics due to a bad index or slice expression, then throw instead of panicing. This will skip calls to recover and dump the entire runtime stack trace. The runtime should never panic due to an out of bounds index, and this will help with debugging if it does. For #24991 Updates #25201 Change-Id: I85a9feded8f0de914ee1558425931853223c0514 Reviewed-on: https://go-review.googlesource.com/121515 Reviewed-by: Austin Clements <austin@google.com>
Diffstat (limited to 'src/runtime/crash_test.go')
-rw-r--r--src/runtime/crash_test.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/runtime/crash_test.go b/src/runtime/crash_test.go
index 843b415006..9f11aea4e9 100644
--- a/src/runtime/crash_test.go
+++ b/src/runtime/crash_test.go
@@ -654,3 +654,33 @@ func TestAbort(t *testing.T) {
}
}
}
+
+// For TestRuntimePanic: test a panic in the runtime package without
+// involving the testing harness.
+func init() {
+ if os.Getenv("GO_TEST_RUNTIME_PANIC") == "1" {
+ defer func() {
+ if r := recover(); r != nil {
+ // We expect to crash, so exit 0
+ // to indicate failure.
+ os.Exit(0)
+ }
+ }()
+ runtime.PanicForTesting(nil, 1)
+ // We expect to crash, so exit 0 to indicate failure.
+ os.Exit(0)
+ }
+}
+
+func TestRuntimePanic(t *testing.T) {
+ testenv.MustHaveExec(t)
+ cmd := exec.Command(os.Args[0], "-test.run=TestRuntimePanic")
+ cmd.Env = append(cmd.Env, "GO_TEST_RUNTIME_PANIC=1")
+ out, err := cmd.CombinedOutput()
+ t.Logf("%s", out)
+ if err == nil {
+ t.Error("child process did not fail")
+ } else if want := "runtime.unexportedPanicForTesting"; !bytes.Contains(out, []byte(want)) {
+ t.Errorf("output did not contain expected string %q", want)
+ }
+}