vtable_hook/
lib.rs

1#![allow(clippy::missing_safety_doc, clippy::mut_from_ref)]
2
3pub mod hook;
4
5pub type RawVTable = *const Method;
6pub type Method = *mut unsafe fn();
7
8#[derive(Debug, Clone)]
9pub struct VTable {
10    pub begin: RawVTable,
11    pub size: usize,
12}
13
14impl VTable {
15    pub unsafe fn new(vtable: RawVTable) -> Self {
16        Self::new_with_size(vtable, Self::count_methods_raw(vtable))
17    }
18
19    pub unsafe fn new_with_size(vtable: RawVTable, size: usize) -> Self {
20        Self {
21            begin: vtable,
22            size,
23        }
24    }
25
26    pub unsafe fn count_methods_raw(mut vtable: RawVTable) -> usize {
27        /* Try to dynamically count amount of methods in a VTable, very unsafe. */
28        let mut size = 0;
29        while !std::ptr::read(vtable).is_null() {
30            vtable = vtable.add(1);
31            size += 1;
32        }
33        size
34    }
35
36    pub unsafe fn as_slice(&self) -> &[Method] {
37        std::slice::from_raw_parts(self.begin, self.size)
38    }
39
40    // TODO
41    /*pub unsafe fn as_mut_slice(&self) -> &mut [Method] {
42        std::slice::from_raw_parts_mut(self.begin, self.size)
43    }*/
44}