hyperlight_host/sandbox/
mod.rs1pub mod config;
19pub(crate) mod host_funcs;
21pub(crate) mod hypervisor;
23pub mod initialized_multi_use;
26pub(crate) mod outb;
27pub mod uninitialized;
30pub(crate) mod uninitialized_evolve;
33
34pub mod snapshot;
36
37mod callable;
39
40#[cfg(feature = "trace_guest")]
42pub(crate) mod trace;
43
44pub use callable::Callable;
46pub use config::SandboxConfiguration;
48pub use initialized_multi_use::MultiUseSandbox;
50use tracing::{Span, instrument};
51pub use uninitialized::GuestBinary;
53pub use uninitialized::UninitializedSandbox;
55
56#[cfg(target_os = "windows")]
57use crate::hypervisor::windows_hypervisor_platform;
58
59#[instrument(skip_all, parent = Span::current())]
68pub fn is_supported_platform() -> bool {
69 #[cfg(not(target_os = "linux"))]
70 #[cfg(not(target_os = "windows"))]
71 return false;
72
73 true
74}
75
76pub type ExtraAllowedSyscall = i64;
78
79#[instrument(skip_all, parent = Span::current())]
84pub fn is_hypervisor_present() -> bool {
85 hypervisor::get_available_hypervisor().is_some()
86}
87
88#[cfg(test)]
89mod tests {
90 use std::sync::Arc;
91 use std::thread;
92
93 use crossbeam_queue::ArrayQueue;
94 use hyperlight_testing::simple_guest_as_string;
95
96 use crate::sandbox::uninitialized::GuestBinary;
97 use crate::{MultiUseSandbox, UninitializedSandbox, new_error};
98
99 #[test]
100 #[cfg(target_os = "linux")]
102 fn is_hypervisor_present() {
103 use std::path::Path;
104
105 cfg_if::cfg_if! {
106 if #[cfg(all(kvm, mshv3))] {
107 assert_eq!(Path::new("/dev/kvm").exists() || Path::new("/dev/mshv").exists(), super::is_hypervisor_present());
108 } else if #[cfg(kvm)] {
109 assert_eq!(Path::new("/dev/kvm").exists(), super::is_hypervisor_present());
110 } else if #[cfg(mshv3)] {
111 assert_eq!(Path::new("/dev/mshv").exists(), super::is_hypervisor_present());
112 } else {
113 assert!(!super::is_hypervisor_present());
114 }
115 }
116 }
117
118 #[test]
119 fn check_create_and_use_sandbox_on_different_threads() {
120 let unintializedsandbox_queue = Arc::new(ArrayQueue::<UninitializedSandbox>::new(10));
121 let sandbox_queue = Arc::new(ArrayQueue::<MultiUseSandbox>::new(10));
122
123 for i in 0..10 {
124 let simple_guest_path = simple_guest_as_string().expect("Guest Binary Missing");
125 let unintializedsandbox =
126 UninitializedSandbox::new(GuestBinary::FilePath(simple_guest_path), None)
127 .unwrap_or_else(|_| panic!("Failed to create UninitializedSandbox {}", i));
128
129 unintializedsandbox_queue
130 .push(unintializedsandbox)
131 .unwrap_or_else(|_| panic!("Failed to push UninitializedSandbox {}", i));
132 }
133
134 let thread_handles = (0..10)
135 .map(|i| {
136 let uq = unintializedsandbox_queue.clone();
137 let sq = sandbox_queue.clone();
138 thread::spawn(move || {
139 let uninitialized_sandbox = uq.pop().unwrap_or_else(|| {
140 panic!("Failed to pop UninitializedSandbox thread {}", i)
141 });
142 let host_funcs = uninitialized_sandbox
143 .host_funcs
144 .try_lock()
145 .map_err(|_| new_error!("Error locking"));
146
147 assert!(host_funcs.is_ok());
148
149 host_funcs
150 .unwrap()
151 .host_print(format!(
152 "Printing from UninitializedSandbox on Thread {}\n",
153 i
154 ))
155 .unwrap();
156
157 let sandbox = uninitialized_sandbox.evolve().unwrap_or_else(|_| {
158 panic!("Failed to initialize UninitializedSandbox thread {}", i)
159 });
160
161 sq.push(sandbox).unwrap_or_else(|_| {
162 panic!("Failed to push UninitializedSandbox thread {}", i)
163 })
164 })
165 })
166 .collect::<Vec<_>>();
167
168 for handle in thread_handles {
169 handle.join().unwrap();
170 }
171
172 let thread_handles = (0..10)
173 .map(|i| {
174 let sq = sandbox_queue.clone();
175 thread::spawn(move || {
176 let sandbox = sq
177 .pop()
178 .unwrap_or_else(|| panic!("Failed to pop Sandbox thread {}", i));
179 let host_funcs = sandbox
180 ._host_funcs
181 .try_lock()
182 .map_err(|_| new_error!("Error locking"));
183
184 assert!(host_funcs.is_ok());
185
186 host_funcs
187 .unwrap()
188 .host_print(format!("Print from Sandbox on Thread {}\n", i))
189 .unwrap();
190 })
191 })
192 .collect::<Vec<_>>();
193
194 for handle in thread_handles {
195 handle.join().unwrap();
196 }
197 }
198}