Skip to content

Commit 177a0c1

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 465259d commit 177a0c1

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/hyperlight_host/src/hypervisor/surrogate_process.rs

Lines changed: 28 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,16 @@ 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 memory_mapped_view_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) =
65+
unsafe { UnmapViewOfFile2(process_handle, memory_mapped_view_address, flags) }
66+
{
5967
tracing::error!(
60-
"Failed to free surrogate process resources (VirtualFreeEx failed): {:?}",
68+
"Failed to free surrogate process resources (UnmapViewOfFile2 failed): {:?}",
6169
e
6270
);
6371
}
@@ -66,9 +74,21 @@ impl Drop for SurrogateProcess {
6674
// of the SurrogateProcess being dropped. this is ok to
6775
// do because we are in the process of dropping ourselves
6876
// anyway.
69-
get_surrogate_process_manager()
70-
.unwrap()
71-
.return_surrogate_process(self.process_handle)
72-
.unwrap();
77+
match get_surrogate_process_manager() {
78+
Ok(manager) => match manager.return_surrogate_process(self.process_handle) {
79+
Ok(_) => (),
80+
Err(e) => {
81+
tracing::error!("Failed to return surrogate process to surrogate process manager when dropping : {:?}", e);
82+
return;
83+
}
84+
},
85+
Err(e) => {
86+
tracing::error!(
87+
"Failed to get surrogate process manager when dropping SurrogateProcess: {:?}",
88+
e
89+
);
90+
return;
91+
}
92+
}
7393
}
7494
}

0 commit comments

Comments
 (0)