public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libgcc/113337] Uncaught rethrown exceptions don't invoke std::terminate if SEH-based unwinding is used
Date: Tue, 06 Feb 2024 08:24:09 +0000	[thread overview]
Message-ID: <bug-113337-4-NzsdKJ0mhH@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-113337-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113337

--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Yong <jyong@gcc.gnu.org>:

https://gcc.gnu.org/g:16774daa597f7633ae2f64efef20cad744b877b9

commit r14-8819-g16774daa597f7633ae2f64efef20cad744b877b9
Author: Matteo Italia <matteo@mitalia.net>
Date:   Wed Jan 17 12:51:44 2024 +0100

    libgcc: fix SEH C++ rethrow semantics [PR113337]

    SEH _Unwind_Resume_or_Rethrow invokes abort directly if
    _Unwind_RaiseException doesn't manage to find a handler for the rethrown
    exception; this is incorrect, as in this case std::terminate should be
    invoked, allowing an application-provided terminate handler to handle
    the situation instead of straight crashing the application through
    abort.

    The bug can be demonstrated with this simple test case:
    ===
    static void custom_terminate_handler() {
        fprintf(stderr, "custom_terminate_handler invoked\n");
        std::exit(1);
    }

    int main(int argc, char *argv[]) {
        std::set_terminate(&custom_terminate_handler);
        if (argc < 2) return 1;
        const char *mode = argv[1];
        fprintf(stderr, "%s\n", mode);
        if (strcmp(mode, "throw") == 0) {
            throw std::exception();
        } else if (strcmp(mode, "rethrow") == 0) {
            try {
                throw std::exception();
            } catch (...) {
                throw;
            }
        } else {
            return 1;
        }
        return 0;
    }
    ===

    On all gcc builds with non-SEH exceptions, this will print
    "custom_terminate_handler invoked" both if launched as ./a.out throw or
    as ./a.out rethrow, on SEH builds instead if will work as expected only
    with ./a.exe throw, but will crash with the "built-in" abort message
    with ./a.exe rethrow.

    This patch fixes the problem, forwarding back the error code to the
    caller (__cxa_rethrow), that calls std::terminate if
    _Unwind_Resume_or_Rethrow returns.

    The change makes the code path coherent with SEH _Unwind_RaiseException,
    and with the generic _Unwind_Resume_or_Rethrow from libgcc/unwind.inc
    (used for SjLj and Dw2 exception backend).

    libgcc/ChangeLog:

            PR libgcc/113337

            * unwind-seh.c (_Unwind_Resume_or_Rethrow): forward
            _Unwind_RaiseException return code back to caller instead of
            calling abort, allowing __cxa_rethrow to invoke std::terminate
            in case of uncaught rethrown exception

  parent reply	other threads:[~2024-02-06  8:24 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-11 17:00 [Bug libgcc/113337] New: Rethrown uncaught " matteo at mitalia dot net
2024-01-15 15:17 ` [Bug libgcc/113337] Uncaught rethrown " matteo at mitalia dot net
2024-01-15 15:19 ` matteo at mitalia dot net
2024-02-06  8:24 ` cvs-commit at gcc dot gnu.org [this message]
2024-02-19 14:10 ` jyong at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-113337-4-NzsdKJ0mhH@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).