Skip to content

Commit 564d0cc

Browse files
authored
map types public (#1313)
Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
1 parent d4c91ff commit 564d0cc

3 files changed

Lines changed: 85 additions & 5 deletions

File tree

src/hyperlight_host/src/hypervisor/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(crate) mod surrogate_process;
3131
pub(crate) mod surrogate_process_manager;
3232
/// Safe wrappers around windows types like `PSTR`
3333
#[cfg(target_os = "windows")]
34-
pub(crate) mod wrappers;
34+
pub mod wrappers;
3535

3636
#[cfg(crashdump)]
3737
pub(crate) mod crashdump;

src/hyperlight_host/src/mem/memory_region.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl TryFrom<hv_arm64_memory_intercept_message> for MemoryRegionFlags {
129129
// and crash dumps. Not part of the public API.
130130
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
131131
/// The type of memory region
132-
pub(crate) enum MemoryRegionType {
132+
pub enum MemoryRegionType {
133133
/// The region contains the guest's code
134134
Code,
135135
/// The region contains the guest's init data
@@ -156,7 +156,7 @@ impl MemoryRegionType {
156156
/// `MappedFile` regions use read-only file-backed mappings with no
157157
/// guard pages; all other region types use the standard sandbox
158158
/// shared memory mapping with guard pages.
159-
pub(crate) fn surrogate_mapping(&self) -> SurrogateMapping {
159+
pub fn surrogate_mapping(&self) -> SurrogateMapping {
160160
match self {
161161
MemoryRegionType::MappedFile => SurrogateMapping::ReadOnlyFile,
162162
_ => SurrogateMapping::SandboxMemory,
@@ -203,7 +203,7 @@ impl MemoryRegionKind for HostGuestMemoryRegion {
203203
/// behaviour when projected into the surrogate process via `MapViewOfFileNuma2`.
204204
#[cfg(target_os = "windows")]
205205
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
206-
pub(crate) enum SurrogateMapping {
206+
pub enum SurrogateMapping {
207207
/// Standard sandbox shared memory: mapped with `PAGE_READWRITE` protection
208208
/// and guard pages (`PAGE_NOACCESS`) set on the first and last pages.
209209
SandboxMemory,
@@ -293,7 +293,7 @@ pub struct MemoryRegion_<K: MemoryRegionKind> {
293293
/// memory access flags for the given region
294294
pub flags: MemoryRegionFlags,
295295
/// the type of memory region
296-
pub(crate) region_type: MemoryRegionType,
296+
pub region_type: MemoryRegionType,
297297
}
298298

299299
/// A memory region that tracks both host and guest addresses.

src/hyperlight_host/tests/integration_test.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,3 +1768,83 @@ fn interrupt_cancel_delete_race() {
17681768
handle.join().unwrap();
17691769
}
17701770
}
1771+
1772+
/// Compile-time regression guard for public visibility of memory
1773+
/// region types.
1774+
///
1775+
/// This test MUST live in an integration test (not a unit test)
1776+
/// because integration tests are compiled as separate crates and can
1777+
/// only access items marked `pub`. A unit test inside the crate
1778+
/// would also see `pub(crate)` items, defeating the purpose.
1779+
///
1780+
/// If any of these types or fields revert to `pub(crate)`, this test
1781+
/// will fail to **compile** — catching the regression before CI even
1782+
/// runs the test binary.
1783+
#[test]
1784+
fn memory_region_types_are_publicly_accessible() {
1785+
use hyperlight_host::mem::memory_region::{
1786+
HostGuestMemoryRegion, MemoryRegion_, MemoryRegionFlags, MemoryRegionKind, MemoryRegionType,
1787+
};
1788+
1789+
// This test is a compile-time guard. If it compiles, it passes.
1790+
// Every line below would cause a compile error if the referenced
1791+
// type, variant, or field reverted to pub(crate).
1792+
1793+
// MemoryRegionType enum and all its variants are pub
1794+
let _rt = MemoryRegionType::Code;
1795+
let _rt = MemoryRegionType::InitData;
1796+
let _rt = MemoryRegionType::Peb;
1797+
let _rt = MemoryRegionType::Heap;
1798+
let _rt = MemoryRegionType::Scratch;
1799+
let _rt = MemoryRegionType::Snapshot;
1800+
let _rt = MemoryRegionType::MappedFile;
1801+
1802+
// MemoryRegionFlags is pub and combinable
1803+
let _flags = MemoryRegionFlags::READ | MemoryRegionFlags::WRITE | MemoryRegionFlags::EXECUTE;
1804+
1805+
// SurrogateMapping enum and MemoryRegionType::surrogate_mapping()
1806+
// are pub (Windows only).
1807+
#[cfg(target_os = "windows")]
1808+
{
1809+
use hyperlight_host::mem::memory_region::SurrogateMapping;
1810+
1811+
let mapping = MemoryRegionType::MappedFile.surrogate_mapping();
1812+
let _: SurrogateMapping = mapping;
1813+
let _ = SurrogateMapping::SandboxMemory;
1814+
let _ = SurrogateMapping::ReadOnlyFile;
1815+
}
1816+
1817+
// MemoryRegion_ struct and all its fields are pub (struct literal
1818+
// construction requires every field to be pub).
1819+
#[cfg(not(target_os = "windows"))]
1820+
{
1821+
let base: <HostGuestMemoryRegion as MemoryRegionKind>::HostBaseType = 0x1000;
1822+
let _region = MemoryRegion_::<HostGuestMemoryRegion> {
1823+
guest_region: 0x1000..0x2000,
1824+
host_region: base..<HostGuestMemoryRegion as MemoryRegionKind>::add(base, 0x1000),
1825+
flags: MemoryRegionFlags::READ,
1826+
region_type: MemoryRegionType::Code,
1827+
};
1828+
}
1829+
1830+
#[cfg(target_os = "windows")]
1831+
{
1832+
use hyperlight_host::hypervisor::wrappers::HandleWrapper;
1833+
use hyperlight_host::mem::memory_region::HostRegionBase;
1834+
use windows::Win32::Foundation::HANDLE;
1835+
1836+
let host_base = HostRegionBase {
1837+
from_handle: HandleWrapper::from(HANDLE(std::ptr::null_mut())),
1838+
handle_base: 0x1000,
1839+
handle_size: 0x1000,
1840+
offset: 0,
1841+
};
1842+
let _region = MemoryRegion_::<HostGuestMemoryRegion> {
1843+
guest_region: 0x1000..0x2000,
1844+
host_region: host_base
1845+
..<HostGuestMemoryRegion as MemoryRegionKind>::add(host_base, 0x1000),
1846+
flags: MemoryRegionFlags::READ,
1847+
region_type: MemoryRegionType::Code,
1848+
};
1849+
}
1850+
}

0 commit comments

Comments
 (0)