#![allow(clippy::disallowed_macros)]
extern crate hyperlight_host;
use std::sync::{Arc, Barrier};
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
use hyperlight_host::{GuestBinary, Result};
use hyperlight_testing::simple_guest_as_string;
fn fn_writer(_msg: String) -> Result<i32> {
Ok(0)
}
fn main() -> Result<()> {
env_logger::builder()
.parse_filters("none,hyperlight=info")
.init();
let hyperlight_guest_path =
simple_guest_as_string().expect("Cannot find the guest binary at the expected location.");
for _ in 0..20 {
let path = hyperlight_guest_path.clone();
let res: Result<()> = {
let mut usandbox = UninitializedSandbox::new(GuestBinary::FilePath(path), None)?;
usandbox.register_print(fn_writer)?;
let mut multiuse_sandbox = usandbox.evolve()?;
for _ in 0..5 {
multiuse_sandbox
.call::<String>("Echo", "a".to_string())
.unwrap();
}
let msg = "Hello, World!!\n".to_string();
for _ in 0..5 {
multiuse_sandbox
.call::<i32>("PrintOutput", msg.clone())
.unwrap();
}
Ok(())
};
res.unwrap()
}
let usandbox =
UninitializedSandbox::new(GuestBinary::FilePath(hyperlight_guest_path.clone()), None)?;
let mut multiuse_sandbox = usandbox.evolve()?;
let interrupt_handle = multiuse_sandbox.interrupt_handle();
let barrier = Arc::new(Barrier::new(2));
let barrier2 = barrier.clone();
const NUM_CALLS: i32 = 5;
let thread = std::thread::spawn(move || {
for _ in 0..NUM_CALLS {
barrier2.wait();
std::thread::sleep(std::time::Duration::from_millis(500));
interrupt_handle.kill();
}
});
for _ in 0..NUM_CALLS {
barrier.wait();
multiuse_sandbox.call::<()>("Spin", ()).unwrap_err();
}
thread.join().unwrap();
Ok(())
}