Skip to content

Commit b5f3d27

Browse files
committed
Remove the host writer from the arguments to UninitializedSandbox::new
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
1 parent dcf7f92 commit b5f3d27

File tree

27 files changed

+193
-199
lines changed

27 files changed

+193
-199
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ fn main() -> hyperlight_host::Result<()> {
4444
hyperlight_host::GuestBinary::FilePath(hyperlight_testing::simple_guest_as_string().unwrap()),
4545
None, // default configuration
4646
None, // default run options
47-
None, // default host print function
4847
)?;
4948

5049
// Registering a host function makes it available to be called by the guest

fuzz/fuzz_targets/guest_call.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ fuzz_target!(
3636
GuestBinary::FilePath(simple_guest_for_fuzzing_as_string().expect("Guest Binary Missing")),
3737
None,
3838
None,
39-
None,
4039
)
4140
.unwrap();
4241

fuzz/fuzz_targets/host_call.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use std::sync::{Mutex, OnceLock};
2020

2121
use hyperlight_host::func::{ParameterValue, ReturnType};
2222
use hyperlight_host::sandbox::uninitialized::GuestBinary;
23-
use hyperlight_host::sandbox::SandboxConfiguration;
2423
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2524
use hyperlight_host::sandbox_state::transition::Noop;
2625
use hyperlight_host::{HyperlightError, MultiUseSandbox, UninitializedSandbox};
@@ -36,7 +35,6 @@ fuzz_target!(
3635
GuestBinary::FilePath(simple_guest_for_fuzzing_as_string().expect("Guest Binary Missing")),
3736
None,
3837
None,
39-
None,
4038
)
4139
.unwrap();
4240

fuzz/fuzz_targets/host_print.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ fuzz_target!(
2222
GuestBinary::FilePath(simple_guest_for_fuzzing_as_string().expect("Guest Binary Missing")),
2323
None,
2424
None,
25-
None,
2625
)
2726
.unwrap();
2827

src/hyperlight_host/benches/benchmarks.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use hyperlight_testing::simple_guest_as_string;
2626

2727
fn create_uninit_sandbox() -> UninitializedSandbox {
2828
let path = simple_guest_as_string().unwrap();
29-
UninitializedSandbox::new(GuestBinary::FilePath(path), None, None, None).unwrap()
29+
UninitializedSandbox::new(GuestBinary::FilePath(path), None, None).unwrap()
3030
}
3131

3232
fn create_multiuse_sandbox() -> MultiUseSandbox {
@@ -83,7 +83,6 @@ fn guest_call_benchmark(c: &mut Criterion) {
8383
GuestBinary::FilePath(simple_guest_as_string().unwrap()),
8484
Some(config),
8585
None,
86-
None,
8786
)
8887
.unwrap();
8988
let mut sandbox = sandbox.evolve(Noop::default()).unwrap();

src/hyperlight_host/examples/func_ctx/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ fn main() {
2727
// test guest binary
2828
let sbox1: MultiUseSandbox = {
2929
let path = simple_guest_as_string().unwrap();
30-
let u_sbox =
31-
UninitializedSandbox::new(GuestBinary::FilePath(path), None, None, None).unwrap();
30+
let u_sbox = UninitializedSandbox::new(GuestBinary::FilePath(path), None, None).unwrap();
3231
u_sbox.evolve(Noop::default())
3332
}
3433
.unwrap();

src/hyperlight_host/examples/guest-debugging/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ fn main() -> hyperlight_host::Result<()> {
4949
),
5050
cfg, // sandbox configuration
5151
None, // default run options
52-
None, // default host print function
5352
)?;
5453

5554
// Register a host functions

src/hyperlight_host/examples/hello-world/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ fn main() -> hyperlight_host::Result<()> {
2929
),
3030
None, // default configuration
3131
None, // default run options
32-
None, // default host print function
3332
)?;
3433

3534
// Register a host functions

