Skip to content

Commit df9e7f2

Browse files
simongdaviesludfjig
authored andcommitted
Integrate DirtyPageTracker into UninitializedSandbox starting host page tracking after memory is allocated and stopping it once the uninitialized sandbox is evolved
Signed-off-by: Simon Davies <[email protected]>
1 parent 2128991 commit df9e7f2

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/hyperlight_host/src/sandbox/uninitialized.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use crate::func::host_functions::{HostFunction, register_host_function};
2929
use crate::func::{ParameterTuple, SupportedReturnType};
3030
#[cfg(feature = "build-metadata")]
3131
use crate::log_build_details;
32+
use crate::mem::dirty_page_tracking::DirtyPageTracker;
3233
use crate::mem::exe::ExeInfo;
3334
use crate::mem::memory_region::{DEFAULT_GUEST_BLOB_MEM_FLAGS, MemoryRegionFlags};
3435
use crate::mem::mgr::{STACK_COOKIE_LEN, SandboxMemoryManager};
@@ -80,6 +81,7 @@ pub struct UninitializedSandbox {
8081
pub(crate) config: SandboxConfiguration,
8182
#[cfg(any(crashdump, gdb))]
8283
pub(crate) rt_cfg: SandboxRuntimeConfig,
84+
pub(crate) tracker: Option<DirtyPageTracker>,
8385
}
8486

8587
impl crate::sandbox_state::sandbox::UninitializedSandbox for UninitializedSandbox {
@@ -250,17 +252,15 @@ impl UninitializedSandbox {
250252
}
251253
};
252254

253-
let mut mem_mgr_wrapper = {
254-
let mut mgr = UninitializedSandbox::load_guest_binary(
255-
sandbox_cfg,
256-
&guest_binary,
257-
guest_blob.as_ref(),
258-
)?;
255+
let (mut mgr, tracker) = UninitializedSandbox::load_guest_binary(
256+
sandbox_cfg,
257+
&guest_binary,
258+
guest_blob.as_ref(),
259+
)?;
259260

260-
let stack_guard = Self::create_stack_guard();
261-
mgr.set_stack_guard(&stack_guard)?;
262-
MemMgrWrapper::new(mgr, stack_guard)
263-
};
261+
let stack_guard = Self::create_stack_guard();
262+
mgr.set_stack_guard(&stack_guard)?;
263+
let mut mem_mgr_wrapper = MemMgrWrapper::new(mgr, stack_guard);
264264

265265
mem_mgr_wrapper.write_memory_layout()?;
266266

@@ -278,6 +278,7 @@ impl UninitializedSandbox {
278278
config: sandbox_cfg,
279279
#[cfg(any(crashdump, gdb))]
280280
rt_cfg,
281+
tracker: Some(tracker),
281282
};
282283

283284
// If we were passed a writer for host print register it otherwise use the default.
@@ -308,7 +309,10 @@ impl UninitializedSandbox {
308309
cfg: SandboxConfiguration,
309310
guest_binary: &GuestBinary,
310311
guest_blob: Option<&GuestBlob>,
311-
) -> Result<SandboxMemoryManager<ExclusiveSharedMemory>> {
312+
) -> Result<(
313+
SandboxMemoryManager<ExclusiveSharedMemory>,
314+
DirtyPageTracker,
315+
)> {
312316
let mut exe_info = match guest_binary {
313317
GuestBinary::FilePath(bin_path_str) => ExeInfo::from_file(bin_path_str)?,
314318
GuestBinary::Buffer(buffer) => ExeInfo::from_buf(buffer)?,
@@ -396,6 +400,7 @@ impl UninitializedSandbox {
396400
Ok(())
397401
}
398402
}
403+
399404
// Check to see if the current version of Windows is supported
400405
// Hyperlight is only supported on Windows 11 and Windows Server 2022 and later
401406
#[cfg(target_os = "windows")]

src/hyperlight_host/src/sandbox/uninitialized_evolve.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use super::uninitialized::SandboxRuntimeConfig;
2828
use crate::HyperlightError::NoHypervisorFound;
2929
use crate::hypervisor::Hypervisor;
3030
use crate::hypervisor::handlers::{MemAccessHandlerCaller, OutBHandlerCaller};
31+
use crate::mem::dirty_page_tracking::DirtyPageTracker;
3132
use crate::mem::layout::SandboxMemoryLayout;
3233
use crate::mem::mgr::SandboxMemoryManager;
3334
use crate::mem::ptr::{GuestPtr, RawPtr};
@@ -73,11 +74,13 @@ where
7374
) -> Result<ResSandbox>,
7475
{
7576
let (hshm, mut gshm) = u_sbox.mgr.build();
77+
7678
let mut vm = set_up_hypervisor_partition(
7779
&mut gshm,
7880
&u_sbox.config,
7981
#[cfg(any(crashdump, gdb))]
8082
&u_sbox.rt_cfg,
83+
u_sbox.tracker,
8184
)?;
8285
let outb_hdl = outb_handler_wrapper(hshm.clone(), u_sbox.host_funcs.clone());
8386

@@ -129,9 +132,12 @@ where
129132
pub(super) fn evolve_impl_multi_use(u_sbox: UninitializedSandbox) -> Result<MultiUseSandbox> {
130133
evolve_impl(
131134
u_sbox,
132-
|hf, mut hshm, vm, out_hdl, mem_hdl, dispatch_ptr| {
135+
|hf, mut hshm, mut vm, out_hdl, mem_hdl, dispatch_ptr| {
133136
{
134-
hshm.as_mut().push_state()?;
137+
let vm_dirty_pages = vm.get_and_clear_dirty_pages()?;
138+
let layout = hshm.unwrap_mgr().layout;
139+
hshm.as_mut()
140+
.create_initial_snapshot(Some(&vm_dirty_pages), &layout)?;
135141
}
136142
Ok(MultiUseSandbox::from_uninit(
137143
hf,
@@ -151,6 +157,7 @@ pub(crate) fn set_up_hypervisor_partition(
151157
mgr: &mut SandboxMemoryManager<GuestSharedMemory>,
152158
#[cfg_attr(target_os = "windows", allow(unused_variables))] config: &SandboxConfiguration,
153159
#[cfg(any(crashdump, gdb))] rt_cfg: &SandboxRuntimeConfig,
160+
tracker: Option<DirtyPageTracker>,
154161
) -> Result<Box<dyn Hypervisor>> {
155162
#[cfg(feature = "init-paging")]
156163
let rsp_ptr = {
@@ -163,6 +170,10 @@ pub(crate) fn set_up_hypervisor_partition(
163170
#[cfg(not(feature = "init-paging"))]
164171
let rsp_ptr = GuestPtr::try_from(Offset::from(0))?;
165172
let regions = mgr.layout.get_memory_regions(&mgr.shared_mem)?;
173+
174+
let tracker = tracker.ok_or_else(|| new_error!("No Dirty page tracker found"))?;
175+
mgr.shared_mem
176+
.with_exclusivity(|e| e.stop_tracking_dirty_pages(tracker))??;
166177
let base_ptr = GuestPtr::try_from(Offset::from(0))?;
167178
let pml4_ptr = {
168179
let pml4_offset_u64 = u64::try_from(SandboxMemoryLayout::PML4_OFFSET)?;

0 commit comments

Comments
 (0)