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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! An abstraction over the windows tlhelp32 api.
//! It offers a generic [`Snapshot`] struct which acts as an iterator to easily iterate over the
//! returned entries.

#![cfg(windows)]
#![warn(
    missing_docs,
    missing_copy_implementations,
    missing_debug_implementations
)]

use widestring::U16CString;
use winapi::shared::minwindef::{BOOL, HMODULE, LPCVOID};
use winapi::um::{
    handleapi::{CloseHandle, INVALID_HANDLE_VALUE},
    tlhelp32::*,
    winnt::HANDLE,
};

use std::{
    fmt,
    io::{Error, Result},
    iter::{FusedIterator, Iterator},
    mem,
};

type Tl32helpFunc<T> = unsafe extern "system" fn(HANDLE, *mut T) -> BOOL;

macro_rules! to_u16cstring {
    ($ident:expr) => {
        U16CString::from_vec_with_nul(Box::new($ident) as Box<[u16]>).unwrap_or_default()
    };
}

/// Copies memory allocated to another process at the specified address into a supplied slice.
/// The number of bytes to copy is the length of the supplied slice.
pub fn read_process_memory(
    process_id: u32,
    base_address: LPCVOID,
    buffer: &mut [u8],
) -> Result<usize> {
    let mut num_bytes_read = 0;
    if unsafe {
        Toolhelp32ReadProcessMemory(
            process_id,
            base_address,
            buffer.as_mut_ptr() as *mut _,
            buffer.len(),
            &mut num_bytes_read,
        )
    } == 0
    {
        Err(Error::last_os_error())
    } else {
        Ok(num_bytes_read)
    }
}

/// A trait for the different [`Snapshot`] types. You shouldn't need to work with this directly.
pub trait TagTl32: private::Sealed {
    /// The raw windows counterpart of the implementing struct
    type Raw: Copy;
    /// The corresponding Snapshot flags
    const FLAGS: u32;
    /// The `*32First` windows function
    const ITER_FIRST: Tl32helpFunc<Self::Raw>;
    /// The `*32Next` windows function
    const ITER_NEXT: Tl32helpFunc<Self::Raw>;

    /// Creates a new instance of this raw representation and initializes its `dwSize` field.
    fn init_raw() -> Self::Raw;

    /// Creates a new instance of `Self` from its windows counterpart.
    fn from_raw(raw: Self::Raw) -> Self;
}

mod private {
    pub trait Sealed {}
    impl Sealed for super::ProcessEntry {}
    impl Sealed for super::HeapList {}
    impl Sealed for super::ModuleEntry {}
    impl Sealed for super::ThreadEntry {}
}

/// A process entry taken from a [`Snapshot`].
/// For more information on the fields meanings visit the [`microsoft docs`](https://docs.microsoft.com/en-us/windows/desktop/api/tlhelp32/ns-tlhelp32-tagprocessentry32)
#[allow(missing_docs)]
#[derive(Clone)]
pub struct ProcessEntry {
    pub process_id: u32,
    pub cnt_threads: u32,
    pub parent_process_id: u32,
    pub pc_pri_class_base: i32,
    pub sz_exe_file: U16CString,
}

impl TagTl32 for ProcessEntry {
    type Raw = PROCESSENTRY32W;
    const FLAGS: u32 = TH32CS_SNAPPROCESS;
    const ITER_FIRST: Tl32helpFunc<Self::Raw> = Process32FirstW;
    const ITER_NEXT: Tl32helpFunc<Self::Raw> = Process32NextW;

    #[inline]
    fn init_raw() -> Self::Raw {
        Self::Raw {
            dwSize: mem::size_of::<Self::Raw>() as u32,
            ..unsafe { mem::uninitialized() }
        }
    }

