1 //! Trap codes describing the reason for a trap. 2 3 use core::fmt::{self, Display, Formatter}; 4 use core::str::FromStr; 5 #[cfg(feature = "enable-serde")] 6 use serde::{Deserialize, Serialize}; 7 8 /// A trap code describing the reason for a trap. 9 /// 10 /// All trap instructions have an explicit trap code. 11 #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] 12 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] 13 pub enum TrapCode { 14 /// The current stack space was exhausted. 15 /// 16 /// On some platforms, a stack overflow may also be indicated by a segmentation fault from the 17 /// stack guard page. 18 StackOverflow, 19 20 /// A `heap_addr` instruction detected an out-of-bounds error. 21 /// 22 /// Note that not all out-of-bounds heap accesses are reported this way; 23 /// some are detected by a segmentation fault on the heap unmapped or 24 /// offset-guard pages. 25 HeapOutOfBounds, 26 27 /// A `table_addr` instruction detected an out-of-bounds error. 28 TableOutOfBounds, 29 30 /// Indirect call to a null table entry. 31 IndirectCallToNull, 32 33 /// Signature mismatch on indirect call. 34 BadSignature, 35 36 /// An integer arithmetic operation caused an overflow. 37 IntegerOverflow, 38 39 /// An integer division by zero. 40 IntegerDivisionByZero, 41 42 /// Failed float-to-int conversion. 43 BadConversionToInteger, 44 45 /// Code that was supposed to have been unreachable was reached. 46 UnreachableCodeReached, 47 48 /// Execution has potentially run too long and may be interrupted. 49 /// This trap is resumable. 50 Interrupt, 51 52 /// A user-defined trap code. 53 User(u16), 54 } 55 56 impl Display for TrapCode { fmt(&self, f: &mut Formatter) -> fmt::Result57 fn fmt(&self, f: &mut Formatter) -> fmt::Result { 58 use self::TrapCode::*; 59 let identifier = match *self { 60 StackOverflow => "stk_ovf", 61 HeapOutOfBounds => "heap_oob", 62 TableOutOfBounds => "table_oob", 63 IndirectCallToNull => "icall_null", 64 BadSignature => "bad_sig", 65 IntegerOverflow => "int_ovf", 66 IntegerDivisionByZero => "int_divz", 67 BadConversionToInteger => "bad_toint", 68 UnreachableCodeReached => "unreachable", 69 Interrupt => "interrupt", 70 User(x) => return write!(f, "user{}", x), 71 }; 72 f.write_str(identifier) 73 } 74 } 75 76 impl FromStr for TrapCode { 77 type Err = (); 78 from_str(s: &str) -> Result<Self, Self::Err>79 fn from_str(s: &str) -> Result<Self, Self::Err> { 80 use self::TrapCode::*; 81 match s { 82 "stk_ovf" => Ok(StackOverflow), 83 "heap_oob" => Ok(HeapOutOfBounds), 84 "table_oob" => Ok(TableOutOfBounds), 85 "icall_null" => Ok(IndirectCallToNull), 86 "bad_sig" => Ok(BadSignature), 87 "int_ovf" => Ok(IntegerOverflow), 88 "int_divz" => Ok(IntegerDivisionByZero), 89 "bad_toint" => Ok(BadConversionToInteger), 90 "unreachable" => Ok(UnreachableCodeReached), 91 "interrupt" => Ok(Interrupt), 92 _ if s.starts_with("user") => s[4..].parse().map(User).map_err(|_| ()), 93 _ => Err(()), 94 } 95 } 96 } 97 98 #[cfg(test)] 99 mod tests { 100 use super::*; 101 use alloc::string::ToString; 102 103 // Everything but user-defined codes. 104 const CODES: [TrapCode; 10] = [ 105 TrapCode::StackOverflow, 106 TrapCode::HeapOutOfBounds, 107 TrapCode::TableOutOfBounds, 108 TrapCode::IndirectCallToNull, 109 TrapCode::BadSignature, 110 TrapCode::IntegerOverflow, 111 TrapCode::IntegerDivisionByZero, 112 TrapCode::BadConversionToInteger, 113 TrapCode::UnreachableCodeReached, 114 TrapCode::Interrupt, 115 ]; 116 117 #[test] display()118 fn display() { 119 for r in &CODES { 120 let tc = *r; 121 assert_eq!(tc.to_string().parse(), Ok(tc)); 122 } 123 assert_eq!("bogus".parse::<TrapCode>(), Err(())); 124 125 assert_eq!(TrapCode::User(17).to_string(), "user17"); 126 assert_eq!("user22".parse(), Ok(TrapCode::User(22))); 127 assert_eq!("user".parse::<TrapCode>(), Err(())); 128 assert_eq!("user-1".parse::<TrapCode>(), Err(())); 129 assert_eq!("users".parse::<TrapCode>(), Err(())); 130 } 131 } 132