Skip to content

Commit 73ae9e0

Browse files
committed
Fix UnsupportedOperation when using ptrace_* functions
1 parent 5d29650 commit 73ae9e0

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/sys/ptrace.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,16 @@ fn ptrace_peek(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data
9191
}
9292
}
9393

94-
/// Function for ptrace requests that return values from the data field.
94+
/// Function for ptrace requests that return values from the data field.
9595
/// Some ptrace get requests populate structs or larger elements than c_long
9696
/// and therefore use the data field to return values. This function handles these
9797
/// requests.
9898
fn ptrace_get_data<T>(request: ptrace::PtraceRequest, pid: Pid) -> Result<T> {
99-
// Creates an uninitialized pointer to store result in
99+
// Creates an uninitialized pointer to store result in
100100
let data: Box<T> = Box::new(unsafe { mem::uninitialized() });
101101
let data: *mut c_void = unsafe { mem::transmute(data) };
102-
ptrace(request, pid, ptr::null_mut(), data)?;
102+
let res = unsafe { ffi::ptrace(request, pid.into(), ptr::null_mut(), data) };
103+
Errno::result(res)?;
103104
// Convert back into the original data format and return unboxed value
104105
let data: Box<T> = unsafe { mem::transmute(data) };
105106
Ok(*data)
@@ -114,7 +115,8 @@ pub fn ptrace_setoptions(pid: Pid, options: ptrace::PtraceOptions) -> Result<()>
114115
use self::ptrace::*;
115116
use std::ptr;
116117

117-
ptrace(PTRACE_SETOPTIONS, pid, ptr::null_mut(), options as *mut c_void).map(drop)
118+
let res = unsafe { ffi::ptrace(PTRACE_SETOPTIONS, pid.into(), ptr::null_mut(), options as *mut c_void) };
119+
Errno::result(res).map(|_| ())
118120
}
119121

120122
/// Gets a ptrace event as described by `ptrace(PTRACE_GETEVENTMSG,...)`

test/sys/test_ptrace.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use nix::errno::*;
33
use nix::unistd::*;
44
use nix::sys::ptrace::*;
55
use nix::sys::ptrace::ptrace::*;
6-
use std::ptr;
6+
7+
use std::{mem, ptr};
78

89
#[test]
910
fn test_ptrace() {
@@ -12,3 +13,36 @@ fn test_ptrace() {
1213
let err = ptrace(PTRACE_ATTACH, getpid(), ptr::null_mut(), ptr::null_mut()).unwrap_err();
1314
assert!(err == Error::Sys(Errno::EPERM) || err == Error::Sys(Errno::ENOSYS));
1415
}
16+
17+
// Just make sure ptrace_setoptions can be called at all, for now.
18+
#[test]
19+
fn test_ptrace_setoptions() {
20+
let err = ptrace_setoptions(getpid(), PTRACE_O_TRACESYSGOOD).unwrap_err();
21+
assert!(err != Error::UnsupportedOperation);
22+
}
23+
24+
// Just make sure ptrace_getevent can be called at all, for now.
25+
#[test]
26+
fn test_ptrace_getevent() {
27+
let err = ptrace_getevent(getpid()).unwrap_err();
28+
assert!(err != Error::UnsupportedOperation);
29+
}
30+
31+
// Just make sure ptrace_getsiginfo can be called at all, for now.
32+
#[test]
33+
fn test_ptrace_getsiginfo() {
34+
match ptrace_getsiginfo(getpid()) {
35+
Err(Error::UnsupportedOperation) => panic!("ptrace_getsiginfo returns Error::UnsupportedOperation!"),
36+
_ => (),
37+
}
38+
}
39+
40+
// Just make sure ptrace_setsiginfo can be called at all, for now.
41+
#[test]
42+
fn test_ptrace_setsiginfo() {
43+
let siginfo = unsafe { mem::uninitialized() };
44+
match ptrace_setsiginfo(getpid(), &siginfo) {
45+
Err(Error::UnsupportedOperation) => panic!("ptrace_setsiginfo returns Error::UnsupportedOperation!"),
46+
_ => (),
47+
}
48+
}

0 commit comments

Comments
 (0)