From 1254b564b2d36cbf96ef8b8fe0c17fa2fa668ae3 Mon Sep 17 00:00:00 2001 From: Heejin Ahn Date: Thu, 18 Feb 2021 04:36:56 +0900 Subject: [EH] Make rethrow's target a try label (#3568) I was previously mistaken about `rethrow`'s argument rule and thought it only counted `catch`'s depth. But it turns out it follows the same rule `delegate`'s label: the immediate argument follows the same rule as when computing branch labels, but it only can target `try` labels (semantically it targets that `try`'s corresponding `catch`); otherwise it will be a validation failure. Unlike `delegate`, `rethrow`'s label denotes not where to rethrow, but which exception to rethrow. For example, ```wasm try $l0 catch ($l0) try $l1 catch ($l1) rethrow $l0 ;; rethrow the exception caught by 'catch ($l0)' end end ``` Refer to this comment for the more detailed informal semantics: https://github.com/WebAssembly/exception-handling/issues/146#issuecomment-777714491 --- This also reverts some of `delegateTarget` -> `exceptionTarget` changes done in #3562 in the validator. Label validation rules apply differently for `delegate` and `rethrow` for try-catch. For example, this is valid: ```wasm try $l0 try delegate $l0 catch ($l0) end ``` But this is NOT valid: ```wasm try $l0 catch ($l0) try delegate $l0 end ``` So `try`'s label should be used within try-catch range (not catch-end range) for `delegate`s. But for the `rethrow` the rule is different. For example, this is valid: ```wasm try $l0 catch ($l0) rethrow $l0 end ``` But this is NOT valid: ```wasm try $l0 rethrow $l0 catch ($l0) end ``` So the `try`'s label should be used within catch-end range instead. --- test/exception-handling.wast | 119 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 108 insertions(+), 11 deletions(-) (limited to 'test/exception-handling.wast') diff --git a/test/exception-handling.wast b/test/exception-handling.wast index 399fef14c..45cb21910 100644 --- a/test/exception-handling.wast +++ b/test/exception-handling.wast @@ -130,17 +130,6 @@ ) ) ) - - ;; rethrow - (try - (do - (throw $e-i32 (i32.const 0)) - ) - (catch $e-i32 - (drop (pop i32)) - (rethrow 0) - ) - ) ) (func $delegate-test @@ -200,4 +189,112 @@ (delegate 0) ) ) + + (func $rethrow-test + ;; Simple try-catch-rethrow + (try $l0 + (do + (call $foo) + ) + (catch $e-i32 + (drop (pop i32)) + (rethrow $l0) ;; by label + ) + (catch_all + (rethrow 0) ;; by depth + ) + ) + + ;; When there are both a branch and a rethrow that target the same try + ;; label. Because binaryen only allows blocks and loops to be targetted by + ;; branches, we wrap the try with a block and make branches that block + ;; instead, resulting in the br and rethrow target different labels in the + ;; output. + (try $l0 + (do + (call $foo) + ) + (catch $e-i32 + (drop (pop i32)) + (rethrow $l0) + ) + (catch_all + (br $l0) + ) + ) + + ;; One more level deep + (try $l0 + (do + (call $foo) + ) + (catch_all + (try + (do + (call $foo) + ) + (catch $e-i32 + (drop (pop i32)) + (rethrow $l0) ;; by label + ) + (catch_all + (rethrow 1) ;; by depth + ) + ) + ) + ) + + ;; Interleaving block + (try $l0 + (do + (call $foo) + ) + (catch_all + (try + (do + (call $foo) + ) + (catch $e-i32 + (drop (pop i32)) + (block $b0 + (rethrow $l0) ;; by label + ) + ) + (catch_all + (block $b1 + (rethrow 2) ;; by depth + ) + ) + ) + ) + ) + + ;; Within nested try, but rather in 'try' part and not 'catch' + (try $l0 + (do + (call $foo) + ) + (catch_all + (try + (do + (rethrow $l0) ;; by label + ) + (catch_all) + ) + ) + ) + (try $l0 + (do + (call $foo) + ) + (catch_all + (try + (do + (rethrow 1) ;; by depth + ) + (catch_all) + ) + ) + ) + ) ) -- cgit v1.2.3