Skip to content

Commit 335e9c9

Browse files
committed
Allow rewriting and reading of user-data
Signed-off-by: Quetzal Bradley <[email protected]>
1 parent b61265e commit 335e9c9

File tree

6 files changed

+67
-6
lines changed

6 files changed

+67
-6
lines changed

src/hyperlight_guest/src/guest_handle/host_comm.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ use crate::exit::out32;
3636
impl GuestHandle {
3737
/// Get user memory region as bytes.
3838
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).init_data.ptr as *mut u8 };
41-
let user_memory_region_size = unsafe { (*peb_ptr).init_data.size };
39+
let (user_memory_region_ptr, user_memory_region_size) = self.get_user_memory();
4240

4341
if num > user_memory_region_size {
4442
Err(HyperlightGuestError::new(
@@ -57,6 +55,14 @@ impl GuestHandle {
5755
}
5856
}
5957

58+
pub fn get_user_memory(&self) -> (*const u8, u64) {
59+
let peb_ptr = self.peb().unwrap();
60+
let user_memory_region_ptr = unsafe { (*peb_ptr).init_data.ptr as *const u8 };
61+
let user_memory_region_size = unsafe { (*peb_ptr).init_data.size };
62+
63+
(user_memory_region_ptr, user_memory_region_size)
64+
}
65+
6066
/// Get a return value from a host function call.
6167
/// This usually requires a host function to be called first using
6268
/// `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
@@ -70,6 +70,11 @@ pub fn read_n_bytes_from_user_memory(num: u64) -> Result<Vec<u8>> {
7070
handle.read_n_bytes_from_user_memory(num)
7171
}
7272

73+
pub fn get_user_memory() -> (*const u8, u64) {
74+
let handle = unsafe { GUEST_HANDLE };
75+
handle.get_user_memory()
76+
}
77+
7378
/// Print a message using the host's print function.
7479
///
7580
/// This function requires memory to be setup to be used. In particular, the

src/hyperlight_guest_capi/src/dispatch.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
2626
use hyperlight_guest::error::{HyperlightGuestError, Result};
2727
use hyperlight_guest_bin::guest_function::definition::GuestFunctionDefinition;
2828
use hyperlight_guest_bin::guest_function::register::GuestFunctionRegister;
29-
use hyperlight_guest_bin::host_comm::call_host_function_without_returning_result;
29+
use hyperlight_guest_bin::host_comm::{
30+
call_host_function_without_returning_result, get_user_memory,
31+
};
3032

3133
use crate::types::{FfiFunctionCall, FfiVec};
3234
static mut REGISTERED_C_GUEST_FUNCTIONS: GuestFunctionRegister = GuestFunctionRegister::new();
@@ -114,3 +116,15 @@ pub extern "C" fn hl_call_host_function(function_call: &FfiFunctionCall) {
114116
let _ = call_host_function_without_returning_result(&func_name, Some(parameters), return_type)
115117
.expect("Failed to call host function");
116118
}
119+
120+
#[unsafe(no_mangle)]
121+
pub extern "C" fn hl_get_user_memory_ptr() -> *const u8 {
122+
let (user_memory_region_ptr, _) = get_user_memory();
123+
user_memory_region_ptr
124+
}
125+
126+
#[unsafe(no_mangle)]
127+
pub extern "C" fn hl_get_user_memory_size() -> u64 {
128+
let (_, user_memory_region_size) = get_user_memory();
129+
user_memory_region_size
130+
}

src/hyperlight_host/src/func/call_ctx.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ impl MultiUseGuestCallContext {
9595
pub fn map_file_cow(&mut self, fp: &std::path::Path, guest_base: u64) -> Result<u64> {
9696
self.sbox.map_file_cow(fp, guest_base)
9797
}
98+
99+
/// Write new data to init data
100+
pub fn rewrite_init_data(&mut self, user_memory: &[u8]) -> Result<()> {
101+
use crate::sandbox::WrapperGetter;
102+
let mem_mgr = self.sbox.get_mgr_wrapper_mut();
103+
mem_mgr.rewrite_init_data(user_memory)?;
104+
Ok(())
105+
}
106+
107+
/// Read init data
108+
pub fn read_init_data(&mut self, user_memory: &mut [u8]) -> Result<()> {
109+
use crate::sandbox::WrapperGetter;
110+
let mem_mgr = self.sbox.get_mgr_wrapper_mut();
111+
mem_mgr.read_init_data(user_memory)?;
112+
Ok(())
113+
}
98114
}
99115

100116
impl Callable for MultiUseGuestCallContext {

src/hyperlight_host/src/mem/layout.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub(crate) struct SandboxMemoryLayout {
9191
pub(super) stack_size: usize,
9292
/// The heap size of this sandbox.
9393
pub(super) heap_size: usize,
94-
init_data_size: usize,
94+
pub(crate) init_data_size: usize,
9595

9696
/// The following fields are offsets to the actual PEB struct fields.
9797
/// They are used when writing the PEB struct itself
@@ -114,7 +114,7 @@ pub(crate) struct SandboxMemoryLayout {
114114
guest_heap_buffer_offset: usize,
115115
guard_page_offset: usize,
116116
guest_user_stack_buffer_offset: usize, // the lowest address of the user stack
117-
init_data_offset: usize,
117+
pub(crate) init_data_offset: usize,
118118

119119
// other
120120
pub(crate) peb_address: usize,

src/hyperlight_host/src/sandbox/mem_mgr.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,24 @@ impl MemMgrWrapper<HostSharedMemory> {
120120
self.unwrap_mgr()
121121
.check_stack_guard(*self.get_stack_cookie())
122122
}
123+
124+
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
125+
pub(crate) fn rewrite_init_data(&mut self, user_memory: &[u8]) -> Result<()> {
126+
let mgr = self.unwrap_mgr_mut();
127+
let layout = mgr.layout;
128+
let shared_mem = mgr.get_shared_mem_mut();
129+
assert!(user_memory.len() <= layout.init_data_size);
130+
shared_mem.copy_from_slice(user_memory, layout.init_data_offset)?;
131+
Ok(())
132+
}
133+
134+
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
135+
pub(crate) fn read_init_data(&mut self, user_memory: &mut [u8]) -> Result<()> {
136+
let mgr = self.unwrap_mgr_mut();
137+
let layout = mgr.layout;
138+
let shared_mem = mgr.get_shared_mem_mut();
139+
assert!(user_memory.len() <= layout.init_data_size);
140+
shared_mem.copy_to_slice(user_memory, layout.init_data_offset)?;
141+
Ok(())
142+
}
123143
}

0 commit comments

Comments
 (0)