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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//!Structures to access a CPU context.
//!
//!The structures in this module give access to the values of a CPU register
//!at various points in Frida usage. As registers are architecture dependent,
//!Frida APIs will return a [`CpuContext`](CpuContext) has to be unwrapped to
//!the appropriate struct.
use crate::nativepointer::NativePointer;
use crate::plumbing;
use js_sys::Reflect;
use wasm_bindgen::prelude::JsValue;

#[derive(Debug)]
pub enum CpuContext {
    Ia32CpuContext(Ia32CpuContext),
    X64CpuContext(X64CpuContext),
    ArmCpuContext(ArmCpuContext),
    Arm64CpuContext(Arm64CpuContext),
    MipsCpuContext(MipsCpuContext),
    PortableCpuContext(PortableCpuContext),
}

impl From<plumbing::cpu::CpuContext> for CpuContext {
    fn from(m: plumbing::cpu::CpuContext) -> Self {
        match crate::process::get_arch() {
            "ia32" => Self::Ia32CpuContext(Ia32CpuContext::from(m)),
            "x64" => Self::X64CpuContext(X64CpuContext::from(m)),
            "arm" => Self::ArmCpuContext(ArmCpuContext::from(m)),
            "arm64" => Self::Arm64CpuContext(Arm64CpuContext::from(m)),
            "mips" => Self::MipsCpuContext(MipsCpuContext::from(m)),
            _ => Self::PortableCpuContext(PortableCpuContext::from(m)),
        }
    }
}

#[derive(Debug)]
pub struct PortableCpuContext {
    pub pc: NativePointer,
    pub sp: NativePointer,
}

impl From<plumbing::cpu::CpuContext> for PortableCpuContext {
    fn from(m: plumbing::cpu::CpuContext) -> Self {
        PortableCpuContext {
            pc: NativePointer::from(Reflect::get(&m, &JsValue::from_str("pc")).unwrap()),
            sp: NativePointer::from(Reflect::get(&m, &JsValue::from_str("sp")).unwrap()),
        }
    }
}

#[derive(Debug)]
pub struct Ia32CpuContext {
    pub pc: NativePointer,
    pub sp: NativePointer,
    pub eax: NativePointer,
    pub ecx: NativePointer,
    pub edx: NativePointer,
    pub ebx: NativePointer,
    pub esp: NativePointer,
    pub ebp: NativePointer,
    pub esi: NativePointer,
    pub edi: NativePointer,
    pub eip: NativePointer,
}

impl From<plumbing::cpu::CpuContext> for Ia32CpuContext {
    fn from(m: plumbing::cpu::CpuContext) -> Self {
        Ia32CpuContext {
            pc: NativePointer::from(Reflect::get(&m, &JsValue::from_str("pc")).unwrap()),
            sp: NativePointer::from(Reflect::get(&m, &JsValue::from_str("sp")).unwrap()),
            eax: NativePointer::from(Reflect::get(&m, &JsValue::from_str("eax")).unwrap()),
            ecx: NativePointer::from(Reflect::get(&m, &JsValue::from_str("ecx")).unwrap()),
            edx: NativePointer::from(Reflect::get(&m, &JsValue::from_str("edx")).unwrap()),
            ebx: NativePointer::from(Reflect::get(&m, &JsValue::from_str("ebx")).unwrap()),
            esp: NativePointer::from(Reflect::get(&m, &JsValue::from_str("esp")).unwrap()),
            ebp: NativePointer::from(Reflect::get(&m, &JsValue::from_str("ebp")).unwrap()),
            esi: NativePointer::from(Reflect::get(&m, &JsValue::from_str("esi")).unwrap()),
            edi: NativePointer::from(Reflect::get(&m, &JsValue::from_str("edi")).unwrap()),
            eip: NativePointer::from(Reflect::get(&m, &JsValue::from_str("eip")).unwrap()),
        }
    }
}

