Skip to content

Commit

Permalink
x/sys/unix: use uintptr for tracee addresses on FreeBSD
Browse files Browse the repository at this point in the history
Change PtraceLwpInfoStruct, PtraceIoDesc and PtraceIO to not use *byte
to represent addresses that belong to the traced process.

Fixes golang/go#54113

Change-Id: I6efad26046b784d79b83fed4498cac93957274ca
Reviewed-on: https://go-review.googlesource.com/c/sys/+/419915
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Michael Pratt <[email protected]>
Reviewed-by: Russ Cox <[email protected]>
Run-TryBot: Russ Cox <[email protected]>
Auto-Submit: Russ Cox <[email protected]>
  • Loading branch information
aarzilli authored and gopherbot committed Sep 6, 2022
1 parent d48e67d commit 9e1f761
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 20 deletions.
26 changes: 26 additions & 0 deletions unix/mkpost.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,32 @@ func main() {
b = convertEprocRegex.ReplaceAll(b, []byte("$1$2[$3]byte"))
}

if goos == "freebsd" {
// Inside PtraceLwpInfoStruct replace __Siginfo with __PtraceSiginfo,
// Create __PtraceSiginfo as a copy of __Siginfo where every *byte instance is replaced by uintptr
ptraceLwpInfoStruct := regexp.MustCompile(`(?s:type PtraceLwpInfoStruct struct \{.*?\})`)
b = ptraceLwpInfoStruct.ReplaceAllFunc(b, func(in []byte) []byte {
return bytes.ReplaceAll(in, []byte("__Siginfo"), []byte("__PtraceSiginfo"))
})

siginfoStruct := regexp.MustCompile(`(?s:type __Siginfo struct \{.*?\})`)
b = siginfoStruct.ReplaceAllFunc(b, func(in []byte) []byte {
out := append([]byte{}, in...)
out = append(out, '\n', '\n')
out = append(out,
bytes.ReplaceAll(
bytes.ReplaceAll(in, []byte("__Siginfo"), []byte("__PtraceSiginfo")),
[]byte("*byte"), []byte("uintptr"))...)
return out
})

// Inside PtraceIoDesc replace all *byte with uintptr
ptraceIoDescStruct := regexp.MustCompile(`(?s:type PtraceIoDesc struct \{.*?\})`)
b = ptraceIoDescStruct.ReplaceAllFunc(b, func(in []byte) []byte {
return bytes.ReplaceAll(in, []byte("*byte"), []byte("uintptr"))
})
}

// Intentionally export __val fields in Fsid and Sigset_t
valRegex := regexp.MustCompile(`type (Fsid|Sigset_t) struct {(\s+)X__(bits|val)(\s+\S+\s+)}`)
b = valRegex.ReplaceAll(b, []byte("type $1 struct {${2}Val$4}"))
Expand Down
2 changes: 1 addition & 1 deletion unix/syscall_freebsd_386.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func PtraceGetFsBase(pid int, fsbase *int64) (err error) {
}

func PtraceIO(req int, pid int, addr uintptr, out []byte, countin int) (count int, err error) {
ioDesc := PtraceIoDesc{Op: int32(req), Offs: (*byte)(unsafe.Pointer(addr)), Addr: (*byte)(unsafe.Pointer(&out[0])), Len: uint32(countin)}
ioDesc := PtraceIoDesc{Op: int32(req), Offs: uintptr(unsafe.Pointer(addr)), Addr: uintptr(unsafe.Pointer(&out[0])), Len: uint32(countin)}
err = ptrace(PT_IO, pid, uintptr(unsafe.Pointer(&ioDesc)), 0)
return int(ioDesc.Len), err
}
2 changes: 1 addition & 1 deletion unix/syscall_freebsd_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func PtraceGetFsBase(pid int, fsbase *int64) (err error) {
}

func PtraceIO(req int, pid int, addr uintptr, out []byte, countin int) (count int, err error) {
ioDesc := PtraceIoDesc{Op: int32(req), Offs: (*byte)(unsafe.Pointer(addr)), Addr: (*byte)(unsafe.Pointer(&out[0])), Len: uint64(countin)}
ioDesc := PtraceIoDesc{Op: int32(req), Offs: uintptr(unsafe.Pointer(addr)), Addr: uintptr(unsafe.Pointer(&out[0])), Len: uint64(countin)}
err = ptrace(PT_IO, pid, uintptr(unsafe.Pointer(&ioDesc)), 0)
return int(ioDesc.Len), err
}
2 changes: 1 addition & 1 deletion unix/syscall_freebsd_arm.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func sendfile(outfd int, infd int, offset *int64, count int) (written int, err e
func Syscall9(num, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err syscall.Errno)

func PtraceIO(req int, pid int, addr uintptr, out []byte, countin int) (count int, err error) {
ioDesc := PtraceIoDesc{Op: int32(req), Offs: (*byte)(unsafe.Pointer(addr)), Addr: (*byte)(unsafe.Pointer(&out[0])), Len: uint32(countin)}
ioDesc := PtraceIoDesc{Op: int32(req), Offs: uintptr(unsafe.Pointer(addr)), Addr: uintptr(unsafe.Pointer(&out[0])), Len: uint32(countin)}
err = ptrace(PT_IO, pid, uintptr(unsafe.Pointer(&ioDesc)), 0)
return int(ioDesc.Len), err
}
2 changes: 1 addition & 1 deletion unix/syscall_freebsd_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func sendfile(outfd int, infd int, offset *int64, count int) (written int, err e
func Syscall9(num, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err syscall.Errno)

func PtraceIO(req int, pid int, addr uintptr, out []byte, countin int) (count int, err error) {
ioDesc := PtraceIoDesc{Op: int32(req), Offs: (*byte)(unsafe.Pointer(addr)), Addr: (*byte)(unsafe.Pointer(&out[0])), Len: uint64(countin)}
ioDesc := PtraceIoDesc{Op: int32(req), Offs: uintptr(unsafe.Pointer(addr)), Addr: uintptr(unsafe.Pointer(&out[0])), Len: uint64(countin)}
err = ptrace(PT_IO, pid, uintptr(unsafe.Pointer(&ioDesc)), 0)
return int(ioDesc.Len), err
}
2 changes: 1 addition & 1 deletion unix/syscall_freebsd_riscv64.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func sendfile(outfd int, infd int, offset *int64, count int) (written int, err e
func Syscall9(num, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err syscall.Errno)

func PtraceIO(req int, pid int, addr uintptr, out []byte, countin int) (count int, err error) {
ioDesc := PtraceIoDesc{Op: int32(req), Offs: (*byte)(unsafe.Pointer(addr)), Addr: (*byte)(unsafe.Pointer(&out[0])), Len: uint64(countin)}
ioDesc := PtraceIoDesc{Op: int32(req), Offs: uintptr(unsafe.Pointer(addr)), Addr: uintptr(unsafe.Pointer(&out[0])), Len: uint64(countin)}
err = ptrace(PT_IO, pid, uintptr(unsafe.Pointer(&ioDesc)), 0)
return int(ioDesc.Len), err
}
17 changes: 14 additions & 3 deletions unix/ztypes_freebsd_386.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions unix/ztypes_freebsd_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions unix/ztypes_freebsd_arm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions unix/ztypes_freebsd_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions unix/ztypes_freebsd_riscv64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9e1f761

Please sign in to comment.