Skip to content

Commit

Permalink
Merge #958
Browse files Browse the repository at this point in the history
958: Added AddressType type to ptrace::linux + peek/poke user fix r=asomers a=xd009642

This was added to the BSD ptrace API and probably should have been added to the linux API to make it easier to write code for both platforms without the user having to reexport the types to their own crate.

I went to use the latest nix myself and found that having this added would improve usability for nix users.

Could potentially add a type for what they return (BSDs: `c_int`, linux: `c_long`). Data input might be trickier as linux is `*mut c_void` and BSD just takes data as `c_int`.

Co-authored-by: xd009642 <danielmckenna93@gmail.com>
  • Loading branch information
bors[bot] and xd009642 committed Oct 30, 2018
2 parents 19affae + 12578de commit bd05d72
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -27,7 +27,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#955](https://github.com/nix-rust/nix/pull/955))
- Added support for `ptrace` on BSD operating systems ([#949](https://github.com/nix-rust/nix/pull/949))
- Added `ptrace` functions for reads and writes to tracee memory and ptrace kill
([#949](https://github.com/nix-rust/nix/pull/949))
([#949](https://github.com/nix-rust/nix/pull/949)) ([#958](https://github.com/nix-rust/nix/pull/958))
- Added a `acct` wrapper module for enabling and disabling process accounting
([#952](https://github.com/nix-rust/nix/pull/952))

Expand Down
16 changes: 8 additions & 8 deletions src/sys/ptrace/linux.rs
Expand Up @@ -7,6 +7,7 @@ use libc::{self, c_void, c_long, siginfo_t};
use ::unistd::Pid;
use sys::signal::Signal;

pub type AddressType = *mut ::libc::c_void;

cfg_if! {
if #[cfg(any(all(target_os = "linux", arch = "s390x"),
Expand Down Expand Up @@ -170,18 +171,17 @@ libc_bitflags! {
since="0.10.0",
note="usages of `ptrace()` should be replaced with the specialized helper functions instead"
)]
pub unsafe fn ptrace(request: Request, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
pub unsafe fn ptrace(request: Request, pid: Pid, addr: AddressType, data: *mut c_void) -> Result<c_long> {
use self::Request::*;
match request {
PTRACE_PEEKTEXT | PTRACE_PEEKDATA | PTRACE_PEEKUSER | PTRACE_GETSIGINFO |
PTRACE_PEEKTEXT | PTRACE_PEEKDATA | PTRACE_GETSIGINFO |
PTRACE_GETEVENTMSG | PTRACE_SETSIGINFO | PTRACE_SETOPTIONS |
PTRACE_POKETEXT | PTRACE_POKEDATA | PTRACE_POKEUSER |
PTRACE_KILL => Err(Error::UnsupportedOperation),
PTRACE_POKETEXT | PTRACE_POKEDATA | PTRACE_KILL => Err(Error::UnsupportedOperation),
_ => ptrace_other(request, pid, addr, data)
}
}

fn ptrace_peek(request: Request, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
fn ptrace_peek(request: Request, pid: Pid, addr: AddressType, data: *mut c_void) -> Result<c_long> {
let ret = unsafe {
Errno::clear();
libc::ptrace(request as RequestType, libc::pid_t::from(pid), addr, data)
Expand Down Expand Up @@ -209,7 +209,7 @@ fn ptrace_get_data<T>(request: Request, pid: Pid) -> Result<T> {
Ok(data)
}

unsafe fn ptrace_other(request: Request, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
unsafe fn ptrace_other(request: Request, pid: Pid, addr: AddressType, data: *mut c_void) -> Result<c_long> {
Errno::result(libc::ptrace(request as RequestType, libc::pid_t::from(pid), addr, data)).map(|_| 0)
}

Expand Down Expand Up @@ -368,12 +368,12 @@ pub fn step<T: Into<Option<Signal>>>(pid: Pid, sig: T) -> Result<()> {


/// Reads a word from a processes memory at the given address
pub fn read(pid: Pid, addr: *mut c_void) -> Result<c_long> {
pub fn read(pid: Pid, addr: AddressType) -> Result<c_long> {
ptrace_peek(Request::PTRACE_PEEKDATA, pid, addr, ptr::null_mut())
}

/// Writes a word into the processes memory at the given address
pub fn write(pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<()> {
pub fn write(pid: Pid, addr: AddressType, data: *mut c_void) -> Result<()> {
unsafe {
ptrace_other(Request::PTRACE_POKEDATA, pid, addr, data).map(|_| ())
}
Expand Down

0 comments on commit bd05d72

Please sign in to comment.