Skip to content

Commit f46ffef

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 076c024 commit f46ffef

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-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
@@ -32,6 +32,29 @@ use crate::error::{HyperlightGuestError, Result};
3232
use crate::exit::out32;
3333

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

61+
pub fn read_n_bytes_from_user_memory(num: u64) -> Result<Vec<u8>> {
62+
let handle = unsafe { GUEST_HANDLE };
63+
handle.read_n_bytes_from_user_memory(num)
64+
}
65+
6166
/// Print a message using the host's print function.
6267
///
6368
/// This function requires memory to be setup to be used. In particular, the

src/hyperlight_host/src/sandbox/uninitialized.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,11 +448,44 @@ mod tests {
448448
use hyperlight_testing::simple_guest_as_string;
449449

450450
use crate::sandbox::SandboxConfiguration;
451-
use crate::sandbox::uninitialized::GuestBlob;
451+
use crate::sandbox::uninitialized::{GuestBlob, GuestEnvironment};
452452
use crate::sandbox_state::sandbox::EvolvableSandbox;
453453
use crate::sandbox_state::transition::Noop;
454454
use crate::{MultiUseSandbox, Result, UninitializedSandbox, new_error};
455455

456+
#[test]
457+
fn test_load_extra_blob() {
458+
let binary_path = simple_guest_as_string().unwrap();
459+
let buffer = [0xde, 0xad, 0xbe, 0xef];
460+
let guest_env = GuestEnvironment::new(
461+
GuestBlob::FilePath(binary_path.clone()),
462+
Some(GuestBlob::Buffer(&buffer)),
463+
);
464+
465+
{
466+
let uninitialized_sandbox = UninitializedSandbox::new(guest_env.clone(), None);
467+
assert!(uninitialized_sandbox.is_err()); // should fail because we don't have enough memory to accommodate the extra blob
468+
}
469+
470+
{
471+
let mut cfg = SandboxConfiguration::default();
472+
cfg.set_guest_memory_size(0x500_000);
473+
474+
let uninitialized_sandbox = UninitializedSandbox::new(guest_env, Some(cfg)).unwrap();
475+
let mut sandbox: MultiUseSandbox =
476+
uninitialized_sandbox.evolve(Noop::default()).unwrap();
477+
478+
let res = sandbox
479+
.call_guest_function_by_name::<Vec<u8>>(
480+
"ReadFromUserMemory",
481+
(4u64, buffer.to_vec()),
482+
)
483+
.expect("Failed to call ReadFromUserMemory");
484+
485+
assert_eq!(res, buffer.to_vec());
486+
}
487+
}
488+
456489
#[test]
457490
fn test_new_sandbox() {
458491
// 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)