Skip to content

Commit 2ee6459

Browse files
committed
Fix issues with using CreateMapViewOfFile with inprocess feature
Also extends timeouts for failing tests Signed-off-by: Simon Davies <[email protected]>
1 parent adee60a commit 2ee6459

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/hyperlight_host/src/hypervisor/hypervisor_handler.rs

Lines changed: 8 additions & 3 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,7 +937,7 @@ mod tests {
937937
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
938938
use hyperlight_testing::simple_guest_as_string;
939939

940-
use crate::sandbox::WrapperGetter;
940+
use crate::sandbox::{SandboxConfiguration, WrapperGetter};
941941
use crate::sandbox_state::sandbox::EvolvableSandbox;
942942
use crate::sandbox_state::transition::Noop;
943943
use crate::HyperlightError::HypervisorHandlerExecutionCancelAttemptOnFinishedExecution;
@@ -950,9 +950,14 @@ mod tests {
950950
if !is_hypervisor_present() {
951951
panic!("Panic on create_multi_use_sandbox because no hypervisor is present");
952952
}
953+
954+
let mut cfg = SandboxConfiguration::default();
955+
cfg.set_max_initialization_time(std::time::Duration::from_secs(5));
956+
cfg.set_max_execution_time(std::time::Duration::from_secs(5));
957+
953958
let usbox = UninitializedSandbox::new(
954959
GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")),
955-
None,
960+
Some(cfg),
956961
None,
957962
None,
958963
)

src/hyperlight_host/src/mem/shared_mem.rs

Lines changed: 24 additions & 9 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,6 +425,7 @@ 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+
#[cfg(not(inprocess))]
428429
let handle = unsafe {
429430
CreateFileMappingA(
430431
INVALID_HANDLE_VALUE,
@@ -437,10 +438,24 @@ impl ExclusiveSharedMemory {
437438
};
438439

439440
#[cfg(inprocess)]
440-
let addr =
441-
unsafe { MapViewOfFile(handle, FILE_MAP_ALL_ACCESS | FILE_MAP_EXECUTE, 0, 0, 0) };
441+
let handle = unsafe {
442+
CreateFileMappingA(
443+
INVALID_HANDLE_VALUE,
444+
None,
445+
PAGE_EXECUTE_READWRITE,
446+
dwmaximumsizehigh,
447+
dwmaximumsizelow,
448+
PCSTR::null(),
449+
)?
450+
};
451+
442452
#[cfg(not(inprocess))]
443453
let addr = unsafe { MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, 0) };
454+
455+
#[cfg(inprocess)]
456+
let addr =
457+
unsafe { MapViewOfFile(handle, FILE_MAP_ALL_ACCESS | FILE_MAP_EXECUTE, 0, 0, 0) };
458+
444459
if addr.Value.is_null() {
445460
log_then_return!(MemoryAllocationFailed(
446461
Error::last_os_error().raw_os_error()

0 commit comments

Comments
 (0)