Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
444 changes: 229 additions & 215 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exclude = [
]

[workspace.package]
version = "0.1.0"
version = "0.2.0"
edition = "2021"
rust-version = "1.80.0"
license = "Apache-2.0"
Expand All @@ -34,9 +34,9 @@ readme = "README.md"

[workspace.dependencies]

hyperlight-common = { path = "src/hyperlight_common", version = "0.1.0", default-features = false }
hyperlight-host = { path = "src/hyperlight_host", version = "0.1.0", default-features = false }
hyperlight-guest = { path = "src/hyperlight_guest", version = "0.1.0", default-features = false }
hyperlight-common = { path = "src/hyperlight_common", version = "0.2.0", default-features = false }
hyperlight-host = { path = "src/hyperlight_host", version = "0.2.0", default-features = false }
hyperlight-guest = { path = "src/hyperlight_guest", version = "0.2.0", default-features = false }
hyperlight-testing = { path = "src/hyperlight_testing", default-features = false }

[workspace.lints.rust]
Expand Down
5 changes: 3 additions & 2 deletions src/hyperlight_host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ strum = { version = "0.27", features = ["derive"] }
tempfile = { version = "3.17", optional = true }
serde_yaml = "0.9"
anyhow = "1.0"
uuid = { version = "1.13.2", features = ["v4"] }