#[derive(Debug)]
pub struct X64CpuContext {
    pub pc: NativePointer,
    pub sp: NativePointer,
    pub rax: NativePointer,
    pub rcx: NativePointer,
    pub rdx: NativePointer,
    pub rbx: NativePointer,
    pub rsp: NativePointer,
    pub rbp: NativePointer,
    pub rsi: NativePointer,
    pub rdi: NativePointer,
    pub r8: NativePointer,
    pub r10: NativePointer,
    pub r11: NativePointer,
    pub r12: NativePointer,
    pub r13: NativePointer,
    pub r14: NativePointer,
    pub r15: NativePointer,
    pub rip: NativePointer,
}

impl From<plumbing::cpu::CpuContext> for X64CpuContext {
    fn from(m: plumbing::cpu::CpuContext) -> Self {
        X64CpuContext {
            pc: NativePointer::from(Reflect::get(&m, &JsValue::from_str("pc")).unwrap()),
            sp: NativePointer::from(Reflect::get(&m, &JsValue::from_str("sp")).unwrap()),
            rax: NativePointer::from(Reflect::get(&m, &JsValue::from_str("rax")).unwrap()),
            rcx: NativePointer::from(Reflect::get(&m, &JsValue::from_str("rcx")).unwrap()),
            rdx: NativePointer::from(Reflect::get(&m, &JsValue::from_str("rdx")).unwrap()),
            rbx: NativePointer::from(Reflect::get(&m, &JsValue::from_str("rbx")).unwrap()),
            rsp: NativePointer::from(Reflect::get(&m, &JsValue::from_str("rsp")).unwrap()),
            rbp: NativePointer::from(Reflect::get(&m, &JsValue::from_str("rbp")).unwrap()),
            rsi: NativePointer::from(Reflect::get(&m, &JsValue::from_str("rsi")).unwrap()),
            rdi: NativePointer::from(Reflect::get(&m, &JsValue::from_str("rdi")).unwrap()),
            r8: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r8")).unwrap()),
            r10: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r10")).unwrap()),
            r11: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r11")).unwrap()),
            r12: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r12")).unwrap()),
            r13: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r13")).unwrap()),
            r14: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r14")).unwrap()),
            r15: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r15")).unwrap()),
            rip: NativePointer::from(Reflect::get(&m, &JsValue::from_str("rip")).unwrap()),
        }
    }
}

#[derive(Debug)]
pub struct ArmCpuContext {
    pub pc: NativePointer,
    pub sp: NativePointer,
    pub r0: NativePointer,
    pub r1: NativePointer,
    pub r2: NativePointer,
    pub r3: NativePointer,
    pub r4: NativePointer,
    pub r5: NativePointer,
    pub r6: NativePointer,
    pub r7: NativePointer,
    pub r8: NativePointer,
    pub r9: NativePointer,
    pub r10: NativePointer,
    pub r11: NativePointer,
    pub r12: NativePointer,
    pub lr: NativePointer,
}

impl From<plumbing::cpu::CpuContext> for ArmCpuContext {
    fn from(m: plumbing::cpu::CpuContext) -> Self {
        ArmCpuContext {
            pc: NativePointer::from(Reflect::get(&m, &JsValue::from_str("pc")).unwrap()),
            sp: NativePointer::from(Reflect::get(&m, &JsValue::from_str("sp")).unwrap()),
            r0: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r0")).unwrap()),
            r1: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r1")).unwrap()),
            r2: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r2")).unwrap()),
            r3: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r3")).unwrap()),
            r4: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r4")).unwrap()),
            r5: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r5")).unwrap()),
            r6: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r6")).unwrap()),
            r7: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r7")).unwrap()),
            r8: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r8")).unwrap()),
            r9: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r9")).unwrap()),
            r10: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r10")).unwrap()),
            r11: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r11")).unwrap()),
            r12: NativePointer::from(Reflect::get(&m, &JsValue::from_str("r12")).unwrap()),
            lr: NativePointer::from(Reflect::get(&m, &JsValue::from_str("lr")).unwrap()),
        }
    }
}

