extern crate hyperlight_host;
use std::sync::{Arc, Mutex};
use std::thread::{spawn, JoinHandle};
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
use hyperlight_host::sandbox_state::transition::Noop;
use hyperlight_host::{GuestBinary, MultiUseSandbox, Result};
use hyperlight_testing::simple_guest_as_string;
fn main() {
let prometheus_handle = metrics_exporter_prometheus::PrometheusBuilder::new()
.install_recorder()
.expect("Failed to install Prometheus exporter");
do_hyperlight_stuff();
let payload = prometheus_handle.render();
println!("Prometheus metrics:\n{}", payload);
}
fn do_hyperlight_stuff() {
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![];
for _ in 0..20 {
let path = hyperlight_guest_path.clone();
let writer_func = Arc::new(Mutex::new(fn_writer));
let handle = spawn(move || -> Result<()> {
let usandbox = UninitializedSandbox::new(
GuestBinary::FilePath(path),
None,
None,
Some(&writer_func),
)?;
let no_op = Noop::<UninitializedSandbox, MultiUseSandbox>::default();
let mut multiuse_sandbox = usandbox.evolve(no_op).expect("Failed to evolve sandbox");
for _ in 0..5 {
let result = multiuse_sandbox.call_guest_function_by_name(
"Echo",
ReturnType::String,
Some(vec![ParameterValue::String("a".to_string())]),
);
assert!(result.is_ok());
}
let msg = "Hello, World!!\n".to_string();
for _ in 0..5 {
let result = multiuse_sandbox.call_guest_function_by_name(
"PrintOutput",
ReturnType::Int,
Some(vec![ParameterValue::String(msg.clone())]),
);
assert!(result.is_ok());
}
Ok(())
});
join_handles.push(handle);
}
let usandbox = UninitializedSandbox::new(
GuestBinary::FilePath(hyperlight_guest_path.clone()),
None,
None,
None,
)
.expect("Failed to create UninitializedSandbox");
let no_op = Noop::<UninitializedSandbox, MultiUseSandbox>::default();
let mut multiuse_sandbox = usandbox.evolve(no_op).expect("Failed to evolve sandbox");
for _ in 0..5 {
let mut ctx = multiuse_sandbox.new_call_context();
let result = ctx.call("Spin", ReturnType::Void, None);
assert!(result.is_err());
let result = ctx.finish();
assert!(result.is_ok());
multiuse_sandbox = result.unwrap();
}
for join_handle in join_handles {
let result = join_handle.join();
assert!(result.is_ok());
}
}
fn fn_writer(_msg: String) -> Result<i32> {
Ok(0)
}