aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/internal/testsuite/ptrace.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2026-10-06 22:23:20 +0900
committerOphestra <cat@gensokyo.uk>2026-10-06 23:09:19 +0900
commita9e2749f6654d0aa07b274a45c9177d10323f80a (patch)
tree291f5b23a67af036cbe5d374b3d3fb0240fc438c /test/internal/testsuite/ptrace.go
parent19f36491f2e2a5029ac396c10408d653cad6c81b (diff)
internal/testsuite: move from test
This structure is a lot less clumsy than the old nix-centric layout. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'test/internal/testsuite/ptrace.go')
-rw-r--r--test/internal/testsuite/ptrace.go144
1 files changed, 0 insertions, 144 deletions
diff --git a/test/internal/testsuite/ptrace.go b/test/internal/testsuite/ptrace.go
deleted file mode 100644
index ccf0900c..00000000
--- a/test/internal/testsuite/ptrace.go
+++ /dev/null
@@ -1,144 +0,0 @@
-package testsuite
-
-import (
- "crypto/sha512"
- "encoding/base64"
- "errors"
- "fmt"
- "os"
- "syscall"
- "unsafe"
-)
-
-const (
- // _PTRACE_ATTACH attaches to the process specified in pid.
- _PTRACE_ATTACH = 16
- // _PTRACE_DETACH restarts the stopped tracee as for PTRACE_CONT, but first
- // detaches from it.
- _PTRACE_DETACH = 17
-
- // _PTRACE_SECCOMP_GET_FILTER allows the tracer to dump the tracee's classic
- // BPF filters.
- _PTRACE_SECCOMP_GET_FILTER = 0x420c
-)
-
-// ptrace wraps the ptrace syscall.
-func ptrace(
- op uintptr,
- pid, addr int,
- data unsafe.Pointer,
-) (r uintptr, errno syscall.Errno) {
- r, _, errno = syscall.Syscall6(
- syscall.SYS_PTRACE,
- op,
- uintptr(pid),
- uintptr(addr),
- uintptr(data),
- 0, 0,
- )
- return
-}
-
-// ptraceAttach attaches to the process referred to by pid.
-func ptraceAttach(pid int) error {
- if _, errno := ptrace(_PTRACE_ATTACH, pid, 0, nil); errno != 0 {
- return os.NewSyscallError("PTRACE_ATTACH", errno)
- }
-
- var status syscall.WaitStatus
- for {
- if _, err := syscall.Wait4(
- pid,
- &status,
- syscall.WALL,
- nil,
- ); err != nil {
- if errors.Is(err, syscall.EINTR) {
- continue
- }
- return os.NewSyscallError("wait4", err)
- }
- switch {
- case status.Stopped():
- return nil
-
- case status.Continued():
- continue
-
- case status.Signaled():
- return fmt.Errorf(
- "tracee terminated by signal %s",
- status.Signal(),
- )
-
- case status.Exited():
- return fmt.Errorf(
- "tracee terminated unexpectedly with code %d",
- status.ExitStatus(),
- )
- }
- }
-}
-
-// ptraceDetach detaches from the attached process referred to by pid.
-func ptraceDetach(pid int) error {
- if _, errno := ptrace(_PTRACE_DETACH, pid, 0, nil); errno != 0 {
- return os.NewSyscallError("PTRACE_DETACH", errno)
- }
- return nil
-}
-
-// getFilter dumps the specified tracee's cBPF filter at the specified index
-// and returns the resulting payload. T must be eight bytes long and must not
-// contain pointers.
-func getFilter(pid, index int) ([]syscall.SockFilter, error) {
- var buf []syscall.SockFilter
- if n, errno := ptrace(
- _PTRACE_SECCOMP_GET_FILTER,
- pid, index, nil,
- ); errno != 0 {
- return nil, os.NewSyscallError("PTRACE_SECCOMP_GET_FILTER", errno)
- } else {
- buf = make([]syscall.SockFilter, n)
- }
- if _, errno := ptrace(
- _PTRACE_SECCOMP_GET_FILTER,
- pid, index, unsafe.Pointer(&buf[0]),
- ); errno != 0 {
- return nil, os.NewSyscallError("PTRACE_SECCOMP_GET_FILTER", errno)
- }
- return buf, nil
-}
-
-// CheckFilter checks the process at pid to have its first filter's contents
-// match the specified sha512 checksum.
-func CheckFilter(pid, index int, sum [sha512.Size]byte) (err error) {
- if err = ptraceAttach(pid); err != nil {
- return
- }
- defer func() {
- if detachErr := ptraceDetach(pid); err == nil {
- err = detachErr
- }
- }()
-
- var buf []syscall.SockFilter
- h := sha512.New()
- if buf, err = getFilter(pid, index); err != nil {
- return
- } else {
- h.Write(unsafe.Slice(
- (*byte)(unsafe.Pointer(&buf[0])),
- uintptr(len(buf))*unsafe.Sizeof(buf[0]),
- ))
- }
-
- if got := h.Sum(nil); string(got) != string(sum[:]) {
- return fmt.Errorf(
- "bad filter\n\t got: %s\n\twant: %s",
- base64.StdEncoding.EncodeToString(got),
- base64.StdEncoding.EncodeToString(sum[:]),
- )
- }
- return
-}