Skip to content

Commit e063a4b

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 2119779 commit e063a4b

6 files changed

Lines changed: 34 additions & 62 deletions

File tree

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

@@ -584,68 +582,70 @@ impl SandboxMemoryLayout {
584582
/// Note: `shared_mem` may have been modified, even if `Err` was returned
585583
/// from this function.
586584
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
587-
pub(crate) fn write(
588-
&self,
589-
shared_mem: &mut ExclusiveSharedMemory,
590-
guest_offset: usize,
591-
//TODO: Unused remove
592-
_size: usize,
593-
) -> Result<()> {
585+
pub(crate) fn write_peb(&self, mem: &mut [u8]) -> Result<()> {
586+
let guest_offset = SandboxMemoryLayout::BASE_ADDRESS;
587+
588+
fn write_u64(mem: &mut [u8], offset: usize, value: u64) -> Result<()> {
589+
if offset + 8 > mem.len() {
590+
return Err(new_error!(
591+
"Cannot write to offset {} in slice of len {}",
592+
offset,
593+
mem.len()
594+
));
595+
}
596+
mem[offset..offset + 8].copy_from_slice(&u64::to_ne_bytes(value));
597+
Ok(())
598+
}
599+
594600
macro_rules! get_address {
595601
($something:ident) => {
596602
u64::try_from(guest_offset + self.$something)?
597603
};
598604
}
599605

600-
if guest_offset != SandboxMemoryLayout::BASE_ADDRESS
601-
&& guest_offset != shared_mem.base_addr()
602-
{
603-
return Err(GuestOffsetIsInvalid(guest_offset));
604-
}
605-
606606
// Start of setting up the PEB. The following are in the order of the PEB fields
607607

608-
// Skip guest_dispatch_function_ptr_offset because it is set by the guest
609-
610-
// Skip code, is set when loading binary
611-
// skip outb and outb context, is set when running in_proc
612-
613608
// Set up input buffer pointer
614-
shared_mem.write_u64(
609+
write_u64(
610+
mem,
615611
self.get_input_data_size_offset(),
616612
self.sandbox_memory_config
617613
.get_input_data_size()
618614
.try_into()?,
619615
)?;
620-
shared_mem.write_u64(
616+
write_u64(
617+
mem,
621618
self.get_input_data_pointer_offset(),
622619
self.get_input_data_buffer_gva(),
623620
)?;
624621

625622
// Set up output buffer pointer
626-
shared_mem.write_u64(
623+
write_u64(
624+
mem,
627625
self.get_output_data_size_offset(),
628626
self.sandbox_memory_config
629627
.get_output_data_size()
630628
.try_into()?,
631629
)?;
632-
shared_mem.write_u64(
630+
write_u64(
631+
mem,
633632
self.get_output_data_pointer_offset(),
634633
self.get_output_data_buffer_gva(),
635634
)?;
636635

637636
// Set up init data pointer
638-
shared_mem.write_u64(
637+
write_u64(
638+
mem,
639639
self.get_init_data_size_offset(),
640640
(self.get_unaligned_memory_size() - self.init_data_offset).try_into()?,
641641
)?;
642642
let addr = get_address!(init_data_offset);
643-
shared_mem.write_u64(self.get_init_data_pointer_offset(), addr)?;
643+
write_u64(mem, self.get_init_data_pointer_offset(), addr)?;
644644

645645
// Set up heap buffer pointer
646646
let addr = get_address!(guest_heap_buffer_offset);
647-
shared_mem.write_u64(self.get_heap_size_offset(), self.heap_size.try_into()?)?;
648-
shared_mem.write_u64(self.get_heap_pointer_offset(), addr)?;
647+
write_u64(mem, self.get_heap_size_offset(), self.heap_size.try_into()?)?;
648+
write_u64(mem, self.get_heap_pointer_offset(), addr)?;
649649

650650
// Set up the file_mappings descriptor in the PEB.
651651
// - 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
@@ -303,17 +303,6 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
303303
Ok(Self::new(layout, shared_mem, scratch_mem, entrypoint))
304304
}
305305

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