Skip to content
Snippets Groups Projects
  • Matteo Italia's avatar
    16774daa
    libgcc: fix SEH C++ rethrow semantics [PR113337] · 16774daa
    Matteo Italia authored
    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
    16774daa
    History
    libgcc: fix SEH C++ rethrow semantics [PR113337]
    Matteo Italia authored
    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