Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ members = [
"src/hyperlight_testing",
"fuzz",
"src/hyperlight_guest_bin",
"src/hyperlight_guest_macro",
"src/hyperlight_component_util",
"src/hyperlight_component_macro",
"src/trace_dump",
Expand All @@ -41,6 +42,7 @@ hyperlight-common = { path = "src/hyperlight_common", version = "0.9.0", default
hyperlight-host = { path = "src/hyperlight_host", version = "0.9.0", default-features = false }
hyperlight-guest = { path = "src/hyperlight_guest", version = "0.9.0", default-features = false }
hyperlight-guest-bin = { path = "src/hyperlight_guest_bin", version = "0.9.0", default-features = false }
hyperlight-guest-macro = { path = "src/hyperlight_guest_macro", version = "0.9.0", default-features = false }
hyperlight-testing = { path = "src/hyperlight_testing", default-features = false }
hyperlight-guest-tracing = { path = "src/hyperlight_guest_tracing", version = "0.9.0", default-features = false }
hyperlight-guest-tracing-macro = { path = "src/hyperlight_guest_tracing_macro", version = "0.9.0", default-features = false }
Expand Down
4 changes: 3 additions & 1 deletion src/hyperlight_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ log = "0.4.27"
tracing = { version = "0.1.41", optional = true }
arbitrary = {version = "1.4.2", optional = true, features = ["derive"]}
spin = "0.10.0"
thiserror = { version = "2.0.16", default-features = false }

[features]
default = ["tracing"]
tracing = ["dep:tracing"]
fuzzing = ["dep:arbitrary"]
trace_guest = []
unwind_guest = []
mem_profile = []
std = []
std = ["thiserror/std", "log/std", "tracing/std"]

[dev-dependencies]
hyperlight-testing = { workspace = true }
Expand Down
45 changes: 45 additions & 0 deletions src/hyperlight_common/src/func/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2025 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 alloc::string::String;

use thiserror::Error;

use crate::func::{ParameterValue, ReturnValue};

/// The error type for Hyperlight operations
#[derive(Error, Debug)]
pub enum Error {
/// Failed to get value from parameter value
#[error("Failed To Convert Parameter Value {0:?} to {1:?}")]
ParameterValueConversionFailure(ParameterValue, &'static str),

/// Failed to get value from return value
#[error("Failed To Convert Return Value {0:?} to {1:?}")]
ReturnValueConversionFailure(ReturnValue, &'static str),

/// A function was called with an incorrect number of arguments
#[error("The number of arguments to the function is wrong: got {0:?} expected {1:?}")]
UnexpectedNoOfArguments(usize, usize),

/// The parameter value type is unexpected
#[error("The parameter value type is unexpected got {0:?} expected {1:?}")]
UnexpectedParameterValueType(ParameterValue, String),

/// The return value type is unexpected
#[error("The return value type is unexpected got {0:?} expected {1:?}")]
UnexpectedReturnValueType(ReturnValue, String),
}
40 changes: 40 additions & 0 deletions src/hyperlight_common/src/func/functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2025 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 super::utils::for_each_tuple;
use super::{Error, ParameterTuple, ResultType, SupportedReturnType};

pub trait Function<Output: SupportedReturnType, Args: ParameterTuple, E: From<Error>> {
fn call(&self, args: Args) -> Result<Output, E>;
}

macro_rules! impl_function {
([$N:expr] ($($p:ident: $P:ident),*)) => {
impl<F, R, E, $($P),*> Function<R::ReturnType, ($($P,)*), E> for F
where
F: Fn($($P),*) -> R,
($($P,)*): ParameterTuple,
R: ResultType<E>,
E: From<Error> + core::fmt::Debug,
{
fn call(&self, ($($p,)*): ($($P,)*)) -> Result<R::ReturnType, E> {
(self)($($p),*).into_result()
}
}
};
}

for_each_tuple!(impl_function);
49 changes: 49 additions & 0 deletions src/hyperlight_common/src/func/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2025 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.
*/

/// Error types related to function support
pub(crate) mod error;
/// Definitions and functionality to enable guest-to-host function calling,
/// also called "host functions"
///
/// This module includes functionality to do the following
///
/// - Define several prototypes for what a host function must look like,
/// including the number of arguments (arity) they can have, supported argument
/// types, and supported return types
/// - Registering host functions to be callable by the guest
/// - Dynamically dispatching a call from the guest to the appropriate
/// host function
pub(crate) mod functions;
/// Definitions and functionality for supported parameter types
pub(crate) mod param_type;
/// Definitions and functionality for supported return types
pub(crate) mod ret_type;

pub use error::Error;
/// Re-export for `HostFunction` trait
pub use functions::Function;
pub use param_type::{ParameterTuple, SupportedParameterType};
pub use ret_type::{ResultType, SupportedReturnType};

/// Re-export for `ParameterValue` enum
pub use crate::flatbuffer_wrappers::function_types::ParameterValue;
/// Re-export for `ReturnType` enum
pub use crate::flatbuffer_wrappers::function_types::ReturnType;
/// Re-export for `ReturnType` enum
pub use crate::flatbuffer_wrappers::function_types::ReturnValue;

mod utils;
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterType, ParameterValue};
use tracing::{Span, instrument};
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;

use super::error::Error;
use super::utils::for_each_tuple;
use crate::HyperlightError::{ParameterValueConversionFailure, UnexpectedNoOfArguments};
use crate::{Result, log_then_return};
use crate::flatbuffer_wrappers::function_types::{ParameterType, ParameterValue};

/// This is a marker trait that is used to indicate that a type is a
/// valid Hyperlight parameter type.
Expand All @@ -34,7 +35,7 @@ pub trait SupportedParameterType: Sized + Clone + Send + Sync + 'static {
/// `SupportedParameterType`
fn into_value(self) -> ParameterValue;
/// Get the actual inner value of this `SupportedParameterType`
fn from_value(value: ParameterValue) -> Result<Self>;
fn from_value(value: ParameterValue) -> Result<Self, Error>;
}

// We can then implement these traits for each type that Hyperlight supports as a parameter or return type
Expand All @@ -57,21 +58,17 @@ macro_rules! impl_supported_param_type {
impl SupportedParameterType for $type {
const TYPE: ParameterType = ParameterType::$enum;

#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn into_value(self) -> ParameterValue {
ParameterValue::$enum(self)
}

#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
fn from_value(value: ParameterValue) -> Result<Self> {
fn from_value(value: ParameterValue) -> Result<Self, Error> {
match value {
ParameterValue::$enum(i) => Ok(i),
other => {
log_then_return!(ParameterValueConversionFailure(
other.clone(),
stringify!($type)
));
}
other => Err(Error::ParameterValueConversionFailure(
other.clone(),
stringify!($type),
)),
}
}
}
Expand All @@ -93,26 +90,22 @@ pub trait ParameterTuple: Sized + Clone + Send + Sync + 'static {
fn into_value(self) -> Vec<ParameterValue>;

/// Get the actual inner value of this `SupportedParameterType`
fn from_value(value: Vec<ParameterValue>) -> Result<Self>;
fn from_value(value: Vec<ParameterValue>) -> Result<Self, Error>;
}

impl<T: SupportedParameterType> ParameterTuple for T {
const SIZE: usize = 1;

const TYPE: &[ParameterType] = &[T::TYPE];

#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn into_value(self) -> Vec<ParameterValue> {
vec![self.into_value()]
}

#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
fn from_value(value: Vec<ParameterValue>) -> Result<Self> {
fn from_value(value: Vec<ParameterValue>) -> Result<Self, Error> {
match <[ParameterValue; 1]>::try_from(value) {
Ok([val]) => Ok(T::from_value(val)?),
Err(value) => {
log_then_return!(UnexpectedNoOfArguments(value.len(), 1));
}
Err(value) => Err(Error::UnexpectedNoOfArguments(value.len(), 1)),
}
}
}
Expand All @@ -126,17 +119,15 @@ macro_rules! impl_param_tuple {
$($param::TYPE),*
];

#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn into_value(self) -> Vec<ParameterValue> {
let ($($name,)*) = self;
vec![$($name.into_value()),*]
}

#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
fn from_value(value: Vec<ParameterValue>) -> Result<Self> {
fn from_value(value: Vec<ParameterValue>) -> Result<Self, Error> {
match <[ParameterValue; $N]>::try_from(value) {
Ok([$($name,)*]) => Ok(($($param::from_value($name)?,)*)),
Err(value) => { log_then_return!(UnexpectedNoOfArguments(value.len(), $N)); }
Err(value) => Err(Error::UnexpectedNoOfArguments(value.len(), $N))
}
}
}
Expand Down
Loading
Loading