Skip to content

Commit a61a7a8

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

2 files changed

Lines changed: 25 additions & 23 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: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,17 +1319,9 @@ mod tests {
13191319
guest_base: usize,
13201320
flags: MemoryRegionFlags,
13211321
) -> MemoryRegion {
1322-
#[cfg(not(windows))]
1323-
let host_base = mem.base_addr();
1324-
#[cfg(windows)]
1325-
let host_base = mem.host_region_base();
13261322
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-
);
13311323
MemoryRegion {
1332-
host_region: host_base..host_end,
1324+
host_region: mem.host_region_base()..mem.host_region_end(),
13331325
guest_region: guest_base..(guest_base + len),
13341326
flags,
13351327
region_type: MemoryRegionType::Heap,

0 commit comments

Comments
 (0)