Skip to content

Commit

Permalink
Make FunctionContext::control fallible
Browse files Browse the repository at this point in the history
This helps centralize some error reporting and otherwise makes the code
a bit more streamlined!
  • Loading branch information
alexcrichton committed Jan 25, 2019
1 parent 034ddc7 commit 560ff0f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
9 changes: 6 additions & 3 deletions src/module/functions/local_function/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,12 @@ impl<'a> FunctionContext<'a> {
impl_unreachable(&mut self.operands, &mut self.controls, expr);
}

pub fn control(&self, n: usize) -> &ControlFrame {
pub fn control(&self, n: usize) -> Result<&ControlFrame> {
if n >= self.controls.len() {
failure::bail!("jump to nonexistent control block");
}
let idx = self.controls.len() - n - 1;
&self.controls[idx]
Ok(&self.controls[idx])
}

pub fn add_to_block<E>(&mut self, block: BlockId, expr: E)
Expand All @@ -182,7 +185,7 @@ impl<'a> FunctionContext<'a> {
where
E: Into<ExprId>,
{
let ctrl = self.control(control_frame);
let ctrl = self.control(control_frame).unwrap();
if ctrl.unreachable.is_some() {
return;
}
Expand Down
28 changes: 5 additions & 23 deletions src/module/functions/local_function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,16 +783,10 @@ fn validate_instruction(
.context("`br` to out-of-bounds block")?;

let n = relative_depth as usize;
if ctx.controls.len() <= n {
return Err(ErrorKind::InvalidWasm
.context("attempt to branch to out-of-bounds block")
.into());
}

let expected = ctx.control(n).label_types.clone();
let expected = ctx.control(n)?.label_types.clone();
let args = ctx.pop_operands(&expected)?.into_boxed_slice();

let to_block = ctx.control(n).block;
let to_block = ctx.control(n)?.block;
let expr = ctx.func.alloc(Br {
block: to_block,
args,
Expand All @@ -805,18 +799,12 @@ fn validate_instruction(
.context("`br_if` to out-of-bounds block")?;

let n = relative_depth as usize;
if ctx.controls.len() <= n {
return Err(ErrorKind::InvalidWasm
.context("attempt to branch to out-of-bounds block")
.into());
}

let (_, condition) = ctx.pop_operand_expected(Some(ValType::I32))?;

let expected = ctx.control(n).label_types.clone();
let expected = ctx.control(n)?.label_types.clone();
let args = ctx.pop_operands(&expected)?.into_boxed_slice();

let to_block = ctx.control(n).block;
let to_block = ctx.control(n)?.block;
let expr = ctx.func.alloc(BrIf {
condition,
block: to_block,
Expand All @@ -837,13 +825,7 @@ fn validate_instruction(
ctx.validation
.label(n)
.context("`br_table` with out-of-bounds block")?;
let n = n as usize;
if ctx.controls.len() < n {
return Err(ErrorKind::InvalidWasm
.context("attempt to jump to an out-of-bounds block from a table entry")
.into());
}
let control = ctx.control(n);
let control = ctx.control(n as usize)?;
match label_types {
None => label_types = Some(&control.label_types),
Some(n) => {
Expand Down

0 comments on commit 560ff0f

Please sign in to comment.