github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/runtime/crash_cgo_test.go (about)

     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  //go:build cgo
     6  
     7  package runtime_test
     8  
     9  import (
    10  	"fmt"
    11  	"internal/goos"
    12  	"internal/platform"
    13  	"internal/testenv"
    14  	"os"
    15  	"os/exec"
    16  	"runtime"
    17  	"strconv"
    18  	"strings"
    19  	"testing"
    20  	"time"
    21  )
    22  
    23  func TestCgoCrashHandler(t *testing.T) {
    24  	t.Parallel()
    25  	testCrashHandler(t, true)
    26  }
    27  
    28  func TestCgoSignalDeadlock(t *testing.T) {
    29  	// Don't call t.Parallel, since too much work going on at the
    30  	// same time can cause the testprogcgo code to overrun its
    31  	// timeouts (issue #18598).
    32  
    33  	if testing.Short() && runtime.GOOS == "windows" {
    34  		t.Skip("Skipping in short mode") // takes up to 64 seconds
    35  	}
    36  	got := runTestProg(t, "testprogcgo", "CgoSignalDeadlock")
    37  	want := "OK\n"
    38  	if got != want {
    39  		t.Fatalf("expected %q, but got:\n%s", want, got)
    40  	}
    41  }
    42  
    43  func TestCgoTraceback(t *testing.T) {
    44  	t.Parallel()
    45  	got := runTestProg(t, "testprogcgo", "CgoTraceback")
    46  	want := "OK\n"
    47  	if got != want {
    48  		t.Fatalf("expected %q, but got:\n%s", want, got)
    49  	}
    50  }
    51  
    52  func TestCgoCallbackGC(t *testing.T) {
    53  	t.Parallel()
    54  	switch runtime.GOOS {
    55  	case "plan9", "windows":
    56  		t.Skipf("no pthreads on %s", runtime.GOOS)
    57  	}
    58  	if testing.Short() {
    59  		switch {
    60  		case runtime.GOOS == "dragonfly":
    61  			t.Skip("see golang.org/issue/11990")
    62  		case runtime.GOOS == "linux" && runtime.GOARCH == "arm":
    63  			t.Skip("too slow for arm builders")
    64  		case runtime.GOOS == "linux" && (runtime.GOARCH == "mips64" || runtime.GOARCH == "mips64le"):
    65  			t.Skip("too slow for mips64x builders")
    66  		}
    67  	}
    68  	if testenv.Builder() == "darwin-amd64-10_14" {
    69  		// TODO(#23011): When the 10.14 builders are gone, remove this skip.
    70  		t.Skip("skipping due to platform bug on macOS 10.14; see https://golang.org/issue/43926")
    71  	}
    72  	got := runTestProg(t, "testprogcgo", "CgoCallbackGC")
    73  	want := "OK\n"
    74  	if got != want {
    75  		t.Fatalf("expected %q, but got:\n%s", want, got)
    76  	}
    77  }
    78  
    79  func TestCgoExternalThreadPanic(t *testing.T) {
    80  	t.Parallel()
    81  	if runtime.GOOS == "plan9" {
    82  		t.Skipf("no pthreads on %s", runtime.GOOS)
    83  	}
    84  	got := runTestProg(t, "testprogcgo", "CgoExternalThreadPanic")
    85  	want := "panic: BOOM"
    86  	if !strings.Contains(got, want) {
    87  		t.Fatalf("want failure containing %q. output:\n%s\n", want, got)
    88  	}
    89  }
    90  
    91  func TestCgoExternalThreadSIGPROF(t *testing.T) {
    92  	t.Parallel()
    93  	// issue 9456.
    94  	switch runtime.GOOS {
    95  	case "plan9", "windows":
    96  		t.Skipf("no pthreads on %s", runtime.GOOS)
    97  	}
    98  
    99  	got := runTestProg(t, "testprogcgo", "CgoExternalThreadSIGPROF", "GO_START_SIGPROF_THREAD=1")
   100  	if want := "OK\n"; got != want {
   101  		t.Fatalf("expected %q, but got:\n%s", want, got)
   102  	}
   103  }
   104  
   105  func TestCgoExternalThreadSignal(t *testing.T) {
   106  	t.Parallel()
   107  	// issue 10139
   108  	switch runtime.GOOS {
   109  	case "plan9", "windows":
   110  		t.Skipf("no pthreads on %s", runtime.GOOS)
   111  	}
   112  
   113  	got := runTestProg(t, "testprogcgo", "CgoExternalThreadSignal")
   114  	if want := "OK\n"; got != want {
   115  		if runtime.GOOS == "ios" && strings.Contains(got, "C signal did not crash as expected") {
   116  			testenv.SkipFlaky(t, 59913)
   117  		}
   118  		t.Fatalf("expected %q, but got:\n%s", want, got)
   119  	}
   120  }
   121  
   122  func TestCgoDLLImports(t *testing.T) {
   123  	// test issue 9356
   124  	if runtime.GOOS != "windows" {
   125  		t.Skip("skipping windows specific test")
   126  	}
   127  	got := runTestProg(t, "testprogcgo", "CgoDLLImportsMain")
   128  	want := "OK\n"
   129  	if got != want {
   130  		t.Fatalf("expected %q, but got %v", want, got)
   131  	}
   132  }
   133  
   134  func TestCgoExecSignalMask(t *testing.T) {
   135  	t.Parallel()
   136  	// Test issue 13164.
   137  	switch runtime.GOOS {
   138  	case "windows", "plan9":
   139  		t.Skipf("skipping signal mask test on %s", runtime.GOOS)
   140  	}
   141  	got := runTestProg(t, "testprogcgo", "CgoExecSignalMask", "GOTRACEBACK=system")
   142  	want := "OK\n"
   143  	if got != want {
   144  		t.Errorf("expected %q, got %v", want, got)
   145  	}
   146  }
   147  
   148  func TestEnsureDropM(t *testing.T) {
   149  	t.Parallel()
   150  	// Test for issue 13881.
   151  	switch runtime.GOOS {
   152  	case "windows", "plan9":
   153  		t.Skipf("skipping dropm test on %s", runtime.GOOS)
   154  	}
   155  	got := runTestProg(t, "testprogcgo", "EnsureDropM")
   156  	want := "OK\n"
   157  	if got != want {
   158  		t.Errorf("expected %q, got %v", want, got)
   159  	}
   160  }
   161  
   162  // Test for issue 14387.
   163  // Test that the program that doesn't need any cgo pointer checking
   164  // takes about the same amount of time with it as without it.
   165  func TestCgoCheckBytes(t *testing.T) {
   166  	t.Parallel()
   167  	// Make sure we don't count the build time as part of the run time.
   168  	testenv.MustHaveGoBuild(t)
   169  	exe, err := buildTestProg(t, "testprogcgo")
   170  	if err != nil {
   171  		t.Fatal(err)
   172  	}
   173  
   174  	// Try it 10 times to avoid flakiness.
   175  	const tries = 10
   176  	var tot1, tot2 time.Duration
   177  	for i := 0; i < tries; i++ {
   178  		cmd := testenv.CleanCmdEnv(exec.Command(exe, "CgoCheckBytes"))
   179  		cmd.Env = append(cmd.Env, "GODEBUG=cgocheck=0", fmt.Sprintf("GO_CGOCHECKBYTES_TRY=%d", i))
   180  
   181  		start := time.Now()
   182  		cmd.Run()
   183  		d1 := time.Since(start)
   184  
   185  		cmd = testenv.CleanCmdEnv(exec.Command(exe, "CgoCheckBytes"))
   186  		cmd.Env = append(cmd.Env, fmt.Sprintf("GO_CGOCHECKBYTES_TRY=%d", i))
   187  
   188  		start = time.Now()
   189  		cmd.Run()
   190  		d2 := time.Since(start)
   191  
   192  		if d1*20 > d2 {
   193  			// The slow version (d2) was less than 20 times
   194  			// slower than the fast version (d1), so OK.
   195  			return
   196  		}
   197  
   198  		tot1 += d1
   199  		tot2 += d2
   200  	}
   201  
   202  	t.Errorf("cgo check too slow: got %v, expected at most %v", tot2/tries, (tot1/tries)*20)
   203  }
   204  
   205  func TestCgoPanicDeadlock(t *testing.T) {
   206  	t.Parallel()
   207  	// test issue 14432
   208  	got := runTestProg(t, "testprogcgo", "CgoPanicDeadlock")
   209  	want := "panic: cgo error\n\n"
   210  	if !strings.HasPrefix(got, want) {
   211  		t.Fatalf("output does not start with %q:\n%s", want, got)
   212  	}
   213  }
   214  
   215  func TestCgoCCodeSIGPROF(t *testing.T) {
   216  	t.Parallel()
   217  	got := runTestProg(t, "testprogcgo", "CgoCCodeSIGPROF")
   218  	want := "OK\n"
   219  	if got != want {
   220  		t.Errorf("expected %q got %v", want, got)
   221  	}
   222  }
   223  
   224  func TestCgoPprofCallback(t *testing.T) {
   225  	if testing.Short() {
   226  		t.Skip("skipping in short mode") // takes a full second
   227  	}
   228  	switch runtime.GOOS {
   229  	case "windows", "plan9":
   230  		t.Skipf("skipping cgo pprof callback test on %s", runtime.GOOS)
   231  	}
   232  	got := runTestProg(t, "testprogcgo", "CgoPprofCallback")
   233  	want := "OK\n"
   234  	if got != want {
   235  		t.Errorf("expected %q got %v", want, got)
   236  	}
   237  }
   238  
   239  func TestCgoCrashTraceback(t *testing.T) {
   240  	t.Parallel()
   241  	switch platform := runtime.GOOS + "/" + runtime.GOARCH; platform {
   242  	case "darwin/amd64":
   243  	case "linux/amd64":
   244  	case "linux/arm64":
   245  	case "linux/ppc64le":
   246  	default:
   247  		t.Skipf("not yet supported on %s", platform)
   248  	}
   249  	got := runTestProg(t, "testprogcgo", "CrashTraceback")
   250  	for i := 1; i <= 3; i++ {
   251  		if !strings.Contains(got, fmt.Sprintf("cgo symbolizer:%d", i)) {
   252  			t.Errorf("missing cgo symbolizer:%d", i)
   253  		}
   254  	}
   255  }
   256  
   257  func TestCgoCrashTracebackGo(t *testing.T) {
   258  	t.Parallel()
   259  	switch platform := runtime.GOOS + "/" + runtime.GOARCH; platform {
   260  	case "darwin/amd64":
   261  	case "linux/amd64":
   262  	case "linux/arm64":
   263  	case "linux/ppc64le":
   264  	default:
   265  		t.Skipf("not yet supported on %s", platform)
   266  	}
   267  	got := runTestProg(t, "testprogcgo", "CrashTracebackGo")
   268  	for i := 1; i <= 3; i++ {
   269  		want := fmt.Sprintf("main.h%d", i)
   270  		if !strings.Contains(got, want) {
   271  			t.Errorf("missing %s", want)
   272  		}
   273  	}
   274  }
   275  
   276  func TestCgoTracebackContext(t *testing.T) {
   277  	t.Parallel()
   278  	got := runTestProg(t, "testprogcgo", "TracebackContext")
   279  	want := "OK\n"
   280  	if got != want {
   281  		t.Errorf("expected %q got %v", want, got)
   282  	}
   283  }
   284  
   285  func TestCgoTracebackContextPreemption(t *testing.T) {
   286  	t.Parallel()
   287  	got := runTestProg(t, "testprogcgo", "TracebackContextPreemption")
   288  	want := "OK\n"
   289  	if got != want {
   290  		t.Errorf("expected %q got %v", want, got)
   291  	}
   292  }
   293  
   294  func testCgoPprof(t *testing.T, buildArg, runArg, top, bottom string) {
   295  	t.Parallel()
   296  	if runtime.GOOS != "linux" || (runtime.GOARCH != "amd64" && runtime.GOARCH != "ppc64le" && runtime.GOARCH != "arm64") {
   297  		t.Skipf("not yet supported on %s/%s", runtime.GOOS, runtime.GOARCH)
   298  	}
   299  	testenv.MustHaveGoRun(t)
   300  
   301  	exe, err := buildTestProg(t, "testprogcgo", buildArg)
   302  	if err != nil {
   303  		t.Fatal(err)
   304  	}
   305  
   306  	cmd := testenv.CleanCmdEnv(exec.Command(exe, runArg))
   307  	got, err := cmd.CombinedOutput()
   308  	if err != nil {
   309  		if testenv.Builder() == "linux-amd64-alpine" {
   310  			// See Issue 18243 and Issue 19938.
   311  			t.Skipf("Skipping failing test on Alpine (golang.org/issue/18243). Ignoring error: %v", err)
   312  		}
   313  		t.Fatalf("%s\n\n%v", got, err)
   314  	}
   315  	fn := strings.TrimSpace(string(got))
   316  	defer os.Remove(fn)
   317  
   318  	for try := 0; try < 2; try++ {
   319  		cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "tool", "pprof", "-tagignore=ignore", "-traces"))
   320  		// Check that pprof works both with and without explicit executable on command line.
   321  		if try == 0 {
   322  			cmd.Args = append(cmd.Args, exe, fn)
   323  		} else {
   324  			cmd.Args = append(cmd.Args, fn)
   325  		}
   326  
   327  		found := false
   328  		for i, e := range cmd.Env {
   329  			if strings.HasPrefix(e, "PPROF_TMPDIR=") {
   330  				cmd.Env[i] = "PPROF_TMPDIR=" + os.TempDir()
   331  				found = true
   332  				break
   333  			}
   334  		}
   335  		if !found {
   336  			cmd.Env = append(cmd.Env, "PPROF_TMPDIR="+os.TempDir())
   337  		}
   338  
   339  		out, err := cmd.CombinedOutput()
   340  		t.Logf("%s:\n%s", cmd.Args, out)
   341  		if err != nil {
   342  			t.Error(err)
   343  			continue
   344  		}
   345  
   346  		trace := findTrace(string(out), top)
   347  		if len(trace) == 0 {
   348  			t.Errorf("%s traceback missing.", top)
   349  			continue
   350  		}
   351  		if trace[len(trace)-1] != bottom {
   352  			t.Errorf("invalid traceback origin: got=%v; want=[%s ... %s]", trace, top, bottom)
   353  		}
   354  	}
   355  }
   356  
   357  func TestCgoPprof(t *testing.T) {
   358  	testCgoPprof(t, "", "CgoPprof", "cpuHog", "runtime.main")
   359  }
   360  
   361  func TestCgoPprofPIE(t *testing.T) {
   362  	testCgoPprof(t, "-buildmode=pie", "CgoPprof", "cpuHog", "runtime.main")
   363  }
   364  
   365  func TestCgoPprofThread(t *testing.T) {
   366  	testCgoPprof(t, "", "CgoPprofThread", "cpuHogThread", "cpuHogThread2")
   367  }
   368  
   369  func TestCgoPprofThreadNoTraceback(t *testing.T) {
   370  	testCgoPprof(t, "", "CgoPprofThreadNoTraceback", "cpuHogThread", "runtime._ExternalCode")
   371  }
   372  
   373  func TestRaceProf(t *testing.T) {
   374  	if !platform.RaceDetectorSupported(runtime.GOOS, runtime.GOARCH) {
   375  		t.Skipf("skipping on %s/%s because race detector not supported", runtime.GOOS, runtime.GOARCH)
   376  	}
   377  	if runtime.GOOS == "windows" {
   378  		t.Skipf("skipping: test requires pthread support")
   379  		// TODO: Can this test be rewritten to use the C11 thread API instead?
   380  	}
   381  
   382  	testenv.MustHaveGoRun(t)
   383  
   384  	// This test requires building various packages with -race, so
   385  	// it's somewhat slow.
   386  	if testing.Short() {
   387  		t.Skip("skipping test in -short mode")
   388  	}
   389  
   390  	exe, err := buildTestProg(t, "testprogcgo", "-race")
   391  	if err != nil {
   392  		t.Fatal(err)
   393  	}
   394  
   395  	got, err := testenv.CleanCmdEnv(exec.Command(exe, "CgoRaceprof")).CombinedOutput()
   396  	if err != nil {
   397  		t.Fatal(err)
   398  	}
   399  	want := "OK\n"
   400  	if string(got) != want {
   401  		t.Errorf("expected %q got %s", want, got)
   402  	}
   403  }
   404  
   405  func TestRaceSignal(t *testing.T) {
   406  	if !platform.RaceDetectorSupported(runtime.GOOS, runtime.GOARCH) {
   407  		t.Skipf("skipping on %s/%s because race detector not supported", runtime.GOOS, runtime.GOARCH)
   408  	}
   409  	if runtime.GOOS == "windows" {
   410  		t.Skipf("skipping: test requires pthread support")
   411  		// TODO: Can this test be rewritten to use the C11 thread API instead?
   412  	}
   413  
   414  	t.Parallel()
   415  
   416  	testenv.MustHaveGoRun(t)
   417  
   418  	// This test requires building various packages with -race, so
   419  	// it's somewhat slow.
   420  	if testing.Short() {
   421  		t.Skip("skipping test in -short mode")
   422  	}
   423  
   424  	exe, err := buildTestProg(t, "testprogcgo", "-race")
   425  	if err != nil {
   426  		t.Fatal(err)
   427  	}
   428  
   429  	got, err := testenv.CleanCmdEnv(testenv.Command(t, exe, "CgoRaceSignal")).CombinedOutput()
   430  	if err != nil {
   431  		t.Logf("%s\n", got)
   432  		t.Fatal(err)
   433  	}
   434  	want := "OK\n"
   435  	if string(got) != want {
   436  		t.Errorf("expected %q got %s", want, got)
   437  	}
   438  }
   439  
   440  func TestCgoNumGoroutine(t *testing.T) {
   441  	switch runtime.GOOS {
   442  	case "windows", "plan9":
   443  		t.Skipf("skipping numgoroutine test on %s", runtime.GOOS)
   444  	}
   445  	t.Parallel()
   446  	got := runTestProg(t, "testprogcgo", "NumGoroutine")
   447  	want := "OK\n"
   448  	if got != want {
   449  		t.Errorf("expected %q got %v", want, got)
   450  	}
   451  }
   452  
   453  func TestCatchPanic(t *testing.T) {
   454  	t.Parallel()
   455  	switch runtime.GOOS {
   456  	case "plan9", "windows":
   457  		t.Skipf("no signals on %s", runtime.GOOS)
   458  	case "darwin":
   459  		if runtime.GOARCH == "amd64" {
   460  			t.Skipf("crash() on darwin/amd64 doesn't raise SIGABRT")
   461  		}
   462  	}
   463  
   464  	testenv.MustHaveGoRun(t)
   465  
   466  	exe, err := buildTestProg(t, "testprogcgo")
   467  	if err != nil {
   468  		t.Fatal(err)
   469  	}
   470  
   471  	for _, early := range []bool{true, false} {
   472  		cmd := testenv.CleanCmdEnv(exec.Command(exe, "CgoCatchPanic"))
   473  		// Make sure a panic results in a crash.
   474  		cmd.Env = append(cmd.Env, "GOTRACEBACK=crash")
   475  		if early {
   476  			// Tell testprogcgo to install an early signal handler for SIGABRT
   477  			cmd.Env = append(cmd.Env, "CGOCATCHPANIC_EARLY_HANDLER=1")
   478  		}
   479  		if out, err := cmd.CombinedOutput(); err != nil {
   480  			t.Errorf("testprogcgo CgoCatchPanic failed: %v\n%s", err, out)
   481  		}
   482  	}
   483  }
   484  
   485  func TestCgoLockOSThreadExit(t *testing.T) {
   486  	switch runtime.GOOS {
   487  	case "plan9", "windows":
   488  		t.Skipf("no pthreads on %s", runtime.GOOS)
   489  	}
   490  	t.Parallel()
   491  	testLockOSThreadExit(t, "testprogcgo")
   492  }
   493  
   494  func TestWindowsStackMemoryCgo(t *testing.T) {
   495  	if runtime.GOOS != "windows" {
   496  		t.Skip("skipping windows specific test")
   497  	}
   498  	testenv.SkipFlaky(t, 22575)
   499  	o := runTestProg(t, "testprogcgo", "StackMemory")
   500  	stackUsage, err := strconv.Atoi(o)
   501  	if err != nil {
   502  		t.Fatalf("Failed to read stack usage: %v", err)
   503  	}
   504  	if expected, got := 100<<10, stackUsage; got > expected {
   505  		t.Fatalf("expected < %d bytes of memory per thread, got %d", expected, got)
   506  	}
   507  }
   508  
   509  func TestSigStackSwapping(t *testing.T) {
   510  	switch runtime.GOOS {
   511  	case "plan9", "windows":
   512  		t.Skipf("no sigaltstack on %s", runtime.GOOS)
   513  	}
   514  	t.Parallel()
   515  	got := runTestProg(t, "testprogcgo", "SigStack")
   516  	want := "OK\n"
   517  	if got != want {
   518  		t.Errorf("expected %q got %v", want, got)
   519  	}
   520  }
   521  
   522  func TestCgoTracebackSigpanic(t *testing.T) {
   523  	// Test unwinding over a sigpanic in C code without a C
   524  	// symbolizer. See issue #23576.
   525  	if runtime.GOOS == "windows" {
   526  		// On Windows if we get an exception in C code, we let
   527  		// the Windows exception handler unwind it, rather
   528  		// than injecting a sigpanic.
   529  		t.Skip("no sigpanic in C on windows")
   530  	}
   531  	if runtime.GOOS == "ios" {
   532  		testenv.SkipFlaky(t, 59912)
   533  	}
   534  	t.Parallel()
   535  	got := runTestProg(t, "testprogcgo", "TracebackSigpanic")
   536  	t.Log(got)
   537  	// We should see the function that calls the C function.
   538  	want := "main.TracebackSigpanic"
   539  	if !strings.Contains(got, want) {
   540  		if runtime.GOOS == "android" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") {
   541  			testenv.SkipFlaky(t, 58794)
   542  		}
   543  		t.Errorf("did not see %q in output", want)
   544  	}
   545  	// We shouldn't inject a sigpanic call. (see issue 57698)
   546  	nowant := "runtime.sigpanic"
   547  	if strings.Contains(got, nowant) {
   548  		t.Errorf("unexpectedly saw %q in output", nowant)
   549  	}
   550  	// No runtime errors like "runtime: unexpected return pc".
   551  	nowant = "runtime: "
   552  	if strings.Contains(got, nowant) {
   553  		t.Errorf("unexpectedly saw %q in output", nowant)
   554  	}
   555  }
   556  
   557  func TestCgoPanicCallback(t *testing.T) {
   558  	t.Parallel()
   559  	got := runTestProg(t, "testprogcgo", "PanicCallback")
   560  	t.Log(got)
   561  	want := "panic: runtime error: invalid memory address or nil pointer dereference"
   562  	if !strings.Contains(got, want) {
   563  		t.Errorf("did not see %q in output", want)
   564  	}
   565  	want = "panic_callback"
   566  	if !strings.Contains(got, want) {
   567  		t.Errorf("did not see %q in output", want)
   568  	}
   569  	want = "PanicCallback"
   570  	if !strings.Contains(got, want) {
   571  		t.Errorf("did not see %q in output", want)
   572  	}
   573  	// No runtime errors like "runtime: unexpected return pc".
   574  	nowant := "runtime: "
   575  	if strings.Contains(got, nowant) {
   576  		t.Errorf("did not see %q in output", want)
   577  	}
   578  }
   579  
   580  // Test that C code called via cgo can use large Windows thread stacks
   581  // and call back in to Go without crashing. See issue #20975.
   582  //
   583  // See also TestBigStackCallbackSyscall.
   584  func TestBigStackCallbackCgo(t *testing.T) {
   585  	if runtime.GOOS != "windows" {
   586  		t.Skip("skipping windows specific test")
   587  	}
   588  	t.Parallel()
   589  	got := runTestProg(t, "testprogcgo", "BigStack")
   590  	want := "OK\n"
   591  	if got != want {
   592  		t.Errorf("expected %q got %v", want, got)
   593  	}
   594  }
   595  
   596  func nextTrace(lines []string) ([]string, []string) {
   597  	var trace []string
   598  	for n, line := range lines {
   599  		if strings.HasPrefix(line, "---") {
   600  			return trace, lines[n+1:]
   601  		}
   602  		fields := strings.Fields(strings.TrimSpace(line))
   603  		if len(fields) == 0 {
   604  			continue
   605  		}
   606  		// Last field contains the function name.
   607  		trace = append(trace, fields[len(fields)-1])
   608  	}
   609  	return nil, nil
   610  }
   611  
   612  func findTrace(text, top string) []string {
   613  	lines := strings.Split(text, "\n")
   614  	_, lines = nextTrace(lines) // Skip the header.
   615  	for len(lines) > 0 {
   616  		var t []string
   617  		t, lines = nextTrace(lines)
   618  		if len(t) == 0 {
   619  			continue
   620  		}
   621  		if t[0] == top {
   622  			return t
   623  		}
   624  	}
   625  	return nil
   626  }
   627  
   628  func TestSegv(t *testing.T) {
   629  	switch runtime.GOOS {
   630  	case "plan9", "windows":
   631  		t.Skipf("no signals on %s", runtime.GOOS)
   632  	}
   633  
   634  	for _, test := range []string{"Segv", "SegvInCgo", "TgkillSegv", "TgkillSegvInCgo"} {
   635  		test := test
   636  
   637  		// The tgkill variants only run on Linux.
   638  		if runtime.GOOS != "linux" && strings.HasPrefix(test, "Tgkill") {
   639  			continue
   640  		}
   641  
   642  		t.Run(test, func(t *testing.T) {
   643  			if test == "SegvInCgo" && runtime.GOOS == "ios" {
   644  				testenv.SkipFlaky(t, 59947) // Don't even try, in case it times out.
   645  			}
   646  
   647  			t.Parallel()
   648  			got := runTestProg(t, "testprogcgo", test)
   649  			t.Log(got)
   650  			want := "SIGSEGV"
   651  			if !strings.Contains(got, want) {
   652  				if runtime.GOOS == "darwin" && runtime.GOARCH == "amd64" && strings.Contains(got, "fatal: morestack on g0") {
   653  					testenv.SkipFlaky(t, 39457)
   654  				}
   655  				t.Errorf("did not see %q in output", want)
   656  			}
   657  
   658  			// No runtime errors like "runtime: unknown pc".
   659  			switch runtime.GOOS {
   660  			case "darwin", "ios", "illumos", "solaris":
   661  				// Runtime sometimes throws when generating the traceback.
   662  				testenv.SkipFlaky(t, 49182)
   663  			case "linux":
   664  				if runtime.GOARCH == "386" {
   665  					// Runtime throws when generating a traceback from
   666  					// a VDSO call via asmcgocall.
   667  					testenv.SkipFlaky(t, 50504)
   668  				}
   669  			}
   670  			if test == "SegvInCgo" && strings.Contains(got, "unknown pc") {
   671  				testenv.SkipFlaky(t, 50979)
   672  			}
   673  
   674  			for _, nowant := range []string{"fatal error: ", "runtime: "} {
   675  				if strings.Contains(got, nowant) {
   676  					if runtime.GOOS == "darwin" && strings.Contains(got, "0xb01dfacedebac1e") {
   677  						// See the comment in signal_darwin_amd64.go.
   678  						t.Skip("skipping due to Darwin handling of malformed addresses")
   679  					}
   680  					t.Errorf("unexpectedly saw %q in output", nowant)
   681  				}
   682  			}
   683  		})
   684  	}
   685  }
   686  
   687  func TestAbortInCgo(t *testing.T) {
   688  	switch runtime.GOOS {
   689  	case "plan9", "windows":
   690  		// N.B. On Windows, C abort() causes the program to exit
   691  		// without going through the runtime at all.
   692  		t.Skipf("no signals on %s", runtime.GOOS)
   693  	}
   694  
   695  	t.Parallel()
   696  	got := runTestProg(t, "testprogcgo", "Abort")
   697  	t.Log(got)
   698  	want := "SIGABRT"
   699  	if !strings.Contains(got, want) {
   700  		t.Errorf("did not see %q in output", want)
   701  	}
   702  	// No runtime errors like "runtime: unknown pc".
   703  	nowant := "runtime: "
   704  	if strings.Contains(got, nowant) {
   705  		t.Errorf("did not see %q in output", want)
   706  	}
   707  }
   708  
   709  // TestEINTR tests that we handle EINTR correctly.
   710  // See issue #20400 and friends.
   711  func TestEINTR(t *testing.T) {
   712  	switch runtime.GOOS {
   713  	case "plan9", "windows":
   714  		t.Skipf("no EINTR on %s", runtime.GOOS)
   715  	case "linux":
   716  		if runtime.GOARCH == "386" {
   717  			// On linux-386 the Go signal handler sets
   718  			// a restorer function that is not preserved
   719  			// by the C sigaction call in the test,
   720  			// causing the signal handler to crash when
   721  			// returning the normal code. The test is not
   722  			// architecture-specific, so just skip on 386
   723  			// rather than doing a complicated workaround.
   724  			t.Skip("skipping on linux-386; C sigaction does not preserve Go restorer")
   725  		}
   726  	}
   727  
   728  	t.Parallel()
   729  	output := runTestProg(t, "testprogcgo", "EINTR")
   730  	want := "OK\n"
   731  	if output != want {
   732  		t.Fatalf("want %s, got %s\n", want, output)
   733  	}
   734  }
   735  
   736  // Issue #42207.
   737  func TestNeedmDeadlock(t *testing.T) {
   738  	switch runtime.GOOS {
   739  	case "plan9", "windows":
   740  		t.Skipf("no signals on %s", runtime.GOOS)
   741  	}
   742  	output := runTestProg(t, "testprogcgo", "NeedmDeadlock")
   743  	want := "OK\n"
   744  	if output != want {
   745  		t.Fatalf("want %s, got %s\n", want, output)
   746  	}
   747  }
   748  
   749  func TestCgoTracebackGoroutineProfile(t *testing.T) {
   750  	output := runTestProg(t, "testprogcgo", "GoroutineProfile")
   751  	want := "OK\n"
   752  	if output != want {
   753  		t.Fatalf("want %s, got %s\n", want, output)
   754  	}
   755  }
   756  
   757  func TestCgoTraceParser(t *testing.T) {
   758  	// Test issue 29707.
   759  	switch runtime.GOOS {
   760  	case "plan9", "windows":
   761  		t.Skipf("no pthreads on %s", runtime.GOOS)
   762  	}
   763  	output := runTestProg(t, "testprogcgo", "CgoTraceParser")
   764  	want := "OK\n"
   765  	ErrTimeOrder := "ErrTimeOrder\n"
   766  	if output == ErrTimeOrder {
   767  		t.Skipf("skipping due to golang.org/issue/16755: %v", output)
   768  	} else if output != want {
   769  		t.Fatalf("want %s, got %s\n", want, output)
   770  	}
   771  }
   772  
   773  func TestCgoTraceParserWithOneProc(t *testing.T) {
   774  	// Test issue 29707.
   775  	switch runtime.GOOS {
   776  	case "plan9", "windows":
   777  		t.Skipf("no pthreads on %s", runtime.GOOS)
   778  	}
   779  	output := runTestProg(t, "testprogcgo", "CgoTraceParser", "GOMAXPROCS=1")
   780  	want := "OK\n"
   781  	ErrTimeOrder := "ErrTimeOrder\n"
   782  	if output == ErrTimeOrder {
   783  		t.Skipf("skipping due to golang.org/issue/16755: %v", output)
   784  	} else if output != want {
   785  		t.Fatalf("GOMAXPROCS=1, want %s, got %s\n", want, output)
   786  	}
   787  }
   788  
   789  func TestCgoSigfwd(t *testing.T) {
   790  	t.Parallel()
   791  	if !goos.IsUnix {
   792  		t.Skipf("no signals on %s", runtime.GOOS)
   793  	}
   794  
   795  	got := runTestProg(t, "testprogcgo", "CgoSigfwd", "GO_TEST_CGOSIGFWD=1")
   796  	if want := "OK\n"; got != want {
   797  		t.Fatalf("expected %q, but got:\n%s", want, got)
   798  	}
   799  }
   800  
   801  func TestDestructorCallback(t *testing.T) {
   802  	t.Parallel()
   803  	got := runTestProg(t, "testprogcgo", "DestructorCallback")
   804  	if want := "OK\n"; got != want {
   805  		t.Errorf("expected %q, but got:\n%s", want, got)
   806  	}
   807  }
   808  
   809  func TestDestructorCallbackRace(t *testing.T) {
   810  	// This test requires building with -race,
   811  	// so it's somewhat slow.
   812  	if testing.Short() {
   813  		t.Skip("skipping test in -short mode")
   814  	}
   815  
   816  	if !platform.RaceDetectorSupported(runtime.GOOS, runtime.GOARCH) {
   817  		t.Skipf("skipping on %s/%s because race detector not supported", runtime.GOOS, runtime.GOARCH)
   818  	}
   819  
   820  	t.Parallel()
   821  
   822  	exe, err := buildTestProg(t, "testprogcgo", "-race")
   823  	if err != nil {
   824  		t.Fatal(err)
   825  	}
   826  
   827  	got, err := testenv.CleanCmdEnv(exec.Command(exe, "DestructorCallback")).CombinedOutput()
   828  	if err != nil {
   829  		t.Fatal(err)
   830  	}
   831  
   832  	if want := "OK\n"; string(got) != want {
   833  		t.Errorf("expected %q, but got:\n%s", want, got)
   834  	}
   835  }