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