You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 25, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 25, 2025. It is now read-only.
Any formal execution definition of exception handling? #87
I'm interested in formal execution definition of WASM with exception handling proposal included.
What I now think is that the try catch end block semantics with throw instruction can be described in reduction rules, similar to PLDI'17 MVP spec paper.
Also, I heard from @aheejin that @rossberg has done some work on the formal spec of the proposal.
Are there in-progress works related to formal definition of exception handling?
There is nothing written up in publishable form yet. So let me just dump the rules as an extension to the PLDI paper (or rather, the somewhat updated CACM version) in ASCII art here. Hope that helps answering your question. :)
Abstract Syntax
Value Types
t ::= ... | exnref
Exception Types
et ::= t*
Instructions
e ::= ... | throw x | rethrow | try ft e* catch e* end | br_on_exn l x
Exceptions
exn ::= ex* exception et | ex* exception et im
Modules
mod ::= module ... exn*
Typing
Contexts
C ::= {..., exn et*}
Rules
C_exn(x) = t*
----------------------------
C |- throw x : t1* t* -> t2*
--------------------------------
C |- rethrow : t1* exnref -> t2*
ft = t1* -> t2*
C, label t2* |- e1* : t1* -> t2*
C, label t2* |- e2* : exnref -> t2*
-----------------------------------
C |- try ft e1* catch e2* end : ft
C_label(l) = C_exn(x)
-------------------------------------
C |- br_on_exn l x : exnref -> exnref
Reduction
Stores
S ::= {..., exn ei*}
Exception Instances
ei ::= {type et}
Module Instances
m ::= {..., exn a*}
Values
v ::= ... | exn a v*
Administrative Instructions
e ::= ... | throw a | catch_n{e*} e* end | exn a v*
Throw Contexts
T ::= v* [_] e* | label_n{e*} T end | catch_n{e*} T end | frame_n{F} T end
Rules
F; throw x --> F; throw a
(iff F_exn(x) = a)
v^n (try ft e1* catch e2* end) --> catch_m{e2*} (label_m{} v^n e1* end) end)
(iff ft = t1^n -> t2^m)
catch_m{e*} v^m end --> v^m
S; F; catch_m{e*} T[v^n (throw a)] end --> S; F; label_m{} (exn a v^n) e* end
(iff S_exn(a) = {typ t^n})
(exn a v*) rethrow --> v* (throw a)
ref.null rethrow --> trap
F; (exn a v*) (br_on_exn l x) --> F; v* (br l)
(iff F_exn(x) = a)
F; (exn a v*) (br_on_exn l x) --> F; (exn a v*)
(iff F_exn(x) =/= a)
F; ref.null (br_on_exn l x) --> F; trap
Thank you for the response!
I have additional questions about the semantics provided.
In the proposal, br_on_exn pops the exnref values from the stack and instead pushes the exception's argument values on top of the stack, if given exception index matches. The typing rule for br_on_exn then should assume exnref value on the top, and result in t* values afterwards. In other words, the typing rule should be,
C_label(l) = C_exn(x) = t*
-------------------------------------------
C |- br_on_exn l x : exnref -> t*
Also in the reduction rule, exception values are represented as exn a v*. Reduction rules for rethrow and br_on_exn assumes that exception arguments v* resides in operand stack. In implementation, where exception argument values are actually stored? From my understanding, only exnref value stays at the stack and exception arguments are stored in linear memory space, where the exnref value points to. Is this correct interpretation, or am I missing something?
No, the typing rule for br_on_exn is correct as given. Keep in mind that the values are only pushed onto the stack if the branch is taken, while the typing rule describes the stack manipulation of linear execution.
Exnref is an abstract (reference) type. It is an implementation detail how and where its argument values are stored. It is separate from any linear memory.
@sys5867, sorry, as was just pointed out to me by @ioannad, there indeed was a bug in the typing rules for br_on_exn: the t* in the conclusion should not be there, the overall type should just be exnref -> exnref. Fixed now.
@rossberg , with the addition of throw contexts, invoke might end up in a state where it neither succeeded with values nor has trapped, but is some throw context with a throw waiting for its catching try block. The simplest example is when invoking the function wasm_throw defined below:
Invoking wasm_throw gets "stuck" in the state store; frame_1{F'} label_1 {} (throw tag) end end, where tag is the event address of exn, and F' the frame with the above module's instance.
I suppose this is the case meant by "the embedder defines how to handle the uncaught exception". But in this case it is not true any more that "Reduction terminates when a thread’s instruction sequence has been reduced to a result, that is, either a sequence of values or to a trap." (e.g., in the definition of evaluation contexts).
Should some other form of "result" (which is not a sequence of values or a trap) be defined to cover such cases of unresolved throw contexts?
@ioannad, yes, correct: with exceptions we will naturally need to extend the notion of result to include throw a. And the rules for reducing evaluation contexts will also need a couple of extra rules to get there.
Basically, it all needs to mirror what we have for traps. Except that there also is the try rule to intercept a throw.
Ah, okay, excellent point. Thanks! I stand corrected.
Of course, it wouldn't be hard to work around that by using a secondary dynamic token and compare that in the handler. But I admit that reduces the appeal of that solution. A more elegant solution would be possible if we removed the (somewhat artificial) limitation of static exception tag allocation in the EH proposal, which might be desirable for other things as well.
Yeah, I'm still uncomfortable with committing to ad-hoc solutions prematurely and without a strategy that sound sounds different from "let's built in every feature". Dynamic tag generation would be a more orthogonal and principled solution to the particular problem you pointed out. OTOH, the potential harm of unwind is hopefully limited, so I won't die on this hill. But I do remain worried about the path this approach puts us on.
At present the proposal has dynamically scoped local and non-local control but only lexically scoped local, so it seems natural to complete the square with lexically scoped non-local control. Regarding principles, lexically scoped non-local control has stronger parametricity properties. It also supports more efficient (in terms of order of complexity) non-local control transfers because there is no need to search. Dynamic tag generation can emulate lexically scoped non-local control but with weaker guarantees and poorer performance, and the current proposal can already emulate dynamic tag generation. As for the path, completing the square seems to cover the single-stack control space very well.
I'm not very sure why we are discussing lexical vs. dynamically scoping here. Do we plan to support any dynamically scoped language? Is wasm a dynamically scoped model? (I think no)
I share @rossberg's concern that, even if we need a functionality similar to unwind in the follow-on 2PEH proposal, given that it does not have a meaningful difference with catch_all now, adding it in the current proposal seems premature. We don't know what the follow-on 2PEH proposal will look like, and adding unwind there looks OK to me.
Reacted by Ioanna M Dimitriou H and Asumu Takikawa
given that it [unwind] does not have a meaningful difference with catch_all now, adding it in the current proposal seems premature. We don't know what the follow-on 2PEH proposal will look like, and adding unwind there looks OK to me.
I agree with this. And as I feared in a comment to PR#137, trying to specify unwind is already leading to long discussions which probably belong in a follow-on 2PEH proposal. I think the point of changing the 2nd EH proposal to the current, was just to have it be compatible with a 2PEH follow-on proposal, not more.
I just made PR #143 with a document attempting to describe the current spec, as @tlively requested.
@aheejin if you like the idea of keeping such a formal-overview.md document, perhaps we could close this issue by pointing to that PR? Or to the document when everyone agrees with it?
Yes, I think continuing discussions in a dedicated issue or PR is more desirable. This issue started as a question from someone else that if we had a formal spec at all, and got expanded to include a number of topics I can't even count.
Activity
rossberg commentedon Aug 1, 2019
There is nothing written up in publishable form yet. So let me just dump the rules as an extension to the PLDI paper (or rather, the somewhat updated CACM version) in ASCII art here. Hope that helps answering your question. :)
Abstract Syntax
Value Types
Exception Types
Instructions
Exceptions
Modules
Typing
Contexts
Rules
Reduction
Stores
Exception Instances
Module Instances
Values
Administrative Instructions
Throw Contexts
Rules
sys5867 commentedon Aug 16, 2019
Thank you for the response!
I have additional questions about the semantics provided.
In the proposal,
br_on_exnpops theexnrefvalues from the stack and instead pushes the exception's argument values on top of the stack, if given exception index matches. The typing rule forbr_on_exnthen should assumeexnrefvalue on the top, and result in t* values afterwards. In other words, the typing rule should be,Also in the reduction rule, exception values are represented as
exn a v*. Reduction rules forrethrowandbr_on_exnassumes that exception argumentsv*resides in operand stack. In implementation, where exception argument values are actually stored? From my understanding, onlyexnrefvalue stays at the stack and exception arguments are stored in linear memory space, where theexnrefvalue points to. Is this correct interpretation, or am I missing something?rossberg commentedon Aug 16, 2019
No, the typing rule for
br_on_exnis correct as given. Keep in mind that the values are only pushed onto the stack if the branch is taken, while the typing rule describes the stack manipulation of linear execution.Exnref is an abstract (reference) type. It is an implementation detail how and where its argument values are stored. It is separate from any linear memory.
rossberg commentedon Jan 17, 2020
@sys5867, sorry, as was just pointed out to me by @ioannad, there indeed was a bug in the typing rules for br_on_exn: the
t*in the conclusion should not be there, the overall type should just beexnref -> exnref. Fixed now.Dependencies in proposal (#99)
current core formal spec for the WebAssembly Exception Handling proposal
ioannad commentedon Jun 4, 2020
@rossberg , with the addition of throw contexts,
invokemight end up in a state where it neither succeeded with values nor has trapped, but is some throw context with a throw waiting for its catching try block. The simplest example is when invoking the functionwasm_throwdefined below:Invoking
wasm_throwgets "stuck" in the statestore; frame_1{F'} label_1 {} (throw tag) end end, wheretagis the event address ofexn, andF'the frame with the above module's instance.I suppose this is the case meant by "the embedder defines how to handle the uncaught exception". But in this case it is not true any more that "Reduction terminates when a thread’s instruction sequence has been reduced to a result, that is, either a sequence of values or to a trap." (e.g., in the definition of evaluation contexts).
Should some other form of "result" (which is not a sequence of values or a trap) be defined to cover such cases of unresolved throw contexts?
rossberg commentedon Jun 4, 2020
@ioannad, yes, correct: with exceptions we will naturally need to extend the notion of result to include
throw a. And the rules for reducing evaluation contexts will also need a couple of extra rules to get there.Basically, it all needs to mirror what we have for traps. Except that there also is the try rule to intercept a throw.
Fix merge conflicts introduced w/ PRs WebAssembly#84 and WebAssembly#86…
Remove subtyping (WebAssembly#87)
Dependencies in proposal (WebAssembly#99)
Current core formal spec for the WebAssembly Exception Handling Propo…
Current core formal spec for WebAssembly Exception Handling
Current core formal spec for WebAssembly Exception Handling
Current core formal spec for WebAssembly Exception Handling (#121)
56 remaining items
rossberg commentedon Oct 22, 2020
Ah, okay, excellent point. Thanks! I stand corrected.
Of course, it wouldn't be hard to work around that by using a secondary dynamic token and compare that in the handler. But I admit that reduces the appeal of that solution. A more elegant solution would be possible if we removed the (somewhat artificial) limitation of static exception tag allocation in the EH proposal, which might be desirable for other things as well.
RossTate commentedon Oct 26, 2020
Thanks for helping me find the right illustrative example. Did that also help convey to you the rationale of
unwindthen?rossberg commentedon Oct 27, 2020
Yeah, I'm still uncomfortable with committing to ad-hoc solutions prematurely and without a strategy that sound sounds different from "let's built in every feature". Dynamic tag generation would be a more orthogonal and principled solution to the particular problem you pointed out. OTOH, the potential harm of unwind is hopefully limited, so I won't die on this hill. But I do remain worried about the path this approach puts us on.
RossTate commentedon Oct 27, 2020
At present the proposal has dynamically scoped local and non-local control but only lexically scoped local, so it seems natural to complete the square with lexically scoped non-local control. Regarding principles, lexically scoped non-local control has stronger parametricity properties. It also supports more efficient (in terms of order of complexity) non-local control transfers because there is no need to search. Dynamic tag generation can emulate lexically scoped non-local control but with weaker guarantees and poorer performance, and the current proposal can already emulate dynamic tag generation. As for the path, completing the square seems to cover the single-stack control space very well.
aheejin commentedon Nov 4, 2020
I'm not very sure why we are discussing lexical vs. dynamically scoping here. Do we plan to support any dynamically scoped language? Is wasm a dynamically scoped model? (I think no)
I share @rossberg's concern that, even if we need a functionality similar to
unwindin the follow-on 2PEH proposal, given that it does not have a meaningful difference withcatch_allnow, adding it in the current proposal seems premature. We don't know what the follow-on 2PEH proposal will look like, and addingunwindthere looks OK to me.ioannad commentedon Nov 4, 2020
I agree with this. And as I feared in a comment to PR#137, trying to specify
unwindis already leading to long discussions which probably belong in a follow-on 2PEH proposal. I think the point of changing the 2nd EH proposal to the current, was just to have it be compatible with a 2PEH follow-on proposal, not more.formal spec overview for the 3rd exception handling proposal
ioannad commentedon Nov 10, 2020
I just made PR #143 with a document attempting to describe the current spec, as @tlively requested.
@aheejin if you like the idea of keeping such a formal-overview.md document, perhaps we could close this issue by pointing to that PR? Or to the document when everyone agrees with it?
aheejin commentedon Nov 10, 2020
Yes, I think continuing discussions in a dedicated issue or PR is more desirable. This issue started as a question from someone else that if we had a formal spec at all, and got expanded to include a number of topics I can't even count.
Dependencies in proposal (WebAssembly#99)
Current core formal spec for WebAssembly Exception Handling (WebAssem…
Current core formal spec for WebAssembly Exception Handling (WebAssem…
formal spec overview for the 3rd exception handling proposal
formal spec overview for the 3rd exception handling proposal