@@ -15,6 +15,7 @@ limitations under the License.
15
15
*/
16
16
17
17
use std:: cmp:: Ordering ;
18
+ use std:: sync:: Arc ;
18
19
19
20
use hyperlight_common:: flatbuffer_wrappers:: function_call:: {
20
21
FunctionCall , validate_guest_function_call_buffer,
@@ -74,6 +75,8 @@ pub(crate) struct SandboxMemoryManager<S> {
74
75
pub ( crate ) entrypoint_offset : Offset ,
75
76
/// How many memory regions were mapped after sandbox creation
76
77
pub ( crate ) mapped_rgns : u64 ,
78
+ /// Most recent snapshot taken, in other words, the most recent state that `self` has been in (disregarding currently dirty pages)
79
+ pub ( crate ) most_recent_snapshot : Option < Arc < SharedMemorySnapshot > > ,
77
80
}
78
81
79
82
impl < S > SandboxMemoryManager < S >
94
97
load_addr,
95
98
entrypoint_offset,
96
99
mapped_rgns : 0 ,
100
+ most_recent_snapshot : None ,
97
101
}
98
102
}
99
103
@@ -265,20 +269,32 @@ where
265
269
& mut self ,
266
270
sandbox_id : u64 ,
267
271
mapped_regions : Vec < MemoryRegion > ,
268
- ) -> Result < SharedMemorySnapshot > {
269
- SharedMemorySnapshot :: new ( & mut self . shared_mem , sandbox_id, mapped_regions)
272
+ dirty_pages_bitmap : & [ u64 ] ,
273
+ ) -> Result < Arc < SharedMemorySnapshot > > {
274
+ let snapshot = Arc :: new ( SharedMemorySnapshot :: new (
275
+ & mut self . shared_mem ,
276
+ sandbox_id,
277
+ mapped_regions,
278
+ dirty_pages_bitmap,
279
+ self . most_recent_snapshot . clone ( ) ,
280
+ ) ?) ;
281
+ self . most_recent_snapshot = Some ( snapshot. clone ( ) ) ;
282
+ Ok ( snapshot)
270
283
}
271
284
272
285
/// This function restores a memory snapshot from a given snapshot.
273
- pub ( crate ) fn restore_snapshot ( & mut self , snapshot : & SharedMemorySnapshot ) -> Result < ( ) > {
274
- if self . shared_mem . mem_size ( ) != snapshot. mem_size ( ) {
275
- return Err ( new_error ! (
276
- "Snapshot size does not match current memory size: {} != {}" ,
277
- self . shared_mem. raw_mem_size( ) ,
278
- snapshot. mem_size( )
279
- ) ) ;
280
- }
281
- snapshot. restore_from_snapshot ( & mut self . shared_mem ) ?;
286
+ pub ( crate ) fn restore_snapshot (
287
+ & mut self ,
288
+ snapshot : & Arc < SharedMemorySnapshot > ,
289
+ dirty_pages_bitmap : & [ u64 ] ,
290
+ ) -> Result < ( ) > {
291
+ snapshot. restore_from_snapshot (
292
+ & mut self . shared_mem ,
293
+ dirty_pages_bitmap,
294
+ & self . most_recent_snapshot ,
295
+ ) ?;
296
+ // Update the most recent snapshot to the one we just restored to
297
+ self . most_recent_snapshot = Some ( snapshot. clone ( ) ) ;
282
298
Ok ( ( ) )
283
299
}
284
300
@@ -415,13 +431,15 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
415
431
load_addr : self . load_addr . clone ( ) ,
416
432
entrypoint_offset : self . entrypoint_offset ,
417
433
mapped_rgns : 0 ,
434
+ most_recent_snapshot : self . most_recent_snapshot . clone ( ) ,
418
435
} ,
419
436
SandboxMemoryManager {
420
437
shared_mem : gshm,
421
438
layout : self . layout ,
422
439
load_addr : self . load_addr . clone ( ) ,
423
440
entrypoint_offset : self . entrypoint_offset ,
424
441
mapped_rgns : 0 ,
442
+ most_recent_snapshot : self . most_recent_snapshot . clone ( ) ,
425
443
} ,
426
444
)
427
445
}
0 commit comments