Skip to content

Commit 8d15b69

Browse files
Copilotsimongdavies
andcommitted
Remove dynamic dispatch from OutBHandlerFunction
Co-authored-by: simongdavies <[email protected]>
1 parent 4c61fc4 commit 8d15b69

File tree

4 files changed

+56
-44
lines changed

4 files changed

+56
-44
lines changed

src/hyperlight_host/src/hypervisor/handlers.rs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,10 @@ use std::sync::{Arc, Mutex};
1818

1919
use tracing::{instrument, Span};
2020

21+
/// Re-export OutBHandler from the outb module where it naturally belongs
22+
pub(crate) use crate::sandbox::outb::OutBHandler;
2123
use crate::{new_error, Result};
2224

23-
pub(crate) type OutBHandlerFunction = Box<dyn FnMut(u16, u32) -> Result<()> + Send>;
24-
25-
/// A `OutBHandler` implementation using a `OutBHandlerFunction`
26-
///
27-
/// Note: This handler must live no longer than the `Sandbox` to which it belongs
28-
pub(crate) struct OutBHandler(Arc<Mutex<OutBHandlerFunction>>);
29-
30-
impl From<OutBHandlerFunction> for OutBHandler {
31-
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
32-
fn from(func: OutBHandlerFunction) -> Self {
33-
Self(Arc::new(Mutex::new(func)))
34-
}
35-
}
36-
37-
impl OutBHandler {
38-
/// Function that gets called when an outb operation has occurred.
39-
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
40-
pub fn call(&mut self, port: u16, payload: u32) -> Result<()> {
41-
let mut func = self
42-
.0
43-
.try_lock()
44-
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?;
45-
func(port, payload)
46-
}
47-
}
48-
4925
/// The trait representing custom logic to handle the case when
5026
/// a Hypervisor's virtual CPU (vCPU) informs Hyperlight a memory access
5127
/// outside the designated address space has occurred.

