• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1package psx
2
3import (
4	"runtime"
5	"syscall"
6	"testing"
7)
8
9func TestSyscall3(t *testing.T) {
10	want := syscall.Getpid()
11	if got, _, err := Syscall3(syscall.SYS_GETPID, 0, 0, 0); err != 0 {
12		t.Errorf("failed to get PID via libpsx: %v", err)
13	} else if int(got) != want {
14		t.Errorf("pid mismatch: got=%d want=%d", got, want)
15	}
16	if got, _, err := Syscall3(syscall.SYS_CAPGET, 0, 0, 0); err != 14 {
17		t.Errorf("malformed capget returned %d: %v (want 14: %v)", err, err, syscall.Errno(14))
18	} else if ^got != 0 {
19		t.Errorf("malformed capget did not return -1, got=%d", got)
20	}
21}
22
23func TestSyscall6(t *testing.T) {
24	want := syscall.Getpid()
25	if got, _, err := Syscall6(syscall.SYS_GETPID, 0, 0, 0, 0, 0, 0); err != 0 {
26		t.Errorf("failed to get PID via libpsx: %v", err)
27	} else if int(got) != want {
28		t.Errorf("pid mismatch: got=%d want=%d", got, want)
29	}
30	if got, _, err := Syscall6(syscall.SYS_CAPGET, 0, 0, 0, 0, 0, 0); err != 14 {
31		t.Errorf("malformed capget errno %d: %v (want 14: %v)", err, err, syscall.Errno(14))
32	} else if ^got != 0 {
33		t.Errorf("malformed capget did not return -1, got=%d", got)
34	}
35}
36
37// killAThread locks the goroutine to a thread and exits. This has the
38// effect of making the go runtime terminate the thread.
39func killAThread(c <-chan struct{}) {
40	runtime.LockOSThread()
41	<-c
42}
43
44// Test to confirm no regression against:
45//
46//   https://github.com/golang/go/issues/42494
47func TestThreadChurn(t *testing.T) {
48	const prSetKeepCaps = 8
49
50	for j := 0; j < 4; j++ {
51		kill := (j & 1) != 0
52		sysc := (j & 2) != 0
53		t.Logf("[%d] testing kill=%v, sysc=%v", j, kill, sysc)
54		for i := 50; i > 0; i-- {
55			if kill {
56				c := make(chan struct{})
57				go killAThread(c)
58				close(c)
59			}
60			if sysc {
61				if _, _, e := Syscall3(syscall.SYS_PRCTL, prSetKeepCaps, uintptr(i&1), 0); e != 0 {
62					t.Fatalf("[%d] psx:prctl(SET_KEEPCAPS, %d) failed: %v", i, i&1, syscall.Errno(e))
63				}
64			}
65		}
66		t.Logf("[%d] PASSED kill=%v, sysc=%v", j, kill, sysc)
67	}
68}
69