Skip to content

Commit a3d0f2b

Browse files
committed
[common,host/mem] added init data offset to PEB
W/ this, now the guest can access that memory region. Signed-off-by: danbugs <danilochiarlone@gmail.com>
1 parent dd654f5 commit a3d0f2b

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/hyperlight_common/src/mem.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub struct HyperlightPEB {
4646
pub code_ptr: u64,
4747
pub input_stack: GuestMemoryRegion,
4848
pub output_stack: GuestMemoryRegion,
49+
pub init_data: GuestMemoryRegion,
4950
pub guest_heap: GuestMemoryRegion,
5051
pub guest_stack: GuestStack,
5152
pub host_function_definitions: GuestMemoryRegion,

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).init_data.ptr as *mut u8 };
41+
let user_memory_region_size = unsafe { (*peb_ptr).init_data.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_host/src/mem/layout.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub(crate) struct SandboxMemoryLayout {
100100
pub(super) peb_host_function_definitions_offset: usize,
101101
peb_input_data_offset: usize,
102102
peb_output_data_offset: usize,
103+
peb_init_data_offset: usize,
103104
peb_heap_data_offset: usize,
104105
peb_guest_stack_data_offset: usize,
105106

@@ -163,6 +164,10 @@ impl Debug for SandboxMemoryLayout {
163164
"Output Data Offset",
164165
&format_args!("{:#x}", self.peb_output_data_offset),
165166
)
167+
.field(
168+
"Init Data Offset",
169+
&format_args!("{:#x}", self.peb_init_data_offset),
170+
)
166171
.field(
167172
"Guest Heap Offset",
168173
&format_args!("{:#x}", self.peb_heap_data_offset),
@@ -264,6 +269,7 @@ impl SandboxMemoryLayout {
264269
let peb_code_pointer_offset = peb_offset + offset_of!(HyperlightPEB, code_ptr);
265270
let peb_input_data_offset = peb_offset + offset_of!(HyperlightPEB, input_stack);
266271
let peb_output_data_offset = peb_offset + offset_of!(HyperlightPEB, output_stack);
272+
let peb_init_data_offset = peb_offset + offset_of!(HyperlightPEB, init_data);
267273
let peb_heap_data_offset = peb_offset + offset_of!(HyperlightPEB, guest_heap);
268274
let peb_guest_stack_data_offset = peb_offset + offset_of!(HyperlightPEB, guest_stack);
269275
let peb_host_function_definitions_offset =
@@ -307,6 +313,7 @@ impl SandboxMemoryLayout {
307313
peb_host_function_definitions_offset,
308314
peb_input_data_offset,
309315
peb_output_data_offset,
316+
peb_init_data_offset,
310317
peb_heap_data_offset,
311318
peb_guest_stack_data_offset,
312319
sandbox_memory_config: cfg,
@@ -349,6 +356,13 @@ impl SandboxMemoryLayout {
349356
self.peb_host_function_definitions_offset + size_of::<u64>()
350357
}
351358

359+
/// Get the offset in guest memory to the init data size
360+
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
361+
pub(super) fn get_init_data_size_offset(&self) -> usize {
362+
// The init data size is the first field in the `GuestMemoryRegion` struct
363+
self.peb_init_data_offset
364+
}
365+
352366
/// Get the offset in guest memory to the minimum guest stack address.
353367
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
354368
fn get_min_guest_stack_address_offset(&self) -> usize {
@@ -385,6 +399,14 @@ impl SandboxMemoryLayout {
385399
self.get_output_data_size_offset() + size_of::<u64>()
386400
}
387401

402+
/// Get the offset in guest memory to the init data pointer.
403+
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
404+
pub(super) fn get_init_data_pointer_offset(&self) -> usize {
405+
// The init data pointer is immediately after the init data size field,
406+
// which is a `u64`.
407+
self.get_init_data_size_offset() + size_of::<u64>()
408+
}
409+
388410
/// Get the offset in guest memory to the start of output data.
389411
///
390412
/// This function exists to accommodate the macro that generates C API
@@ -820,6 +842,14 @@ impl SandboxMemoryLayout {
820842
let addr = get_address!(output_data_buffer_offset);
821843
shared_mem.write_u64(self.get_output_data_pointer_offset(), addr)?;
822844

845+
// Set up init data pointer
846+
shared_mem.write_u64(
847+
self.get_init_data_size_offset(),
848+
(self.get_unaligned_memory_size() - self.init_data_offset).try_into()?,
849+
)?;
850+
let addr = get_address!(init_data_offset);
851+
shared_mem.write_u64(self.get_init_data_pointer_offset(), addr)?;
852+
823853
// Set up heap buffer pointer
824854
let addr = get_address!(guest_heap_buffer_offset);
825855
shared_mem.write_u64(self.get_heap_size_offset(), self.heap_size.try_into()?)?;

0 commit comments

Comments
 (0)