Skip to content

Commit db43b0b

Browse files
committed
Revert "[flatbuffer] updated flatbuffer schema to remove host_function_*"
This reverts commit af6c1a9. Signed-off-by: danbugs <[email protected]>
1 parent 5e8c704 commit db43b0b

35 files changed

+795
-236
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
Copyright 2025 The Hyperlight Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
use alloc::string::{String, ToString};
18+
use alloc::vec::Vec;
19+
20+
use anyhow::{Error, Result, anyhow};
21+
use flatbuffers::{FlatBufferBuilder, WIPOffset};
22+
#[cfg(feature = "tracing")]
23+
use tracing::{Span, instrument};
24+
25+
use super::function_types::{ParameterType, ReturnType};
26+
use crate::flatbuffers::hyperlight::generated::{
27+
HostFunctionDefinition as FbHostFunctionDefinition,
28+
HostFunctionDefinitionArgs as FbHostFunctionDefinitionArgs, ParameterType as FbParameterType,
29+
};
30+
31+
/// The definition of a function exposed from the host to the guest
32+
#[derive(Debug, Default, Clone, PartialEq, Eq)]
33+
pub struct HostFunctionDefinition {
34+
/// The function name
35+
pub function_name: String,
36+
/// The type of the parameter values for the host function call.
37+
pub parameter_types: Option<Vec<ParameterType>>,
38+
/// The type of the return value from the host function call
39+
pub return_type: ReturnType,
40+
}
41+
42+
impl HostFunctionDefinition {
43+
/// Create a new `HostFunctionDefinition`.
44+
#[cfg_attr(feature = "tracing", instrument(skip_all, parent = Span::current(), level= "Trace"))]
45+
pub fn new(
46+
function_name: String,
47+
parameter_types: Option<Vec<ParameterType>>,
48+
return_type: ReturnType,
49+
) -> Self {
50+
Self {
51+
function_name,
52+
parameter_types,
53+
return_type,
54+
}
55+
}
56+
57+
/// Convert this `HostFunctionDefinition` into a `WIPOffset<FbHostFunctionDefinition>`.
58+
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
59+
pub(crate) fn convert_to_flatbuffer_def<'a>(
60+
&self,
61+
builder: &mut FlatBufferBuilder<'a>,
62+
) -> Result<WIPOffset<FbHostFunctionDefinition<'a>>> {
63+
let host_function_name = builder.create_string(&self.function_name);
64+
let return_value_type = self.return_type.into();
65+
let vec_parameters = match &self.parameter_types {
66+
Some(vec_pvt) => {
67+
let num_items = vec_pvt.len();
68+
let mut parameters: Vec<FbParameterType> = Vec::with_capacity(num_items);
69+
for pvt in vec_pvt {
70+
let fb_pvt = pvt.clone().into();
71+
parameters.push(fb_pvt);
72+
}
73+
Some(builder.create_vector(&parameters))
74+
}
75+
None => None,
76+
};
77+
78+
let fb_host_function_definition: WIPOffset<FbHostFunctionDefinition> =
79+
FbHostFunctionDefinition::create(
80+
builder,
81+
&FbHostFunctionDefinitionArgs {
82+
function_name: Some(host_function_name),
83+
return_type: return_value_type,
84+
parameters: vec_parameters,
85+
},
86+
);
87+
88+
Ok(fb_host_function_definition)
89+
}
90+
91+
/// Verify that the function call has the correct parameter types.
92+
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
93+
pub fn verify_equal_parameter_types(
94+
&self,
95+
function_call_parameter_types: &[ParameterType],
96+
) -> Result<()> {
97+
if let Some(parameter_types) = &self.parameter_types {
98+
for (i, parameter_type) in parameter_types.iter().enumerate() {
99+
if parameter_type != &function_call_parameter_types[i] {
100+
return Err(anyhow!("Incorrect parameter type for parameter {}", i + 1));
101+
}
102+
}
103+
}
104+
Ok(())
105+
}
106+
}
107+
108+
impl TryFrom<&FbHostFunctionDefinition<'_>> for HostFunctionDefinition {
109+
type Error = Error;
110+
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
111+
fn try_from(value: &FbHostFunctionDefinition) -> Result<Self> {
112+
let function_name = value.function_name().to_string();
113+
let return_type = value.return_type().try_into().map_err(|_| {
114+
anyhow!(
115+
"Failed to convert return type for function {}",
116+
function_name
117+
)
118+
})?;
119+
let parameter_types = match value.parameters() {
120+
Some(pvt) => {
121+
let len = pvt.len();
122+
let mut pv: Vec<ParameterType> = Vec::with_capacity(len);
123+
for fb_pvt in pvt {
124+
let pvt: ParameterType = fb_pvt.try_into().map_err(|_| {
125+
anyhow!(
126+
"Failed to convert parameter type for function {}",
127+
function_name
128+
)
129+
})?;
130+
pv.push(pvt);
131+
}
132+
Some(pv)
133+
}
134+
None => None,
135+
};
136+
137+
Ok(Self::new(function_name, parameter_types, return_type))
138+
}
139+
}
140+
141+
impl TryFrom<&[u8]> for HostFunctionDefinition {
142+
type Error = Error;
143+
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
144+
fn try_from(value: &[u8]) -> Result<Self> {
145+
let fb_host_function_definition = flatbuffers::root::<FbHostFunctionDefinition<'_>>(value)
146+
.map_err(|e| anyhow!("Error while reading HostFunctionDefinition: {:?}", e))?;
147+
Self::try_from(&fb_host_function_definition)
148+
}
149+
}
150+
151+
impl TryFrom<&HostFunctionDefinition> for Vec<u8> {
152+
type Error = Error;
153+
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
154+
fn try_from(hfd: &HostFunctionDefinition) -> Result<Vec<u8>> {
155+
let mut builder = flatbuffers::FlatBufferBuilder::new();
156+
let host_function_definition = hfd.convert_to_flatbuffer_def(&mut builder)?;
157+
builder.finish_size_prefixed(host_function_definition, None);
158+
Ok(builder.finished_data().to_vec())
159+
}
160+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Copyright 2025 The Hyperlight Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
use alloc::vec::Vec;
18+
19+
use anyhow::{Error, Result};
20+
use flatbuffers::{WIPOffset, size_prefixed_root};
21+
#[cfg(feature = "tracing")]
22+
use tracing::{Span, instrument};
23+
24+
use super::host_function_definition::HostFunctionDefinition;
25+
use crate::flatbuffers::hyperlight::generated::{
26+
HostFunctionDefinition as FbHostFunctionDefinition,
27+
HostFunctionDetails as FbHostFunctionDetails,
28+
HostFunctionDetailsArgs as FbHostFunctionDetailsArgs,
29+
};
30+
31+
/// `HostFunctionDetails` represents the set of functions that the host exposes to the guest.
32+
#[derive(Debug, Default, Clone)]
33+
pub struct HostFunctionDetails {
34+
/// The host functions.
35+
pub host_functions: Option<Vec<HostFunctionDefinition>>,
36+
}
37+
38+
impl TryFrom<&[u8]> for HostFunctionDetails {
39+
type Error = Error;
40+
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
41+
fn try_from(value: &[u8]) -> Result<Self> {
42+
let host_function_details_fb = size_prefixed_root::<FbHostFunctionDetails>(value)
43+
.map_err(|e| anyhow::anyhow!("Error while reading HostFunctionDetails: {:?}", e))?;
44+
45+
let host_function_definitions = match host_function_details_fb.functions() {
46+
Some(hfd) => {
47+
let len = hfd.len();
48+
let mut vec_hfd: Vec<HostFunctionDefinition> = Vec::with_capacity(len);
49+
for i in 0..len {
50+
let fb_host_function_definition = hfd.get(i);
51+
let hfdef = HostFunctionDefinition::try_from(&fb_host_function_definition)?;
52+
vec_hfd.push(hfdef);
53+
}
54+
55+
Some(vec_hfd)
56+
}
57+
58+
None => None,
59+
};
60+
61+
Ok(Self {
62+
host_functions: host_function_definitions,
63+
})
64+
}
65+
}
66+
67+
impl TryFrom<&HostFunctionDetails> for Vec<u8> {
68+
type Error = Error;
69+
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
70+
fn try_from(value: &HostFunctionDetails) -> Result<Vec<u8>> {
71+
let mut builder = flatbuffers::FlatBufferBuilder::new();
72+
let vec_host_function_definitions = match &value.host_functions {
73+
Some(vec_hfd) => {
74+
let num_items = vec_hfd.len();
75+
let mut host_function_definitions: Vec<WIPOffset<FbHostFunctionDefinition>> =
76+
Vec::with_capacity(num_items);
77+
78+
for hfd in vec_hfd {
79+
let host_function_definition = hfd.convert_to_flatbuffer_def(&mut builder)?;
80+
host_function_definitions.push(host_function_definition);
81+
}
82+
83+
Some(host_function_definitions)
84+
}
85+
None => None,
86+
};
87+
88+
let fb_host_function_definitions =
89+
vec_host_function_definitions.map(|v| builder.create_vector(&v));
90+
91+
let host_function_details = FbHostFunctionDetails::create(
92+
&mut builder,
93+
&FbHostFunctionDetailsArgs {
94+
functions: fb_host_function_definitions,
95+
},
96+
);
97+
builder.finish_size_prefixed(host_function_details, None);
98+
let res = builder.finished_data().to_vec();
99+
100+
Ok(res)
101+
}
102+
}

src/hyperlight_common/src/flatbuffer_wrappers/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ pub mod guest_error;
2121
pub mod guest_log_data;
2222
/// cbindgen:ignore
2323
pub mod guest_log_level;
24+
/// cbindgen:ignore
25+
pub mod host_function_definition;
26+
/// cbindgen:ignore
27+
pub mod host_function_details;
2428
pub mod util;

src/hyperlight_common/src/flatbuffers/hyperlight/generated/error_code_generated.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,16 @@ impl<'a> flatbuffers::Follow<'a> for ErrorCode {
124124
type Inner = Self;
125125
#[inline]
126126
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
127-
unsafe {
128-
let b = flatbuffers::read_scalar_at::<u64>(buf, loc);
129-
Self(b)
130-
}
127+
let b = flatbuffers::read_scalar_at::<u64>(buf, loc);
128+
Self(b)
131129
}
132130
}
133131

