@@ -10,7 +10,7 @@ use crate::{
10
10
OPCODE_INFO_JUMPTABLE , STACK_LIMIT ,
11
11
} ;
12
12
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 } ;
14
14
15
15
const EOF_NON_RETURNING_FUNCTION : u8 = 0x80 ;
16
16
@@ -163,6 +163,18 @@ impl From<EofValidationError> for EofError {
163
163
}
164
164
}
165
165
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
+
166
178
#[ derive( Debug , Hash , PartialEq , Eq , PartialOrd , Ord , Clone , Copy ) ]
167
179
pub enum EofValidationError {
168
180
FalsePossitive ,
@@ -229,6 +241,68 @@ pub enum EofValidationError {
229
241
NoCodeSections ,
230
242
}
231
243
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
+
232
306
/// Validates that:
233
307
/// * All instructions are valid.
234
308
/// * It ends with a terminating instruction or RJUMP.
0 commit comments