Skip to main content

vmi_core/ctx/
context.rs

1use super::{VmiOsState, state::VmiState};
2use crate::{VmiCore, VmiEvent, VmiOs};
3
4/// A VMI context.
5///
6/// The context combines access to a [`VmiState`] with [`VmiEvent`] to
7/// provide unified access to VMI operations in the context of a specific
8/// event.
9///
10/// This structure is created inside the [`VmiSession::handle`] method and
11/// passed to the [`VmiHandler::handle_event`] method to handle VMI events.
12///
13/// [`VmiSession::handle`]: super::VmiSession::handle
14/// [`VmiHandler::handle_event`]: crate::VmiHandler::handle_event
15pub struct VmiContext<'a, Os>
16where
17    Os: VmiOs,
18{
19    /// The VMI session.
20    state: &'a VmiState<'a, Os>,
21
22    /// The VMI event.
23    event: &'a VmiEvent<Os::Architecture>,
24}
25
26impl<Os> Clone for VmiContext<'_, Os>
27where
28    Os: VmiOs,
29{
30    fn clone(&self) -> Self {
31        *self
32    }
33}
34
35impl<Os> Copy for VmiContext<'_, Os> where Os: VmiOs {}
36
37impl<'a, Os> std::ops::Deref for VmiContext<'a, Os>
38where
39    Os: VmiOs,
40{
41    type Target = VmiState<'a, Os>;
42
43    fn deref(&self) -> &Self::Target {
44        self.state
45    }
46}
47
48impl<'a, Os> VmiContext<'a, Os>
49where
50    Os: VmiOs,
51{
52    /// Creates a new VMI context.
53    pub fn new(state: &'a VmiState<'a, Os>, event: &'a VmiEvent<Os::Architecture>) -> Self {
54        debug_assert_eq!(state.registers() as *const _, event.registers() as *const _);
55
56        Self { state, event }
57    }
58
59    // Note that `core()` and `underlying_os()` and other methods are delegated
60    // to the `VmiState`.
61
62    /// Returns the VMI session.
63    pub fn state(&self) -> VmiState<'a, Os> {
64        *self.state
65    }
66
67    /// Returns the current VMI event.
68    pub fn event(&self) -> &VmiEvent<Os::Architecture> {
69        self.event
70    }
71
72    /// Returns a wrapper providing access to OS-specific operations.
73    pub fn os(&self) -> VmiOsContext<'_, Os> {
74        VmiOsContext {
75            state: self.state.os(),
76            event: self.event,
77        }
78    }
79}
80
81/// Wrapper providing access to OS-specific operations.
82pub struct VmiOsContext<'a, Os>
83where
84    Os: VmiOs,
85{
86    /// The VMI OS state.
87    state: VmiOsState<'a, Os>,
88
89    /// The VMI event.
90    event: &'a VmiEvent<Os::Architecture>,
91}
92
93impl<'a, Os> std::ops::Deref for VmiOsContext<'a, Os>
94where
95    Os: VmiOs,
96{
97    type Target = VmiOsState<'a, Os>;
98
99    fn deref(&self) -> &Self::Target {
100        &self.state
101    }
102}
103
104impl<Os> VmiOsContext<'_, Os>
105where
106    Os: VmiOs,
107{
108    /// Returns the VMI context.
109    pub fn core(&self) -> &VmiCore<Os::Driver> {
110        self.state.core()
111    }
112
113    /// Returns the underlying OS-specific implementation.
114    pub fn underlying_os(&self) -> &Os {
115        self.state.underlying_os()
116    }
117
118    /// Returns the current VMI event.
119    pub fn event(&self) -> &VmiEvent<Os::Architecture> {
120        self.event
121    }
122}