src/hyperlight_host/src/hypervisor/hyperv_windows.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,15 +505,21 @@ pub mod tests {
505505

506506
use crate::hypervisor::handlers::{MemAccessHandler, OutBHandler};
507507
use crate::hypervisor::tests::test_initialise;
508+
use crate::sandbox::uninitialized::{GuestBinary, UninitializedSandbox};
508509
use crate::Result;
510+
use hyperlight_testing::dummy_guest_as_string;
509511

510512
#[test]
511513
#[serial]
512514
fn test_init() {
515+
let filename = dummy_guest_as_string().expect("Guest Binary Missing");
516+
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(filename), None).unwrap();
517+
let (hshm, gshm) = sandbox.mgr.build();
518+
drop(gshm);
519+
let host_funcs = sandbox.host_funcs.clone();
520+
513521
let outb_handler = {
514-
let func: Box<dyn FnMut(u16, u32) -> Result<()> + Send> =
515-
Box::new(|_, _| -> Result<()> { Ok(()) });
516-
Arc::new(Mutex::new(OutBHandler::from(func)))
522+
crate::sandbox::outb::outb_handler_wrapper(hshm.clone(), host_funcs)
517523
};
518524
let mem_access_handler = {
519525
let func: Box<dyn FnMut() -> Result<()> + Send> = Box::new(|| -> Result<()> { Ok(()) });

src/hyperlight_host/src/hypervisor/kvm.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,9 @@ mod tests {
641641
use crate::hypervisor::handlers::DbgMemAccessHandlerCaller;
642642
use crate::hypervisor::handlers::{MemAccessHandler, OutBHandler};
643643
use crate::hypervisor::tests::test_initialise;
644+
use crate::sandbox::uninitialized::{GuestBinary, UninitializedSandbox};
644645
use crate::Result;
646+
use hyperlight_testing::dummy_guest_as_string;
645647

646648
#[cfg(gdb)]
647649
struct DbgMemAccessHandler {}
@@ -667,10 +669,14 @@ mod tests {
667669
return;
668670
}
669671

672+
let filename = dummy_guest_as_string().expect("Guest Binary Missing");
673+
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(filename), None).unwrap();
674+
let (hshm, gshm) = sandbox.mgr.build();
675+
drop(gshm);
676+
let host_funcs = sandbox.host_funcs.clone();
677+
670678
let outb_handler: Arc<Mutex<OutBHandler>> = {
671-
let func: Box<dyn FnMut(u16, u32) -> Result<()> + Send> =
672-
Box::new(|_, _| -> Result<()> { Ok(()) });
673-
Arc::new(Mutex::new(OutBHandler::from(func)))
679+
crate::sandbox::outb::outb_handler_wrapper(hshm.clone(), host_funcs)
674680
};
675681
let mem_access_handler = {
676682
let func: Box<dyn FnMut() -> Result<()> + Send> = Box::new(|| -> Result<()> { Ok(()) });

src/hyperlight_host/src/sandbox/outb.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,43 @@ use tracing_log::format_trace;
2626

2727
use super::host_funcs::FunctionRegistry;
2828
use super::mem_mgr::MemMgrWrapper;
29-
use crate::hypervisor::handlers::{OutBHandler, OutBHandlerFunction};
3029
use crate::mem::mgr::SandboxMemoryManager;
3130
use crate::mem::shared_mem::HostSharedMemory;
3231
use crate::{new_error, HyperlightError, Result};
3332

33+
/// A `OutBHandler` implementation that contains the required data directly
34+
///
35+
/// Note: This handler must live no longer than the `Sandbox` to which it belongs
36+
pub(crate) struct OutBHandler {
37+
mem_mgr_wrapper: MemMgrWrapper<HostSharedMemory>,
38+
host_funcs_wrapper: Arc<Mutex<FunctionRegistry>>,
39+
}
40+
41+
impl OutBHandler {
42+
/// Create a new OutBHandler with the required dependencies
43+
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
44+
pub fn new(
45+
mem_mgr_wrapper: MemMgrWrapper<HostSharedMemory>,
46+
host_funcs_wrapper: Arc<Mutex<FunctionRegistry>>,
47+
) -> Self {
48+
Self {
49+
mem_mgr_wrapper,
50+
host_funcs_wrapper,
51+
}
52+
}
53+
54+
/// Function that gets called when an outb operation has occurred.
55+
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
56+
pub fn call(&mut self, port: u16, payload: u32) -> Result<()> {
57+
handle_outb_impl(
58+
&mut self.mem_mgr_wrapper,
59+
self.host_funcs_wrapper.clone(),
60+
port,
61+
payload,
62+
)
63+
}
64+
}
65+
3466
#[instrument(err(Debug), skip_all, parent = Span::current(), level="Trace")]
3567
pub(super) fn outb_log(mgr: &mut SandboxMemoryManager<HostSharedMemory>) -> Result<()> {
3668
// This code will create either a logging record or a tracing record for the GuestLogData depending on if the host has set up a tracing subscriber.
@@ -146,7 +178,7 @@ fn outb_abort(mem_mgr: &mut MemMgrWrapper<HostSharedMemory>, data: u32) -> Resul
146178

147179
/// Handles OutB operations from the guest.
148180
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
149-
fn handle_outb_impl(
181+
pub(crate) fn handle_outb_impl(
150182
mem_mgr: &mut MemMgrWrapper<HostSharedMemory>,
151183
host_funcs: Arc<Mutex<FunctionRegistry>>,
152184
port: u16,
@@ -189,18 +221,10 @@ fn handle_outb_impl(
189221
/// TODO: pass at least the `host_funcs_wrapper` param by reference.
190222
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
191223
pub(crate) fn outb_handler_wrapper(
192-
mut mem_mgr_wrapper: MemMgrWrapper<HostSharedMemory>,
224+
mem_mgr_wrapper: MemMgrWrapper<HostSharedMemory>,
193225
host_funcs_wrapper: Arc<Mutex<FunctionRegistry>>,
194226
) -> Arc<Mutex<OutBHandler>> {
195-
let outb_func: OutBHandlerFunction = Box::new(move |port, payload| {
196-
handle_outb_impl(
197-
&mut mem_mgr_wrapper,
198-
host_funcs_wrapper.clone(),
199-
port,
200-
payload,
201-
)
202-
});
203-
let outb_hdl = OutBHandler::from(outb_func);
227+
let outb_hdl = OutBHandler::new(mem_mgr_wrapper, host_funcs_wrapper);
204228
Arc::new(Mutex::new(outb_hdl))
205229
}
206230

0 commit comments

Comments
 (0)