Skip to content

Commit 7ea1310

Browse files
committed
Fixed control stack.
1 parent 192d017 commit 7ea1310

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

wasm/src/main.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
collections::{btree_map::Entry, BTreeMap},
3-
env::var,
3+
iter::Peekable,
44
ops::RangeFrom,
55
str::FromStr,
66
};
@@ -906,7 +906,7 @@ fn infinite_registers_allocation<'a>(
906906
);
907907

908908
// The rest of the directives are taken from the function body definition.
909-
let mut op_reader = body.get_operators_reader()?.into_iter();
909+
let mut op_reader = body.get_operators_reader()?.into_iter().peekable();
910910
while let Some(operator) = op_reader.next() {
911911
// There shouldn't be any more operators after the outmost
912912
// block (the function itself) has ended.
@@ -1174,6 +1174,7 @@ fn infinite_registers_allocation<'a>(
11741174
}
11751175
}
11761176
}
1177+
assert!(tracker.control_stack.is_empty());
11771178

11781179
Ok(directives)
11791180
}
@@ -1584,28 +1585,37 @@ impl<'a> StackTracker<'a> {
15841585
/// and fix the stack.
15851586
fn discard_unreachable_and_fix_the_stack(
15861587
&mut self,
1587-
op_reader: &mut OperatorsIterator<'_>,
1588+
op_reader: &mut Peekable<OperatorsIterator<'_>>,
15881589
) -> wasmparser::Result<()> {
15891590
// Discard unreachable code
15901591
let mut stack_count = 0;
1591-
for operator in op_reader {
1592-
match operator? {
1593-
Operator::Block { .. } | Operator::Loop { .. } | Operator::If { .. } => {
1592+
1593+
// We do a peek loop so we leave Else and End operators in the iterator,
1594+
// to be handled by the caller.
1595+
while let Some(operator) = op_reader.peek() {
1596+
match operator {
1597+
Ok(Operator::Block { .. })
1598+
| Ok(Operator::Loop { .. })
1599+
| Ok(Operator::If { .. }) => {
15941600
stack_count += 1;
15951601
}
1596-
Operator::Else => {
1602+
Ok(Operator::Else) => {
15971603
if stack_count == 0 {
15981604
break;
15991605
}
16001606
}
1601-
Operator::End => {
1607+
Ok(Operator::End) => {
16021608
if stack_count == 0 {
16031609
break;
16041610
}
16051611
stack_count -= 1;
16061612
}
1607-
_ => {}
1613+
Ok(_) => {}
1614+
Err(_) => {
1615+
return Err(op_reader.next().unwrap().unwrap_err());
1616+
}
16081617
}
1618+
op_reader.next();
16091619
}
16101620

16111621
// Fix the stack

0 commit comments

Comments
 (0)