orca_wasm/
error.rs

1use std::ops::Range;
2
3use wasmparser::BinaryReaderError;
4
5/// Error for parsing
6#[derive(Debug, Clone)]
7#[allow(clippy::enum_variant_names)]
8pub enum Error {
9    BinaryReaderError(BinaryReaderError),
10    UnknownVersion(u32),
11    UnknownSection {
12        section_id: u8,
13    },
14    MissingFunctionEnd {
15        func_range: Range<usize>,
16    },
17    IncorrectDataCount {
18        declared_count: usize,
19        actual_count: usize,
20    },
21    ConversionError(String),
22    IncorrectCodeCounts {
23        function_section_count: usize,
24        code_section_declared_count: usize,
25        code_section_actual_count: usize,
26    },
27    MultipleStartSections,
28    /// `memory.grow` and `memory.size` operations must have a 0x00 byte
29    /// immediately after the instruction (it is not valid to have some other
30    /// variable length encoding representation of 0). This is because the
31    /// immediate byte will be used to reference other memories in the
32    /// multi-memory proposal.
33    InvalidMemoryReservedByte {
34        func_range: Range<usize>,
35    },
36}
37
38impl From<BinaryReaderError> for Error {
39    fn from(e: BinaryReaderError) -> Self {
40        Self::BinaryReaderError(e)
41    }
42}
43
44impl std::fmt::Display for Error {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        match self {
47            Error::BinaryReaderError(err) => {
48                write!(f, "Error from wasmparser: {}", err)
49            }
50            Error::UnknownVersion(ver) => {
51                write!(f, "Unknown version: {}", ver)
52            }
53            Error::UnknownSection { section_id } => {
54                write!(f, "Unknown section: {}", section_id)
55            }
56            Error::MissingFunctionEnd { func_range } => {
57                write!(
58                    f,
59                    "Missing function End for function in range {} - {}",
60                    func_range.start, func_range.end
61                )
62            }
63            Error::IncorrectDataCount {
64                declared_count,
65                actual_count,
66            } => {
67                write!(
68                    f,
69                    "Incorrect data count. Declared: {}, actual: {}",
70                    declared_count, actual_count
71                )
72            }
73            Error::ConversionError(s) => {
74                write!(
75                    f,
76                    "Unable to convert wasmparser type to wasm-encoder: {}",
77                    s
78                )
79            }
80            Error::IncorrectCodeCounts {
81                function_section_count,
82                code_section_declared_count,
83                code_section_actual_count,
84            } => {
85                write!(
86                    f,
87                    "Incorrect code counts. Function section count: {}, code section declared count: {}, code section actual count: {}",
88                    function_section_count, code_section_declared_count, code_section_actual_count
89                )
90            }
91            Error::MultipleStartSections => {
92                write!(f, "Multiple start sections")
93            }
94            Error::InvalidMemoryReservedByte { func_range } => {
95                write!(f, "Found a `memory.*` instruction with an invalid reserved byte in function at {:?}", func_range)
96            }
97        }
98    }
99}