You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
let process_handle:HANDLE = self.process_receiver.recv()?.into();
149
+
let surrogate_process_handle:HANDLE = self.process_receiver.recv()?.into();
150
+
let mapping_file_handle:HANDLE = mmap_file_handle.into();
150
151
151
-
// allocate memory
152
+
// Allocate the memory by creating a view over the memory mapped file
153
+
154
+
// Use MapViewOfFile2 to map memoy into the surrogate process, the MapViewOfFile2 API is implemented in as an inline function in a windows header file
155
+
// (see https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffile2#remarks) so we use the same API it uses in the header file here instead of
156
+
// MapViewOfFile2 which does not exist in the rust crate (see https://github.com/microsoft/windows-rs/issues/2595)
157
+
constNUMA_NO_PREFERRED_NODE:u32 = 0xFFFFFFFF;
152
158
let allocated_address = unsafe{
153
-
VirtualAllocEx(
154
-
process_handle,
159
+
MapViewOfFileNuma2(
160
+
mapping_file_handle,
161
+
surrogate_process_handle,
162
+
0,
155
163
Some(raw_source_address),
156
164
raw_size,
157
-
MEM_COMMIT | MEM_RESERVE,
158
-
PAGE_READWRITE,
165
+
0,
166
+
PAGE_READWRITE.0,
167
+
NUMA_NO_PREFERRED_NODE,
159
168
)
160
169
};
161
-
if allocated_address.is_null(){
170
+
171
+
if allocated_address.Value.is_null(){
162
172
log_then_return!(
163
-
"VirtualAllocEx failed for mem address {:?}",
173
+
"MapViewOfFileNuma2 failed for mem address {:?}",
164
174
raw_source_address
165
175
);
166
176
}
167
177
168
-
// set up guard page
178
+
if allocated_address.Valueas*constc_void != raw_source_address {
INVALID_HANDLE_VALUE,// Causes the page file to be used as the backing store
456
+
None,
457
+
PAGE_READWRITE | SEC_COMMIT,
458
+
dwmaximumsizehigh,
459
+
dwmaximumsizelow,
460
+
PCSTR::null(),
461
+
)
462
+
.unwrap()
428
463
};
464
+
465
+
let addr = unsafe{MapViewOfFile(handle,FILE_MAP_ALL_ACCESS,0,0,0)};
466
+
429
467
let timer = Instant::now();
430
468
let surrogate_process = {
431
-
let res = surrogate_process_manager
432
-
.get_surrogate_process(size, allocated_address)?;
469
+
let res = surrogate_process_manager.get_surrogate_process(
470
+
size,
471
+
addr.Value,
472
+
HandleWrapper::from(handle),
473
+
)?;
433
474
let elapsed = timer.elapsed();
434
475
// Print out the time it took to get the process if its greater than 150ms (this is just to allow us to see that threads are blocking on the process queue)
435
476
if(elapsed.as_millis()asu64) > 150{
@@ -454,10 +495,11 @@ mod tests {
454
495
// dropping the surrogate process, as we do in the line
455
496
// below, will return it to the surrogate process manager
456
497
drop(surrogate_process);
457
-
unsafe{
458
-
let res = VirtualFree(allocated_address,0,MEM_RELEASE);
459
-
assert!(res.is_ok())
460
-
}
498
+
let res = unsafe{UnmapViewOfFile(addr)};
499
+
assert!(res.is_ok(),"Failed to UnmapViewOfFile: {:?}", res.err());
500
+
501
+
let res = unsafe{CloseHandle(handle)};
502
+
assert!(res.is_ok(),"Failed to CloseHandle: {:?}", res.err());
461
503
}
462
504
Ok(())
463
505
});
@@ -516,7 +558,11 @@ mod tests {
516
558
let mem = ExclusiveSharedMemory::new(SIZE).unwrap();
0 commit comments