Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

What counts as an exception? #1

Closed

Description

@KarlSchimpf

Restoring lost issue:

@wibblymat

Should things like unreachable, out-of-bounds memory access, call_indirect with an invalid table index, etc., become catchable exceptions?

Activity

  1. dschuff commented on Apr 27, 2017

    @dschuff
    Member

    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.

  2. jfbastien commented on Apr 27, 2017

    @jfbastien
    Member

    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.

  3. AndrewScheidecker commented on May 4, 2017

    @AndrewScheidecker
    Contributor

    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.

  4. KarlSchimpf commented on May 11, 2017

    @KarlSchimpf
    ContributorAuthor

    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.

  5. AndrewScheidecker commented on May 15, 2017

    @AndrewScheidecker
    Contributor

    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?

  6. KarlSchimpf commented on May 15, 2017

    @KarlSchimpf
    ContributorAuthor

    Good point. I hadn't thought of those issues. I agree that sometime in the future, we should consider runtime traps.

  7. KarlSchimpf commented on May 15, 2017

    @KarlSchimpf
    ContributorAuthor

    Marked as (future) enhancement to add handling of runtime traps.

  8. jfbastien commented on May 15, 2017

    @jfbastien
    Member

    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.

  9. rossberg commented on May 15, 2017

    @rossberg
    Member

    @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.

  10. jfbastien commented on May 15, 2017

    @jfbastien
    Member

    @jfbastien, are you referring to reifying traps as exceptions, or supporting resumption?

    Both, separately.

    Both are separate features, and the latter is far more difficult and debatable than the former.

    Agreed.

  11. lukewagner commented on May 17, 2017

    @lukewagner
    Member

    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.

  12. jfbastien commented on May 17, 2017

    @jfbastien
    Member

    knowing they can be added later in a complementary way

    That's the bit I'm not convinced about.

  13. lukewagner commented on May 17, 2017

    @lukewagner
    Member

    Is there are particular scenario you're thinking about?

  14. jfbastien commented on May 17, 2017

    @jfbastien
    Member

    Is there are particular scenario you're thinking about?

    Maybe I haven't thought about it enough. I'm not convinced this ins't a plausible scenario:

    1. We standardize exceptions.
    2. We want traps.
    3. Traps are kinda different from exceptions, but not that much, and turn out to be incompatible in a weird way.
    4. We end up with two mechanisms that are almost, but not quite, the same.
    5. We're sad.

    I'm just not convinced this isn't the case. As well as the above scenario, it could unfold as:

    • We standardize both at the same time (either as one feature or as two), it works great.
    • We standardize one then the other, but they magically coexist well.
  15. 15 remaining items

  16. rossberg commented on Mar 10, 2019

    @rossberg
    Member

    @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.

  17. Horcrux7 commented on Mar 11, 2019

    @Horcrux7

    @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.

  18. binji commented on Mar 11, 2019

    @binji
    Member

    To finish this discussion this should be a topic in one of the next community/working group meetings.

    Added to the March 19 agenda. Does that work?

  19. aheejin commented on Mar 11, 2019

    @aheejin
    Member

    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?

  20. aheejin commented on Mar 11, 2019

    @aheejin
    Member

    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.

  21. binji commented on Mar 12, 2019

    @binji
    Member

    OK, no problem, let's postpone.

  22. aheejin commented on Oct 26, 2019

    @aheejin
    Member

    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.

  23. aheejin commented on Oct 26, 2019

    @aheejin
    Member

    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)

  24. dschuff commented on Oct 31, 2019

    @dschuff
    Member

    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.

  25. aheejin commented on Oct 31, 2019

    @aheejin
    Member

    @dschuff

    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.

    I opened #89 to discuss this.

  26. added a commit that references this issue on Dec 30, 2019
    485ce96
  27. added a commit that references this issue on Jan 10, 2020
    0297b94
  28. added a commit that references this issue on Jun 6, 2020
    5f09fc4
  29. added a commit that references this issue on Feb 23, 2021
    5cb3194
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions