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