Skip to content

Commit

Permalink
When dropping a surrogate process use UnmapViewOfFile2 instead of Vir…
Browse files Browse the repository at this point in the history
…tualFreeEx

Signed-off-by: Simon Davies <[email protected]>
  • Loading branch information
simongdavies committed Jan 16, 2025
1 parent 85b123e commit 67d4173
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/hyperlight_host/src/hypervisor/surrogate_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use core::ffi::c_void;

use tracing::{instrument, Span};
use windows::Win32::Foundation::HANDLE;
use windows::Win32::System::Memory::{VirtualFreeEx, MEM_RELEASE};
use windows::Win32::System::Memory::{
UnmapViewOfFile2, MEMORY_MAPPED_VIEW_ADDRESS, UNMAP_VIEW_OF_FILE_FLAGS,
};

use super::surrogate_process_manager::get_surrogate_process_manager;
use super::wrappers::HandleWrapper;
Expand Down Expand Up @@ -54,10 +56,14 @@ impl Default for SurrogateProcess {
impl Drop for SurrogateProcess {
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn drop(&mut self) {
let handle: HANDLE = self.process_handle.into();
if let Err(e) = unsafe { VirtualFreeEx(handle, self.allocated_address, 0, MEM_RELEASE) } {
let process_handle: HANDLE = self.process_handle.into();
let mem_mapped_address = MEMORY_MAPPED_VIEW_ADDRESS {
Value: self.allocated_address,
};
let flags = UNMAP_VIEW_OF_FILE_FLAGS(0);
if let Err(e) = unsafe { UnmapViewOfFile2(process_handle, mem_mapped_address, flags) } {
tracing::error!(
"Failed to free surrogate process resources (VirtualFreeEx failed): {:?}",
"Failed to free surrogate process resources (UnmapViewOfFile2 failed): {:?}",
e
);
}
Expand All @@ -66,9 +72,21 @@ impl Drop for SurrogateProcess {
// of the SurrogateProcess being dropped. this is ok to
// do because we are in the process of dropping ourselves
// anyway.
get_surrogate_process_manager()
.unwrap()
.return_surrogate_process(self.process_handle)
.unwrap();
match get_surrogate_process_manager() {
Ok(manager) => match manager.return_surrogate_process(self.process_handle) {
Ok(_) => (),
Err(e) => {
tracing::error!("Failed to return surrogate process to surrogate process manager when dropping : {:?}", e);
return;
}
},
Err(e) => {
tracing::error!(
"Failed to get surrogate process manager when drropping SurrogateProcess: {:?}",
e
);
return;
}
}
}
}

0 comments on commit 67d4173

Please sign in to comment.