Skip to content

Commit 54cd065

Browse files
committed
fixup! Modify guest physical page allocator to allocate from the scratch region
Signed-off-by: Lucy Menon <168595099+syntactically@users.noreply.github.com>
1 parent 2a2b444 commit 54cd065

6 files changed

Lines changed: 31 additions & 31 deletions

File tree

src/hyperlight_host/src/hypervisor/gdb/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ mod tests {
497497
.inspect_err(|_| unsafe {
498498
libc::munmap(mapped_mem, size);
499499
})?;
500-
let (mem_mgr, _) = sandbox.mgr.build();
500+
let (mem_mgr, _) = sandbox.mgr.build()?;
501501

502502
// Create the memory access struct
503503
let mem_access = DebugMemoryAccess {

src/hyperlight_host/src/hypervisor/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ pub(crate) mod tests {
525525
let rt_cfg: SandboxRuntimeConfig = Default::default();
526526
let sandbox =
527527
UninitializedSandbox::new(GuestBinary::FilePath(filename.clone()), Some(config))?;
528-
let (mut mem_mgr, gshm) = sandbox.mgr.build();
528+
let (mut mem_mgr, gshm) = sandbox.mgr.build().unwrap();
529529
let mut vm = set_up_hypervisor_partition(
530530
gshm,
531531
&config,

src/hyperlight_host/src/mem/mgr.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,21 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
235235
}
236236

237237
/// Wraps ExclusiveSharedMemory::build
238+
// Morally, this should not have to be a Result: this operation is
239+
// infallible. The source of the Result is
240+
// update_scratch_bookkeeping(), which calls functions that can
241+
// fail due to bounds checks (which are statically known to be ok
242+
// in this situation) or due to failing to take the scratch shared
243+
// memory lock, but the scratch shared memory is built in this
244+
// function, its lock does not escape before the end of the
245+
// function, and the lock is taken by no other code path, so we
246+
// know it is not contended.
238247
pub fn build(
239248
self,
240-
) -> (
249+
) -> Result<(
241250
SandboxMemoryManager<HostSharedMemory>,
242251
SandboxMemoryManager<GuestSharedMemory>,
243-
) {
252+
)> {
244253
let (hshm, gshm) = self.shared_mem.build();
245254
let (hscratch, gscratch) = self.scratch_mem.build();
246255
let mut host_mgr = SandboxMemoryManager {
@@ -265,8 +274,8 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
265274
};
266275
host_mgr.update_scratch_bookkeeping(
267276
(SandboxMemoryLayout::BASE_ADDRESS + self.layout.get_pt_offset()) as u64,
268-
);
269-
(host_mgr, guest_mgr)
277+
)?;
278+
Ok((host_mgr, guest_mgr))
270279
}
271280
}
272281

@@ -424,39 +433,30 @@ impl SandboxMemoryManager<HostSharedMemory> {
424433

425434
Some(gscratch)
426435
};
427-
self.update_scratch_bookkeeping(snapshot.root_pt_gpa());
436+
self.update_scratch_bookkeeping(snapshot.root_pt_gpa())?;
428437
Ok((gsnapshot, gscratch))
429438
}
430439

431-
fn update_scratch_bookkeeping(&mut self, snapshot_pt_base_gpa: u64) {
440+
fn update_scratch_bookkeeping(&mut self, snapshot_pt_base_gpa: u64) -> Result<()> {
432441
let scratch_size = self.scratch_mem.mem_size();
442+
433443
let size_offset =
434444
scratch_size - hyperlight_common::layout::SCRATCH_TOP_SIZE_OFFSET as usize;
435-
// The only way that write can fail is if the offset is
436-
// outside of the memory, which would be sufficiently much of
437-
// an invariant violation that panicking is probably
438-
// sensible...
439-
#[allow(clippy::unwrap_used)]
440445
self.scratch_mem
441-
.write::<u64>(size_offset, scratch_size as u64)
442-
.unwrap();
446+
.write::<u64>(size_offset, scratch_size as u64)?;
447+
443448
let alloc_offset =
444449
scratch_size - hyperlight_common::layout::SCRATCH_TOP_ALLOCATOR_OFFSET as usize;
445-
// See above comment about unwrap() on write
446-
#[allow(clippy::unwrap_used)]
447-
self.scratch_mem
448-
.write::<u64>(
449-
alloc_offset,
450-
hyperlight_common::layout::scratch_base_gpa(scratch_size),
451-
)
452-
.unwrap();
450+
self.scratch_mem.write::<u64>(
451+
alloc_offset,
452+
hyperlight_common::layout::scratch_base_gpa(scratch_size),
453+
)?;
454+
453455
let snapshot_pt_base_gpa_offset = scratch_size
454456
- hyperlight_common::layout::SCRATCH_TOP_SNAPSHOT_PT_GPA_BASE_OFFSET as usize;
455-
// See above comment about unwrap() on write
456-
#[allow(clippy::unwrap_used)]
457457
self.scratch_mem
458-
.write::<u64>(snapshot_pt_base_gpa_offset, snapshot_pt_base_gpa)
459-
.unwrap();
458+
.write::<u64>(snapshot_pt_base_gpa_offset, snapshot_pt_base_gpa)?;
459+
Ok(())
460460
}
461461
}
462462

src/hyperlight_host/src/sandbox/outb.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ mod tests {
287287
layout
288288
.write(shared_mem, SandboxMemoryLayout::BASE_ADDRESS, mem_size)
289289
.unwrap();
290-
let (hmgr, _) = mgr.build();
290+
let (hmgr, _) = mgr.build().unwrap();
291291
hmgr
292292
};
293293
{
@@ -399,7 +399,7 @@ mod tests {
399399
layout
400400
.write(shared_mem, SandboxMemoryLayout::BASE_ADDRESS, mem_size)
401401
.unwrap();
402-
let (hmgr, _) = mgr.build();
402+
let (hmgr, _) = mgr.build().unwrap();
403403
hmgr
404404
};
405405

src/hyperlight_host/src/sandbox/snapshot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ mod tests {
613613
None,
614614
[0u8; 16],
615615
);
616-
let (mgr, _) = mgr.build();
616+
let (mgr, _) = mgr.build().unwrap();
617617
(mgr, pt_base as u64)
618618
}
619619

src/hyperlight_host/src/sandbox/uninitialized_evolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::{MultiUseSandbox, Result, UninitializedSandbox, new_error};
3939

4040
#[instrument(err(Debug), skip_all, parent = Span::current(), level = "Trace")]
4141
pub(super) fn evolve_impl_multi_use(u_sbox: UninitializedSandbox) -> Result<MultiUseSandbox> {
42-
let (mut hshm, gshm) = u_sbox.mgr.build();
42+
let (mut hshm, gshm) = u_sbox.mgr.build()?;
4343
let mut vm = set_up_hypervisor_partition(
4444
gshm,
4545
&u_sbox.config,

0 commit comments

Comments
 (0)