Skip to main content

sbpf_disassembler/
errors.rs

1use {sbpf_common::errors::SBPFError, thiserror::Error};
2
3#[derive(Debug, Error)]
4pub enum DisassemblerError {
5    #[error("Non-standard ELF header")]
6    NonStandardElfHeader,
7    #[error("Invalid Program Type")]
8    InvalidProgramType,
9    #[error("Invalid Section Header Type")]
10    InvalidSectionHeaderType,
11    #[error("Invalid OpCode")]
12    InvalidOpcode,
13    #[error("Invalid Immediate")]
14    InvalidImmediate,
15    #[error("Invalid data length")]
16    InvalidDataLength,
17    #[error("Invalid string")]
18    InvalidString,
19    #[error("Bytecode error: {0}")]
20    BytecodeError(String),
21    #[error("Missing text section")]
22    MissingTextSection,
23    #[error("Invalid offset in .dynstr section")]
24    InvalidDynstrOffset,
25    #[error("Non-UTF8 data in .dynstr section")]
26    InvalidUtf8InDynstr,
27}
28
29impl From<SBPFError> for DisassemblerError {
30    fn from(err: SBPFError) -> Self {
31        match err {
32            SBPFError::BytecodeError { error, .. } => DisassemblerError::BytecodeError(error),
33        }
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test_from_sbpf_error() {
43        let sbpf_error = SBPFError::BytecodeError {
44            error: "test error".to_string(),
45            span: 0..8,
46            custom_label: None,
47        };
48        let disasm_error: DisassemblerError = sbpf_error.into();
49        assert!(matches!(disasm_error, DisassemblerError::BytecodeError(_)));
50    }
51
52    #[test]
53    fn test_error_display() {
54        assert_eq!(
55            DisassemblerError::NonStandardElfHeader.to_string(),
56            "Non-standard ELF header"
57        );
58        assert_eq!(
59            DisassemblerError::InvalidProgramType.to_string(),
60            "Invalid Program Type"
61        );
62        assert_eq!(
63            DisassemblerError::InvalidSectionHeaderType.to_string(),
64            "Invalid Section Header Type"
65        );
66        assert_eq!(
67            DisassemblerError::InvalidOpcode.to_string(),
68            "Invalid OpCode"
69        );
70        assert_eq!(
71            DisassemblerError::InvalidImmediate.to_string(),
72            "Invalid Immediate"
73        );
74        assert_eq!(
75            DisassemblerError::InvalidDataLength.to_string(),
76            "Invalid data length"
77        );
78        assert_eq!(
79            DisassemblerError::InvalidString.to_string(),
80            "Invalid string"
81        );
82        assert_eq!(
83            DisassemblerError::BytecodeError("custom".to_string()).to_string(),
84            "Bytecode error: custom"
85        );
86        assert_eq!(
87            DisassemblerError::MissingTextSection.to_string(),
88            "Missing text section"
89        );
90    }
91}