@@ -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,
@@ -73,6 +74,8 @@ pub(crate) struct SandboxMemoryManager<S> {
73
74
pub ( crate ) entrypoint_offset : Offset ,
74
75
/// How many memory regions were mapped after sandbox creation
75
76
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 > > ,
76
79
}
77
80
78
81
impl < S > SandboxMemoryManager < S >
93
96
load_addr,
94
97
entrypoint_offset,
95
98
mapped_rgns : 0 ,
99
+ most_recent_snapshot : None ,
96
100
}
97
101
}
98
102
@@ -259,25 +263,40 @@ where
259
263
}
260
264
}
261
265
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)
264
278
}
265
279
266
280
/// This function restores a memory snapshot from a given snapshot.
267
281
///
268
282
/// Returns the number of memory regions mapped into the sandbox
269
283
/// that need to be unmapped in order for the restore to be
270
284
/// 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 > {
279
290
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
+
281
300
Ok ( old_rgns - self . mapped_rgns )
282
301
}
283
302
@@ -407,13 +426,15 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
407
426
load_addr : self . load_addr . clone ( ) ,
408
427
entrypoint_offset : self . entrypoint_offset ,
409
428
mapped_rgns : 0 ,
429
+ most_recent_snapshot : self . most_recent_snapshot . clone ( ) ,
410
430
} ,
411
431
SandboxMemoryManager {
412
432
shared_mem : gshm,
413
433
layout : self . layout ,
414
434
load_addr : self . load_addr . clone ( ) ,
415
435
entrypoint_offset : self . entrypoint_offset ,
416
436
mapped_rgns : 0 ,
437
+ most_recent_snapshot : self . most_recent_snapshot . clone ( ) ,
417
438
} ,
418
439
)
419
440
}
0 commit comments