Skip to content

Fix inner block problem with 'catch' #3129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,7 @@ class WasmBinaryBuilder {
void visitBlock(Block* curr);

// Gets a block of expressions. If it's just one, return that singleton.
Expression* getBlockOrSingleton(Type type, unsigned numPops = 0);
Expression* getBlockOrSingleton(Type type);

void visitIf(If* curr);
void visitLoop(Loop* curr);
Expand Down Expand Up @@ -1444,7 +1444,7 @@ class WasmBinaryBuilder {
void visitRefNull(RefNull* curr);
void visitRefIsNull(RefIsNull* curr);
void visitRefFunc(RefFunc* curr);
void visitTry(Try* curr);
void visitTryOrTryInBlock(Expression*& out);
void visitThrow(Throw* curr);
void visitRethrow(Rethrow* curr);
void visitBrOnExn(BrOnExn* curr);
Expand Down
102 changes: 83 additions & 19 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2441,7 +2441,7 @@ BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) {
visitRefFunc((curr = allocator.alloc<RefFunc>())->cast<RefFunc>());
break;
case BinaryConsts::Try:
visitTry((curr = allocator.alloc<Try>())->cast<Try>());
visitTryOrTryInBlock(curr);
break;
case BinaryConsts::Throw:
visitThrow((curr = allocator.alloc<Throw>())->cast<Throw>());
Expand Down Expand Up @@ -2692,21 +2692,11 @@ void WasmBinaryBuilder::visitBlock(Block* curr) {
}

// Gets a block of expressions. If it's just one, return that singleton.
// numPops is the number of pop instructions we add before starting to parse the
// block. Can be used when we need to assume certain number of values are on top
// of the stack in the beginning.
Expression* WasmBinaryBuilder::getBlockOrSingleton(Type type,
unsigned numPops) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This numPops thing was added to be used by catch, but now it does not use this method anymore, so deleted it.

Expression* WasmBinaryBuilder::getBlockOrSingleton(Type type) {
Name label = getNextLabel();
breakStack.push_back({label, type});
auto start = expressionStack.size();

Builder builder(wasm);
for (unsigned i = 0; i < numPops; i++) {
auto* pop = builder.makePop(Type::exnref);
pushExpression(pop);
}

processExpressions();
size_t end = expressionStack.size();
if (end < start) {
Expand Down Expand Up @@ -2757,12 +2747,12 @@ void WasmBinaryBuilder::visitLoop(Loop* curr) {
auto start = expressionStack.size();
processExpressions();
size_t end = expressionStack.size();
if (start > end) {
throwError("block cannot pop from outside");
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just moved to make code cleaner (in my opinion)

if (end - start == 1) {
curr->body = popExpression();
} else {
if (start > end) {
throwError("block cannot pop from outside");
}
auto* block = allocator.alloc<Block>();
pushBlockElements(block, curr->type, start);
block->finalize(curr->type);
Expand Down Expand Up @@ -4834,8 +4824,9 @@ void WasmBinaryBuilder::visitRefFunc(RefFunc* curr) {
curr->finalize();
}

void WasmBinaryBuilder::visitTry(Try* curr) {
void WasmBinaryBuilder::visitTryOrTryInBlock(Expression*& out) {
BYN_TRACE("zz node: Try\n");
auto* curr = allocator.alloc<Try>();
startControlFlow(curr);
// For simplicity of implementation, like if scopes, we create a hidden block
// within each try-body and catch-body, and let branches target those inner
Expand All @@ -4845,11 +4836,84 @@ void WasmBinaryBuilder::visitTry(Try* curr) {
if (lastSeparator != BinaryConsts::Catch) {
throwError("No catch instruction within a try scope");
}
curr->catchBody = getBlockOrSingleton(curr->type, 1);

// For simplicity, we create an inner block within the catch body too, but the
// one within the 'catch' *must* be omitted when we write out the binary back
// later, because the 'catch' instruction pushes a value onto the stack and
// the inner block does not support block input parameters without multivalue
// support.
// try
// ...
// catch ;; Pushes a value onto the stack
// block ;; Inner block. Should be deleted when writing binary!
// use the pushed value
// end
// end
//
// But when input binary code is like
// try
// ...
// catch
// br 0
// end
//
// 'br 0' accidentally happens to target the inner block, creating code like
// this in Binaryen IR, making the inner block not deletable, resulting in a
// validation error:
// (try
// ...
// (catch
// (block $label0 ;; Cannot be deleted, because there's a branch to this
// ...
// (br $label0)
// )
// )
// )
//
// When this happens, we fix this by creating a block that wraps the whole
// try-catch, and making the branches target that block instead, like this:
// (block $label ;; New enclosing block, new target for the branch
// (try
// ...
// (catch
// (block ;; Now this can be deleted when writing binary
// ...
// (br $label0)
// )
// )
// )
// )
Name catchLabel = getNextLabel();
breakStack.push_back({catchLabel, curr->type});
auto start = expressionStack.size();

Builder builder(wasm);
pushExpression(builder.makePop(Type::exnref));

processExpressions();
size_t end = expressionStack.size();
if (start > end) {
throwError("block cannot pop from outside");
}
if (end - start == 1) {
curr->catchBody = popExpression();
} else {
auto* block = allocator.alloc<Block>();
pushBlockElements(block, curr->type, start);
block->finalize(curr->type);
curr->catchBody = block;
}
curr->finalize(curr->type);
if (lastSeparator != BinaryConsts::End) {
throwError("try should end with end");

if (breakTargetNames.find(catchLabel) == breakTargetNames.end()) {
out = curr;
} else {
// Create a new block that encloses the whole try-catch
auto* block = builder.makeBlock(catchLabel, curr);
out = block;
}
breakStack.pop_back();
breakTargetNames.erase(catchLabel);
}

void WasmBinaryBuilder::visitThrow(Throw* curr) {
Expand Down
Binary file added test/break-within-catch.wasm
Binary file not shown.
19 changes: 19 additions & 0 deletions test/break-within-catch.wasm.fromBinary
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(module
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original break-within-catch.wasm's wasm2wat output is this:

(module
  (type (;0;) (func))
  (func (;0;) (type 0)
    try  ;; label = @1
      nop
    catch
      drop
      br 0 (;@1;)
    end))

(type $none_=>_none (func))
(func $0
(block $label$2
(try
(do
(nop)
)
(catch
(drop
(pop exnref)
)
(br $label$2)
)
)
)
)
)