    #[inline]
    fn from_raw(raw: Self::Raw) -> Self {
        ProcessEntry {
            process_id: raw.th32ProcessID,
            cnt_threads: raw.cntThreads,
            parent_process_id: raw.th32ParentProcessID,
            pc_pri_class_base: raw.pcPriClassBase,
            sz_exe_file: to_u16cstring!(raw.szExeFile),
        }
    }
}

impl fmt::Debug for ProcessEntry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ProcessEntry")
            .field("process_id", &self.process_id)
            .field("cnt_threads", &self.cnt_threads)
            .field("parent_process_id", &self.parent_process_id)
            .field("pc_pri_class_base", &self.pc_pri_class_base)
            .field(
                "sz_exe_file",
                &self.sz_exe_file.to_string().unwrap_or_default(),
            )
            .finish()
    }
}

/// A module entry taken from a [`Snapshot`].
/// For more information on the fields meanings visit the [`microsoft docs`](https://docs.microsoft.com/en-us/windows/desktop/api/tlhelp32/ns-tlhelp32-tagmoduleentry32)
#[allow(missing_docs)]
#[derive(Clone)]
pub struct ModuleEntry {
    pub process_id: u32,
    pub base_addr: *mut u8,
    pub base_size: u32,
    pub h_module: HMODULE,
    pub sz_module: U16CString,
    pub sz_exe_path: U16CString,
}

impl TagTl32 for ModuleEntry {
    type Raw = MODULEENTRY32W;
    const FLAGS: u32 = TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32;
    const ITER_FIRST: Tl32helpFunc<Self::Raw> = Module32FirstW;
    const ITER_NEXT: Tl32helpFunc<Self::Raw> = Module32NextW;

    #[inline]
    fn init_raw() -> Self::Raw {
        Self::Raw {
            dwSize: mem::size_of::<Self::Raw>() as u32,
            ..unsafe { mem::uninitialized() }
        }
    }

    #[inline]
    fn from_raw(raw: Self::Raw) -> Self {
        ModuleEntry {
            process_id: raw.th32ProcessID,
            base_addr: raw.modBaseAddr,
            base_size: raw.modBaseSize,
            h_module: raw.hModule,
            sz_module: to_u16cstring!(raw.szModule),
            sz_exe_path: to_u16cstring!(raw.szExePath),
        }
    }
}

impl fmt::Debug for ModuleEntry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ProcessEntry")
            .field("process_id", &self.process_id)
            .field("base_addr", &self.base_addr)
            .field("base_size", &self.base_size)
            .field("h_module", &self.h_module)
            .field("sz_module", &self.sz_module.to_string().unwrap_or_default())
            .field(
                "sz_exe_file",
                &self.sz_exe_path.to_string().unwrap_or_default(),
            )
            .finish()
    }
}

/// A heap list taken from a [`Snapshot`]. This struct is an iterator over the heap entries of its heap.
/// For more information on the fields meanings visit the [`microsoft docs`](https://docs.microsoft.com/en-us/windows/desktop/api/tlhelp32/ns-tlhelp32-tagheaplist32)
#[allow(missing_docs, missing_copy_implementations)]
pub struct HeapList {
    pub process_id: u32,
    pub heap_id: usize,
    pub flags: u32,
    current: Option<HEAPENTRY32>,
}

impl TagTl32 for HeapList {
    type Raw = HEAPLIST32;
    const FLAGS: u32 = TH32CS_SNAPHEAPLIST;
    const ITER_FIRST: Tl32helpFunc<Self::Raw> = Heap32ListFirst;
    const ITER_NEXT: Tl32helpFunc<Self::Raw> = Heap32ListNext;

    #[inline]
    fn init_raw() -> Self::Raw {
        Self::Raw {
            dwSize: mem::size_of::<Self::Raw>(),
            ..unsafe { mem::uninitialized() }
        }
    }

