Skip to content

Commit b9c67fb

Browse files
committed
Add clarification on why we must take GuestSharedMemory::lock
Previously, the code comments were not sufficiently clear as to why apparently-useless locks during the execution of the guest are necessary for the invariants of the GuestSharedMemory/HostSharedMemory split. This led to confusion as to whether it would be a good idea to remove them, so this commit clarifies that there is a good reason for them to be there. Signed-off-by: Lucy Menon <[email protected]>
1 parent 288d6f9 commit b9c67fb

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/hyperlight_host/src/hypervisor/hypervisor_handler.rs

+12
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,12 @@ impl HypervisorHandler {
322322
e
323323
)
324324
})?;
325+
// This apparently-useless lock is
326+
// needed to ensure the host does not
327+
// make unsynchronized accesses while
328+
// the guest is executing. See the
329+
// documentation for
330+
// GuestSharedMemory::lock.
325331
let mem_lock_guard = evar_lock_guard
326332
.as_mut()
327333
.ok_or_else(|| {
@@ -399,6 +405,12 @@ impl HypervisorHandler {
399405
e
400406
)
401407
})?;
408+
// This apparently-useless lock is
409+
// needed to ensure the host does not
410+
// make unsynchronized accesses while
411+
// the guest is executing. See the
412+
// documentation for
413+
// GuestSharedMemory::lock.
402414
let mem_lock_guard = evar_lock_guard
403415
.as_mut()
404416
.ok_or_else(|| {

src/hyperlight_host/src/mem/shared_mem.rs

+9
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ unsafe impl Send for ExclusiveSharedMemory {}
126126
pub struct GuestSharedMemory {
127127
region: Arc<HostMapping>,
128128
/// The lock that indicates this shared memory is being used by non-Rust code
129+
///
130+
/// This lock _must_ be held whenever the guest is executing,
131+
/// because it prevents the host from converting its
132+
/// HostSharedMemory to an ExclusiveSharedMemory. Since the guest
133+
/// may arbitrarily mutate the shared memory, only synchronized
134+
/// accesses from Rust should be allowed!
135+
///
136+
/// We cannot enforce this in the type system, because the memory
137+
/// is mapped in to the VM at VM creation time.
129138
pub lock: Arc<RwLock<()>>,
130139
}
131140
unsafe impl Send for GuestSharedMemory {}

0 commit comments

Comments
 (0)