From 9149f4cdeee0949e5dab4b0e0e076c73b822cbfc Mon Sep 17 00:00:00 2001 From: "Alon Zakai (kripken)" Date: Sat, 5 Aug 2017 10:44:12 -0700 Subject: fix reading of stacky unreadable code with elements we need to drop --- src/wasm/wasm-binary.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src/wasm/wasm-binary.cpp') diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 47bcc8ba5..018e463e0 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -2073,6 +2073,20 @@ BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) { return BinaryConsts::ASTNodes(code); } +void WasmBinaryBuilder::pushBlockElements(Block* curr, size_t start, size_t end) { + for (size_t i = start; i < end; i++) { + auto* item = expressionStack[i]; + curr->list.push_back(item); + if (i < end - 1) { + // stacky&unreachable code may introduce elements that need to be dropped in non-final positions + if (isConcreteWasmType(item->type)) { + curr->list.back() = Builder(wasm).makeDrop(curr->list.back()); + } + } + } + expressionStack.resize(start); +} + void WasmBinaryBuilder::visitBlock(Block *curr) { if (debug) std::cerr << "zz node: Block" << std::endl; // special-case Block and de-recurse nested blocks in their first position, as that is @@ -2108,18 +2122,7 @@ void WasmBinaryBuilder::visitBlock(Block *curr) { if (end < start) { throw ParseException("block cannot pop from outside"); } - for (size_t i = start; i < end; i++) { - if (debug) std::cerr << " " << size_t(expressionStack[i]) << "\n zz Block element " << curr->list.size() << std::endl; - auto* item = expressionStack[i]; - curr->list.push_back(item); - if (i < end - 1) { - // stacky&unreachable code may introduce elements that need to be dropped in non-final positions - if (isConcreteWasmType(item->type)) { - curr->list.back() = Builder(wasm).makeDrop(curr->list.back()); - } - } - } - expressionStack.resize(start); + pushBlockElements(curr, start, end); curr->finalize(curr->type); breakStack.pop_back(); } @@ -2136,11 +2139,8 @@ Expression* WasmBinaryBuilder::getMaybeBlock(WasmType type) { throw ParseException("block cannot pop from outside"); } auto* block = allocator.alloc(); - for (size_t i = start; i < end; i++) { - block->list.push_back(expressionStack[i]); - } + pushBlockElements(block, start, end); block->finalize(type); - expressionStack.resize(start); return block; } -- cgit v1.2.3 From 5b9fff4399b959a4af28a6c82e8ab9243898f360 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 3 Aug 2017 14:49:41 -0700 Subject: emit an unreachable if an unreachable block context does not end in an unreachable --- src/wasm/wasm-binary.cpp | 5 +++++ test/unit.wast | 12 ++++++++++++ test/unit.wast.from-wast | 12 ++++++++++++ test/unit.wast.fromBinary | 15 +++++++++++++++ test/unit.wast.fromBinary.noDebugInfo | 15 +++++++++++++++ 5 files changed, 59 insertions(+) (limited to 'src/wasm/wasm-binary.cpp') diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 018e463e0..63dddbdd6 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -583,6 +583,11 @@ void WasmBinaryWriter::recursePossibleBlockContents(Expression* curr) { for (auto* child : block->list) { recurse(child); } + if (block->type == unreachable && block->list.back()->type != unreachable) { + // similar to in visitBlock, here we could skip emitting the block itself, + // but must still end the 'block' (the contents, really) with an unreachable + o << int8_t(BinaryConsts::Unreachable); + } } void WasmBinaryWriter::visitIf(If *curr) { diff --git a/test/unit.wast b/test/unit.wast index c1a945264..a429675de 100644 --- a/test/unit.wast +++ b/test/unit.wast @@ -537,4 +537,16 @@ (if (i32.const 1) (nop) (unreachable)) (if (i32.const 1) (unreachable) (unreachable)) ) + (func $unreachable-if-arm + (if + (i32.const 1) + (block + (nop) + ) + (block + (unreachable) + (i32.const 1) ;; ends in a concrete, after an unreachable + ) + ) + ) ) diff --git a/test/unit.wast.from-wast b/test/unit.wast.from-wast index bb66845af..7fb82d69d 100644 --- a/test/unit.wast.from-wast +++ b/test/unit.wast.from-wast @@ -602,4 +602,16 @@ (unreachable) ) ) + (func $unreachable-if-arm (type $FUNCSIG$v) + (if + (i32.const 1) + (block $block + (nop) + ) + (block $block12 + (unreachable) + (i32.const 1) + ) + ) + ) ) diff --git a/test/unit.wast.fromBinary b/test/unit.wast.fromBinary index 78ae640b1..8bb80bcca 100644 --- a/test/unit.wast.fromBinary +++ b/test/unit.wast.fromBinary @@ -646,5 +646,20 @@ ) (unreachable) ) + (func $unreachable-if-arm (type $1) + (if + (i32.const 1) + (block $label$0 + (nop) + ) + (block $label$1 + (unreachable) + (drop + (i32.const 1) + ) + (unreachable) + ) + ) + ) ) diff --git a/test/unit.wast.fromBinary.noDebugInfo b/test/unit.wast.fromBinary.noDebugInfo index 3c24886f5..09da9a15b 100644 --- a/test/unit.wast.fromBinary.noDebugInfo +++ b/test/unit.wast.fromBinary.noDebugInfo @@ -646,5 +646,20 @@ ) (unreachable) ) + (func $36 (type $1) + (if + (i32.const 1) + (block $label$0 + (nop) + ) + (block $label$1 + (unreachable) + (drop + (i32.const 1) + ) + (unreachable) + ) + ) + ) ) -- cgit v1.2.3