-
Notifications
You must be signed in to change notification settings - Fork 791
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>()); | ||
|
@@ -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) { | ||
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) { | ||
|
@@ -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"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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 | ||
|
@@ -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) { | ||
|
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
(module | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original (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) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 bycatch
, but now it does not use this method anymore, so deleted it.