Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -478,5 +478,8 @@ target/

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
src/component_sample/**/*.wasm

# WebAssembly build artifacts
*.wasm

src/wasmsamples/components/bindings/
45 changes: 38 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mkdir-arg := if os() == "windows" { "-Force" } else { "-p" }
latest-release:= if os() == "windows" {"$(git tag -l --sort=v:refname | select -last 2 | select -first 1)"} else {`git tag -l --sort=v:refname | tail -n 2 | head -n 1`}
wit-world := if os() == "windows" { "$env:WIT_WORLD=\"" + justfile_directory() + "\\src\\component_sample\\wit\\component-world.wasm" + "\";" } else { "WIT_WORLD=" + justfile_directory() + "/src/component_sample/wit/component-world.wasm" }
wit-world-c := if os() == "windows" { "$env:WIT_WORLD=\"" + justfile_directory() + "\\src\\wasmsamples\\components\\runcomponent-world.wasm" + "\";" } else { "WIT_WORLD=" + justfile_directory() + "/src/wasmsamples/components/runcomponent-world.wasm" }
wit-world-monte-carlo := if os() == "windows" { "$env:WIT_WORLD=\"" + justfile_directory() + "\\src\\wasip2_guest\\monte-carlo-world.wasm" + "\";" } else { "WIT_WORLD=" + justfile_directory() + "/src/wasip2_guest/monte-carlo-world.wasm" }

set windows-shell := ["pwsh.exe", "-NoLogo", "-Command"]

Expand Down Expand Up @@ -32,6 +33,7 @@ mkdir-redist target=default-target:
compile-wit:
wasm-tools component wit ./src/wasmsamples/components/runcomponent.wit -w -o ./src/wasmsamples/components/runcomponent-world.wasm
wasm-tools component wit ./src/component_sample/wit/example.wit -w -o ./src/component_sample/wit/component-world.wasm
wasm-tools component wit ./src/wasip2_guest/wit/monte-carlo.wit -w -o ./src/wasip2_guest/monte-carlo-world.wasm

build-examples target=default-target features="": (build-wasm-examples target features) (build-rust-wasm-examples target features) (build-rust-component-examples target features)

