Skip to content

Commit b91ea93

Browse files
committed
replace evolving a MultiUseSandbox with persist_call_guest_function_by_name
Signed-off-by: Jorge Prendes <[email protected]>
1 parent ae08c62 commit b91ea93

File tree

3 files changed

+25
-143
lines changed

3 files changed

+25
-143
lines changed

src/hyperlight_host/src/func/call_ctx.rs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
use tracing::{Span, instrument};
1818

1919
use super::{ParameterTuple, SupportedReturnType};
20-
use crate::mem::memory_region::MemoryRegion;
2120
use crate::sandbox::Callable;
2221
use crate::{MultiUseSandbox, Result};
2322
/// A context for calling guest functions.
@@ -57,44 +56,6 @@ impl MultiUseGuestCallContext {
5756
self.sbox.restore_state()?;
5857
Ok(self.sbox)
5958
}
60-
/// Close out the context and get back the internally-stored
61-
/// `MultiUseSandbox`.
62-
///
63-
/// Note that this method is pub(crate) and does not reset the state of the
64-
/// sandbox.
65-
///
66-
/// It is intended to be used when evolving a MultiUseSandbox to a new state
67-
/// and is not intended to be called publicly. It allows the state of the guest to be altered
68-
/// during the evolution of one sandbox state to another, enabling the new state created
69-
/// to be captured and stored in the Sandboxes state stack.
70-
///
71-
pub(crate) fn finish_no_reset(self) -> MultiUseSandbox {
72-
self.sbox
73-
}
74-
75-
/// Map a region of host memory into the sandbox.
76-
///
77-
/// Depending on the host platform, there are likely alignment
78-
/// requirements of at least one page for base and len.
79-
///
80-
/// `rgn.region_type` is ignored, since guest PTEs are not created
81-
/// for the new memory.
82-
///
83-
/// # Safety
84-
/// It is the caller's responsibility to ensure that the host side
85-
/// of the region remains intact and is not written to until this
86-
/// mapping is removed, either due to the destruction of the
87-
/// sandbox or due to a state rollback
88-
pub unsafe fn map_region(&mut self, rgn: &MemoryRegion) -> Result<()> {
89-
unsafe { self.sbox.map_region(rgn) }
90-
}
91-
92-
/// Map the contents of a file into the guest at a particular address
93-
///
94-
/// Returns the length of the mapping
95-
pub fn map_file_cow(&mut self, fp: &std::path::Path, guest_base: u64) -> Result<u64> {
96-
self.sbox.map_file_cow(fp, guest_base)
97-
}
9859
}
9960

10061
impl Callable for MultiUseGuestCallContext {

src/hyperlight_host/src/sandbox/initialized_multi_use.rs

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
4343
use crate::mem::ptr::RawPtr;
4444
use crate::mem::shared_mem::HostSharedMemory;
4545
use crate::metrics::maybe_time_and_emit_guest_call;
46-
use crate::sandbox_state::sandbox::{EvolvableSandbox, Sandbox};
47-
use crate::sandbox_state::transition::MultiUseContextCallback;
46+
use crate::sandbox_state::sandbox::Sandbox;
4847
use crate::{HyperlightError, Result, log_then_return};
4948

5049
/// A sandbox that supports being used Multiple times.
@@ -199,6 +198,26 @@ impl MultiUseSandbox {
199198
})
200199
}
201200

201+
/// Call a guest function by name, with the given return type and arguments.
202+
/// The changes made to the sandbox are persisted
203+
#[instrument(err(Debug), skip(self, args), parent = Span::current())]
204+
pub fn persist_call_guest_function_by_name<Output: SupportedReturnType>(
205+
&mut self,
206+
func_name: &str,
207+
args: impl ParameterTuple,
208+
) -> Result<Output> {
209+
maybe_time_and_emit_guest_call(func_name, || {
210+
let ret = self.call_guest_function_by_name_no_reset(
211+
func_name,
212+
Output::TYPE,
213+
args.into_value(),
214+
);
215+
let ret = Output::from_value(ret?);
216+
self.mem_mgr.unwrap_mgr_mut().push_state()?;
217+
ret
218+
})
219+
}
220+
202221
/// Map a region of host memory into the sandbox.
203222
///
204223
/// Depending on the host platform, there are likely alignment
@@ -360,52 +379,20 @@ impl std::fmt::Debug for MultiUseSandbox {
360379
}
361380
}
362381

363-
impl<'a, F>
364-
EvolvableSandbox<
365-
MultiUseSandbox,
366-
MultiUseSandbox,
367-
MultiUseContextCallback<'a, MultiUseSandbox, F>,
368-
> for MultiUseSandbox
369-
where
370-
F: FnOnce(&mut MultiUseGuestCallContext) -> Result<()> + 'a,
371-
{
372-
/// The purpose of this function is to allow multiple states to be associated with a single MultiUseSandbox.
373-
///
374-
/// An implementation such as HyperlightJs or HyperlightWasm can use this to call guest functions to load JS or WASM code and then evolve the sandbox causing state to be captured.
375-
/// The new MultiUseSandbox can then be used to call guest functions to execute the loaded code.
376-
///
377-
/// The evolve function creates a new MultiUseCallContext which is then passed to a callback function allowing the
378-
/// callback function to call guest functions as part of the evolve process, once the callback function is complete
379-
/// the context is finished using a crate internal method that does not restore the prior state of the Sandbox.
380-
/// It then creates a mew memory snapshot on the snapshot stack and returns the MultiUseSandbox
381-
#[instrument(err(Debug), skip_all, parent = Span::current(), level = "Trace")]
382-
fn evolve(
383-
self,
384-
transition_func: MultiUseContextCallback<'a, MultiUseSandbox, F>,
385-
) -> Result<MultiUseSandbox> {
386-
let mut ctx = self.new_call_context();
387-
transition_func.call(&mut ctx)?;
388-
let mut sbox = ctx.finish_no_reset();
389-
sbox.mem_mgr.unwrap_mgr_mut().push_state()?;
390-
Ok(sbox)
391-
}
392-
}
393-
394382
#[cfg(test)]
395383
mod tests {
396384
use std::sync::{Arc, Barrier};
397385
use std::thread;
398386

399387
use hyperlight_testing::simple_guest_as_string;
400388

401-
use crate::func::call_ctx::MultiUseGuestCallContext;
402389
#[cfg(target_os = "linux")]
403390
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags, MemoryRegionType};
404391
#[cfg(target_os = "linux")]
405392
use crate::mem::shared_mem::{ExclusiveSharedMemory, GuestSharedMemory, SharedMemory as _};
406393
use crate::sandbox::{Callable, SandboxConfiguration};
407394
use crate::sandbox_state::sandbox::EvolvableSandbox;
408-
use crate::sandbox_state::transition::{MultiUseContextCallback, Noop};
395+
use crate::sandbox_state::transition::Noop;
409396
use crate::{GuestBinary, HyperlightError, MultiUseSandbox, Result, UninitializedSandbox};
410397

