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
use std::{ffi::c_void, fmt};
use crate::{Program, vftable_classname};
/// A pointer whose target has not yet been reverse-engineered.
///
/// ## Safety
///
/// This may be null and has no particular alignment requirements, but it does
/// require that if it's not nullit must point within the current process's
/// memory space.
#[repr(transparent)]
#[derive(Clone, Copy)]
pub struct UnknownPtr(*const c_void);
impl UnknownPtr {
/// Interprets `value` as a pointer.
///
/// ## Safety
///
/// The caller must ensure that `value` is either 0 or a valid address in
/// the current address space.
pub unsafe fn from(value: usize) -> Self {
UnknownPtr(value as *const c_void)
}
/// Returns the class name for this pointer, if it's pointing to a vftable
/// whose name is in the RTTI.
pub fn rtti_classname(&self) -> Option<String> {
if self.0.is_null() {
return None;
}
let usize_ptr = self.cast::<usize>();
if !usize_ptr.is_aligned() {
return None;
}
// Even though we don't have any guarantees about what the pointer
// points to, we know it's aligned for `usize` and it's within the
// current address space so dereferencing it should be safe.
vftable_classname(&Program::current(), unsafe { *usize_ptr })
}
/// Returns `true` if this pointer is null.
pub fn is_null(&self) -> bool {
self.0.is_null()
}
/// Casts this to a pointer to `T`.
pub fn cast<T>(self) -> *const T {
self.0.cast::<T>()
}
/// Gets the "address" portion of the pointer.
pub fn addr(self) -> usize {
self.0.addr()
}
}
impl fmt::Debug for UnknownPtr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
if let Some(name) = self.rtti_classname() {
write!(f, "{:?} [{name}*]", self.0)
} else {
fmt::Debug::fmt(&self.0, f)
}
}
}
impl fmt::Pointer for UnknownPtr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
if let Some(name) = self.rtti_classname() {
write!(f, "{:p} [{name}*]", self.0)
} else {
fmt::Pointer::fmt(&self.0, f)
}
}
}