Skip to content

Commit e798a92

Browse files
committed
Upgrade to Hyperlight 0.13
Signed-off-by: James Sturtevant <jsturtevant@gmail.com>
1 parent 3f70f6d commit e798a92

15 files changed

Lines changed: 311 additions & 216 deletions

File tree

Cargo.lock

Lines changed: 102 additions & 76 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exclude = [ "src/rust_wasm_samples", "src/component_sample" ]
44
resolver = "2"
55

66
[workspace.package]
7-
version = "0.12.0"
7+
version = "0.13.0"
88
edition = "2024"
99
rust-version = "1.89"
1010
license = "Apache-2.0"
@@ -13,11 +13,11 @@ repository = "https://github.com/hyperlight-dev/hyperlight-wasm"
1313
readme = "README.md"
1414

1515
[workspace.dependencies]
16-
hyperlight-common = { version = "0.12.0", default-features = false }
16+
hyperlight-common = { version = "0.13.0", default-features = false }
1717
hyperlight-component-macro = { version = "0.13.0" }
18-
hyperlight-component-util = { version = "0.12.0" }
19-
hyperlight-guest = { version = "0.12.0" }
20-
hyperlight-guest-bin = { version = "0.12.0", features = [ "printf" ] }
21-
hyperlight-host = { version = "0.12.0", default-features = false, features = ["executable_heap", "init-paging"] }
22-
hyperlight-wasm-macro = { version = "0.12.0", path = "src/hyperlight_wasm_macro" }
23-
hyperlight-wasm-runtime = { version = "0.12.0", path = "src/hyperlight_wasm_runtime" }
18+
hyperlight-component-util = { version = "0.13.0" }
19+
hyperlight-guest = { version = "0.13.0" }
20+
hyperlight-guest-bin = { version = "0.13.0", features = [ "printf" ] }
21+
hyperlight-host = { version = "0.13.0", default-features = false, features = ["executable_heap", "init-paging"] }
22+
hyperlight-wasm-macro = { version = "0.13.0", path = "src/hyperlight_wasm_macro" }
23+
hyperlight-wasm-runtime = { version = "0.13.0", path = "src/hyperlight_wasm_runtime" }

src/hyperlight_wasm/examples/component_example/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn main() {
4848
let mut sb: hyperlight_wasm::ProtoWasmSandbox = hyperlight_wasm::SandboxBuilder::new()
4949
.with_guest_input_buffer_size(70000000)
5050
.with_guest_heap_size(200000000)
51-
.with_guest_stack_size(100000000)
51+
.with_guest_scratch_size(100 * 1024 * 1024)
5252
.build()
5353
.unwrap();
5454
let rt = bindings::register_host_functions(&mut sb, state);

src/hyperlight_wasm/examples/helloworld/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn main() -> Result<()> {
112112
"RoundToInt test case {idx} failed: got {}, expected {}",
113113
result, expected_result
114114
);
115-
loaded_wasm_sandbox.restore(&snapshot)?
115+
loaded_wasm_sandbox.restore(snapshot.clone())?
116116
}
117117
Ok(())
118118
}

