@@ -27,12 +27,18 @@ use tracing::{instrument, Span};
2727use windows:: core:: PCSTR ;
2828#[ cfg( target_os = "windows" ) ]
2929use 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" ) ]
3135use 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" ) ]
3743use crate :: HyperlightError :: { MemoryRequestTooBig , WindowsAPIError } ;
3844use 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