Expand All @@ -50,6 +52,13 @@ build-rust-component-examples target=default-target features="": (compile-wit)
cd ./src/component_sample && cargo component build --target wasm32-unknown-unknown --profile={{ if target == "debug" {"dev"} else { target } }}
cargo run {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--features " + features } }} -p hyperlight-wasm-aot compile {{ if features =~ "gdb" {"--debug"} else {""} }} --component ./src/component_sample/target/wasm32-unknown-unknown/{{ target }}/component_sample.wasm ./x64/{{ target }}/component_sample.aot

build-monte-carlo-example features="": (compile-wit) (mkdir-redist "release")
# Monte Carlo Pi example using native wasm32-wasip2 + wit-bindgen
# Always build in release mode to avoid WASI dependencies (debug mode pulls in entire WASI for some reason)
rustup target add wasm32-wasip2
cd ./src/wasip2_guest && cargo build --lib --target wasm32-wasip2 --release
cargo run {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--features " + features } }} -p hyperlight-wasm-aot compile {{ if features =~ "gdb" {"--debug"} else {""} }} --component ./src/wasip2_guest/target/wasm32-wasip2/release/monte_carlo.wasm ./x64/release/monte_carlo.aot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we update the existing to use wasip2 target?

hyperlight-wasm/Justfile

Lines 47 to 49 in 192a036

# use cargo component so we don't get all the wasi imports https://github.com/bytecodealliance/cargo-component?tab=readme-ov-file#relationship-with-wasm32-wasip2
# we also explicitly target wasm32-unknown-unknown since cargo component might try to pull in wasi imports https://github.com/bytecodealliance/cargo-component/issues/290
rustup target add wasm32-unknown-unknown
Looks like there were similar issues and now the custom wit works.

Copy link
Contributor Author

@ludfjig ludfjig Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently it only works in release mode, so I would say let's wait a bit longer. in debug profile, it still does bring in entire wasi


check target=default-target:
cargo check --profile={{ if target == "debug" {"dev"} else { target } }}
cd src/rust_wasm_samples && cargo check --profile={{ if target == "debug" {"dev"} else { target } }}
Expand Down Expand Up @@ -99,9 +108,10 @@ examples-ci target=default-target features="": (build-rust-wasm-examples target)
cargo run {{ if features =="" {''} else {"--no-default-features -F function_call_metrics," + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} --example metrics
cargo run {{ if features =="" {"--no-default-features --features kvm,mshv3"} else {"--no-default-features -F function_call_metrics," + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} --example metrics

examples-components target=default-target features="": (build-rust-component-examples target)
examples-components target=default-target features="": (build-rust-component-examples target) (build-monte-carlo-example)
{{ wit-world }} cargo run {{ if features =="" {''} else {"--no-default-features -F kvm -F " + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} --example component_example
{{ wit-world-c }} cargo run {{ if features =="" {''} else {"--no-default-features -F kvm -F " + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} --example c-component
{{ wit-world-monte-carlo }} cargo run {{ if features =="" {''} else {"--no-default-features -F kvm -F " + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} --example monte_carlo_example

# warning, compares to and then OVERWRITES the given baseline
bench-ci baseline target="release" features="":
Expand Down
6 changes: 6 additions & 0 deletions src/hyperlight_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ name = "interruption"
path = "examples/interruption/main.rs"
test = true

[[example]]
name = "monte_carlo_example"
path = "examples/monte_carlo_example/main.rs"
test = true

[dependencies]
hyperlight-host = { workspace = true }
libc = { version = "0.2.180" }
Expand Down Expand Up @@ -92,6 +97,7 @@ tracing = "0.1.44"
tracing-subscriber = {version = "0.3.22", features = ["std", "env-filter"]}
tracing-opentelemetry = "0.32.1"
uuid = { version = "1.19.0", features = ["v4"] }
rand = "0.8"

[build-dependencies]
cfg_aliases = "0.2.1"
Expand Down
105 changes: 105 additions & 0 deletions src/hyperlight_wasm/examples/monte_carlo_example/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Copyright 2024 The Hyperlight Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

//! Monte Carlo Pi Estimator - Host Example
//!
//! Demonstrates running a WASI P2 component built with native `wasm32-wasip2`
//! target and `wit-bindgen`. The host provides a random number generator
//! that the guest component imports to estimate Pi.
//!
//! Build the guest first: `just build-monte-carlo-example`
//! Run: `just examples-components`

extern crate alloc;

use std::env;
use std::path::Path;

use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};

mod bindings {
hyperlight_component_macro::host_bindgen!("../wasip2_guest/monte-carlo-world.wasm");
}

/// Host-provided RNG state for the guest component.
///
/// The guest cannot generate randomness in `no_std` mode, so the host
/// provides random numbers via the imported `random` interface.
struct State {
rng: StdRng,
}

impl State {
fn new() -> Self {
State {
rng: StdRng::from_entropy(),
}
}
}

impl bindings::my::monte_carlo::Random for State {
fn random(&mut self) -> f32 {
self.rng.r#gen::<f32>()
}
}

#[allow(refining_impl_trait)]
impl bindings::my::monte_carlo::EstimatorImports for State {
type Random = State;

fn random(&mut self) -> &mut Self::Random {
self
}
}

fn main() {
let state = State::new();

let mut sb: hyperlight_wasm::ProtoWasmSandbox = hyperlight_wasm::SandboxBuilder::new()
.with_guest_input_buffer_size(1000000)
.with_guest_heap_size(10000000)
.with_guest_stack_size(1000000)
.build()
.unwrap();

let rt = bindings::register_host_functions(&mut sb, state);

let sb = sb.load_runtime().unwrap();

// Always look in x64/release since the guest is always built in release mode
// (debug mode pulls in WASI dependencies that we don't support)
let proj_dir = env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
let mod_path = Path::new(&proj_dir)
.join("../../x64/release/monte_carlo.aot")
.canonicalize()
.expect("Failed to find monte_carlo.aot - run 'just build-monte-carlo-example' first");
let sb = sb.load_module(mod_path).unwrap();

let mut wrapped = bindings::EstimatorSandbox { sb, rt };

// Estimate pi with different sample sizes
for samples in [100, 1000, 10000] {
let pi_estimate =
bindings::my::monte_carlo::EstimatorExports::estimate_pi(&mut wrapped, samples);
println!(
"Pi estimate with {} samples: {:.6} (error: {:.6})",
samples,
pi_estimate,
(pi_estimate - std::f32::consts::PI).abs()
);
}
}
Loading