    #[inline]
    fn from_raw(raw: Self::Raw) -> Self {
        let mut entry = HEAPENTRY32 {
            dwSize: mem::size_of::<HEAPENTRY32>(),
            ..unsafe { mem::uninitialized() }
        };
        let current = if unsafe { Heap32First(&mut entry, raw.th32ProcessID, raw.th32HeapID) == 0 }
        {
            None
        } else {
            Some(entry)
        };
        HeapList {
            process_id: raw.th32ProcessID,
            heap_id: raw.th32HeapID,
            flags: raw.dwFlags,
            current,
        }
    }
}

impl Iterator for HeapList {
    type Item = HeapEntry;
    fn next(&mut self) -> Option<Self::Item> {
        let val = HeapEntry::from_raw(self.current?);
        if unsafe { Heap32Next(self.current.as_mut().unwrap()) == 0 } {
            self.current = None
        }
        Some(val)
    }
}

impl fmt::Debug for HeapList {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("HeapList")
            .field("process_id", &self.process_id)
            .field("heap_id", &self.heap_id)
            .field("flags", &self.flags)
            .field("exhausted", &self.current.is_none())
            .finish()
    }
}

/// A heap entry taken from a [`HeapList`].
/// For more information on the fields meanings visit the [`microsoft docs`](https://docs.microsoft.com/en-us/windows/desktop/api/tlhelp32/ns-tlhelp32-tagheapentry32)
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug)]
pub struct HeapEntry {
    pub handle: HANDLE,
    pub address: usize,
    pub block_size: usize,
    pub flags: u32,
    pub process_id: u32,
    pub heap_id: usize,
}

impl HeapEntry {
    fn from_raw(raw: HEAPENTRY32) -> Self {
        HeapEntry {
            handle: raw.hHandle,
            address: raw.dwAddress,
            block_size: raw.dwBlockSize,
            flags: raw.dwFlags,
            process_id: raw.th32ProcessID,
            heap_id: raw.th32HeapID,
        }
    }
}

/// A thread entry taken from a [`Snapshot`].
/// For more information on the fields meanings visit the [`microsoft docs`](https://docs.microsoft.com/en-us/windows/desktop/api/tlhelp32/ns-tlhelp32-tagthreadentry32)
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug)]
pub struct ThreadEntry {
    pub thread_id: u32,
    pub owner_process_id: u32,
    pub base_pri: i32,
}

impl TagTl32 for ThreadEntry {
    type Raw = THREADENTRY32;
    const FLAGS: u32 = TH32CS_SNAPTHREAD;
    const ITER_FIRST: Tl32helpFunc<Self::Raw> = Thread32First;
    const ITER_NEXT: Tl32helpFunc<Self::Raw> = Thread32Next;

    #[inline]
    fn init_raw() -> Self::Raw {
        Self::Raw {
            dwSize: mem::size_of::<Self::Raw>() as u32,
            ..unsafe { mem::uninitialized() }
        }
    }

    #[inline]
    fn from_raw(raw: Self::Raw) -> Self {
        ThreadEntry {
            thread_id: raw.th32ThreadID,
            owner_process_id: raw.th32OwnerProcessID,
            base_pri: raw.tpBasePri,
        }
    }
}

/// An iterator for the Toolhelp32Snapshot Windows API.
/// You create them by calling the appropriate `new_*` methods.
#[derive(Debug)]
pub struct Snapshot<T: TagTl32> {
    snapshot: HANDLE,
    current: Option<T::Raw>,
}

impl<T: TagTl32> Snapshot<T> {
    #[inline]
    fn new(pid: u32) -> Result<Self> {
        unsafe { Self::from_handle(CreateToolhelp32Snapshot(T::FLAGS, pid)) }
    }

    /// Creates a snapshot from a given handle. Avoid using this unless you have a specific reason to.
    /// # Safety
    /// This function does not check whether the generic type and the flags belong together.
    /// If used incorrectly this will produce an iterator that returns [`None`] from the very beginning.
    pub unsafe fn from_handle(snapshot: HANDLE) -> Result<Self> {
        match snapshot {
            INVALID_HANDLE_VALUE => Err(Error::last_os_error()),
            snapshot => {
                let mut entry = T::init_raw();
                let current = if T::ITER_FIRST(snapshot, &mut entry) == 0 {
                    None
                } else {
                    Some(entry)
                };
                Ok(Snapshot { snapshot, current })
            }
        }
    }

