Skip to content

Commit 6b08240

Browse files
committed
feat(virtq): fix host function error test
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
1 parent 9bde4de commit 6b08240

3 files changed

Lines changed: 16 additions & 26 deletions

File tree

src/hyperlight_guest/src/virtq/context.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub struct GuestContext {
7272
g2h_producer: G2hProducer,
7373
h2g_producer: H2gProducer,
7474
generation: u16,
75-
last_host_return: Option<ReturnValue>,
75+
last_host_result: Option<Result<ReturnValue>>,
7676
}
7777

7878
impl GuestContext {
@@ -101,7 +101,7 @@ impl GuestContext {
101101
g2h_producer,
102102
h2g_producer,
103103
generation,
104-
last_host_return: None,
104+
last_host_result: None,
105105
};
106106

107107
ctx.prefill_h2g();
@@ -309,6 +309,7 @@ impl GuestContext {
309309
// restore_h2g_prefill() wrote matching descriptors to the
310310
// zeroed ring memory. Both sides are in sync.
311311
self.generation = new_generation;
312+
self.last_host_result = None;
312313
}
313314

314315
pub(super) fn generation(&self) -> u16 {
@@ -346,24 +347,27 @@ impl GuestContext {
346347
self.g2h_producer.submit(entry)
347348
}
348349

349-
/// Stash a host function return value for later retrieval.
350+
/// Stash a host function result for later retrieval.
350351
///
351352
/// Used by the C API's two-step calling convention where
352353
/// `hl_call_host_function` and `hl_get_host_return_value_as_*`
353354
/// are separate calls.
354-
pub fn stash_host_return(&mut self, value: ReturnValue) {
355-
self.last_host_return = Some(value);
355+
pub fn stash_host_result(&mut self, result: Result<ReturnValue>) {
356+
self.last_host_result = Some(result);
356357
}
357358

358359
/// Take the stashed host return value.
359360
///
360361
/// Panics if no value was stashed or if the type conversion fails.
362+
/// If the stashed result was an error, panics with the error message.
361363
pub fn take_host_return<T: TryFrom<ReturnValue>>(&mut self) -> T {
362-
let rv = self
363-
.last_host_return
364+
let val = self
365+
.last_host_result
364366
.take()
365-
.expect("No host return value available");
366-
match T::try_from(rv) {
367+
.expect("No host return value available")
368+
.expect("Host function returned an error");
369+
370+
match T::try_from(val) {
367371
Ok(v) => v,
368372
Err(_) => panic!("Host return value type mismatch"),
369373
}

src/hyperlight_guest_capi/src/dispatch.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,9 @@ pub extern "C" fn hl_call_host_function(function_call: &FfiFunctionCall) {
109109
let return_type = unsafe { function_call.copy_return_type() };
110110

111111
virtq::with_context(|ctx| {
112-
match ctx.call_host_function::<ReturnValue>(&func_name, Some(parameters), return_type) {
113-
Ok(result) => ctx.stash_host_return(result),
114-
Err(e) => {
115-
// Host function returned an error. Abort with the error
116-
// message so the host can capture it via the abort buffer.
117-
let msg = alloc::ffi::CString::new(e.message)
118-
.unwrap_or_else(|_| alloc::ffi::CString::new("host error").unwrap());
119-
120-
unsafe {
121-
hyperlight_guest::exit::abort_with_code_and_message(
122-
&[e.kind as u8],
123-
msg.as_ptr(),
124-
);
125-
}
126-
}
127-
}
112+
let result =
113+
ctx.call_host_function::<ReturnValue>(&func_name, Some(parameters), return_type);
114+
ctx.stash_host_result(result);
128115
});
129116
}
130117

src/hyperlight_host/tests/sandbox_host_tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@ fn callback_test_parallel() {
344344
}
345345

346346
#[test]
347-
#[ignore] // TODO(virtq): C guest host-function error path needs fixing.
348347
fn host_function_error() {
349348
with_all_uninit_sandboxes(|mut sandbox| {
350349
// create host function

0 commit comments

Comments
 (0)