1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use frida_gum_sys as gum_sys;
use gum_sys::GumCpuContext;
use paste::paste;
use std::marker::PhantomData;
use std::os::raw::c_void;
macro_rules! cpu_accesors {
($reg:ty,$($name:ident),*) => {
$(
pub fn $name(&self) -> $reg {
unsafe { (*self.cpu_context).$name }
}
paste! {
pub fn [<set_ $name>](&mut self, $name: $reg) {
unsafe { (*self.cpu_context).$name = $name }
}
}
)*
}
}
pub struct CpuContext<'a> {
cpu_context: *mut GumCpuContext,
phantom: PhantomData<&'a GumCpuContext>,
}
impl<'a> CpuContext<'a> {
pub(crate) fn from_raw(cpu_context: *mut GumCpuContext) -> CpuContext<'a> {
CpuContext {
cpu_context,
phantom: PhantomData,
}
}
pub fn arg(&self, n: u32) -> usize {
unsafe { gum_sys::gum_cpu_context_get_nth_argument(self.cpu_context, n) as usize }
}
pub fn set_arg(&mut self, n: u32, value: usize) {
unsafe {
gum_sys::gum_cpu_context_replace_nth_argument(self.cpu_context, n, value as *mut c_void)
}
}
pub fn return_value(&self) -> usize {
unsafe { gum_sys::gum_cpu_context_get_return_value(self.cpu_context) as usize }
}
pub fn set_return_value(&mut self, value: usize) {
unsafe {
gum_sys::gum_cpu_context_replace_return_value(self.cpu_context, value as *mut c_void)
}
}
#[cfg(target_arch = "x86_64")]
cpu_accesors!(
u64, rip, r15, r14, r13, r12, r11, r10, r9, r8, rdi, rsi, rbp, rsp, rbx, rdx, rcx, rax
);
#[cfg(target_arch = "x86")]
cpu_accesors!(u32, eip, edi, esi, ebp, esp, ebx, edx, ecx, eax);
#[cfg(target_arch = "arm")]
cpu_accesors!(u32, cpsr, pc, sp, r8, r9, r10, r11, r12, lr);
#[cfg(target_arch = "aarch64")]
cpu_accesors!(u64, pc, sp, fp, lr);
#[cfg(target_arch = "aarch64")]
pub fn reg(&self, index: usize) -> u64 {
assert!(index < 29);
unsafe { (*self.cpu_context).x[index] }
}
#[cfg(target_arch = "aarch64")]
pub fn set_reg(&mut self, index: usize, value: u64) {
assert!(index < 29);
unsafe { (*self.cpu_context).x[index] = value };
}
}