Skip to content

Commit 5b9fff4

Browse files
committed
emit an unreachable if an unreachable block context does not end in an unreachable
1 parent 9149f4c commit 5b9fff4

5 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/wasm/wasm-binary.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,11 @@ void WasmBinaryWriter::recursePossibleBlockContents(Expression* curr) {
583583
for (auto* child : block->list) {
584584
recurse(child);
585585
}
586+
if (block->type == unreachable && block->list.back()->type != unreachable) {
587+
// similar to in visitBlock, here we could skip emitting the block itself,
588+
// but must still end the 'block' (the contents, really) with an unreachable
589+
o << int8_t(BinaryConsts::Unreachable);
590+
}
586591
}
587592

588593
void WasmBinaryWriter::visitIf(If *curr) {

test/unit.wast

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,4 +537,16 @@
537537
(if (i32.const 1) (nop) (unreachable))
538538
(if (i32.const 1) (unreachable) (unreachable))
539539
)
540+
(func $unreachable-if-arm
541+
(if
542+
(i32.const 1)
543+
(block
544+
(nop)
545+
)
546+
(block
547+
(unreachable)
548+
(i32.const 1) ;; ends in a concrete, after an unreachable
549+
)
550+
)
551+
)
540552
)

test/unit.wast.from-wast

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,4 +602,16 @@
602602
(unreachable)
603603
)
604604
)
605+
(func $unreachable-if-arm (type $FUNCSIG$v)
606+
(if
607+
(i32.const 1)
608+
(block $block
609+
(nop)
610+
)
611+
(block $block12
612+
(unreachable)
613+
(i32.const 1)
614+
)
615+
)
616+
)
605617
)

test/unit.wast.fromBinary

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,5 +646,20 @@
646646
)
647647
(unreachable)
648648
)
649+
(func $unreachable-if-arm (type $1)
650+
(if
651+
(i32.const 1)
652+
(block $label$0
653+
(nop)
654+
)
655+
(block $label$1
656+
(unreachable)
657+
(drop
658+
(i32.const 1)
659+
)
660+
(unreachable)
661+
)
662+
)
663+
)
649664
)
650665

test/unit.wast.fromBinary.noDebugInfo

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,5 +646,20 @@
646646
)
647647
(unreachable)
648648
)
649+
(func $36 (type $1)
650+
(if
651+
(i32.const 1)
652+
(block $label$0
653+
(nop)
654+
)
655+
(block $label$1
656+
(unreachable)
657+
(drop
658+
(i32.const 1)
659+
)
660+
(unreachable)
661+
)
662+
)
663+
)
649664
)
650665

0 commit comments

Comments
 (0)