throw_gcc.h (4498B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: sw=2 ts=4 et : 3 */ 4 /* This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 7 8 #ifndef mozilla_throw_gcc_h 9 #define mozilla_throw_gcc_h 10 11 #if !defined(_LIBCPP_VERSION) || _LIBCPP_VERSION < 14000 12 13 # include "mozilla/Attributes.h" 14 15 # include <stdio.h> // snprintf 16 # include <string.h> // strerror 17 18 // For gcc, we define these inline to abort so that we're absolutely 19 // certain that (i) no exceptions are thrown from Gecko; (ii) these 20 // errors are always terminal and caught by breakpad. 21 22 # include "mozilla/mozalloc_abort.h" 23 24 // libc++ 4.0.0 and higher use C++11 [[noreturn]] attributes for the functions 25 // below, and since clang does not allow mixing __attribute__((noreturn)) and 26 // [[noreturn]], we have to explicitly use the latter here. See bug 1329520. 27 # if defined(__clang__) 28 # if __has_feature(cxx_attributes) && defined(_LIBCPP_VERSION) && \ 29 _LIBCPP_VERSION >= 4000 30 # define MOZ_THROW_NORETURN [[noreturn]] 31 # endif 32 # endif 33 # ifndef MOZ_THROW_NORETURN 34 # define MOZ_THROW_NORETURN __attribute__((__noreturn__)) 35 # endif 36 37 // MinGW doesn't appropriately inline these functions in debug builds, 38 // so we need to do some extra coercion for it to do so. Bug 1332747 39 # ifdef __MINGW32__ 40 # define MOZ_THROW_INLINE MOZ_ALWAYS_INLINE_EVEN_DEBUG 41 # define MOZ_THROW_EXPORT 42 # else 43 # define MOZ_THROW_INLINE MOZ_ALWAYS_INLINE 44 # define MOZ_THROW_EXPORT MOZ_EXPORT 45 # endif 46 47 namespace std { 48 49 // NB: user code is not supposed to touch the std:: namespace. We're 50 // doing this after careful review because we want to define our own 51 // exception throwing semantics. Don't try this at home! 52 53 MOZ_THROW_NORETURN MOZ_THROW_EXPORT MOZ_THROW_INLINE void __throw_bad_exception( 54 void) { 55 mozalloc_abort("fatal: STL threw bad_exception"); 56 } 57 58 MOZ_THROW_NORETURN MOZ_THROW_EXPORT MOZ_THROW_INLINE void __throw_bad_alloc( 59 void) { 60 mozalloc_abort("fatal: STL threw bad_alloc"); 61 } 62 63 MOZ_THROW_NORETURN MOZ_THROW_EXPORT MOZ_THROW_INLINE void __throw_bad_cast( 64 void) { 65 mozalloc_abort("fatal: STL threw bad_cast"); 66 } 67 68 MOZ_THROW_NORETURN MOZ_THROW_EXPORT MOZ_THROW_INLINE void __throw_bad_typeid( 69 void) { 70 mozalloc_abort("fatal: STL threw bad_typeid"); 71 } 72 73 // used by <functional> 74 MOZ_THROW_NORETURN MOZ_THROW_EXPORT MOZ_THROW_INLINE void 75 __throw_bad_function_call(void) { 76 mozalloc_abort("fatal: STL threw bad_function_call"); 77 } 78 79 MOZ_THROW_NORETURN MOZ_THROW_EXPORT MOZ_THROW_INLINE void __throw_logic_error( 80 const char* msg) { 81 mozalloc_abort(msg); 82 } 83 84 MOZ_THROW_NORETURN MOZ_THROW_EXPORT MOZ_THROW_INLINE void __throw_domain_error( 85 const char* msg) { 86 mozalloc_abort(msg); 87 } 88 89 MOZ_THROW_NORETURN MOZ_THROW_EXPORT MOZ_THROW_INLINE void 90 __throw_invalid_argument(const char* msg) { 91 mozalloc_abort(msg); 92 } 93 94 MOZ_THROW_NORETURN MOZ_THROW_EXPORT MOZ_THROW_INLINE void __throw_length_error( 95 const char* msg) { 96 mozalloc_abort(msg); 97 } 98 99 MOZ_THROW_NORETURN MOZ_THROW_EXPORT MOZ_THROW_INLINE void __throw_out_of_range( 100 const char* msg) { 101 mozalloc_abort(msg); 102 } 103 104 MOZ_THROW_NORETURN MOZ_THROW_EXPORT MOZ_THROW_INLINE void __throw_runtime_error( 105 const char* msg) { 106 mozalloc_abort(msg); 107 } 108 109 MOZ_THROW_NORETURN MOZ_THROW_EXPORT MOZ_THROW_INLINE void __throw_range_error( 110 const char* msg) { 111 mozalloc_abort(msg); 112 } 113 114 MOZ_THROW_NORETURN MOZ_THROW_EXPORT MOZ_THROW_INLINE void 115 __throw_overflow_error(const char* msg) { 116 mozalloc_abort(msg); 117 } 118 119 MOZ_THROW_NORETURN MOZ_THROW_EXPORT MOZ_THROW_INLINE void 120 __throw_underflow_error(const char* msg) { 121 mozalloc_abort(msg); 122 } 123 124 MOZ_THROW_NORETURN MOZ_THROW_EXPORT MOZ_THROW_INLINE void __throw_ios_failure( 125 const char* msg) { 126 mozalloc_abort(msg); 127 } 128 129 MOZ_THROW_NORETURN MOZ_THROW_EXPORT MOZ_THROW_INLINE void __throw_system_error( 130 int err) { 131 char error[128]; 132 snprintf(error, sizeof(error) - 1, "fatal: STL threw system_error: %s (%d)", 133 strerror(err), err); 134 mozalloc_abort(error); 135 } 136 137 MOZ_THROW_NORETURN MOZ_EXPORT MOZ_ALWAYS_INLINE void __throw_regex_error( 138 int err) { 139 char error[128]; 140 snprintf(error, sizeof(error) - 1, "fatal: STL threw regex_error: %s (%d)", 141 strerror(err), err); 142 mozalloc_abort(error); 143 } 144 145 } // namespace std 146 147 # undef MOZ_THROW_NORETURN 148 # undef MOZ_THROW_INLINE 149 150 #endif 151 152 #endif // mozilla_throw_gcc_h