From d8a434074356c01ab96b98f6ae62861b3f98fc6c Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Sat, 13 Nov 2021 16:44:53 -0800 Subject: [PATCH] Some fixes to wasm-to-IR step. --- fuzz/Cargo.toml | 1 + fuzz/fuzz_targets/parse_ir.rs | 1 + src/frontend.rs | 29 +++++++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 1fd6d3e..04f6384 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -12,6 +12,7 @@ cargo-fuzz = true [dependencies] libfuzzer-sys = "0.4" wasm-smith = "0.8" +env_logger = "0.9" [dependencies.waffle] path = ".." diff --git a/fuzz/fuzz_targets/parse_ir.rs b/fuzz/fuzz_targets/parse_ir.rs index 24e3021..780e018 100644 --- a/fuzz/fuzz_targets/parse_ir.rs +++ b/fuzz/fuzz_targets/parse_ir.rs @@ -5,5 +5,6 @@ use waffle::frontend::wasm_to_ir; use wasm_smith::Module; fuzz_target!(|module: Module| { + let _ = env_logger::try_init(); let _parsed_module = wasm_to_ir(&module.to_bytes()[..]).unwrap(); }); diff --git a/src/frontend.rs b/src/frontend.rs index 2df8b73..a4bb68f 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -108,6 +108,12 @@ fn parse_body<'a, 'b>( } } + trace!( + "Parsing function body: locals = {:?} sig = {:?}", + ret.locals, + module.signatures[my_sig] + ); + let mut builder = FunctionBodyBuilder::new(module, my_sig, &mut ret); let ops = body.get_operators_reader()?; for op in ops.into_iter() { @@ -115,6 +121,12 @@ fn parse_body<'a, 'b>( builder.handle_op(op)?; } + if builder.cur_block.is_some() { + builder.handle_op(Operator::Return)?; + } + + trace!("Final function body:{:?}", ret); + Ok(ret) } @@ -190,14 +202,26 @@ impl Frame { impl<'a, 'b> FunctionBodyBuilder<'a, 'b> { fn new(module: &'b Module<'a>, my_sig: SignatureId, body: &'b mut FunctionBody<'a>) -> Self { body.blocks.push(Block::default()); - Self { + let mut ret = Self { module, my_sig, body, ctrl_stack: vec![], op_stack: vec![], cur_block: Some(0), - } + }; + + // Push initial implicit Block. + let params = module.signatures[my_sig].params.to_vec(); + let results = module.signatures[my_sig].params.to_vec(); + let out = ret.create_block(); + ret.ctrl_stack.push(Frame::Block { + start_depth: 0, + out, + params, + results, + }); + ret } fn add_block_params(&mut self, block: BlockId, tys: &[Type]) { @@ -217,6 +241,7 @@ impl<'a, 'b> FunctionBodyBuilder<'a, 'b> { } fn handle_op(&mut self, op: Operator<'a>) -> Result<()> { + trace!("handle_op: {:?}", op); match &op { Operator::Unreachable | Operator::LocalGet { .. }