Skip to content
Open
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
4 changes: 3 additions & 1 deletion .github/workflows/Benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ jobs:

- name: Build Wasm Runtime Binary
working-directory: ./src/hyperlight_wasm
run: just build-wasm-runtime ${{ matrix.config }}
run: |
just ensure-tools
just build-wasm-runtime ${{ matrix.config }}

- uses: dtolnay/rust-toolchain@1.89
with:
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/dep_rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ jobs:

- name: Build Wasm Runtime Binary
working-directory: ./src/hyperlight_wasm
run: just build-wasm-runtime ${{ matrix.config }}
run: |
just ensure-tools
just build-wasm-runtime ${{ matrix.config }}
- name: Download Wasm Modules
uses: actions/download-artifact@v5
Expand All @@ -81,7 +83,6 @@ jobs:
run: |
# this must be build before the formatting and other jobs run
# because the component model example depends on the wasm component built here
just ensure-tools
just compile-wit
just build-rust-component-examples ${{ matrix.config }}
Expand Down
16 changes: 10 additions & 6 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ ensure-tools:
cargo install wasm-tools --locked --version 1.235.0
cargo install cargo-component --locked --version 0.21.1
cargo install wit-bindgen-cli --locked --version 0.43.0
cargo install --git https://github.com/hyperlight-dev/cargo-hyperlight cargo-hyperlight
Copy link
Contributor

Choose a reason for hiding this comment

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

you should be able to use published version, it already includes the CPU features changes

Suggested change
cargo install --git https://github.com/hyperlight-dev/cargo-hyperlight cargo-hyperlight
cargo install cargo-hyperlight


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

build target=default-target features="": (fmt-check)
cargo build {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --verbose --profile={{ if target == "debug" {"dev"} else { target } }}
Expand All @@ -33,9 +34,11 @@ compile-wit:
wasm-tools component wit ./src/component_sample/wit/example.wit -w -o ./src/component_sample/wit/component-world.wasm

build-wasm-runtime target=default-target features="":
cd ./src/wasm_runtime && cargo build --verbose {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--features " + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} && rm -R target
cd ./src/wasm_runtime && cargo hyperlight build --verbose {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--features " + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} && rm -R target

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

build-wasm-examples target=default-target features="": (compile-wit)
{{ build-wasm-examples-command }} {{target}} {{features}}

build-rust-wasm-examples target=default-target features="": (mkdir-redist target)
Expand All @@ -54,7 +57,7 @@ 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 } }}
cd src/component_sample && cargo check --profile={{ if target == "debug" {"dev"} else { target } }}
cd src/wasm_runtime && cargo check --profile={{ if target == "debug" {"dev"} else { target } }}
cd src/wasm_runtime && cargo hyperlight check --profile={{ if target == "debug" {"dev"} else { target } }}
cd src/hyperlight_wasm_macro && cargo check --profile={{ if target == "debug" {"dev"} else { target } }}

fmt-check:
Expand All @@ -79,7 +82,7 @@ clippy target=default-target: (check target)
cargo clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings
cd src/rust_wasm_samples && cargo clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings
cd src/component_sample && cargo clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings
cd src/wasm_runtime && cargo clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings
cd src/wasm_runtime && cargo hyperlight clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings
cd src/hyperlight_wasm_macro && cargo clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings

