Skip to content

Commit 8135271

Browse files
authored
Restructure guest/host error handling (#868)
* Add test that fails due memory leaking when host function error occurs Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> * Update FlatBuffer schema and generation process - Add all.fbs to include all schema files in one place - Restructure function_call_result.fbs to use Result-like union - Add HostError variant to ErrorCode enum in guest_error.fbs - Update flatbuffer generation command in Justfile to use all.fbs - Update documentation for new generation process Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> * Regenerate FlatBuffer generated code Update all generated Rust code based on the new schema definitions. This includes new types for error handling and result structures. Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> * Refactor error handling types and utilities - Update function_types.rs to handle Result-like return values - Simplify guest_error.rs wrapper implementation - Update util.rs for new generated types - Update mod.rs for new generated types Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> * Remove obsolete guest error handling - Remove guest_err.rs from hyperlight_host (replaced by new error handling) - Remove guest_err.rs from hyperlight_guest_bin (replaced by new error handling) - Update func/mod.rs to remove obsolete import Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> * Update host-side error handling - Update initialized_multi_use.rs to use new Result-like error handling - Update mem/mgr.rs to handle host function errors properly - Update sandbox/outb.rs for new error propagation pattern Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> * Update guest-side error handling - Update guest/host_comm.rs to use new Result-like return values - Update guest_bin/call.rs to properly handle host function errors - Update guest_bin/lib.rs to remove obsolete error handling import and make GUEST_HANDLE public (for use in C-API) - Update guest_capi/error.rs to support new error types Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> * Fix up test to work with new error handling Update sandbox_host_tests.rs to use the new Result-like error handling pattern. Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> * Update Cargo dependencies Update Cargo.lock and Cargo.toml files to reflect the dependency changes needed for the new error handling implementation. Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> * Fix test so it passes Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> * First round of PR feedback Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> * Unignore forgotten test Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> * Update flatc version in docs Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> --------- Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
1 parent 8ad812e commit 8135271

36 files changed

+1330
-1162
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ build-fuzzer fuzz-target:
372372
###################
373373

374374
gen-all-fbs-rust-code:
375-
for fbs in `find src -name "*.fbs"`; do flatc -r --rust-module-root-file --gen-all -o ./src/hyperlight_common/src/flatbuffers/ $fbs; done
375+
flatc --rust --rust-module-root-file --gen-all -o ./src/hyperlight_common/src/flatbuffers/ ./src/schema/all.fbs
376376
just fmt-apply
377377

378378
install-vcpkg:

docs/how-to-use-flatbuffers.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# How to use FlatBuffers
22

3-
> Note: the last generation of the flatbuffer code was with done with flatc version 25.2.10 (i.e., the last version as of May 1st, 2025).
3+
> Note: the last generation of the flatbuffer code was with done with flatc version 25.9.23 (i.e., the last version as of Oct 2nd, 2025).
44
55
Flatbuffers is used to serialize and deserialize some data structures.
66

@@ -19,9 +19,3 @@ We recommend building `flatc` from source. To generate rust code, use
1919
```console
2020
just gen-all-fbs-rust-code
2121
```
22-
23-
### Note about generated code
24-
25-
Because we invoke `flatc` multiple times when generating the Rust code, the `mod.rs` generated in `./src/hyperlight_common/src/flatbuffers` is overwritten multiple times and will likely be incorrect. Make sure to manually inspect and if necessary update this file before continuing with your changes as certain modules might be missing. After fixing `mod.rs`, you might need to re-run `just fmt`, since it might not have applied to all generated files if your `mod.rs` was invalid.
26-
27-
>`flatc` does support passing multiple schema files (e.g. it is possible to pass `.\src\schema\*.fbs`), so we could regenerate all the files each time a change was made, however that generates incorrect code (see [here](https://github.com/google/flatbuffers/issues/6800) for details).

src/hyperlight_common/src/flatbuffer_wrappers/function_types.rs

Lines changed: 333 additions & 90 deletions
Large diffs are not rendered by default.

src/hyperlight_common/src/flatbuffer_wrappers/guest_error.rs

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,11 @@ limitations under the License.
1717
extern crate flatbuffers;
1818

1919
use alloc::string::{String, ToString};
20-
use alloc::vec::Vec;
2120

22-
use anyhow::{Error, Result};
23-
use flatbuffers::size_prefixed_root;
2421
#[cfg(feature = "tracing")]
2522
use tracing::{Span, instrument};
2623

27-
use crate::flatbuffers::hyperlight::generated::{
28-
ErrorCode as FbErrorCode, GuestError as FbGuestError, GuestErrorArgs,
29-
};
24+
use crate::flatbuffers::hyperlight::generated::ErrorCode as FbErrorCode;
3025

3126
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
3227
#[repr(C)]
@@ -48,6 +43,7 @@ pub enum ErrorCode {
4843
GuestFunctionParameterTypeMismatch = 14,
4944
GuestError = 15,
5045
ArrayLengthParamIsMissing = 16,
46+
HostFunctionError = 17,
5147
}
5248

5349
impl From<ErrorCode> for FbErrorCode {
@@ -73,6 +69,7 @@ impl From<ErrorCode> for FbErrorCode {
7369
}
7470
ErrorCode::GuestError => Self::GuestError,
7571
ErrorCode::ArrayLengthParamIsMissing => Self::ArrayLengthParamIsMissing,
72+
ErrorCode::HostFunctionError => Self::HostError,
7673
}
7774
}
7875
}
@@ -99,6 +96,7 @@ impl From<FbErrorCode> for ErrorCode {
9996
}
10097
FbErrorCode::GuestError => Self::GuestError,
10198
FbErrorCode::ArrayLengthParamIsMissing => Self::ArrayLengthParamIsMissing,
99+
FbErrorCode::HostError => Self::HostFunctionError,
102100
_ => Self::UnknownError,
103101
}
104102
}
@@ -123,6 +121,7 @@ impl From<u64> for ErrorCode {
123121
14 => Self::GuestFunctionParameterTypeMismatch,
124122
15 => Self::GuestError,
125123
16 => Self::ArrayLengthParamIsMissing,
124+
17 => Self::HostFunctionError,
126125
_ => Self::UnknownError,
127126
}
128127
}
@@ -147,6 +146,7 @@ impl From<ErrorCode> for u64 {
147146
ErrorCode::GuestFunctionParameterTypeMismatch => 14,
148147
ErrorCode::GuestError => 15,
149148
ErrorCode::ArrayLengthParamIsMissing => 16,
149+
ErrorCode::HostFunctionError => 17,
150150
}
151151
}
152152
}
@@ -174,6 +174,7 @@ impl From<ErrorCode> for String {
174174
}
175175
ErrorCode::GuestError => "GuestError".to_string(),
176176
ErrorCode::ArrayLengthParamIsMissing => "ArrayLengthParamIsMissing".to_string(),
177+
ErrorCode::HostFunctionError => "HostFunctionError".to_string(),
177178
}
178179
}
179180
}
@@ -194,44 +195,6 @@ impl GuestError {
194195
}
195196
}
196197

