Skip to content

Commit 820d60c

Browse files
committed
Move PEB setup to snapshot creation
For historical reasons, PEB setup still happened when an uninitialised sandbox was created, instead of happening at snapshot time, which is far more sensible. This removes the need for the snapshot memory to be writable during initialisation. Signed-off-by: Lucy Menon <168595099+syntactically@users.noreply.github.com>
1 parent cb17894 commit 820d60c

File tree

6 files changed

+34
-62
lines changed

6 files changed

+34
-62
lines changed

src/hyperlight_host/src/error.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ pub enum HyperlightError {
104104
#[error("Unsupported type: {0}")]
105105
GuestInterfaceUnsupportedType(String),
106106

107-
/// The guest offset is invalid.
108-
#[error("The guest offset {0} is invalid.")]
109-
GuestOffsetIsInvalid(usize),
110-
111107
/// The guest binary was built with a different hyperlight-guest-bin version than the host expects.
112108
/// Hyperlight currently provides no backwards compatibility guarantees for guest binaries,
113109
/// so the guest and host versions must match exactly. This might change in the future.
@@ -369,7 +365,6 @@ impl HyperlightError {
369365
| HyperlightError::GuestExecutionHungOnHostFunctionCall()
370366
| HyperlightError::GuestFunctionCallAlreadyInProgress()
371367
| HyperlightError::GuestInterfaceUnsupportedType(_)
372-
| HyperlightError::GuestOffsetIsInvalid(_)
373368
| HyperlightError::HostFunctionNotFound(_)
374369
| HyperlightError::HyperlightVmError(HyperlightVmError::Create(_))
375370
| HyperlightError::HyperlightVmError(HyperlightVmError::Initialize(_))

src/hyperlight_host/src/mem/layout.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ use super::memory_region::{
7272
MemoryRegionVecBuilder,
7373
};
7474
use super::shared_mem::{ExclusiveSharedMemory, SharedMemory};
75-
use crate::error::HyperlightError::{
76-
GuestOffsetIsInvalid, MemoryRequestTooBig, MemoryRequestTooSmall,
77-
};
75+
use crate::error::HyperlightError::{MemoryRequestTooBig, MemoryRequestTooSmall};
7876
use crate::sandbox::SandboxConfiguration;
7977
use crate::{Result, new_error};
8078

@@ -568,68 +566,70 @@ impl SandboxMemoryLayout {
568566
/// Note: `shared_mem` may have been modified, even if `Err` was returned
569567
/// from this function.
570568
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
571-
pub(crate) fn write(
572-
&self,
573-
shared_mem: &mut ExclusiveSharedMemory,
574-
guest_offset: usize,
575-
//TODO: Unused remove
576-
_size: usize,
577-
) -> Result<()> {
569+
pub(crate) fn write_peb(&self, mem: &mut [u8]) -> Result<()> {
570+
let guest_offset = SandboxMemoryLayout::BASE_ADDRESS;
571+
572+
fn write_u64(mem: &mut [u8], offset: usize, value: u64) -> Result<()> {
573+
if offset + 8 > mem.len() {
574+
return Err(new_error!(
575+
"Cannot write to offset {} in slice of len {}",
576+
offset,
577+
mem.len()
578+
));
579+
}
580+
mem[offset..offset + 8].copy_from_slice(&u64::to_ne_bytes(value));
581+
Ok(())
582+
}
583+
578584
macro_rules! get_address {
579585
($something:ident) => {
580586
u64::try_from(guest_offset + self.$something)?
581587
};
582588
}
583589

584-
if guest_offset != SandboxMemoryLayout::BASE_ADDRESS
585-
&& guest_offset != shared_mem.base_addr()
586-
{
587-
return Err(GuestOffsetIsInvalid(guest_offset));
588-
}
589-
590590
// Start of setting up the PEB. The following are in the order of the PEB fields
591591

592-
// Skip guest_dispatch_function_ptr_offset because it is set by the guest
593-
594-
// Skip code, is set when loading binary
595-
// skip outb and outb context, is set when running in_proc
596-
597592
// Set up input buffer pointer
598-
shared_mem.write_u64(
593+
write_u64(
594+
mem,
599595
self.get_input_data_size_offset(),
600596
self.sandbox_memory_config
601597
.get_input_data_size()
602598
.try_into()?,
603599
)?;
604-
shared_mem.write_u64(
600+
write_u64(
601+
mem,
605602
self.get_input_data_pointer_offset(),
606603
self.get_input_data_buffer_gva(),
607604
)?;
608605

609606
// Set up output buffer pointer
610-
shared_mem.write_u64(
607+
write_u64(
608+
mem,
611609
self.get_output_data_size_offset(),
612610
self.sandbox_memory_config
613611
.get_output_data_size()
614612
.try_into()?,
615613
)?;
616-
shared_mem.write_u64(
614+
write_u64(
615+
mem,
617616
self.get_output_data_pointer_offset(),
618617
self.get_output_data_buffer_gva(),
619618
)?;
620619

621620
// Set up init data pointer
622-
shared_mem.write_u64(
621+
write_u64(
622+
mem,
623623
self.get_init_data_size_offset(),
624624
(self.get_unaligned_memory_size() - self.init_data_offset).try_into()?,
625625
)?;
626626
let addr = get_address!(init_data_offset);
627-
shared_mem.write_u64(self.get_init_data_pointer_offset(), addr)?;
627+
write_u64(mem, self.get_init_data_pointer_offset(), addr)?;
628628

629629
// Set up heap buffer pointer
630630
let addr = get_address!(guest_heap_buffer_offset);
631-
shared_mem.write_u64(self.get_heap_size_offset(), self.heap_size.try_into()?)?;
632-
shared_mem.write_u64(self.get_heap_pointer_offset(), addr)?;
631+
write_u64(mem, self.get_heap_size_offset(), self.heap_size.try_into()?)?;
632+
write_u64(mem, self.get_heap_pointer_offset(), addr)?;
633633

634634
// Set up the file_mappings descriptor in the PEB.
635635
// - The `size` field holds the number of valid FileMappingInfo

src/hyperlight_host/src/mem/mgr.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -302,17 +302,6 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
302302
Ok(Self::new(layout, shared_mem, scratch_mem, entrypoint))
303303
}
304304

305-
/// Write memory layout
306-
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
307-
pub(crate) fn write_memory_layout(&mut self) -> Result<()> {
308-
let mem_size = self.shared_mem.mem_size();
309-
self.layout.write(
310-
&mut self.shared_mem,
311-
SandboxMemoryLayout::BASE_ADDRESS,
312-
mem_size,
313-
)
314-
}
315-
316305
/// Wraps ExclusiveSharedMemory::build
317306
// Morally, this should not have to be a Result: this operation is
318307
// infallible. The source of the Result is

src/hyperlight_host/src/sandbox/outb.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,7 @@ mod tests {
274274
let new_mgr = || {
275275
let bin = GuestBinary::FilePath(simple_guest_as_string().unwrap());
276276
let snapshot = crate::sandbox::snapshot::Snapshot::from_env(bin, sandbox_cfg).unwrap();
277-
let mut mgr = SandboxMemoryManager::from_snapshot(&snapshot).unwrap();
278-
let mem_size = mgr.get_shared_mem_mut().mem_size();
279-
let layout = mgr.layout;
280-
let shared_mem = mgr.get_shared_mem_mut();
281-
layout
282-
.write(shared_mem, SandboxMemoryLayout::BASE_ADDRESS, mem_size)
283-
.unwrap();
277+
let mgr = SandboxMemoryManager::from_snapshot(&snapshot).unwrap();
284278
let (hmgr, _) = mgr.build().unwrap();
285279
hmgr
286280
};
@@ -386,13 +380,7 @@ mod tests {
386380
let bin = GuestBinary::FilePath(simple_guest_as_string().unwrap());
387381
let snapshot =
388382
crate::sandbox::snapshot::Snapshot::from_env(bin, sandbox_cfg).unwrap();
389-
let mut mgr = SandboxMemoryManager::from_snapshot(&snapshot).unwrap();
390-
let mem_size = mgr.get_shared_mem_mut().mem_size();
391-
let layout = mgr.layout;
392-
let shared_mem = mgr.get_shared_mem_mut();
393-
layout
394-
.write(shared_mem, SandboxMemoryLayout::BASE_ADDRESS, mem_size)
395-
.unwrap();
383+
let mgr = SandboxMemoryManager::from_snapshot(&snapshot).unwrap();
396384
let (hmgr, _) = mgr.build().unwrap();
397385
hmgr
398386
};

src/hyperlight_host/src/sandbox/snapshot.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ impl Snapshot {
382382
&mut memory[layout.get_guest_code_offset()..],
383383
)?;
384384

385+
layout.write_peb(&mut memory)?;
386+
385387
blob.map(|x| layout.write_init_data(&mut memory, x.data))
386388
.transpose()?;
387389

src/hyperlight_host/src/sandbox/uninitialized.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,9 @@ impl UninitializedSandbox {
362362
}
363363
};
364364

365-
let mut mem_mgr_wrapper =
365+
let mem_mgr_wrapper =
366366
SandboxMemoryManager::<ExclusiveSharedMemory>::from_snapshot(snapshot.as_ref())?;
367367

368-
mem_mgr_wrapper.write_memory_layout()?;
369-
370368
let host_funcs = Arc::new(Mutex::new(FunctionRegistry::default()));
371369

372370
let mut sandbox = Self {

0 commit comments

Comments
 (0)