Skip to content

Commit 9f22e6a

Browse files
committed
Fix issues with using CreateMapViewOfFile with inprocess feature
Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
1 parent adee60a commit 9f22e6a

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

src/hyperlight_host/src/hypervisor/hypervisor_handler.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ fn set_up_hypervisor_partition(
871871
leaked_outb_wrapper,
872872
})?;
873873
Ok(Box::new(hv))
874-
} else if #[cfg(feature = "inprocess")]{
874+
} else if #[cfg(inprocess)]{
875875
// in-process feature, but not debug build
876876
log_then_return!("In-process mode is only available on debug-builds");
877877
} else if #[cfg(debug_assertions)] {
@@ -937,6 +937,8 @@ mod tests {
937937
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
938938
use hyperlight_testing::simple_guest_as_string;
939939

940+
#[cfg(target_os = "windows")]
941+
use crate::sandbox::SandboxConfiguration;
940942
use crate::sandbox::WrapperGetter;
941943
use crate::sandbox_state::sandbox::EvolvableSandbox;
942944
use crate::sandbox_state::transition::Noop;
@@ -950,9 +952,26 @@ mod tests {
950952
if !is_hypervisor_present() {
951953
panic!("Panic on create_multi_use_sandbox because no hypervisor is present");
952954
}
955+
956+
// Tests that use this function seem to fail with timeouts sporadically on windows so timeouts are raised here
957+
958+
let cfg = {
959+
#[cfg(target_os = "windows")]
960+
{
961+
let mut cfg = SandboxConfiguration::default();
962+
cfg.set_max_initialization_time(std::time::Duration::from_secs(10));
963+
cfg.set_max_execution_time(std::time::Duration::from_secs(3));
964+
Some(cfg)
965+
}
966+
#[cfg(not(target_os = "windows"))]
967+
{
968+
None
969+
}
970+
};
971+
953972
let usbox = UninitializedSandbox::new(
954973
GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")),
955-
None,
974+
cfg,
956975
None,
957976
None,
958977
)

src/hyperlight_host/src/mem/shared_mem.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ use tracing::{instrument, Span};
2727
use windows::core::PCSTR;
2828
#[cfg(target_os = "windows")]
2929
use windows::Win32::Foundation::{CloseHandle, HANDLE, INVALID_HANDLE_VALUE};
30+
#[cfg(all(target_os = "windows", inprocess))]
31+
use windows::Win32::System::Memory::FILE_MAP_EXECUTE;
32+
#[cfg(all(target_os = "windows", not(inprocess)))]
33+
use windows::Win32::System::Memory::PAGE_READWRITE;
3034
#[cfg(target_os = "windows")]
3135
use windows::Win32::System::Memory::{
3236
CreateFileMappingA, MapViewOfFile, UnmapViewOfFile, VirtualProtect, FILE_MAP_ALL_ACCESS,
33-
MEMORY_MAPPED_VIEW_ADDRESS, PAGE_EXECUTE_READWRITE, PAGE_PROTECTION_FLAGS, PAGE_READWRITE,
37+
MEMORY_MAPPED_VIEW_ADDRESS, PAGE_EXECUTE_READWRITE, PAGE_NOACCESS, PAGE_PROTECTION_FLAGS,
3438
};
3539

40+
#[cfg(target_os = "windows")]
41+
use crate::HyperlightError::MemoryAllocationFailed;
3642
#[cfg(target_os = "windows")]
3743
use crate::HyperlightError::{MemoryRequestTooBig, WindowsAPIError};
3844
use crate::{log_then_return, new_error, Result};
@@ -388,12 +394,6 @@ impl ExclusiveSharedMemory {
388394
#[cfg(target_os = "windows")]
389395
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
390396
pub fn new(min_size_bytes: usize) -> Result<Self> {
391-
#[cfg(inprocess)]
392-
use windows::Win32::System::Memory::FILE_MAP_EXECUTE;
393-
use windows::Win32::System::Memory::{PAGE_NOACCESS, PAGE_PROTECTION_FLAGS};
394-
395-
use crate::HyperlightError::MemoryAllocationFailed;
396-
397397
if min_size_bytes == 0 {
398398
return Err(new_error!("Cannot create shared memory with size 0"));
399399
}
@@ -425,22 +425,36 @@ impl ExclusiveSharedMemory {
425425

426426
// Allocate the memory use CreateFileMapping instead of VirtualAlloc
427427
// This allows us to map the memory into the surrogate process using MapViewOfFile2
428+
429+
#[cfg(not(inprocess))]
430+
let flags = PAGE_READWRITE;
431+
#[cfg(inprocess)]
432+
let flags = PAGE_EXECUTE_READWRITE;
433+
428434
let handle = unsafe {
429435
CreateFileMappingA(
430436
INVALID_HANDLE_VALUE,
431437
None,
432-
PAGE_READWRITE,
438+
flags,
433439
dwmaximumsizehigh,
434440
dwmaximumsizelow,
435441
PCSTR::null(),
436442
)?
437443
};
438444

439-
#[cfg(inprocess)]
440-
let addr =
441-
unsafe { MapViewOfFile(handle, FILE_MAP_ALL_ACCESS | FILE_MAP_EXECUTE, 0, 0, 0) };
445+
if handle.is_invalid() {
446+
log_then_return!(MemoryAllocationFailed(
447+
Error::last_os_error().raw_os_error()
448+
));
449+
}
450+
442451
#[cfg(not(inprocess))]
443-
let addr = unsafe { MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, 0) };
452+
let file_map = FILE_MAP_ALL_ACCESS;
453+
#[cfg(inprocess)]
454+
let file_map = FILE_MAP_ALL_ACCESS | FILE_MAP_EXECUTE;
455+
456+
let addr = unsafe { MapViewOfFile(handle, file_map, 0, 0, 0) };
457+
444458
if addr.Value.is_null() {
445459
log_then_return!(MemoryAllocationFailed(
446460
Error::last_os_error().raw_os_error()

0 commit comments

Comments
 (0)