@@ -148,6 +148,9 @@ pub(crate) struct SandboxMemoryManager<S: SharedMemory> {
148148 pub ( crate ) mapped_rgns : u64 ,
149149 /// Buffer for accumulating guest abort messages
150150 pub ( crate ) abort_buffer : Vec < u8 > ,
151+ /// Snapshot restore generation counter. 0 means no restore
152+ /// and is incremented on each `restore_snapshot` call.
153+ pub ( crate ) restore_count : u64 ,
151154}
152155
153156/// Buffer for building guest page tables during snapshot creation.
@@ -274,6 +277,7 @@ where
274277 entrypoint,
275278 mapped_rgns : 0 ,
276279 abort_buffer : Vec :: new ( ) ,
280+ restore_count : 0 ,
277281 }
278282 }
279283
@@ -341,6 +345,7 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
341345 entrypoint : self . entrypoint ,
342346 mapped_rgns : self . mapped_rgns ,
343347 abort_buffer : self . abort_buffer ,
348+ restore_count : self . restore_count ,
344349 } ;
345350 let guest_mgr = SandboxMemoryManager {
346351 shared_mem : gshm,
@@ -349,6 +354,7 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
349354 entrypoint : self . entrypoint ,
350355 mapped_rgns : self . mapped_rgns ,
351356 abort_buffer : Vec :: new ( ) , // Guest doesn't need abort buffer
357+ restore_count : self . restore_count ,
352358 } ;
353359 host_mgr. update_scratch_bookkeeping ( ) ?;
354360 host_mgr. copy_pt_to_scratch ( ) ?;
@@ -541,6 +547,8 @@ impl SandboxMemoryManager<HostSharedMemory> {
541547 Some ( gscratch)
542548 } ;
543549 self . layout = * snapshot. layout ( ) ;
550+ self . restore_count += 1 ;
551+
544552 self . update_scratch_bookkeeping ( ) ?;
545553 self . copy_pt_to_scratch ( ) ?;
546554 Ok ( ( gsnapshot, gscratch) )
@@ -565,6 +573,10 @@ impl SandboxMemoryManager<HostSharedMemory> {
565573 SCRATCH_TOP_SNAPSHOT_PT_GPA_BASE_OFFSET ,
566574 self . layout . get_pt_base_gpa ( ) ,
567575 ) ?;
576+ self . update_scratch_bookkeeping_item (
577+ SCRATCH_TOP_SNAPSHOT_GENERATION_OFFSET ,
578+ self . restore_count ,
579+ ) ?;
568580
569581 // Initialise the guest input and output data buffers in
570582 // scratch memory. TODO: remove the need for this.
0 commit comments