@@ -73,6 +73,8 @@ pub(crate) struct SandboxMemoryManager<S> {
7373 pub ( crate ) load_addr : RawPtr ,
7474 /// Offset for the execution entrypoint from `load_addr`
7575 pub ( crate ) entrypoint_offset : Offset ,
76+ /// How many memory regions were mapped after sandbox creation
77+ pub ( crate ) mapped_rgns : u64 ,
7678 /// A vector of memory snapshots that can be used to save and restore the state of the memory
7779 /// This is used by the Rust Sandbox implementation (rather than the mem_snapshot field above which only exists to support current C API)
7880 snapshots : Arc < Mutex < Vec < SharedMemorySnapshot > > > ,
9597 shared_mem,
9698 load_addr,
9799 entrypoint_offset,
100+ mapped_rgns : 0 ,
98101 snapshots : Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ,
99102 }
100103 }
@@ -265,7 +268,7 @@ where
265268 /// this function will create a memory snapshot and push it onto the stack of snapshots
266269 /// It should be used when you want to save the state of the memory, for example, when evolving a sandbox to a new state
267270 pub ( crate ) fn push_state ( & mut self ) -> Result < ( ) > {
268- let snapshot = SharedMemorySnapshot :: new ( & mut self . shared_mem ) ?;
271+ let snapshot = SharedMemorySnapshot :: new ( & mut self . shared_mem , self . mapped_rgns ) ?;
269272 self . snapshots
270273 . try_lock ( )
271274 . map_err ( |e| new_error ! ( "Error locking at {}:{}: {}" , file!( ) , line!( ) , e) ) ?
@@ -277,7 +280,11 @@ where
277280 /// off the stack
278281 /// It should be used when you want to restore the state of the memory to a previous state but still want to
279282 /// retain that state, for example after calling a function in the guest
280- pub ( crate ) fn restore_state_from_last_snapshot ( & mut self ) -> Result < ( ) > {
283+ ///
284+ /// Returns the number of memory regions mapped into the sandbox
285+ /// that need to be unmapped in order for the restore to be
286+ /// completed.
287+ pub ( crate ) fn restore_state_from_last_snapshot ( & mut self ) -> Result < u64 > {
281288 let mut snapshots = self
282289 . snapshots
283290 . try_lock ( )
@@ -288,13 +295,15 @@ where
288295 }
289296 #[ allow( clippy:: unwrap_used) ] // We know that last is not None because we checked it above
290297 let snapshot = last. unwrap ( ) ;
291- snapshot. restore_from_snapshot ( & mut self . shared_mem )
298+ let old_rgns = self . mapped_rgns ;
299+ self . mapped_rgns = snapshot. restore_from_snapshot ( & mut self . shared_mem ) ?;
300+ Ok ( old_rgns - self . mapped_rgns )
292301 }
293302
294303 /// this function pops the last snapshot off the stack and restores the memory to the previous state
295304 /// It should be used when you want to restore the state of the memory to a previous state and do not need to retain that state
296305 /// for example when devolving a sandbox to a previous state.
297- pub ( crate ) fn pop_and_restore_state_from_snapshot ( & mut self ) -> Result < ( ) > {
306+ pub ( crate ) fn pop_and_restore_state_from_snapshot ( & mut self ) -> Result < u64 > {
298307 let last = self
299308 . snapshots
300309 . try_lock ( )
@@ -430,13 +439,15 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
430439 layout : self . layout ,
431440 load_addr : self . load_addr . clone ( ) ,
432441 entrypoint_offset : self . entrypoint_offset ,
442+ mapped_rgns : 0 ,
433443 snapshots : Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ,
434444 } ,
435445 SandboxMemoryManager {
436446 shared_mem : gshm,
437447 layout : self . layout ,
438448 load_addr : self . load_addr . clone ( ) ,
439449 entrypoint_offset : self . entrypoint_offset ,
450+ mapped_rgns : 0 ,
440451 snapshots : Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ,
441452 } ,
442453 )
0 commit comments