1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use core::fmt::{self, Display, Formatter};
use core::str::FromStr;
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, Serialize, Deserialize, Error)]
#[repr(u32)]
pub enum TrapCode {
StackOverflow = 0,
HeapSetterOutOfBounds = 1,
HeapAccessOutOfBounds = 2,
TableSetterOutOfBounds = 3,
TableAccessOutOfBounds = 4,
OutOfBounds = 5,
IndirectCallToNull = 6,
BadSignature = 7,
IntegerOverflow = 8,
IntegerDivisionByZero = 9,
BadConversionToInteger = 10,
UnreachableCodeReached = 11,
Interrupt = 12,
UnalignedAtomic = 13,
VMOutOfMemory = 14,
}
impl TrapCode {
pub fn message(&self) -> &str {
match self {
Self::StackOverflow => "call stack exhausted",
Self::HeapSetterOutOfBounds => "memory out of bounds: data segment does not fit",
Self::HeapAccessOutOfBounds => "out of bounds memory access",
Self::TableSetterOutOfBounds => "table out of bounds: elements segment does not fit",
Self::TableAccessOutOfBounds => "undefined element: out of bounds table access",
Self::OutOfBounds => "out of bounds",
Self::IndirectCallToNull => "uninitialized element",
Self::BadSignature => "indirect call type mismatch",
Self::IntegerOverflow => "integer overflow",
Self::IntegerDivisionByZero => "integer divide by zero",
Self::BadConversionToInteger => "invalid conversion to integer",
Self::UnreachableCodeReached => "unreachable",
Self::Interrupt => "interrupt",
Self::UnalignedAtomic => "unaligned atomic access",
Self::VMOutOfMemory => "out of memory",
}
}
}
impl Display for TrapCode {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let identifier = match *self {
Self::StackOverflow => "stk_ovf",
Self::HeapSetterOutOfBounds => "heap_set_oob",
Self::HeapAccessOutOfBounds => "heap_get_oob",
Self::TableSetterOutOfBounds => "table_set_oob",
Self::TableAccessOutOfBounds => "table_get_oob",
Self::OutOfBounds => "oob",
Self::IndirectCallToNull => "icall_null",
Self::BadSignature => "bad_sig",
Self::IntegerOverflow => "int_ovf",
Self::IntegerDivisionByZero => "int_divz",
Self::BadConversionToInteger => "bad_toint",
Self::UnreachableCodeReached => "unreachable",
Self::Interrupt => "interrupt",
Self::UnalignedAtomic => "unalign_atom",
Self::VMOutOfMemory => "oom",
};
f.write_str(identifier)
}
}
impl FromStr for TrapCode {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
use self::TrapCode::*;
match s {
"stk_ovf" => Ok(StackOverflow),
"heap_set_oob" => Ok(HeapSetterOutOfBounds),
"heap_get_oob" => Ok(HeapAccessOutOfBounds),
"table_set_oob" => Ok(TableSetterOutOfBounds),
"table_get_oob" => Ok(TableAccessOutOfBounds),
"oob" => Ok(OutOfBounds),
"icall_null" => Ok(IndirectCallToNull),
"bad_sig" => Ok(BadSignature),
"int_ovf" => Ok(IntegerOverflow),
"int_divz" => Ok(IntegerDivisionByZero),
"bad_toint" => Ok(BadConversionToInteger),
"unreachable" => Ok(UnreachableCodeReached),
"interrupt" => Ok(Interrupt),
"unalign_atom" => Ok(UnalignedAtomic),
"oom" => Ok(VMOutOfMemory),
_ => Err(()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const CODES: [TrapCode; 14] = [
TrapCode::StackOverflow,
TrapCode::HeapSetterOutOfBounds,
TrapCode::HeapAccessOutOfBounds,
TrapCode::TableSetterOutOfBounds,
TrapCode::TableAccessOutOfBounds,
TrapCode::OutOfBounds,
TrapCode::IndirectCallToNull,
TrapCode::BadSignature,
TrapCode::IntegerOverflow,
TrapCode::IntegerDivisionByZero,
TrapCode::BadConversionToInteger,
TrapCode::UnreachableCodeReached,
TrapCode::Interrupt,
TrapCode::UnalignedAtomic,
];
#[test]
fn display() {
for r in &CODES {
let tc = *r;
assert_eq!(tc.to_string().parse(), Ok(tc));
}
assert_eq!("bogus".parse::<TrapCode>(), Err(()));
assert_eq!("user".parse::<TrapCode>(), Err(()));
assert_eq!("user-1".parse::<TrapCode>(), Err(()));
assert_eq!("users".parse::<TrapCode>(), Err(()));
}
}