    /// Retrieves the windows snapshot handle
    pub fn handle(&self) -> HANDLE {
        self.snapshot
    }
}

impl Snapshot<ProcessEntry> {
    /// Creates a new [`ProcessEntry`] [`Snapshot`]. This is equal to creating a snapshot with the `TH32CS_SNAPPROCESS` flag.
    /// # Errors
    /// This function fails and returns the appropriate os error if it is unable to create a [`Snapshot`]
    ///
    /// # Usage
    ///
    /// ```rust,no_run
    /// for entry in tlhelp32::Snapshot::new_process()? {
    ///     println!("{:?}", entry);
    /// }
    /// ```
    pub fn new_process() -> Result<Self> {
        Self::new(0)
    }
}

impl Snapshot<HeapList> {
    /// Creates a new [`HeapList`] [`Snapshot`]. This is equal to creating a snapshot with the `TH32CS_SNAPHEAPLIST` flag.
    /// # Errors
    /// This function fails and returns the appropriate os error if it is unable to create a [`Snapshot`]
    /// # Usage
    ///
    /// ```rust,no_run
    /// for heap_list in tlhelp32::Snapshot::new_heap_list(pid)? {
    ///     for heap_entry in heap_list {
    ///         println!("{:?}", heap_entry);
    ///     }
    /// }
    /// ```
    pub fn new_heap_list(pid: u32) -> Result<Self> {
        Self::new(pid)
    }
}

impl Snapshot<ModuleEntry> {
    /// Creates a new [`ModuleEntry`] [`Snapshot`]. This is equal to creating a snapshot with the `TH32CS_SNAPMODULE` and `TH32CS_SNAPMODULE32` flags.
    /// # Errors
    /// This function fails and returns the appropriate os error if it is unable to create a [`Snapshot`]
    ///
    /// # Usage
    ///
    /// ```rust,no_run
    /// for mod_entry in tlhelp32::Snapshot::new_module(entry.process_id)? {
    ///     println!("{:?}", mod_entry);
    /// }
    /// ```
    pub fn new_module(pid: u32) -> Result<Self> {
        Self::new(pid)
    }
}

impl Snapshot<ThreadEntry> {
    /// Creates a new [`ThreadEntry`] [`Snapshot`]. This is equal to creating a snapshot with the `TH32CS_SNAPTHREAD` flag.
    /// # Errors
    /// This function fails and returns the appropriate os error if it is unable to create a [`Snapshot`]
    ///
    /// # Usage
    ///
    /// ```rust,no_run
    /// for thread_entry in tlhelp32::Snapshot::new_thread()? {
    ///     println!("{:?}", mod_entry);
    /// }
    /// ```
    pub fn new_thread() -> Result<Self> {
        Self::new(0)
    }
}

impl<T: TagTl32> Iterator for Snapshot<T> {
    type Item = T;
    fn next(&mut self) -> Option<Self::Item> {
        let val = T::from_raw(self.current?);
        if unsafe { T::ITER_NEXT(self.snapshot, self.current.as_mut().unwrap()) == 0 } {
            self.current = None
        }
        Some(val)
    }
}

impl<T: TagTl32> FusedIterator for Snapshot<T> {}

impl<T: TagTl32> Drop for Snapshot<T> {
    fn drop(&mut self) {
        unsafe { CloseHandle(self.snapshot) };
    }
}

unsafe impl Send for ModuleEntry {}
unsafe impl Sync for ModuleEntry {}
unsafe impl Send for HeapList {}
unsafe impl Send for HeapEntry {}
unsafe impl Sync for HeapEntry {}