Path: blob/main/system/lib/libcxxabi/src/cxa_handlers.cpp
6175 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//7// This file implements the functionality associated with the terminate_handler,8// unexpected_handler, and new_handler.9//===----------------------------------------------------------------------===//1011#include <stdexcept>12#include <new>13#include <exception>14#include "abort_message.h"15#include "cxxabi.h"16#include "cxa_handlers.h"17#include "cxa_exception.h"18#include "private_typeinfo.h"19#include "include/atomic_support.h" // from libc++2021namespace std22{2324unexpected_handler25get_unexpected() noexcept26{27return __libcpp_atomic_load(&__cxa_unexpected_handler, _AO_Acquire);28}2930void31__unexpected(unexpected_handler func)32{33func();34// unexpected handler should not return35__abort_message("unexpected_handler unexpectedly returned");36}3738__attribute__((noreturn))39void40unexpected()41{42__unexpected(get_unexpected());43}4445terminate_handler46get_terminate() noexcept47{48return __libcpp_atomic_load(&__cxa_terminate_handler, _AO_Acquire);49}5051void52__terminate(terminate_handler func) noexcept53{54#ifndef _LIBCXXABI_NO_EXCEPTIONS55try56{57#endif // _LIBCXXABI_NO_EXCEPTIONS58func();59// handler should not return60__abort_message("terminate_handler unexpectedly returned");61#ifndef _LIBCXXABI_NO_EXCEPTIONS62}63catch (...)64{65// handler should not throw exception66__abort_message("terminate_handler unexpectedly threw an exception");67}68#endif // _LIBCXXABI_NO_EXCEPTIONS69}7071__attribute__((noreturn))72void73terminate() noexcept74{75#if !defined(_LIBCXXABI_NO_EXCEPTIONS) && !defined(__EMSCRIPTEN_EXCEPTIONS__)76// If there might be an uncaught exception77using namespace __cxxabiv1;78__cxa_eh_globals* globals = __cxa_get_globals_fast();79if (globals)80{81__cxa_exception* exception_header = globals->caughtExceptions;82if (exception_header)83{84_Unwind_Exception* unwind_exception =85reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1;86if (__isOurExceptionClass(unwind_exception))87__terminate(exception_header->terminateHandler);88}89}90#endif91__terminate(get_terminate());92}9394new_handler95get_new_handler() noexcept96{97return __libcpp_atomic_load(&__cxa_new_handler, _AO_Acquire);98}99100} // std101102103