# TESTING
Expand All @@ -99,7 +102,8 @@ examples-ci target=default-target features="": (build-rust-wasm-examples target)
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)
{{ wit-world }} cargo run {{ if features =="" {''} else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} --example component_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

# warning, compares to and then OVERWRITES the given baseline
bench-ci baseline target="release" features="":
Expand Down
23 changes: 5 additions & 18 deletions src/hyperlight_wasm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,14 @@ limitations under the License.
// this file is included in lib.rs.
// The wasm_runtime binary is expected to be in the x64/{config} directory.

use std::ffi::OsString;
use std::fs::OpenOptions;
use std::io::Write;
use std::iter::once;
use std::path::{Path, PathBuf};
use std::{env, fs};

use anyhow::Result;
use built::write_built_file;

fn path_with(path: impl Into<PathBuf>) -> OsString {
let path = path.into();
let paths = env::var_os("PATH").unwrap_or_default();
let paths = env::split_paths(&paths);
let paths = once(path).chain(paths);
env::join_paths(paths).unwrap()
}

fn get_wasm_runtime_path() -> PathBuf {
let manifest_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap();
let manifest_dir = PathBuf::from(manifest_dir);
Expand Down Expand Up @@ -105,7 +95,6 @@ fn build_wasm_runtime() -> PathBuf {
let out_dir = env::var_os("OUT_DIR").unwrap();

let target_dir = Path::new("").join(&out_dir).join("target");
let toolchain_dir = Path::new("").join(&out_dir).join("toolchain");

let in_repo_dir = get_wasm_runtime_path();

Expand All @@ -126,19 +115,17 @@ fn build_wasm_runtime() -> PathBuf {

let mut cargo_cmd = std::process::Command::new(&cargo_bin);
let mut cmd = cargo_cmd
.arg("hyperlight")
.arg("build")
.arg("--target-dir")
.arg(&target_dir)
.arg("--profile")
.arg(cargo_profile)
.arg("-v")
.arg("--target-dir")
.arg(&target_dir)
.current_dir(&in_repo_dir)
.env_clear()
// On windows when `gdb` features is enabled this is not set correctly
.env("CFLAGS_x86_64_unknown_none", "-fPIC")
.envs(env_vars)
.env("PATH", path_with(&toolchain_dir))
.env("HYPERLIGHT_GUEST_TOOLCHAIN_ROOT", &toolchain_dir);
.env("CFLAGS_x86_64_unknown_none", "-fPIC"); // Should this go on cargo hyperlight

// Add --features gdb if the gdb feature is enabled for this build script
if std::env::var("CARGO_FEATURE_GDB").is_ok() {
Expand All @@ -156,7 +143,7 @@ fn build_wasm_runtime() -> PathBuf {
panic!("could not compile wasm_runtime");
}
let resource = target_dir
.join("x86_64-unknown-none")
.join("x86_64-hyperlight-none")
.join(profile)
.join("wasm_runtime");

Expand Down
68 changes: 68 additions & 0 deletions src/hyperlight_wasm/examples/c-component/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use examples_common::get_wasm_module_path;
use hyperlight_wasm::SandboxBuilder;

use crate::bindings::example::runcomponent::Guest;

extern crate alloc;
mod bindings {
hyperlight_component_macro::host_bindgen!(
"../../src/wasmsamples/components/runcomponent-world.wasm"
);
}

pub struct State {}
impl State {
pub fn new() -> Self {
State {}
}
}

impl Default for State {
fn default() -> Self {
Self::new()
}
}

impl bindings::example::runcomponent::Host for State {
fn r#get_time_since_boot_microsecond(&mut self) -> i64 {
let res = std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.unwrap()
.as_micros();
i64::try_from(res).unwrap()
}
}

impl bindings::example::runcomponent::RuncomponentImports for State {
type Host = State;

fn r#host(&mut self) -> impl ::core::borrow::BorrowMut<Self::Host> {
self
}
}

fn main() {
let state = State::new();
let mut sandbox = SandboxBuilder::new()
.with_guest_input_buffer_size(70000000)
.with_guest_heap_size(200000000)
.with_guest_stack_size(100000000)
//.with_debugging_enabled(8080)
.build()
.unwrap();
let rt = bindings::register_host_functions(&mut sandbox, state);

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

let mod_path = get_wasm_module_path("runcomponent.aot").unwrap();
let sb = sb.load_module(mod_path).unwrap();

let mut wrapped = bindings::RuncomponentSandbox { sb, rt };
let instance = bindings::example::runcomponent::RuncomponentExports::guest(&mut wrapped);
let echo = instance.echo("Hello World!".to_string());
println!("{}", echo);

let result = instance.round_to_nearest_int(1.331, 24.0);
println!("rounded result {}", result);
assert_eq!(result, 32);
}
32 changes: 32 additions & 0 deletions src/hyperlight_wasm/examples/helloworld/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,37 @@ fn main() -> Result<()> {
);
}
}

let tests = [
(1.331, 24.0, 32),
(std::f32::consts::PI, std::f32::consts::E, 9),
(-5.7, 10.3, -59),
(0.0, 0.0, 0),
(99.999, 0.001, 0),
(-std::f32::consts::PI, -2.86, 9),
(1.5, 1.5, 2),
];
let mut sandbox = SandboxBuilder::new().build()?;
sandbox
.register(
"GetTimeSinceBootMicrosecond",
get_time_since_boot_microsecond,
)
.unwrap();
let wasm_sandbox = sandbox.load_runtime()?;
let mod_path = get_wasm_module_path("RunWasm.aot")?;
let mut loaded_wasm_sandbox = wasm_sandbox.load_module(mod_path)?;
let snapshot = loaded_wasm_sandbox.snapshot()?;

for (idx, case) in tests.iter().enumerate() {
let (a, b, expected_result): (f32, f32, i32) = *case;
let result: i32 = loaded_wasm_sandbox.call_guest_function("RoundToNearestInt", (a, b))?;
assert_eq!(
result, expected_result,
"RoundToInt test case {idx} failed: got {}, expected {}",
result, expected_result
);
loaded_wasm_sandbox.restore(&snapshot)?
}
Ok(())
}
21 changes: 0 additions & 21 deletions src/wasm_runtime/.cargo/config.toml