src/hyperlight_host/examples/logging/main.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ limitations under the License.
1515
*/
1616
#![allow(clippy::disallowed_macros)]
1717
extern crate hyperlight_host;
18-
use std::sync::{Arc, Mutex};
1918

2019
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
2120
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
@@ -41,15 +40,10 @@ fn main() -> Result<()> {
4140

4241
for _ in 0..20 {
4342
let path = hyperlight_guest_path.clone();
44-
let writer_func = Arc::new(Mutex::new(fn_writer));
4543
let res: Result<()> = {
4644
// Create a new sandbox.
47-
let usandbox = UninitializedSandbox::new(
48-
GuestBinary::FilePath(path),
49-
None,
50-
None,
51-
Some(&writer_func),
52-
)?;
45+
let mut usandbox = UninitializedSandbox::new(GuestBinary::FilePath(path), None, None)?;
46+
usandbox.register_print(fn_writer)?;
5347

5448
// Initialize the sandbox.
5549

@@ -91,7 +85,6 @@ fn main() -> Result<()> {
9185
GuestBinary::FilePath(hyperlight_guest_path.clone()),
9286
None,
9387
None,
94-
None,
9588
)?;
9689

9790
// Initialize the sandbox.

src/hyperlight_host/examples/metrics/main.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ limitations under the License.
1515
*/
1616
#![allow(clippy::disallowed_macros)]
1717
extern crate hyperlight_host;
18-
use std::sync::{Arc, Mutex};
1918
use std::thread::{spawn, JoinHandle};
2019

2120
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
@@ -53,15 +52,10 @@ fn do_hyperlight_stuff() {
5352

5453
for _ in 0..20 {
5554
let path = hyperlight_guest_path.clone();
56-
let writer_func = Arc::new(Mutex::new(fn_writer));
5755
let handle = spawn(move || -> Result<()> {
5856
// Create a new sandbox.
59-
let usandbox = UninitializedSandbox::new(
60-
GuestBinary::FilePath(path),
61-
None,
62-
None,
63-
Some(&writer_func),
64-
)?;
57+
let mut usandbox = UninitializedSandbox::new(GuestBinary::FilePath(path), None, None)?;
58+
usandbox.register_print(fn_writer)?;
6559

6660
// Initialize the sandbox.
6761

@@ -103,7 +97,6 @@ fn do_hyperlight_stuff() {
10397
GuestBinary::FilePath(hyperlight_guest_path.clone()),
10498
None,
10599
None,
106-
None,
107100
)
108101
.expect("Failed to create UninitializedSandbox");
109102

src/hyperlight_host/examples/tracing-chrome/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ fn main() -> Result<()> {
3333
simple_guest_as_string().expect("Cannot find the guest binary at the expected location.");
3434

3535
// Create a new sandbox.
36-
let usandbox =
37-
UninitializedSandbox::new(GuestBinary::FilePath(simple_guest_path), None, None, None)?;
36+
let usandbox = UninitializedSandbox::new(GuestBinary::FilePath(simple_guest_path), None, None)?;
3837

3938
let mut sbox = usandbox
4039
.evolve(Noop::<UninitializedSandbox, MultiUseSandbox>::default())

src/hyperlight_host/examples/tracing-otlp/main.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ fn run_example(wait_input: bool) -> HyperlightResult<()> {
116116
for i in 0..10 {
117117
let path = hyperlight_guest_path.clone();
118118
let exit = Arc::clone(&should_exit);
119-
let writer_func = Arc::new(Mutex::new(fn_writer));
120119
let handle = spawn(move || -> HyperlightResult<()> {
121120
while !*exit.try_lock().unwrap() {
122121
// Construct a new span named "hyperlight tracing example thread" with INFO level.
@@ -130,12 +129,9 @@ fn run_example(wait_input: bool) -> HyperlightResult<()> {
130129
let _entered = span.enter();
131130

132131
// Create a new sandbox.
133-
let usandbox = UninitializedSandbox::new(
134-
GuestBinary::FilePath(path.clone()),
135-
None,
136-
None,
137-
Some(&writer_func),
138-
)?;
132+
let mut usandbox =
133+
UninitializedSandbox::new(GuestBinary::FilePath(path.clone()), None, None)?;
134+
usandbox.register_print(fn_writer)?;
139135

140136
// Initialize the sandbox.
141137

src/hyperlight_host/examples/tracing-tracy/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ fn main() -> Result<()> {
3939
simple_guest_as_string().expect("Cannot find the guest binary at the expected location.");
4040

4141
// Create a new sandbox.
42-
let usandbox =
43-
UninitializedSandbox::new(GuestBinary::FilePath(simple_guest_path), None, None, None)?;
42+
let usandbox = UninitializedSandbox::new(GuestBinary::FilePath(simple_guest_path), None, None)?;
4443

4544
let mut sbox = usandbox
4645
.evolve(Noop::<UninitializedSandbox, MultiUseSandbox>::default())

src/hyperlight_host/examples/tracing/main.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
1818
use tracing::{span, Level};
1919
extern crate hyperlight_host;
20-
use std::sync::{Arc, Mutex};
2120
use std::thread::{spawn, JoinHandle};
2221

2322
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
@@ -60,7 +59,6 @@ fn run_example() -> Result<()> {
6059

6160
for i in 0..10 {
6261
let path = hyperlight_guest_path.clone();
63-
let writer_func = Arc::new(Mutex::new(fn_writer));
6462
let handle = spawn(move || -> Result<()> {
6563
// Construct a new span named "hyperlight tracing example thread" with INFO level.
6664
let id = Uuid::new_v4();
@@ -73,12 +71,8 @@ fn run_example() -> Result<()> {
7371
let _entered = span.enter();
7472

7573
// Create a new sandbox.
76-
let usandbox = UninitializedSandbox::new(
77-
GuestBinary::FilePath(path),
78-
None,
79-
None,
80-
Some(&writer_func),
81-
)?;
74+
let mut usandbox = UninitializedSandbox::new(GuestBinary::FilePath(path), None, None)?;
75+
usandbox.register_print(fn_writer)?;
8276

8377
// Initialize the sandbox.
8478

@@ -119,7 +113,6 @@ fn run_example() -> Result<()> {
119113
GuestBinary::FilePath(hyperlight_guest_path.clone()),
120114
None,
121115
None,
122-
None,
123116
)?;
124117

125118
// Initialize the sandbox.

src/hyperlight_host/src/func/call_ctx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ mod tests {
117117
let path = simple_guest_as_string().map_err(|e| {
118118
HyperlightError::Error(format!("failed to get simple guest path ({e:?})"))
119119
})?;
120-
UninitializedSandbox::new(GuestBinary::FilePath(path), None, None, None)
120+
UninitializedSandbox::new(GuestBinary::FilePath(path), None, None)
121121
}
122122

123123
/// Test to create a `MultiUseSandbox`, then call several guest functions

src/hyperlight_host/src/func/guest_dispatch.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ mod tests {
155155
GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")),
156156
None,
157157
None,
158-
None,
159158
)
160159
.unwrap();
161160

@@ -189,7 +188,6 @@ mod tests {
189188
GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")),
190189
None,
191190
None,
192-
None,
193191
)
194192
.unwrap();
195193

@@ -221,7 +219,6 @@ mod tests {
221219
GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")),
222220
None,
223221
None,
224-
None,
225222
)
226223
.unwrap()
227224
};
@@ -337,8 +334,6 @@ mod tests {
337334
None,
338335
// by default, the below represents in-hypervisor mode
339336
None,
340-
// just use the built-in host print function
341-
None,
342337
)
343338
.unwrap();
344339
test_call_guest_function_by_name(u_sbox);
@@ -360,7 +355,6 @@ mod tests {
360355
GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")),
361356
None,
362357
None,
363-
None,
364358
)?;
365359
let sandbox: MultiUseSandbox = usbox.evolve(Noop::default())?;
366360
let mut ctx = sandbox.new_call_context();
@@ -409,7 +403,6 @@ mod tests {
409403
GuestBinary::FilePath(callback_guest_as_string().expect("Guest Binary Missing")),
410404
None,
411405
None,
412-
None,
413406
)
414407
.unwrap();
415408

@@ -448,7 +441,6 @@ mod tests {
448441
GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")),
449442
None,
450443
None,
451-
None,
452444
)
453445
.unwrap();
454446

src/hyperlight_host/src/func/host_functions.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,39 @@ macro_rules! impl_host_function {
9090
}
9191
}
9292

93+
impl<R $(, $P)*> HostFunction<R, ($($P,)*)> for &dyn HostFunction<R, ($($P,)*)>
94+
where
95+
$($P: SupportedParameterType + Clone,)*
96+
R: SupportedReturnType,
97+
{
98+
/// Register the host function with the given name in the sandbox.
99+
#[instrument(
100+
err(Debug), skip(self, sandbox), parent = Span::current(), level = "Trace"
101+
)]
102+
fn register(
103+
&self,
104+
sandbox: &mut UninitializedSandbox,
105+
name: &str,
106+
) -> Result<()> {
107+
(**self).register(sandbox, name)
108+
}
109+
110+
/// Register the host function with the given name in the sandbox, allowing extra syscalls.
111+
#[cfg(all(feature = "seccomp", target_os = "linux"))]
112+
#[instrument(
113+
err(Debug), skip(self, sandbox, extra_allowed_syscalls),
114+
parent = Span::current(), level = "Trace"
115+
)]
116+
fn register_with_extra_allowed_syscalls(
117+
&self,
118+
sandbox: &mut UninitializedSandbox,
119+
name: &str,
120+
extra_allowed_syscalls: Vec<ExtraAllowedSyscall>,
121+
) -> Result<()> {
122+
(**self).register_with_extra_allowed_syscalls(sandbox, name, extra_allowed_syscalls)
123+
}
124+
}
125+
93126
impl<R $(, $P)*, F> IntoHostFunction<R, ($($P,)*)> for F
94127
where
95128
F: FnMut($($P),*) -> Result<R> + Send + 'static,
@@ -126,6 +159,18 @@ macro_rules! impl_host_function {
126159
}
127160
}
128161

162+
impl<R $(, $P)*> IntoHostFunction<R, ($($P,)*)> for &dyn HostFunction<R, ($($P,)*)>
163+
where
164+
R: SupportedReturnType,
165+
$($P: SupportedParameterType + Clone,)*
166+
{
167+
type Output = Self;
168+
169+
fn into_host_function(self) -> Self::Output {
170+
self
171+
}
172+
}
173+
129174
fn register_host_function<T, $($P,)* R>(
130175
self_: Arc<Mutex<T>>,
131176
sandbox: &mut UninitializedSandbox,

src/hyperlight_host/src/hypervisor/hypervisor_handler.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,6 @@ mod tests {
976976
GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")),
977977
cfg,
978978
None,
979-
None,
980979
)
981980
.unwrap();
982981

src/hyperlight_host/src/hypervisor/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ pub(crate) mod tests {
364364
}
365365

366366
let sandbox =
367-
UninitializedSandbox::new(GuestBinary::FilePath(filename.clone()), None, None, None)?;
367+
UninitializedSandbox::new(GuestBinary::FilePath(filename.clone()), None, None)?;
368368
let (hshm, gshm) = sandbox.mgr.build();
369369
drop(hshm);
370370

src/hyperlight_host/src/metrics/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ mod tests {
111111
GuestBinary::FilePath(simple_guest_as_string().unwrap()),
112112
None,
113113
None,
114-
None,
115114
)
116115
.unwrap();
117116

0 commit comments

Comments
 (0)