-
Notifications
You must be signed in to change notification settings - Fork 58
/
context.rs
346 lines (301 loc) Β· 9.71 KB
/
context.rs
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//! Context needed when validating instructions and constructing our `Instr` IR.
use crate::error::{ErrorKind, Result};
use crate::ir::{BlockKind, Instr, InstrSeq, InstrSeqId, InstrSeqType};
use crate::module::functions::{FunctionId, LocalFunction};
use crate::module::Module;
use crate::parse::IndicesToIds;
use crate::ty::ValType;
use crate::{ModuleTypes, TypeId};
use anyhow::Context;
#[derive(Debug)]
pub(crate) struct ControlFrame {
/// The parameter types of the block (checked before entering the block).
pub start_types: Box<[ValType]>,
/// The result type of the block (used to check its result).
pub end_types: Box<[ValType]>,
/// The height of the operand stack at the start of the block (used to check
/// that operands do not underflow the current block).
pub height: usize,
/// If `true`, then this frame is unreachable. This is used to handle
/// stack-polymorphic typing after unconditional branches.
pub unreachable: bool,
/// The id of this control frame's block.
pub block: InstrSeqId,
/// This control frame's kind of block, eg loop vs block vs if/else.
pub kind: BlockKind,
}
impl ControlFrame {
/// Get the expected types on the stack for branches to this block.
pub fn label_types(&self) -> &[ValType] {
if let BlockKind::Loop = self.kind {
&self.start_types
} else {
&self.end_types
}
}
}
/// The operand stack.
///
/// `None` is used for `Unknown` stack-polymophic values.
///
/// We also keep track of the instruction that created the value at each stack
/// slot.
pub(crate) type OperandStack = Vec<Option<ValType>>;
/// The control frame stack.
pub(crate) type ControlStack = Vec<ControlFrame>;
#[derive(Debug)]
pub(crate) struct ValidationContext<'a> {
/// The module that we're adding a function for.
pub module: &'a Module,
/// Mapping of indexes back to ids.
pub indices: &'a IndicesToIds,
/// The arena id of `func`.
pub func_id: FunctionId,
/// The function being validated/constructed.
pub func: &'a mut LocalFunction,
/// The operands stack.
pub operands: &'a mut OperandStack,
/// The control frames stack.
pub controls: &'a mut ControlStack,
/// If we're currently parsing an if/else instruction, where we're at
pub if_else: Vec<IfElseState>,
}
#[derive(Debug)]
pub struct IfElseState {
pub consequent: InstrSeqId,
pub alternative: Option<InstrSeqId>,
}
impl<'a> ValidationContext<'a> {
/// Create a new function context.
pub fn new(
module: &'a Module,
indices: &'a IndicesToIds,
func_id: FunctionId,
func: &'a mut LocalFunction,
operands: &'a mut OperandStack,
controls: &'a mut ControlStack,
) -> ValidationContext<'a> {
ValidationContext {
module,
indices,
func_id,
func,
operands,
controls,
if_else: Vec::new(),
}
}
pub fn push_operand(&mut self, op: Option<ValType>) {
impl_push_operand(&mut self.operands, op);
}
pub fn pop_operand(&mut self) -> Result<Option<ValType>> {
impl_pop_operand(&mut self.operands, &mut self.controls)
}
pub fn pop_operand_expected(&mut self, expected: Option<ValType>) -> Result<Option<ValType>> {
impl_pop_operand_expected(&mut self.operands, &mut self.controls, expected)
}
pub fn push_operands(&mut self, types: &[ValType]) {
impl_push_operands(&mut self.operands, types);
}
pub fn pop_operands(&mut self, expected: &[ValType]) -> Result<()> {
impl_pop_operands(&mut self.operands, &self.controls, expected)
}
pub fn push_control(
&mut self,
kind: BlockKind,
start_types: Box<[ValType]>,
end_types: Box<[ValType]>,
) -> Result<InstrSeqId> {
impl_push_control(
&self.module.types,
kind,
self.func,
self.controls,
self.operands,
start_types,
end_types,
)
}
pub fn push_control_with_ty(&mut self, kind: BlockKind, ty: TypeId) -> InstrSeqId {
let (start_types, end_types) = self.module.types.params_results(ty);
let start_types: Box<[_]> = start_types.into();
let end_types: Box<[_]> = end_types.into();
impl_push_control_with_ty(
&self.module.types,
kind,
self.func,
self.controls,
self.operands,
ty.into(),
start_types,
end_types,
)
}
pub fn pop_control(&mut self) -> Result<(ControlFrame, InstrSeqId)> {
let frame = impl_pop_control(&mut self.controls, &mut self.operands)?;
let block = frame.block;
Ok((frame, block))
}
pub fn unreachable(&mut self) {
let frame = self.controls.last_mut().unwrap();
frame.unreachable = true;
let height = frame.height;
self.operands.truncate(height);
}
pub fn control(&self, n: usize) -> Result<&ControlFrame> {
if n >= self.controls.len() {
anyhow::bail!("jump to nonexistent control block");
}
let idx = self.controls.len() - n - 1;
Ok(&self.controls[idx])
}
pub fn alloc_instr_in_block(&mut self, block: InstrSeqId, instr: impl Into<Instr>) {
self.func.block_mut(block).instrs.push(instr.into());
}
pub fn alloc_instr_in_control(
&mut self,
control: usize,
instr: impl Into<Instr>,
) -> Result<()> {
let frame = self.control(control)?;
if frame.unreachable {
return Ok(());
}
let block = frame.block;
self.alloc_instr_in_block(block, instr);
Ok(())
}
pub fn alloc_instr(&mut self, instr: impl Into<Instr>) {
self.alloc_instr_in_control(0, instr).unwrap();
}
}
fn impl_push_operand(operands: &mut OperandStack, op: Option<ValType>) {
log::trace!("push operand: {:?}", op);
operands.push(op);
}
fn impl_pop_operand(
operands: &mut OperandStack,
controls: &ControlStack,
) -> Result<Option<ValType>> {
if let Some(f) = controls.last() {
if operands.len() == f.height {
if f.unreachable {
log::trace!("pop operand: None");
return Ok(None);
}
return Err(ErrorKind::InvalidWasm)
.context("popped operand past control frame height in non-unreachable code");
}
}
let op = operands.pop().unwrap();
log::trace!("pop operand: {:?}", op);
Ok(op)
}
fn impl_pop_operand_expected(
operands: &mut OperandStack,
controls: &ControlStack,
expected: Option<ValType>,
) -> Result<Option<ValType>> {
match (impl_pop_operand(operands, controls)?, expected) {
(None, expected) => Ok(expected),
(actual, None) => Ok(actual),
(Some(actual), Some(expected)) => {
if actual != expected {
Err(ErrorKind::InvalidWasm)
.context(format!("expected type {}", expected))
.context(format!("found type {}", actual))
} else {
Ok(Some(actual))
}
}
}
}
fn impl_push_operands(operands: &mut OperandStack, types: &[ValType]) {
for ty in types {
impl_push_operand(operands, Some(*ty));
}
}
fn impl_pop_operands(
operands: &mut OperandStack,
controls: &ControlStack,
expected: &[ValType],
) -> Result<()> {
for ty in expected.iter().cloned().rev() {
impl_pop_operand_expected(operands, controls, Some(ty))?;
}
Ok(())
}
fn impl_push_control(
types: &ModuleTypes,
kind: BlockKind,
func: &mut LocalFunction,
controls: &mut ControlStack,
operands: &mut OperandStack,
start_types: Box<[ValType]>,
end_types: Box<[ValType]>,
) -> Result<InstrSeqId> {
let ty = InstrSeqType::existing(types, &start_types, &end_types).ok_or_else(|| {
anyhow::anyhow!(
"attempted to push a control frame for an instruction \
sequence with a type that does not exist"
)
.context(format!("type: {:?} -> {:?}", &start_types, &end_types))
})?;
Ok(impl_push_control_with_ty(
types,
kind,
func,
controls,
operands,
ty,
start_types,
end_types,
))
}
fn impl_push_control_with_ty(
types: &ModuleTypes,
kind: BlockKind,
func: &mut LocalFunction,
controls: &mut ControlStack,
operands: &mut OperandStack,
ty: InstrSeqType,
start_types: Box<[ValType]>,
end_types: Box<[ValType]>,
) -> InstrSeqId {
if let InstrSeqType::MultiValue(ty) = ty {
debug_assert_eq!(types.params(ty), &start_types[..]);
debug_assert_eq!(types.results(ty), &end_types[..]);
}
let height = operands.len();
impl_push_operands(operands, &start_types);
let block = func.add_block(|id| InstrSeq::new(id, ty));
controls.push(ControlFrame {
start_types,
end_types,
height,
unreachable: false,
block,
kind,
});
block
}
fn impl_pop_control(
controls: &mut ControlStack,
operands: &mut OperandStack,
) -> Result<ControlFrame> {
let frame = controls
.last()
.ok_or_else(|| ErrorKind::InvalidWasm)
.context("attempted to pop a frame from an empty control stack")?;
impl_pop_operands(operands, controls, &frame.end_types)?;
if operands.len() != frame.height {
return Err(ErrorKind::InvalidWasm).context(format!(
"incorrect number of operands on the stack at the end of a control frame; \
found {}, expected {}",
operands.len(),
frame.height
));
}
let frame = controls.pop().unwrap();
Ok(frame)
}