Skip to content

Commit c41a531

Browse files
committed
Fix ownership Sync issues
Signed-off-by: Simon Davies <[email protected]>
1 parent 14063f7 commit c41a531

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

src/hyperlight_host/src/hypervisor/hyperv_linux.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,14 @@ use crate::HyperlightError;
7878
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
7979
use crate::mem::ptr::{GuestPtr, RawPtr};
8080
use crate::mem::shared_mem::HostSharedMemory;
81+
use crate::sandbox::SandboxConfiguration;
8182
#[cfg(feature = "trace_guest")]
8283
use crate::sandbox::TraceInfo;
8384
use crate::sandbox::host_funcs::FunctionRegistry;
85+
use crate::sandbox::mem_mgr::MemMgrWrapper;
8486
use crate::sandbox::outb::handle_outb;
8587
#[cfg(crashdump)]
8688
use crate::sandbox::uninitialized::SandboxRuntimeConfig;
87-
use crate::sandbox::{MemMgrWrapper, SandboxConfiguration};
8889
use crate::{Result, log_then_return, new_error};
8990

9091
#[cfg(gdb)]

src/hyperlight_host/src/hypervisor/hyperv_windows.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,12 @@ pub(crate) struct HypervWindowsDriver {
295295
#[allow(dead_code)]
296296
trace_info: TraceInfo,
297297
}
298-
/* This does not automatically impl Send/Sync because the host
298+
/* This does not automatically impl Send because the host
299299
* address of the shared memory region is a raw pointer, which are
300-
* marked as !Send and !Sync. However, the access patterns used
300+
* marked as !Send (and !Sync). However, the access patterns used
301301
* here are safe.
302302
*/
303303
unsafe impl Send for HypervWindowsDriver {}
304-
unsafe impl Sync for HypervWindowsDriver {}
305304

306305
impl HypervWindowsDriver {
307306
#[allow(clippy::too_many_arguments)]

src/hyperlight_host/src/hypervisor/kvm.rs

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ limitations under the License.
1616

1717
use std::convert::TryFrom;
1818
use std::fmt::Debug;
19-
use std::sync::Arc;
20-
#[cfg(gdb)]
21-
use std::sync::Mutex;
2219
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
20+
use std::sync::{Arc, Mutex};
2321

2422
use kvm_bindings::{kvm_fpu, kvm_regs, kvm_userspace_memory_region};
2523
use kvm_ioctls::Cap::UserMemory;
@@ -609,24 +607,41 @@ impl Hypervisor for KVMDriver {
609607
padded[..copy_len].copy_from_slice(&data[..copy_len]);
610608
let value = u32::from_le_bytes(padded);
611609

612-
let mem_mgr = self
613-
.mem_mgr
614-
.as_mut()
615-
.ok_or_else(|| new_error!("mem_mgr not initialized"))?;
616-
let host_funcs = self
617-
.host_funcs
618-
.as_ref()
619-
.ok_or_else(|| new_error!("host_funcs not initialized"))?
620-
.clone();
621-
622-
handle_outb(
623-
mem_mgr,
624-
host_funcs,
625-
#[cfg(feature = "trace_guest")]
626-
self,
627-
port,
628-
value,
629-
)?;
610+
#[cfg(feature = "trace_guest")]
611+
{
612+
// We need to handle the borrow checker issue where we need both:
613+
// - &mut MemMgrWrapper (from self.mem_mgr.as_mut())
614+
// - &mut dyn Hypervisor (from self)
615+
// We'll use a temporary approach to extract the mem_mgr temporarily
616+
let mem_mgr_option = self.mem_mgr.take();
617+
let mut mem_mgr =
618+
mem_mgr_option.ok_or_else(|| new_error!("mem_mgr not initialized"))?;
619+
let host_funcs = self
620+
.host_funcs
621+
.as_ref()
622+
.ok_or_else(|| new_error!("host_funcs not initialized"))?
623+
.clone();
624+
625+
handle_outb(&mut mem_mgr, host_funcs, self, port, value)?;
626+
627+
// Put the mem_mgr back
628+
self.mem_mgr = Some(mem_mgr);
629+
}
630+
631+
#[cfg(not(feature = "trace_guest"))]
632+
{
633+
let mem_mgr = self
634+
.mem_mgr
635+
.as_mut()
636+
.ok_or_else(|| new_error!("mem_mgr not initialized"))?;
637+
let host_funcs = self
638+
.host_funcs
639+
.as_ref()
640+
.ok_or_else(|| new_error!("host_funcs not initialized"))?
641+
.clone();
642+
643+
handle_outb(mem_mgr, host_funcs, port, value)?;
644+
}
630645
}
631646

632647
Ok(())

src/hyperlight_host/src/hypervisor/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub enum TraceRegister {
136136
}
137137

138138
/// A common set of hypervisor functionality
139-
pub(crate) trait Hypervisor: Debug + Sync + Send {
139+
pub(crate) trait Hypervisor: Debug + Send {
140140
/// Initialise the internally stored vCPU with the given PEB address and
141141
/// random number seed, then run it until a HLT instruction.
142142
#[allow(clippy::too_many_arguments)]

0 commit comments

Comments
 (0)