Skip to content

Commit cefb1ac

Browse files
committed
test: cover host bindgen guest panic
Signed-off-by: James Sturtevant <jsturtevant@gmail.com>
1 parent 498499a commit cefb1ac

3 files changed

Lines changed: 20 additions & 30 deletions

File tree

src/hyperlight_host/tests/wit_test.rs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,13 @@ fn sb() -> TestSandbox<Host, MultiUseSandbox> {
293293

294294
mod wit_test {
295295

296-
use std::sync::{Arc, Mutex};
297-
298296
use hyperlight_host::error::HyperlightError;
299-
use hyperlight_host::func::{ParameterTuple, SupportedReturnType};
300-
use hyperlight_host::sandbox::Callable;
301297
use proptest::prelude::*;
302298

303299
use crate::bindings::test::wit::{
304-
RoundtripExports, TestExports, TestHostResourceExports, roundtrip,
300+
FailableExports, RoundtripExports, TestExports, TestHostResourceExports, roundtrip,
305301
};
306-
use crate::{TestResources, TestSandbox, sb};
302+
use crate::sb;
307303

308304
prop_compose! {
309305
fn arb_testrecord()(contents in ".*", length in any::<u64>()) -> roundtrip::Testrecord {
@@ -432,34 +428,13 @@ mod wit_test {
432428
drop(guard);
433429
}
434430

435-
struct FailingSandbox;
436-
437-
impl Callable for FailingSandbox {
438-
fn call<Output: SupportedReturnType>(
439-
&mut self,
440-
_func_name: &str,
441-
_args: impl ParameterTuple,
442-
) -> hyperlight_host::Result<Output> {
443-
Err(HyperlightError::Error(
444-
"synthetic guest call failure".into(),
445-
))
446-
}
447-
}
448-
449-
fn failing_sb() -> TestSandbox<crate::Host, FailingSandbox> {
450-
TestSandbox {
451-
sb: FailingSandbox,
452-
rt: Arc::new(Mutex::new(TestResources::<crate::Host>::new())),
453-
}
454-
}
455-
456431
#[test]
457432
fn test_guest_call_error_returns_error() {
458-
let mut sb = failing_sb();
459-
let err = sb.roundtrip().roundtrip_bool(true).unwrap_err();
433+
let mut sb = sb();
434+
let err = sb.failable().guest_panic().unwrap_err();
460435
assert!(matches!(
461436
err,
462-
HyperlightError::Error(msg) if msg == "synthetic guest call failure"
437+
HyperlightError::GuestAborted(_, msg) if msg.contains("deliberate guest panic")
463438
));
464439
}
465440
}

src/tests/rust_guests/witguest/guest.wit

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ world test {
55
import host-resource;
66
export roundtrip;
77
export test-host-resource;
8+
export failable;
89
}
910

1011
interface roundtrip {
@@ -89,4 +90,8 @@ interface test-host-resource {
8990
test-accepts-borrow: func(x: borrow<testresource>);
9091
test-accepts-own: func(x: own<testresource>);
9192
test-returns: func() -> own<testresource>;
93+
}
94+
95+
interface failable {
96+
guest-panic: func() -> string;
9297
}

src/tests/rust_guests/witguest/src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ impl test::wit::Roundtrip for Guest {
166166
}
167167
}
168168

169+
impl test::wit::Failable for Guest {
170+
fn guest_panic(&mut self) -> alloc::string::String {
171+
panic!("deliberate guest panic")
172+
}
173+
}
174+
169175
use alloc::string::ToString;
170176

171177
use test::wit::host_resource::Testresource;
@@ -214,6 +220,10 @@ impl test::wit::TestExports<Host> for Guest {
214220
fn test_host_resource(&mut self) -> &mut Self {
215221
self
216222
}
223+
type Failable = Self;
224+
fn failable(&mut self) -> &mut Self {
225+
self
226+
}
217227
}
218228

219229
static GUEST_STATE: Mutex<Guest> = Mutex::new(Guest {

0 commit comments

Comments
 (0)