Skip to content

Commit 28cbf38

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 f7b5b6b commit 28cbf38

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/hyperlight_host/src/func/guest_dispatch.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,4 +480,36 @@ 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!(
510+
"Expected HyperlightError::GuestExecutionError but got {:?}",
511+
e
512+
),
513+
}
514+
}
483515
}

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

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

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

642649
fn add_to_static(function_call: &FunctionCall) -> Result<Vec<u8>> {
@@ -1094,6 +1101,14 @@ pub extern "C" fn hyperlight_main() {
10941101
add as i64,
10951102
);
10961103
register_function(add_def);
1104+
1105+
let trigger_exception_def = GuestFunctionDefinition::new(
1106+
"TriggerException".to_string(),
1107+
Vec::new(),
1108+
ReturnType::Void,
1109+
trigger_exception as i64,
1110+
);
1111+
register_function(trigger_exception_def);
10971112
}
10981113

10991114
#[no_mangle]

0 commit comments

Comments
 (0)