wasm/core/decoding/
error.rs

1use core::{error, fmt, str};
2
3use crate::core::decoding::modules::sections::SectionTy;
4
5#[derive(Debug, PartialEq, Eq, Clone)]
6pub enum DecodingError {
7    /// The magic number at the start of the Wasm bytecode is invalid.
8    InvalidMagic,
9    /// The binary format version at the start of the Wasm bytecode is invalid.
10    InvalidBinaryFormatVersion,
11    /// The end of the binary file was reached unexpectedly.
12    Eof,
13
14    /// A UTF-8 string is malformed.
15    MalformedUtf8(str::Utf8Error),
16    /// The type of a section is malformed.
17    MalformedSectionTypeDiscriminator(u8),
18    /// The discriminator of a number type is malformed.
19    MalformedNumTypeDiscriminator(u8),
20    /// The discriminator of a vector type is malformed.
21    MalformedVecTypeDiscriminator(u8),
22    /// The discriminator of a function type is malformed.
23    MalformedFuncTypeDiscriminator(u8),
24    /// The discriminator of a reference type is malformed.
25    MalformedRefTypeDiscriminator(u8),
26    /// A valtype is malformed because it is neither a number, reference nor vector type.
27    MalformedValType,
28    /// The discriminator of an export description is malformed.
29    MalformedExportDescDiscriminator(u8),
30    /// The discriminator of an import description is malformed.
31    MalformedImportDescDiscriminator(u8),
32    /// The discriminator of a limits type is malformed.
33    MalformedLimitsDiscriminator(u8),
34    /// The discriminator of a mut type is malformed.
35    MalformedMutDiscriminator(u8),
36    /// Block types use a special 33-bit signed integer for encoding type indices.
37    MalformedBlockTypeTypeIdx(i64),
38    /// A variable-length integer was read but it overflowed.
39    MalformedVariableLengthInteger,
40    /// The discriminator of an element kind is malformed.
41    MalformedElemKindDiscriminator(u8),
42    /// 33-bit signed integers are sometimes used to encode unsigned 32-bit
43    /// integers to prevent collisions between bit patterns of different types.
44    /// Therefore, 33-bit signed integers may never be negative.
45    I33IsNegative,
46    /// A function specifies too many locals, i.e. more than 2^32 - 1
47    TooManyLocals(u64),
48    /// A section's contents were successfully decoded, but either too many or not enough bytes of
49    /// the section's bytecode were consumed.
50    SectionSizeMismatch,
51    /// A section with given type is out of order. All section types have a fixed order in which they must occur.
52    SectionOutOfOrder(SectionTy),
53}
54
55impl error::Error for DecodingError {}
56
57impl fmt::Display for DecodingError {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        match self {
60            DecodingError::InvalidMagic => write!(f, "The magic number is invalid"),
61            DecodingError::InvalidBinaryFormatVersion => write!(f, "The Wasm binary format version is invalid"),
62            DecodingError::Eof => write!(f, "The end of the Wasm bytecode was reached unexpectedly"),
63            DecodingError::MalformedUtf8(utf8_error) => write!(f, "Failed to parse a UTF-8 string: {utf8_error}"),
64            DecodingError::MalformedSectionTypeDiscriminator(byte) => write!(f, "Failed to parse {byte:#x} as a section type discriminator"),
65            DecodingError::MalformedNumTypeDiscriminator(byte) => write!(f, "Failed to parse {byte:#x} as a number type discriminator"),
66            DecodingError::MalformedVecTypeDiscriminator(byte) => write!(f, "Failed to parse {byte:#x} as a vector type discriminator"),
67            DecodingError::MalformedFuncTypeDiscriminator(byte) => write!(f, "Failed to parse {byte:#x} as a function type discriminator"),
68            DecodingError::MalformedRefTypeDiscriminator(byte) => write!(f, "Failed to parse {byte:#x} as a reference type discriminator"),
69            DecodingError::MalformedValType => write!(f, "Failed to read a value type because it is neither a number, reference or vector type"),
70            DecodingError::MalformedExportDescDiscriminator(byte) => write!(f, "Failed to parse {byte:#x} as an export description discriminator"),
71            DecodingError::MalformedImportDescDiscriminator(byte) => write!(f, "Failed to parse {byte:#x} as an import description discriminator"),
72            DecodingError::MalformedLimitsDiscriminator(byte) => write!(f, "Failed to parse {byte:#x} as a limits type discriminator"),
73            DecodingError::MalformedMutDiscriminator(byte) => write!(f, "Failed to parse {byte:#x} as a mute type discriminator"),
74            DecodingError::MalformedBlockTypeTypeIdx(idx) => write!(f, "The type index {idx} which is encoded as a singed 33-bit integer inside a block type is malformed"),
75            DecodingError::MalformedVariableLengthInteger => write!(f, "Reading a variable-length integer overflowed"),
76            DecodingError::MalformedElemKindDiscriminator(byte) => write!(f, "Failed to parse {byte:#x} as an element kind discriminator"),
77            DecodingError::I33IsNegative => f.write_str("An i33 type is negative which is not allowed"),
78            DecodingError::TooManyLocals(n) => write!(f,"There are {n} locals and this exceeds the maximum allowed number of 2^32-1"),
79            DecodingError::SectionSizeMismatch => f.write_str("A section's contents were successfully decoded, but either too many or not enough bytes of the section's bytecode were consumed."),
80            DecodingError::SectionOutOfOrder(ty) => write!(f, "A section of type `{ty:?}` is defined out of order"),
81        }
82    }
83}