Skip to content

Commit 918bc74

Browse files
committed
[test] added test for exception handling
- added new guest fxn to simpleguest that triggers an invalid opcode exception - added test in guest_dispatch that calls the new guest fxn to test exception handling Signed-off-by: danbugs <[email protected]>
1 parent b7d8b2b commit 918bc74

File tree

4 files changed

+65
-23
lines changed

4 files changed

+65
-23
lines changed

src/hyperlight_host/src/func/guest_dispatch.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ mod tests {
160160
None,
161161
None,
162162
)
163-
.unwrap();
163+
.unwrap();
164164

165165
make_get_pid_syscall_func.register(&mut usbox, "MakeGetpidSyscall")?;
166166

@@ -196,7 +196,7 @@ mod tests {
196196
None,
197197
None,
198198
)
199-
.unwrap();
199+
.unwrap();
200200

201201
make_get_pid_syscall_func.register_with_extra_allowed_syscalls(
202202
&mut usbox,
@@ -228,7 +228,7 @@ mod tests {
228228
None,
229229
None,
230230
)
231-
.unwrap()
231+
.unwrap()
232232
};
233233

234234
// test_function0
@@ -345,7 +345,7 @@ mod tests {
345345
// just use the built-in host print function
346346
None,
347347
)
348-
.unwrap();
348+
.unwrap();
349349
test_call_guest_function_by_name(u_sbox);
350350
}
351351

@@ -365,7 +365,7 @@ mod tests {
365365
Some(crate::SandboxRunOptions::RunInProcess(true)),
366366
None,
367367
)
368-
.unwrap();
368+
.unwrap();
369369
test_call_guest_function_by_name(u_sbox);
370370
}
371371

@@ -378,7 +378,7 @@ mod tests {
378378
Some(crate::SandboxRunOptions::RunInProcess(false)),
379379
None,
380380
)
381-
.unwrap();
381+
.unwrap();
382382
test_call_guest_function_by_name(u_sbox);
383383
}
384384

@@ -444,7 +444,7 @@ mod tests {
444444
None,
445445
None,
446446
)
447-
.unwrap();
447+
.unwrap();
448448

449449
// Make this host call run for 5 seconds
450450

@@ -480,4 +480,33 @@ mod tests {
480480
),
481481
}
482482
}
483+
484+
#[test]
485+
fn test_trigger_exception_on_guest() {
486+
let usbox = UninitializedSandbox::new(
487+
GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")),
488+
None,
489+
None,
490+
None,
491+
)
492+
.unwrap();
493+
494+
let mut multi_use_sandbox: MultiUseSandbox = usbox.evolve(Noop::default()).unwrap();
495+
496+
let res = multi_use_sandbox.call_guest_function_by_name(
497+
"TriggerException",
498+
ReturnType::Void,
499+
None,
500+
);
501+
502+
assert!(res.is_err());
503+
504+
match res.unwrap_err() {
505+
HyperlightError::GuestAborted(_, msg) => {
506+
// msg should indicate we got an invalid opcode exception
507+
assert!(msg.contains("EXCEPTION: 0x6"));
508+
}
509+
e => panic!("Expected HyperlightError::GuestExecutionError but got {:?}", e),
510+
}
511+
}
483512
}

src/tests/rust_guests/callbackguest/Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tests/rust_guests/simpleguest/Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tests/rust_guests/simpleguest/src/main.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,11 @@ fn log_message(function_call: &FunctionCall) -> Result<Vec<u8>> {
637637
}
638638
}
639639

640+
fn trigger_exception(_: &FunctionCall) -> Result<Vec<u8>> {
641+
unsafe { core::arch::asm!("ud2"); } // trigger an undefined instruction exception
642+
Ok(get_flatbuffer_result_from_void())
643+
}
644+
640645
static mut COUNTER: i32 = 0;
641646

642647
fn add_to_static(function_call: &FunctionCall) -> Result<Vec<u8>> {
@@ -1094,6 +1099,14 @@ pub extern "C" fn hyperlight_main() {
10941099
add as i64,
10951100
);
10961101
register_function(add_def);
1102+
1103+
let trigger_exception_def = GuestFunctionDefinition::new(
1104+
"TriggerException".to_string(),
1105+
Vec::new(),
1106+
ReturnType::Void,
1107+
trigger_exception as i64,
1108+
);
1109+
register_function(trigger_exception_def);
10971110
}
10981111

10991112
#[no_mangle]

0 commit comments

Comments
 (0)