411398
// Tests to ensure that many (1000) function calls can be made in a call context with a small stack (1K) and heap(14K).
@@ -460,18 +447,13 @@ mod tests {
460447

461448
let snapshot = sbox.snapshot().unwrap();
462449

463-
let func = Box::new(|call_ctx: &mut MultiUseGuestCallContext| {
464-
call_ctx.call::<i32>("AddToStatic", 5i32)?;
465-
Ok(())
466-
});
467-
let transition_func = MultiUseContextCallback::from(func);
468-
let mut sbox = sbox.evolve(transition_func).unwrap();
450+
let _ = sbox.persist_call_guest_function_by_name::<i32>("AddToStatic", 5i32).unwrap();
469451

470-
let res: i32 = sbox.call_guest_function_by_name("GetStatic", ()).unwrap();
452+
let res: i32 = sbox.persist_call_guest_function_by_name("GetStatic", ()).unwrap();
471453
assert_eq!(res, 5);
472454

473455
sbox.restore(&snapshot).unwrap();
474-
let res: i32 = sbox.call_guest_function_by_name("GetStatic", ()).unwrap();
456+
let res: i32 = sbox.persist_call_guest_function_by_name("GetStatic", ()).unwrap();
475457
assert_eq!(res, 0);
476458
}
477459

src/hyperlight_host/src/sandbox_state/transition.rs

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ limitations under the License.
1616

1717
use std::marker::PhantomData;
1818

19-
use tracing::{Span, instrument};
20-
2119
use super::sandbox::Sandbox;
22-
use crate::Result;
23-
use crate::func::call_ctx::MultiUseGuestCallContext;
2420

2521
/// Metadata about an evolution. Any `Sandbox` implementation
2622
/// that also implements `EvolvableSandbox` can decide the following
@@ -82,60 +78,3 @@ impl<Cur: Sandbox, Next: Sandbox> Default for Noop<Cur, Next> {
8278
}
8379

8480
impl<Cur: Sandbox, Next: Sandbox> TransitionMetadata<Cur, Next> for Noop<Cur, Next> {}
85-
86-
/// A `TransitionMetadata` that calls a callback. The callback function takes
87-
/// a mutable reference to a `MultiUseGuestCallContext` and returns a `Result<()>`
88-
/// to signify success or failure of the function.
89-
///
90-
/// The function use the context to call guest functions.
91-
///
92-
/// Construct one of these by passing your callback to
93-
/// `MultiUseContextCallback::from`, as in the following code (assuming `MySandbox`
94-
/// is a `Sandbox` implementation):
95-
///
96-
/// ```ignore
97-
/// let my_cb_fn: dyn FnOnce(&mut MultiUseGuestCallContext) -> Result<()> = |_sbox| {
98-
/// println!("hello world!");
99-
/// };
100-
/// let mutating_cb = MultiUseContextCallback::from(my_cb_fn);
101-
/// ```
102-
pub struct MultiUseContextCallback<'func, Cur: Sandbox, F>
103-
where
104-
F: FnOnce(&mut MultiUseGuestCallContext) -> Result<()> + 'func,
105-
{
106-
cur_ph: PhantomData<Cur>,
107-
fn_life_ph: PhantomData<&'func ()>,
108-
cb: F,
109-
}
110-
111-
impl<Cur: Sandbox, Next: Sandbox, F> TransitionMetadata<Cur, Next>
112-
for MultiUseContextCallback<'_, Cur, F>
113-
where
114-
F: FnOnce(&mut MultiUseGuestCallContext) -> Result<()>,
115-
{
116-
}
117-
118-
impl<Cur: Sandbox, F> MultiUseContextCallback<'_, Cur, F>
119-
where
120-
F: FnOnce(&mut MultiUseGuestCallContext) -> Result<()>,
121-
{
122-
/// Invokes the callback on the provided guest context
123-
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
124-
pub fn call(self, cur: &mut MultiUseGuestCallContext) -> Result<()> {
125-
(self.cb)(cur)
126-
}
127-
}
128-
129-
impl<'a, Cur: Sandbox, F> From<F> for MultiUseContextCallback<'a, Cur, F>
130-
where
131-
F: FnOnce(&mut MultiUseGuestCallContext) -> Result<()> + 'a,
132-
{
133-
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
134-
fn from(val: F) -> Self {
135-
MultiUseContextCallback {
136-
cur_ph: PhantomData,
137-
fn_life_ph: PhantomData,
138-
cb: val,
139-
}
140-
}
141-
}

0 commit comments

Comments
 (0)