Skip to content

Commit 3e17197

Browse files
committed
First round of PR feedback
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent c6e9aaf commit 3e17197

File tree

3 files changed

+79
-10
lines changed

3 files changed

+79
-10
lines changed

src/hyperlight_common/src/flatbuffer_wrappers/function_types.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -738,16 +738,6 @@ impl TryFrom<ReturnValueBox<'_>> for ReturnValue {
738738
}
739739
}
740740

741-
// impl TryFrom<&[u8]> for ReturnValue {
742-
// type Error = Error;
743-
// #[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
744-
// fn try_from(value: &[u8]) -> Result<Self> {
745-
// let function_call_result_fb = size_prefixed_root::<FbFunctionCallResult>(value)
746-
// .map_err(|e| anyhow!("Failed to get ReturnValue from bytes: {:?}", e))?;
747-
// function_call_result_fb.try_into()
748-
// }
749-
// }
750-
751741
impl TryFrom<&ReturnValue> for Vec<u8> {
752742
type Error = Error;
753743
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
@@ -961,3 +951,36 @@ impl TryFrom<&ReturnValue> for Vec<u8> {
961951
Ok(result_bytes)
962952
}
963953
}
954+
955+
#[cfg(test)]
956+
mod tests {
957+
use flatbuffers::FlatBufferBuilder;
958+
959+
use super::super::guest_error::ErrorCode;
960+
use super::*;
961+
962+
#[test]
963+
fn encode_success_result() {
964+
let mut builder = FlatBufferBuilder::new();
965+
let test_data = FunctionCallResult::new(Ok(ReturnValue::Int(42))).encode(&mut builder);
966+
967+
let function_call_result = FunctionCallResult::try_from(test_data).unwrap();
968+
let result = function_call_result.into_inner().unwrap();
969+
assert_eq!(result, ReturnValue::Int(42));
970+
}
971+
972+
#[test]
973+
fn encode_error_result() {
974+
let mut builder = FlatBufferBuilder::new();
975+
let test_error = GuestError::new(
976+
ErrorCode::GuestFunctionNotFound,
977+
"Function not found".to_string(),
978+
);
979+
let test_data = FunctionCallResult::new(Err(test_error.clone())).encode(&mut builder);
980+
981+
let function_call_result = FunctionCallResult::try_from(test_data).unwrap();
982+
let error = function_call_result.into_inner().unwrap_err();
983+
assert_eq!(error.code, test_error.code);
984+
assert_eq!(error.message, test_error.message);
985+
}
986+
}

src/hyperlight_host/src/sandbox/initialized_multi_use.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,16 @@ mod tests {
540540
}
541541
}
542542

543+
#[test]
544+
fn call_host_func_expect_error() {
545+
let path = simple_guest_as_string().unwrap();
546+
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(path), None).unwrap();
547+
let mut sandbox = sandbox.evolve().unwrap();
548+
sandbox
549+
.call::<()>("CallHostExpectError", "SomeUnknownHostFunc".to_string())
550+
.unwrap();
551+
}
552+
543553
/// Make sure input/output buffers are properly reset after guest call (with host call)
544554
#[test]
545555
fn io_buffer_reset() {

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,9 +930,45 @@ fn exec_mapped_buffer(function_call: &FunctionCall) -> Result<Vec<u8>> {
930930
}
931931
}
932932

933+
fn call_host_expect_error(function_call: &FunctionCall) -> Result<Vec<u8>> {
934+
if let ParameterValue::String(hostfuncname) =
935+
function_call.parameters.clone().unwrap()[0].clone()
936+
{
937+
let res = call_host_function::<i32>(&hostfuncname, None, ReturnType::Int);
938+
939+
match res {
940+
Ok(_) => Err(HyperlightGuestError::new(
941+
ErrorCode::GuestError,
942+
"Expected host function to fail, but it succeeded".to_string(),
943+
)),
944+
Err(e) => {
945+
assert_eq!(e.kind, ErrorCode::HostFunctionError);
946+
assert_eq!(
947+
e.message,
948+
format!("HostFunction {} was not found", hostfuncname)
949+
);
950+
Ok(get_flatbuffer_result(()))
951+
}
952+
}
953+
} else {
954+
Err(HyperlightGuestError::new(
955+
ErrorCode::GuestFunctionParameterTypeMismatch,
956+
"Invalid parameters passed to call_host_expect_error".to_string(),
957+
))
958+
}
959+
}
960+
933961
#[no_mangle]
934962
#[hyperlight_guest_tracing::trace_function]
935963
pub extern "C" fn hyperlight_main() {
964+
let expect_error_def = GuestFunctionDefinition::new(
965+
"CallHostExpectError".to_string(),
966+
Vec::from(&[ParameterType::String]),
967+
ReturnType::Void,
968+
call_host_expect_error as usize,
969+
);
970+
register_function(expect_error_def);
971+
936972
let twenty_four_k_in_def = GuestFunctionDefinition::new(
937973
"24K_in_8K_out".to_string(),
938974
Vec::from(&[ParameterType::VecBytes]),

0 commit comments

Comments
 (0)