use hyperlight_common::flatbuffer_wrappers::function_types::{
ParameterValue, ReturnType, ReturnValue,
};
use tracing::{instrument, Span};
use super::{MemMgrWrapper, WrapperGetter};
use crate::func::call_ctx::SingleUseGuestCallContext;
use crate::hypervisor::hypervisor_handler::HypervisorHandler;
use crate::mem::shared_mem::HostSharedMemory;
use crate::sandbox_state::sandbox::Sandbox;
use crate::Result;
pub struct SingleUseSandbox {
pub(super) mem_mgr: MemMgrWrapper<HostSharedMemory>,
hv_handler: HypervisorHandler,
}
impl Drop for SingleUseSandbox {
fn drop(&mut self) {
match self.hv_handler.kill_hypervisor_handler_thread() {
Ok(_) => {}
Err(e) => {
log::error!("[POTENTIAL THREAD LEAK] Potentially failed to kill hypervisor handler thread when dropping MultiUseSandbox: {:?}", e);
}
}
}
}
impl SingleUseSandbox {
#[instrument(skip_all, parent = Span::current(), level = "Trace")]
pub(super) fn from_uninit(
mgr: MemMgrWrapper<HostSharedMemory>,
hv_handler: HypervisorHandler,
) -> SingleUseSandbox {
Self {
mem_mgr: mgr,
hv_handler,
}
}
#[instrument(skip_all, parent = Span::current())]
pub fn new_call_context(self) -> SingleUseGuestCallContext {
SingleUseGuestCallContext::start(self)
}
#[instrument(err(Debug), skip(self, args), parent = Span::current())]
pub fn call_guest_function_by_name(
self,
name: &str,
ret: ReturnType,
args: Option<Vec<ParameterValue>>,
) -> Result<ReturnValue> {
self.new_call_context().call(name, ret, args)
}
}
impl WrapperGetter for SingleUseSandbox {
fn get_mgr_wrapper(&self) -> &MemMgrWrapper<HostSharedMemory> {
&self.mem_mgr
}
fn get_mgr_wrapper_mut(&mut self) -> &mut MemMgrWrapper<HostSharedMemory> {
&mut self.mem_mgr
}
fn get_hv_handler(&self) -> &HypervisorHandler {
&self.hv_handler
}
fn get_hv_handler_mut(&mut self) -> &mut HypervisorHandler {
&mut self.hv_handler
}
}
impl Sandbox for SingleUseSandbox {
#[instrument(skip_all, parent = Span::current(), level = "Trace")]
fn check_stack_guard(&self) -> Result<bool> {
self.mem_mgr.check_stack_guard()
}
}
impl std::fmt::Debug for SingleUseSandbox {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SingleUseSandbox")
.field("stack_guard", &self.mem_mgr.get_stack_cookie())
.finish()
}
}