Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make EbpfError enum discriminant size explicit #361

Merged
merged 2 commits into from
Aug 10, 2022
Merged
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
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub trait UserDefinedError: 'static + std::error::Error {}

/// Error definitions
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
#[repr(u64)] // discriminant size, used in emit_exception_kind in JIT
pub enum EbpfError<E: UserDefinedError> {
/// User defined error
#[error("{0}")]
Expand Down
45 changes: 45 additions & 0 deletions tests/ubpf_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4158,6 +4158,51 @@ fn test_tcp_sack_nomatch() {
);
}

#[inline(never)]
fn copy_ebpf_error(err: &mut EbpfError<UserError>) {
*err = EbpfError::DivideByZero(0);
}

#[inline(never)]
fn copy_result(err: &mut Result) {
*err = Result::Err(EbpfError::DivideByZero(0));
}

#[test]
fn test_result_discriminant_size() {
unsafe {
// Create a Result<u64, EbpfError<UserError>> filled with 0xaa
let test: [u8; std::mem::size_of::<Result>()] = [0xaa; std::mem::size_of::<Result>()];
let mut test_result =
std::mem::transmute::<[u8; std::mem::size_of::<Result>()], Result>(test);
// Overwrite the Result with another result
copy_result(&mut test_result);

// Check that all 64-bits of the discriminant were changed
let err_kind = *(&test_result as *const _ as *const u64);
assert_eq!(err_kind, 1u64);
}
}

#[test]
fn test_err_discriminant_size() {
unsafe {
// Create a EbpfError filled with 0xaa
let test: [u8; std::mem::size_of::<EbpfError<UserError>>()] =
[0xaa; std::mem::size_of::<EbpfError<UserError>>()];
let mut test_err = std::mem::transmute::<
[u8; std::mem::size_of::<EbpfError<UserError>>()],
EbpfError<UserError>,
>(test);
// Overwrite the EbpfError with another error
copy_ebpf_error(&mut test_err);

// Check that all 64-bits of the discriminant were changed
let err_kind = *(&test_err as *const _ as *const u64);
assert_eq!(err_kind, 7u64);
}
}

// Fuzzy

#[cfg(all(not(windows), target_arch = "x86_64"))]
Expand Down