@@ -27,12 +27,18 @@ use tracing::{instrument, Span};
27
27
use windows:: core:: PCSTR ;
28
28
#[ cfg( target_os = "windows" ) ]
29
29
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 ;
30
34
#[ cfg( target_os = "windows" ) ]
31
35
use windows:: Win32 :: System :: Memory :: {
32
36
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 ,
34
38
} ;
35
39
40
+ #[ cfg( target_os = "windows" ) ]
41
+ use crate :: HyperlightError :: MemoryAllocationFailed ;
36
42
#[ cfg( target_os = "windows" ) ]
37
43
use crate :: HyperlightError :: { MemoryRequestTooBig , WindowsAPIError } ;
38
44
use crate :: { log_then_return, new_error, Result } ;
@@ -388,12 +394,6 @@ impl ExclusiveSharedMemory {
388
394
#[ cfg( target_os = "windows" ) ]
389
395
#[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
390
396
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
-
397
397
if min_size_bytes == 0 {
398
398
return Err ( new_error ! ( "Cannot create shared memory with size 0" ) ) ;
399
399
}
@@ -425,6 +425,7 @@ impl ExclusiveSharedMemory {
425
425
426
426
// Allocate the memory use CreateFileMapping instead of VirtualAlloc
427
427
// This allows us to map the memory into the surrogate process using MapViewOfFile2
428
+ #[ cfg( not( inprocess) ) ]
428
429
let handle = unsafe {
429
430
CreateFileMappingA (
430
431
INVALID_HANDLE_VALUE ,
@@ -437,10 +438,24 @@ impl ExclusiveSharedMemory {
437
438
} ;
438
439
439
440
#[ 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
+
442
452
#[ cfg( not( inprocess) ) ]
443
453
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
+
444
459
if addr. Value . is_null ( ) {
445
460
log_then_return ! ( MemoryAllocationFailed (
446
461
Error :: last_os_error( ) . raw_os_error( )
0 commit comments