Skip to content

Commit 2b99c30

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

File tree

16 files changed

+836
-20
lines changed

16 files changed

+836
-20
lines changed

src/hyperlight_common/src/flatbuffer_wrappers/function_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ impl TryFrom<&ReturnValue> for Vec<u8> {
710710
&mut builder,
711711
&hlsizeprefixedbufferArgs {
712712
value: Some(val),
713-
size: v.len() as i32,
713+
size_: v.len() as i32,
714714
},
715715
)
716716
};
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/flatbuffer_wrappers/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl FlatbufferSerializable for &[u8] {
8282
Fbhlsizeprefixedbuffer::create(
8383
builder,
8484
&FbhlsizeprefixedbufferArgs {
85-
size: self.len() as i32,
85+
size_: self.len() as i32,
8686
value: Some(vec_offset),
8787
},
8888
)

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<'a> flatbuffers::Follow<'a> for hlsizeprefixedbuffer<'a> {
3030
}
3131

3232
impl<'a> hlsizeprefixedbuffer<'a> {
33-
pub const VT_SIZE: flatbuffers::VOffsetT = 4;
33+
pub const VT_SIZE_: flatbuffers::VOffsetT = 4;
3434
pub const VT_VALUE: flatbuffers::VOffsetT = 6;
3535

3636
#[inline]
@@ -46,18 +46,18 @@ impl<'a> hlsizeprefixedbuffer<'a> {
4646
if let Some(x) = args.value {
4747
builder.add_value(x);
4848
}
49-
builder.add_size(args.size);
49+
builder.add_size_(args.size_);
5050
builder.finish()
5151
}
5252

5353
#[inline]
54-
pub fn size(&self) -> i32 {
54+
pub fn size_(&self) -> i32 {
5555
// Safety:
5656
// Created from valid Table for this object
5757
// which contains a valid value in this slot
5858
unsafe {
5959
self._tab
60-
.get::<i32>(hlsizeprefixedbuffer::VT_SIZE, Some(0))
60+
.get::<i32>(hlsizeprefixedbuffer::VT_SIZE_, Some(0))
6161
.unwrap()
6262
}
6363
}
@@ -84,7 +84,7 @@ impl flatbuffers::Verifiable for hlsizeprefixedbuffer<'_> {
8484
) -> Result<(), flatbuffers::InvalidFlatbuffer> {
8585
use self::flatbuffers::Verifiable;
8686
v.visit_table(pos)?
87-
.visit_field::<i32>("size", Self::VT_SIZE, false)?
87+
.visit_field::<i32>("size_", Self::VT_SIZE_, false)?
8888
.visit_field::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'_, u8>>>(
8989
"value",
9090
Self::VT_VALUE,
@@ -95,14 +95,14 @@ impl flatbuffers::Verifiable for hlsizeprefixedbuffer<'_> {
9595
}
9696
}
9797
pub struct hlsizeprefixedbufferArgs<'a> {
98-
pub size: i32,
98+
pub size_: i32,
9999
pub value: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a, u8>>>,
100100
}
101101
impl<'a> Default for hlsizeprefixedbufferArgs<'a> {
102102
#[inline]
103103
fn default() -> Self {
104104
hlsizeprefixedbufferArgs {
105-
size: 0,
105+
size_: 0,
106106
value: None,
107107
}
108108
}
@@ -114,9 +114,9 @@ pub struct hlsizeprefixedbufferBuilder<'a: 'b, 'b, A: flatbuffers::Allocator + '
114114
}
115115
impl<'a: 'b, 'b, A: flatbuffers::Allocator + 'a> hlsizeprefixedbufferBuilder<'a, 'b, A> {
116116
#[inline]
117-
pub fn add_size(&mut self, size: i32) {
117+
pub fn add_size_(&mut self, size_: i32) {
118118
self.fbb_
119-
.push_slot::<i32>(hlsizeprefixedbuffer::VT_SIZE, size, 0);
119+
.push_slot::<i32>(hlsizeprefixedbuffer::VT_SIZE_, size_, 0);
120120
}
121121
#[inline]
122122
pub fn add_value(&mut self, value: flatbuffers::WIPOffset<flatbuffers::Vector<'b, u8>>) {
@@ -143,7 +143,7 @@ impl<'a: 'b, 'b, A: flatbuffers::Allocator + 'a> hlsizeprefixedbufferBuilder<'a,
143143
impl core::fmt::Debug for hlsizeprefixedbuffer<'_> {
144144
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
145145
let mut ds = f.debug_struct("hlsizeprefixedbuffer");
146-
ds.field("size", &self.size());
146+
ds.field("size_", &self.size_());
147147
ds.field("value", &self.value());
148148
ds.finish()
149149
}

0 commit comments

Comments
 (0)