134132
impl flatbuffers::Push for ErrorCode {
135133
type Output = ErrorCode;
136134
#[inline]
137135
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
138-
unsafe {
139-
flatbuffers::emplace_scalar::<u64>(dst, self.0);
140-
}
136+
flatbuffers::emplace_scalar::<u64>(dst, self.0);
141137
}
142138
}
143139

src/hyperlight_common/src/flatbuffers/hyperlight/generated/function_call_generated.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ impl<'a> flatbuffers::Follow<'a> for FunctionCall<'a> {
2121
type Inner = FunctionCall<'a>;
2222
#[inline]
2323
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
24-
unsafe {
25-
Self {
26-
_tab: flatbuffers::Table::new(buf, loc),
27-
}
24+
Self {
25+
_tab: flatbuffers::Table::new(buf, loc),
2826
}
2927
}
3028
}
@@ -292,14 +290,14 @@ pub fn size_prefixed_root_as_function_call_with_opts<'b, 'o>(
292290
/// # Safety
293291
/// Callers must trust the given bytes do indeed contain a valid `FunctionCall`.
294292
pub unsafe fn root_as_function_call_unchecked(buf: &[u8]) -> FunctionCall {
295-
unsafe { flatbuffers::root_unchecked::<FunctionCall>(buf) }
293+
flatbuffers::root_unchecked::<FunctionCall>(buf)
296294
}
297295
#[inline]
298296
/// Assumes, without verification, that a buffer of bytes contains a size prefixed FunctionCall and returns it.
299297
/// # Safety
300298
/// Callers must trust the given bytes do indeed contain a valid size prefixed `FunctionCall`.
301299
pub unsafe fn size_prefixed_root_as_function_call_unchecked(buf: &[u8]) -> FunctionCall {
302-
unsafe { flatbuffers::size_prefixed_root_unchecked::<FunctionCall>(buf) }
300+
flatbuffers::size_prefixed_root_unchecked::<FunctionCall>(buf)
303301
}
304302
#[inline]
305303
pub fn finish_function_call_buffer<'a, 'b, A: flatbuffers::Allocator + 'a>(

src/hyperlight_common/src/flatbuffers/hyperlight/generated/function_call_result_generated.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ impl<'a> flatbuffers::Follow<'a> for FunctionCallResult<'a> {
2121
type Inner = FunctionCallResult<'a>;
2222
#[inline]
2323
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
24-
unsafe {
25-
Self {
26-
_tab: flatbuffers::Table::new(buf, loc),
27-
}
24+
Self {
25+
_tab: flatbuffers::Table::new(buf, loc),
2826
}
2927
}
3028
}
@@ -515,7 +513,7 @@ pub fn size_prefixed_root_as_function_call_result_with_opts<'b, 'o>(
515513
/// # Safety
516514
/// Callers must trust the given bytes do indeed contain a valid `FunctionCallResult`.
517515
pub unsafe fn root_as_function_call_result_unchecked(buf: &[u8]) -> FunctionCallResult {
518-
unsafe { flatbuffers::root_unchecked::<FunctionCallResult>(buf) }
516+
flatbuffers::root_unchecked::<FunctionCallResult>(buf)
519517
}
520518
#[inline]
521519
/// Assumes, without verification, that a buffer of bytes contains a size prefixed FunctionCallResult and returns it.
@@ -524,7 +522,7 @@ pub unsafe fn root_as_function_call_result_unchecked(buf: &[u8]) -> FunctionCall
524522
pub unsafe fn size_prefixed_root_as_function_call_result_unchecked(
525523
buf: &[u8],
526524
) -> FunctionCallResult {
527-
unsafe { flatbuffers::size_prefixed_root_unchecked::<FunctionCallResult>(buf) }
525+
flatbuffers::size_prefixed_root_unchecked::<FunctionCallResult>(buf)
528526
}
529527
#[inline]
530528
pub fn finish_function_call_result_buffer<'a, 'b, A: flatbuffers::Allocator + 'a>(

src/hyperlight_common/src/flatbuffers/hyperlight/generated/function_call_type_generated.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,16 @@ impl<'a> flatbuffers::Follow<'a> for FunctionCallType {
6666
type Inner = Self;
6767
#[inline]
6868
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
69-
unsafe {
70-
let b = flatbuffers::read_scalar_at::<u8>(buf, loc);
71-
Self(b)
72-
}
69+
let b = flatbuffers::read_scalar_at::<u8>(buf, loc);
70+
Self(b)
7371
}
7472
}
7573

7674
impl flatbuffers::Push for FunctionCallType {
7775
type Output = FunctionCallType;
7876
#[inline]
7977
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
80-
unsafe {
81-
flatbuffers::emplace_scalar::<u8>(dst, self.0);
82-
}
78+
flatbuffers::emplace_scalar::<u8>(dst, self.0);
8379
}
8480
}
8581

0 commit comments

Comments
 (0)