diff --git a/src/error.rs b/src/error.rs index bdd370f9..6163747c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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 { /// User defined error #[error("{0}")] diff --git a/tests/ubpf_execution.rs b/tests/ubpf_execution.rs index 40d77f5c..8c8cbc36 100644 --- a/tests/ubpf_execution.rs +++ b/tests/ubpf_execution.rs @@ -4158,6 +4158,51 @@ fn test_tcp_sack_nomatch() { ); } +#[inline(never)] +fn copy_ebpf_error(err: &mut EbpfError) { + *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> filled with 0xaa + let test: [u8; std::mem::size_of::()] = [0xaa; std::mem::size_of::()]; + let mut test_result = + std::mem::transmute::<[u8; std::mem::size_of::()], 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::>()] = + [0xaa; std::mem::size_of::>()]; + let mut test_err = std::mem::transmute::< + [u8; std::mem::size_of::>()], + EbpfError, + >(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"))]