Skip to content

Commit 7d903d0

Browse files
committed
fix: size foundation memory tests
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
1 parent 70de7f8 commit 7d903d0

4 files changed

Lines changed: 38 additions & 27 deletions

File tree

src/hyperlight_host/src/sandbox/initialized_multi_use.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,15 +1406,12 @@ mod tests {
14061406
assert_eq!(res, 0);
14071407
}
14081408

1409-
// Tests to ensure that many (1000) function calls can be made in a call context with a small stack (24K) and heap(32K).
1410-
// This test effectively ensures that the stack is being properly reset after each call and we are not leaking memory in the Guest.
1409+
// Checks that 1,000 calls work with constrained guest memory.
1410+
// This catches guest stack reset and heap leaks.
14111411
#[test]
14121412
fn test_with_small_stack_and_heap() {
1413-
const HEAP_SIZE: u64 = 32 * 1024;
1414-
// min_scratch_size already includes 1 page (4k on most
1415-
// platforms) of guest stack, so add 20k more to get 24k
1416-
// total, and then add some more for the eagerly-copied page
1417-
// tables on amd64
1413+
const HEAP_SIZE: u64 = 128 * 1024;
1414+
// Leave headroom for legacy transport and eagerly copied page tables.
14181415
let scratch_size = {
14191416
let defaults = SandboxConfiguration::default();
14201417
hyperlight_common::layout::min_scratch_size(
@@ -1425,8 +1422,7 @@ mod tests {
14251422
defaults.get_g2h_pool_pages(),
14261423
defaults.get_h2g_pool_pages(),
14271424
)
1428-
} + 0x10000
1429-
+ 0x10000;
1425+
} + 0x40000;
14301426

14311427
let mut sbox1 = SandboxBuilder::from_file(simple_guest_as_pathbuf())
14321428
.heap_size(HEAP_SIZE)
@@ -2147,7 +2143,7 @@ mod tests {
21472143
#[test]
21482144
fn snapshot_restore_recovers_oom_with_larger_heap() {
21492145
let mut source_cfg = SandboxConfiguration::default();
2150-
source_cfg.set_heap_size(0x20_000);
2146+
source_cfg.set_heap_size(0x40_000);
21512147
let path = simple_guest_as_pathbuf();
21522148
let mut source = UninitializedSandbox::new(GuestBinary::FilePath(path), Some(source_cfg))
21532149
.unwrap()
@@ -2156,7 +2152,7 @@ mod tests {
21562152
let snapshot = source.snapshot().unwrap();
21572153

21582154
let mut target_cfg = SandboxConfiguration::default();
2159-
target_cfg.set_heap_size(0x8000);
2155+
target_cfg.set_heap_size(0x20_000);
21602156
let path = simple_guest_as_pathbuf();
21612157
let mut target = UninitializedSandbox::new(GuestBinary::FilePath(path), Some(target_cfg))
21622158
.unwrap()
@@ -2177,7 +2173,7 @@ mod tests {
21772173
#[test]
21782174
fn snapshot_restore_applies_smaller_heap_limit() {
21792175
let mut source_cfg = SandboxConfiguration::default();
2180-
source_cfg.set_heap_size(0x8000);
2176+
source_cfg.set_heap_size(0x20_000);
21812177
let path = simple_guest_as_pathbuf();
21822178
let mut source = UninitializedSandbox::new(GuestBinary::FilePath(path), Some(source_cfg))
21832179
.unwrap()
@@ -2186,26 +2182,28 @@ mod tests {
21862182
let snapshot = source.snapshot().unwrap();
21872183

21882184
let mut target_cfg = SandboxConfiguration::default();
2189-
target_cfg.set_heap_size(0x20_000);
2185+
target_cfg.set_heap_size(0x80_000);
21902186
let path = simple_guest_as_pathbuf();
21912187
let mut target = UninitializedSandbox::new(GuestBinary::FilePath(path), Some(target_cfg))
21922188
.unwrap()
21932189
.evolve()
21942190
.unwrap();
21952191

21962192
assert_eq!(
2197-
target.call::<i32>("CallMalloc", 0x10_000i32).unwrap(),
2198-
0x10_000
2193+
target.call::<i32>("CallMalloc", 0x30_000i32).unwrap(),
2194+
0x30_000
21992195
);
22002196
target.restore(snapshot).unwrap();
2201-
assert_eq!(target.mem_mgr.layout.heap_size(), 0x8000);
2202-
assert!(target.call::<i32>("CallMalloc", 0x10_000i32).is_err());
2197+
assert_eq!(target.mem_mgr.layout.heap_size(), 0x20_000);
2198+
assert!(target.call::<i32>("CallMalloc", 0x30_000i32).is_err());
22032199
assert!(target.status().is_poisoned());
22042200
}
22052201

22062202
#[test]
22072203
fn snapshot_restore_applies_smaller_io_limits() {
22082204
let mut source_cfg = SandboxConfiguration::default();
2205+
source_cfg.set_heap_size(0x40_000);
2206+
source_cfg.set_scratch_size(SandboxConfiguration::DEFAULT_SCRATCH_SIZE + 256 * 1024);
22092207
source_cfg.set_input_data_size(0x2000);
22102208
source_cfg.set_output_data_size(0x2000);
22112209
let path = simple_guest_as_pathbuf();
@@ -2216,6 +2214,8 @@ mod tests {
22162214
let snapshot = source.snapshot().unwrap();
22172215

22182216
let mut target_cfg = SandboxConfiguration::default();
2217+
target_cfg.set_heap_size(0x40_000);
2218+
target_cfg.set_scratch_size(SandboxConfiguration::DEFAULT_SCRATCH_SIZE + 256 * 1024);
22192219
target_cfg.set_input_data_size(0x8000);
22202220
target_cfg.set_output_data_size(0x8000);
22212221
let path = simple_guest_as_pathbuf();
@@ -2242,7 +2242,7 @@ mod tests {
22422242
let mut small_cfg = SandboxConfiguration::default();
22432243
small_cfg.set_input_data_size(0x2000);
22442244
small_cfg.set_output_data_size(0x2000);
2245-
small_cfg.set_heap_size(0x8000);
2245+
small_cfg.set_heap_size(0x20_000);
22462246
let path = simple_guest_as_pathbuf();
22472247
let mut small = UninitializedSandbox::new(GuestBinary::FilePath(path), Some(small_cfg))
22482248
.unwrap()
@@ -2272,15 +2272,15 @@ mod tests {
22722272

22732273
target.restore(small_snapshot.clone()).unwrap();
22742274
assert_eq!(target.call::<i32>("GetStatic", ()).unwrap(), 11);
2275-
assert_eq!(target.mem_mgr.layout.heap_size(), 0x8000);
2275+
assert_eq!(target.mem_mgr.layout.heap_size(), 0x20_000);
22762276

22772277
target.restore(large_snapshot).unwrap();
22782278
assert_eq!(target.call::<i32>("GetStatic", ()).unwrap(), 22);
22792279
assert_eq!(target.mem_mgr.layout.heap_size(), 0x40_000);
22802280

22812281
target.restore(small_snapshot).unwrap();
22822282
assert_eq!(target.call::<i32>("GetStatic", ()).unwrap(), 11);
2283-
assert_eq!(target.mem_mgr.layout.heap_size(), 0x8000);
2283+
assert_eq!(target.mem_mgr.layout.heap_size(), 0x20_000);
22842284
}
22852285

22862286
#[test]

src/hyperlight_host/src/sandbox/snapshot/file_tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2781,7 +2781,9 @@ fn round_trip_preserves_stack_top_gva() {
27812781

27822782
#[test]
27832783
fn round_trip_preserves_non_default_scratch_size() {
2784-
let custom_scratch: usize = 256 * 1024;
2784+
use crate::sandbox::SandboxConfiguration;
2785+
2786+
let custom_scratch = SandboxConfiguration::DEFAULT_SCRATCH_SIZE + 64 * 1024;
27852787
let mut sbox = SandboxBuilder::from_file(simple_guest_as_pathbuf())
27862788
.scratch_size(custom_scratch)
27872789
.build()

src/hyperlight_host/src/sandbox/uninitialized.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,7 @@ mod tests {
11631163
{
11641164
let mut cfg = SandboxConfiguration::default();
11651165
cfg.set_heap_size(16 * 1024 * 1024); // 16MB heap
1166+
cfg.set_scratch_size(SandboxConfiguration::DEFAULT_SCRATCH_SIZE + 256 * 1024);
11661167

11671168
let env = GuestEnvironment::new(GuestBinary::FilePath(binary_path.clone()), None);
11681169

@@ -1185,7 +1186,7 @@ mod tests {
11851186
// Test 3: Create snapshot with custom scratch size
11861187
{
11871188
let mut cfg = SandboxConfiguration::default();
1188-
cfg.set_scratch_size(256 * 1024); // 256KB scratch
1189+
cfg.set_scratch_size(SandboxConfiguration::DEFAULT_SCRATCH_SIZE + 64 * 1024);
11891190

11901191
let env = GuestEnvironment::new(GuestBinary::FilePath(binary_path.clone()), None);
11911192

@@ -1208,6 +1209,7 @@ mod tests {
12081209
// Test 4: Create snapshot with custom input/output buffer sizes
12091210
{
12101211
let mut cfg = SandboxConfiguration::default();
1212+
cfg.set_scratch_size(SandboxConfiguration::DEFAULT_SCRATCH_SIZE + 128 * 1024);
12111213
cfg.set_input_data_size(64 * 1024); // 64KB input
12121214
cfg.set_output_data_size(64 * 1024); // 64KB output
12131215

@@ -1233,7 +1235,7 @@ mod tests {
12331235
{
12341236
let mut cfg = SandboxConfiguration::default();
12351237
cfg.set_heap_size(32 * 1024 * 1024); // 32MB heap
1236-
cfg.set_scratch_size(256 * 1024 * 2); // 512KB scratch (256KB will be input/output)
1238+
cfg.set_scratch_size(SandboxConfiguration::DEFAULT_SCRATCH_SIZE + 1024 * 1024);
12371239
cfg.set_input_data_size(128 * 1024); // 128KB input
12381240
cfg.set_output_data_size(128 * 1024); // 128KB output
12391241

src/hyperlight_host/tests/integration_test.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,8 @@ fn guest_malloc_abort() {
523523
});
524524

525525
// allocate a vector (on heap) that is bigger than the heap
526-
let heap_size = 0x8000;
527-
let size_to_allocate = 0x10000;
526+
let heap_size = 128 * 1024;
527+
let size_to_allocate = 256 * 1024;
528528
assert!(
529529
size_to_allocate > heap_size,
530530
"precondition: size_to_allocate ({size_to_allocate}) must be > heap_size ({heap_size})"
@@ -601,7 +601,7 @@ fn corrupt_output_back_pointer_rejected() {
601601

602602
#[test]
603603
fn guest_panic_no_alloc() {
604-
let heap_size = 0x8000;
604+
let heap_size = 128 * 1024;
605605

606606
let configure = |builder: SandboxBuilder| builder.heap_size(heap_size);
607607
with_rust_sandbox_from(configure, |mut sbox| {
@@ -612,10 +612,15 @@ fn guest_panic_no_alloc() {
612612
)
613613
.unwrap_err();
614614

615+
// Legacy transport may report its own allocation failure.
615616
assert!(
616617
matches!(
617618
&res,
618-
HyperlightError::GuestAborted(code, msg) if *code == ErrorCode::UnknownError as u8 && msg.contains("memory allocation of ") && msg.contains("bytes failed")
619+
HyperlightError::GuestAborted(code, msg)
620+
if (*code == ErrorCode::UnknownError as u8
621+
&& msg.contains("memory allocation of ")
622+
&& msg.contains("bytes failed"))
623+
|| *code == ErrorCode::MallocFailed as u8
619624
),
620625
"unexpected error: {res:?}"
621626
);
@@ -1664,6 +1669,8 @@ fn fill_heap_and_cause_exception() {
16641669

16651670
let err = result.unwrap_err();
16661671
match &err {
1672+
// Legacy transport may report its own allocation failure.
1673+
HyperlightError::GuestAborted(code, _) if *code == ErrorCode::MallocFailed as u8 => {}
16671674
HyperlightError::GuestAborted(code, message) => {
16681675
assert_eq!(*code, ErrorCode::GuestError as u8, "Full error: {:?}", err);
16691676

0 commit comments

Comments
 (0)