Skip to content

Commit 4633add

Browse files
committed
[test,guest/guest_bin/simpleguest] added test w/ utilizing extra blob data
+ modified guest and guest_bin libs for it too Signed-off-by: danbugs <[email protected]>
1 parent a3d0f2b commit 4633add

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/hyperlight_guest_bin/src/host_comm.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ pub fn get_host_function_details() -> HostFunctionDetails {
6565
handle.get_host_function_details()
6666
}
6767

68+
pub fn read_n_bytes_from_user_memory(num: u64) -> Result<Vec<u8>> {
69+
let handle = unsafe { GUEST_HANDLE };
70+
handle.read_n_bytes_from_user_memory(num)
71+
}
72+
6873
/// Print a message using the host's print function.
6974
///
7075
/// This function requires memory to be setup to be used. In particular, the

src/hyperlight_host/src/sandbox/uninitialized.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,28 @@ mod tests {
432432
use hyperlight_testing::simple_guest_as_string;
433433

434434
use crate::sandbox::SandboxConfiguration;
435-
use crate::sandbox::uninitialized::GuestBinary;
435+
use crate::sandbox::uninitialized::{GuestBinary, GuestEnvironment};
436436
use crate::sandbox_state::sandbox::EvolvableSandbox;
437437
use crate::sandbox_state::transition::Noop;
438438
use crate::{MultiUseSandbox, Result, UninitializedSandbox, new_error};
439439

440+
#[test]
441+
fn test_load_extra_blob() {
442+
let binary_path = simple_guest_as_string().unwrap();
443+
let buffer = [0xde, 0xad, 0xbe, 0xef];
444+
let guest_env =
445+
GuestEnvironment::new(GuestBinary::FilePath(binary_path.clone()), Some(&buffer));
446+
447+
let uninitialized_sandbox = UninitializedSandbox::new(guest_env, None).unwrap();
448+
let mut sandbox: MultiUseSandbox = uninitialized_sandbox.evolve(Noop::default()).unwrap();
449+
450+
let res = sandbox
451+
.call_guest_function_by_name::<Vec<u8>>("ReadFromUserMemory", (4u64, buffer.to_vec()))
452+
.expect("Failed to call ReadFromUserMemory");
453+
454+
assert_eq!(res, buffer.to_vec());
455+
}
456+
440457
#[test]
441458
fn test_new_sandbox() {
442459
// Guest Binary exists at path

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use hyperlight_guest::exit::{abort_with_code, abort_with_code_and_message};
4646
use hyperlight_guest_bin::guest_function::definition::GuestFunctionDefinition;
4747
use hyperlight_guest_bin::guest_function::register::register_function;
4848
use hyperlight_guest_bin::host_comm::{
49-
call_host_function, call_host_function_without_returning_result,
49+
call_host_function, call_host_function_without_returning_result, read_n_bytes_from_user_memory,
5050
};
5151
use hyperlight_guest_bin::memory::malloc;
5252
use hyperlight_guest_bin::{MIN_STACK_ADDRESS, guest_logger};
@@ -750,8 +750,42 @@ fn large_parameters(function_call: &FunctionCall) -> Result<Vec<u8>> {
750750
}
751751
}
752752

753+
fn read_from_user_memory(function_call: &FunctionCall) -> Result<Vec<u8>> {
754+
if let (ParameterValue::ULong(num), ParameterValue::VecBytes(expected)) = (
755+
function_call.parameters.clone().unwrap()[0].clone(),
756+
function_call.parameters.clone().unwrap()[1].clone(),
757+
) {
758+
let bytes = read_n_bytes_from_user_memory(num).expect("Failed to read from user memory");
759+
760+
// verify that the user memory contains the expected data
761+
if bytes != expected {
762+
error!("User memory does not contain the expected data");
763+
return Err(HyperlightGuestError::new(
764+
ErrorCode::GuestError,
765+
"User memory does not contain the expected data".to_string(),
766+
));
767+
}
768+
769+
Ok(get_flatbuffer_result(&*bytes))
770+
} else {
771+
Err(HyperlightGuestError::new(
772+
ErrorCode::GuestFunctionParameterTypeMismatch,
773+
"Invalid parameters passed to read_from_user_memory".to_string(),
774+
))
775+
}
776+
}
777+
753778
#[no_mangle]
754779
pub extern "C" fn hyperlight_main() {
780+
let read_from_user_memory_def = GuestFunctionDefinition::new(
781+
"ReadFromUserMemory".to_string(),
782+
Vec::from(&[ParameterType::ULong, ParameterType::VecBytes]),
783+
ReturnType::VecBytes,
784+
read_from_user_memory as usize,
785+
);
786+
787+
register_function(read_from_user_memory_def);
788+
755789
let set_static_def = GuestFunctionDefinition::new(
756790
"SetStatic".to_string(),
757791
Vec::new(),

0 commit comments

Comments
 (0)