use hyperlight_host::func::HostFunction1;
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
use hyperlight_host::sandbox_state::transition::Noop;
use hyperlight_host::{GuestBinary, MultiUseSandbox, Result, UninitializedSandbox};
use hyperlight_testing::{
c_callback_guest_as_string, c_simple_guest_as_string, callback_guest_as_string,
simple_guest_as_string,
};
pub fn new_uninit() -> Result<UninitializedSandbox> {
UninitializedSandbox::new(
GuestBinary::FilePath(get_c_or_rust_simpleguest_path()),
None,
None,
None,
)
}
pub fn new_uninit_rust() -> Result<UninitializedSandbox> {
UninitializedSandbox::new(
GuestBinary::FilePath(simple_guest_as_string().unwrap()),
None,
None,
None,
)
}
pub fn get_simpleguest_sandboxes(
writer: Option<&dyn HostFunction1<String, i32>>, ) -> Vec<MultiUseSandbox> {
let elf_path = get_c_or_rust_simpleguest_path();
let exe_path = format!("{elf_path}.exe");
vec![
UninitializedSandbox::new(GuestBinary::FilePath(elf_path.clone()), None, None, writer)
.unwrap()
.evolve(Noop::default())
.unwrap(),
UninitializedSandbox::new(GuestBinary::FilePath(exe_path.clone()), None, None, writer)
.unwrap()
.evolve(Noop::default())
.unwrap(),
#[cfg(inprocess)]
UninitializedSandbox::new(
GuestBinary::FilePath(elf_path.clone()),
None,
Some(hyperlight_host::SandboxRunOptions::RunInProcess(false)),
writer,
)
.unwrap()
.evolve(Noop::default())
.unwrap(),
#[cfg(inprocess)]
UninitializedSandbox::new(
GuestBinary::FilePath(exe_path.clone()),
None,
Some(hyperlight_host::SandboxRunOptions::RunInProcess(false)),
writer,
)
.unwrap()
.evolve(Noop::default())
.unwrap(),
#[cfg(all(target_os = "windows", inprocess))]
UninitializedSandbox::new(
GuestBinary::FilePath(exe_path.clone()),
None,
Some(hyperlight_host::SandboxRunOptions::RunInProcess(true)),
writer,
)
.unwrap()
.evolve(Noop::default())
.unwrap(),
]
}
pub fn get_callbackguest_uninit_sandboxes(
writer: Option<&dyn HostFunction1<String, i32>>, ) -> Vec<UninitializedSandbox> {
let elf_path = get_c_or_rust_callbackguest_path();
let exe_path = format!("{elf_path}.exe");
vec![
UninitializedSandbox::new(GuestBinary::FilePath(elf_path.clone()), None, None, writer)
.unwrap(),
UninitializedSandbox::new(GuestBinary::FilePath(exe_path.clone()), None, None, writer)
.unwrap(),
#[cfg(inprocess)]
UninitializedSandbox::new(
GuestBinary::FilePath(elf_path.clone()),
None,
Some(hyperlight_host::SandboxRunOptions::RunInProcess(false)),
writer,
)
.unwrap(),
#[cfg(inprocess)]
UninitializedSandbox::new(
GuestBinary::FilePath(exe_path.clone()),
None,
Some(hyperlight_host::SandboxRunOptions::RunInProcess(false)),
writer,
)
.unwrap(),
#[cfg(all(target_os = "windows", inprocess))]
UninitializedSandbox::new(
GuestBinary::FilePath(exe_path.clone()),
None,
Some(hyperlight_host::SandboxRunOptions::RunInProcess(true)),
writer,
)
.unwrap(),
]
}
pub(crate) fn get_c_or_rust_simpleguest_path() -> String {
let guest_type = std::env::var("GUEST").unwrap_or("rust".to_string());
match guest_type.as_str() {
"rust" => simple_guest_as_string().unwrap(),
"c" => c_simple_guest_as_string().unwrap(),
_ => panic!("Unknown guest type '{guest_type}', use either 'rust' or 'c'"),
}
}
fn get_c_or_rust_callbackguest_path() -> String {
let guest_type = std::env::var("GUEST").unwrap_or("rust".to_string());
match guest_type.as_str() {
"rust" => callback_guest_as_string().unwrap(),
"c" => c_callback_guest_as_string().unwrap(),
_ => panic!("Unknown guest type '{guest_type}', use either 'rust' or 'c'"),
}
}