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
#![feature(return_position_impl_trait_in_trait)]
#![feature(offset_of)]

#[cfg(target_os = "macos")]
pub mod macos;

#[cfg(target_os = "linux")]
pub mod linux;

#[cfg(target_os = "windows")]
pub mod windows;

#[cfg(any(target_os = "macos", target_os = "linux"))]
pub type Pid = i32;

#[cfg(target_os = "windows")]
pub type Pid = u32;

mod error;
pub use error::Error;

pub mod vmmap64 {
    use std::path::Path;

    #[cfg(target_os = "linux")]
    pub use super::linux::proc64::Process;
    #[cfg(target_os = "macos")]
    pub use super::macos::proc64::Process;
    #[cfg(target_os = "windows")]
    pub use super::windows::proc64::Process;
    use super::Pid;

    pub trait VirtualMemoryRead {
        type Error: std::error::Error;

        fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<usize, Self::Error>;
    }

    pub trait VirtualMemoryWrite {
        type Error: std::error::Error;

        fn write_at(&self, offset: u64, buf: &[u8]) -> Result<(), Self::Error>;
    }

    #[cfg(target_os = "macos")]
    pub trait VirtualQueryExt {
        fn tag(&self) -> u32;
        fn is_reserve(&self) -> bool;
        fn path(&self) -> Option<&Path>;
    }

    #[cfg(target_os = "linux")]
    pub trait VirtualQueryExt {
        fn name(&self) -> &str;
    }

    #[cfg(target_os = "windows")]
    pub trait VirtualQueryExt {
        fn path(&self) -> Option<&Path>;
    }

    pub trait VirtualQuery: VirtualQueryExt {
        fn start(&self) -> u64;
        fn end(&self) -> u64;
        fn size(&self) -> u64;
        fn is_read(&self) -> bool;
        fn is_write(&self) -> bool;
        fn is_exec(&self) -> bool;
    }

    pub trait ProcessInfo {
        fn pid(&self) -> Pid;
        fn app_path(&self) -> &Path;
        fn get_maps(&self) -> impl Iterator<Item = impl VirtualQuery + '_>;
    }
}

#[cfg(not(target_os = "macos"))]
pub mod vmmap32 {
    use std::path::Path;

    #[cfg(target_os = "linux")]
    pub use super::linux::proc32::Process;
    use super::Pid;

    pub trait VirtualMemoryRead {
        type Error: std::error::Error;

        fn read_at(&self, offset: u32, buf: &mut [u8]) -> Result<usize, Self::Error>;
    }

    pub trait VirtualMemoryWrite {
        type Error: std::error::Error;

        fn write_at(&self, offset: u32, buf: &[u8]) -> Result<(), Self::Error>;
    }

    #[cfg(target_os = "linux")]
    pub trait VirtualQueryExt {
        fn name(&self) -> &str;
    }

    #[cfg(target_os = "windows")]
    pub trait VirtualQueryExt {
        fn path(&self) -> Option<&Path>;
    }

    pub trait VirtualQuery: VirtualQueryExt {
        fn start(&self) -> u32;
        fn end(&self) -> u32;
        fn size(&self) -> u32;
        fn is_read(&self) -> bool;
        fn is_write(&self) -> bool;
        fn is_exec(&self) -> bool;
    }

    pub trait ProcessInfo {
        type Error: std::error::Error;

        fn pid(&self) -> Pid;
        fn app_path(&self) -> &Path;
        fn get_maps(&self) -> impl Iterator<Item = impl VirtualQuery + '_>;
    }
}