Skip to content

Commit 1e45758

Browse files
jsturtevantCopilot
andauthored
Remove Linux-only restriction from map_region on MultiUseSandbox (#1330)
* Remove Linux-only restriction from map_region on MultiUseSandbox The map_region method and its tests were gated behind #[cfg(target_os = "linux")] but the underlying VM trait (VirtualMachine::map_memory) has implementations for KVM, MSHV, and WHP (Windows). The method itself contains no platform-specific code. - Remove #[cfg(target_os = "linux")] from map_region, its imports (log_then_return, MemoryRegionFlags), and all related tests/helpers - Fix region_for_memory helper to use platform-correct host base type (HostRegionBase on Windows, usize on Linux) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: James Sturtevant <jsturtevant@gmail.com> * Add cross-platform host_region_base/end helpers to SharedMemory Replace platform-conditional #[cfg] blocks and verbose turbofish calls with host_region_base() and host_region_end() on SharedMemory. This simplifies mapping_at() and the test region_for_memory helper. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: James Sturtevant <jsturtevant@gmail.com> --------- Signed-off-by: James Sturtevant <jsturtevant@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent de7f940 commit 1e45758

2 files changed

Lines changed: 27 additions & 33 deletions

File tree

src/hyperlight_host/src/mem/shared_mem.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -721,14 +721,9 @@ impl GuestSharedMemory {
721721
),
722722
};
723723
let guest_base = guest_base as usize;
724-
#[cfg(not(windows))]
725-
let host_base = self.base_addr();
726-
#[cfg(windows)]
727-
let host_base = self.host_region_base();
728-
let host_end = <HostGuestMemoryRegion as MemoryRegionKind>::add(host_base, self.mem_size());
729724
MemoryRegion {
730725
guest_region: guest_base..(guest_base + self.mem_size()),
731-
host_region: host_base..host_end,
726+
host_region: self.host_region_base()..self.host_region_end(),
732727
region_type,
733728
flags,
734729
}
@@ -779,15 +774,30 @@ pub trait SharedMemory {
779774
}
780775

