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
use std::collections::{BTreeMap, HashMap};
use std::fmt;
use std::sync::{Arc, RwLock};
use wasmtime_environ::ir;
#[derive(Default)]
pub struct TrapRegistry {
ranges: Arc<RwLock<BTreeMap<usize, TrapGroup>>>,
}
#[derive(Debug)]
struct TrapGroup {
start: usize,
traps: HashMap<usize, TrapDescription>,
}
#[derive(Clone)]
pub struct TrapRegistration {
ranges: Arc<RwLock<BTreeMap<usize, TrapGroup>>>,
end: Option<usize>,
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct TrapDescription {
pub source_loc: ir::SourceLoc,
pub trap_code: ir::TrapCode,
}
impl fmt::Display for TrapDescription {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"wasm trap: {}, source location: {}",
trap_code_to_expected_string(self.trap_code),
self.source_loc
)
}
}
fn trap_code_to_expected_string(trap_code: ir::TrapCode) -> String {
use ir::TrapCode::*;
match trap_code {
StackOverflow => "call stack exhausted".to_string(),
HeapOutOfBounds => "out of bounds memory access".to_string(),
TableOutOfBounds => "undefined element".to_string(),
OutOfBounds => "out of bounds".to_string(),
IndirectCallToNull => "uninitialized element".to_string(),
BadSignature => "indirect call type mismatch".to_string(),
IntegerOverflow => "integer overflow".to_string(),
IntegerDivisionByZero => "integer divide by zero".to_string(),
BadConversionToInteger => "invalid conversion to integer".to_string(),
UnreachableCodeReached => "unreachable".to_string(),
Interrupt => "interrupt".to_string(),
User(x) => format!("user trap {}", x),
}
}
impl TrapRegistry {
pub fn register_traps(
&self,
list: impl IntoIterator<Item = (usize, ir::SourceLoc, ir::TrapCode)>,
) -> TrapRegistration {
let mut start = usize::max_value();
let mut end = 0;
let mut traps = HashMap::new();
for (addr, source_loc, trap_code) in list.into_iter() {
traps.insert(
addr,
TrapDescription {
source_loc,
trap_code,
},
);
if addr < start {
start = addr;
}
if addr > end {
end = addr;
}
}
if traps.len() == 0 {
return TrapRegistration {
ranges: self.ranges.clone(),
end: None,
};
}
let mut ranges = self.ranges.write().unwrap();
if let Some((_, prev)) = ranges.range(end..).next() {
assert!(prev.start > end);
}
if let Some((prev_end, _)) = ranges.range(..=start).next_back() {
assert!(*prev_end < start);
}
assert!(ranges.insert(end, TrapGroup { start, traps }).is_none());
TrapRegistration {
ranges: self.ranges.clone(),
end: Some(end),
}
}
}
impl TrapRegistration {
pub fn get_trap(&self, address: usize) -> Option<TrapDescription> {
let ranges = self.ranges.read().ok()?;
let (end, group) = ranges.range(address..).next()?;
if group.start <= address && address <= *end {
group.traps.get(&address).copied()
} else {
None
}
}
}
impl Drop for TrapRegistration {
fn drop(&mut self) {
if let Some(end) = self.end {
if let Ok(mut ranges) = self.ranges.write() {
ranges.remove(&end);
}
}
}
}