hyperlight_host/sandbox_state/transition.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
/*
Copyright 2024 The Hyperlight Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
use std::marker::PhantomData;
use tracing::{instrument, Span};
use super::sandbox::Sandbox;
use crate::func::call_ctx::MultiUseGuestCallContext;
use crate::Result;
/// Metadata about an evolution or devolution. Any `Sandbox` implementation
/// that also implements `EvolvableSandbox` or `DevolvableSandbox`
/// can decide the following things in a type-safe way:
///
/// 1. That transition is possible
/// 2. That transition requires a specific kind of metadata
///
/// For example, if you have the following structs:
///
/// ```ignore
/// struct MySandbox1 {}
/// struct MySandbox2 {}
///
/// impl Sandbox for MySandbox1 {...}
/// impl Sandbox for MySandbox2 {...}
/// ```
///
/// ...then you can define a metadata-free evolve transition between
/// `MySandbox1` and `MySandbox2`, and a devolve transition that requires
/// a callback between `MySandbox2` and `MySandbox` as follows:
///
/// ```ignore
/// impl EvolvableSandbox<
/// MySandbox1,
/// MySandbox2,
/// Noop<MySandbox1, MySandbox2>
/// > for MySandbox1 {
/// fn evolve(
/// self,
/// _: Noop<MySandbox1, MySandbox2>
/// ) -> Result<MySandbox2> {
/// Ok(MySandbox2{})
/// }
/// }
///
/// ```
///
/// Most transitions will likely involve `Noop`, but some may involve
/// implementing their own.
pub trait TransitionMetadata<Cur: Sandbox, Next: Sandbox> {}
/// Transition metadata that contains and does nothing. `Noop` is a
/// placeholder when you want to implement an `EvolvableSandbox`
/// or `DevolvableSandbox` that needs no additional metadata to succeed.
///
/// Construct one of these by using the `default()` method.
pub struct Noop<Cur: Sandbox, Next: Sandbox> {
cur_ph: PhantomData<Cur>,
next_ph: PhantomData<Next>,
}
impl<Cur: Sandbox, Next: Sandbox> Default for Noop<Cur, Next> {
fn default() -> Self {
Self {
cur_ph: PhantomData,
next_ph: PhantomData,
}
}
}
impl<Cur: Sandbox, Next: Sandbox> TransitionMetadata<Cur, Next> for Noop<Cur, Next> {}
/// A `TransitionMetadata` that calls a callback. The callback function takes
/// a mutable reference to a `MultiUseGuestCallContext` and returns a `Result<()>`
/// to signify success or failure of the function.
///
/// The function use the context to call guest functions.
///
/// Construct one of these by passing your callback to
/// `MultiUseContextCallback::from`, as in the following code (assuming `MySandbox`
/// is a `Sandbox` implementation):
///
/// ```ignore
/// let my_cb_fn: dyn FnOnce(&mut MultiUseGuestCallContext) -> Result<()> = |_sbox| {
/// println!("hello world!");
/// };
/// let mutating_cb = MultiUseContextCallback::from(my_cb_fn);
/// ```
pub struct MultiUseContextCallback<'func, Cur: Sandbox, F>
where
F: FnOnce(&mut MultiUseGuestCallContext) -> Result<()> + 'func,
{
cur_ph: PhantomData<Cur>,
fn_life_ph: PhantomData<&'func ()>,
cb: F,
}
impl<'a, Cur: Sandbox, Next: Sandbox, F> TransitionMetadata<Cur, Next>
for MultiUseContextCallback<'a, Cur, F>
where
F: FnOnce(&mut MultiUseGuestCallContext) -> Result<()>,
{
}
impl<'a, Cur: Sandbox, F> MultiUseContextCallback<'a, Cur, F>
where
F: FnOnce(&mut MultiUseGuestCallContext) -> Result<()>,
{
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
pub fn call(self, cur: &mut MultiUseGuestCallContext) -> Result<()> {
(self.cb)(cur)
}
}
impl<'a, Cur: Sandbox, F> From<F> for MultiUseContextCallback<'a, Cur, F>
where
F: FnOnce(&mut MultiUseGuestCallContext) -> Result<()> + 'a,
{
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn from(val: F) -> Self {
MultiUseContextCallback {
cur_ph: PhantomData,
fn_life_ph: PhantomData,
cb: val,
}
}
}
#[cfg(test)]
mod tests {
use super::Noop;
use crate::sandbox_state::sandbox::{DevolvableSandbox, EvolvableSandbox, Sandbox};
use crate::Result;
#[derive(Debug, Eq, PartialEq, Clone)]
struct MySandbox1 {}
#[derive(Debug, Eq, PartialEq, Clone)]
struct MySandbox2 {}
impl Sandbox for MySandbox1 {}
impl Sandbox for MySandbox2 {}
impl EvolvableSandbox<MySandbox1, MySandbox2, Noop<MySandbox1, MySandbox2>> for MySandbox1 {
fn evolve(self, _: Noop<MySandbox1, MySandbox2>) -> Result<MySandbox2> {
Ok(MySandbox2 {})
}
}
impl DevolvableSandbox<MySandbox2, MySandbox1, Noop<MySandbox2, MySandbox1>> for MySandbox2 {
fn devolve(self, _: Noop<MySandbox2, MySandbox1>) -> Result<MySandbox1> {
Ok(MySandbox1 {})
}
}
#[test]
fn test_evolve_devolve() {
let sbox_1_1 = MySandbox1 {};
let sbox_2_1 = sbox_1_1.clone().evolve(Noop::default()).unwrap();
let sbox_1_2 = sbox_2_1.clone().devolve(Noop::default()).unwrap();
let sbox_2_2 = sbox_1_2.clone().evolve(Noop::default()).unwrap();
assert_eq!(sbox_1_1, sbox_1_2);
assert_eq!(sbox_2_1, sbox_2_2);
}
}