Skip to main content

vmi_core/os/
module.rs

1use super::{VmiOs, impl_predicate};
2use crate::{Va, VmiDriver, VmiError, VmiVa};
3
4impl_predicate! {
5    /// Predicate used by [`VmiOsExt::find_module`].
6    ///
7    /// [`VmiOsExt::find_module`]: super::VmiOsExt::find_module
8    pub trait ModulePredicate & impl for &str {
9        fn matches(&self, module: &Os::Module<'_>) -> Result<bool, VmiError> {
10            Ok(module.name()?.eq_ignore_ascii_case(self))
11        }
12    }
13
14    #[any]
15    pub struct AnyModule;
16}
17
18/// A trait for kernel modules.
19///
20/// This trait provides an abstraction over dynamically loaded modules,
21/// such as kernel drivers and shared libraries, within a guest OS.
22pub trait VmiOsModule<'a, Driver>: VmiVa + 'a
23where
24    Driver: VmiDriver,
25{
26    /// The VMI OS type.
27    type Os: VmiOs<Driver = Driver, Module<'a> = Self>;
28
29    /// Returns the base address of the module.
30    ///
31    /// # Platform-specific
32    ///
33    /// - **Windows**: `KLDR_DATA_TABLE_ENTRY.DllBase`
34    /// - **Linux**:
35    ///   - since v6.4-rc1: `module::mem[0 /* MOD_TEXT */].base`
36    ///   - before v6.4-rc1: `module::core_layout.base`
37    fn base_address(&self) -> Result<Va, VmiError>;
38
39    /// Returns the size of the module.
40    ///
41    /// # Platform-specific
42    ///
43    /// - **Windows**: `KLDR_DATA_TABLE_ENTRY.SizeOfImage`
44    /// - **Linux**:
45    ///   - since v6.4-rc1: sum of `module::mem[MOD_*].size`
46    ///   - before v6.4-rc1: `module::init_layout.size + module::core_layout.size (+ module::data_layout.size)`
47    fn size(&self) -> Result<u64, VmiError>;
48
49    /// Returns the name of the module.
50    ///
51    /// # Platform-specific
52    ///
53    /// - **Windows**: `KLDR_DATA_TABLE_ENTRY.BaseDllName`
54    /// - **Linux**: `module::name`
55    fn name(&self) -> Result<String, VmiError>;
56}