diff options
Diffstat (limited to 'src/syscall/syscall_linux_test.go')
| -rw-r--r-- | src/syscall/syscall_linux_test.go | 64 |
1 files changed, 50 insertions, 14 deletions
diff --git a/src/syscall/syscall_linux_test.go b/src/syscall/syscall_linux_test.go index 41ae8cc5a1..adeb7c9ebb 100644 --- a/src/syscall/syscall_linux_test.go +++ b/src/syscall/syscall_linux_test.go @@ -9,7 +9,6 @@ import ( "fmt" "io" "io/fs" - "io/ioutil" "os" "os/exec" "os/signal" @@ -30,7 +29,7 @@ func chtmpdir(t *testing.T) func() { if err != nil { t.Fatalf("chtmpdir: %v", err) } - d, err := ioutil.TempDir("", "test") + d, err := os.MkdirTemp("", "test") if err != nil { t.Fatalf("chtmpdir: %v", err) } @@ -160,7 +159,7 @@ func TestLinuxDeathSignal(t *testing.T) { // Copy the test binary to a location that a non-root user can read/execute // after we drop privileges - tempDir, err := ioutil.TempDir("", "TestDeathSignal") + tempDir, err := os.MkdirTemp("", "TestDeathSignal") if err != nil { t.Fatalf("cannot create temporary directory: %v", err) } @@ -321,7 +320,7 @@ func TestSyscallNoError(t *testing.T) { // Copy the test binary to a location that a non-root user can read/execute // after we drop privileges - tempDir, err := ioutil.TempDir("", "TestSyscallNoError") + tempDir, err := os.MkdirTemp("", "TestSyscallNoError") if err != nil { t.Fatalf("cannot create temporary directory: %v", err) } @@ -543,37 +542,69 @@ func TestAllThreadsSyscall(t *testing.T) { func compareStatus(filter, expect string) error { expected := filter + expect pid := syscall.Getpid() - fs, err := ioutil.ReadDir(fmt.Sprintf("/proc/%d/task", pid)) + fs, err := os.ReadDir(fmt.Sprintf("/proc/%d/task", pid)) if err != nil { return fmt.Errorf("unable to find %d tasks: %v", pid, err) } + expectedProc := fmt.Sprintf("Pid:\t%d", pid) + foundAThread := false for _, f := range fs { tf := fmt.Sprintf("/proc/%s/status", f.Name()) - d, err := ioutil.ReadFile(tf) - if os.IsNotExist(err) { - // We are racing against threads dying, which - // is out of our control, so ignore the - // missing file and skip to the next one. - continue - } + d, err := os.ReadFile(tf) if err != nil { - return fmt.Errorf("unable to read %q: %v", tf, err) + // There are a surprising number of ways this + // can error out on linux. We've seen all of + // the following, so treat any error here as + // equivalent to the "process is gone": + // os.IsNotExist(err), + // "... : no such process", + // "... : bad file descriptor. + continue } lines := strings.Split(string(d), "\n") for _, line := range lines { // Different kernel vintages pad differently. line = strings.TrimSpace(line) + if strings.HasPrefix(line, "Pid:\t") { + // On loaded systems, it is possible + // for a TID to be reused really + // quickly. As such, we need to + // validate that the thread status + // info we just read is a task of the + // same process PID as we are + // currently running, and not a + // recently terminated thread + // resurfaced in a different process. + if line != expectedProc { + break + } + // Fall through in the unlikely case + // that filter at some point is + // "Pid:\t". + } if strings.HasPrefix(line, filter) { if line != expected { - return fmt.Errorf("%q got:%q want:%q (bad)\n", tf, line, expected) + return fmt.Errorf("%q got:%q want:%q (bad) [pid=%d file:'%s' %v]\n", tf, line, expected, pid, string(d), expectedProc) } + foundAThread = true break } } } + if !foundAThread { + return fmt.Errorf("found no thread /proc/<TID>/status files for process %q", expectedProc) + } return nil } +// killAThread locks the goroutine to an OS thread and exits; this +// causes an OS thread to terminate. +func killAThread(c <-chan struct{}) { + runtime.LockOSThread() + <-c + return +} + // TestSetuidEtc performs tests on all of the wrapped system calls // that mirror to the 9 glibc syscalls with POSIX semantics. The test // here is considered authoritative and should compile and run @@ -624,6 +655,11 @@ func TestSetuidEtc(t *testing.T) { } for i, v := range vs { + // Generate some thread churn as we execute the tests. + c := make(chan struct{}) + go killAThread(c) + close(c) + if err := v.fn(); err != nil { t.Errorf("[%d] %q failed: %v", i, v.call, err) continue |
