Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CreateFileMapping\MapViewOfFile and UnmapViewOfFile\CloseHandle instead of VitualAllocEx and VirtualFreeEx` on Windows #135

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

simongdavies
Copy link
Contributor

This PR replaces the use of VirtualAllocEx and VirtualFreeEx with CreateFileMapping, MapViewOfFile, UnmapViewOfFile and CloseHandle on Windows.

This enables the mapping of memory into the surrogate processes without zero copying.

There are 9 commits as part of this PR each with its own description/explanation.

@simongdavies simongdavies added the enhancement New feature or request label Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 7 out of 10 changed files in this pull request and generated no comments.

Files not reviewed (3)
  • docs/assets/hyperlight_arch.excalidraw: Language not supported
  • src/hyperlight_host/src/hypervisor/hyperv_windows.rs: Evaluated as low risk
  • src/hyperlight_host/src/hypervisor/hypervisor_handler.rs: Evaluated as low risk
Comments suppressed due to low confidence (3)

src/hyperlight_host/src/mem/shared_mem.rs:111

  • The new behavior introduced in the Drop implementation of HostMapping should be covered by tests.
if let Err(e) = unsafe { UnmapViewOfFile(mem_mapped_address) } {

src/hyperlight_host/src/hypervisor/surrogate_process.rs:85

  • There is a spelling mistake in the error message: 'drropping' should be 'dropping'.
tracing::error!("Failed to get surrogate process manager when drropping SurrogateProcess: {:?}", e);

src/hyperlight_host/src/hypervisor/surrogate_process.rs:60

  • [nitpick] The variable 'mem_mapped_address' should follow Rust's naming convention and be renamed to 'memory_mapped_view_address'.
let mem_mapped_address = MEMORY_MAPPED_VIEW_ADDRESS {
Reflect the use of CreateFileMapping/MapViewOfFile/UnmapViewOfFile/CloseHandle instead of VirtualAlloc/VirtualFree

Signed-off-by: Simon Davies <[email protected]>
When the driver is created a handle to the FileMapping for the memory for this sandbox.

Uses that handle as an argument to the get_surrogate_process function on the SurrogateProcessManager

Removes the code that copies the Sandbox memory to and from the surrogate process memory each time the VM is run.

Signed-off-by: Simon Davies <[email protected]>
Replace use of VirtualAlloc/VirtualFree with CreateFileMapping/MapViewOFile/UnmapViewOfFile/CloseHandle to allow memry to be mapped into surrogate processes without any copying

Signed-off-by: Simon Davies <[email protected]>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 7 out of 10 changed files in this pull request and generated no comments.

Files not reviewed (3)
  • docs/assets/hyperlight_arch.excalidraw: Language not supported
  • src/hyperlight_host/src/hypervisor/hyperv_windows.rs: Evaluated as low risk
  • src/hyperlight_host/src/mem/mgr.rs: Evaluated as low risk
Comments suppressed due to low confidence (4)

src/hyperlight_host/src/sandbox/uninitialized.rs:1162

  • The error message should be clear and consistent. Consider rephrasing to: 'GuestBinary not found: 'some/path/that/does/not/exist': The system cannot find the path specified (os error)'.
matches!(sbox, Err(e) if e.to_string().contains("GuestBinary not found: 'some/path/that/does/not/exist': The system cannot find the path specified. (os error"))

src/hyperlight_host/src/hypervisor/surrogate_process.rs:66

  • The error message should be updated to reflect the new function name UnmapViewOfFile2.
Failed to free surrogate process resources (UnmapViewOfFile2 failed): {:?}

src/hyperlight_host/src/hypervisor/surrogate_process.rs:85

  • The error message should be updated to reflect the new function name return_surrogate_process.
Failed to get surrogate process manager when dropping SurrogateProcess: {:?}

src/hyperlight_host/src/hypervisor/surrogate_process.rs:64

  • Ensure that the new behavior introduced by UnmapViewOfFile2 is adequately covered by tests.
if let Err(e) = unsafe { UnmapViewOfFile2(process_handle, memory_mapped_view_address, flags) } {
@simongdavies simongdavies force-pushed the use-mmap-files branch 3 times, most recently from 0e2c611 to 7f56eec Compare January 16, 2025 23:15
@simongdavies simongdavies changed the title Use CreateFileMapping\MapViewOFile and UnmapViewOfFile\CloseHandle instead of VitualAllocEx and VirtualFreeEx` on Windows Use CreateFileMapping\MapViewOfFile and UnmapViewOfFile\CloseHandle instead of VitualAllocEx and VirtualFreeEx` on Windows Jan 16, 2025
Copy link
Contributor

@marosset marosset left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants