Skip to content

Catch unwinding panics across FFI boundary  #1061

Closed

Description

@bdbai

Currently, generated interface implementation (including callback consumption) does not catch unwinding panics. By default, when a panic occurs in user code, the thread starts unwinding all the way down to system dlls. Since unwinding across FFI boundary is undefined behavior, user code invocations should be wrapped in catch_unwind.

Activity

  1. kennykerr commented on Aug 18, 2021

    @kennykerr
    Collaborator

    Yeah, I'm still trying to figure out how best to handle this. On Windows, we generally have an abort/terminate function that immediately terminates the process (e.g. TerminateProcess) without unwinding in order to collect reliable crash dumps. This is distinct from "exceptions" that may unwind the stack, and we certainly don't want to allow exceptions to unwind as that is not reliable across ABI boundaries in C++ either. So ideally, we could ensure that panic does not unwind but if I don't know whether that is practical.

    And if catch_unwind is used, the question is what to do with the resulting error. I don't want it to propagate but neither do I want to simply swallow the error and lose that context.

  2. deleted a comment from on Aug 18, 2021
  3. bdbai commented on Aug 18, 2021

    @bdbai
    ContributorAuthor

    I don't want it to propagate

    May I know the reason not to do so?

  4. kennykerr commented on Aug 18, 2021

    @kennykerr
    Collaborator

    Because the ABI semantics don't allow for it. Errors must be reported via return codes. Unwinding is also not compatible across compilers and languages.

  5. bdbai commented on Aug 18, 2021

    @bdbai
    ContributorAuthor

    Can we mimic the behavior when an exception is thrown in cppwinrt, like reporting the error via RoOriginateLanguageException?

  6. RobJellinghaus commented on Oct 11, 2021

    @RobJellinghaus

    FWIW there is an RFC to reduce undefined behavior when unwinding across FFI boundaries, but it looks to be fairly static in the last year or so: https://github.com/rust-lang/rfcs/blob/master/text/2945-c-unwind-abi.md

  7. kennykerr commented on Mar 6, 2023

    @kennykerr
    Collaborator

    Just looking again at this old issue. I think I'm going to leave this up to the implementor. I don't see what the implement macro can do beyond simply aborting in such cases, in which case it would be more efficient to do that declaratively:

    https://doc.rust-lang.org/book/ch09-01-unrecoverable-errors-with-panic.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions