uncaught_exceptions.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright Andrey Semashev 2018 - 2020.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * https://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file uncaught_exceptions.hpp
  9. * \author Andrey Semashev
  10. * \date 2018-11-10
  11. *
  12. * \brief This header provides an `uncaught_exceptions` function implementation, which was introduced in C++17.
  13. *
  14. * The code in this file is based on the implementation by Evgeny Panasyuk:
  15. *
  16. */
  17. #ifndef BHO_CORE_UNCAUGHT_EXCEPTIONS_HPP_INCLUDED_
  18. #define BHO_CORE_UNCAUGHT_EXCEPTIONS_HPP_INCLUDED_
  19. #include <exception>
  20. #include <asio2/bho/config.hpp>
  21. #if defined(BHO_HAS_PRAGMA_ONCE)
  22. #pragma once
  23. #endif
  24. #if (defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411)
  25. #if defined(__APPLE__)
  26. #include <Availability.h>
  27. // Apple systems only support std::uncaught_exceptions starting with specific versions:
  28. // - Mac OS >= 10.12
  29. // - iOS >= 10.0
  30. // - tvOS >= 10.0
  31. // - watchOS >= 3.0
  32. // https://github.com/boostorg/core/issues/80
  33. #if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200) || \
  34. (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000)
  35. #define BHO_CORE_HAS_UNCAUGHT_EXCEPTIONS
  36. #endif
  37. #else
  38. #define BHO_CORE_HAS_UNCAUGHT_EXCEPTIONS
  39. #endif // defined(__APPLE__)
  40. // Visual Studio 14.0 supports N4152 std::uncaught_exceptions() but doesn't define __cpp_lib_uncaught_exceptions
  41. #elif (defined(_MSC_VER) && _MSC_VER >= 1900)
  42. #define BHO_CORE_HAS_UNCAUGHT_EXCEPTIONS
  43. #endif
  44. #if !defined(BHO_CORE_HAS_UNCAUGHT_EXCEPTIONS)
  45. // cxxabi.h availability macro
  46. #if defined(__has_include) && (!defined(BHO_GCC) || (__GNUC__ >= 5))
  47. # if __has_include(<cxxabi.h>)
  48. # define BHO_CORE_HAS_CXXABI_H
  49. # endif
  50. #elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
  51. # define BHO_CORE_HAS_CXXABI_H
  52. #endif
  53. #if defined(BHO_CORE_HAS_CXXABI_H)
  54. // MinGW GCC 4.4 seem to not work the same way the newer GCC versions do. As a result, __cxa_get_globals based implementation will always return 0.
  55. // Just disable it for now and fall back to std::uncaught_exception().
  56. // On AIX, xlclang++ does have cxxabi.h but doesn't have __cxa_get_globals (https://github.com/boostorg/core/issues/78).
  57. #if !( \
  58. (defined(__MINGW32__) && (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) < 405)) || \
  59. defined(__ibmxl__) \
  60. )
  61. #include <cxxabi.h>
  62. #include <cstring>
  63. #define BHO_CORE_HAS_CXA_GET_GLOBALS
  64. // At least on MinGW and Linux, only GCC since 4.7 declares __cxa_get_globals() in cxxabi.h. Older versions of GCC do not expose this function but it's there.
  65. // On OpenBSD, it seems, the declaration is also missing.
  66. // Note that at least on FreeBSD 11, cxxabi.h declares __cxa_get_globals with a different exception specification, so we can't declare the function unconditionally.
  67. // On Linux with clang and libc++ and on OS X, there is a version of cxxabi.h from libc++abi that doesn't declare __cxa_get_globals, but provides __cxa_uncaught_exceptions.
  68. // The function only appeared in version _LIBCPPABI_VERSION >= 1002 of the library. Unfortunately, there are linking errors about undefined reference to __cxa_uncaught_exceptions
  69. // on Ubuntu Trusty and OS X, so we avoid using it and forward-declare __cxa_get_globals instead.
  70. // On QNX SDP 7.0 (QCC 5.4.0), there are multiple cxxabi.h, one from glibcxx from gcc and another from libc++abi from LLVM. Which one is included will be determined by the qcc
  71. // command line arguments (-V and/or -Y; http://www.qnx.com/developers/docs/7.0.0/#com.qnx.doc.neutrino.utilities/topic/q/qcc.html). The LLVM libc++abi is missing the declaration
  72. // of __cxa_get_globals but it is also patched by QNX developers to not define _LIBCPPABI_VERSION. Older QNX SDP versions, up to and including 6.6, don't provide LLVM and libc++abi.
  73. // See https://github.com/boostorg/core/issues/59.
  74. #if !defined(__FreeBSD__) && \
  75. ( \
  76. (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) < 407) || \
  77. defined(__OpenBSD__) || \
  78. (defined(__QNXNTO__) && !defined(__GLIBCXX__) && !defined(__GLIBCPP__)) || \
  79. defined(_LIBCPPABI_VERSION) \
  80. )
  81. namespace __cxxabiv1 {
  82. struct __cxa_eh_globals;
  83. #if defined(__OpenBSD__)
  84. extern "C" __cxa_eh_globals* __cxa_get_globals();
  85. #else
  86. extern "C" __cxa_eh_globals* __cxa_get_globals() BHO_NOEXCEPT_OR_NOTHROW __attribute__((__const__));
  87. #endif
  88. } // namespace __cxxabiv1
  89. #endif
  90. #endif
  91. #endif // defined(BHO_CORE_HAS_CXXABI_H)
  92. #if defined(_MSC_VER) && _MSC_VER >= 1400
  93. #include <cstring>
  94. #define BHO_CORE_HAS_GETPTD
  95. namespace bho {
  96. namespace core {
  97. namespace detail {
  98. extern "C" void* _getptd();
  99. } // namespace detail
  100. } // namespace core
  101. } // namespace bho
  102. #endif // defined(_MSC_VER) && _MSC_VER >= 1400
  103. #endif // !defined(BHO_CORE_HAS_UNCAUGHT_EXCEPTIONS)
  104. #if !defined(BHO_CORE_HAS_UNCAUGHT_EXCEPTIONS) && !defined(BHO_CORE_HAS_CXA_GET_GLOBALS) && !defined(BHO_CORE_HAS_GETPTD)
  105. //! This macro is defined when `uncaught_exceptions` is not guaranteed to return values greater than 1 if multiple exceptions are pending
  106. #define BHO_CORE_UNCAUGHT_EXCEPTIONS_EMULATED
  107. #endif
  108. namespace bho {
  109. namespace core {
  110. //! Returns the number of currently pending exceptions
  111. inline unsigned int uncaught_exceptions() BHO_NOEXCEPT
  112. {
  113. #if defined(BHO_CORE_HAS_UNCAUGHT_EXCEPTIONS)
  114. // C++17 implementation
  115. return static_cast< unsigned int >(std::uncaught_exceptions());
  116. #elif defined(BHO_CORE_HAS_CXA_GET_GLOBALS)
  117. // Tested on {clang 3.2,GCC 3.5.6,GCC 4.1.2,GCC 4.4.6,GCC 4.4.7}x{x32,x64}
  118. unsigned int count;
  119. std::memcpy(&count, reinterpret_cast< const unsigned char* >(::abi::__cxa_get_globals()) + sizeof(void*), sizeof(count)); // __cxa_eh_globals::uncaughtExceptions, x32 offset - 0x4, x64 - 0x8
  120. return count;
  121. #elif defined(BHO_CORE_HAS_GETPTD)
  122. // MSVC specific. Tested on {MSVC2005SP1,MSVC2008SP1,MSVC2010SP1,MSVC2012}x{x32,x64}.
  123. unsigned int count;
  124. std::memcpy(&count, static_cast< const unsigned char* >(bho::core::detail::_getptd()) + (sizeof(void*) == 8u ? 0x100 : 0x90), sizeof(count)); // _tiddata::_ProcessingThrow, x32 offset - 0x90, x64 - 0x100
  125. return count;
  126. #else
  127. // Portable C++03 implementation. Does not allow to detect multiple nested exceptions.
  128. return static_cast< unsigned int >(std::uncaught_exception());
  129. #endif
  130. }
  131. } // namespace core
  132. } // namespace bho
  133. #undef BHO_CORE_HAS_CXXABI_H
  134. #undef BHO_CORE_HAS_CXA_GET_GLOBALS
  135. #undef BHO_CORE_HAS_UNCAUGHT_EXCEPTIONS
  136. #undef BHO_CORE_HAS_GETPTD
  137. #endif // BHO_CORE_UNCAUGHT_EXCEPTIONS_HPP_INCLUDED_