Skip to content

Commit 7a06499

Browse files
committed
fix binary emitting of untaken branches, and also handle reading of unreachable stacky code which may introduce concrete elements in non-final block positoins
1 parent db51c2e commit 7a06499

5 files changed

Lines changed: 92 additions & 3 deletions

File tree

src/wasm/wasm-binary.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ void WasmBinaryWriter::recurse(Expression*& curr) {
545545
}
546546

547547
static bool brokenTo(Block* block) {
548-
return block->name.is() && BranchUtils::BranchSeeker::has(block, block->name);
548+
return block->name.is() && BranchUtils::BranchSeeker::hasNamed(block, block->name);
549549
}
550550

551551
void WasmBinaryWriter::visitBlock(Block *curr) {
@@ -636,7 +636,7 @@ int32_t WasmBinaryWriter::getBreakIndex(Name name) { // -1 if not found
636636
return breakStack.size() - 1 - i;
637637
}
638638
}
639-
std::cerr << "bad break: " << name << std::endl;
639+
std::cerr << "bad break: " << name << " in " << currFunction->name << std::endl;
640640
abort();
641641
}
642642

@@ -2070,7 +2070,14 @@ void WasmBinaryBuilder::visitBlock(Block *curr) {
20702070
}
20712071
for (size_t i = start; i < end; i++) {
20722072
if (debug) std::cerr << " " << size_t(expressionStack[i]) << "\n zz Block element " << curr->list.size() << std::endl;
2073-
curr->list.push_back(expressionStack[i]);
2073+
auto* item = expressionStack[i];
2074+
curr->list.push_back(item);
2075+
if (i < end - 1) {
2076+
// stacky&unreachable code may introduce elements that need to be dropped in non-final positoins
2077+
if (isConcreteWasmType(item->type)) {
2078+
curr->list.back() = Builder(wasm).makeDrop(curr->list.back());
2079+
}
2080+
}
20742081
}
20752082
expressionStack.resize(start);
20762083
curr->finalize(curr->type);

test/untaken-br_if.wast

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(module
2+
(func $binaryify-untaken-br_if (result f32)
3+
(if
4+
(i32.const 1)
5+
(unreachable)
6+
(block $label$1
7+
(br_if $label$1
8+
(i32.const 1)
9+
(unreachable)
10+
)
11+
)
12+
)
13+
)
14+
)

test/untaken-br_if.wast.from-wast

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(module
2+
(type $0 (func (result f32)))
3+
(memory $0 0)
4+
(func $binaryify-untaken-br_if (type $0) (result f32)
5+
(if
6+
(i32.const 1)
7+
(unreachable)
8+
(block $label$1
9+
(br_if $label$1
10+
(i32.const 1)
11+
(unreachable)
12+
)
13+
)
14+
)
15+
)
16+
)

test/untaken-br_if.wast.fromBinary

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
(module
2+
(type $0 (func (result f32)))
3+
(memory $0 0)
4+
(func $binaryify-untaken-br_if (type $0) (result f32)
5+
(if
6+
(i32.const 1)
7+
(block $label$0
8+
(unreachable)
9+
)
10+
(block $label$1
11+
(block $label$2
12+
(drop
13+
(i32.const 1)
14+
)
15+
(br_if $label$2
16+
(unreachable)
17+
)
18+
(unreachable)
19+
)
20+
(unreachable)
21+
)
22+
)
23+
(unreachable)
24+
)
25+
)
26+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
(module
2+
(type $0 (func (result f32)))
3+
(memory $0 0)
4+
(func $0 (type $0) (result f32)
5+
(if
6+
(i32.const 1)
7+
(block $label$0
8+
(unreachable)
9+
)
10+
(block $label$1
11+
(block $label$2
12+
(drop
13+
(i32.const 1)
14+
)
15+
(br_if $label$2
16+
(unreachable)
17+
)
18+
(unreachable)
19+
)
20+
(unreachable)
21+
)
22+
)
23+
(unreachable)
24+
)
25+
)
26+

0 commit comments

Comments
 (0)