src/hyperlight_wasm/examples/interruption/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ fn main() -> Result<()> {
9797

9898
// Recovery option 1: Use restore() to recover the sandbox
9999
println!("\n7. Recovering sandbox using restore()...");
100-
loaded.restore(&snapshot)?;
100+
loaded.restore(snapshot.clone())?;
101101
assert!(!loaded.is_poisoned()?);
102102
println!(" is_poisoned after restore: {}", loaded.is_poisoned()?);
103103

src/hyperlight_wasm/src/sandbox/loaded_wasm_sandbox.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct LoadedWasmSandbox {
4242
// because of this we cannot implement drop without making inner an Option (alternatively we could make MultiUseSandbox Copy but that would introduce other issues)
4343
inner: Option<MultiUseSandbox>,
4444
// The state the sandbox was in before loading a wasm module. Used for transitioning back to a `WasmSandbox` (unloading the wasm module).
45-
runtime_snapshot: Option<Snapshot>,
45+
runtime_snapshot: Option<Arc<Snapshot>>,
4646
}
4747

4848
impl LoadedWasmSandbox {
@@ -86,7 +86,7 @@ impl LoadedWasmSandbox {
8686
/// Returns `Err(HyperlightError::PoisonedSandbox)` if the sandbox is in a
8787
/// poisoned state. Use [`restore()`](Self::restore) with a previously
8888
/// taken snapshot to recover before taking a new snapshot.
89-
pub fn snapshot(&mut self) -> Result<Snapshot> {
89+
pub fn snapshot(&mut self) -> Result<Arc<Snapshot>> {
9090
match &mut self.inner {
9191
Some(inner) => inner.snapshot(),
9292
None => log_then_return!("No inner MultiUseSandbox to snapshot"),
@@ -105,7 +105,7 @@ impl LoadedWasmSandbox {
105105
/// 1. Clear the poisoned state
106106
/// 2. Reset memory to the snapshot state
107107
/// 3. Allow subsequent [`call_guest_function()`](Self::call_guest_function) calls to succeed
108-
pub fn restore(&mut self, snapshot: &Snapshot) -> Result<()> {
108+
pub fn restore(&mut self, snapshot: Arc<Snapshot>) -> Result<()> {
109109
match &mut self.inner {
110110
Some(inner) => inner.restore(snapshot),
111111
None => log_then_return!("No inner MultiUseSandbox to restore"),
@@ -135,7 +135,7 @@ impl LoadedWasmSandbox {
135135

136136
pub(super) fn new(
137137
inner: MultiUseSandbox,
138-
runtime_snapshot: Snapshot,
138+
runtime_snapshot: Arc<Snapshot>,
139139
) -> Result<LoadedWasmSandbox> {
140140
metrics::gauge!(METRIC_ACTIVE_LOADED_WASM_SANDBOXES).increment(1);
141141
metrics::counter!(METRIC_TOTAL_LOADED_WASM_SANDBOXES).increment(1);
@@ -360,7 +360,7 @@ mod tests {
360360
#[test]
361361
fn test_call_guest_functions_with_custom_config_multiple_times() {
362362
let mut sandbox = SandboxBuilder::new()
363-
.with_guest_stack_size(32 * 1024)
363+
.with_guest_scratch_size(32 * 1024)
364364
.with_guest_heap_size(128 * 1024)
365365
.build()
366366
.unwrap();

src/hyperlight_wasm/src/sandbox/sandbox_builder.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ use super::proto_wasm_sandbox::ProtoWasmSandbox;
2222

2323
// use unreasonably large minimum stack/heap/input data sizes for now to
2424
// deal with the size of wasmtime/wasi-libc aot artifacts
25-
pub const MIN_STACK_SIZE: u64 = 64 * 1024;
25+
// use reasonably large minimum scratch/heap/input data sizes
26+
// to deal with the size of wasmtime/wasi-libc aot artifacts
27+
pub const MIN_SCRATCH_SIZE: usize = 2 * 1024 * 1024;
2628
pub const MIN_INPUT_DATA_SIZE: usize = 192 * 1024;
2729
pub const MIN_HEAP_SIZE: u64 = 1024 * 1024;
2830

@@ -39,6 +41,7 @@ impl SandboxBuilder {
3941
let mut config: SandboxConfiguration = Default::default();
4042
config.set_input_data_size(MIN_INPUT_DATA_SIZE);
4143
config.set_heap_size(MIN_HEAP_SIZE);
44+
config.set_scratch_size(MIN_SCRATCH_SIZE);
4245

4346
Self {
4447
config,
@@ -95,13 +98,14 @@ impl SandboxBuilder {
9598
self
9699
}
97100

98-
/// Set the guest stack size
99-
/// This is the size of the stack that code executing in the guest can use.
100-
/// If this value is too small then the guest will fail with a stack overflow error
101-
/// The default value (and minimum) is set to the value of the MIN_STACK_SIZE const.
102-
pub fn with_guest_stack_size(mut self, guest_stack_size: u64) -> Self {
103-
if guest_stack_size > MIN_STACK_SIZE {
104-
self.config.set_stack_size(guest_stack_size);
101+
/// Set the guest scratch size in bytes.
102+
/// The scratch region provides writable memory for the guest, including the
103+
/// dynamically-sized stack. Increase this if your guest code needs deep
104+
/// recursion or large local variables.
105+
/// Values smaller than the default (288 KiB) are ignored.
106+
pub fn with_guest_scratch_size(mut self, guest_scratch_size: usize) -> Self {
107+
if guest_scratch_size > MIN_SCRATCH_SIZE {
108+
self.config.set_scratch_size(guest_scratch_size);
105109
}
106110
self
107111
}

src/hyperlight_wasm/src/sandbox/wasm_sandbox.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ limitations under the License.
1515
*/
1616

1717
use std::path::Path;
18+
use std::sync::Arc;
1819

19-
use hyperlight_host::mem::memory_region::{MemoryRegion, MemoryRegionFlags, MemoryRegionType};
2020
use hyperlight_host::sandbox::snapshot::Snapshot;
2121
use hyperlight_host::{MultiUseSandbox, Result, new_error};
2222

@@ -38,7 +38,7 @@ pub struct WasmSandbox {
3838
inner: Option<MultiUseSandbox>,
3939
// Snapshot of state of an initial WasmSandbox (runtime loaded, but no guest module code loaded).
4040
// Used for LoadedWasmSandbox to be able restore state back to WasmSandbox
41-
snapshot: Option<Snapshot>,
41+
snapshot: Option<Arc<Snapshot>>,
4242
}
4343

4444
const MAPPED_BINARY_VA: u64 = 0x1_0000_0000u64;
@@ -61,8 +61,11 @@ impl WasmSandbox {
6161
/// for example when creating a `WasmSandbox` from a `LoadedWasmSandbox`, since
6262
/// the snapshot has already been created in that case.
6363
/// Expects a snapshot of the state where wasm runtime is loaded, but no guest module code is loaded.
64-
pub(super) fn new_from_loaded(mut loaded: MultiUseSandbox, snapshot: Snapshot) -> Result<Self> {
65-
loaded.restore(&snapshot)?;
64+
pub(super) fn new_from_loaded(
65+
mut loaded: MultiUseSandbox,
66+
snapshot: Arc<Snapshot>,
67+
) -> Result<Self> {
68+
loaded.restore(snapshot.clone())?;
6669
metrics::gauge!(METRIC_ACTIVE_WASM_SANDBOXES).increment(1);
6770
metrics::counter!(METRIC_TOTAL_WASM_SANDBOXES).increment(1);
6871
Ok(WasmSandbox {
@@ -113,19 +116,8 @@ impl WasmSandbox {
113116
.as_mut()
114117
.ok_or_else(|| new_error!("WasmSandbox is None"))?;
115118

116-
let guest_base: usize = MAPPED_BINARY_VA as usize;
117-
let rgn = MemoryRegion {
118-
host_region: base as usize..base.wrapping_add(len) as usize,
119-
guest_region: guest_base..guest_base + len,
120-
flags: MemoryRegionFlags::READ | MemoryRegionFlags::EXECUTE,
121-
region_type: MemoryRegionType::Heap,
122-
};
123-
if let Ok(()) = unsafe { inner.map_region(&rgn) } {
124-
inner.call::<()>("LoadWasmModulePhys", (MAPPED_BINARY_VA, len as u64))?;
125-
} else {
126-
let wasm_bytes = unsafe { std::slice::from_raw_parts(base as *const u8, len).to_vec() };
127-
load_wasm_module_from_bytes(inner, wasm_bytes)?;
128-
}
119+
let wasm_bytes = unsafe { std::slice::from_raw_parts(base as *const u8, len).to_vec() };
120+
load_wasm_module_from_bytes(inner, wasm_bytes)?;
129121

130122
self.finalize_module_load()
131123
}
@@ -390,7 +382,7 @@ mod tests {
390382
assert!(loaded.is_poisoned()?, "Sandbox should be poisoned");
391383

392384
// Restore should recover the sandbox
393-
loaded.restore(&snapshot)?;
385+
loaded.restore(snapshot)?;
394386

395387
assert!(
396388
!loaded.is_poisoned()?,
@@ -526,7 +518,7 @@ mod tests {
526518
let builder = SandboxBuilder::new()
527519
.with_guest_input_buffer_size(0x8000)
528520
.with_guest_output_buffer_size(0x8000)
529-
.with_guest_stack_size(0x2000)
521+
.with_guest_scratch_size(0x2000)
530522
.with_guest_heap_size(0x100000);
531523

532524
let mut sandboxes: Vec<SandboxTest> = Vec::new();

src/hyperlight_wasm_macro/Cargo.lock

Lines changed: 71 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hyperlight_wasm_macro/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mod wasmguest;
2929
#[proc_macro]
3030
pub fn wasm_guest_bindgen(_: proc_macro::TokenStream) -> proc_macro::TokenStream {
3131
let path = std::env::var_os("WIT_WORLD").unwrap();
32-
util::read_wit_type_from_file(path, |kebab_name, ct| {
32+
util::read_wit_type_from_file(path, None, |kebab_name, ct| {
3333
let decls = emit::run_state(true, true, |s| {
3434
// Emit type/trait definitions for all instances in the world
3535
rtypes::emit_toplevel(s, &kebab_name, ct);

0 commit comments

Comments
 (0)