From 73ae9e012e65f631dd96f45cf7d25de678958600 Mon Sep 17 00:00:00 2001 From: Bryant Mairs Date: Mon, 17 Jul 2017 16:12:40 -0700 Subject: Fix UnsupportedOperation when using ptrace_* functions --- src/sys/ptrace.rs | 10 ++++++---- test/sys/test_ptrace.rs | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/sys/ptrace.rs b/src/sys/ptrace.rs index bf395259..4c3d29b1 100644 --- a/src/sys/ptrace.rs +++ b/src/sys/ptrace.rs @@ -91,15 +91,16 @@ fn ptrace_peek(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data } } -/// Function for ptrace requests that return values from the data field. +/// Function for ptrace requests that return values from the data field. /// Some ptrace get requests populate structs or larger elements than c_long /// and therefore use the data field to return values. This function handles these /// requests. fn ptrace_get_data(request: ptrace::PtraceRequest, pid: Pid) -> Result { - // Creates an uninitialized pointer to store result in + // Creates an uninitialized pointer to store result in let data: Box = Box::new(unsafe { mem::uninitialized() }); let data: *mut c_void = unsafe { mem::transmute(data) }; - ptrace(request, pid, ptr::null_mut(), data)?; + let res = unsafe { ffi::ptrace(request, pid.into(), ptr::null_mut(), data) }; + Errno::result(res)?; // Convert back into the original data format and return unboxed value let data: Box = unsafe { mem::transmute(data) }; Ok(*data) @@ -114,7 +115,8 @@ pub fn ptrace_setoptions(pid: Pid, options: ptrace::PtraceOptions) -> Result<()> use self::ptrace::*; use std::ptr; - ptrace(PTRACE_SETOPTIONS, pid, ptr::null_mut(), options as *mut c_void).map(drop) + let res = unsafe { ffi::ptrace(PTRACE_SETOPTIONS, pid.into(), ptr::null_mut(), options as *mut c_void) }; + Errno::result(res).map(|_| ()) } /// Gets a ptrace event as described by `ptrace(PTRACE_GETEVENTMSG,...)` diff --git a/test/sys/test_ptrace.rs b/test/sys/test_ptrace.rs index 6318495a..57b452cb 100644 --- a/test/sys/test_ptrace.rs +++ b/test/sys/test_ptrace.rs @@ -3,7 +3,8 @@ use nix::errno::*; use nix::unistd::*; use nix::sys::ptrace::*; use nix::sys::ptrace::ptrace::*; -use std::ptr; + +use std::{mem, ptr}; #[test] fn test_ptrace() { @@ -12,3 +13,36 @@ fn test_ptrace() { let err = ptrace(PTRACE_ATTACH, getpid(), ptr::null_mut(), ptr::null_mut()).unwrap_err(); assert!(err == Error::Sys(Errno::EPERM) || err == Error::Sys(Errno::ENOSYS)); } + +// Just make sure ptrace_setoptions can be called at all, for now. +#[test] +fn test_ptrace_setoptions() { + let err = ptrace_setoptions(getpid(), PTRACE_O_TRACESYSGOOD).unwrap_err(); + assert!(err != Error::UnsupportedOperation); +} + +// Just make sure ptrace_getevent can be called at all, for now. +#[test] +fn test_ptrace_getevent() { + let err = ptrace_getevent(getpid()).unwrap_err(); + assert!(err != Error::UnsupportedOperation); +} + +// Just make sure ptrace_getsiginfo can be called at all, for now. +#[test] +fn test_ptrace_getsiginfo() { + match ptrace_getsiginfo(getpid()) { + Err(Error::UnsupportedOperation) => panic!("ptrace_getsiginfo returns Error::UnsupportedOperation!"), + _ => (), + } +} + +// Just make sure ptrace_setsiginfo can be called at all, for now. +#[test] +fn test_ptrace_setsiginfo() { + let siginfo = unsafe { mem::uninitialized() }; + match ptrace_setsiginfo(getpid(), &siginfo) { + Err(Error::UnsupportedOperation) => panic!("ptrace_setsiginfo returns Error::UnsupportedOperation!"), + _ => (), + } +} -- cgit v1.2.3 From bba9e436393e3606bfe19a2a1d40c849a9242f4a Mon Sep 17 00:00:00 2001 From: Bryant Mairs Date: Mon, 17 Jul 2017 16:16:05 -0700 Subject: Refactor ptrace_get_data The boxing and unboxing was unnecessary and instead the references to the type on the stack can just be cast. --- src/sys/ptrace.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/sys/ptrace.rs b/src/sys/ptrace.rs index 4c3d29b1..73ca9160 100644 --- a/src/sys/ptrace.rs +++ b/src/sys/ptrace.rs @@ -97,13 +97,10 @@ fn ptrace_peek(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data /// requests. fn ptrace_get_data(request: ptrace::PtraceRequest, pid: Pid) -> Result { // Creates an uninitialized pointer to store result in - let data: Box = Box::new(unsafe { mem::uninitialized() }); - let data: *mut c_void = unsafe { mem::transmute(data) }; - let res = unsafe { ffi::ptrace(request, pid.into(), ptr::null_mut(), data) }; + let data: T = unsafe { mem::uninitialized() }; + let res = unsafe { ffi::ptrace(request, pid.into(), ptr::null_mut(), &data as *const _ as *const c_void) }; Errno::result(res)?; - // Convert back into the original data format and return unboxed value - let data: Box = unsafe { mem::transmute(data) }; - Ok(*data) + Ok(data) } fn ptrace_other(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result { -- cgit v1.2.3