Skip to content

Commit 9149f4c

Browse files
committed
fix reading of stacky unreadable code with elements we need to drop
1 parent b9ce586 commit 9149f4c

2 files changed

Lines changed: 17 additions & 16 deletions

File tree

src/wasm-binary.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ class WasmBinaryBuilder {
861861
int depth = 0; // only for debugging
862862

863863
BinaryConsts::ASTNodes readExpression(Expression*& curr);
864+
void pushBlockElements(Block* curr, size_t start, size_t end);
864865
void visitBlock(Block *curr);
865866
Expression* getMaybeBlock(WasmType type);
866867
Expression* getBlock(WasmType type);

src/wasm/wasm-binary.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,6 +2073,20 @@ BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) {
20732073
return BinaryConsts::ASTNodes(code);
20742074
}
20752075

2076+
void WasmBinaryBuilder::pushBlockElements(Block* curr, size_t start, size_t end) {
2077+
for (size_t i = start; i < end; i++) {
2078+
auto* item = expressionStack[i];
2079+
curr->list.push_back(item);
2080+
if (i < end - 1) {
2081+
// stacky&unreachable code may introduce elements that need to be dropped in non-final positions
2082+
if (isConcreteWasmType(item->type)) {
2083+
curr->list.back() = Builder(wasm).makeDrop(curr->list.back());
2084+
}
2085+
}
2086+
}
2087+
expressionStack.resize(start);
2088+
}
2089+
20762090
void WasmBinaryBuilder::visitBlock(Block *curr) {
20772091
if (debug) std::cerr << "zz node: Block" << std::endl;
20782092
// special-case Block and de-recurse nested blocks in their first position, as that is
@@ -2108,18 +2122,7 @@ void WasmBinaryBuilder::visitBlock(Block *curr) {
21082122
if (end < start) {
21092123
throw ParseException("block cannot pop from outside");
21102124
}
2111-
for (size_t i = start; i < end; i++) {
2112-
if (debug) std::cerr << " " << size_t(expressionStack[i]) << "\n zz Block element " << curr->list.size() << std::endl;
2113-
auto* item = expressionStack[i];
2114-
curr->list.push_back(item);
2115-
if (i < end - 1) {
2116-
// stacky&unreachable code may introduce elements that need to be dropped in non-final positions
2117-
if (isConcreteWasmType(item->type)) {
2118-
curr->list.back() = Builder(wasm).makeDrop(curr->list.back());
2119-
}
2120-
}
2121-
}
2122-
expressionStack.resize(start);
2125+
pushBlockElements(curr, start, end);
21232126
curr->finalize(curr->type);
21242127
breakStack.pop_back();
21252128
}
@@ -2136,11 +2139,8 @@ Expression* WasmBinaryBuilder::getMaybeBlock(WasmType type) {
21362139
throw ParseException("block cannot pop from outside");
21372140
}
21382141
auto* block = allocator.alloc<Block>();
2139-
for (size_t i = start; i < end; i++) {
2140-
block->list.push_back(expressionStack[i]);
2141-
}
2142+
pushBlockElements(block, start, end);
21422143
block->finalize(type);
2143-
expressionStack.resize(start);
21442144
return block;
21452145
}
21462146

0 commit comments

Comments
 (0)