This file was deleted.

12 changes: 1 addition & 11 deletions src/wasm_runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

use std::path::{Path, PathBuf};
use std::path::Path;
use std::{env, fs};

use cargo_metadata::{MetadataCommand, Package};

fn main() {
let toolchain_dir = env::var_os("HYPERLIGHT_GUEST_TOOLCHAIN_ROOT").unwrap();
let toolchain_dir = PathBuf::from(toolchain_dir);
let clang_path = toolchain_dir.join("clang");

assert!(
clang_path.exists(),
"could not find clang at {clang_path:?}"
);

println!("cargo:rerun-if-changed=.");
let mut cfg = cc::Build::new();

Expand Down Expand Up @@ -63,7 +54,6 @@ fn main() {

cfg.include("src/include");
cfg.file("src/platform.c");
cfg.compiler(clang_path);
if cfg!(windows) {
env::set_var("AR_x86_64_unknown_none", "llvm-ar");
}
Expand Down
10 changes: 10 additions & 0 deletions src/wasmsamples/RunWasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
#include <limits.h>
#include <stdint.h>
#include <ctype.h>
#include <math.h>

int HostPrint(char* msg); // Implementation of this will be available in the native host

Expand Down Expand Up @@ -129,3 +130,12 @@ int KeepCPUBusy(int ms)
printf("Kept CPU busy for %d ms using %d iterations of fib(10) %d|toreach max = %d|", ms, iter, INT_MAX, INT_MAX-iter);
return ms;
}

__attribute__((export_name("RoundToNearestInt")))
int RoundToNearestInt(float a, float b)
{
float c = a*b;
float r = lrintf(c);
printf("rounded answer: %f\n", r);
return r;
}
8 changes: 8 additions & 0 deletions src/wasmsamples/components/runcomponent.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#include "bindings/runcomponent.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>

void exports_example_runcomponent_guest_echo(runcomponent_string_t *msg, runcomponent_string_t *ret)
{
ret->len = msg->len;
ret->ptr = (uint8_t *) malloc(ret->len);
memcpy(ret->ptr, msg->ptr, ret->len);
runcomponent_string_free(msg);
}

int32_t exports_example_runcomponent_guest_round_to_nearest_int(float a, float b)
{
float c = a*b;
float r = lrintf(c);
return r;
}
1 change: 1 addition & 0 deletions src/wasmsamples/components/runcomponent.wit
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package example:runcomponent;

interface guest {
echo: func(msg: string) -> string;
round-to-nearest-int: func(a: f32, b: f32) -> s32;
}

interface host {
Expand Down
Loading