[target.'cfg(windows)'.dependencies]
windows = { version = "0.59", features = [
Expand Down Expand Up @@ -78,8 +79,8 @@ kvm-bindings = { version = "0.11", features = ["fam-wrappers"], optional = true
kvm-ioctls = { version = "0.20", optional = true }
mshv-bindings2 = { package="mshv-bindings", version = "=0.2.1", optional = true }
mshv-ioctls2 = { package="mshv-ioctls", version = "=0.2.1", optional = true}
mshv-bindings3 = { package="mshv-bindings", version = "0.3.2", optional = true }
mshv-ioctls3 = { package="mshv-ioctls", version = "0.3.2", optional = true}
mshv-bindings3 = { package="mshv-bindings", version = "0.3.3", optional = true }
mshv-ioctls3 = { package="mshv-ioctls", version = "0.3.3", optional = true}

[dev-dependencies]
uuid = { version = "1.13.2", features = ["v4"] }
Expand Down
1 change: 1 addition & 0 deletions src/hyperlight_host/benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ fn sandbox_benchmark(c: &mut Criterion) {

// Benchmarks the time to create a new sandbox, create a new call context, and drop the call context.
group.bench_function("create_sandbox_and_call_context_and_drop", |b| {
let _ = env_logger::builder().parse_filters("debug").try_init();
b.iter(|| create_multiuse_sandbox().new_call_context());
});

Expand Down
10 changes: 10 additions & 0 deletions src/hyperlight_host/src/hypervisor/hyperv_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,33 @@ impl HypervLinuxDriver {
// create_vm_with_args() with an empty arguments structure
// here, because otherwise the partition is set up with a SynIC.

log::debug!("Creating VM with args");
let vm_fd = mshv.create_vm_with_args(&pr)?;
log::debug!("Setting partition property");
let features: hv_partition_synthetic_processor_features = Default::default();
vm_fd.hvcall_set_partition_property(
hv_partition_property_code_HV_PARTITION_PROPERTY_SYNTHETIC_PROC_FEATURES,
unsafe { features.as_uint64[0] },
)?;
log::debug!("Initializing VM");
vm_fd.initialize()?;
vm_fd
};

log::debug!("Creating VCPU");
let mut vcpu_fd = vm_fd.create_vcpu(0)?;

mem_regions.iter().try_for_each(|region| {
let mshv_region = region.to_owned().into();
log::debug!("Mapping user memory for region: {:?}", mshv_region);
vm_fd.map_user_memory(mshv_region)
})?;

log::debug!("Setting up initial sregs");
Self::setup_initial_sregs(&mut vcpu_fd, pml4_ptr.absolute()?)?;

log::debug!("Setting up initial registers");

Ok(Self {
_mshv: mshv,
vm_fd,
Expand Down Expand Up @@ -163,7 +171,9 @@ impl HypervLinuxDriver {
},
..Default::default()
};
log::debug!("Setting up initial sregs: {:?}", sregs);
vcpu.set_sregs(&sregs)?;
log::debug!("Setting up initial sregs complete");
Ok(())
}
}
Expand Down
63 changes: 44 additions & 19 deletions src/hyperlight_host/src/hypervisor/hypervisor_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub(crate) struct HypervisorHandler {
communication_channels: HvHandlerCommChannels,
configuration: HvHandlerConfig,
execution_variables: HvHandlerExecVars,
id: String,
}

impl HypervisorHandler {
Expand All @@ -86,6 +87,10 @@ impl HypervisorHandler {
pub(crate) fn set_run_cancelled(&self, run_cancelled: bool) {
self.execution_variables.run_cancelled.store(run_cancelled);
}

pub(crate) fn get_id(&self) -> String {
self.id.clone()
}
}

// Note: `join_handle` and `running` have to be `Arc` because we need
Expand Down Expand Up @@ -226,6 +231,7 @@ impl HypervisorHandler {
communication_channels,
configuration,
execution_variables,
id: uuid::Uuid::new_v4().to_string(),
}
}

Expand Down Expand Up @@ -289,6 +295,8 @@ impl HypervisorHandler {
#[cfg(target_os = "linux")]
setup_signal_handlers()?;

let id = self.id.clone();

let join_handle = {
thread::Builder::new()
.name("Hypervisor Handler".to_string())
Expand Down Expand Up @@ -324,7 +332,7 @@ impl HypervisorHandler {
#[cfg(target_os = "linux")]
execution_variables.run_cancelled.store(false);

log::info!("Initialising Hypervisor Handler");
log::info!("Initialising Hypervisor Handler id: {}", id);

let mut evar_lock_guard =
execution_variables.shm.try_lock().map_err(|e| {
Expand Down Expand Up @@ -367,7 +375,7 @@ impl HypervisorHandler {

match res {
Ok(_) => {
log::info!("Initialised Hypervisor Handler");
log::info!("Initialised Hypervisor Handler id: {}", id);
from_handler_tx
.send(HandlerMsg::FinishedHypervisorHandlerAction)
.map_err(|_| {
Expand All @@ -376,8 +384,8 @@ impl HypervisorHandler {
}
Err(e) => {
log::info!(
"Error initialising Hypervisor Handler: {:?}",
e
"Error initialising Hypervisor Handler: {:?} id: {}",
e, id
);
from_handler_tx.send(HandlerMsg::Error(e)).map_err(|_| {
HyperlightError::HypervisorHandlerCommunicationFailure()
Expand All @@ -394,7 +402,7 @@ impl HypervisorHandler {
#[cfg(target_os = "linux")]
execution_variables.run_cancelled.store(false);

info!("Dispatching call from host: {}", function_name);
info!("Dispatching call from host: {} id: {}", function_name, id);

let dispatch_function_addr = configuration
.dispatch_function_addr
Expand Down Expand Up @@ -473,8 +481,10 @@ impl HypervisorHandler {
match res {
Ok(_) => {
log::info!(
"Finished dispatching call from host: {}",
function_name
"Finished dispatching call from host: {} id: {}",
function_name,
id

);
from_handler_tx
.send(HandlerMsg::FinishedHypervisorHandlerAction)
Expand All @@ -484,9 +494,10 @@ impl HypervisorHandler {
}
Err(e) => {
log::info!(
"Error dispatching call from host: {}: {:?}",
"Error dispatching call from host: {}: {:?} id: {}",
function_name,
e
e,
id
);
from_handler_tx.send(HandlerMsg::Error(e)).map_err(|_| {
HyperlightError::HypervisorHandlerCommunicationFailure()
Expand All @@ -495,21 +506,22 @@ impl HypervisorHandler {
}
}
HypervisorHandlerAction::TerminateHandlerThread => {
info!("Terminating Hypervisor Handler Thread");
info!("Terminating Hypervisor Handler Thread id: {}", id);
break;
}
}
}

// If we make it here, it means the main thread issued a `TerminateHandlerThread` action,
// and we are now exiting the handler thread.
{
log::info!("Handler thread sending exit message, id: {}", id);
from_handler_tx
.send(HandlerMsg::FinishedHypervisorHandlerAction)
.map_err(|_| {
HyperlightError::HypervisorHandlerCommunicationFailure()
})?;
}).inspect_err(|e| log::error!("Failed to send finish message on termination: {:?} id :{}", e, id))?;
}
log::info!("Handler thread exiting, id: {}", id);

Ok(())
})
Expand Down Expand Up @@ -561,7 +573,7 @@ impl HypervisorHandler {
/// Tries to kill the Hypervisor Handler Thread.
#[instrument(err(Debug), skip_all, parent = Span::current(), level = "Trace")]
pub(crate) fn kill_hypervisor_handler_thread(&mut self) -> Result<()> {
log::debug!("Killing Hypervisor Handler Thread");
log::debug!("Killing Hypervisor Handler Thread Id {:?}", self.id);
self.execute_hypervisor_handler_action(HypervisorHandlerAction::TerminateHandlerThread)?;

self.try_join_hypervisor_handler_thread()
Expand All @@ -576,8 +588,9 @@ impl HypervisorHandler {
hypervisor_handler_action: HypervisorHandlerAction,
) -> Result<()> {
log::debug!(
"Sending Hypervisor Handler Action: {:?}",
hypervisor_handler_action
"Sending Hypervisor Handler Action: {:?} id: {}",
hypervisor_handler_action,
self.id
);

match hypervisor_handler_action {
Expand All @@ -600,7 +613,7 @@ impl HypervisorHandler {
.send(hypervisor_handler_action)
.map_err(|_| HyperlightError::HypervisorHandlerCommunicationFailure())?;

log::debug!("Waiting for Hypervisor Handler Response");
log::debug!("Waiting for Hypervisor Handler Response id: {}", self.id);

self.try_receive_handler_msg()
}
Expand All @@ -626,6 +639,8 @@ impl HypervisorHandler {
.from_handler_rx
.recv_timeout(self.execution_variables.get_timeout()?);

let id = self.id.clone();

match response {
Ok(msg) => match msg {
HandlerMsg::Error(e) => Err(e),
Expand All @@ -647,7 +662,13 @@ impl HypervisorHandler {
// If the thread has finished, we try to join it and return the error if it has one
let res = handle.join();
if res.as_ref().is_ok_and(|inner_res| inner_res.is_err()) {
return Err(res.unwrap().unwrap_err());
let err = res.unwrap().unwrap_err();
log::debug!(
"Handler thread finished with error: {:?} before sending message id: {}",
err,
id,
);
return Err(err);
}
Err(HyperlightError::HypervisorHandlerMessageReceiveTimedout())
}
Expand Down Expand Up @@ -724,7 +745,7 @@ impl HypervisorHandler {
// Re-initialise the vCPU.
// This is 100% needed because, otherwise, all it takes to cause a DoS is for a
// function to timeout as the vCPU will be in a bad state without re-init.
log::debug!("Re-initialising vCPU");
log::debug!("Re-initialising vCPU id {}", self.id);
self.execute_hypervisor_handler_action(HypervisorHandlerAction::Initialise)?;

res
Expand Down Expand Up @@ -931,12 +952,16 @@ fn set_up_hypervisor_partition(
match *get_available_hypervisor() {
#[cfg(mshv)]
Some(HypervisorType::Mshv) => {
log::debug!("Creating mshv hypervisor driver");
let hv = crate::hypervisor::hyperv_linux::HypervLinuxDriver::new(
regions,
entrypoint_ptr,
rsp_ptr,
pml4_ptr,
)?;
)
.inspect_err(|e| {
log::debug!("Failed to create mshv hypervisor driver: {:#}", e);
})?;
Ok(Box::new(hv))
}

Expand Down
10 changes: 8 additions & 2 deletions src/hyperlight_host/src/sandbox/initialized_multi_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ pub struct MultiUseSandbox {
// `create_1000_sandboxes`.
impl Drop for MultiUseSandbox {
fn drop(&mut self) {
log::debug!("Killing hypervisor handler thread in multiusesandbox drop");
match self.hv_handler.kill_hypervisor_handler_thread() {
Ok(_) => {}
Ok(_) => {
log::debug!("Killed hypervisor handler thread in multiusesandbox drop");
}
Err(e) => {
log::error!("[POTENTIAL THREAD LEAK] Potentially failed to kill hypervisor handler thread when dropping MultiUseSandbox: {:?}", e);
}
Expand Down Expand Up @@ -152,7 +155,10 @@ impl MultiUseSandbox {
/// ```
#[instrument(skip_all, parent = Span::current())]
pub fn new_call_context(self) -> MultiUseGuestCallContext {
MultiUseGuestCallContext::start(self)
log::debug!("Creating new MultiUseGuestCallContext");
let c = MultiUseGuestCallContext::start(self);
log::debug!("Created new MultiUseGuestCallContext");
c
}

/// Call a guest function by name, with the given return type and arguments.
Expand Down
12 changes: 9 additions & 3 deletions src/hyperlight_host/src/sandbox/uninitialized_evolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,15 @@ fn hv_init(

hv_handler
.execute_hypervisor_handler_action(HypervisorHandlerAction::Initialise)
.map_err(|exec_e| match hv_handler.kill_hypervisor_handler_thread() {
Ok(_) => exec_e,
Err(kill_e) => new_error!("{}", format!("{}, {}", exec_e, kill_e)),
.map_err(|exec_e| {
log::debug!(
"error executing hypervisor handler action: {exec_e} id: {}",
hv_handler.get_id()
);
match hv_handler.kill_hypervisor_handler_thread() {
Ok(_) => exec_e,
Err(kill_e) => new_error!("{}", format!("{}, {}", exec_e, kill_e)),
}
})?;

Ok(hv_handler)
Expand Down
Loading
Loading