Skip to content

Commit 948406e

Browse files
committed
Change sandbox restore to be a bit more judicious about when to restore
When we are loading from a snapshot, we can get away with just a restore and never going through a wasmtime load. Signed-off-by: Lucy Menon <168595099+syntactically@users.noreply.github.com>
1 parent 931e297 commit 948406e

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

src/hyperlight_wasm/src/sandbox/wasm_sandbox.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub struct WasmSandbox {
4141
// Snapshot of state of an initial WasmSandbox (runtime loaded, but no guest module code loaded).
4242
// Used for LoadedWasmSandbox to be able restore state back to WasmSandbox
4343
snapshot: Option<Arc<Snapshot>>,
44+
needs_restore: bool,
4445
}
4546

4647
const MAPPED_BINARY_VA: u64 = 0x1_0000_0000u64;
@@ -56,6 +57,7 @@ impl WasmSandbox {
5657
Ok(WasmSandbox {
5758
inner: Some(inner),
5859
snapshot: Some(snapshot),
60+
needs_restore: false,
5961
})
6062
}
6163

@@ -64,24 +66,33 @@ impl WasmSandbox {
6466
/// the snapshot has already been created in that case.
6567
/// Expects a snapshot of the state where wasm runtime is loaded, but no guest module code is loaded.
6668
pub(super) fn new_from_loaded(
67-
mut loaded: MultiUseSandbox,
69+
loaded: MultiUseSandbox,
6870
snapshot: Arc<Snapshot>,
6971
) -> Result<Self> {
70-
loaded.restore(snapshot.clone())?;
7172
metrics::gauge!(METRIC_ACTIVE_WASM_SANDBOXES).increment(1);
7273
metrics::counter!(METRIC_TOTAL_WASM_SANDBOXES).increment(1);
7374
Ok(WasmSandbox {
7475
inner: Some(loaded),
7576
snapshot: Some(snapshot),
77+
needs_restore: true,
7678
})
7779
}
7880

81+
fn restore_if_needed(&mut self) -> Result<()> {
82+
if self.needs_restore {
83+
self.inner.as_mut().ok_or(new_error!("WasmSandbox is none"))?.restore(self.snapshot.as_ref().ok_or(new_error!("Snapshot is none"))?.clone())?;
84+
self.needs_restore = false;
85+
}
86+
Ok(())
87+
}
88+
7989
/// Load a Wasm module at the given path into the sandbox and return a `LoadedWasmSandbox`
8090
/// able to execute code in the loaded Wasm Module.
8191
///
8292
/// Before you can call guest functions in the sandbox, you must call
8393
/// this function and use the returned value to call guest functions.
8494
pub fn load_module(mut self, file: impl AsRef<Path>) -> Result<LoadedWasmSandbox> {
95+
self.restore_if_needed()?;
8596
let inner = self
8697
.inner
8798
.as_mut()
@@ -97,6 +108,17 @@ impl WasmSandbox {
97108
self.finalize_module_load()
98109
}
99110

111+
/// Load a Wasm module by restoring a Hyperlight snapshot taken
112+
/// from a `LoadedWasmSandbox`.
113+
pub fn load_from_snapshot(mut self, snapshot: Arc<Snapshot>) -> Result<LoadedWasmSandbox> {
114+
let mut sb = self.inner.take().unwrap();
115+
sb.restore(snapshot)?;
116+
LoadedWasmSandbox::new(
117+
sb,
118+
self.snapshot.take().unwrap(),
119+
)
120+
}
121+
100122
/// Load a Wasm module that is currently present in a buffer in
101123
/// host memory, by mapping the host memory directly into the
102124
/// sandbox.
@@ -114,6 +136,7 @@ impl WasmSandbox {
114136
base: *mut libc::c_void,
115137
len: usize,
116138
) -> Result<LoadedWasmSandbox> {
139+
self.restore_if_needed()?;
117140
let inner = self
118141
.inner
119142
.as_mut()

0 commit comments

Comments
 (0)