Skip to content

Commit f605fba

Browse files
committed
Add implementation of PTRACE_{GET,SET}REGSET
Also added `PTRACE_{GET,SET}REGS` for most platforms other than x86 using aforementioned implementation.
1 parent 4ab23c3 commit f605fba

File tree

2 files changed

+141
-6
lines changed

2 files changed

+141
-6
lines changed

src/sys/ptrace/linux.rs

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ pub type AddressType = *mut ::libc::c_void;
1717
target_arch = "x86_64",
1818
any(target_env = "gnu", target_env = "musl")
1919
),
20-
all(target_arch = "x86", target_env = "gnu")
21-
)
20+
all(target_arch = "x86", target_env = "gnu"),
21+
all(target_arch = "aarch64", target_env = "gnu"),
22+
all(target_arch = "riscv64", target_env = "gnu"),
23+
),
2224
))]
2325
use libc::user_regs_struct;
2426

@@ -170,6 +172,29 @@ libc_enum! {
170172
}
171173
}
172174

175+
libc_enum! {
176+
#[cfg(all(
177+
target_os = "linux",
178+
target_env = "gnu",
179+
any(
180+
target_arch = "x86_64",
181+
target_arch = "x86",
182+
target_arch = "aarch64",
183+
target_arch = "riscv64",
184+
)
185+
))]
186+
#[repr(i32)]
187+
/// Defining a specific register set, as used in [`getregset`] and [`setregset`].
188+
#[non_exhaustive]
189+
pub enum RegisterSet {
190+
NT_PRSTATUS,
191+
NT_PRFPREG,
192+
NT_PRPSINFO,
193+
NT_TASKSTRUCT,
194+
NT_AUXV,
195+
}
196+
}
197+
173198
libc_bitflags! {
174199
/// Ptrace options used in conjunction with the PTRACE_SETOPTIONS request.
175200
/// See `man ptrace` for more details.
@@ -231,6 +256,48 @@ pub fn getregs(pid: Pid) -> Result<user_regs_struct> {
231256
ptrace_get_data::<user_regs_struct>(Request::PTRACE_GETREGS, pid)
232257
}
233258

259+
/// Get user registers, as with `ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, ...)`
260+
#[cfg(all(
261+
target_os = "linux",
262+
target_env = "gnu",
263+
any(
264+
target_arch = "aarch64",
265+
target_arch = "riscv64",
266+
)
267+
))]
268+
pub fn getregs(pid: Pid) -> Result<user_regs_struct> {
269+
getregset(pid, RegisterSet::NT_PRSTATUS)
270+
}
271+
272+
/// Get a particular set of user registers, as with `ptrace(PTRACE_GETREGSET, ...)`
273+
#[cfg(all(
274+
target_os = "linux",
275+
target_env = "gnu",
276+
any(
277+
target_arch = "x86_64",
278+
target_arch = "x86",
279+
target_arch = "aarch64",
280+
target_arch = "riscv64",
281+
)
282+
))]
283+
pub fn getregset(pid: Pid, set: RegisterSet) -> Result<user_regs_struct> {
284+
let request = Request::PTRACE_GETREGSET;
285+
let mut data = mem::MaybeUninit::<user_regs_struct>::uninit();
286+
let mut iov = libc::iovec {
287+
iov_base: data.as_mut_ptr() as *mut _,
288+
iov_len: mem::size_of::<user_regs_struct>(),
289+
};
290+
unsafe {
291+
ptrace_other(
292+
request,
293+
pid,
294+
set as i32 as AddressType,
295+
&mut iov as *mut _ as *mut c_void,
296+
)?;
297+
};
298+
Ok(unsafe { data.assume_init() })
299+
}
300+
234301
/// Set user registers, as with `ptrace(PTRACE_SETREGS, ...)`
235302
#[cfg(all(
236303
target_os = "linux",
@@ -254,6 +321,50 @@ pub fn setregs(pid: Pid, regs: user_regs_struct) -> Result<()> {
254321
Errno::result(res).map(drop)
255322
}
256323