#[derive(Debug)]
pub struct Arm64CpuContext {
    pub pc: NativePointer,
    pub sp: NativePointer,
    pub x0: NativePointer,
    pub x1: NativePointer,
    pub x2: NativePointer,
    pub x3: NativePointer,
    pub x4: NativePointer,
    pub x5: NativePointer,
    pub x6: NativePointer,
    pub x7: NativePointer,
    pub x8: NativePointer,
    pub x9: NativePointer,
    pub x10: NativePointer,
    pub x11: NativePointer,
    pub x12: NativePointer,
    pub x13: NativePointer,
    pub x14: NativePointer,
    pub x15: NativePointer,
    pub x16: NativePointer,
    pub x17: NativePointer,
    pub x18: NativePointer,
    pub x19: NativePointer,
    pub x20: NativePointer,
    pub x21: NativePointer,
    pub x22: NativePointer,
    pub x23: NativePointer,
    pub x24: NativePointer,
    pub x25: NativePointer,
    pub x26: NativePointer,
    pub x27: NativePointer,
    pub x28: NativePointer,
    pub fp: NativePointer,
    pub lr: NativePointer,
}

impl From<plumbing::cpu::CpuContext> for Arm64CpuContext {
    fn from(m: plumbing::cpu::CpuContext) -> Self {
        Arm64CpuContext {
            pc: NativePointer::from(Reflect::get(&m, &JsValue::from_str("pc")).unwrap()),
            sp: NativePointer::from(Reflect::get(&m, &JsValue::from_str("sp")).unwrap()),
            x0: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x0")).unwrap()),
            x1: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x1")).unwrap()),
            x2: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x2")).unwrap()),
            x3: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x3")).unwrap()),
            x4: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x4")).unwrap()),
            x5: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x5")).unwrap()),
            x6: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x6")).unwrap()),
            x7: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x7")).unwrap()),
            x8: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x8")).unwrap()),
            x9: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x9")).unwrap()),
            x10: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x10")).unwrap()),
            x11: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x11")).unwrap()),
            x12: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x12")).unwrap()),
            x13: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x13")).unwrap()),
            x14: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x14")).unwrap()),
            x15: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x15")).unwrap()),
            x16: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x16")).unwrap()),
            x17: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x17")).unwrap()),
            x18: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x18")).unwrap()),
            x19: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x19")).unwrap()),
            x20: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x20")).unwrap()),
            x21: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x21")).unwrap()),
            x22: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x22")).unwrap()),
            x23: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x23")).unwrap()),
            x24: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x24")).unwrap()),
            x25: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x25")).unwrap()),
            x26: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x26")).unwrap()),
            x27: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x27")).unwrap()),
            x28: NativePointer::from(Reflect::get(&m, &JsValue::from_str("x28")).unwrap()),
            fp: NativePointer::from(Reflect::get(&m, &JsValue::from_str("fp")).unwrap()),
            lr: NativePointer::from(Reflect::get(&m, &JsValue::from_str("lr")).unwrap()),
        }
    }
}

#[derive(Debug)]
pub struct MipsCpuContext {
    pub pc: NativePointer,
    pub sp: NativePointer,
    pub gp: NativePointer,
    pub fp: NativePointer,
    pub ra: NativePointer,
    pub hi: NativePointer,
    pub lo: NativePointer,
    pub at: NativePointer,
    pub v0: NativePointer,
    pub v1: NativePointer,
    pub a0: NativePointer,
    pub a1: NativePointer,
    pub a2: NativePointer,
    pub a3: NativePointer,
    pub t0: NativePointer,
    pub t1: NativePointer,
    pub t2: NativePointer,
    pub t3: NativePointer,
    pub t4: NativePointer,
    pub t5: NativePointer,
    pub t6: NativePointer,
    pub t7: NativePointer,
    pub t8: NativePointer,
    pub t9: NativePointer,
    pub s0: NativePointer,
    pub s1: NativePointer,
    pub s2: NativePointer,
    pub s3: NativePointer,
    pub s4: NativePointer,
    pub s5: NativePointer,
    pub s6: NativePointer,
    pub s7: NativePointer,
    pub k0: NativePointer,
    pub k1: NativePointer,
}

