runtime: don't check for stale runtime
[gcc.git] / libgo / go / 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 "io/ioutil"
13 "os"
14 "os/exec"
15 "path/filepath"
16 "regexp"
17 "runtime"
18 "strconv"
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 exe string
43 err error
44 }
45
46 func runTestProg(t *testing.T, binary, name string, env ...string) string {
47 if *flagQuick {
48 t.Skip("-quick")
49 }
50
51 testenv.MustHaveGoBuild(t)
52
53 exe, err := buildTestProg(t, binary)
54 if err != nil {
55 t.Fatal(err)
56 }
57
58 cmd := testenv.CleanCmdEnv(exec.Command(exe, name))
59 cmd.Env = append(cmd.Env, env...)
60 if testing.Short() {
61 cmd.Env = append(cmd.Env, "RUNTIME_TEST_SHORT=1")
62 }
63 var b bytes.Buffer
64 cmd.Stdout = &b
65 cmd.Stderr = &b
66 if err := cmd.Start(); err != nil {
67 t.Fatalf("starting %s %s: %v", binary, name, err)
68 }
69
70 // If the process doesn't complete within 1 minute,
71 // assume it is hanging and kill it to get a stack trace.
72 p := cmd.Process
73 done := make(chan bool)
74 go func() {
75 scale := 1
76 // This GOARCH/GOOS test is copied from cmd/dist/test.go.
77 // TODO(iant): Have cmd/dist update the environment variable.
78 if runtime.GOARCH == "arm" || runtime.GOOS == "windows" {
79 scale = 2
80 }
81 if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
82 if sc, err := strconv.Atoi(s); err == nil {
83 scale = sc
84 }
85 }
86
87 select {
88 case <-done:
89 case <-time.After(time.Duration(scale) * time.Minute):
90 p.Signal(sigquit)
91 }
92 }()
93
94 if err := cmd.Wait(); err != nil {
95 t.Logf("%s %s exit status: %v", binary, name, err)
96 }
97 close(done)
98
99 return b.String()
100 }
101
102 func buildTestProg(t *testing.T, binary string, flags ...string) (string, error) {
103 if *flagQuick {
104 t.Skip("-quick")
105 }
106
107 checkStaleRuntime(t)
108
109 testprog.Lock()
110 defer testprog.Unlock()
111 if testprog.dir == "" {
112 dir, err := ioutil.TempDir("", "go-build")
113 if err != nil {
114 t.Fatalf("failed to create temp directory: %v", err)
115 }
116 testprog.dir = dir
117 toRemove = append(toRemove, dir)
118 }
119
120 if testprog.target == nil {
121 testprog.target = make(map[string]buildexe)
122 }
123 name := binary
124 if len(flags) > 0 {
125 name += "_" + strings.Join(flags, "_")
126 }
127 target, ok := testprog.target[name]
128 if ok {
129 return target.exe, target.err
130 }
131
132 exe := filepath.Join(testprog.dir, name+".exe")
133 cmd := exec.Command(testenv.GoToolPath(t), append([]string{"build", "-o", exe}, flags...)...)
134 cmd.Dir = "testdata/" + binary
135 out, err := testenv.CleanCmdEnv(cmd).CombinedOutput()
136 if err != nil {
137 target.err = fmt.Errorf("building %s %v: %v\n%s", binary, flags, err, out)
138 testprog.target[name] = target
139 return "", target.err
140 }
141 target.exe = exe
142 testprog.target[name] = target
143 return exe, nil
144 }
145
146 var (
147 staleRuntimeOnce sync.Once // guards init of staleRuntimeErr
148 staleRuntimeErr error
149 )
150
151 func checkStaleRuntime(t *testing.T) {
152 staleRuntimeOnce.Do(func() {
153 if runtime.Compiler == "gccgo" {
154 return
155 }
156 // 'go run' uses the installed copy of runtime.a, which may be out of date.
157 out, err := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "list", "-gcflags=all="+os.Getenv("GO_GCFLAGS"), "-f", "{{.Stale}}", "runtime")).CombinedOutput()
158 if err != nil {
159 staleRuntimeErr = fmt.Errorf("failed to execute 'go list': %v\n%v", err, string(out))
160 return
161 }
162 if string(out) != "false\n" {
163 t.Logf("go list -f {{.Stale}} runtime:\n%s", out)
164 out, err := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "list", "-gcflags=all="+os.Getenv("GO_GCFLAGS"), "-f", "{{.StaleReason}}", "runtime")).CombinedOutput()
165 if err != nil {
166 t.Logf("go list -f {{.StaleReason}} failed: %v", err)
167 }
168 t.Logf("go list -f {{.StaleReason}} runtime:\n%s", out)
169 staleRuntimeErr = fmt.Errorf("Stale runtime.a. Run 'go install runtime'.")
170 }
171 })
172 if staleRuntimeErr != nil {
173 t.Fatal(staleRuntimeErr)
174 }
175 }
176
177 func testCrashHandler(t *testing.T, cgo bool) {
178 type crashTest struct {
179 Cgo bool
180 }
181 var output string
182 if cgo {
183 output = runTestProg(t, "testprogcgo", "Crash")
184 } else {
185 output = runTestProg(t, "testprog", "Crash")
186 }
187 want := "main: recovered done\nnew-thread: recovered done\nsecond-new-thread: recovered done\nmain-again: recovered done\n"
188 if output != want {
189 t.Fatalf("output:\n%s\n\nwanted:\n%s", output, want)
190 }
191 }
192
193 func TestCrashHandler(t *testing.T) {
194 testCrashHandler(t, false)
195 }
196
197 func testDeadlock(t *testing.T, name string) {
198 output := runTestProg(t, "testprog", name)
199 want := "fatal error: all goroutines are asleep - deadlock!\n"
200 if !strings.HasPrefix(output, want) {
201 t.Fatalf("output does not start with %q:\n%s", want, output)
202 }
203 }
204
205 func TestSimpleDeadlock(t *testing.T) {
206 testDeadlock(t, "SimpleDeadlock")
207 }
208
209 func TestInitDeadlock(t *testing.T) {
210 testDeadlock(t, "InitDeadlock")
211 }
212
213 func TestLockedDeadlock(t *testing.T) {
214 testDeadlock(t, "LockedDeadlock")
215 }
216
217 func TestLockedDeadlock2(t *testing.T) {
218 testDeadlock(t, "LockedDeadlock2")
219 }
220
221 func TestGoexitDeadlock(t *testing.T) {
222 output := runTestProg(t, "testprog", "GoexitDeadlock")
223 want := "no goroutines (main called runtime.Goexit) - deadlock!"
224 if !strings.Contains(output, want) {
225 t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
226 }
227 }
228
229 func TestStackOverflow(t *testing.T) {
230 if runtime.Compiler == "gccgo" {
231 t.Skip("gccgo does not do stack overflow checking")
232 }
233 output := runTestProg(t, "testprog", "StackOverflow")
234 want := "runtime: goroutine stack exceeds 1474560-byte limit\nfatal error: stack overflow"
235 if !strings.HasPrefix(output, want) {
236 t.Fatalf("output does not start with %q:\n%s", want, output)
237 }
238 }
239
240 func TestThreadExhaustion(t *testing.T) {
241 output := runTestProg(t, "testprog", "ThreadExhaustion")
242 want := "runtime: program exceeds 10-thread limit\nfatal error: thread exhaustion"
243 if !strings.HasPrefix(output, want) {
244 t.Fatalf("output does not start with %q:\n%s", want, output)
245 }
246 }
247
248 func TestRecursivePanic(t *testing.T) {
249 output := runTestProg(t, "testprog", "RecursivePanic")
250 want := `wrap: bad
251 panic: again
252
253 `
254 if !strings.HasPrefix(output, want) {
255 t.Fatalf("output does not start with %q:\n%s", want, output)
256 }
257
258 }
259
260 func TestGoexitCrash(t *testing.T) {
261 output := runTestProg(t, "testprog", "GoexitExit")
262 want := "no goroutines (main called runtime.Goexit) - deadlock!"
263 if !strings.Contains(output, want) {
264 t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
265 }
266 }
267
268 func TestGoexitDefer(t *testing.T) {
269 c := make(chan struct{})
270 go func() {
271 defer func() {
272 r := recover()
273 if r != nil {
274 t.Errorf("non-nil recover during Goexit")
275 }
276 c <- struct{}{}
277 }()
278 runtime.Goexit()
279 }()
280 // Note: if the defer fails to run, we will get a deadlock here
281 <-c
282 }
283
284 func TestGoNil(t *testing.T) {
285 output := runTestProg(t, "testprog", "GoNil")
286 want := "go of nil func value"
287 if !strings.Contains(output, want) {
288 t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
289 }
290 }
291
292 func TestMainGoroutineID(t *testing.T) {
293 output := runTestProg(t, "testprog", "MainGoroutineID")
294 want := "panic: test\n\ngoroutine 1 [running]:\n"
295 if !strings.HasPrefix(output, want) {
296 t.Fatalf("output does not start with %q:\n%s", want, output)
297 }
298 }
299
300 func TestNoHelperGoroutines(t *testing.T) {
301 output := runTestProg(t, "testprog", "NoHelperGoroutines")
302 matches := regexp.MustCompile(`goroutine [0-9]+ \[`).FindAllStringSubmatch(output, -1)
303 if len(matches) != 1 || matches[0][0] != "goroutine 1 [" {
304 t.Fatalf("want to see only goroutine 1, see:\n%s", output)
305 }
306 }
307
308 func TestBreakpoint(t *testing.T) {
309 output := runTestProg(t, "testprog", "Breakpoint")
310 // If runtime.Breakpoint() is inlined, then the stack trace prints
311 // "runtime.Breakpoint(...)" instead of "runtime.Breakpoint()".
312 // For gccgo, no parens.
313 want := "runtime.Breakpoint"
314 if !strings.Contains(output, want) {
315 t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
316 }
317 }
318
319 func TestGoexitInPanic(t *testing.T) {
320 // see issue 8774: this code used to trigger an infinite recursion
321 output := runTestProg(t, "testprog", "GoexitInPanic")
322 want := "fatal error: no goroutines (main called runtime.Goexit) - deadlock!"
323 if !strings.HasPrefix(output, want) {
324 t.Fatalf("output does not start with %q:\n%s", want, output)
325 }
326 }
327
328 // Issue 14965: Runtime panics should be of type runtime.Error
329 func TestRuntimePanicWithRuntimeError(t *testing.T) {
330 testCases := [...]func(){
331 0: func() {
332 var m map[uint64]bool
333 m[1234] = true
334 },
335 1: func() {
336 ch := make(chan struct{})
337 close(ch)
338 close(ch)
339 },
340 2: func() {
341 var ch = make(chan struct{})
342 close(ch)
343 ch <- struct{}{}
344 },
345 3: func() {
346 var s = make([]int, 2)
347 _ = s[2]
348 },
349 4: func() {
350 n := -1
351 _ = make(chan bool, n)
352 },
353 5: func() {
354 close((chan bool)(nil))
355 },
356 }
357
358 for i, fn := range testCases {
359 got := panicValue(fn)
360 if _, ok := got.(runtime.Error); !ok {
361 t.Errorf("test #%d: recovered value %v(type %T) does not implement runtime.Error", i, got, got)
362 }
363 }
364 }
365
366 func panicValue(fn func()) (recovered interface{}) {
367 defer func() {
368 recovered = recover()
369 }()
370 fn()
371 return
372 }
373
374 func TestPanicAfterGoexit(t *testing.T) {
375 // an uncaught panic should still work after goexit
376 output := runTestProg(t, "testprog", "PanicAfterGoexit")
377 want := "panic: hello"
378 if !strings.HasPrefix(output, want) {
379 t.Fatalf("output does not start with %q:\n%s", want, output)
380 }
381 }
382
383 func TestRecoveredPanicAfterGoexit(t *testing.T) {
384 output := runTestProg(t, "testprog", "RecoveredPanicAfterGoexit")
385 want := "fatal error: no goroutines (main called runtime.Goexit) - deadlock!"
386 if !strings.HasPrefix(output, want) {
387 t.Fatalf("output does not start with %q:\n%s", want, output)
388 }
389 }
390
391 func TestRecoverBeforePanicAfterGoexit(t *testing.T) {
392 // 1. defer a function that recovers
393 // 2. defer a function that panics
394 // 3. call goexit
395 // Goexit should run the #2 defer. Its panic
396 // should be caught by the #1 defer, and execution
397 // should resume in the caller. Like the Goexit
398 // never happened!
399 defer func() {
400 r := recover()
401 if r == nil {
402 panic("bad recover")
403 }
404 }()
405 defer func() {
406 panic("hello")
407 }()
408 runtime.Goexit()
409 }
410
411 func TestNetpollDeadlock(t *testing.T) {
412 t.Parallel()
413 output := runTestProg(t, "testprognet", "NetpollDeadlock")
414 want := "done\n"
415 if !strings.HasSuffix(output, want) {
416 t.Fatalf("output does not start with %q:\n%s", want, output)
417 }
418 }
419
420 func TestPanicTraceback(t *testing.T) {
421 t.Parallel()
422 output := runTestProg(t, "testprog", "PanicTraceback")
423 want := "panic: hello"
424 if !strings.HasPrefix(output, want) {
425 t.Fatalf("output does not start with %q:\n%s", want, output)
426 }
427
428 // Check functions in the traceback.
429 fns := []string{"main.pt1.func1", "panic", "main.pt2.func1", "panic", "main.pt2", "main.pt1"}
430 if runtime.Compiler == "gccgo" {
431 fns = []string{"main.pt1..func1", "panic", "main.pt2..func1", "panic", "main.pt2", "main.pt1"}
432 }
433 for _, fn := range fns {
434 var re *regexp.Regexp
435 if runtime.Compiler != "gccgo" {
436 re = regexp.MustCompile(`(?m)^` + regexp.QuoteMeta(fn) + `\(.*\n`)
437 } else {
438 re = regexp.MustCompile(`(?m)^` + regexp.QuoteMeta(fn) + `.*\n`)
439 }
440 idx := re.FindStringIndex(output)
441 if idx == nil {
442 t.Fatalf("expected %q function in traceback:\n%s", fn, output)
443 }
444 output = output[idx[1]:]
445 }
446 }
447
448 func testPanicDeadlock(t *testing.T, name string, want string) {
449 // test issue 14432
450 output := runTestProg(t, "testprog", name)
451 if !strings.HasPrefix(output, want) {
452 t.Fatalf("output does not start with %q:\n%s", want, output)
453 }
454 }
455
456 func TestPanicDeadlockGosched(t *testing.T) {
457 testPanicDeadlock(t, "GoschedInPanic", "panic: errorThatGosched\n\n")
458 }
459
460 func TestPanicDeadlockSyscall(t *testing.T) {
461 testPanicDeadlock(t, "SyscallInPanic", "1\n2\npanic: 3\n\n")
462 }
463
464 func TestPanicLoop(t *testing.T) {
465 output := runTestProg(t, "testprog", "PanicLoop")
466 if want := "panic while printing panic value"; !strings.Contains(output, want) {
467 t.Errorf("output does not contain %q:\n%s", want, output)
468 }
469 }
470
471 func TestMemPprof(t *testing.T) {
472 testenv.MustHaveGoRun(t)
473 if runtime.Compiler == "gccgo" {
474 t.Skip("gccgo may not have the pprof tool")
475 }
476
477 exe, err := buildTestProg(t, "testprog")
478 if err != nil {
479 t.Fatal(err)
480 }
481
482 got, err := testenv.CleanCmdEnv(exec.Command(exe, "MemProf")).CombinedOutput()
483 if err != nil {
484 t.Fatal(err)
485 }
486 fn := strings.TrimSpace(string(got))
487 defer os.Remove(fn)
488
489 for try := 0; try < 2; try++ {
490 cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "tool", "pprof", "-alloc_space", "-top"))
491 // Check that pprof works both with and without explicit executable on command line.
492 if try == 0 {
493 cmd.Args = append(cmd.Args, exe, fn)
494 } else {
495 cmd.Args = append(cmd.Args, fn)
496 }
497 found := false
498 for i, e := range cmd.Env {
499 if strings.HasPrefix(e, "PPROF_TMPDIR=") {
500 cmd.Env[i] = "PPROF_TMPDIR=" + os.TempDir()
501 found = true
502 break
503 }
504 }
505 if !found {
506 cmd.Env = append(cmd.Env, "PPROF_TMPDIR="+os.TempDir())
507 }
508
509 top, err := cmd.CombinedOutput()
510 t.Logf("%s:\n%s", cmd.Args, top)
511 if err != nil {
512 t.Error(err)
513 } else if !bytes.Contains(top, []byte("MemProf")) {
514 t.Error("missing MemProf in pprof output")
515 }
516 }
517 }
518
519 var concurrentMapTest = flag.Bool("run_concurrent_map_tests", false, "also run flaky concurrent map tests")
520
521 func TestConcurrentMapWrites(t *testing.T) {
522 if !*concurrentMapTest {
523 t.Skip("skipping without -run_concurrent_map_tests")
524 }
525 testenv.MustHaveGoRun(t)
526 output := runTestProg(t, "testprog", "concurrentMapWrites")
527 want := "fatal error: concurrent map writes"
528 if !strings.HasPrefix(output, want) {
529 t.Fatalf("output does not start with %q:\n%s", want, output)
530 }
531 }
532 func TestConcurrentMapReadWrite(t *testing.T) {
533 if !*concurrentMapTest {
534 t.Skip("skipping without -run_concurrent_map_tests")
535 }
536 testenv.MustHaveGoRun(t)
537 output := runTestProg(t, "testprog", "concurrentMapReadWrite")
538 want := "fatal error: concurrent map read and map write"
539 if !strings.HasPrefix(output, want) {
540 t.Fatalf("output does not start with %q:\n%s", want, output)
541 }
542 }
543 func TestConcurrentMapIterateWrite(t *testing.T) {
544 if !*concurrentMapTest {
545 t.Skip("skipping without -run_concurrent_map_tests")
546 }
547 testenv.MustHaveGoRun(t)
548 output := runTestProg(t, "testprog", "concurrentMapIterateWrite")
549 want := "fatal error: concurrent map iteration and map write"
550 if !strings.HasPrefix(output, want) {
551 t.Fatalf("output does not start with %q:\n%s", want, output)
552 }
553 }
554
555 type point struct {
556 x, y *int
557 }
558
559 func (p *point) negate() {
560 *p.x = *p.x * -1
561 *p.y = *p.y * -1
562 }
563
564 // Test for issue #10152.
565 func TestPanicInlined(t *testing.T) {
566 defer func() {
567 r := recover()
568 if r == nil {
569 t.Fatalf("recover failed")
570 }
571 buf := make([]byte, 2048)
572 n := runtime.Stack(buf, false)
573 buf = buf[:n]
574 want := []byte("(*point).negate(")
575 if runtime.Compiler == "gccgo" {
576 want = []byte("point.negate")
577 }
578 if !bytes.Contains(buf, want) {
579 t.Logf("%s", buf)
580 t.Fatalf("expecting stack trace to contain call to %s", want)
581 }
582 }()
583
584 pt := new(point)
585 pt.negate()
586 }
587
588 // Test for issues #3934 and #20018.
589 // We want to delay exiting until a panic print is complete.
590 func TestPanicRace(t *testing.T) {
591 testenv.MustHaveGoRun(t)
592
593 exe, err := buildTestProg(t, "testprog")
594 if err != nil {
595 t.Fatal(err)
596 }
597
598 // The test is intentionally racy, and in my testing does not
599 // produce the expected output about 0.05% of the time.
600 // So run the program in a loop and only fail the test if we
601 // get the wrong output ten times in a row.
602 const tries = 10
603 retry:
604 for i := 0; i < tries; i++ {
605 got, err := testenv.CleanCmdEnv(exec.Command(exe, "PanicRace")).CombinedOutput()
606 if err == nil {
607 t.Logf("try %d: program exited successfully, should have failed", i+1)
608 continue
609 }
610
611 if i > 0 {
612 t.Logf("try %d:\n", i+1)
613 }
614 t.Logf("%s\n", got)
615
616 wants := []string{
617 "panic: crash",
618 "PanicRace",
619 "created by ",
620 }
621 if runtime.Compiler == "gccgo" {
622 // gccgo will dump a function name like main.$nested30.
623 // Match on the file name instead.
624 wants[1] = "panicrace"
625 }
626 for _, want := range wants {
627 if !bytes.Contains(got, []byte(want)) {
628 t.Logf("did not find expected string %q", want)
629 continue retry
630 }
631 }
632
633 // Test generated expected output.
634 return
635 }
636 t.Errorf("test ran %d times without producing expected output", tries)
637 }
638
639 func TestBadTraceback(t *testing.T) {
640 if runtime.Compiler == "gccgo" {
641 t.Skip("gccgo does not do a hex dump")
642 }
643 output := runTestProg(t, "testprog", "BadTraceback")
644 for _, want := range []string{
645 "runtime: unexpected return pc",
646 "called from 0xbad",
647 "00000bad", // Smashed LR in hex dump
648 "<main.badLR", // Symbolization in hex dump (badLR1 or badLR2)
649 } {
650 if !strings.Contains(output, want) {
651 t.Errorf("output does not contain %q:\n%s", want, output)
652 }
653 }
654 }