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.
22 func TestCgoCrashHandler(t *testing.T) {
24 testCrashHandler(t, true)
27 func TestCgoSignalDeadlock(t *testing.T) {
28 // Don't call t.Parallel, since too much work going on at the
29 // same time can cause the testprogcgo code to overrun its
30 // timeouts (issue #18598).
32 if testing.Short() && runtime.GOOS == "windows" {
33 t.Skip("Skipping in short mode") // takes up to 64 seconds
35 got := runTestProg(t, "testprogcgo", "CgoSignalDeadlock")
38 t.Fatalf("expected %q, but got:\n%s", want, got)
42 func TestCgoTraceback(t *testing.T) {
44 got := runTestProg(t, "testprogcgo", "CgoTraceback")
47 t.Fatalf("expected %q, but got:\n%s", want, got)
51 func TestCgoCallbackGC(t *testing.T) {
54 case "plan9", "windows":
55 t.Skipf("no pthreads on %s", runtime.GOOS)
59 case runtime.GOOS == "dragonfly":
60 t.Skip("see golang.org/issue/11990")
61 case runtime.GOOS == "linux" && runtime.GOARCH == "arm":
62 t.Skip("too slow for arm builders")
63 case runtime.GOOS == "linux" && (runtime.GOARCH == "mips64" || runtime.GOARCH == "mips64le"):
64 t.Skip("too slow for mips64x builders")
67 if testenv.Builder() == "darwin-amd64-10_14" {
68 // TODO(#23011): When the 10.14 builders are gone, remove this skip.
69 t.Skip("skipping due to platform bug on macOS 10.14; see https://golang.org/issue/43926")
71 got := runTestProg(t, "testprogcgo", "CgoCallbackGC")
74 t.Fatalf("expected %q, but got:\n%s", want, got)
78 func TestCgoExternalThreadPanic(t *testing.T) {
80 if runtime.GOOS == "plan9" {
81 t.Skipf("no pthreads on %s", runtime.GOOS)
83 got := runTestProg(t, "testprogcgo", "CgoExternalThreadPanic")
85 if !strings.Contains(got, want) {
86 t.Fatalf("want failure containing %q. output:\n%s\n", want, got)
90 func TestCgoExternalThreadSIGPROF(t *testing.T) {
94 case "plan9", "windows":
95 t.Skipf("no pthreads on %s", runtime.GOOS)
98 got := runTestProg(t, "testprogcgo", "CgoExternalThreadSIGPROF", "GO_START_SIGPROF_THREAD=1")
99 if want := "OK\n"; got != want {
100 t.Fatalf("expected %q, but got:\n%s", want, got)
104 func TestCgoExternalThreadSignal(t *testing.T) {
107 switch runtime.GOOS {
108 case "plan9", "windows":
109 t.Skipf("no pthreads on %s", runtime.GOOS)
112 got := runTestProg(t, "testprogcgo", "CgoExternalThreadSignal")
113 if want := "OK\n"; got != want {
114 t.Fatalf("expected %q, but got:\n%s", want, got)
118 func TestCgoDLLImports(t *testing.T) {
120 if runtime.GOOS != "windows" {
121 t.Skip("skipping windows specific test")
123 got := runTestProg(t, "testprogcgo", "CgoDLLImportsMain")
126 t.Fatalf("expected %q, but got %v", want, got)
130 func TestCgoExecSignalMask(t *testing.T) {
133 switch runtime.GOOS {
134 case "windows", "plan9":
135 t.Skipf("skipping signal mask test on %s", runtime.GOOS)
137 got := runTestProg(t, "testprogcgo", "CgoExecSignalMask", "GOTRACEBACK=system")
140 t.Errorf("expected %q, got %v", want, got)
144 func TestEnsureDropM(t *testing.T) {
146 // Test for issue 13881.
147 switch runtime.GOOS {
148 case "windows", "plan9":
149 t.Skipf("skipping dropm test on %s", runtime.GOOS)
151 got := runTestProg(t, "testprogcgo", "EnsureDropM")
154 t.Errorf("expected %q, got %v", want, got)
158 // Test for issue 14387.
159 // Test that the program that doesn't need any cgo pointer checking
160 // takes about the same amount of time with it as without it.
161 func TestCgoCheckBytes(t *testing.T) {
163 // Make sure we don't count the build time as part of the run time.
164 testenv.MustHaveGoBuild(t)
165 exe, err := buildTestProg(t, "testprogcgo")
170 // Try it 10 times to avoid flakiness.
172 var tot1, tot2 time.Duration
173 for i := 0; i < tries; i++ {
174 cmd := testenv.CleanCmdEnv(exec.Command(exe, "CgoCheckBytes"))
175 cmd.Env = append(cmd.Env, "GODEBUG=cgocheck=0", fmt.Sprintf("GO_CGOCHECKBYTES_TRY=%d", i))
179 d1 := time.Since(start)
181 cmd = testenv.CleanCmdEnv(exec.Command(exe, "CgoCheckBytes"))
182 cmd.Env = append(cmd.Env, fmt.Sprintf("GO_CGOCHECKBYTES_TRY=%d", i))
186 d2 := time.Since(start)
189 // The slow version (d2) was less than 20 times
190 // slower than the fast version (d1), so OK.
198 t.Errorf("cgo check too slow: got %v, expected at most %v", tot2/tries, (tot1/tries)*20)
201 func TestCgoPanicDeadlock(t *testing.T) {
204 got := runTestProg(t, "testprogcgo", "CgoPanicDeadlock")
205 want := "panic: cgo error\n\n"
206 if !strings.HasPrefix(got, want) {
207 t.Fatalf("output does not start with %q:\n%s", want, got)
211 func TestCgoCCodeSIGPROF(t *testing.T) {
213 got := runTestProg(t, "testprogcgo", "CgoCCodeSIGPROF")
216 t.Errorf("expected %q got %v", want, got)
220 func TestCgoPprofCallback(t *testing.T) {
222 switch runtime.GOOS {
223 case "windows", "plan9":
224 t.Skipf("skipping cgo pprof callback test on %s", runtime.GOOS)
226 got := runTestProg(t, "testprogcgo", "CgoPprofCallback")
229 t.Errorf("expected %q got %v", want, got)
233 func TestCgoCrashTraceback(t *testing.T) {
235 switch platform := runtime.GOOS + "/" + runtime.GOARCH; platform {
239 case "linux/ppc64le":
241 t.Skipf("not yet supported on %s", platform)
243 got := runTestProg(t, "testprogcgo", "CrashTraceback")
244 for i := 1; i <= 3; i++ {
245 if !strings.Contains(got, fmt.Sprintf("cgo symbolizer:%d", i)) {
246 t.Errorf("missing cgo symbolizer:%d", i)
251 func TestCgoCrashTracebackGo(t *testing.T) {
253 switch platform := runtime.GOOS + "/" + runtime.GOARCH; platform {
257 case "linux/ppc64le":
259 t.Skipf("not yet supported on %s", platform)
261 got := runTestProg(t, "testprogcgo", "CrashTracebackGo")
262 for i := 1; i <= 3; i++ {
263 want := fmt.Sprintf("main.h%d", i)
264 if !strings.Contains(got, want) {
265 t.Errorf("missing %s", want)
270 func TestCgoTracebackContext(t *testing.T) {
272 got := runTestProg(t, "testprogcgo", "TracebackContext")
275 t.Errorf("expected %q got %v", want, got)
279 func TestCgoTracebackContextPreemption(t *testing.T) {
281 got := runTestProg(t, "testprogcgo", "TracebackContextPreemption")
284 t.Errorf("expected %q got %v", want, got)
288 func testCgoPprof(t *testing.T, buildArg, runArg, top, bottom string) {
290 if runtime.GOOS != "linux" || (runtime.GOARCH != "amd64" && runtime.GOARCH != "ppc64le" && runtime.GOARCH != "arm64") {
291 t.Skipf("not yet supported on %s/%s", runtime.GOOS, runtime.GOARCH)
293 testenv.MustHaveGoRun(t)
295 exe, err := buildTestProg(t, "testprogcgo", buildArg)
300 cmd := testenv.CleanCmdEnv(exec.Command(exe, runArg))
301 got, err := cmd.CombinedOutput()
303 if testenv.Builder() == "linux-amd64-alpine" {
304 // See Issue 18243 and Issue 19938.
305 t.Skipf("Skipping failing test on Alpine (golang.org/issue/18243). Ignoring error: %v", err)
307 t.Fatalf("%s\n\n%v", got, err)
309 fn := strings.TrimSpace(string(got))
312 for try := 0; try < 2; try++ {
313 cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "tool", "pprof", "-tagignore=ignore", "-traces"))
314 // Check that pprof works both with and without explicit executable on command line.
316 cmd.Args = append(cmd.Args, exe, fn)
318 cmd.Args = append(cmd.Args, fn)
322 for i, e := range cmd.Env {
323 if strings.HasPrefix(e, "PPROF_TMPDIR=") {
324 cmd.Env[i] = "PPROF_TMPDIR=" + os.TempDir()
330 cmd.Env = append(cmd.Env, "PPROF_TMPDIR="+os.TempDir())
333 out, err := cmd.CombinedOutput()
334 t.Logf("%s:\n%s", cmd.Args, out)
340 trace := findTrace(string(out), top)
342 t.Errorf("%s traceback missing.", top)
345 if trace[len(trace)-1] != bottom {
346 t.Errorf("invalid traceback origin: got=%v; want=[%s ... %s]", trace, top, bottom)
351 func TestCgoPprof(t *testing.T) {
352 testCgoPprof(t, "", "CgoPprof", "cpuHog", "runtime.main")
355 func TestCgoPprofPIE(t *testing.T) {
356 testCgoPprof(t, "-buildmode=pie", "CgoPprof", "cpuHog", "runtime.main")
359 func TestCgoPprofThread(t *testing.T) {
360 testCgoPprof(t, "", "CgoPprofThread", "cpuHogThread", "cpuHogThread2")
363 func TestCgoPprofThreadNoTraceback(t *testing.T) {
364 testCgoPprof(t, "", "CgoPprofThreadNoTraceback", "cpuHogThread", "runtime._ExternalCode")
367 func TestRaceProf(t *testing.T) {
368 if (runtime.GOOS != "linux" && runtime.GOOS != "freebsd") || runtime.GOARCH != "amd64" {
369 t.Skipf("not yet supported on %s/%s", runtime.GOOS, runtime.GOARCH)
372 testenv.MustHaveGoRun(t)
374 // This test requires building various packages with -race, so
375 // it's somewhat slow.
377 t.Skip("skipping test in -short mode")
380 exe, err := buildTestProg(t, "testprogcgo", "-race")
385 got, err := testenv.CleanCmdEnv(exec.Command(exe, "CgoRaceprof")).CombinedOutput()
390 if string(got) != want {
391 t.Errorf("expected %q got %s", want, got)
395 func TestRaceSignal(t *testing.T) {
397 if (runtime.GOOS != "linux" && runtime.GOOS != "freebsd") || runtime.GOARCH != "amd64" {
398 t.Skipf("not yet supported on %s/%s", runtime.GOOS, runtime.GOARCH)
401 testenv.MustHaveGoRun(t)
403 // This test requires building various packages with -race, so
404 // it's somewhat slow.
406 t.Skip("skipping test in -short mode")
409 exe, err := buildTestProg(t, "testprogcgo", "-race")
414 got, err := testenv.CleanCmdEnv(exec.Command(exe, "CgoRaceSignal")).CombinedOutput()
420 if string(got) != want {
421 t.Errorf("expected %q got %s", want, got)
425 func TestCgoNumGoroutine(t *testing.T) {
426 switch runtime.GOOS {
427 case "windows", "plan9":
428 t.Skipf("skipping numgoroutine test on %s", runtime.GOOS)
431 got := runTestProg(t, "testprogcgo", "NumGoroutine")
434 t.Errorf("expected %q got %v", want, got)
438 func TestCatchPanic(t *testing.T) {
440 switch runtime.GOOS {
441 case "plan9", "windows":
442 t.Skipf("no signals on %s", runtime.GOOS)
444 if runtime.GOARCH == "amd64" {
445 t.Skipf("crash() on darwin/amd64 doesn't raise SIGABRT")
449 testenv.MustHaveGoRun(t)
451 exe, err := buildTestProg(t, "testprogcgo")
456 for _, early := range []bool{true, false} {
457 cmd := testenv.CleanCmdEnv(exec.Command(exe, "CgoCatchPanic"))
458 // Make sure a panic results in a crash.
459 cmd.Env = append(cmd.Env, "GOTRACEBACK=crash")
461 // Tell testprogcgo to install an early signal handler for SIGABRT
462 cmd.Env = append(cmd.Env, "CGOCATCHPANIC_EARLY_HANDLER=1")
464 if out, err := cmd.CombinedOutput(); err != nil {
465 t.Errorf("testprogcgo CgoCatchPanic failed: %v\n%s", err, out)
470 func TestCgoLockOSThreadExit(t *testing.T) {
471 switch runtime.GOOS {
472 case "plan9", "windows":
473 t.Skipf("no pthreads on %s", runtime.GOOS)
476 testLockOSThreadExit(t, "testprogcgo")
479 func TestWindowsStackMemoryCgo(t *testing.T) {
480 if runtime.GOOS != "windows" {
481 t.Skip("skipping windows specific test")
483 testenv.SkipFlaky(t, 22575)
484 o := runTestProg(t, "testprogcgo", "StackMemory")
485 stackUsage, err := strconv.Atoi(o)
487 t.Fatalf("Failed to read stack usage: %v", err)
489 if expected, got := 100<<10, stackUsage; got > expected {
490 t.Fatalf("expected < %d bytes of memory per thread, got %d", expected, got)
494 func TestSigStackSwapping(t *testing.T) {
495 switch runtime.GOOS {
496 case "plan9", "windows":
497 t.Skipf("no sigaltstack on %s", runtime.GOOS)
500 got := runTestProg(t, "testprogcgo", "SigStack")
503 t.Errorf("expected %q got %v", want, got)
507 func TestCgoTracebackSigpanic(t *testing.T) {
508 // Test unwinding over a sigpanic in C code without a C
509 // symbolizer. See issue #23576.
510 if runtime.GOOS == "windows" {
511 // On Windows if we get an exception in C code, we let
512 // the Windows exception handler unwind it, rather
513 // than injecting a sigpanic.
514 t.Skip("no sigpanic in C on windows")
517 got := runTestProg(t, "testprogcgo", "TracebackSigpanic")
519 want := "runtime.sigpanic"
520 if !strings.Contains(got, want) {
521 t.Errorf("did not see %q in output", want)
523 // No runtime errors like "runtime: unexpected return pc".
524 nowant := "runtime: "
525 if strings.Contains(got, nowant) {
526 t.Errorf("unexpectedly saw %q in output", nowant)
530 func TestCgoPanicCallback(t *testing.T) {
532 got := runTestProg(t, "testprogcgo", "PanicCallback")
534 want := "panic: runtime error: invalid memory address or nil pointer dereference"
535 if !strings.Contains(got, want) {
536 t.Errorf("did not see %q in output", want)
538 want = "panic_callback"
539 if !strings.Contains(got, want) {
540 t.Errorf("did not see %q in output", want)
542 want = "PanicCallback"
543 if !strings.Contains(got, want) {
544 t.Errorf("did not see %q in output", want)
546 // No runtime errors like "runtime: unexpected return pc".
547 nowant := "runtime: "
548 if strings.Contains(got, nowant) {
549 t.Errorf("did not see %q in output", want)
553 // Test that C code called via cgo can use large Windows thread stacks
554 // and call back in to Go without crashing. See issue #20975.
556 // See also TestBigStackCallbackSyscall.
557 func TestBigStackCallbackCgo(t *testing.T) {
558 if runtime.GOOS != "windows" {
559 t.Skip("skipping windows specific test")
562 got := runTestProg(t, "testprogcgo", "BigStack")
565 t.Errorf("expected %q got %v", want, got)
569 func nextTrace(lines []string) ([]string, []string) {
571 for n, line := range lines {
572 if strings.HasPrefix(line, "---") {
573 return trace, lines[n+1:]
575 fields := strings.Fields(strings.TrimSpace(line))
576 if len(fields) == 0 {
579 // Last field contains the function name.
580 trace = append(trace, fields[len(fields)-1])
585 func findTrace(text, top string) []string {
586 lines := strings.Split(text, "\n")
587 _, lines = nextTrace(lines) // Skip the header.
590 t, lines = nextTrace(lines)
601 func TestSegv(t *testing.T) {
602 switch runtime.GOOS {
603 case "plan9", "windows":
604 t.Skipf("no signals on %s", runtime.GOOS)
607 for _, test := range []string{"Segv", "SegvInCgo", "TgkillSegv", "TgkillSegvInCgo"} {
610 // The tgkill variants only run on Linux.
611 if runtime.GOOS != "linux" && strings.HasPrefix(test, "Tgkill") {
615 t.Run(test, func(t *testing.T) {
617 got := runTestProg(t, "testprogcgo", test)
620 if !strings.Contains(got, want) {
621 if runtime.GOOS == "darwin" && runtime.GOARCH == "amd64" && strings.Contains(got, "fatal: morestack on g0") {
622 testenv.SkipFlaky(t, 39457)
624 t.Errorf("did not see %q in output", want)
627 // No runtime errors like "runtime: unknown pc".
628 switch runtime.GOOS {
629 case "darwin", "illumos", "solaris":
630 // Runtime sometimes throws when generating the traceback.
631 testenv.SkipFlaky(t, 49182)
633 if runtime.GOARCH == "386" {
634 // Runtime throws when generating a traceback from
635 // a VDSO call via asmcgocall.
636 testenv.SkipFlaky(t, 50504)
639 if test == "SegvInCgo" && strings.Contains(got, "unknown pc") {
640 testenv.SkipFlaky(t, 50979)
643 for _, nowant := range []string{"fatal error: ", "runtime: "} {
644 if strings.Contains(got, nowant) {
645 if runtime.GOOS == "darwin" && strings.Contains(got, "0xb01dfacedebac1e") {
646 // See the comment in signal_darwin_amd64.go.
647 t.Skip("skipping due to Darwin handling of malformed addresses")
649 t.Errorf("unexpectedly saw %q in output", nowant)
656 func TestAbortInCgo(t *testing.T) {
657 switch runtime.GOOS {
658 case "plan9", "windows":
659 // N.B. On Windows, C abort() causes the program to exit
660 // without going through the runtime at all.
661 t.Skipf("no signals on %s", runtime.GOOS)
665 got := runTestProg(t, "testprogcgo", "Abort")
668 if !strings.Contains(got, want) {
669 t.Errorf("did not see %q in output", want)
671 // No runtime errors like "runtime: unknown pc".
672 nowant := "runtime: "
673 if strings.Contains(got, nowant) {
674 t.Errorf("did not see %q in output", want)
678 // TestEINTR tests that we handle EINTR correctly.
679 // See issue #20400 and friends.
680 func TestEINTR(t *testing.T) {
681 switch runtime.GOOS {
682 case "plan9", "windows":
683 t.Skipf("no EINTR on %s", runtime.GOOS)
685 if runtime.GOARCH == "386" {
686 // On linux-386 the Go signal handler sets
687 // a restorer function that is not preserved
688 // by the C sigaction call in the test,
689 // causing the signal handler to crash when
690 // returning the normal code. The test is not
691 // architecture-specific, so just skip on 386
692 // rather than doing a complicated workaround.
693 t.Skip("skipping on linux-386; C sigaction does not preserve Go restorer")
698 output := runTestProg(t, "testprogcgo", "EINTR")
701 t.Fatalf("want %s, got %s\n", want, output)
706 func TestNeedmDeadlock(t *testing.T) {
707 switch runtime.GOOS {
708 case "plan9", "windows":
709 t.Skipf("no signals on %s", runtime.GOOS)
711 output := runTestProg(t, "testprogcgo", "NeedmDeadlock")
714 t.Fatalf("want %s, got %s\n", want, output)
718 func TestCgoTracebackGoroutineProfile(t *testing.T) {
719 output := runTestProg(t, "testprogcgo", "GoroutineProfile")
722 t.Fatalf("want %s, got %s\n", want, output)
726 func TestCgoTraceParser(t *testing.T) {
728 switch runtime.GOOS {
729 case "plan9", "windows":
730 t.Skipf("no pthreads on %s", runtime.GOOS)
732 output := runTestProg(t, "testprogcgo", "CgoTraceParser")
734 ErrTimeOrder := "ErrTimeOrder\n"
735 if output == ErrTimeOrder {
736 t.Skipf("skipping due to golang.org/issue/16755: %v", output)
737 } else if output != want {
738 t.Fatalf("want %s, got %s\n", want, output)
742 func TestCgoTraceParserWithOneProc(t *testing.T) {
744 switch runtime.GOOS {
745 case "plan9", "windows":
746 t.Skipf("no pthreads on %s", runtime.GOOS)
748 output := runTestProg(t, "testprogcgo", "CgoTraceParser", "GOMAXPROCS=1")
750 ErrTimeOrder := "ErrTimeOrder\n"
751 if output == ErrTimeOrder {
752 t.Skipf("skipping due to golang.org/issue/16755: %v", output)
753 } else if output != want {
754 t.Fatalf("GOMAXPROCS=1, want %s, got %s\n", want, output)
758 func TestCgoSigfwd(t *testing.T) {
760 if goos.IsLinux == 0 {
761 t.Skipf("only supported on Linux")
764 got := runTestProg(t, "testprogcgo", "CgoSigfwd", "GO_TEST_CGOSIGFWD=1")
765 if want := "OK\n"; got != want {
766 t.Fatalf("expected %q, but got:\n%s", want, got)