@@ -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,22 +425,36 @@ 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
+
429
+ #[ cfg( not( inprocess) ) ]
430
+ let flags = PAGE_READWRITE ;
431
+ #[ cfg( inprocess) ]
432
+ let flags = PAGE_EXECUTE_READWRITE ;
433
+
428
434
let handle = unsafe {
429
435
CreateFileMappingA (
430
436
INVALID_HANDLE_VALUE ,
431
437
None ,
432
- PAGE_READWRITE ,
438
+ flags ,
433
439
dwmaximumsizehigh,
434
440
dwmaximumsizelow,
435
441
PCSTR :: null ( ) ,
436
442
) ?
437
443
} ;
438
444
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
+
442
451
#[ 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
+
444
458
if addr. Value . is_null ( ) {
445
459
log_then_return ! ( MemoryAllocationFailed (
446
460
Error :: last_os_error( ) . raw_os_error( )
0 commit comments