summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2021-02-19 19:19:56 +0000
committerGitHub <noreply@github.com>2021-02-19 11:19:56 -0800
commite24c1f0c826db6dcd63fc015f961919657accce5 (patch)
treef504fd0d227464befcb044b166ecbab056305cda
parentb6c094c8760b3c3e7ffbc54c46c6329b22d88cfe (diff)
downloadbinaryen-e24c1f0c826db6dcd63fc015f961919657accce5.tar.gz
binaryen-e24c1f0c826db6dcd63fc015f961919657accce5.tar.bz2
binaryen-e24c1f0c826db6dcd63fc015f961919657accce5.zip
[Wasm Exceptions] Fix binary parsing of a normal break to a try in a singleton (#3581)
The fix here is to remove the code with // maybe we don't need a block here? That would remove a try's block if we thought it wasn't needed. However, it just checked for exception branches, but not normal branches, which are also possible. At that location, we don't have a good way to find out if the block has other branches to it aside from scanning its contents. So this PR just gives up on doing so, which means we add an unnecessary block if the optimizer is not run. If this matters we could make the binary parser more complicated by remembering whether a block had branches in the past, but I'm not sure if it's worth it.
-rw-r--r--src/wasm/wasm-binary.cpp4
-rw-r--r--test/br_to_try.wasmbin0 -> 41 bytes
-rw-r--r--test/br_to_try.wasm.fromBinary20
-rw-r--r--test/exception-handling.wast.fromBinary10
-rw-r--r--test/exception-handling.wast.fromBinary.noDebugInfo10
-rw-r--r--test/passes/dwarf_with_exceptions.cpp10
-rw-r--r--test/try-delegate.wasm.fromBinary28
7 files changed, 55 insertions, 27 deletions
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index dc0b1ab21..730129892 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -5846,10 +5846,6 @@ void WasmBinaryBuilder::visitTryOrTryInBlock(Expression*& out) {
BranchUtils::replaceExceptionTargets(block, block->name, curr->name);
exceptionTargetNames.erase(block->name);
}
- // maybe we don't need a block here?
- if (block->list.size() == 1) {
- curr->body = block->list[0];
- }
}
}
if (exceptionTargetNames.find(catchLabel) != exceptionTargetNames.end()) {
diff --git a/test/br_to_try.wasm b/test/br_to_try.wasm
new file mode 100644
index 000000000..92e990359
--- /dev/null
+++ b/test/br_to_try.wasm
Binary files differ
diff --git a/test/br_to_try.wasm.fromBinary b/test/br_to_try.wasm.fromBinary
new file mode 100644
index 000000000..8aa1bdd5e
--- /dev/null
+++ b/test/br_to_try.wasm.fromBinary
@@ -0,0 +1,20 @@
+(module
+ (type $none_=>_none (func))
+ (type $i32_=>_none (func (param i32)))
+ (event $event$0 (attr 0) (param i32))
+ (func $0
+ (try $label$3
+ (do
+ (block $label$1
+ (br $label$1)
+ )
+ )
+ (catch $event$0
+ (drop
+ (pop i32)
+ )
+ )
+ )
+ )
+)
+
diff --git a/test/exception-handling.wast.fromBinary b/test/exception-handling.wast.fromBinary
index 89d0bb7fb..afb9da9ac 100644
--- a/test/exception-handling.wast.fromBinary
+++ b/test/exception-handling.wast.fromBinary
@@ -246,11 +246,13 @@
)
(try $label$25
(do
- (try $label$23
- (do
- (call $foo)
+ (block $label$20
+ (try $label$23
+ (do
+ (call $foo)
+ )
+ (delegate $label$25)
)
- (delegate $label$25)
)
)
(delegate 0)
diff --git a/test/exception-handling.wast.fromBinary.noDebugInfo b/test/exception-handling.wast.fromBinary.noDebugInfo
index 47577065c..19ac444fc 100644
--- a/test/exception-handling.wast.fromBinary.noDebugInfo
+++ b/test/exception-handling.wast.fromBinary.noDebugInfo
@@ -246,11 +246,13 @@
)
(try $label$25
(do
- (try $label$23
- (do
- (call $0)
+ (block $label$20
+ (try $label$23
+ (do
+ (call $0)
+ )
+ (delegate $label$25)
)
- (delegate $label$25)
)
)
(delegate 0)
diff --git a/test/passes/dwarf_with_exceptions.cpp b/test/passes/dwarf_with_exceptions.cpp
index 283b30064..5854202e1 100644
--- a/test/passes/dwarf_with_exceptions.cpp
+++ b/test/passes/dwarf_with_exceptions.cpp
@@ -7,8 +7,12 @@ void dwarf_with_exceptions() {
}
}
// How to generate dwarf_with_exceptions.wasm:
-// $ clang++ -std=c++14 --target=wasm32-unknown-unknown -g -fwasm-exceptions -Xclang -disable-O0-optnone -c -S -emit-llvm dwarf_with_exceptions.cpp -o temp.ll
+// $ clang++ -std=c++14 --target=wasm32-unknown-unknown -g -fwasm-exceptions \
+// -Xclang -disable-O0-optnone -c -S -emit-llvm
+// dwarf_with_exceptions.cpp -o temp.ll
// $ opt -S -mem2reg -simplifycfg temp.ll -o dwarf_with_exceptions.ll
// Remove some personal info from dwarf_with_exceptions.ll
-// $ llc -exception-model=wasm -mattr=+exception-handling -filetype=obj dwarf_with_exceptions.ll -o dwarf_with_exceptions.o
-// $ wasm-ld --no-entry --no-gc-sections --allow-undefined dwarf_with_exceptions.o -o dwarf_with_exceptions.wasm
+// $ llc -exception-model=wasm -mattr=+exception-handling -filetype=obj \
+// dwarf_with_exceptions.ll -o dwarf_with_exceptions.o
+// $ wasm-ld --no-entry --no-gc-sections --allow-undefined \
+// dwarf_with_exceptions.o -o dwarf_with_exceptions.wasm
diff --git a/test/try-delegate.wasm.fromBinary b/test/try-delegate.wasm.fromBinary
index fabbfebd0..ee4dbaabe 100644
--- a/test/try-delegate.wasm.fromBinary
+++ b/test/try-delegate.wasm.fromBinary
@@ -4,10 +4,12 @@
(func $0
(try $label$6
(do
- (try $label$4
- (do
+ (block $label$1
+ (try $label$4
+ (do
+ )
+ (delegate $label$6)
)
- (delegate $label$6)
)
)
(catch $event$0
@@ -17,17 +19,19 @@
(func $1
(try $label$9
(do
- (try $label$7
- (do
- )
- (catch $event$0
- (drop
- (i32.const 0)
+ (block $label$1
+ (try $label$7
+ (do
)
- (try $label$6
- (do
+ (catch $event$0
+ (drop
+ (i32.const 0)
+ )
+ (try $label$6
+ (do
+ )
+ (delegate $label$9)
)
- (delegate $label$9)
)
)
)