Skip to content

Commit 3c1b2f8

Browse files
committed
[common,host/mem] added user memory offset to PEB
W/ this, now the guest can access that memory region. Signed-off-by: danbugs <[email protected]>
1 parent bfb1d65 commit 3c1b2f8

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-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 user_memory: GuestMemoryRegion,
4950
pub guest_heap: GuestMemoryRegion,
5051
pub guest_stack: GuestStack,
5152
pub host_function_definitions: GuestMemoryRegion,

src/hyperlight_host/src/mem/layout.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub(crate) struct SandboxMemoryLayout {
9494
pub(super) peb_host_function_definitions_offset: usize,
9595
peb_input_data_offset: usize,
9696
peb_output_data_offset: usize,
97+
peb_user_memory_offset: usize,
9798
peb_heap_data_offset: usize,
9899
peb_guest_stack_data_offset: usize,
99100

@@ -152,6 +153,10 @@ impl Debug for SandboxMemoryLayout {
152153
"Output Data Offset",
153154
&format_args!("{:#x}", self.peb_output_data_offset),
154155
)
156+
.field(
157+
"User Memory Offset",
158+
&format_args!("{:#x}", self.peb_user_memory_offset),
159+
)
155160
.field(
156161
"Guest Heap Offset",
157162
&format_args!("{:#x}", self.peb_heap_data_offset),
@@ -251,6 +256,7 @@ impl SandboxMemoryLayout {
251256
let peb_code_pointer_offset = peb_offset + offset_of!(HyperlightPEB, code_ptr);
252257
let peb_input_data_offset = peb_offset + offset_of!(HyperlightPEB, input_stack);
253258
let peb_output_data_offset = peb_offset + offset_of!(HyperlightPEB, output_stack);
259+
let peb_user_memory_offset = peb_offset + offset_of!(HyperlightPEB, user_memory);
254260
let peb_heap_data_offset = peb_offset + offset_of!(HyperlightPEB, guest_heap);
255261
let peb_guest_stack_data_offset = peb_offset + offset_of!(HyperlightPEB, guest_stack);
256262
let peb_host_function_definitions_offset =
@@ -294,6 +300,7 @@ impl SandboxMemoryLayout {
294300
peb_host_function_definitions_offset,
295301
peb_input_data_offset,
296302
peb_output_data_offset,
303+
peb_user_memory_offset,
297304
peb_heap_data_offset,
298305
peb_guest_stack_data_offset,
299306
sandbox_memory_config: cfg,
@@ -334,6 +341,13 @@ impl SandboxMemoryLayout {
334341
self.peb_host_function_definitions_offset + size_of::<u64>()
335342
}
336343

344+
/// Get the offset in guest memory to the user memory size
345+
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
346+
pub(super) fn get_user_memory_size_offset(&self) -> usize {
347+
// The user memory size is the first field in the `GuestMemoryRegion` struct
348+
self.peb_user_memory_offset
349+
}
350+
337351
/// Get the offset in guest memory to the minimum guest stack address.
338352
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
339353
fn get_min_guest_stack_address_offset(&self) -> usize {
@@ -370,6 +384,14 @@ impl SandboxMemoryLayout {
370384
self.get_output_data_size_offset() + size_of::<u64>()
371385
}
372386

387+
/// Get the offset in guest memory to the user memory pointer.
388+
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
389+
pub(super) fn get_user_memory_pointer_offset(&self) -> usize {
390+
// The user memory pointer is immediately after the user memory size field,
391+
// which is a `u64`.
392+
self.get_user_memory_size_offset() + size_of::<u64>()
393+
}
394+
373395
/// Get the offset in guest memory to the start of output data.
374396
///
375397
/// This function exists to accommodate the macro that generates C API
@@ -817,6 +839,14 @@ impl SandboxMemoryLayout {
817839
let addr = get_address!(output_data_buffer_offset);
818840
shared_mem.write_u64(self.get_output_data_pointer_offset(), addr)?;
819841

842+
// Set up user memory pointer
843+
shared_mem.write_u64(
844+
self.get_user_memory_size_offset(),
845+
(self.get_unaligned_memory_size() - self.user_memory_offset).try_into()?,
846+
)?;
847+
let addr = get_address!(user_memory_offset);
848+
shared_mem.write_u64(self.get_user_memory_pointer_offset(), addr)?;
849+
820850
// Set up heap buffer pointer
821851
let addr = get_address!(guest_heap_buffer_offset);
822852
shared_mem.write_u64(self.get_heap_size_offset(), self.heap_size.try_into()?)?;

0 commit comments

Comments
 (0)