]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/crash_test.go
Merge branch 'master' into dev.regabi
[gostls13.git] / src / runtime / crash_test.go
1 // Copyright 2012 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 runtime_test
6
7 import (
8         "bytes"
9         "flag"
10         "fmt"
11         "internal/testenv"
12         "os"
13         "os/exec"
14         "path/filepath"
15         "regexp"
16         "runtime"
17         "strconv"
18         "strings"
19         "sync"
20         "testing"
21         "time"
22 )
23
24 var toRemove []string
25
26 func TestMain(m *testing.M) {
27         status := m.Run()
28         for _, file := range toRemove {
29                 os.RemoveAll(file)
30         }
31         os.Exit(status)
32 }
33
34 var testprog struct {
35         sync.Mutex
36         dir    string
37         target map[string]buildexe
38 }
39
40 type buildexe struct {
41         exe string
42         err error
43 }
44
45 func runTestProg(t *testing.T, binary, name string, env ...string) string {
46         if *flagQuick {
47                 t.Skip("-quick")
48         }
49
50         testenv.MustHaveGoBuild(t)
51
52         exe, err := buildTestProg(t, binary)
53         if err != nil {
54                 t.Fatal(err)
55         }
56
57         return runBuiltTestProg(t, exe, name, env...)
58 }
59
60 func runBuiltTestProg(t *testing.T, exe, name string, env ...string) string {
61         if *flagQuick {
62                 t.Skip("-quick")
63         }
64
65         testenv.MustHaveGoBuild(t)
66
67         cmd := testenv.CleanCmdEnv(exec.Command(exe, name))
68         cmd.Env = append(cmd.Env, env...)
69         if testing.Short() {
70                 cmd.Env = append(cmd.Env, "RUNTIME_TEST_SHORT=1")
71         }
72         var b bytes.Buffer
73         cmd.Stdout = &b
74         cmd.Stderr = &b
75         if err := cmd.Start(); err != nil {
76                 t.Fatalf("starting %s %s: %v", exe, name, err)
77         }
78
79         // If the process doesn't complete within 1 minute,
80         // assume it is hanging and kill it to get a stack trace.
81         p := cmd.Process
82         done := make(chan bool)
83         go func() {
84                 scale := 1
85                 // This GOARCH/GOOS test is copied from cmd/dist/test.go.
86                 // TODO(iant): Have cmd/dist update the environment variable.
87                 if runtime.GOARCH == "arm" || runtime.GOOS == "windows" {
88                         scale = 2
89                 }
90                 if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
91                         if sc, err := strconv.Atoi(s); err == nil {
92                                 scale = sc
93                         }
94                 }
95
96                 select {
97                 case <-done:
98                 case <-time.After(time.Duration(scale) * time.Minute):
99                         p.Signal(sigquit)
100                 }
101         }()
102
103         if err := cmd.Wait(); err != nil {
104                 t.Logf("%s %s exit status: %v", exe, name, err)
105         }
106         close(done)
107
108         return b.String()
109 }
110
111 func buildTestProg(t *testing.T, binary string, flags ...string) (string, error) {
112         if *flagQuick {
113                 t.Skip("-quick")
114         }
115
116         testprog.Lock()
117         defer testprog.Unlock()
118         if testprog.dir == "" {
119                 dir, err := os.MkdirTemp("", "go-build")
120                 if err != nil {
121                         t.Fatalf("failed to create temp directory: %v", err)
122                 }
123                 testprog.dir = dir
124                 toRemove = append(toRemove, dir)
125         }
126
127         if testprog.target == nil {
128                 testprog.target = make(map[string]buildexe)
129         }
130         name := binary
131         if len(flags) > 0 {
132                 name += "_" + strings.Join(flags, "_")
133         }
134         target, ok := testprog.target[name]
135         if ok {
136                 return target.exe, target.err
137         }
138
139         exe := filepath.Join(testprog.dir, name+".exe")
140         cmd := exec.Command(testenv.GoToolPath(t), append([]string{"build", "-o", exe}, flags...)...)
141         cmd.Dir = "testdata/" + binary
142         out, err := testenv.CleanCmdEnv(cmd).CombinedOutput()
143         if err != nil {
144                 target.err = fmt.Errorf("building %s %v: %v\n%s", binary, flags, err, out)
145                 testprog.target[name] = target
146                 return "", target.err
147         }
148         target.exe = exe
149         testprog.target[name] = target
150         return exe, nil
151 }
152
153 func TestVDSO(t *testing.T) {
154         t.Parallel()
155         output := runTestProg(t, "testprog", "SignalInVDSO")
156         want := "success\n"
157         if output != want {
158                 t.Fatalf("output:\n%s\n\nwanted:\n%s", output, want)
159         }
160 }
161
162 func testCrashHandler(t *testing.T, cgo bool) {
163         type crashTest struct {
164                 Cgo bool
165         }
166         var output string
167         if cgo {
168                 output = runTestProg(t, "testprogcgo", "Crash")
169         } else {
170                 output = runTestProg(t, "testprog", "Crash")
171         }
172         want := "main: recovered done\nnew-thread: recovered done\nsecond-new-thread: recovered done\nmain-again: recovered done\n"
173         if output != want {
174                 t.Fatalf("output:\n%s\n\nwanted:\n%s", output, want)
175         }
176 }
177
178 func TestCrashHandler(t *testing.T) {
179         testCrashHandler(t, false)
180 }
181
182 func testDeadlock(t *testing.T, name string) {
183         // External linking brings in cgo, causing deadlock detection not working.
184         testenv.MustInternalLink(t)
185
186         output := runTestProg(t, "testprog", name)
187         want := "fatal error: all goroutines are asleep - deadlock!\n"
188         if !strings.HasPrefix(output, want) {
189                 t.Fatalf("output does not start with %q:\n%s", want, output)
190         }
191 }
192
193 func TestSimpleDeadlock(t *testing.T) {
194         testDeadlock(t, "SimpleDeadlock")
195 }
196
197 func TestInitDeadlock(t *testing.T) {
198         testDeadlock(t, "InitDeadlock")
199 }
200
201 func TestLockedDeadlock(t *testing.T) {
202         testDeadlock(t, "LockedDeadlock")
203 }
204
205 func TestLockedDeadlock2(t *testing.T) {
206         testDeadlock(t, "LockedDeadlock2")
207 }
208
209 func TestGoexitDeadlock(t *testing.T) {
210         // External linking brings in cgo, causing deadlock detection not working.
211         testenv.MustInternalLink(t)
212
213         output := runTestProg(t, "testprog", "GoexitDeadlock")
214         want := "no goroutines (main called runtime.Goexit) - deadlock!"
215         if !strings.Contains(output, want) {
216                 t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
217         }
218 }
219
220 func TestStackOverflow(t *testing.T) {
221         output := runTestProg(t, "testprog", "StackOverflow")
222         want := []string{
223                 "runtime: goroutine stack exceeds 1474560-byte limit\n",
224                 "fatal error: stack overflow",
225                 // information about the current SP and stack bounds
226                 "runtime: sp=",
227                 "stack=[",
228         }
229         if !strings.HasPrefix(output, want[0]) {
230                 t.Errorf("output does not start with %q", want[0])
231         }
232         for _, s := range want[1:] {
233                 if !strings.Contains(output, s) {
234                         t.Errorf("output does not contain %q", s)
235                 }
236         }
237         if t.Failed() {
238                 t.Logf("output:\n%s", output)
239         }
240 }
241
242 func TestThreadExhaustion(t *testing.T) {
243         output := runTestProg(t, "testprog", "ThreadExhaustion")
244         want := "runtime: program exceeds 10-thread limit\nfatal error: thread exhaustion"
245         if !strings.HasPrefix(output, want) {
246                 t.Fatalf("output does not start with %q:\n%s", want, output)
247         }
248 }
249
250 func TestRecursivePanic(t *testing.T) {
251         output := runTestProg(t, "testprog", "RecursivePanic")
252         want := `wrap: bad
253 panic: again
254
255 `
256         if !strings.HasPrefix(output, want) {
257                 t.Fatalf("output does not start with %q:\n%s", want, output)
258         }
259
260 }
261
262 func TestRecursivePanic2(t *testing.T) {
263         output := runTestProg(t, "testprog", "RecursivePanic2")
264         want := `first panic
265 second panic
266 panic: third panic
267
268 `
269         if !strings.HasPrefix(output, want) {
270                 t.Fatalf("output does not start with %q:\n%s", want, output)
271         }
272
273 }
274
275 func TestRecursivePanic3(t *testing.T) {
276         output := runTestProg(t, "testprog", "RecursivePanic3")
277         want := `panic: first panic
278
279 `
280         if !strings.HasPrefix(output, want) {
281                 t.Fatalf("output does not start with %q:\n%s", want, output)
282         }
283
284 }
285
286 func TestRecursivePanic4(t *testing.T) {
287         output := runTestProg(t, "testprog", "RecursivePanic4")
288         want := `panic: first panic [recovered]
289         panic: second panic
290 `
291         if !strings.HasPrefix(output, want) {
292                 t.Fatalf("output does not start with %q:\n%s", want, output)
293         }
294
295 }
296
297 func TestGoexitCrash(t *testing.T) {
298         // External linking brings in cgo, causing deadlock detection not working.
299         testenv.MustInternalLink(t)
300
301         output := runTestProg(t, "testprog", "GoexitExit")
302         want := "no goroutines (main called runtime.Goexit) - deadlock!"
303         if !strings.Contains(output, want) {
304                 t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
305         }
306 }
307
308 func TestGoexitDefer(t *testing.T) {
309         c := make(chan struct{})
310         go func() {
311                 defer func() {
312                         r := recover()
313                         if r != nil {
314                                 t.Errorf("non-nil recover during Goexit")
315                         }
316                         c <- struct{}{}
317                 }()
318                 runtime.Goexit()
319         }()
320         // Note: if the defer fails to run, we will get a deadlock here
321         <-c
322 }
323
324 func TestGoNil(t *testing.T) {
325         output := runTestProg(t, "testprog", "GoNil")
326         want := "go of nil func value"
327         if !strings.Contains(output, want) {
328                 t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
329         }
330 }
331
332 func TestMainGoroutineID(t *testing.T) {
333         output := runTestProg(t, "testprog", "MainGoroutineID")
334         want := "panic: test\n\ngoroutine 1 [running]:\n"
335         if !strings.HasPrefix(output, want) {
336                 t.Fatalf("output does not start with %q:\n%s", want, output)
337         }
338 }
339
340 func TestNoHelperGoroutines(t *testing.T) {
341         output := runTestProg(t, "testprog", "NoHelperGoroutines")
342         matches := regexp.MustCompile(`goroutine [0-9]+ \[`).FindAllStringSubmatch(output, -1)
343         if len(matches) != 1 || matches[0][0] != "goroutine 1 [" {
344                 t.Fatalf("want to see only goroutine 1, see:\n%s", output)
345         }
346 }
347
348 func TestBreakpoint(t *testing.T) {
349         output := runTestProg(t, "testprog", "Breakpoint")
350         // If runtime.Breakpoint() is inlined, then the stack trace prints
351         // "runtime.Breakpoint(...)" instead of "runtime.Breakpoint()".
352         want := "runtime.Breakpoint("
353         if !strings.Contains(output, want) {
354                 t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
355         }
356 }
357
358 func TestGoexitInPanic(t *testing.T) {
359         // External linking brings in cgo, causing deadlock detection not working.
360         testenv.MustInternalLink(t)
361
362         // see issue 8774: this code used to trigger an infinite recursion
363         output := runTestProg(t, "testprog", "GoexitInPanic")
364         want := "fatal error: no goroutines (main called runtime.Goexit) - deadlock!"
365         if !strings.HasPrefix(output, want) {
366                 t.Fatalf("output does not start with %q:\n%s", want, output)
367         }
368 }
369
370 // Issue 14965: Runtime panics should be of type runtime.Error
371 func TestRuntimePanicWithRuntimeError(t *testing.T) {
372         testCases := [...]func(){
373                 0: func() {
374                         var m map[uint64]bool
375                         m[1234] = true
376                 },
377                 1: func() {
378                         ch := make(chan struct{})
379                         close(ch)
380                         close(ch)
381                 },
382                 2: func() {
383                         var ch = make(chan struct{})
384                         close(ch)
385                         ch <- struct{}{}
386                 },
387                 3: func() {
388                         var s = make([]int, 2)
389                         _ = s[2]
390                 },
391                 4: func() {
392                         n := -1
393                         _ = make(chan bool, n)
394                 },
395                 5: func() {
396                         close((chan bool)(nil))
397                 },
398         }
399
400         for i, fn := range testCases {
401                 got := panicValue(fn)
402                 if _, ok := got.(runtime.Error); !ok {
403                         t.Errorf("test #%d: recovered value %v(type %T) does not implement runtime.Error", i, got, got)
404                 }
405         }
406 }
407
408 func panicValue(fn func()) (recovered interface{}) {
409         defer func() {
410                 recovered = recover()
411         }()
412         fn()
413         return
414 }
415
416 func TestPanicAfterGoexit(t *testing.T) {
417         // an uncaught panic should still work after goexit
418         output := runTestProg(t, "testprog", "PanicAfterGoexit")
419         want := "panic: hello"
420         if !strings.HasPrefix(output, want) {
421                 t.Fatalf("output does not start with %q:\n%s", want, output)
422         }
423 }
424
425 func TestRecoveredPanicAfterGoexit(t *testing.T) {
426         // External linking brings in cgo, causing deadlock detection not working.
427         testenv.MustInternalLink(t)
428
429         output := runTestProg(t, "testprog", "RecoveredPanicAfterGoexit")
430         want := "fatal error: no goroutines (main called runtime.Goexit) - deadlock!"
431         if !strings.HasPrefix(output, want) {
432                 t.Fatalf("output does not start with %q:\n%s", want, output)
433         }
434 }
435
436 func TestRecoverBeforePanicAfterGoexit(t *testing.T) {
437         // External linking brings in cgo, causing deadlock detection not working.
438         testenv.MustInternalLink(t)
439
440         t.Parallel()
441         output := runTestProg(t, "testprog", "RecoverBeforePanicAfterGoexit")
442         want := "fatal error: no goroutines (main called runtime.Goexit) - deadlock!"
443         if !strings.HasPrefix(output, want) {
444                 t.Fatalf("output does not start with %q:\n%s", want, output)
445         }
446 }
447
448 func TestRecoverBeforePanicAfterGoexit2(t *testing.T) {
449         // External linking brings in cgo, causing deadlock detection not working.
450         testenv.MustInternalLink(t)
451
452         t.Parallel()
453         output := runTestProg(t, "testprog", "RecoverBeforePanicAfterGoexit2")
454         want := "fatal error: no goroutines (main called runtime.Goexit) - deadlock!"
455         if !strings.HasPrefix(output, want) {
456                 t.Fatalf("output does not start with %q:\n%s", want, output)
457         }
458 }
459
460 func TestNetpollDeadlock(t *testing.T) {
461         if os.Getenv("GO_BUILDER_NAME") == "darwin-amd64-10_12" {
462                 // A suspected kernel bug in macOS 10.12 occasionally results in
463                 // an apparent deadlock when dialing localhost. The errors have not
464                 // been observed on newer versions of the OS, so we don't plan to work
465                 // around them. See https://golang.org/issue/22019.
466                 testenv.SkipFlaky(t, 22019)
467         }
468
469         t.Parallel()
470         output := runTestProg(t, "testprognet", "NetpollDeadlock")
471         want := "done\n"
472         if !strings.HasSuffix(output, want) {
473                 t.Fatalf("output does not start with %q:\n%s", want, output)
474         }
475 }
476
477 func TestPanicTraceback(t *testing.T) {
478         t.Parallel()
479         output := runTestProg(t, "testprog", "PanicTraceback")
480         want := "panic: hello\n\tpanic: panic pt2\n\tpanic: panic pt1\n"
481         if !strings.HasPrefix(output, want) {
482                 t.Fatalf("output does not start with %q:\n%s", want, output)
483         }
484
485         // Check functions in the traceback.
486         fns := []string{"main.pt1.func1", "panic", "main.pt2.func1", "panic", "main.pt2", "main.pt1"}
487         for _, fn := range fns {
488                 re := regexp.MustCompile(`(?m)^` + regexp.QuoteMeta(fn) + `\(.*\n`)
489                 idx := re.FindStringIndex(output)
490                 if idx == nil {
491                         t.Fatalf("expected %q function in traceback:\n%s", fn, output)
492                 }
493                 output = output[idx[1]:]
494         }
495 }
496
497 func testPanicDeadlock(t *testing.T, name string, want string) {
498         // test issue 14432
499         output := runTestProg(t, "testprog", name)
500         if !strings.HasPrefix(output, want) {
501                 t.Fatalf("output does not start with %q:\n%s", want, output)
502         }
503 }
504
505 func TestPanicDeadlockGosched(t *testing.T) {
506         testPanicDeadlock(t, "GoschedInPanic", "panic: errorThatGosched\n\n")
507 }
508
509 func TestPanicDeadlockSyscall(t *testing.T) {
510         testPanicDeadlock(t, "SyscallInPanic", "1\n2\npanic: 3\n\n")
511 }
512
513 func TestPanicLoop(t *testing.T) {
514         output := runTestProg(t, "testprog", "PanicLoop")
515         if want := "panic while printing panic value"; !strings.Contains(output, want) {
516                 t.Errorf("output does not contain %q:\n%s", want, output)
517         }
518 }
519
520 func TestMemPprof(t *testing.T) {
521         testenv.MustHaveGoRun(t)
522
523         exe, err := buildTestProg(t, "testprog")
524         if err != nil {
525                 t.Fatal(err)
526         }
527
528         got, err := testenv.CleanCmdEnv(exec.Command(exe, "MemProf")).CombinedOutput()
529         if err != nil {
530                 t.Fatal(err)
531         }
532         fn := strings.TrimSpace(string(got))
533         defer os.Remove(fn)
534
535         for try := 0; try < 2; try++ {
536                 cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "tool", "pprof", "-alloc_space", "-top"))
537                 // Check that pprof works both with and without explicit executable on command line.
538                 if try == 0 {
539                         cmd.Args = append(cmd.Args, exe, fn)
540                 } else {
541                         cmd.Args = append(cmd.Args, fn)
542                 }
543                 found := false
544                 for i, e := range cmd.Env {
545                         if strings.HasPrefix(e, "PPROF_TMPDIR=") {
546                                 cmd.Env[i] = "PPROF_TMPDIR=" + os.TempDir()
547                                 found = true
548                                 break
549                         }
550                 }
551                 if !found {
552                         cmd.Env = append(cmd.Env, "PPROF_TMPDIR="+os.TempDir())
553                 }
554
555                 top, err := cmd.CombinedOutput()
556                 t.Logf("%s:\n%s", cmd.Args, top)
557                 if err != nil {
558                         t.Error(err)
559                 } else if !bytes.Contains(top, []byte("MemProf")) {
560                         t.Error("missing MemProf in pprof output")
561                 }
562         }
563 }
564
565 var concurrentMapTest = flag.Bool("run_concurrent_map_tests", false, "also run flaky concurrent map tests")
566
567 func TestConcurrentMapWrites(t *testing.T) {
568         if !*concurrentMapTest {
569                 t.Skip("skipping without -run_concurrent_map_tests")
570         }
571         testenv.MustHaveGoRun(t)
572         output := runTestProg(t, "testprog", "concurrentMapWrites")
573         want := "fatal error: concurrent map writes"
574         if !strings.HasPrefix(output, want) {
575                 t.Fatalf("output does not start with %q:\n%s", want, output)
576         }
577 }
578 func TestConcurrentMapReadWrite(t *testing.T) {
579         if !*concurrentMapTest {
580                 t.Skip("skipping without -run_concurrent_map_tests")
581         }
582         testenv.MustHaveGoRun(t)
583         output := runTestProg(t, "testprog", "concurrentMapReadWrite")
584         want := "fatal error: concurrent map read and map write"
585         if !strings.HasPrefix(output, want) {
586                 t.Fatalf("output does not start with %q:\n%s", want, output)
587         }
588 }
589 func TestConcurrentMapIterateWrite(t *testing.T) {
590         if !*concurrentMapTest {
591                 t.Skip("skipping without -run_concurrent_map_tests")
592         }
593         testenv.MustHaveGoRun(t)
594         output := runTestProg(t, "testprog", "concurrentMapIterateWrite")
595         want := "fatal error: concurrent map iteration and map write"
596         if !strings.HasPrefix(output, want) {
597                 t.Fatalf("output does not start with %q:\n%s", want, output)
598         }
599 }
600
601 type point struct {
602         x, y *int
603 }
604
605 func (p *point) negate() {
606         *p.x = *p.x * -1
607         *p.y = *p.y * -1
608 }
609
610 // Test for issue #10152.
611 func TestPanicInlined(t *testing.T) {
612         defer func() {
613                 r := recover()
614                 if r == nil {
615                         t.Fatalf("recover failed")
616                 }
617                 buf := make([]byte, 2048)
618                 n := runtime.Stack(buf, false)
619                 buf = buf[:n]
620                 if !bytes.Contains(buf, []byte("(*point).negate(")) {
621                         t.Fatalf("expecting stack trace to contain call to (*point).negate()")
622                 }
623         }()
624
625         pt := new(point)
626         pt.negate()
627 }
628
629 // Test for issues #3934 and #20018.
630 // We want to delay exiting until a panic print is complete.
631 func TestPanicRace(t *testing.T) {
632         testenv.MustHaveGoRun(t)
633
634         exe, err := buildTestProg(t, "testprog")
635         if err != nil {
636                 t.Fatal(err)
637         }
638
639         // The test is intentionally racy, and in my testing does not
640         // produce the expected output about 0.05% of the time.
641         // So run the program in a loop and only fail the test if we
642         // get the wrong output ten times in a row.
643         const tries = 10
644 retry:
645         for i := 0; i < tries; i++ {
646                 got, err := testenv.CleanCmdEnv(exec.Command(exe, "PanicRace")).CombinedOutput()
647                 if err == nil {
648                         t.Logf("try %d: program exited successfully, should have failed", i+1)
649                         continue
650                 }
651
652                 if i > 0 {
653                         t.Logf("try %d:\n", i+1)
654                 }
655                 t.Logf("%s\n", got)
656
657                 wants := []string{
658                         "panic: crash",
659                         "PanicRace",
660                         "created by ",
661                 }
662                 for _, want := range wants {
663                         if !bytes.Contains(got, []byte(want)) {
664                                 t.Logf("did not find expected string %q", want)
665                                 continue retry
666                         }
667                 }
668
669                 // Test generated expected output.
670                 return
671         }
672         t.Errorf("test ran %d times without producing expected output", tries)
673 }
674
675 func TestBadTraceback(t *testing.T) {
676         output := runTestProg(t, "testprog", "BadTraceback")
677         for _, want := range []string{
678                 "runtime: unexpected return pc",
679                 "called from 0xbad",
680                 "00000bad",    // Smashed LR in hex dump
681                 "<main.badLR", // Symbolization in hex dump (badLR1 or badLR2)
682         } {
683                 if !strings.Contains(output, want) {
684                         t.Errorf("output does not contain %q:\n%s", want, output)
685                 }
686         }
687 }
688
689 func TestTimePprof(t *testing.T) {
690         // Pass GOTRACEBACK for issue #41120 to try to get more
691         // information on timeout.
692         fn := runTestProg(t, "testprog", "TimeProf", "GOTRACEBACK=crash")
693         fn = strings.TrimSpace(fn)
694         defer os.Remove(fn)
695
696         cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "tool", "pprof", "-top", "-nodecount=1", fn))
697         cmd.Env = append(cmd.Env, "PPROF_TMPDIR="+os.TempDir())
698         top, err := cmd.CombinedOutput()
699         t.Logf("%s", top)
700         if err != nil {
701                 t.Error(err)
702         } else if bytes.Contains(top, []byte("ExternalCode")) {
703                 t.Error("profiler refers to ExternalCode")
704         }
705 }
706
707 // Test that runtime.abort does so.
708 func TestAbort(t *testing.T) {
709         // Pass GOTRACEBACK to ensure we get runtime frames.
710         output := runTestProg(t, "testprog", "Abort", "GOTRACEBACK=system")
711         if want := "runtime.abort"; !strings.Contains(output, want) {
712                 t.Errorf("output does not contain %q:\n%s", want, output)
713         }
714         if strings.Contains(output, "BAD") {
715                 t.Errorf("output contains BAD:\n%s", output)
716         }
717         // Check that it's a signal traceback.
718         want := "PC="
719         // For systems that use a breakpoint, check specifically for that.
720         switch runtime.GOARCH {
721         case "386", "amd64":
722                 switch runtime.GOOS {
723                 case "plan9":
724                         want = "sys: breakpoint"
725                 case "windows":
726                         want = "Exception 0x80000003"
727                 default:
728                         want = "SIGTRAP"
729                 }
730         }
731         if !strings.Contains(output, want) {
732                 t.Errorf("output does not contain %q:\n%s", want, output)
733         }
734 }
735
736 // For TestRuntimePanic: test a panic in the runtime package without
737 // involving the testing harness.
738 func init() {
739         if os.Getenv("GO_TEST_RUNTIME_PANIC") == "1" {
740                 defer func() {
741                         if r := recover(); r != nil {
742                                 // We expect to crash, so exit 0
743                                 // to indicate failure.
744                                 os.Exit(0)
745                         }
746                 }()
747                 runtime.PanicForTesting(nil, 1)
748                 // We expect to crash, so exit 0 to indicate failure.
749                 os.Exit(0)
750         }
751 }
752
753 func TestRuntimePanic(t *testing.T) {
754         testenv.MustHaveExec(t)
755         cmd := testenv.CleanCmdEnv(exec.Command(os.Args[0], "-test.run=TestRuntimePanic"))
756         cmd.Env = append(cmd.Env, "GO_TEST_RUNTIME_PANIC=1")
757         out, err := cmd.CombinedOutput()
758         t.Logf("%s", out)
759         if err == nil {
760                 t.Error("child process did not fail")
761         } else if want := "runtime.unexportedPanicForTesting"; !bytes.Contains(out, []byte(want)) {
762                 t.Errorf("output did not contain expected string %q", want)
763         }
764 }
765
766 // Test that g0 stack overflows are handled gracefully.
767 func TestG0StackOverflow(t *testing.T) {
768         testenv.MustHaveExec(t)
769
770         switch runtime.GOOS {
771         case "darwin", "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "android":
772                 t.Skipf("g0 stack is wrong on pthread platforms (see golang.org/issue/26061)")
773         }
774
775         if os.Getenv("TEST_G0_STACK_OVERFLOW") != "1" {
776                 cmd := testenv.CleanCmdEnv(exec.Command(os.Args[0], "-test.run=TestG0StackOverflow", "-test.v"))
777                 cmd.Env = append(cmd.Env, "TEST_G0_STACK_OVERFLOW=1")
778                 out, err := cmd.CombinedOutput()
779                 // Don't check err since it's expected to crash.
780                 if n := strings.Count(string(out), "morestack on g0\n"); n != 1 {
781                         t.Fatalf("%s\n(exit status %v)", out, err)
782                 }
783                 // Check that it's a signal-style traceback.
784                 if runtime.GOOS != "windows" {
785                         if want := "PC="; !strings.Contains(string(out), want) {
786                                 t.Errorf("output does not contain %q:\n%s", want, out)
787                         }
788                 }
789                 return
790         }
791
792         runtime.G0StackOverflow()
793 }
794
795 // Test that panic message is not clobbered.
796 // See issue 30150.
797 func TestDoublePanic(t *testing.T) {
798         output := runTestProg(t, "testprog", "DoublePanic", "GODEBUG=clobberfree=1")
799         wants := []string{"panic: XXX", "panic: YYY"}
800         for _, want := range wants {
801                 if !strings.Contains(output, want) {
802                         t.Errorf("output:\n%s\n\nwant output containing: %s", output, want)
803                 }
804         }
805 }