* builtins.def (BUILT_IN_SETJMP): Revert latest change.
[official-gcc.git] / libgo / go / runtime / crash_cgo_test.go
blobb79873185cc809b82b4300e7a0c2c9d1db885979
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.
5 // +build cgo
7 package runtime_test
9 import (
10 "bytes"
11 "fmt"
12 "internal/testenv"
13 "os"
14 "os/exec"
15 "runtime"
16 "strings"
17 "testing"
18 "time"
21 func TestCgoCrashHandler(t *testing.T) {
22 t.Parallel()
23 testCrashHandler(t, true)
26 func TestCgoSignalDeadlock(t *testing.T) {
27 // Don't call t.Parallel, since too much work going on at the
28 // same time can cause the testprogcgo code to overrun its
29 // timeouts (issue #18598).
31 if testing.Short() && runtime.GOOS == "windows" {
32 t.Skip("Skipping in short mode") // takes up to 64 seconds
34 got := runTestProg(t, "testprogcgo", "CgoSignalDeadlock")
35 want := "OK\n"
36 if got != want {
37 t.Fatalf("expected %q, but got:\n%s", want, got)
41 func TestCgoTraceback(t *testing.T) {
42 t.Parallel()
43 got := runTestProg(t, "testprogcgo", "CgoTraceback")
44 want := "OK\n"
45 if got != want {
46 t.Fatalf("expected %q, but got:\n%s", want, got)
50 func TestCgoCallbackGC(t *testing.T) {
51 t.Parallel()
52 switch runtime.GOOS {
53 case "plan9", "windows":
54 t.Skipf("no pthreads on %s", runtime.GOOS)
56 if testing.Short() {
57 switch {
58 case runtime.GOOS == "dragonfly":
59 t.Skip("see golang.org/issue/11990")
60 case runtime.GOOS == "linux" && runtime.GOARCH == "arm":
61 t.Skip("too slow for arm builders")
62 case runtime.GOOS == "linux" && (runtime.GOARCH == "mips64" || runtime.GOARCH == "mips64le"):
63 t.Skip("too slow for mips64x builders")
66 got := runTestProg(t, "testprogcgo", "CgoCallbackGC")
67 want := "OK\n"
68 if got != want {
69 t.Fatalf("expected %q, but got:\n%s", want, got)
73 func TestCgoExternalThreadPanic(t *testing.T) {
74 t.Parallel()
75 if runtime.GOOS == "plan9" {
76 t.Skipf("no pthreads on %s", runtime.GOOS)
78 got := runTestProg(t, "testprogcgo", "CgoExternalThreadPanic")
79 want := "panic: BOOM"
80 if !strings.Contains(got, want) {
81 t.Fatalf("want failure containing %q. output:\n%s\n", want, got)
85 func TestCgoExternalThreadSIGPROF(t *testing.T) {
86 t.Parallel()
87 // issue 9456.
88 switch runtime.GOOS {
89 case "plan9", "windows":
90 t.Skipf("no pthreads on %s", runtime.GOOS)
91 case "darwin":
92 if runtime.GOARCH != "arm" && runtime.GOARCH != "arm64" {
93 // static constructor needs external linking, but we don't support
94 // external linking on OS X 10.6.
95 out, err := exec.Command("uname", "-r").Output()
96 if err != nil {
97 t.Fatalf("uname -r failed: %v", err)
99 // OS X 10.6 == Darwin 10.x
100 if strings.HasPrefix(string(out), "10.") {
101 t.Skipf("no external linking on OS X 10.6")
105 if runtime.GOARCH == "ppc64" {
106 // TODO(austin) External linking not implemented on
107 // ppc64 (issue #8912)
108 t.Skipf("no external linking on ppc64")
111 exe, err := buildTestProg(t, "testprogcgo", "-tags=threadprof")
112 if err != nil {
113 t.Fatal(err)
116 got, err := testEnv(exec.Command(exe, "CgoExternalThreadSIGPROF")).CombinedOutput()
117 if err != nil {
118 t.Fatalf("exit status: %v\n%s", err, got)
121 if want := "OK\n"; string(got) != want {
122 t.Fatalf("expected %q, but got:\n%s", want, got)
126 func TestCgoExternalThreadSignal(t *testing.T) {
127 t.Parallel()
128 // issue 10139
129 switch runtime.GOOS {
130 case "plan9", "windows":
131 t.Skipf("no pthreads on %s", runtime.GOOS)
134 exe, err := buildTestProg(t, "testprogcgo", "-tags=threadprof")
135 if err != nil {
136 t.Fatal(err)
139 got, err := testEnv(exec.Command(exe, "CgoExternalThreadSIGPROF")).CombinedOutput()
140 if err != nil {
141 t.Fatalf("exit status: %v\n%s", err, got)
144 want := []byte("OK\n")
145 if !bytes.Equal(got, want) {
146 t.Fatalf("expected %q, but got:\n%s", want, got)
150 func TestCgoDLLImports(t *testing.T) {
151 // test issue 9356
152 if runtime.GOOS != "windows" {
153 t.Skip("skipping windows specific test")
155 got := runTestProg(t, "testprogcgo", "CgoDLLImportsMain")
156 want := "OK\n"
157 if got != want {
158 t.Fatalf("expected %q, but got %v", want, got)
162 func TestCgoExecSignalMask(t *testing.T) {
163 t.Parallel()
164 // Test issue 13164.
165 switch runtime.GOOS {
166 case "windows", "plan9":
167 t.Skipf("skipping signal mask test on %s", runtime.GOOS)
169 got := runTestProg(t, "testprogcgo", "CgoExecSignalMask")
170 want := "OK\n"
171 if got != want {
172 t.Errorf("expected %q, got %v", want, got)
176 func TestEnsureDropM(t *testing.T) {
177 t.Parallel()
178 // Test for issue 13881.
179 switch runtime.GOOS {
180 case "windows", "plan9":
181 t.Skipf("skipping dropm test on %s", runtime.GOOS)
183 got := runTestProg(t, "testprogcgo", "EnsureDropM")
184 want := "OK\n"
185 if got != want {
186 t.Errorf("expected %q, got %v", want, got)
190 // Test for issue 14387.
191 // Test that the program that doesn't need any cgo pointer checking
192 // takes about the same amount of time with it as without it.
193 func TestCgoCheckBytes(t *testing.T) {
194 t.Parallel()
195 // Make sure we don't count the build time as part of the run time.
196 testenv.MustHaveGoBuild(t)
197 exe, err := buildTestProg(t, "testprogcgo")
198 if err != nil {
199 t.Fatal(err)
202 // Try it 10 times to avoid flakiness.
203 const tries = 10
204 var tot1, tot2 time.Duration
205 for i := 0; i < tries; i++ {
206 cmd := testEnv(exec.Command(exe, "CgoCheckBytes"))
207 cmd.Env = append(cmd.Env, "GODEBUG=cgocheck=0", fmt.Sprintf("GO_CGOCHECKBYTES_TRY=%d", i))
209 start := time.Now()
210 cmd.Run()
211 d1 := time.Since(start)
213 cmd = testEnv(exec.Command(exe, "CgoCheckBytes"))
214 cmd.Env = append(cmd.Env, fmt.Sprintf("GO_CGOCHECKBYTES_TRY=%d", i))
216 start = time.Now()
217 cmd.Run()
218 d2 := time.Since(start)
220 if d1*20 > d2 {
221 // The slow version (d2) was less than 20 times
222 // slower than the fast version (d1), so OK.
223 return
226 tot1 += d1
227 tot2 += d2
230 t.Errorf("cgo check too slow: got %v, expected at most %v", tot2/tries, (tot1/tries)*20)
233 func TestCgoPanicDeadlock(t *testing.T) {
234 t.Parallel()
235 // test issue 14432
236 got := runTestProg(t, "testprogcgo", "CgoPanicDeadlock")
237 want := "panic: cgo error\n\n"
238 if !strings.HasPrefix(got, want) {
239 t.Fatalf("output does not start with %q:\n%s", want, got)
243 func TestCgoCCodeSIGPROF(t *testing.T) {
244 t.Parallel()
245 got := runTestProg(t, "testprogcgo", "CgoCCodeSIGPROF")
246 want := "OK\n"
247 if got != want {
248 t.Errorf("expected %q got %v", want, got)
252 func TestCgoCrashTraceback(t *testing.T) {
253 t.Parallel()
254 if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
255 t.Skipf("not yet supported on %s/%s", runtime.GOOS, runtime.GOARCH)
257 if runtime.Compiler == "gccgo" {
258 t.Skip("gccgo does not have SetCgoTraceback")
260 got := runTestProg(t, "testprogcgo", "CrashTraceback")
261 for i := 1; i <= 3; i++ {
262 if !strings.Contains(got, fmt.Sprintf("cgo symbolizer:%d", i)) {
263 t.Errorf("missing cgo symbolizer:%d", i)
268 func TestCgoTracebackContext(t *testing.T) {
269 t.Parallel()
270 if runtime.Compiler == "gccgo" {
271 t.Skip("gccgo does not have SetCgoTraceback")
273 got := runTestProg(t, "testprogcgo", "TracebackContext")
274 want := "OK\n"
275 if got != want {
276 t.Errorf("expected %q got %v", want, got)
280 func testCgoPprof(t *testing.T, buildArg, runArg string) {
281 t.Parallel()
282 if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
283 t.Skipf("not yet supported on %s/%s", runtime.GOOS, runtime.GOARCH)
285 if runtime.Compiler == "gccgo" {
286 t.Skip("gccgo does not have SetCgoTraceback")
288 testenv.MustHaveGoRun(t)
290 exe, err := buildTestProg(t, "testprogcgo", buildArg)
291 if err != nil {
292 t.Fatal(err)
295 got, err := testEnv(exec.Command(exe, runArg)).CombinedOutput()
296 if err != nil {
297 if testenv.Builder() == "linux-amd64-alpine" {
298 // See Issue 18243 and Issue 19938.
299 t.Skipf("Skipping failing test on Alpine (golang.org/issue/18243). Ignoring error: %v", err)
301 t.Fatal(err)
303 fn := strings.TrimSpace(string(got))
304 defer os.Remove(fn)
306 for try := 0; try < 2; try++ {
307 cmd := testEnv(exec.Command(testenv.GoToolPath(t), "tool", "pprof", "-top", "-nodecount=1"))
308 // Check that pprof works both with and without explicit executable on command line.
309 if try == 0 {
310 cmd.Args = append(cmd.Args, exe, fn)
311 } else {
312 cmd.Args = append(cmd.Args, fn)
315 found := false
316 for i, e := range cmd.Env {
317 if strings.HasPrefix(e, "PPROF_TMPDIR=") {
318 cmd.Env[i] = "PPROF_TMPDIR=" + os.TempDir()
319 found = true
320 break
323 if !found {
324 cmd.Env = append(cmd.Env, "PPROF_TMPDIR="+os.TempDir())
327 top, err := cmd.CombinedOutput()
328 t.Logf("%s:\n%s", cmd.Args, top)
329 if err != nil {
330 t.Error(err)
331 } else if !bytes.Contains(top, []byte("cpuHog")) {
332 t.Error("missing cpuHog in pprof output")
337 func TestCgoPprof(t *testing.T) {
338 testCgoPprof(t, "", "CgoPprof")
341 func TestCgoPprofPIE(t *testing.T) {
342 testCgoPprof(t, "-ldflags=-extldflags=-pie", "CgoPprof")
345 func TestCgoPprofThread(t *testing.T) {
346 testCgoPprof(t, "", "CgoPprofThread")
349 func TestCgoPprofThreadNoTraceback(t *testing.T) {
350 testCgoPprof(t, "", "CgoPprofThreadNoTraceback")
353 func TestRaceProf(t *testing.T) {
354 if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
355 t.Skipf("not yet supported on %s/%s", runtime.GOOS, runtime.GOARCH)
357 if runtime.Compiler == "gccgo" {
358 t.Skip("gccgo does not have SetCgoTraceback")
361 testenv.MustHaveGoRun(t)
363 // This test requires building various packages with -race, so
364 // it's somewhat slow.
365 if testing.Short() {
366 t.Skip("skipping test in -short mode")
369 exe, err := buildTestProg(t, "testprogcgo", "-race")
370 if err != nil {
371 t.Fatal(err)
374 got, err := testEnv(exec.Command(exe, "CgoRaceprof")).CombinedOutput()
375 if err != nil {
376 t.Fatal(err)
378 want := "OK\n"
379 if string(got) != want {
380 t.Errorf("expected %q got %s", want, got)
384 func TestRaceSignal(t *testing.T) {
385 t.Parallel()
386 if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
387 t.Skipf("not yet supported on %s/%s", runtime.GOOS, runtime.GOARCH)
390 testenv.MustHaveGoRun(t)
392 // This test requires building various packages with -race, so
393 // it's somewhat slow.
394 if testing.Short() {
395 t.Skip("skipping test in -short mode")
398 exe, err := buildTestProg(t, "testprogcgo", "-race")
399 if err != nil {
400 t.Fatal(err)
403 got, err := testEnv(exec.Command(exe, "CgoRaceSignal")).CombinedOutput()
404 if err != nil {
405 t.Logf("%s\n", got)
406 t.Fatal(err)
408 want := "OK\n"
409 if string(got) != want {
410 t.Errorf("expected %q got %s", want, got)
414 func TestCgoNumGoroutine(t *testing.T) {
415 switch runtime.GOOS {
416 case "windows", "plan9":
417 t.Skipf("skipping numgoroutine test on %s", runtime.GOOS)
419 t.Parallel()
420 got := runTestProg(t, "testprogcgo", "NumGoroutine")
421 want := "OK\n"
422 if got != want {
423 t.Errorf("expected %q got %v", want, got)