Skip to content

Commit bd05d72

Browse files
bors[bot]xd009642
andcommitted
Merge #958
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>
2 parents 19affae + 12578de commit bd05d72

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2727
([#955](https://github.com/nix-rust/nix/pull/955))
2828
- Added support for `ptrace` on BSD operating systems ([#949](https://github.com/nix-rust/nix/pull/949))
2929
- Added `ptrace` functions for reads and writes to tracee memory and ptrace kill
30-
([#949](https://github.com/nix-rust/nix/pull/949))
30+
([#949](https://github.com/nix-rust/nix/pull/949)) ([#958](https://github.com/nix-rust/nix/pull/958))
3131
- Added a `acct` wrapper module for enabling and disabling process accounting
3232
([#952](https://github.com/nix-rust/nix/pull/952))
3333

src/sys/ptrace/linux.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use libc::{self, c_void, c_long, siginfo_t};
77
use ::unistd::Pid;
88
use sys::signal::Signal;
99

10+
pub type AddressType = *mut ::libc::c_void;
1011

1112
cfg_if! {
1213
if #[cfg(any(all(target_os = "linux", arch = "s390x"),
@@ -170,18 +171,17 @@ libc_bitflags! {
170171
since="0.10.0",
171172
note="usages of `ptrace()` should be replaced with the specialized helper functions instead"
172173
)]
173-
pub unsafe fn ptrace(request: Request, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
174+
pub unsafe fn ptrace(request: Request, pid: Pid, addr: AddressType, data: *mut c_void) -> Result<c_long> {
174175
use self::Request::*;
175176
match request {
176-
PTRACE_PEEKTEXT | PTRACE_PEEKDATA | PTRACE_PEEKUSER | PTRACE_GETSIGINFO |
177+
PTRACE_PEEKTEXT | PTRACE_PEEKDATA | PTRACE_GETSIGINFO |
177178
PTRACE_GETEVENTMSG | PTRACE_SETSIGINFO | PTRACE_SETOPTIONS |
178-
PTRACE_POKETEXT | PTRACE_POKEDATA | PTRACE_POKEUSER |
179-
PTRACE_KILL => Err(Error::UnsupportedOperation),
179+
PTRACE_POKETEXT | PTRACE_POKEDATA | PTRACE_KILL => Err(Error::UnsupportedOperation),
180180
_ => ptrace_other(request, pid, addr, data)
181181
}
182182
}
183183

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

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

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

369369

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

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

0 commit comments

Comments
 (0)