Skip to content

Commit f5c18f4

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 dbb4324 commit f5c18f4

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed

src/hyperlight_guest/src/guest_handle/host_comm.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,29 @@ use crate::error::{HyperlightGuestError, Result};
3434
use crate::exit::out32;
3535

3636
impl GuestHandle {
37+
/// Get user memory region as bytes.
38+
pub fn read_n_bytes_from_user_memory(&self, num: u64) -> Result<Vec<u8>> {
39+
let peb_ptr = self.peb().unwrap();
40+
let user_memory_region_ptr = unsafe { (*peb_ptr).user_memory.ptr as *mut u8 };
41+
let user_memory_region_size = unsafe { (*peb_ptr).user_memory.size };
42+
43+
if num > user_memory_region_size {
44+
return Err(HyperlightGuestError::new(
45+
ErrorCode::GuestError,
46+
format!(
47+
"Requested {} bytes from user memory, but only {} bytes are available",
48+
num, user_memory_region_size
49+
),
50+
));
51+
} else {
52+
let user_memory_region_slice =
53+
unsafe { core::slice::from_raw_parts(user_memory_region_ptr, num as usize) };
54+
let user_memory_region_bytes = user_memory_region_slice.to_vec();
55+
56+
Ok(user_memory_region_bytes)
57+
}
58+
}
59+
3760
/// Get a return value from a host function call.
3861
/// This usually requires a host function to be called first using
3962
/// `call_host_function_internal`.

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: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,42 @@ mod tests {
415415
use hyperlight_testing::simple_guest_as_string;
416416

417417
use crate::sandbox::SandboxConfiguration;
418-
use crate::sandbox::uninitialized::GuestBinary;
418+
use crate::sandbox::uninitialized::{GuestBinary, GuestEnvironment};
419419
use crate::sandbox_state::sandbox::EvolvableSandbox;
420420
use crate::sandbox_state::transition::Noop;
421421
use crate::{MultiUseSandbox, Result, UninitializedSandbox, new_error};
422422

423+
#[test]
424+
fn test_load_extra_blob() {
425+
let binary_path = simple_guest_as_string().unwrap();
426+
let buffer = [0xde, 0xad, 0xbe, 0xef];
427+
let guest_env =
428+
GuestEnvironment::new(GuestBinary::FilePath(binary_path.clone()), Some(&buffer));
429+
430+
{
431+
let uninitialized_sandbox = UninitializedSandbox::new(guest_env.clone(), None);
432+
assert!(uninitialized_sandbox.is_err()); // should fail because we don't have enough memory to accommodate the extra blob
433+
}
434+
435+
{
436+
let mut cfg = SandboxConfiguration::default();
437+
cfg.set_guest_memory_size(0x500_000);
438+
439+
let uninitialized_sandbox = UninitializedSandbox::new(guest_env, Some(cfg)).unwrap();
440+
let mut sandbox: MultiUseSandbox =
441+
uninitialized_sandbox.evolve(Noop::default()).unwrap();
442+
443+
let res = sandbox
444+
.call_guest_function_by_name::<Vec<u8>>(
445+
"ReadFromUserMemory",
446+
(4u64, buffer.to_vec()),
447+
)
448+
.expect("Failed to call ReadFromUserMemory");
449+
450+
assert_eq!(res, buffer.to_vec());
451+
}
452+
}
453+
423454
#[test]
424455
fn test_new_sandbox() {
425456
// 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)