| 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 unix |
| 6 |
|
| 7 |
package runtime_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"internal/testenv" |
| 12 |
"io" |
| 13 |
"os" |
| 14 |
"os/exec" |
| 15 |
"runtime" |
| 16 |
"runtime/debug" |
| 17 |
"strings" |
| 18 |
"sync" |
| 19 |
"syscall" |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
"unsafe" |
| 23 |
) |
| 24 |
|
| 25 |
func init() { |
| 26 |
if runtime.Sigisblocked(int(syscall.SIGQUIT)) { |
| 27 |
// We can't use SIGQUIT to kill subprocesses because |
| 28 |
// it's blocked. Use SIGKILL instead. See issue |
| 29 |
// #19196 for an example of when this happens. |
| 30 |
testenv.Sigquit = syscall.SIGKILL |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func TestBadOpen(t *testing.T) { |
| 35 |
// make sure we get the correct error code if open fails. Same for |
| 36 |
// read/write/close on the resulting -1 fd. See issue 10052. |
| 37 |
nonfile := []byte("/notreallyafile") |
| 38 |
fd := runtime.Open(&nonfile[0], 0, 0) |
| 39 |
if fd != -1 { |
| 40 |
t.Errorf("open(%q)=%d, want -1", nonfile, fd) |
| 41 |
} |
| 42 |
var buf [32]byte |
| 43 |
r := runtime.Read(-1, unsafe.Pointer(&buf[0]), int32(len(buf))) |
| 44 |
if got, want := r, -int32(syscall.EBADF); got != want { |
| 45 |
t.Errorf("read()=%d, want %d", got, want) |
| 46 |
} |
| 47 |
w := runtime.Write(^uintptr(0), unsafe.Pointer(&buf[0]), int32(len(buf))) |
| 48 |
if got, want := w, -int32(syscall.EBADF); got != want { |
| 49 |
t.Errorf("write()=%d, want %d", got, want) |
| 50 |
} |
| 51 |
c := runtime.Close(-1) |
| 52 |
if c != -1 { |
| 53 |
t.Errorf("close()=%d, want -1", c) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func TestCrashDumpsAllThreads(t *testing.T) { |
| 58 |
if *flagQuick { |
| 59 |
t.Skip("-quick") |
| 60 |
} |
| 61 |
|
| •62 |
if runtime.GOARCH == "mips64le" { |
| 63 |
t.Skipf("skipping; flaky on %s", runtime.GOARCH) |
| 64 |
} |
| 65 |
|
| •66 |
if runtime.GOARCH == "s390x" { |
| 67 |
t.Skipf("skipping; flaky on %s", runtime.GOARCH) |
| 68 |
} |
| 69 |
|
| 70 |
switch runtime.GOOS { |
| 71 |
case "darwin", "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "illumos", "solaris": |
| 72 |
default: |
| 73 |
t.Skipf("skipping; not supported on %v", runtime.GOOS) |
| 74 |
} |
| 75 |
|
| 76 |
if runtime.GOOS == "openbsd" && (runtime.GOARCH == "arm" || runtime.GOARCH == "ppc64") { |
| 77 |
// This may be ncpu < 2 related... |
| 78 |
t.Skipf("skipping; test fails on %s/%s - see issue #42464", runtime.GOOS, runtime.GOARCH) |
| 79 |
} |
| 80 |
|
| 81 |
if runtime.Sigisblocked(int(syscall.SIGQUIT)) { |
| 82 |
t.Skip("skipping; SIGQUIT is blocked, see golang.org/issue/19196") |
| 83 |
} |
| 84 |
|
| 85 |
testenv.MustHaveGoBuild(t) |
| 86 |
|
| 87 |
if strings.Contains(os.Getenv("GOFLAGS"), "mayMoreStackPreempt") { |
| 88 |
// This test occasionally times out in this debug mode. This is probably |
| 89 |
// revealing a real bug in the scheduler, but since it seems to only |
| 90 |
// affect this test and this is itself a test of a debug mode, it's not |
| 91 |
// a high priority. |
| 92 |
testenv.SkipFlaky(t, 55160) |
| 93 |
} |
| 94 |
|
| 95 |
exe, err := buildTestProg(t, "testprog") |
| 96 |
if err != nil { |
| 97 |
t.Fatal(err) |
| 98 |
} |
| 99 |
|
| 100 |
cmd := testenv.Command(t, exe, "CrashDumpsAllThreads") |
| 101 |
cmd = testenv.CleanCmdEnv(cmd) |
| 102 |
cmd.Dir = t.TempDir() // put any core file in tempdir |
| 103 |
cmd.Env = append(cmd.Env, |
| 104 |
"GOTRACEBACK=crash", |
| 105 |
// Set GOGC=off. Because of golang.org/issue/10958, the tight |
| 106 |
// loops in the test program are not preemptible. If GC kicks |
| 107 |
// in, it may lock up and prevent main from saying it's ready. |
| 108 |
"GOGC=off", |
| 109 |
// Set GODEBUG=asyncpreemptoff=1. If a thread is preempted |
| 110 |
// when it receives SIGQUIT, it won't show the expected |
| 111 |
// stack trace. See issue 35356. |
| 112 |
"GODEBUG=asyncpreemptoff=1", |
| 113 |
) |
| 114 |
|
| 115 |
var outbuf bytes.Buffer |
| 116 |
cmd.Stdout = &outbuf |
| 117 |
cmd.Stderr = &outbuf |
| 118 |
|
| 119 |
rp, wp, err := os.Pipe() |
| 120 |
if err != nil { |
| 121 |
t.Fatal(err) |
| 122 |
} |
| 123 |
defer rp.Close() |
| 124 |
|
| 125 |
cmd.ExtraFiles = []*os.File{wp} |
| 126 |
|
| 127 |
if err := cmd.Start(); err != nil { |
| 128 |
wp.Close() |
| 129 |
t.Fatalf("starting program: %v", err) |
| 130 |
} |
| 131 |
|
| 132 |
if err := wp.Close(); err != nil { |
| 133 |
t.Logf("closing write pipe: %v", err) |
| 134 |
} |
| 135 |
if _, err := rp.Read(make([]byte, 1)); err != nil { |
| 136 |
t.Fatalf("reading from pipe: %v", err) |
| 137 |
} |
| 138 |
|
| 139 |
if err := cmd.Process.Signal(syscall.SIGQUIT); err != nil { |
| 140 |
t.Fatalf("signal: %v", err) |
| 141 |
} |
| 142 |
|
| 143 |
// No point in checking the error return from Wait--we expect |
| 144 |
// it to fail. |
| 145 |
cmd.Wait() |
| 146 |
|
| 147 |
// We want to see a stack trace for each thread. |
| 148 |
// Before https://golang.org/cl/2811 running threads would say |
| 149 |
// "goroutine running on other thread; stack unavailable". |
| 150 |
out := outbuf.Bytes() |
| 151 |
n := bytes.Count(out, []byte("main.crashDumpsAllThreadsLoop(")) |
| 152 |
if n != 4 { |
| 153 |
t.Errorf("found %d instances of main.crashDumpsAllThreadsLoop; expected 4", n) |
| 154 |
t.Logf("%s", out) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
func TestPanicSystemstack(t *testing.T) { |
| 159 |
// Test that GOTRACEBACK=crash prints both the system and user |
| 160 |
// stack of other threads. |
| 161 |
|
| 162 |
// The GOTRACEBACK=crash handler takes 0.1 seconds even if |
| 163 |
// it's not writing a core file and potentially much longer if |
| 164 |
// it is. Skip in short mode. |
| 165 |
if testing.Short() { |
| 166 |
t.Skip("Skipping in short mode (GOTRACEBACK=crash is slow)") |
| 167 |
} |
| 168 |
|
| 169 |
if runtime.Sigisblocked(int(syscall.SIGQUIT)) { |
| 170 |
t.Skip("skipping; SIGQUIT is blocked, see golang.org/issue/19196") |
| 171 |
} |
| 172 |
|
| 173 |
t.Parallel() |
| 174 |
cmd := exec.Command(testenv.Executable(t), "testPanicSystemstackInternal") |
| 175 |
cmd = testenv.CleanCmdEnv(cmd) |
| 176 |
cmd.Dir = t.TempDir() // put any core file in tempdir |
| 177 |
cmd.Env = append(cmd.Env, "GOTRACEBACK=crash") |
| 178 |
pr, pw, err := os.Pipe() |
| 179 |
if err != nil { |
| 180 |
t.Fatal("creating pipe: ", err) |
| 181 |
} |
| 182 |
cmd.Stderr = pw |
| 183 |
if err := cmd.Start(); err != nil { |
| 184 |
t.Fatal("starting command: ", err) |
| 185 |
} |
| 186 |
defer cmd.Process.Wait() |
| 187 |
defer cmd.Process.Kill() |
| 188 |
if err := pw.Close(); err != nil { |
| 189 |
t.Log("closing write pipe: ", err) |
| 190 |
} |
| 191 |
defer pr.Close() |
| 192 |
|
| 193 |
// Wait for "x\nx\n" to indicate almost-readiness. |
| 194 |
buf := make([]byte, 4) |
| 195 |
_, err = io.ReadFull(pr, buf) |
| 196 |
if err != nil || string(buf) != "x\nx\n" { |
| 197 |
t.Fatal("subprocess failed; output:\n", string(buf)) |
| 198 |
} |
| 199 |
|
| 200 |
// The child blockers print "x\n" and then block on a lock. Receiving |
| 201 |
// those bytes only indicates that the child is _about to block_. Since |
| 202 |
// we don't have a way to know when it is fully blocked, sleep a bit to |
| 203 |
// make us less likely to lose the race and signal before the child |
| 204 |
// blocks. |
| 205 |
time.Sleep(100 * time.Millisecond) |
| 206 |
|
| 207 |
// Send SIGQUIT. |
| 208 |
if err := cmd.Process.Signal(syscall.SIGQUIT); err != nil { |
| 209 |
t.Fatal("signaling subprocess: ", err) |
| 210 |
} |
| 211 |
|
| 212 |
// Get traceback. |
| 213 |
tb, err := io.ReadAll(pr) |
| 214 |
if err != nil { |
| 215 |
t.Fatal("reading traceback from pipe: ", err) |
| 216 |
} |
| 217 |
|
| 218 |
// Traceback should have two testPanicSystemstackInternal's |
| 219 |
// and two blockOnSystemStackInternal's. |
| 220 |
userFunc := "testPanicSystemstackInternal" |
| 221 |
sysFunc := "blockOnSystemStackInternal" |
| 222 |
nUser := bytes.Count(tb, []byte(userFunc)) |
| 223 |
nSys := bytes.Count(tb, []byte(sysFunc)) |
| 224 |
if nUser != 2 || nSys != 2 { |
| 225 |
t.Fatalf("want %d user stack frames in %s and %d system stack frames in %s, got %d and %d:\n%s", 2, userFunc, 2, sysFunc, nUser, nSys, string(tb)) |
| 226 |
} |
| 227 |
|
| 228 |
// Traceback should not contain "unexpected SPWRITE" when |
| 229 |
// unwinding the system stacks. |
| 230 |
if bytes.Contains(tb, []byte("unexpected SPWRITE")) { |
| 231 |
t.Errorf("unexpected \"unexpected SPWRITE\" in traceback:\n%s", tb) |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
func init() { |
| 236 |
if len(os.Args) >= 2 && os.Args[1] == "testPanicSystemstackInternal" { |
| 237 |
// Complete any in-flight GCs and disable future ones. We're going to |
| 238 |
// block goroutines on runtime locks, which aren't ever preemptible for the |
| 239 |
// GC to scan them. |
| 240 |
runtime.GC() |
| 241 |
debug.SetGCPercent(-1) |
| 242 |
// Get two threads running on the system stack with |
| 243 |
// something recognizable in the stack trace. |
| 244 |
runtime.GOMAXPROCS(2) |
| 245 |
go testPanicSystemstackInternal() |
| 246 |
testPanicSystemstackInternal() |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
func testPanicSystemstackInternal() { |
| 251 |
runtime.BlockOnSystemStack() |
| 252 |
os.Exit(1) // Should be unreachable. |
| 253 |
} |
| 254 |
|
| 255 |
func TestSignalExitStatus(t *testing.T) { |
| 256 |
testenv.MustHaveGoBuild(t) |
| 257 |
exe, err := buildTestProg(t, "testprog") |
| 258 |
if err != nil { |
| 259 |
t.Fatal(err) |
| 260 |
} |
| 261 |
err = testenv.CleanCmdEnv(exec.Command(exe, "SignalExitStatus")).Run() |
| 262 |
if err == nil { |
| 263 |
t.Error("test program succeeded unexpectedly") |
| 264 |
} else if ee, ok := err.(*exec.ExitError); !ok { |
| 265 |
t.Errorf("error (%v) has type %T; expected exec.ExitError", err, err) |
| 266 |
} else if ws, ok := ee.Sys().(syscall.WaitStatus); !ok { |
| 267 |
t.Errorf("error.Sys (%v) has type %T; expected syscall.WaitStatus", ee.Sys(), ee.Sys()) |
| 268 |
} else if !ws.Signaled() || ws.Signal() != syscall.SIGTERM { |
| 269 |
t.Errorf("got %v; expected SIGTERM", ee) |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
func TestSignalIgnoreSIGTRAP(t *testing.T) { |
| 274 |
if runtime.GOOS == "openbsd" { |
| 275 |
testenv.SkipFlaky(t, 49725) |
| 276 |
} |
| 277 |
|
| 278 |
output := runTestProg(t, "testprognet", "SignalIgnoreSIGTRAP") |
| 279 |
want := "OK\n" |
| 280 |
if output != want { |
| 281 |
t.Fatalf("want %s, got %s\n", want, output) |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
func TestSignalDuringExec(t *testing.T) { |
| 286 |
switch runtime.GOOS { |
| 287 |
case "darwin", "dragonfly", "freebsd", "linux", "netbsd", "openbsd": |
| 288 |
default: |
| 289 |
t.Skipf("skipping test on %s", runtime.GOOS) |
| 290 |
} |
| 291 |
output := runTestProg(t, "testprognet", "SignalDuringExec") |
| 292 |
want := "OK\n" |
| 293 |
if output != want { |
| 294 |
t.Fatalf("want %s, got %s\n", want, output) |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
func TestSignalM(t *testing.T) { |
| 299 |
r, w, errno := runtime.Pipe() |
| 300 |
if errno != 0 { |
| 301 |
t.Fatal(syscall.Errno(errno)) |
| 302 |
} |
| 303 |
defer func() { |
| 304 |
runtime.Close(r) |
| 305 |
runtime.Close(w) |
| 306 |
}() |
| 307 |
runtime.Closeonexec(r) |
| 308 |
runtime.Closeonexec(w) |
| 309 |
|
| 310 |
var want, got int64 |
| 311 |
var wg sync.WaitGroup |
| 312 |
ready := make(chan *runtime.M) |
| 313 |
wg.Add(1) |
| 314 |
go func() { |
| 315 |
runtime.LockOSThread() |
| 316 |
want, got = runtime.WaitForSigusr1(r, w, func(mp *runtime.M) { |
| 317 |
ready <- mp |
| 318 |
}) |
| 319 |
runtime.UnlockOSThread() |
| 320 |
wg.Done() |
| 321 |
}() |
| 322 |
waitingM := <-ready |
| 323 |
runtime.SendSigusr1(waitingM) |
| 324 |
|
| 325 |
timer := time.AfterFunc(time.Second, func() { |
| 326 |
// Write 1 to tell WaitForSigusr1 that we timed out. |
| 327 |
bw := byte(1) |
| 328 |
if n := runtime.Write(uintptr(w), unsafe.Pointer(&bw), 1); n != 1 { |
| 329 |
t.Errorf("pipe write failed: %d", n) |
| 330 |
} |
| 331 |
}) |
| 332 |
defer timer.Stop() |
| 333 |
|
| 334 |
wg.Wait() |
| 335 |
if got == -1 { |
| 336 |
t.Fatal("signalM signal not received") |
| 337 |
} else if want != got { |
| 338 |
t.Fatalf("signal sent to M %d, but received on M %d", want, got) |
| 339 |
} |
| 340 |
} |