Struct SingleUseSandbox

Source
pub struct SingleUseSandbox { /* private fields */ }
Expand description

A sandbox implementation that supports calling no more than 1 guest function

Implementations§

Source§

impl SingleUseSandbox

Source

pub fn new_call_context(self) -> SingleUseGuestCallContext

Create a new SingleUseCallContext . The main purpose of the a SingleUseSandbox is to allow mutiple calls to guest functions from within a callback function.

Since this function consumes self, the returned SingleUseGuestCallContext is guaranteed mutual exclusion for calling functions within the sandbox.

Since this is a SingleUseSandbox, the returned context cannot be converted back into the original SingleUseSandbox. When it’s dropped, all the resources of the context and sandbox are released at once.

Example usage (compiled as a “no_run” doctest since the test binary will not be found):

use hyperlight_host::sandbox::{UninitializedSandbox, SingleUseSandbox};
use hyperlight_common::flatbuffer_wrappers::function_types::{ReturnType, ParameterValue, ReturnValue};
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
use hyperlight_host::sandbox_state::transition::Noop;
use hyperlight_host::GuestBinary;

// First, create a new uninitialized sandbox, then evolve it to become
// an initialized, single-use one.
let u_sbox = UninitializedSandbox::new(
    GuestBinary::FilePath("some_guest_binary".to_string()),
    None,
    None,
    None,
).unwrap();
let sbox: SingleUseSandbox = u_sbox.evolve(Noop::default()).unwrap();
// Next, create a new call context from the single-use sandbox.
// After this line, your code will not compile if you try to use the
// original `sbox` variable.
let mut ctx = sbox.new_call_context();


// Create a closure to call multiple guest functions usings the contexts
// call_from-func method. Assues that the loaded binary
// ("some_guest_binary") has a function therein called "SomeGuestFunc" and another called "SomeOtherGuestFunc"
// that take a single integer argument and return an integer.


let result = ctx.call_from_func( |call_ctx| {

match call_ctx.call(
    "SomeGuestFunc",
    ReturnType::Int,
    Some(vec![ParameterValue::Int(1)])
) {
    Ok(ReturnValue::Int(i)) => println!(
        "got successful return value {}",
        i,
    ),
    other => panic!(
        "failed to get return value as expected ({:?})",
        other,
    ),
}

match call_ctx.call(
    "SomeOtherGuestFunc",
    ReturnType::Int,
    Some(vec![ParameterValue::Int(1)])
) {
    Ok(ReturnValue::Int(i)) => println!(
        "got successful return value {}",
        i,
    ),
    other => panic!(
        "failed to get return value as expected ({:?})",
        other,
    ),
}

Ok(ReturnValue::Int(0))

});

// After the call context is dropped, the sandbox is also dropped.
Source

pub fn call_guest_function_by_name( self, name: &str, ret: ReturnType, args: Option<Vec<ParameterValue>>, ) -> Result<ReturnValue>

Convenience for the following:

self.new_call_context().call(name, ret, args)

Trait Implementations§

Source§

impl Debug for SingleUseSandbox

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for SingleUseSandbox

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl EvolvableSandbox<UninitializedSandbox, SingleUseSandbox, Noop<UninitializedSandbox, SingleUseSandbox>> for UninitializedSandbox

Source§

fn evolve( self, _: Noop<UninitializedSandbox, SingleUseSandbox>, ) -> Result<SingleUseSandbox>

Evolve self to a SingleUseSandbox without any additional metadata.

Source§

impl Sandbox for SingleUseSandbox

Source§

fn check_stack_guard(&self) -> Result<bool>

Check to ensure the current stack cookie matches the one that was selected when the stack was constructed. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more