Skip to content

Commit

Permalink
Move to Rust types
Browse files Browse the repository at this point in the history
  • Loading branch information
marmistrz committed Jul 19, 2017
1 parent 26d0158 commit 8a85c39
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/sys/ptrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub fn ptrace_setsiginfo(pid: Pid, sig: &siginfo_t) -> Result<()> {
}
}

//-------------------------- Second part: a low-level wrapper for ptrace ----------------------//
//-------------------------- Second part: a high-level wrapper for ptrace ----------------------//

#[cfg(target_arch = "x86_64")]
// We're going to export it anyway
Expand Down Expand Up @@ -187,13 +187,15 @@ pub enum Register {
GS = 26 * 8,
}

type Word = usize;

/// Makes the `PTRACE_SYSCALL` request to ptrace
pub fn syscall(pid: Pid) -> Result<()> {
ptrace(ptrace::PTRACE_SYSCALL, pid, ptr::null_mut(), ptr::null_mut()).map(|_| ()) // ignore the useless return value
}

/// Makes the `PTRACE_PEEKUSER` request to ptrace
pub fn peekuser(pid: Pid, reg: Register) -> Result<c_long> {
pub fn peekuser(pid: Pid, reg: Register) -> Result<Word> {
let reg_arg = (reg as i32) as *mut c_void;
ptrace(ptrace::PTRACE_PEEKUSER, pid, reg_arg, ptr::null_mut())
}
Expand All @@ -212,7 +214,7 @@ pub fn traceme() -> Result<()> {
///
/// This function allows to access arbitrary data in the traced process
/// and may crash the inferior if used incorrectly and is thus marked `unsafe`.
pub unsafe fn peekdata(pid: Pid, addr: c_long) -> Result<c_long> {
pub unsafe fn peekdata(pid: Pid, addr: usize) -> Result<Word> {
ptrace(ptrace::PTRACE_PEEKDATA, pid, addr as *mut c_void, ptr::null_mut())
}

Expand All @@ -221,11 +223,35 @@ pub unsafe fn peekdata(pid: Pid, addr: c_long) -> Result<c_long> {
/// This function allows to access arbitrary data in the traced process
/// and may crash the inferior or introduce race conditions if used
/// incorrectly and is thus marked `unsafe`.
pub unsafe fn pokedata(pid: Pid, addr: c_long, val: c_long) -> Result<()> {
pub unsafe fn pokedata(pid: Pid, addr: usize, val: Word) -> Result<()> {
ptrace(
ptrace::PTRACE_POKEDATA,
pid,
addr as *mut c_void,
val as *mut c_void,
).map(|_| ()) // ignore the useless return value
}
}


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_registry() {
let mut reg = OverrideRegistry::new();
let atenter = |_| {
HandlerData {
buflen: 0,
bufptr: 0,
}
};
let atexit = |_, _| {};

reg.add(17, atenter, atexit);
let el = reg.find(17).unwrap();
assert_eq!(el.syscall, 17);
let len = (el.atenter)(Pid::from_raw(17)).buflen;
assert_eq!(len, 0);
}
}

0 comments on commit 8a85c39

Please sign in to comment.