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
3 changes: 2 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,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
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(())
}
2 changes: 2 additions & 0 deletions src/wasm_runtime/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ rustflags = [
"code-model=small",
"-C",
"link-args=-e entrypoint",
"-C",
"target-feature=-soft-float,+sse,+sse2"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

turning off soft-float might cause issues with

ReturnValue::Float(f) => Ok(Val::F32(f.to_bits())),
ReturnValue::Double(f) => Ok(Val::F64(f.to_bits())),

]
linker = "rust-lld"

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