@@ -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,
@@ -74,6 +75,8 @@ pub(crate) struct SandboxMemoryManager<S> {
7475 pub ( crate ) entrypoint_offset : Offset ,
7576 /// How many memory regions were mapped after sandbox creation
7677 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 > > ,
7780}
7881
7982impl < S > SandboxMemoryManager < S >
9497 load_addr,
9598 entrypoint_offset,
9699 mapped_rgns : 0 ,
100+ most_recent_snapshot : None ,
97101 }
98102 }
99103
@@ -265,20 +269,32 @@ where
265269 & mut self ,
266270 sandbox_id : u64 ,
267271 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)
270283 }
271284
272285 /// 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 ( ) ) ;
282298 Ok ( ( ) )
283299 }
284300
@@ -415,13 +431,15 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
415431 load_addr : self . load_addr . clone ( ) ,
416432 entrypoint_offset : self . entrypoint_offset ,
417433 mapped_rgns : 0 ,
434+ most_recent_snapshot : self . most_recent_snapshot . clone ( ) ,
418435 } ,
419436 SandboxMemoryManager {
420437 shared_mem : gshm,
421438 layout : self . layout ,
422439 load_addr : self . load_addr . clone ( ) ,
423440 entrypoint_offset : self . entrypoint_offset ,
424441 mapped_rgns : 0 ,
442+ most_recent_snapshot : self . most_recent_snapshot . clone ( ) ,
425443 } ,
426444 )
427445 }
0 commit comments