Skip to content

Commit 013de92

Browse files
authored
feat(EOF): implement std::error::Error trait for EofValidationError and EofError (#1649)
* feat: implement Error trait for EofValidationError * feat: implement Error trait for EofError * fix: remove unused import * fix: remove format macro
1 parent 8eaff99 commit 013de92

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

crates/interpreter/src/interpreter/analysis.rs

+75-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
OPCODE_INFO_JUMPTABLE, STACK_LIMIT,
1111
};
1212
use core::convert::identity;
13-
use std::{borrow::Cow, sync::Arc, vec, vec::Vec};
13+
use std::{borrow::Cow, fmt, sync::Arc, vec, vec::Vec};
1414

1515
const EOF_NON_RETURNING_FUNCTION: u8 = 0x80;
1616

@@ -163,6 +163,18 @@ impl From<EofValidationError> for EofError {
163163
}
164164
}
165165

166+
impl fmt::Display for EofError {
167+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168+
match self {
169+
EofError::Decode(e) => write!(f, "Bytecode decode error: {}", e),
170+
EofError::Validation(e) => write!(f, "Bytecode validation error: {}", e),
171+
}
172+
}
173+
}
174+
175+
#[cfg(feature = "std")]
176+
impl std::error::Error for EofError {}
177+
166178
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
167179
pub enum EofValidationError {
168180
FalsePossitive,
@@ -229,6 +241,68 @@ pub enum EofValidationError {
229241
NoCodeSections,
230242
}
231243

244+
impl fmt::Display for EofValidationError {
245+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
246+
write!(
247+
f,
248+
"{}",
249+
match self {
250+
EofValidationError::FalsePossitive => "False positive",
251+
EofValidationError::UnknownOpcode => "Opcode is not known",
252+
EofValidationError::OpcodeDisabled => "Opcode is disabled",
253+
EofValidationError::InstructionNotForwardAccessed => "Should have forward jump",
254+
EofValidationError::MissingImmediateBytes => "Bytecode is missing bytes",
255+
EofValidationError::MissingRJUMPVImmediateBytes => {
256+
"Bytecode is missing bytes after RJUMPV opcode"
257+
}
258+
EofValidationError::JumpToImmediateBytes => "Invalid jump",
259+
EofValidationError::BackwardJumpToImmediateBytes => "Invalid backward jump",
260+
EofValidationError::RJUMPVZeroMaxIndex => "Used RJUMPV with zero as MaxIndex",
261+
EofValidationError::JumpZeroOffset => "Used JUMP with zero as offset",
262+
EofValidationError::EOFCREATEInvalidIndex =>
263+
"EOFCREATE points to out of bound index",
264+
EofValidationError::CodeSectionOutOfBounds => "CALLF index is out of bounds",
265+
EofValidationError::CALLFNonReturningFunction => {
266+
"CALLF was used on non-returning function"
267+
}
268+
EofValidationError::StackOverflow => "CALLF stack overflow",
269+
EofValidationError::JUMPFEnoughOutputs => "JUMPF needs more outputs",
270+
EofValidationError::JUMPFStackHigherThanOutputs => {
271+
"JUMPF stack is too high for outputs"
272+
}
273+
EofValidationError::DataLoadOutOfBounds => "DATALOAD is out of bounds",
274+
EofValidationError::RETFBiggestStackNumMoreThenOutputs => {
275+
"RETF biggest stack num is more than outputs"
276+
}
277+
EofValidationError::StackUnderflow =>
278+
"Stack requirement is above smallest stack items",
279+
EofValidationError::TypesStackUnderflow => {
280+
"Smallest stack items is more than output type"
281+
}
282+
EofValidationError::JumpUnderflow => "Jump destination is too low",
283+
EofValidationError::JumpOverflow => "Jump destination is too high",
284+
EofValidationError::BackwardJumpBiggestNumMismatch => {
285+
"Backward jump has different biggest stack item"
286+
}
287+
EofValidationError::BackwardJumpSmallestNumMismatch => {
288+
"Backward jump has different smallest stack item"
289+
}
290+
EofValidationError::LastInstructionNotTerminating => {
291+
"Last instruction of bytecode is not terminating"
292+
}
293+
EofValidationError::CodeSectionNotAccessed => "Code section was not accessed",
294+
EofValidationError::InvalidTypesSection => "Invalid types section",
295+
EofValidationError::InvalidFirstTypesSection => "Invalid first types section",
296+
EofValidationError::MaxStackMismatch => "Max stack element mismatchs",
297+
EofValidationError::NoCodeSections => "No code sections",
298+
}
299+
)
300+
}
301+
}
302+
303+
#[cfg(feature = "std")]
304+
impl std::error::Error for EofValidationError {}
305+
232306
/// Validates that:
233307
/// * All instructions are valid.
234308
/// * It ends with a terminating instruction or RJUMP.

0 commit comments

Comments
 (0)