@@ -87,6 +87,7 @@ pub(crate) struct SandboxMemoryLayout {
87
87
peb_code_pointer_offset : usize ,
88
88
peb_input_data_offset : usize ,
89
89
peb_output_data_offset : usize ,
90
+ peb_user_memory_offset : usize ,
90
91
peb_heap_data_offset : usize ,
91
92
peb_guest_stack_data_offset : usize ,
92
93
@@ -140,6 +141,10 @@ impl Debug for SandboxMemoryLayout {
140
141
"Output Data Offset" ,
141
142
& format_args ! ( "{:#x}" , self . peb_output_data_offset) ,
142
143
)
144
+ . field (
145
+ "User Memory Offset" ,
146
+ & format_args ! ( "{:#x}" , self . peb_user_memory_offset) ,
147
+ )
143
148
. field (
144
149
"Guest Heap Offset" ,
145
150
& format_args ! ( "{:#x}" , self . peb_heap_data_offset) ,
@@ -235,6 +240,7 @@ impl SandboxMemoryLayout {
235
240
let peb_code_pointer_offset = peb_offset + offset_of ! ( HyperlightPEB , code_ptr) ;
236
241
let peb_input_data_offset = peb_offset + offset_of ! ( HyperlightPEB , input_stack) ;
237
242
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) ;
238
244
let peb_heap_data_offset = peb_offset + offset_of ! ( HyperlightPEB , guest_heap) ;
239
245
let peb_guest_stack_data_offset = peb_offset + offset_of ! ( HyperlightPEB , guest_stack) ;
240
246
@@ -270,6 +276,7 @@ impl SandboxMemoryLayout {
270
276
peb_code_pointer_offset,
271
277
peb_input_data_offset,
272
278
peb_output_data_offset,
279
+ peb_user_memory_offset,
273
280
peb_heap_data_offset,
274
281
peb_guest_stack_data_offset,
275
282
sandbox_memory_config : cfg,
@@ -293,6 +300,13 @@ impl SandboxMemoryLayout {
293
300
self . peb_output_data_offset
294
301
}
295
302
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
+
296
310
/// Get the offset in guest memory to the minimum guest stack address.
297
311
#[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
298
312
fn get_min_guest_stack_address_offset ( & self ) -> usize {
@@ -329,6 +343,14 @@ impl SandboxMemoryLayout {
329
343
self . get_output_data_size_offset ( ) + size_of :: < u64 > ( )
330
344
}
331
345
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
+
332
354
/// Get the offset in guest memory to the start of output data.
333
355
///
334
356
/// This function exists to accommodate the macro that generates C API
@@ -745,6 +767,14 @@ impl SandboxMemoryLayout {
745
767
let addr = get_address ! ( output_data_buffer_offset) ;
746
768
shared_mem. write_u64 ( self . get_output_data_pointer_offset ( ) , addr) ?;
747
769
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
+
748
778
// Set up heap buffer pointer
749
779
let addr = get_address ! ( guest_heap_buffer_offset) ;
750
780
shared_mem. write_u64 ( self . get_heap_size_offset ( ) , self . heap_size . try_into ( ) ?) ?;
0 commit comments