proc/native/linux: try to use process_vm_readv/writev

This change adds `ProcessVmRead` and `ProcessVmWrite` wrappers around
the syscalls `process_vm_readv` and `process_vm_writev`, available since
Linux 3.2. These follow the same permission model as `ptrace`, but they
don't actually require being attached, which means they can be called
directly from any thread in the debugger. They also use `iovec` to write
entire blocks at once, rather than having to peek/poke each `uintptr`.

These wrappers are used in `Thread.ReadMemory` and `WriteMemory`, still
falling back to `ptrace` if that fails for any reason.  Notably,
`process_vm_writev` respects memory protection, so it can't modify
read-only memory like `ptrace`. This frequently occurs when writing
breakpoints in read-only `.text`, so to avoid a lot of wasted `EFAULT`
calls, we only try `process_vm_writev` for larger writes.
This commit is contained in:
Josh Stone
2020-02-28 14:19:54 -08:00
committed by Derek Parker
parent 0a650a0e0f
commit d0d2d47885
2 changed files with 39 additions and 4 deletions

View File

@ -49,3 +49,31 @@ func PtracePeekUser(tid int, off uintptr) (uintptr, error) {
}
return val, nil
}
// ProcessVmRead calls process_vm_readv
func ProcessVmRead(tid int, addr uintptr, data []byte) (int, error) {
len_iov := uint64(len(data))
local_iov := sys.Iovec{Base: &data[0], Len: len_iov}
remote_iov := sys.Iovec{Base: (*byte)(unsafe.Pointer(addr)), Len: len_iov}
p_local := uintptr(unsafe.Pointer(&local_iov))
p_remote := uintptr(unsafe.Pointer(&remote_iov))
n, _, err := syscall.Syscall6(sys.SYS_PROCESS_VM_READV, uintptr(tid), p_local, 1, p_remote, 1, 0)
if err != syscall.Errno(0) {
return 0, err
}
return int(n), nil
}
// ProcessVmWrite calls process_vm_writev
func ProcessVmWrite(tid int, addr uintptr, data []byte) (int, error) {
len_iov := uint64(len(data))
local_iov := sys.Iovec{Base: &data[0], Len: len_iov}
remote_iov := sys.Iovec{Base: (*byte)(unsafe.Pointer(addr)), Len: len_iov}
p_local := uintptr(unsafe.Pointer(&local_iov))
p_remote := uintptr(unsafe.Pointer(&remote_iov))
n, _, err := syscall.Syscall6(sys.SYS_PROCESS_VM_WRITEV, uintptr(tid), p_local, 1, p_remote, 1, 0)
if err != syscall.Errno(0) {
return 0, err
}
return int(n), nil
}

View File

@ -90,7 +90,14 @@ func (t *Thread) WriteMemory(addr uintptr, data []byte) (written int, err error)
if len(data) == 0 {
return
}
t.dbp.execPtraceFunc(func() { written, err = sys.PtracePokeData(t.ID, addr, data) })
// ProcessVmWrite can't poke read-only memory like ptrace, so don't
// even bother for small writes -- likely breakpoints and such.
if len(data) > sys.SizeofPtr {
written, _ = ProcessVmWrite(t.ID, addr, data)
}
if written == 0 {
t.dbp.execPtraceFunc(func() { written, err = sys.PtracePokeData(t.ID, addr, data) })
}
return
}
@ -101,9 +108,9 @@ func (t *Thread) ReadMemory(data []byte, addr uintptr) (n int, err error) {
if len(data) == 0 {
return
}
t.dbp.execPtraceFunc(func() { _, err = sys.PtracePeekData(t.ID, addr, data) })
if err == nil {
n = len(data)
n, _ = ProcessVmRead(t.ID, addr, data)
if n == 0 {
t.dbp.execPtraceFunc(func() { n, err = sys.PtracePeekData(t.ID, addr, data) })
}
return
}