Skip to content

Commit 8a85c39

Browse files
committed
Move to Rust types
1 parent 26d0158 commit 8a85c39

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/sys/ptrace.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub fn ptrace_setsiginfo(pid: Pid, sig: &siginfo_t) -> Result<()> {
151151
}
152152
}
153153

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

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

190+
type Word = usize;
191+
190192
/// Makes the `PTRACE_SYSCALL` request to ptrace
191193
pub fn syscall(pid: Pid) -> Result<()> {
192194
ptrace(ptrace::PTRACE_SYSCALL, pid, ptr::null_mut(), ptr::null_mut()).map(|_| ()) // ignore the useless return value
193195
}
194196

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

@@ -221,11 +223,35 @@ pub unsafe fn peekdata(pid: Pid, addr: c_long) -> Result<c_long> {
221223
/// This function allows to access arbitrary data in the traced process
222224
/// and may crash the inferior or introduce race conditions if used
223225
/// incorrectly and is thus marked `unsafe`.
224-
pub unsafe fn pokedata(pid: Pid, addr: c_long, val: c_long) -> Result<()> {
226+
pub unsafe fn pokedata(pid: Pid, addr: usize, val: Word) -> Result<()> {
225227
ptrace(
226228
ptrace::PTRACE_POKEDATA,
227229
pid,
228230
addr as *mut c_void,
229231
val as *mut c_void,
230232
).map(|_| ()) // ignore the useless return value
231-
}
233+
}
234+
235+
236+
#[cfg(test)]
237+
mod tests {
238+
use super::*;
239+
240+
#[test]
241+
fn test_registry() {
242+
let mut reg = OverrideRegistry::new();
243+
let atenter = |_| {
244+
HandlerData {
245+
buflen: 0,
246+
bufptr: 0,
247+
}
248+
};
249+
let atexit = |_, _| {};
250+
251+
reg.add(17, atenter, atexit);
252+
let el = reg.find(17).unwrap();
253+
assert_eq!(el.syscall, 17);
254+
let len = (el.atenter)(Pid::from_raw(17)).buflen;
255+
assert_eq!(len, 0);
256+
}
257+
}

0 commit comments

Comments
 (0)