197-
impl TryFrom<&[u8]> for GuestError {
198-
type Error = Error;
199-
fn try_from(value: &[u8]) -> Result<Self> {
200-
let guest_error_fb = size_prefixed_root::<FbGuestError>(value)
201-
.map_err(|e| anyhow::anyhow!("Error while reading GuestError: {:?}", e))?;
202-
let code = guest_error_fb.code();
203-
let message = match guest_error_fb.message() {
204-
Some(message) => message.to_string(),
205-
None => String::new(),
206-
};
207-
Ok(Self {
208-
code: code.into(),
209-
message,
210-
})
211-
}
212-
}
213-
214-
impl TryFrom<&GuestError> for Vec<u8> {
215-
type Error = Error;
216-
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
217-
fn try_from(value: &GuestError) -> Result<Vec<u8>> {
218-
let mut builder = flatbuffers::FlatBufferBuilder::new();
219-
let message = builder.create_string(&value.message);
220-
221-
let guest_error_fb = FbGuestError::create(
222-
&mut builder,
223-
&GuestErrorArgs {
224-
code: value.code.into(),
225-
message: Some(message),
226-
},
227-
);
228-
builder.finish_size_prefixed(guest_error_fb, None);
229-
let res = builder.finished_data().to_vec();
230-
231-
Ok(res)
232-
}
233-
}
234-
235198
impl Default for GuestError {
236199
#[cfg_attr(feature = "tracing", instrument(parent = Span::current(), level= "Trace"))]
237200
fn default() -> Self {

0 commit comments

Comments
 (0)