Skip to content

Commit fd4147d

Browse files
committed
Use new API in host and guest
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
1 parent f4f9472 commit fd4147d

File tree

10 files changed

+57
-46
lines changed

10 files changed

+57
-46
lines changed

src/hyperlight_guest/src/guest_handle/host_comm.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use alloc::string::ToString;
1919
use alloc::vec::Vec;
2020
use core::slice::from_raw_parts;
2121

22-
use flatbuffers::FlatBufferBuilder;
2322
use hyperlight_common::flatbuffer_wrappers::function_call::{FunctionCall, FunctionCallType};
2423
use hyperlight_common::flatbuffer_wrappers::function_types::{
2524
FunctionCallResult, ParameterValue, ReturnType, ReturnValue,
@@ -28,7 +27,9 @@ use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
2827
use hyperlight_common::flatbuffer_wrappers::guest_log_data::GuestLogData;
2928
use hyperlight_common::flatbuffer_wrappers::guest_log_level::LogLevel;
3029
use hyperlight_common::flatbuffer_wrappers::host_function_details::HostFunctionDetails;
31-
use hyperlight_common::flatbuffer_wrappers::util::estimate_flatbuffer_capacity;
30+
use hyperlight_common::flatbuffer_wrappers::util::{
31+
decode, encode, encode_extend, estimate_flatbuffer_capacity,
32+
};
3233
use hyperlight_common::outb::OutBAction;
3334
use tracing::instrument;
3435

@@ -124,10 +125,15 @@ impl GuestHandle {
124125
return_type,
125126
);
126127

127-
let mut builder = FlatBufferBuilder::with_capacity(estimated_capacity);
128-
129-
let host_function_call_buffer = host_function_call.encode(&mut builder);
130-
self.push_shared_output_data(host_function_call_buffer)?;
128+
let host_function_call_buffer = Vec::with_capacity(estimated_capacity);
129+
let host_function_call_buffer =
130+
encode_extend(&host_function_call, host_function_call_buffer).map_err(|e| {
131+
HyperlightGuestError::new(
132+
ErrorCode::GuestError,
133+
format!("Error serializing host function call to flatbuffer: {}", e),
134+
)
135+
})?;
136+
self.push_shared_output_data(&host_function_call_buffer)?;
131137

132138
unsafe {
133139
out32(OutBAction::CallFunction as u16, 0);
@@ -163,8 +169,7 @@ impl GuestHandle {
163169
let host_function_details_slice: &[u8] =
164170
unsafe { from_raw_parts(host_function_details_buffer, host_function_details_size) };
165171

166-
host_function_details_slice
167-
.try_into()
172+
decode(host_function_details_slice)
168173
.expect("Failed to convert buffer to HostFunctionDetails")
169174
}
170175

@@ -189,9 +194,8 @@ impl GuestHandle {
189194
line,
190195
);
191196

192-
let bytes: Vec<u8> = guest_log_data
193-
.try_into()
194-
.expect("Failed to convert GuestLogData to bytes");
197+
let bytes: Vec<u8> =
198+
encode(&guest_log_data).expect("Failed to convert GuestLogData to bytes");
195199

196200
self.push_shared_output_data(&bytes)
197201
.expect("Unable to push log data to shared output data");

src/hyperlight_guest/src/guest_handle/io.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use core::any::type_name;
2020
use core::slice::from_raw_parts_mut;
2121

2222
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
23+
use hyperlight_common::flatbuffer_wrappers::util::{Deserialize, decode};
2324
use tracing::{Span, instrument};
2425

2526
use super::handle::GuestHandle;
@@ -30,7 +31,7 @@ impl GuestHandle {
3031
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
3132
pub fn try_pop_shared_input_data_into<T>(&self) -> Result<T>
3233
where
33-
T: for<'a> TryFrom<&'a [u8]>,
34+
T: for<'a> Deserialize<'a>,
3435
{
3536
let peb_ptr = self.peb().unwrap();
3637
let input_stack_size = unsafe { (*peb_ptr).input_stack.size as usize };
@@ -69,7 +70,7 @@ impl GuestHandle {
6970
let buffer = &idb[last_element_offset_rel as usize..];
7071

7172
// convert the buffer to T
72-
let type_t = match T::try_from(buffer) {
73+
let type_t = match decode::<T>(buffer) {
7374
Ok(t) => Ok(t),
7475
Err(_e) => {
7576
return Err(HyperlightGuestError::new(

src/hyperlight_guest_bin/src/guest_function/call.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ limitations under the License.
1717
use alloc::format;
1818
use alloc::vec::Vec;
1919

20-
use flatbuffers::FlatBufferBuilder;
2120
use hyperlight_common::flatbuffer_wrappers::function_call::{FunctionCall, FunctionCallType};
2221
use hyperlight_common::flatbuffer_wrappers::function_types::{FunctionCallResult, ParameterType};
2322
use hyperlight_common::flatbuffer_wrappers::guest_error::{ErrorCode, GuestError};
23+
use hyperlight_common::flatbuffer_wrappers::util::encode;
2424
use hyperlight_guest::error::{HyperlightGuestError, Result};
2525
use hyperlight_guest::exit::halt;
2626
use tracing::{Span, instrument};
@@ -107,10 +107,9 @@ fn internal_dispatch_function() {
107107
Err(err) => {
108108
let guest_error = Err(GuestError::new(err.kind, err.message));
109109
let fcr = FunctionCallResult::new(guest_error);
110-
let mut builder = FlatBufferBuilder::new();
111-
let data = fcr.encode(&mut builder);
110+
let data = encode(&fcr).unwrap();
112111
handle
113-
.push_shared_output_data(data)
112+
.push_shared_output_data(&data)
114113
.expect("Failed to serialize function call result");
115114
}
116115
}

src/hyperlight_guest_capi/src/error.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ limitations under the License.
1616

1717
use core::ffi::{CStr, c_char};
1818

19-
use flatbuffers::FlatBufferBuilder;
2019
use hyperlight_common::flatbuffer_wrappers::function_types::FunctionCallResult;
2120
use hyperlight_common::flatbuffer_wrappers::guest_error::{ErrorCode, GuestError};
21+
use hyperlight_common::flatbuffer_wrappers::util::encode;
2222
use hyperlight_guest_bin::GUEST_HANDLE;
2323

2424
use crate::alloc::borrow::ToOwned;
@@ -33,12 +33,11 @@ pub extern "C" fn hl_set_error(err: ErrorCode, message: *const c_char) {
3333
.to_owned(),
3434
));
3535
let fcr = FunctionCallResult::new(guest_error);
36-
let mut builder = FlatBufferBuilder::new();
37-
let data = fcr.encode(&mut builder);
36+
let data = encode(&fcr).unwrap();
3837
unsafe {
3938
#[allow(static_mut_refs)] // we are single threaded
4039
GUEST_HANDLE
41-
.push_shared_output_data(data)
40+
.push_shared_output_data(&data)
4241
.expect("Failed to set error")
4342
}
4443
}

src/hyperlight_host/benches/benchmarks.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ use std::thread;
2424
use std::time::{Duration, Instant};
2525

2626
use criterion::{Criterion, criterion_group, criterion_main};
27-
use flatbuffers::FlatBufferBuilder;
2827
use hyperlight_common::flatbuffer_wrappers::function_call::{FunctionCall, FunctionCallType};
2928
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
30-
use hyperlight_common::flatbuffer_wrappers::util::estimate_flatbuffer_capacity;
29+
use hyperlight_common::flatbuffer_wrappers::util::{
30+
decode, encode, encode_extend, estimate_flatbuffer_capacity,
31+
};
3132
use hyperlight_host::GuestBinary;
3233
use hyperlight_host::sandbox::{MultiUseSandbox, SandboxConfiguration, UninitializedSandbox};
3334
use hyperlight_testing::sandbox_sizes::{LARGE_HEAP_SIZE, MEDIUM_HEAP_SIZE, SMALL_HEAP_SIZE};
@@ -436,18 +437,17 @@ fn function_call_serialization_benchmark(c: &mut Criterion) {
436437
function_call.function_name.as_str(),
437438
function_call.parameters.as_deref().unwrap_or(&[]),
438439
);
439-
let mut builder = FlatBufferBuilder::with_capacity(estimated_capacity);
440-
let serialized: &[u8] = function_call.encode(&mut builder);
440+
let serialized = Vec::with_capacity(estimated_capacity);
441+
let serialized = encode_extend(&function_call, serialized).unwrap();
441442
std::hint::black_box(serialized);
442443
});
443444
});
444445

445446
group.bench_function("deserialize_function_call", |b| {
446-
let mut builder = FlatBufferBuilder::new();
447-
let bytes = function_call.clone().encode(&mut builder);
447+
let bytes = encode(&function_call).unwrap();
448448

449449
b.iter(|| {
450-
let deserialized: FunctionCall = bytes.try_into().unwrap();
450+
let deserialized: FunctionCall = decode(&bytes).unwrap();
451451
std::hint::black_box(deserialized);
452452
});
453453
});

src/hyperlight_host/src/mem/mgr.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ limitations under the License.
1616
#[cfg(feature = "init-paging")]
1717
use std::cmp::Ordering;
1818

19-
use flatbuffers::FlatBufferBuilder;
2019
use hyperlight_common::flatbuffer_wrappers::function_call::{
2120
FunctionCall, validate_guest_function_call_buffer,
2221
};
2322
use hyperlight_common::flatbuffer_wrappers::function_types::FunctionCallResult;
2423
use hyperlight_common::flatbuffer_wrappers::guest_log_data::GuestLogData;
2524
use hyperlight_common::flatbuffer_wrappers::host_function_details::HostFunctionDetails;
25+
use hyperlight_common::flatbuffer_wrappers::util::{decode, encode};
2626
#[cfg(feature = "init-paging")]
2727
use hyperlight_common::vmem;
2828
#[cfg(feature = "init-paging")]
@@ -221,14 +221,14 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
221221
/// Writes host function details to memory
222222
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
223223
pub(crate) fn write_buffer_host_function_details(&mut self, buffer: &[u8]) -> Result<()> {
224-
let host_function_details = HostFunctionDetails::try_from(buffer).map_err(|e| {
224+
let host_function_details: HostFunctionDetails = decode(buffer).map_err(|e| {
225225
new_error!(
226226
"write_buffer_host_function_details: failed to convert buffer to HostFunctionDetails: {}",
227227
e
228228
)
229229
})?;
230230

231-
let host_function_call_buffer: Vec<u8> = (&host_function_details).try_into().map_err(|_| {
231+
let host_function_call_buffer: Vec<u8> = encode(&host_function_details).map_err(|_| {
232232
new_error!(
233233
"write_buffer_host_function_details: failed to convert HostFunctionDetails to Vec<u8>"
234234
)
@@ -353,13 +353,16 @@ impl SandboxMemoryManager<HostSharedMemory> {
353353
&mut self,
354354
res: &FunctionCallResult,
355355
) -> Result<()> {
356-
let mut builder = FlatBufferBuilder::new();
357-
let data = res.encode(&mut builder);
356+
let data = encode(res).map_err(|_| {
357+
new_error!(
358+
"write_response_from_host_function_call: failed to convert FunctionCallResult to Vec<u8>"
359+
)
360+
})?;
358361

359362
self.shared_mem.push_buffer(
360363
self.layout.input_data_buffer_offset,
361364
self.layout.sandbox_memory_config.get_input_data_size(),
362-
data,
365+
&data,
363366
)
364367
}
365368

src/hyperlight_host/src/mem/shared_mem.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::io::Error;
2121
use std::ptr::null_mut;
2222
use std::sync::{Arc, RwLock};
2323

24+
use hyperlight_common::flatbuffer_wrappers::util::{Deserialize, decode};
2425
use hyperlight_common::mem::PAGE_SIZE_USIZE;
2526
use tracing::{Span, instrument};
2627
#[cfg(target_os = "windows")]
@@ -887,7 +888,7 @@ impl HostSharedMemory {
887888
buffer_size: usize,
888889
) -> Result<T>
889890
where
890-
T: for<'b> TryFrom<&'b [u8]>,
891+
T: for<'b> Deserialize<'b>,
891892
{
892893
// get the stackpointer
893894
let stack_pointer_rel = self.read::<u64>(buffer_start_offset)? as usize;
@@ -912,17 +913,20 @@ impl HostSharedMemory {
912913

913914
// Get the size of the flatbuffer buffer from memory
914915
let fb_buffer_size = {
916+
stack_pointer_rel - last_element_offset_rel - 8
917+
/*
915918
let size_i32 = self.read::<u32>(last_element_offset_abs)? + 4;
916919
// ^^^ flatbuffer byte arrays are prefixed by 4 bytes
917920
// indicating its size, so, to get the actual size, we need
918921
// to add 4.
919922
usize::try_from(size_i32)
920-
}?;
923+
*/
924+
};
921925

922926
let mut result_buffer = vec![0; fb_buffer_size];
923927

924928
self.copy_to_slice(&mut result_buffer, last_element_offset_abs)?;
925-
let to_return = T::try_from(result_buffer.as_slice()).map_err(|_e| {
929+
let to_return: T = decode(result_buffer.as_slice()).map_err(|_e| {
926930
new_error!(
927931
"pop_buffer_into: failed to convert buffer to {}",
928932
type_name::<T>()

src/hyperlight_host/src/sandbox/host_funcs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use hyperlight_common::flatbuffer_wrappers::function_types::{
2222
};
2323
use hyperlight_common::flatbuffer_wrappers::host_function_definition::HostFunctionDefinition;
2424
use hyperlight_common::flatbuffer_wrappers::host_function_details::HostFunctionDetails;
25+
use hyperlight_common::flatbuffer_wrappers::util::encode;
2526
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
2627
use tracing::{Span, instrument};
2728

@@ -74,7 +75,7 @@ impl FunctionRegistry {
7475

7576
let hfd = HostFunctionDetails::from(self);
7677

77-
let buffer: Vec<u8> = (&hfd).try_into().map_err(|e| {
78+
let buffer: Vec<u8> = encode(&hfd).map_err(|e| {
7879
new_error!(
7980
"Error serializing host function details to flatbuffer: {}",
8081
e

src/hyperlight_host/src/sandbox/initialized_multi_use.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ use std::path::Path;
2323
use std::sync::atomic::Ordering;
2424
use std::sync::{Arc, Mutex};
2525

26-
use flatbuffers::FlatBufferBuilder;
2726
use hyperlight_common::flatbuffer_wrappers::function_call::{FunctionCall, FunctionCallType};
2827
use hyperlight_common::flatbuffer_wrappers::function_types::{
2928
ParameterValue, ReturnType, ReturnValue,
3029
};
3130
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
32-
use hyperlight_common::flatbuffer_wrappers::util::estimate_flatbuffer_capacity;
31+
use hyperlight_common::flatbuffer_wrappers::util::{encode_extend, estimate_flatbuffer_capacity};
3332
use tracing::{Span, instrument};
3433

3534
use super::Callable;
@@ -592,10 +591,10 @@ impl MultiUseSandbox {
592591
return_type,
593592
);
594593

595-
let mut builder = FlatBufferBuilder::with_capacity(estimated_capacity);
596-
let buffer = fc.encode(&mut builder);
594+
let buffer = Vec::with_capacity(estimated_capacity);
595+
let buffer = encode_extend(&fc, buffer).unwrap();
597596

598-
self.mem_mgr.write_guest_function_call(buffer)?;
597+
self.mem_mgr.write_guest_function_call(&buffer)?;
599598

600599
self.vm.dispatch_call_from_host(
601600
self.dispatch_ptr.clone(),

src/hyperlight_host/src/sandbox/outb.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ pub(crate) fn handle_outb(
196196
#[cfg(test)]
197197
mod tests {
198198
use hyperlight_common::flatbuffer_wrappers::guest_log_level::LogLevel;
199+
use hyperlight_common::flatbuffer_wrappers::util::encode;
199200
use hyperlight_testing::logger::{LOGGER, Logger};
200201
use hyperlight_testing::simple_guest_as_string;
201202
use log::Level;
@@ -254,7 +255,7 @@ mod tests {
254255
let mut mgr = new_mgr();
255256
let log_msg = new_guest_log_data(LogLevel::Information);
256257

257-
let guest_log_data_buffer: Vec<u8> = log_msg.try_into().unwrap();
258+
let guest_log_data_buffer: Vec<u8> = encode(&log_msg).unwrap();
258259
let offset = mgr.layout.get_output_data_offset();
259260
mgr.get_shared_mem_mut()
260261
.push_buffer(
@@ -292,7 +293,7 @@ mod tests {
292293
let layout = mgr.layout;
293294
let log_data = new_guest_log_data(level);
294295

295-
let guest_log_data_buffer: Vec<u8> = log_data.clone().try_into().unwrap();
296+
let guest_log_data_buffer: Vec<u8> = encode(&log_data).unwrap();
296297
mgr.get_shared_mem_mut()
297298
.push_buffer(
298299
layout.get_output_data_offset(),
@@ -374,7 +375,7 @@ mod tests {
374375
let log_data: GuestLogData = new_guest_log_data(level);
375376
subscriber.clear();
376377

377-
let guest_log_data_buffer: Vec<u8> = log_data.try_into().unwrap();
378+
let guest_log_data_buffer: Vec<u8> = encode(&log_data).unwrap();
378379
mgr.get_shared_mem_mut()
379380
.push_buffer(
380381
layout.get_output_data_offset(),

0 commit comments

Comments
 (0)