324+
/// Set user registers, as with `ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, ...)`
325+
#[cfg(all(
326+
target_os = "linux",
327+
target_env = "gnu",
328+
any(
329+
target_arch = "aarch64",
330+
target_arch = "riscv64",
331+
)
332+
))]
333+
pub fn setregs(pid: Pid, regs: user_regs_struct) -> Result<()> {
334+
setregset(pid, RegisterSet::NT_PRSTATUS, regs)
335+
}
336+
337+
/// Set a particular set of user registers, as with `ptrace(PTRACE_SETREGSET, ...)`
338+
#[cfg(all(
339+
target_os = "linux",
340+
target_env = "gnu",
341+
any(
342+
target_arch = "x86_64",
343+
target_arch = "x86",
344+
target_arch = "aarch64",
345+
target_arch = "riscv64",
346+
)
347+
))]
348+
pub fn setregset(
349+
pid: Pid,
350+
set: RegisterSet,
351+
regs: user_regs_struct,
352+
) -> Result<()> {
353+
let iov = libc::iovec {
354+
iov_base: &regs as *const _ as *mut c_void,
355+
iov_len: mem::size_of::<user_regs_struct>(),
356+
};
357+
unsafe {
358+
ptrace_other(
359+
Request::PTRACE_SETREGSET,
360+
pid,
361+
set as i32 as AddressType,
362+
&iov as *const _ as *mut c_void,
363+
)?;
364+
}
365+
Ok(())
366+
}
367+
257368
/// Function for ptrace requests that return values from the data field.
258369
/// Some ptrace get requests populate structs or larger elements than `c_long`
259370
/// and therefore use the data field to return values. This function handles these

test/sys/test_ptrace.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#[cfg(all(
22
target_os = "linux",
3-
any(target_arch = "x86_64", target_arch = "x86"),
4-
target_env = "gnu"
3+
target_env = "gnu",
4+
any(
5+
target_arch = "x86_64",
6+
target_arch = "x86",
7+
target_arch = "aarch64",
8+
target_arch = "riscv64",
9+
)
510
))]
611
use memoffset::offset_of;
712
use nix::errno::Errno;
@@ -179,8 +184,13 @@ fn test_ptrace_interrupt() {
179184
// ptrace::{setoptions, getregs} are only available in these platforms
180185
#[cfg(all(
181186
target_os = "linux",
182-
any(target_arch = "x86_64", target_arch = "x86"),
183-
target_env = "gnu"
187+
target_env = "gnu",
188+
any(
189+
target_arch = "x86_64",
190+
target_arch = "x86",
191+
target_arch = "aarch64",
192+
target_arch = "riscv64",
193+
)
184194
))]
185195
#[test]
186196
fn test_ptrace_syscall() {
@@ -226,14 +236,28 @@ fn test_ptrace_syscall() {
226236
let get_syscall_id =
227237
|| ptrace::getregs(child).unwrap().orig_eax as libc::c_long;
228238

239+
#[cfg(target_arch = "aarch64")]
240+
let get_syscall_id =
241+
|| ptrace::getregs(child).unwrap().regs[8] as libc::c_long;
242+
243+
#[cfg(target_arch = "riscv64")]
244+
let get_syscall_id =
245+
|| ptrace::getregs(child).unwrap().a7 as libc::c_long;
246+
229247
// this duplicates `get_syscall_id` for the purpose of testing `ptrace::read_user`.
230248
#[cfg(target_arch = "x86_64")]
231249
let rax_offset = offset_of!(libc::user_regs_struct, orig_rax);
232250
#[cfg(target_arch = "x86")]
233251
let rax_offset = offset_of!(libc::user_regs_struct, orig_eax);
252+
#[cfg(target_arch = "aarch64")]
253+
let rax_offset = offset_of!(libc::user_regs_struct, regs)
254+
+ 8 * mem::size_of::<libc::c_ulonglong>();
255+
#[cfg(target_arch = "riscv64")]
256+
let rax_offset = offset_of!(libc::user_regs_struct, a7);
234257

235258
let get_syscall_from_user_area = || {
236259
// Find the offset of `user.regs.rax` (or `user.regs.eax` for x86)
260+
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
237261
let rax_offset = offset_of!(libc::user, regs) + rax_offset;
238262
ptrace::read_user(child, rax_offset as _).unwrap()
239263
as libc::c_long

0 commit comments

Comments
 (0)