Skip to content

Commit 98889be

Browse files
jsturtevantCopilot
andcommitted
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>
1 parent cb17894 commit 98889be

1 file changed

Lines changed: 11 additions & 19 deletions

File tree

src/hyperlight_host/src/sandbox/initialized_multi_use.rs

Lines changed: 11 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,33 @@ 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();
1322+
#[cfg(not(windows))]
1323+
let host_base = mem.base_addr();
1324+
#[cfg(windows)]
1325+
let host_base = mem.host_region_base();
13361326
let len = mem.mem_size();
1327+
let host_end =
1328+
<crate::mem::memory_region::HostGuestMemoryRegion as crate::mem::memory_region::MemoryRegionKind>::add(
1329+
host_base, len,
1330+
);
13371331
MemoryRegion {
1338-
host_region: ptr..(ptr + len),
1332+
host_region: host_base..host_end,
13391333
guest_region: guest_base..(guest_base + len),
13401334
flags,
13411335
region_type: MemoryRegionType::Heap,
13421336
}
13431337
}
13441338

1345-
#[cfg(target_os = "linux")]
13461339
fn allocate_guest_memory() -> GuestSharedMemory {
13471340
page_aligned_memory(b"test data for snapshot")
13481341
}
13491342

13501343
#[test]
1351-
#[cfg(target_os = "linux")]
13521344
fn snapshot_restore_handles_remapping_correctly() {
13531345
let mut sbox: MultiUseSandbox = {
13541346
let path = simple_guest_as_string().unwrap();

0 commit comments

Comments
 (0)