Skip to content

Commit ce53ee6

Browse files
committed
When dropping a surrogate process use UnmapViewOfFile2 instead of VirtualFreeEx
Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
1 parent 3c8ee07 commit ce53ee6

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

src/hyperlight_host/src/hypervisor/surrogate_process.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use core::ffi::c_void;
1818

1919
use tracing::{instrument, Span};
2020
use windows::Win32::Foundation::HANDLE;
21-
use windows::Win32::System::Memory::{VirtualFreeEx, MEM_RELEASE};
21+
use windows::Win32::System::Memory::{
22+
UnmapViewOfFile2, MEMORY_MAPPED_VIEW_ADDRESS, UNMAP_VIEW_OF_FILE_FLAGS,
23+
};
2224

2325
use super::surrogate_process_manager::get_surrogate_process_manager;
2426
use super::wrappers::HandleWrapper;
@@ -54,10 +56,14 @@ impl Default for SurrogateProcess {
5456
impl Drop for SurrogateProcess {
5557
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
5658
fn drop(&mut self) {
57-
let handle: HANDLE = self.process_handle.into();
58-
if let Err(e) = unsafe { VirtualFreeEx(handle, self.allocated_address, 0, MEM_RELEASE) } {
59+
let process_handle: HANDLE = self.process_handle.into();
60+
let mem_mapped_address = MEMORY_MAPPED_VIEW_ADDRESS {
61+
Value: self.allocated_address,
62+
};
63+
let flags = UNMAP_VIEW_OF_FILE_FLAGS(0);
64+
if let Err(e) = unsafe { UnmapViewOfFile2(process_handle, mem_mapped_address, flags) } {
5965
tracing::error!(
60-
"Failed to free surrogate process resources (VirtualFreeEx failed): {:?}",
66+
"Failed to free surrogate process resources (UnmapViewOfFile2 failed): {:?}",
6167
e
6268
);
6369
}
@@ -66,9 +72,21 @@ impl Drop for SurrogateProcess {
6672
// of the SurrogateProcess being dropped. this is ok to
6773
// do because we are in the process of dropping ourselves
6874
// anyway.
69-
get_surrogate_process_manager()
70-
.unwrap()
71-
.return_surrogate_process(self.process_handle)
72-
.unwrap();
75+
match get_surrogate_process_manager() {
76+
Ok(manager) => match manager.return_surrogate_process(self.process_handle) {
77+
Ok(_) => (),
78+
Err(e) => {
79+
tracing::error!("Failed to return surrogate process to surrogate process manager when dropping : {:?}", e);
80+
return;
81+
}
82+
},
83+
Err(e) => {
84+
tracing::error!(
85+
"Failed to get surrogate process manager when drropping SurrogateProcess: {:?}",
86+
e
87+
);
88+
return;
89+
}
90+
}
7391
}
7492
}

0 commit comments

Comments
 (0)