@@ -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