@@ -87,6 +87,7 @@ pub(crate) struct SandboxMemoryLayout {
8787 peb_code_pointer_offset : usize ,
8888 peb_input_data_offset : usize ,
8989 peb_output_data_offset : usize ,
90+ peb_user_memory_offset : usize ,
9091 peb_heap_data_offset : usize ,
9192 peb_guest_stack_data_offset : usize ,
9293
@@ -140,6 +141,10 @@ impl Debug for SandboxMemoryLayout {
140141 "Output Data Offset" ,
141142 & format_args ! ( "{:#x}" , self . peb_output_data_offset) ,
142143 )
144+ . field (
145+ "User Memory Offset" ,
146+ & format_args ! ( "{:#x}" , self . peb_user_memory_offset) ,
147+ )
143148 . field (
144149 "Guest Heap Offset" ,
145150 & format_args ! ( "{:#x}" , self . peb_heap_data_offset) ,
@@ -235,6 +240,7 @@ impl SandboxMemoryLayout {
235240 let peb_code_pointer_offset = peb_offset + offset_of ! ( HyperlightPEB , code_ptr) ;
236241 let peb_input_data_offset = peb_offset + offset_of ! ( HyperlightPEB , input_stack) ;
237242 let peb_output_data_offset = peb_offset + offset_of ! ( HyperlightPEB , output_stack) ;
243+ let peb_user_memory_offset = peb_offset + offset_of ! ( HyperlightPEB , user_memory) ;
238244 let peb_heap_data_offset = peb_offset + offset_of ! ( HyperlightPEB , guest_heap) ;
239245 let peb_guest_stack_data_offset = peb_offset + offset_of ! ( HyperlightPEB , guest_stack) ;
240246
@@ -270,6 +276,7 @@ impl SandboxMemoryLayout {
270276 peb_code_pointer_offset,
271277 peb_input_data_offset,
272278 peb_output_data_offset,
279+ peb_user_memory_offset,
273280 peb_heap_data_offset,
274281 peb_guest_stack_data_offset,
275282 sandbox_memory_config : cfg,
@@ -293,6 +300,13 @@ impl SandboxMemoryLayout {
293300 self . peb_output_data_offset
294301 }
295302
303+ /// Get the offset in guest memory to the user memory size
304+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
305+ pub ( super ) fn get_user_memory_size_offset ( & self ) -> usize {
306+ // The user memory size is the first field in the `GuestMemoryRegion` struct
307+ self . peb_user_memory_offset
308+ }
309+
296310 /// Get the offset in guest memory to the minimum guest stack address.
297311 #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
298312 fn get_min_guest_stack_address_offset ( & self ) -> usize {
@@ -329,6 +343,14 @@ impl SandboxMemoryLayout {
329343 self . get_output_data_size_offset ( ) + size_of :: < u64 > ( )
330344 }
331345
346+ /// Get the offset in guest memory to the user memory pointer.
347+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
348+ pub ( super ) fn get_user_memory_pointer_offset ( & self ) -> usize {
349+ // The user memory pointer is immediately after the user memory size field,
350+ // which is a `u64`.
351+ self . get_user_memory_size_offset ( ) + size_of :: < u64 > ( )
352+ }
353+
332354 /// Get the offset in guest memory to the start of output data.
333355 ///
334356 /// This function exists to accommodate the macro that generates C API
@@ -745,6 +767,14 @@ impl SandboxMemoryLayout {
745767 let addr = get_address ! ( output_data_buffer_offset) ;
746768 shared_mem. write_u64 ( self . get_output_data_pointer_offset ( ) , addr) ?;
747769
770+ // Set up user memory pointer
771+ shared_mem. write_u64 (
772+ self . get_user_memory_size_offset ( ) ,
773+ ( self . get_unaligned_memory_size ( ) - self . user_memory_offset ) . try_into ( ) ?,
774+ ) ?;
775+ let addr = get_address ! ( user_memory_offset) ;
776+ shared_mem. write_u64 ( self . get_user_memory_pointer_offset ( ) , addr) ?;
777+
748778 // Set up heap buffer pointer
749779 let addr = get_address ! ( guest_heap_buffer_offset) ;
750780 shared_mem. write_u64 ( self . get_heap_size_offset ( ) , self . heap_size . try_into ( ) ?) ?;
0 commit comments