781776
/// Extract a base address that can be mapped into a VM for this
782-
/// SharedMemory
783-
#[cfg(target_os = "windows")]
784-
fn host_region_base(&self) -> super::memory_region::HostRegionBase {
785-
super::memory_region::HostRegionBase {
786-
from_handle: self.region().handle.into(),
787-
handle_base: self.region().ptr as usize,
788-
handle_size: self.region().size,
789-
offset: PAGE_SIZE_USIZE,
777+
/// SharedMemory.
778+
///
779+
/// On Linux this returns a raw `usize` pointer. On Windows it
780+
/// returns a [`HostRegionBase`](super::memory_region::HostRegionBase)
781+
/// that carries the file-mapping handle metadata needed by WHP.
782+
fn host_region_base(&self) -> <HostGuestMemoryRegion as MemoryRegionKind>::HostBaseType {
783+
#[cfg(not(windows))]
784+
{
785+
self.base_addr()
790786
}
787+
#[cfg(windows)]
788+
{
789+
super::memory_region::HostRegionBase {
790+
from_handle: self.region().handle.into(),
791+
handle_base: self.region().ptr as usize,
792+
handle_size: self.region().size,
793+
offset: PAGE_SIZE_USIZE,
794+
}
795+
}
796+
}
797+
798+
/// Return the end address of the host region (base + usable size).
799+
fn host_region_end(&self) -> <HostGuestMemoryRegion as MemoryRegionKind>::HostBaseType {
800+
<HostGuestMemoryRegion as MemoryRegionKind>::add(self.host_region_base(), self.mem_size())
791801
}
792802

793803
/// Run some code with exclusive access to the SharedMemory

src/hyperlight_host/src/sandbox/initialized_multi_use.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,16 @@ use super::file_mapping::prepare_file_cow;
3232
use super::host_funcs::FunctionRegistry;
3333
use super::snapshot::Snapshot;
3434
use crate::HyperlightError::{self, SnapshotSandboxMismatch};
35-
use crate::Result;
3635
use crate::func::{ParameterTuple, SupportedReturnType};
3736
use crate::hypervisor::InterruptHandle;
3837
use crate::hypervisor::hyperlight_vm::{HyperlightVm, HyperlightVmError};
39-
#[cfg(target_os = "linux")]
40-
use crate::log_then_return;
41-
use crate::mem::memory_region::MemoryRegion;
42-
#[cfg(target_os = "linux")]
43-
use crate::mem::memory_region::MemoryRegionFlags;
38+
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
4439
use crate::mem::mgr::SandboxMemoryManager;
4540
use crate::mem::shared_mem::{HostSharedMemory, SharedMemory as _};
4641
use crate::metrics::{
4742
METRIC_GUEST_ERROR, METRIC_GUEST_ERROR_LABEL_CODE, maybe_time_and_emit_guest_call,
4843
};
44+
use crate::{Result, log_then_return};
4945

5046
/// A fully initialized sandbox that can execute guest functions multiple times.
5147
///
@@ -525,7 +521,6 @@ impl MultiUseSandbox {
525521
/// The caller must ensure the host memory region remains valid and unmodified
526522
/// for the lifetime of `self`.
527523
#[instrument(err(Debug), skip(self, rgn), parent = Span::current())]
528-
#[cfg(target_os = "linux")]
529524
pub unsafe fn map_region(&mut self, rgn: &MemoryRegion) -> Result<()> {
530525
if self.poisoned {
531526
return Err(crate::HyperlightError::PoisonedSandbox);
@@ -914,9 +909,7 @@ mod tests {
914909
use hyperlight_testing::sandbox_sizes::{LARGE_HEAP_SIZE, MEDIUM_HEAP_SIZE, SMALL_HEAP_SIZE};
915910
use hyperlight_testing::simple_guest_as_string;
916911

917-
#[cfg(target_os = "linux")]
918912
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags, MemoryRegionType};
919-
#[cfg(target_os = "linux")]
920913
use crate::mem::shared_mem::{ExclusiveSharedMemory, GuestSharedMemory, SharedMemory as _};
921914
use crate::sandbox::SandboxConfiguration;
922915
use crate::{GuestBinary, HyperlightError, MultiUseSandbox, Result, UninitializedSandbox};
@@ -955,7 +948,6 @@ mod tests {
955948
}
956949

957950
// map_region should fail when poisoned
958-
#[cfg(target_os = "linux")]
959951
{
960952
let map_mem = allocate_guest_memory();
961953
let guest_base = 0x0;
@@ -965,7 +957,6 @@ mod tests {
965957
}
966958

967959
// map_file_cow should fail when poisoned
968-
#[cfg(target_os = "linux")]
969960
{
970961
let temp_file = std::env::temp_dir().join("test_poison_map_file.bin");
971962
let res = sbox.map_file_cow(&temp_file, 0x0, None).unwrap_err();
@@ -1227,7 +1218,6 @@ mod tests {
12271218
}
12281219
}
12291220

1230-
#[cfg(target_os = "linux")]
12311221
#[test]
12321222
fn test_mmap() {
12331223
let mut sbox = UninitializedSandbox::new(
@@ -1263,7 +1253,6 @@ mod tests {
12631253
}
12641254

12651255
// Makes sure MemoryRegionFlags::READ | MemoryRegionFlags::EXECUTE executable but not writable
1266-
#[cfg(target_os = "linux")]
12671256
#[test]
12681257
fn test_mmap_write_exec() {
12691258
let mut sbox = UninitializedSandbox::new(
@@ -1312,7 +1301,6 @@ mod tests {
13121301
};
13131302
}
13141303

1315-
#[cfg(target_os = "linux")]
13161304
fn page_aligned_memory(src: &[u8]) -> GuestSharedMemory {
13171305
use hyperlight_common::mem::PAGE_SIZE_USIZE;
13181306

@@ -1326,29 +1314,25 @@ mod tests {
13261314
guest_mem
13271315
}
13281316

1329-
#[cfg(target_os = "linux")]
13301317
fn region_for_memory(
13311318
mem: &GuestSharedMemory,
13321319
guest_base: usize,
13331320
flags: MemoryRegionFlags,
13341321
) -> MemoryRegion {
1335-
let ptr = mem.base_addr();
13361322
let len = mem.mem_size();
13371323
MemoryRegion {
1338-
host_region: ptr..(ptr + len),
1324+
host_region: mem.host_region_base()..mem.host_region_end(),
13391325
guest_region: guest_base..(guest_base + len),
13401326
flags,
13411327
region_type: MemoryRegionType::Heap,
13421328
}
13431329
}
13441330

1345-
#[cfg(target_os = "linux")]
13461331
fn allocate_guest_memory() -> GuestSharedMemory {
13471332
page_aligned_memory(b"test data for snapshot")
13481333
}
13491334

13501335
#[test]
1351-
#[cfg(target_os = "linux")]
13521336
fn snapshot_restore_handles_remapping_correctly() {
13531337
let mut sbox: MultiUseSandbox = {
13541338
let path = simple_guest_as_string().unwrap();

0 commit comments

Comments
 (0)