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.
I think there is a meaningful difference between explicitly-raised exceptions (which I guess includes JS exceptions but ignoring that for now) and machine-type error traps (OOB, signature mismatch etc) and it's not clear that they should be catchable in the same way that user exceptions are. So obviously we can debate that in this issue (and we can design such a feature) but I think the first proposal should exclude them because the use cases are very different.
Right, we've talked about having support for signals as well as exceptions. It would be interesting to define what we see in each bucket, and what other platforms have done. Specifically, I think the Windows SEH approach, its ups and downs, can inform our decision.
One advantage the Windows SEH approach has over handling runtime traps with signals is that it avoids global state that determines how a trap will be handled. There isn't a good way with POSIX signals to handle a trap from just one thread.
A Windows SEH filter is effectively a stack-scoped signal handler: when an exception occurs, the SEH filter functions are called "on top" of the stack, so the information is still available to resume execution, or get a stack trace, or unwind to the handler. A C++ catch maps to a SEH filter that checks the exception information and returns a value that indicates the associated handler should be executed.
Thread-local signal handlers would provide the same functionality (could you throw from them?), but require some additional mechanism to compose (a stack of handlers, perhaps?).
My most preferred design would be very close to Windows SEH. My least preferred design would be something that unifies traps and exceptions, but doesn't allow resuming or inspecting the stack at the trapping code.
This proposal intentionally did not focus on what exceptions should be produced by WebAsembly. Rather, any such exception would need to be imported to be caught, since it is not one the module defines.
Hence, the framework needed to handle such exceptions, should they be added, is already there. We would not need to add any new concepts.
Hence, the framework needed to handle such exceptions, should they be added, is already there. We would not need to add any new concepts.
If runtime-generated traps can be caught as exceptions, then there should be a mechanism to retry the trapping code. There should also be a way to handle a runtime trap by capturing a stack trace and other state information at the point of the trap for automatic bug reporting.
I think it's fine to start with exceptions as they are and add the ability to catch runtime traps later, but there's additional functionality required if the above cases are to be supported. Would that require changing any of base exception functionality?
I can imagine adding a retry_throw operator that can be used similarly to the rethrow operator. That seems like it is orthogonal to this proposal.
A way to capture a stack trace is not specified by WebAssembly; without considering whether it should be, what would be needed from this proposal for an implementation to expose the trapping call stack to a runtime trap handler?
I don't think we want to outright declare this as a future feature. It's worth exploring having it in the first version as well, and if that's not viable then move to future.
@jfbastien, are you referring to reifying traps as exceptions, or supporting resumption? Both are separate features, and the latter is far more difficult and debatable than the former.
At least in the future I've been imagining, the distinction between traps and exceptions is:
traps: execute at the instruction, as if they were called by the instruction, can inspect local and stack state, and finally choose how to proceed, either by: (1) resume (with one of several suboptions here depending on the faulting instruction category, e.g., "resume with this value" for value-producing insns), (2) throw (with a given exception tag)
exceptions are all about unwinding to an enclosing try block, taking for granted that this instruction has already chosen to throw.
With this distinction, exception handling is single-pass unwind-as-you-go, unlike, e.g., itanium ABI's two-pass design. Also, it is an orthogonal question of how to define which trap handler to call (module-scoped, lexically-scoped, dynamically-scoped like SEH, etc).
Also, with this split, trap handlers are definitely the more advanced feature with the more narrow (but important, in the limit, if we want to allow all the fancy VM tricks) use case, so I think we should keep them separate from the exception handling MVP knowing they can be added later in a complementary way.
@andrewackerman, there are implementation-level type errors and language-level (i.e., user) type errors. Traps signify the former, while the latter will typically have to be implemented by a compiler. I think it's best not to conflate the two. Even user-observable null checks should best be explicit in the code, to avoid as much overhead as possible around implementation-level checks.
@rossberg If the compiler need to check to prevent runtime traps then it can be required to add some more instructions. For example in the complex range of the GC spec. I have not check this if this really required.
The more I think about it the better I like it. Each language can precisely prevent the traps that are defined in its language.
To finish this discussion this should be a topic in one of the next community/working group meetings.
I've been OOO (and will be for some more days) so I haven't fully caught up with the comments so far, but one thing I'd like to make sure is even if we decide to catch traps with catch, that does NOT mean C++'s catch clause will catch them. See #1 (comment) for reasons why it's not possible.
We discussed the topic in the in-person CG meeting in June. It's been a while, but I'd like to reopen the discussions and make decisions on this issue to proceed to Stage 2 for the EH proposal.
I'd like to propose that we don't catch traps with wasm catch instruction. After many discussions, I feel that traps have characteristics that more resemble signals than exceptions, meaning that 1. at least some of them should not be catchable (i.e., should result in abort) and 2. they usually don't need to be handled in local scopes like try-catch. I'm not saying the interface for handling traps should be necessarily similar to that of POSIX signals; what I mean is that it can be a separate proposal from the EH proposal if that functionality becomes necessary, in which traps can be handled in more broader scope, such as per function or per module.
As I mentioned above, for a practical reason, we need a way to abort, to implement functions like C abort or C++ std::terminate. In JS embedding, they are currently implemented in JS as throwing a JS exception, which is catchable by wasm catch instruction. I'd say that we implement those functions using wasm traps, and make those traps uncatchable.
I also think whether hardware traps should be handled like or converted to exceptions is the job of languages themselves. For example, Rust compiler inserts code that checks for divide by zero errors and throws exceptions. C++ treat them as UB and doesn't pay the performance cost. Java VM handles arithmetic errors using signal-handler-like routines and creates and throws Java exceptions.
OTOH, C++, the important language wasm currently supports, is not gonna benefit from making catch instruction catch traps; they would rather be likely to suffer from that. Wasm catch instructions are used for cleanup landing pads, such as ones for calling destructors, and those C++'s cleanup landing pads don't catch traps, which means we should insert bypassing code for traps in every cleanup landing pad. Cleanup landing pads are much more common than landing pads generated by C++'s catch clauses, and this may incur unnecessary code size increases. (One may argue we don't need to care because it's UB in C++ anyway, but then it will run random functions and cause even more traps, making debugging nearly impossible.)
Some people were concerned about debuggability if traps are not catchable; I don't think that will be a problem. If the embedder has stack trace support, users will be able to see stack traces when a program crashes after a trap just fine, provided that we have appropriate JS (or other embedder) API support.
This scheme will require some changes to the JS API, because traps should not be catchable even after they hit a JS frame. In the current JS API, if a trap hits a JS frame, it becomes a WebAssembly.RuntimeError. WebAssembly.RuntimeError contains a few more situations other than traps, I think it makes sense to make catch instruction not catch all WebAssembly.RuntimeErrors. This way traps can be handled consistently before and after they hit JS frames.
cc @titzer, in case he has more insights on support for languages like Java. (We talked a little on that at the CG meeting, but the it was more like a passing conversation, so)
I also think it makes sense not to have traps caught by wasm catch.
I think it makes sense to say that during unwinding a trap propagates into JS frames as a thrown RuntimeError, and unwinds through wasm frames as traps to today; and an exception thrown from wasm would propagate into JS frames as some JS reflection of the exception (if the event type is exported, it seems like you could even potentially get the thrown object out of it).
It's slightly unclear to me (as someone not accustomed to JS spec-ese) whether there could currently ever be a RuntimeError generated other than as the result of a wasm trap. If so, that would be weird if those 2 different kinds of events would be indistinguishable in JS.
It's slightly unclear to me (as someone not accustomed to JS spec-ese) whether there could currently ever be a RuntimeError generated other than as the result of a wasm trap. If so, that would be weird if those 2 different kinds of events would be indistinguishable in JS.
Activity
dschuff commentedon Apr 27, 2017
I think there is a meaningful difference between explicitly-raised exceptions (which I guess includes JS exceptions but ignoring that for now) and machine-type error traps (OOB, signature mismatch etc) and it's not clear that they should be catchable in the same way that user exceptions are. So obviously we can debate that in this issue (and we can design such a feature) but I think the first proposal should exclude them because the use cases are very different.
jfbastien commentedon Apr 27, 2017
Right, we've talked about having support for signals as well as exceptions. It would be interesting to define what we see in each bucket, and what other platforms have done. Specifically, I think the Windows SEH approach, its ups and downs, can inform our decision.
AndrewScheidecker commentedon May 4, 2017
One advantage the Windows SEH approach has over handling runtime traps with signals is that it avoids global state that determines how a trap will be handled. There isn't a good way with POSIX signals to handle a trap from just one thread.
A Windows SEH filter is effectively a stack-scoped signal handler: when an exception occurs, the SEH filter functions are called "on top" of the stack, so the information is still available to resume execution, or get a stack trace, or unwind to the handler. A C++ catch maps to a SEH filter that checks the exception information and returns a value that indicates the associated handler should be executed.
Thread-local signal handlers would provide the same functionality (could you throw from them?), but require some additional mechanism to compose (a stack of handlers, perhaps?).
My most preferred design would be very close to Windows SEH. My least preferred design would be something that unifies traps and exceptions, but doesn't allow resuming or inspecting the stack at the trapping code.
KarlSchimpf commentedon May 11, 2017
This proposal intentionally did not focus on what exceptions should be produced by WebAsembly. Rather, any such exception would need to be imported to be caught, since it is not one the module defines.
Hence, the framework needed to handle such exceptions, should they be added, is already there. We would not need to add any new concepts.
AndrewScheidecker commentedon May 15, 2017
If runtime-generated traps can be caught as exceptions, then there should be a mechanism to retry the trapping code. There should also be a way to handle a runtime trap by capturing a stack trace and other state information at the point of the trap for automatic bug reporting.
I think it's fine to start with exceptions as they are and add the ability to catch runtime traps later, but there's additional functionality required if the above cases are to be supported. Would that require changing any of base exception functionality?
I can imagine adding a
retry_throwoperator that can be used similarly to therethrowoperator. That seems like it is orthogonal to this proposal.A way to capture a stack trace is not specified by WebAssembly; without considering whether it should be, what would be needed from this proposal for an implementation to expose the trapping call stack to a runtime trap handler?
KarlSchimpf commentedon May 15, 2017
Good point. I hadn't thought of those issues. I agree that sometime in the future, we should consider runtime traps.
KarlSchimpf commentedon May 15, 2017
Marked as (future) enhancement to add handling of runtime traps.
jfbastien commentedon May 15, 2017
I don't think we want to outright declare this as a future feature. It's worth exploring having it in the first version as well, and if that's not viable then move to future.
rossberg commentedon May 15, 2017
@jfbastien, are you referring to reifying traps as exceptions, or supporting resumption? Both are separate features, and the latter is far more difficult and debatable than the former.
jfbastien commentedon May 15, 2017
Both, separately.
Agreed.
lukewagner commentedon May 17, 2017
At least in the future I've been imagining, the distinction between traps and exceptions is:
With this distinction, exception handling is single-pass unwind-as-you-go, unlike, e.g., itanium ABI's two-pass design. Also, it is an orthogonal question of how to define which trap handler to call (module-scoped, lexically-scoped, dynamically-scoped like SEH, etc).
Also, with this split, trap handlers are definitely the more advanced feature with the more narrow (but important, in the limit, if we want to allow all the fancy VM tricks) use case, so I think we should keep them separate from the exception handling MVP knowing they can be added later in a complementary way.
jfbastien commentedon May 17, 2017
That's the bit I'm not convinced about.
lukewagner commentedon May 17, 2017
Is there are particular scenario you're thinking about?
jfbastien commentedon May 17, 2017
Maybe I haven't thought about it enough. I'm not convinced this ins't a plausible scenario:
I'm just not convinced this isn't the case. As well as the above scenario, it could unfold as:
15 remaining items
rossberg commentedon Mar 10, 2019
@andrewackerman, there are implementation-level type errors and language-level (i.e., user) type errors. Traps signify the former, while the latter will typically have to be implemented by a compiler. I think it's best not to conflate the two. Even user-observable null checks should best be explicit in the code, to avoid as much overhead as possible around implementation-level checks.
Horcrux7 commentedon Mar 11, 2019
@rossberg If the compiler need to check to prevent runtime traps then it can be required to add some more instructions. For example in the complex range of the GC spec. I have not check this if this really required.
The more I think about it the better I like it. Each language can precisely prevent the traps that are defined in its language.
To finish this discussion this should be a topic in one of the next community/working group meetings.
binji commentedon Mar 11, 2019
Added to the March 19 agenda. Does that work?
aheejin commentedon Mar 11, 2019
I am OOO until 3/22, and in a different time zone, so I don't think I can attend it. Can we do it later?
aheejin commentedon Mar 11, 2019
I've been OOO (and will be for some more days) so I haven't fully caught up with the comments so far, but one thing I'd like to make sure is even if we decide to catch traps with
catch, that does NOT mean C++'scatchclause will catch them. See #1 (comment) for reasons why it's not possible.binji commentedon Mar 12, 2019
OK, no problem, let's postpone.
aheejin commentedon Oct 26, 2019
We discussed the topic in the in-person CG meeting in June. It's been a while, but I'd like to reopen the discussions and make decisions on this issue to proceed to Stage 2 for the EH proposal.
I'd like to propose that we don't catch traps with wasm
catchinstruction. After many discussions, I feel that traps have characteristics that more resemble signals than exceptions, meaning that 1. at least some of them should not be catchable (i.e., should result in abort) and 2. they usually don't need to be handled in local scopes like try-catch. I'm not saying the interface for handling traps should be necessarily similar to that of POSIX signals; what I mean is that it can be a separate proposal from the EH proposal if that functionality becomes necessary, in which traps can be handled in more broader scope, such as per function or per module.As I mentioned above, for a practical reason, we need a way to abort, to implement functions like C
abortor C++std::terminate. In JS embedding, they are currently implemented in JS as throwing a JS exception, which is catchable by wasmcatchinstruction. I'd say that we implement those functions using wasm traps, and make those traps uncatchable.I also think whether hardware traps should be handled like or converted to exceptions is the job of languages themselves. For example, Rust compiler inserts code that checks for divide by zero errors and throws exceptions. C++ treat them as UB and doesn't pay the performance cost. Java VM handles arithmetic errors using signal-handler-like routines and creates and throws Java exceptions.
OTOH, C++, the important language wasm currently supports, is not gonna benefit from making
catchinstruction catch traps; they would rather be likely to suffer from that. Wasmcatchinstructions are used for cleanup landing pads, such as ones for calling destructors, and those C++'s cleanup landing pads don't catch traps, which means we should insert bypassing code for traps in every cleanup landing pad. Cleanup landing pads are much more common than landing pads generated by C++'scatchclauses, and this may incur unnecessary code size increases. (One may argue we don't need to care because it's UB in C++ anyway, but then it will run random functions and cause even more traps, making debugging nearly impossible.)Some people were concerned about debuggability if traps are not catchable; I don't think that will be a problem. If the embedder has stack trace support, users will be able to see stack traces when a program crashes after a trap just fine, provided that we have appropriate JS (or other embedder) API support.
This scheme will require some changes to the JS API, because traps should not be catchable even after they hit a JS frame. In the current JS API, if a trap hits a JS frame, it becomes a
WebAssembly.RuntimeError.WebAssembly.RuntimeErrorcontains a few more situations other than traps, I think it makes sense to makecatchinstruction not catch allWebAssembly.RuntimeErrors. This way traps can be handled consistently before and after they hit JS frames.aheejin commentedon Oct 26, 2019
cc @titzer, in case he has more insights on support for languages like Java. (We talked a little on that at the CG meeting, but the it was more like a passing conversation, so)
dschuff commentedon Oct 31, 2019
I also think it makes sense not to have traps caught by wasm
catch.I think it makes sense to say that during unwinding a trap propagates into JS frames as a thrown
RuntimeError, and unwinds through wasm frames as traps to today; and an exception thrown from wasm would propagate into JS frames as some JS reflection of the exception (if the event type is exported, it seems like you could even potentially get the thrown object out of it).It's slightly unclear to me (as someone not accustomed to JS spec-ese) whether there could currently ever be a
RuntimeErrorgenerated other than as the result of a wasm trap. If so, that would be weird if those 2 different kinds of events would be indistinguishable in JS.aheejin commentedon Oct 31, 2019
@dschuff
I opened #89 to discuss this.
Add overview on traps and JS API
Add overview on traps and JS API (#93)
Add overview on traps and JS API (WebAssembly#93)
Add overview on traps and JS API (WebAssembly#93)