Skip to content

Commit 8eafa0d

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 a128a08 commit 8eafa0d

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

src/hyperlight_host/src/func/guest_dispatch.rs

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

src/hyperlight_host/tests/integration_test.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
1818
use hyperlight_common::mem::PAGE_SIZE;
1919
use hyperlight_host::func::{ParameterValue, ReturnType, ReturnValue};
20-
#[cfg(not(feature = "executable_heap"))]
21-
use hyperlight_host::mem::memory_region::MemoryRegionFlags;
2220
use hyperlight_host::sandbox::SandboxConfiguration;
2321
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2422
use hyperlight_host::sandbox_state::transition::Noop;
@@ -388,7 +386,7 @@ fn execute_on_stack() {
388386
.call_guest_function_by_name("ExecuteOnStack", ReturnType::String, Some(vec![]))
389387
.unwrap_err();
390388

391-
// TODO: because we set the stack as NX in the guest PTE we get a generic error, once we handle the exception correctly in the guest we can make this more specific
389+
#[cfg(inprocess)]
392390
if let HyperlightError::Error(message) = result {
393391
cfg_if::cfg_if! {
394392
if #[cfg(target_os = "linux")] {
@@ -399,8 +397,15 @@ fn execute_on_stack() {
399397
panic!("Unexpected");
400398
}
401399
}
402-
} else {
403-
panic!("Unexpected error type");
400+
}
401+
402+
#[cfg(not(inprocess))]
403+
{
404+
let err = result.to_string();
405+
assert!(
406+
// exception that indicates a page fault
407+
err.contains("EXCEPTION: 0xe")
408+
);
404409
}
405410
}
406411

@@ -419,13 +424,8 @@ fn execute_on_heap() {
419424
{
420425
assert!(result.is_err());
421426
let err = result.unwrap_err();
422-
assert!(
423-
matches!(
424-
err,
425-
HyperlightError::MemoryAccessViolation(_, MemoryRegionFlags::EXECUTE, _)
426-
) || matches!(err, HyperlightError::Error(ref s) if s.starts_with("Unexpected VM Exit"))
427-
|| matches!(err, HyperlightError::Error(ref s) if s.starts_with("unknown Hyper-V run message type")) // Because the memory is set as NX in the guest PTE we get a generic error, once we handle the exception correctly in the guest we can make this more specific
428-
);
427+
428+
assert!(err.to_string().contains("EXCEPTION: 0xe"));
429429
}
430430
}
431431

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

+15
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)