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_derive::{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 StackOverflow, 16 17 /// A `heap_addr` instruction detected an out-of-bounds error. 18 /// 19 /// Note that not all out-of-bounds heap accesses are reported this way; 20 /// some are detected by a segmentation fault on the heap unmapped or 21 /// offset-guard pages. 22 HeapOutOfBounds, 23 24 /// A wasm atomic operation was presented with a not-naturally-aligned linear-memory address. 25 HeapMisaligned, 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 /// A null reference was encountered which was required to be non-null. 56 NullReference, 57 58 /// A null `i31ref` was encountered which was required to be non-null. 59 NullI31Ref, 60 } 61 62 impl TrapCode { 63 /// Returns a slice of all traps except `TrapCode::User` traps 64 pub const fn non_user_traps() -> &'static [TrapCode] { 65 &[ 66 TrapCode::StackOverflow, 67 TrapCode::HeapOutOfBounds, 68 TrapCode::HeapMisaligned, 69 TrapCode::TableOutOfBounds, 70 TrapCode::IndirectCallToNull, 71 TrapCode::BadSignature, 72 TrapCode::IntegerOverflow, 73 TrapCode::IntegerDivisionByZero, 74 TrapCode::BadConversionToInteger, 75 TrapCode::UnreachableCodeReached, 76 TrapCode::Interrupt, 77 TrapCode::NullReference, 78 ] 79 } 80 } 81 82 impl Display for TrapCode { 83 fn fmt(&self, f: &mut Formatter) -> fmt::Result { 84 use self::TrapCode::*; 85 let identifier = match *self { 86 StackOverflow => "stk_ovf", 87 HeapOutOfBounds => "heap_oob", 88 HeapMisaligned => "heap_misaligned", 89 TableOutOfBounds => "table_oob", 90 IndirectCallToNull => "icall_null", 91 BadSignature => "bad_sig", 92 IntegerOverflow => "int_ovf", 93 IntegerDivisionByZero => "int_divz", 94 BadConversionToInteger => "bad_toint", 95 UnreachableCodeReached => "unreachable", 96 Interrupt => "interrupt", 97 User(x) => return write!(f, "user{}", x), 98 NullReference => "null_reference", 99 NullI31Ref => "null_i31ref", 100 }; 101 f.write_str(identifier) 102 } 103 } 104 105 impl FromStr for TrapCode { 106 type Err = (); 107 108 fn from_str(s: &str) -> Result<Self, Self::Err> { 109 use self::TrapCode::*; 110 match s { 111 "stk_ovf" => Ok(StackOverflow), 112 "heap_oob" => Ok(HeapOutOfBounds), 113 "heap_misaligned" => Ok(HeapMisaligned), 114 "table_oob" => Ok(TableOutOfBounds), 115 "icall_null" => Ok(IndirectCallToNull), 116 "bad_sig" => Ok(BadSignature), 117 "int_ovf" => Ok(IntegerOverflow), 118 "int_divz" => Ok(IntegerDivisionByZero), 119 "bad_toint" => Ok(BadConversionToInteger), 120 "unreachable" => Ok(UnreachableCodeReached), 121 "interrupt" => Ok(Interrupt), 122 "null_reference" => Ok(NullReference), 123 "null_i31ref" => Ok(NullI31Ref), 124 _ if s.starts_with("user") => s[4..].parse().map(User).map_err(|_| ()), 125 _ => Err(()), 126 } 127 } 128 } 129 130 #[cfg(test)] 131 mod tests { 132 use super::*; 133 use alloc::string::ToString; 134 135 #[test] 136 fn display() { 137 for r in TrapCode::non_user_traps() { 138 let tc = *r; 139 assert_eq!(tc.to_string().parse(), Ok(tc)); 140 } 141 assert_eq!("bogus".parse::<TrapCode>(), Err(())); 142 143 assert_eq!(TrapCode::User(17).to_string(), "user17"); 144 assert_eq!("user22".parse(), Ok(TrapCode::User(22))); 145 assert_eq!("user".parse::<TrapCode>(), Err(())); 146 assert_eq!("user-1".parse::<TrapCode>(), Err(())); 147 assert_eq!("users".parse::<TrapCode>(), Err(())); 148 } 149 } 150