#![allow(clippy::disallowed_macros)]
use tracing::{Level, span};
extern crate hyperlight_host;
use std::sync::{Arc, Barrier};
use std::thread::{JoinHandle, spawn};
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
use hyperlight_host::{GuestBinary, Result};
use hyperlight_testing::simple_guest_as_string;
use tracing_forest::ForestLayer;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{EnvFilter, Layer, Registry};
use uuid::Uuid;
fn fn_writer(_msg: String) -> Result<i32> {
Ok(0)
}
fn main() -> Result<()> {
let layer = ForestLayer::default()
.with_filter(EnvFilter::builder().parse("none,hyperlight=info").unwrap());
Registry::default().with(layer).init();
run_example()
}
fn run_example() -> Result<()> {
let hyperlight_guest_path =
simple_guest_as_string().expect("Cannot find the guest binary at the expected location.");
let mut join_handles: Vec<JoinHandle<Result<()>>> = vec![];
let span = span!(Level::INFO, "hyperlight tracing example");
let _entered = span.enter();
for i in 0..10 {
let path = hyperlight_guest_path.clone();
let handle = spawn(move || -> Result<()> {
let id = Uuid::new_v4();
let span = span!(
Level::INFO,
"hyperlight tracing example thread",
context = format!("Thread number {}", i),
uuid = %id,
);
let _entered = span.enter();
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(())
});
join_handles.push(handle);
}
let usandbox =
UninitializedSandbox::new(GuestBinary::FilePath(hyperlight_guest_path.clone()), None)?;
let mut multiuse_sandbox = usandbox.evolve()?;
let interrupt_handle = multiuse_sandbox.interrupt_handle();
const NUM_CALLS: i32 = 5;
let barrier = Arc::new(Barrier::new(2));
let barrier2 = barrier.clone();
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 i in 0..NUM_CALLS {
let id = Uuid::new_v4();
let span = span!(
Level::INFO,
"hyperlight tracing call cancellation example thread",
context = format!("Thread number {}", i),
uuid = %id,
);
let _entered = span.enter();
barrier.wait();
multiuse_sandbox.call::<()>("Spin", ()).unwrap_err();
}
for join_handle in join_handles {
let result = join_handle.join();
assert!(result.is_ok());
}
thread.join().unwrap();
Ok(())
}