@@ -94,6 +94,7 @@ pub(crate) struct SandboxMemoryLayout {
94
94
pub ( super ) peb_host_function_definitions_offset : usize ,
95
95
peb_input_data_offset : usize ,
96
96
peb_output_data_offset : usize ,
97
+ peb_user_memory_offset : usize ,
97
98
peb_heap_data_offset : usize ,
98
99
peb_guest_stack_data_offset : usize ,
99
100
@@ -152,6 +153,10 @@ impl Debug for SandboxMemoryLayout {
152
153
"Output Data Offset" ,
153
154
& format_args ! ( "{:#x}" , self . peb_output_data_offset) ,
154
155
)
156
+ . field (
157
+ "User Memory Offset" ,
158
+ & format_args ! ( "{:#x}" , self . peb_user_memory_offset) ,
159
+ )
155
160
. field (
156
161
"Guest Heap Offset" ,
157
162
& format_args ! ( "{:#x}" , self . peb_heap_data_offset) ,
@@ -251,6 +256,7 @@ impl SandboxMemoryLayout {
251
256
let peb_code_pointer_offset = peb_offset + offset_of ! ( HyperlightPEB , code_ptr) ;
252
257
let peb_input_data_offset = peb_offset + offset_of ! ( HyperlightPEB , input_stack) ;
253
258
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) ;
254
260
let peb_heap_data_offset = peb_offset + offset_of ! ( HyperlightPEB , guest_heap) ;
255
261
let peb_guest_stack_data_offset = peb_offset + offset_of ! ( HyperlightPEB , guest_stack) ;
256
262
let peb_host_function_definitions_offset =
@@ -294,6 +300,7 @@ impl SandboxMemoryLayout {
294
300
peb_host_function_definitions_offset,
295
301
peb_input_data_offset,
296
302
peb_output_data_offset,
303
+ peb_user_memory_offset,
297
304
peb_heap_data_offset,
298
305
peb_guest_stack_data_offset,
299
306
sandbox_memory_config : cfg,
@@ -334,6 +341,13 @@ impl SandboxMemoryLayout {
334
341
self . peb_host_function_definitions_offset + size_of :: < u64 > ( )
335
342
}
336
343
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
+
337
351
/// Get the offset in guest memory to the minimum guest stack address.
338
352
#[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
339
353
fn get_min_guest_stack_address_offset ( & self ) -> usize {
@@ -370,6 +384,14 @@ impl SandboxMemoryLayout {
370
384
self . get_output_data_size_offset ( ) + size_of :: < u64 > ( )
371
385
}
372
386
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
+
373
395
/// Get the offset in guest memory to the start of output data.
374
396
///
375
397
/// This function exists to accommodate the macro that generates C API
@@ -817,6 +839,14 @@ impl SandboxMemoryLayout {
817
839
let addr = get_address ! ( output_data_buffer_offset) ;
818
840
shared_mem. write_u64 ( self . get_output_data_pointer_offset ( ) , addr) ?;
819
841
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
+
820
850
// Set up heap buffer pointer
821
851
let addr = get_address ! ( guest_heap_buffer_offset) ;
822
852
shared_mem. write_u64 ( self . get_heap_size_offset ( ) , self . heap_size . try_into ( ) ?) ?;
0 commit comments