Skip to content
Merged
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
12 changes: 0 additions & 12 deletions .github/workflows/dep_cargo_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ jobs:
with:
rust-toolchain: "1.85.0"

- name: Download Wasm Host (debug)
uses: actions/download-artifact@v4
with:
name: wasm-runtime-debug
path: ${{ github.workspace }}/src/hyperlight_wasm/redist/debug/

- name: Download Wasm Host (release)
uses: actions/download-artifact@v4
with:
name: wasm-runtime-release
path: ${{ github.workspace }}/src/hyperlight_wasm/redist/release/

# github actions that run against PRs check out a ref to the PR merge branch
# we need to switch / create a branch for cargo ws to run late
- name: set-branch-for-PRs
Expand Down
44 changes: 44 additions & 0 deletions Cargo.lock

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

20 changes: 13 additions & 7 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ wit-world := if os() == "windows" { "$env:WIT_WORLD=\"" + justfile_directory() +

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

make-vendor-tar:
tar cf ./src/hyperlight_wasm/vendor.tar \
--exclude-vcs-ignores \
-C ./src wasm_runtime hyperlight_wasm_macro

ensure-tools:
cargo install --locked wasm-tools --version 1.235.0
cargo install cargo-component --locked --version 0.21.1
Expand All @@ -19,16 +24,12 @@ build target=default-target features="": (build-wasm-runtime target) (fmt-check)
mkdir-redist target=default-target:
mkdir {{ mkdir-arg }} x64
mkdir {{ mkdir-arg }} x64/{{ target }}
mkdir {{ mkdir-arg }} src/hyperlight_wasm/redist
mkdir {{ mkdir-arg }} src/hyperlight_wasm/redist/{{ target }}

build-wasm-runtime target=default-target: (mkdir-redist target)
cd ./src/wasm_runtime && cargo build --verbose --profile={{ if target == "debug" {"dev"} else { target } }}
cp ./src/wasm_runtime/target/x86_64-unknown-none/{{target}}/wasm_runtime ./x64/{{target}}/wasm_runtime
cp ./src/wasm_runtime/target/x86_64-unknown-none/{{target}}/wasm_runtime ./src/hyperlight_wasm/redist/{{target}}/wasm_runtime
build-wasm-runtime target=default-target:
cd ./src/wasm_runtime && cargo build --verbose --profile={{ if target == "debug" {"dev"} else { target } }} && rm -R target

build-wasm-examples target=default-target:
{{ build-wasm-examples-command}} {{target}}
{{ build-wasm-examples-command }} {{target}}

build-rust-wasm-examples target=default-target: (mkdir-redist target)
rustup target add wasm32-unknown-unknown
Expand All @@ -50,24 +51,29 @@ check target=default-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/hyperlight_wasm_macro && cargo check --profile={{ if target == "debug" {"dev"} else { target } }}

fmt-check:
rustup toolchain install nightly -c rustfmt && cargo +nightly fmt -v --all -- --check
cd src/rust_wasm_samples && rustup toolchain install nightly -c rustfmt && cargo +nightly fmt -v --all -- --check
cd src/component_sample && rustup toolchain install nightly -c rustfmt && cargo +nightly fmt -v --all -- --check
cd src/wasm_runtime && rustup toolchain install nightly -c rustfmt && cargo +nightly fmt -v --all -- --check
cd src/hyperlight_wasm_macro && rustup toolchain install nightly -c rustfmt && cargo +nightly fmt -v --all -- --check

fmt:
rustup toolchain install nightly -c rustfmt
cargo +nightly fmt --all
cd src/rust_wasm_samples && cargo +nightly fmt -v --all
cd src/component_sample && cargo +nightly fmt -v --all
cd src/wasm_runtime && cargo +nightly fmt -v --all
cd src/hyperlight_wasm_macro && cargo +nightly fmt -v --all

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/hyperlight_wasm_macro && cargo clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings

# TESTING
# Metrics tests cannot run with other tests they are marked as ignored so that cargo test works
Expand Down
1 change: 1 addition & 0 deletions src/hyperlight_wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor.tar
13 changes: 12 additions & 1 deletion src/hyperlight_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ name = "hyperlight-wasm"
version = "0.1.0"
edition = "2024"
rust-version = "1.85"
include = ["*"] # Make sure wasm_runtime is included!
description = "Library that enables wasm modules and components to be run inside lightweight Virtual Machine backed Sandbox. It is built on top of Hyperlight."
license = "Apache-2.0"
repository = "https://github.com/hyperlight-dev/hyperlight-wasm"
include = [
"/src",
"/scripts",
"/examples",
"benches",
"/build.rs",
"/vendor.tar", # Make sure vendor.tar is included!
]

[lib]
name = "hyperlight_wasm"
Expand Down Expand Up @@ -59,6 +69,7 @@ blake3 = "1.8"
built = { version = "0.8.0", features = ["chrono", "git2"] }
anyhow = { version = "1.0" }
goblin = "0.10.0"
tar = "0.4.44"

[features]
default = ["function_call_metrics", "kvm", "mshv2"]
Expand Down
90 changes: 76 additions & 14 deletions src/hyperlight_wasm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,83 @@ use std::{env, fs};
use anyhow::Result;
use built::write_built_file;

fn get_wasm_runtime_path() -> PathBuf {
let manifest_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap();
let manifest_dir = PathBuf::from(manifest_dir);

let tar_path = manifest_dir.join("vendor.tar");

let out_dir = env::var_os("OUT_DIR").unwrap();
let out_dir = PathBuf::from(out_dir);
let vendor_dir = out_dir.join("vendor");

if vendor_dir.exists() {
fs::remove_dir_all(&vendor_dir).unwrap();
}

println!("cargo::rerun-if-changed={}", tar_path.display());

// If the vendor.tar file exists, extract it to the OUT_DIR/vendor directory
// and return the wasm_runtime directory inside it.
// This is useful for vendoring the wasm_runtime crate in a release build, since crates.io
// does not allow vendoring folders with Cargo.toml files (i.e., other crates).
// The vendor.tar file is expected to be in the same directory as this build script.
if tar_path.exists() {
let out_dir = env::var_os("OUT_DIR").unwrap();
let out_dir = PathBuf::from(out_dir);
let vendor_dir = out_dir.join("vendor");

let mut tar = tar::Archive::new(fs::File::open(&tar_path).unwrap());
tar.unpack(&vendor_dir).unwrap();

let wasm_runtime_dir = vendor_dir.join("wasm_runtime");

println!(
"cargo::warning=using vendor wasm_runtime from {}",
tar_path.display()
);
return wasm_runtime_dir;
}

let crates_dir = manifest_dir.parent().unwrap();

#[cfg(unix)]
std::os::unix::fs::symlink(crates_dir, &vendor_dir).unwrap();

#[cfg(not(unix))]
std::os::windows::fs::symlink_dir(crates_dir, &vendor_dir).unwrap();

let wasm_runtime_dir = crates_dir.join("wasm_runtime");
if wasm_runtime_dir.exists() {
return wasm_runtime_dir;
}

panic!(
r#"
The wasm_runtime directory not found in the expected locations.
If you are using hyperlight-wasm from a crates.io release, please file an issue: https://github.com/hyperlight-dev/hyperlight-wasm/issues
"#
);
}

fn build_wasm_runtime() -> PathBuf {
let proj_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap();
let cargo_bin = env::var_os("CARGO").unwrap();
let profile = env::var_os("PROFILE").unwrap();
let out_dir = env::var_os("OUT_DIR").unwrap();

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

let in_repo_dir = get_wasm_runtime_path();

let in_repo_dir = PathBuf::from(&proj_dir)
.parent()
.unwrap_or_else(|| panic!("could not find parent of cargo manifest directory"))
.join("wasm_runtime");
if !in_repo_dir.exists() {
panic!("hyperlight_wasm does not yet support being compiled from a release package");
panic!("missing wasm_runtime in-tree dependency");
}
print!("cargo::rerun-if-changed=");
let _ = std::io::stdout()
.write_all(AsRef::<std::ffi::OsStr>::as_ref(&in_repo_dir).as_encoded_bytes());
println!();

println!("cargo::rerun-if-changed={}", in_repo_dir.display());
println!("cargo::rerun-if-env-changed=WIT_WORLD");
// the PROFILE env var unfortunately only gives us 1 bit of "dev or release"
let cargo_profile = if profile == "debug" { "dev" } else { "release" };
let mut cmd = std::process::Command::new("cargo");
let mut cmd = std::process::Command::new(cargo_bin);

// Clear the variables that control Cargo's behaviour (as listed
// at https://doc.rust-lang.org/cargo/reference/environment-variables.html):
Expand All @@ -57,7 +115,12 @@ fn build_wasm_runtime() -> PathBuf {
env_vars.retain(|(key, _)| !key.starts_with("CARGO_"));

let cmd = cmd
.args(["build", &format!("--profile={}", cargo_profile), "-v"])
.arg("build")
.arg("--profile")
.arg(cargo_profile)
.arg("-v")
.arg("--target-dir")
.arg(&target_dir)
.current_dir(&in_repo_dir)
.env_clear()
.envs(env_vars);
Expand All @@ -68,8 +131,7 @@ fn build_wasm_runtime() -> PathBuf {
if !status.success() {
panic!("could not compile wasm_runtime");
}
let resource = in_repo_dir
.join("target")
let resource = target_dir
.join("x86_64-unknown-none")
.join(profile)
.join("wasm_runtime");
Expand Down
6 changes: 2 additions & 4 deletions src/hyperlight_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ mod tests {
fn test_wasmtime_version() {
let wasmtime_version = super::get_wasmtime_version();
// get the wasmtime version from the wasm_runtime binary's Cargo.toml
let proj_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap();
let cargo_toml_path = Path::new(&proj_dir)
.parent()
.unwrap()
let cargo_toml_path = Path::new(env!("OUT_DIR"))
.join("vendor")
.join("wasm_runtime")
.join("Cargo.toml");
let cargo_toml_content =
Expand Down
6 changes: 1 addition & 5 deletions src/hyperlight_wasm/src/sandbox/wasm_sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,7 @@ mod tests {
.expect("Failed to get CARGO_MANIFEST_DIR or RUST_DIR_FOR_DEBUGGING_TESTS env var")
});

let relative_path = if filename == "wasm_runtime" {
"redist"
} else {
"../../x64"
};
let relative_path = "../../x64";

let filename_path = Path::new(&proj_dir)
.join(relative_path)
Expand Down
Loading
Loading