@@ -15,6 +15,7 @@ limitations under the License.
1515*/
1616
1717use std:: cmp:: Ordering ;
18+ use std:: sync:: Arc ;
1819
1920use hyperlight_common:: flatbuffer_wrappers:: function_call:: {
2021 FunctionCall , validate_guest_function_call_buffer,
@@ -73,6 +74,8 @@ pub(crate) struct SandboxMemoryManager<S> {
7374 pub ( crate ) entrypoint_offset : Offset ,
7475 /// How many memory regions were mapped after sandbox creation
7576 pub ( crate ) mapped_rgns : u64 ,
77+ /// Most recent snapshot taken, in other words, the most recent state that `self` has been in (disregarding currently dirty pages)
78+ pub ( crate ) most_recent_snapshot : Option < Arc < SharedMemorySnapshot > > ,
7679}
7780
7881impl < S > SandboxMemoryManager < S >
9396 load_addr,
9497 entrypoint_offset,
9598 mapped_rgns : 0 ,
99+ most_recent_snapshot : None ,
96100 }
97101 }
98102
@@ -259,25 +263,40 @@ where
259263 }
260264 }
261265
262- pub ( crate ) fn snapshot ( & mut self ) -> Result < SharedMemorySnapshot > {
263- SharedMemorySnapshot :: new ( & mut self . shared_mem , self . mapped_rgns )
266+ pub ( crate ) fn snapshot (
267+ & mut self ,
268+ dirty_pages_bitmap : & [ u64 ] ,
269+ ) -> Result < Arc < SharedMemorySnapshot > > {
270+ let snapshot = Arc :: new ( SharedMemorySnapshot :: new (
271+ & mut self . shared_mem ,
272+ dirty_pages_bitmap,
273+ self . mapped_rgns ,
274+ self . most_recent_snapshot . clone ( ) ,
275+ ) ?) ;
276+ self . most_recent_snapshot = Some ( snapshot. clone ( ) ) ;
277+ Ok ( snapshot)
264278 }
265279
266280 /// This function restores a memory snapshot from a given snapshot.
267281 ///
268282 /// Returns the number of memory regions mapped into the sandbox
269283 /// that need to be unmapped in order for the restore to be
270284 /// completed.
271- pub ( crate ) fn restore_snapshot ( & mut self , snapshot : & SharedMemorySnapshot ) -> Result < u64 > {
272- if self . shared_mem . mem_size ( ) != snapshot. mem_size ( ) {
273- return Err ( new_error ! (
274- "Snapshot size does not match current memory size: {} != {}" ,
275- self . shared_mem. raw_mem_size( ) ,
276- snapshot. mem_size( )
277- ) ) ;
278- }
285+ pub ( crate ) fn restore_snapshot (
286+ & mut self ,
287+ snapshot : & Arc < SharedMemorySnapshot > ,
288+ dirty_pages_bitmap : & [ u64 ] ,
289+ ) -> Result < u64 > {
279290 let old_rgns = self . mapped_rgns ;
280- self . mapped_rgns = snapshot. restore_from_snapshot ( & mut self . shared_mem ) ?;
291+ self . mapped_rgns = snapshot. restore_from_snapshot (
292+ & mut self . shared_mem ,
293+ dirty_pages_bitmap,
294+ & self . most_recent_snapshot ,
295+ ) ?;
296+
297+ // Update the most recent snapshot to the one we just restored to
298+ self . most_recent_snapshot = Some ( snapshot. clone ( ) ) ;
299+
281300 Ok ( old_rgns - self . mapped_rgns )
282301 }
283302
@@ -407,13 +426,15 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
407426 load_addr : self . load_addr . clone ( ) ,
408427 entrypoint_offset : self . entrypoint_offset ,
409428 mapped_rgns : 0 ,
429+ most_recent_snapshot : self . most_recent_snapshot . clone ( ) ,
410430 } ,
411431 SandboxMemoryManager {
412432 shared_mem : gshm,
413433 layout : self . layout ,
414434 load_addr : self . load_addr . clone ( ) ,
415435 entrypoint_offset : self . entrypoint_offset ,
416436 mapped_rgns : 0 ,
437+ most_recent_snapshot : self . most_recent_snapshot . clone ( ) ,
417438 } ,
418439 )
419440 }
0 commit comments