Skip to content

Commit 7fc2d80

Browse files
committed
SandboxMemoryLayout: do not pass heap and scratch size explicitly
These values could conflict with those in the SandboxConfiguration also passed in. Signed-off-by: Lucy Menon <168595099+syntactically@users.noreply.github.com>
1 parent 5cc18c1 commit 7fc2d80

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

src/hyperlight_host/src/mem/layout.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,22 @@ impl SandboxMemoryLayout {
200200
pub(crate) fn new(
201201
cfg: SandboxConfiguration,
202202
code_size: usize,
203-
heap_size: usize,
204-
scratch_size: usize,
205203
init_data_size: usize,
206204
init_data_permissions: Option<MemoryRegionFlags>,
207205
) -> Result<Self> {
206+
let heap_size = usize::try_from(cfg.get_heap_size())?;
207+
let scratch_size = cfg.get_scratch_size();
208+
if scratch_size > Self::MAX_MEMORY_SIZE {
209+
return Err(MemoryRequestTooBig(scratch_size, Self::MAX_MEMORY_SIZE));
210+
}
211+
let min_scratch_size = hyperlight_common::layout::min_scratch_size(
212+
cfg.get_input_data_size(),
213+
cfg.get_output_data_size(),
214+
);
215+
if scratch_size < min_scratch_size {
216+
return Err(MemoryRequestTooSmall(scratch_size, min_scratch_size));
217+
}
218+
208219
let guest_code_offset = 0;
209220
// The following offsets are to the fields of the PEB struct itself!
210221
let peb_offset = code_size.next_multiple_of(PAGE_SIZE_USIZE);
@@ -655,8 +666,7 @@ mod tests {
655666
#[test]
656667
fn test_get_memory_size() {
657668
let sbox_cfg = SandboxConfiguration::default();
658-
let sbox_mem_layout =
659-
SandboxMemoryLayout::new(sbox_cfg, 4096, 4096, 0x3000, 0, None).unwrap();
669+
let sbox_mem_layout = SandboxMemoryLayout::new(sbox_cfg, 4096, 0, None).unwrap();
660670
assert_eq!(
661671
sbox_mem_layout.get_memory_size().unwrap(),
662672
get_expected_memory_size(&sbox_mem_layout)
@@ -667,8 +677,7 @@ mod tests {
667677
fn test_max_memory_sandbox() {
668678
let mut cfg = SandboxConfiguration::default();
669679
cfg.set_input_data_size(0x40000000);
670-
let layout = SandboxMemoryLayout::new(cfg, 4096, 2048, 4096, 4096, None).unwrap();
671-
let result = layout.get_memory_size();
672-
assert!(matches!(result.unwrap_err(), MemoryRequestTooBig(..)));
680+
let layout = SandboxMemoryLayout::new(cfg, 4096, 4096, None);
681+
assert!(matches!(layout.unwrap_err(), MemoryRequestTooBig(..)));
673682
}
674683
}

src/hyperlight_host/src/sandbox/snapshot.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,6 @@ impl Snapshot {
359359
let mut layout = crate::mem::layout::SandboxMemoryLayout::new(
360360
cfg,
361361
exe_info.loaded_size(),
362-
usize::try_from(cfg.get_heap_size())?,
363-
cfg.get_scratch_size(),
364362
guest_blob_size,
365363
guest_blob_mem_flags,
366364
)?;
@@ -612,9 +610,8 @@ mod tests {
612610
let mut snapshot_mem = ExclusiveSharedMemory::new(PAGE_SIZE + pt_bytes.len()).unwrap();
613611

614612
snapshot_mem.copy_from_slice(&pt_bytes, PAGE_SIZE).unwrap();
615-
let cfg = crate::sandbox::SandboxConfiguration::default();
616613
let mgr = SandboxMemoryManager::new(
617-
SandboxMemoryLayout::new(cfg, 4096, 2048, 4096, 0x3000, None).unwrap(),
614+
SandboxMemoryLayout::new(cfg, 4096, 0x3000, None).unwrap(),
618615
snapshot_mem,
619616
scratch_mem,
620617
0.into(),

0 commit comments

Comments
 (0)