impl From<plumbing::cpu::CpuContext> for MipsCpuContext {
    fn from(m: plumbing::cpu::CpuContext) -> Self {
        MipsCpuContext {
            pc: NativePointer::from(Reflect::get(&m, &JsValue::from_str("pc")).unwrap()),
            sp: NativePointer::from(Reflect::get(&m, &JsValue::from_str("sp")).unwrap()),
            gp: NativePointer::from(Reflect::get(&m, &JsValue::from_str("gp")).unwrap()),
            fp: NativePointer::from(Reflect::get(&m, &JsValue::from_str("fp")).unwrap()),
            ra: NativePointer::from(Reflect::get(&m, &JsValue::from_str("ra")).unwrap()),
            hi: NativePointer::from(Reflect::get(&m, &JsValue::from_str("hi")).unwrap()),
            lo: NativePointer::from(Reflect::get(&m, &JsValue::from_str("lo")).unwrap()),
            at: NativePointer::from(Reflect::get(&m, &JsValue::from_str("at")).unwrap()),
            v0: NativePointer::from(Reflect::get(&m, &JsValue::from_str("v0")).unwrap()),
            v1: NativePointer::from(Reflect::get(&m, &JsValue::from_str("v1")).unwrap()),
            a0: NativePointer::from(Reflect::get(&m, &JsValue::from_str("a0")).unwrap()),
            a1: NativePointer::from(Reflect::get(&m, &JsValue::from_str("a1")).unwrap()),
            a2: NativePointer::from(Reflect::get(&m, &JsValue::from_str("a2")).unwrap()),
            a3: NativePointer::from(Reflect::get(&m, &JsValue::from_str("a3")).unwrap()),
            t0: NativePointer::from(Reflect::get(&m, &JsValue::from_str("t0")).unwrap()),
            t1: NativePointer::from(Reflect::get(&m, &JsValue::from_str("t1")).unwrap()),
            t2: NativePointer::from(Reflect::get(&m, &JsValue::from_str("t2")).unwrap()),
            t3: NativePointer::from(Reflect::get(&m, &JsValue::from_str("t3")).unwrap()),
            t4: NativePointer::from(Reflect::get(&m, &JsValue::from_str("t4")).unwrap()),
            t5: NativePointer::from(Reflect::get(&m, &JsValue::from_str("t5")).unwrap()),
            t6: NativePointer::from(Reflect::get(&m, &JsValue::from_str("t6")).unwrap()),
            t7: NativePointer::from(Reflect::get(&m, &JsValue::from_str("t7")).unwrap()),
            t8: NativePointer::from(Reflect::get(&m, &JsValue::from_str("t8")).unwrap()),
            t9: NativePointer::from(Reflect::get(&m, &JsValue::from_str("t9")).unwrap()),
            s0: NativePointer::from(Reflect::get(&m, &JsValue::from_str("s0")).unwrap()),
            s1: NativePointer::from(Reflect::get(&m, &JsValue::from_str("s1")).unwrap()),
            s2: NativePointer::from(Reflect::get(&m, &JsValue::from_str("s2")).unwrap()),
            s3: NativePointer::from(Reflect::get(&m, &JsValue::from_str("s3")).unwrap()),
            s4: NativePointer::from(Reflect::get(&m, &JsValue::from_str("s4")).unwrap()),
            s5: NativePointer::from(Reflect::get(&m, &JsValue::from_str("s5")).unwrap()),
            s6: NativePointer::from(Reflect::get(&m, &JsValue::from_str("s6")).unwrap()),
            s7: NativePointer::from(Reflect::get(&m, &JsValue::from_str("s7")).unwrap()),
            k0: NativePointer::from(Reflect::get(&m, &JsValue::from_str("k0")).unwrap()),
            k1: NativePointer::from(Reflect::get(&m, &JsValue::from_str("k1")).unwrap()),
        }
    }
}