| 1 |
// Methods for Exception Support for -*- C++ -*- |
| 2 |
|
| 3 |
// Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// Under Section 7 of GPL version 3, you are granted additional |
| 17 |
// permissions described in the GCC Runtime Library Exception, version |
| 18 |
// 3.1, as published by the Free Software Foundation. |
| 19 |
|
| 20 |
// You should have received a copy of the GNU General Public License and |
| 21 |
// a copy of the GCC Runtime Library Exception along with this program; |
| 22 |
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 |
// <http://www.gnu.org/licenses/>. |
| 24 |
|
| 25 |
// |
| 26 |
// ISO C++ 14882: 19.1 Exception classes |
| 27 |
// |
| 28 |
|
| 29 |
// Enable hooks for support for the Transactional Memory TS (N4514). |
| 30 |
#define _GLIBCXX_TM_TS_INTERNAL |
| 31 |
void |
| 32 |
_txnal_cow_string_C1_for_exceptions(void* that, const char* s, void* exc); |
| 33 |
const char* |
| 34 |
_txnal_cow_string_c_str(const void* that); |
| 35 |
void |
| 36 |
_txnal_cow_string_D1(void* that); |
| 37 |
void |
| 38 |
_txnal_cow_string_D1_commit(void* that); |
| 39 |
void* |
| 40 |
_txnal_logic_error_get_msg(void* e); |
| 41 |
void* |
| 42 |
_txnal_runtime_error_get_msg(void* e); |
| 43 |
|
| 44 |
// All exception classes still use the classic COW std::string. |
| 45 |
#define _GLIBCXX_USE_CXX11_ABI 0 |
| 46 |
#define _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS 1 |
| 47 |
#define __cow_string __cow_stringxxx |
| 48 |
#include <stdexcept> |
| 49 |
#undef __cow_string |
| 50 |
|
| 51 |
namespace std _GLIBCXX_VISIBILITY(default) |
| 52 |
{ |
| 53 |
_GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 54 |
|
| 55 |
// Copy/move constructors and assignment operators defined using COW string. |
| 56 |
// These operations are noexcept even though copying a COW string is not, |
| 57 |
// but we know that the string member in an exception has not been "leaked" |
| 58 |
// so copying is a simple reference count increment. |
| 59 |
|
| 60 |
logic_error::logic_error(const logic_error& e) noexcept |
| 61 |
: exception(e), _M_msg(e._M_msg) { } |
| 62 |
|
| 63 |
logic_error& logic_error::operator=(const logic_error& e) noexcept |
| 64 |
{ _M_msg = e._M_msg; return *this; } |
| 65 |
|
| 66 |
logic_error::logic_error(logic_error&& e) noexcept = default; |
| 67 |
|
| 68 |
logic_error& |
| 69 |
logic_error::operator=(logic_error&& e) noexcept = default; |
| 70 |
|
| 71 |
runtime_error::runtime_error(const runtime_error& e) noexcept |
| 72 |
: exception(e), _M_msg(e._M_msg) { } |
| 73 |
|
| 74 |
runtime_error& |
| 75 |
runtime_error::operator=(const runtime_error& e) noexcept |
| 76 |
{ _M_msg = e._M_msg; return *this; } |
| 77 |
|
| 78 |
runtime_error::runtime_error(runtime_error&& e) noexcept = default; |
| 79 |
|
| 80 |
runtime_error& |
| 81 |
runtime_error::operator=(runtime_error&& e) noexcept = default; |
| 82 |
|
| 83 |
// New C++11 constructors: |
| 84 |
|
| 85 |
logic_error::logic_error(const char* __arg) |
| 86 |
: exception(), _M_msg(__arg) { } |
| 87 |
|
| 88 |
domain_error::domain_error(const char* __arg) |
| 89 |
: logic_error(__arg) { } |
| 90 |
|
| 91 |
invalid_argument::invalid_argument(const char* __arg) |
| 92 |
: logic_error(__arg) { } |
| 93 |
|
| 94 |
length_error::length_error(const char* __arg) |
| 95 |
: logic_error(__arg) { } |
| 96 |
|
| 97 |
out_of_range::out_of_range(const char* __arg) |
| 98 |
: logic_error(__arg) { } |
| 99 |
|
| 100 |
runtime_error::runtime_error(const char* __arg) |
| 101 |
: exception(), _M_msg(__arg) { } |
| 102 |
|
| 103 |
range_error::range_error(const char* __arg) |
| 104 |
: runtime_error(__arg) { } |
| 105 |
|
| 106 |
overflow_error::overflow_error(const char* __arg) |
| 107 |
: runtime_error(__arg) { } |
| 108 |
|
| 109 |
underflow_error::underflow_error(const char* __arg) |
| 110 |
: runtime_error(__arg) { } |
| 111 |
|
| 112 |
#if _GLIBCXX_USE_DUAL_ABI |
| 113 |
// Converting constructor from COW std::string to SSO string. |
| 114 |
__sso_string::__sso_string(const string& s) |
| 115 |
: __sso_string(s.c_str(), s.length()) { } |
| 116 |
|
| 117 |
// Redefine __cow_string so that we can define and export its members |
| 118 |
// in terms of the COW std::string. |
| 119 |
struct __cow_string |
| 120 |
{ |
| 121 |
union { |
| 122 |
const char* _M_p; |
| 123 |
char _M_bytes[sizeof(_M_p)]; |
| 124 |
std::string _M_str; |
| 125 |
}; |
| 126 |
|
| 127 |
__cow_string(); |
| 128 |
__cow_string(const std::string& s); |
| 129 |
__cow_string(const char*, size_t n); |
| 130 |
__cow_string(const __cow_string&) noexcept; |
| 131 |
__cow_string& operator=(const __cow_string&) noexcept; |
| 132 |
~__cow_string(); |
| 133 |
__cow_string(__cow_string&&) noexcept; |
| 134 |
__cow_string& operator=(__cow_string&&) noexcept; |
| 135 |
}; |
| 136 |
|
| 137 |
__cow_string::__cow_string() : _M_str() { } |
| 138 |
|
| 139 |
__cow_string::__cow_string(const std::string& s) : _M_str(s) { } |
| 140 |
|
| 141 |
__cow_string::__cow_string(const char* s, size_t n) : _M_str(s, n) { } |
| 142 |
|
| 143 |
__cow_string::__cow_string(const __cow_string& s) noexcept |
| 144 |
: _M_str(s._M_str) { } |
| 145 |
|
| 146 |
__cow_string& |
| 147 |
__cow_string::operator=(const __cow_string& s) noexcept |
| 148 |
{ |
| 149 |
_M_str = s._M_str; |
| 150 |
return *this; |
| 151 |
} |
| 152 |
|
| 153 |
__cow_string::~__cow_string() { _M_str.~basic_string(); } |
| 154 |
|
| 155 |
__cow_string::__cow_string(__cow_string&& s) noexcept |
| 156 |
: _M_str(std::move(s._M_str)) { } |
| 157 |
|
| 158 |
__cow_string& |
| 159 |
__cow_string::operator=(__cow_string&& s) noexcept |
| 160 |
{ |
| 161 |
_M_str = std::move(s._M_str); |
| 162 |
return *this; |
| 163 |
} |
| 164 |
|
| 165 |
static_assert(sizeof(__cow_string) == sizeof(std::string), |
| 166 |
"sizeof(std::string) has changed"); |
| 167 |
static_assert(alignof(__cow_string) == alignof(std::string), |
| 168 |
"alignof(std::string) has changed"); |
| 169 |
#endif |
| 170 |
|
| 171 |
_GLIBCXX_END_NAMESPACE_VERSION |
| 172 |
} // namespace |
| 173 |
|
| 174 |
// Support for the Transactional Memory TS (N4514). |
| 175 |
// |
| 176 |
// logic_error and runtime_error both carry a message in the form of a COW |
| 177 |
// string. This COW string is never made visible to users of the exception |
| 178 |
// because what() returns a C string. The COW string can be constructed as |
| 179 |
// either a copy of a COW string of another logic_error/runtime_error, or |
| 180 |
// using a C string or SSO string; thus, the COW string's _Rep is only |
| 181 |
// accessed by logic_error operations. We control all txnal clones of those |
| 182 |
// operations and thus can ensure that _Rep is never accessed transactionally. |
| 183 |
// Furthermore, _Rep will always have been allocated or deallocated via |
| 184 |
// global new or delete, so nontransactional writes we do to _Rep cannot |
| 185 |
// interfere with transactional accesses. |
| 186 |
|
| 187 |
// We depend on having support for referencing functions declared weak that |
| 188 |
// are not defined by us. Without such support, the exceptions will not be |
| 189 |
// declared transaction-safe, so we just don't provide transactional clones |
| 190 |
// in this case. |
| 191 |
#if _GLIBCXX_USE_WEAK_REF |
| 192 |
#ifdef _GLIBCXX_USE_C99_STDINT |
| 193 |
|
| 194 |
#include <stdint.h> |
| 195 |
|
| 196 |
using std::size_t; |
| 197 |
|
| 198 |
extern "C" { |
| 199 |
|
| 200 |
#ifndef _GLIBCXX_MANGLE_SIZE_T |
| 201 |
#error Mangled name of size_t type not defined. |
| 202 |
#endif |
| 203 |
#define CONCAT1(x,y) x##y |
| 204 |
#define CONCAT(x,y) CONCAT1(x,y) |
| 205 |
#define _ZGTtnaX CONCAT(_ZGTtna,_GLIBCXX_MANGLE_SIZE_T) |
| 206 |
|
| 207 |
#ifdef __i386__ |
| 208 |
/* Only for 32-bit x86. */ |
| 209 |
# define ITM_REGPARM __attribute__((regparm(2))) |
| 210 |
#else |
| 211 |
# define ITM_REGPARM |
| 212 |
#endif |
| 213 |
|
| 214 |
// Declare all libitm symbols we rely on, but make them weak so that we do |
| 215 |
// not depend on libitm. |
| 216 |
extern void* _ZGTtnaX (size_t sz) __attribute__((weak)); |
| 217 |
extern void _ZGTtdlPv (void* ptr) __attribute__((weak)); |
| 218 |
extern uint8_t _ITM_RU1(const uint8_t *p) |
| 219 |
ITM_REGPARM __attribute__((weak)); |
| 220 |
extern uint16_t _ITM_RU2(const uint16_t *p) |
| 221 |
ITM_REGPARM __attribute__((weak)); |
| 222 |
extern uint32_t _ITM_RU4(const uint32_t *p) |
| 223 |
ITM_REGPARM __attribute__((weak)); |
| 224 |
extern uint64_t _ITM_RU8(const uint64_t *p) |
| 225 |
ITM_REGPARM __attribute__((weak)); |
| 226 |
extern void _ITM_memcpyRtWn(void *, const void *, size_t) |
| 227 |
ITM_REGPARM __attribute__((weak)); |
| 228 |
extern void _ITM_memcpyRnWt(void *, const void *, size_t) |
| 229 |
ITM_REGPARM __attribute__((weak)); |
| 230 |
extern void _ITM_addUserCommitAction(void (*)(void *), uint64_t, void *) |
| 231 |
ITM_REGPARM __attribute__((weak)); |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
// A transactional version of basic_string::basic_string(const char *s) |
| 236 |
// that also notifies the TM runtime about allocations belonging to this |
| 237 |
// exception. |
| 238 |
void |
| 239 |
_txnal_cow_string_C1_for_exceptions(void* that, const char* s, |
| 240 |
void *exc __attribute__((unused))) |
| 241 |
{ |
| 242 |
typedef std::basic_string<char> bs_type; |
| 243 |
bs_type *bs = (bs_type*) that; |
| 244 |
|
| 245 |
// First, do a transactional strlen, but including the trailing zero. |
| 246 |
bs_type::size_type len = 1; |
| 247 |
for (const char *ss = s; _ITM_RU1((const uint8_t*) ss) != 0; ss++, len++); |
| 248 |
|
| 249 |
|
| 250 |
// Allocate memory for the string and the refcount. We use the |
| 251 |
// transactional clone of global new[]; if this throws, it will do so in a |
| 252 |
// transaction-compatible way. |
| 253 |
// The allocation belongs to this exception, so tell the runtime about it. |
| 254 |
// TODO Once this is supported, link the following allocation to this |
| 255 |
// exception: void *prev = _ITM_setAssociatedException(exc); |
| 256 |
bs_type::_Rep *rep; |
| 257 |
__try |
| 258 |
{ |
| 259 |
rep = (bs_type::_Rep*) _ZGTtnaX (len + sizeof (bs_type::_Rep)); |
| 260 |
} |
| 261 |
__catch (...) |
| 262 |
{ |
| 263 |
// Pop the association with this exception. |
| 264 |
// TODO Once this is supported, link the following allocation to this |
| 265 |
// exception: _ITM_setAssociatedException(prev); |
| 266 |
// We do not need to instrument a rethrow. |
| 267 |
__throw_exception_again; |
| 268 |
} |
| 269 |
// Pop the association with this exception. |
| 270 |
// TODO Once this is supported, link the following allocation to this |
| 271 |
// exception: _ITM_setAssociatedException(prev); |
| 272 |
|
| 273 |
// Now initialize the rest of the string and copy the C string. The memory |
| 274 |
// will be freshly allocated, so nontransactional accesses are sufficient, |
| 275 |
// including the writes when copying the string (see above). |
| 276 |
rep->_M_set_sharable(); |
| 277 |
rep->_M_length = rep->_M_capacity = len - 1; |
| 278 |
_ITM_memcpyRtWn(rep->_M_refdata(), s, len); |
| 279 |
new (&bs->_M_dataplus) bs_type::_Alloc_hider(rep->_M_refdata(), |
| 280 |
bs_type::allocator_type()); |
| 281 |
} |
| 282 |
|
| 283 |
static void* txnal_read_ptr(void* const * ptr) |
| 284 |
{ |
| 285 |
static_assert(sizeof(uint64_t) == sizeof(void*) |
| 286 |
|| sizeof(uint32_t) == sizeof(void*) |
| 287 |
|| sizeof(uint16_t) == sizeof(void*), |
| 288 |
"Pointers must be 16 bits, 32 bits or 64 bits wide"); |
| 289 |
#if __UINTPTR_MAX__ == __UINT64_MAX__ |
| 290 |
return (void*)_ITM_RU8((const uint64_t*)ptr); |
| 291 |
#elif __UINTPTR_MAX__ == __UINT32_MAX__ |
| 292 |
return (void*)_ITM_RU4((const uint32_t*)ptr); |
| 293 |
#else |
| 294 |
return (void*)_ITM_RU2((const uint16_t*)ptr); |
| 295 |
#endif |
| 296 |
} |
| 297 |
|
| 298 |
// We must access the data pointer in the COW string transactionally because |
| 299 |
// another transaction can delete the string and reuse the memory. |
| 300 |
const char* |
| 301 |
_txnal_cow_string_c_str(const void* that) |
| 302 |
{ |
| 303 |
typedef std::basic_string<char> bs_type; |
| 304 |
const bs_type *bs = (const bs_type*) that; |
| 305 |
|
| 306 |
return (const char*) txnal_read_ptr((void**)&bs->_M_dataplus._M_p); |
| 307 |
} |
| 308 |
|
| 309 |
#if _GLIBCXX_USE_DUAL_ABI |
| 310 |
const char* |
| 311 |
_txnal_sso_string_c_str(const void* that) |
| 312 |
{ |
| 313 |
return (const char*) txnal_read_ptr( |
| 314 |
(void* const*)const_cast<char* const*>( |
| 315 |
&((const std::__sso_string*) that)->_M_s._M_p)); |
| 316 |
} |
| 317 |
#endif |
| 318 |
|
| 319 |
void |
| 320 |
_txnal_cow_string_D1_commit(void* data) |
| 321 |
{ |
| 322 |
typedef std::basic_string<char> bs_type; |
| 323 |
bs_type::_Rep *rep = (bs_type::_Rep*) data; |
| 324 |
rep->_M_dispose(bs_type::allocator_type()); |
| 325 |
} |
| 326 |
|
| 327 |
void |
| 328 |
_txnal_cow_string_D1(void* that) |
| 329 |
{ |
| 330 |
typedef std::basic_string<char> bs_type; |
| 331 |
bs_type::_Rep *rep = reinterpret_cast<bs_type::_Rep*>( |
| 332 |
const_cast<char*>(_txnal_cow_string_c_str(that))) - 1; |
| 333 |
|
| 334 |
// The string can be shared, in which case we would need to decrement the |
| 335 |
// reference count. We cannot undo that because we might lose the string |
| 336 |
// otherwise. Therefore, we register a commit action that will dispose of |
| 337 |
// the string's _Rep. |
| 338 |
enum {_ITM_noTransactionId = 1}; |
| 339 |
_ITM_addUserCommitAction(_txnal_cow_string_D1_commit, _ITM_noTransactionId, |
| 340 |
rep); |
| 341 |
} |
| 342 |
|
| 343 |
void* |
| 344 |
_txnal_logic_error_get_msg(void* e) |
| 345 |
{ |
| 346 |
std::logic_error* le = (std::logic_error*) e; |
| 347 |
return &le->_M_msg; |
| 348 |
} |
| 349 |
|
| 350 |
void* |
| 351 |
_txnal_runtime_error_get_msg(void* e) |
| 352 |
{ |
| 353 |
std::runtime_error* le = (std::runtime_error*) e; |
| 354 |
return &le->_M_msg; |
| 355 |
} |
| 356 |
|
| 357 |
// The constructors are only declared transaction-safe if the C++11 ABI is |
| 358 |
// used for std::string and the exception classes use a COW string internally. |
| 359 |
// A user must not call these constructors otherwise; if they do, it will |
| 360 |
// result in undefined behavior, which is in this case not initializing this |
| 361 |
// string. |
| 362 |
#if _GLIBCXX_USE_DUAL_ABI |
| 363 |
#define CTORS_FROM_SSOSTRING(NAME, CLASS, BASE) \ |
| 364 |
void \ |
| 365 |
_ZGTtNSt##NAME##C1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE( \ |
| 366 |
CLASS* that, const std::__sso_string& s) \ |
| 367 |
{ \ |
| 368 |
CLASS e(""); \ |
| 369 |
_ITM_memcpyRnWt(that, &e, sizeof(CLASS)); \ |
| 370 |
/* Get the C string from the SSO string. */ \ |
| 371 |
_txnal_cow_string_C1_for_exceptions(_txnal_##BASE##_get_msg(that), \ |
| 372 |
_txnal_sso_string_c_str(&s), that); \ |
| 373 |
} \ |
| 374 |
void \ |
| 375 |
_ZGTtNSt##NAME##C2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE( \ |
| 376 |
CLASS*, const std::__sso_string&) __attribute__((alias \ |
| 377 |
("_ZGTtNSt" #NAME \ |
| 378 |
"C1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"))); |
| 379 |
#else |
| 380 |
#define CTORS_FROM_SSOSTRING(NAME, CLASS, BASE) |
| 381 |
#endif |
| 382 |
|
| 383 |
// This macro defines transaction constructors and destructors for a specific |
| 384 |
// exception class. NAME is the variable part of the mangled name, CLASS is |
| 385 |
// the class name, and BASE must be logic_error or runtime_error (which is |
| 386 |
// then used to call the proper friend function that can return a pointer to |
| 387 |
// the _M_msg member declared by the given (base) class). |
| 388 |
#define CTORDTOR(NAME, CLASS, BASE) \ |
| 389 |
void \ |
| 390 |
_ZGTtNSt##NAME##C1EPKc (CLASS* that, const char* s) \ |
| 391 |
{ \ |
| 392 |
/* This will use the singleton _Rep for an empty string and just \ |
| 393 |
point to it instead of allocating memory. Thus, we can use it as \ |
| 394 |
source, copy it into the object we are constructing, and then \ |
| 395 |
construct the COW string in the latter manually. Note that the \ |
| 396 |
exception classes will not be declared transaction_safe if the \ |
| 397 |
shared empty _Rep is disabled with --enable-fully-dynamic-string \ |
| 398 |
(in which case _GLIBCXX_FULLY_DYNAMIC_STRING is nonzero). */ \ |
| 399 |
CLASS e(""); \ |
| 400 |
_ITM_memcpyRnWt(that, &e, sizeof(CLASS)); \ |
| 401 |
_txnal_cow_string_C1_for_exceptions(_txnal_##BASE##_get_msg(that), \ |
| 402 |
s, that); \ |
| 403 |
} \ |
| 404 |
void \ |
| 405 |
_ZGTtNSt##NAME##C2EPKc (CLASS*, const char*) \ |
| 406 |
__attribute__((alias ("_ZGTtNSt" #NAME "C1EPKc"))); \ |
| 407 |
CTORS_FROM_SSOSTRING(NAME, CLASS, BASE) \ |
| 408 |
void \ |
| 409 |
_ZGTtNSt##NAME##D1Ev(CLASS* that) \ |
| 410 |
{ _txnal_cow_string_D1(_txnal_##BASE##_get_msg(that)); } \ |
| 411 |
void \ |
| 412 |
_ZGTtNSt##NAME##D2Ev(CLASS*) \ |
| 413 |
__attribute__((alias ("_ZGTtNSt" #NAME "D1Ev"))); \ |
| 414 |
void \ |
| 415 |
_ZGTtNSt##NAME##D0Ev(CLASS* that) \ |
| 416 |
{ \ |
| 417 |
_ZGTtNSt##NAME##D1Ev(that); \ |
| 418 |
_ZGTtdlPv(that); \ |
| 419 |
} |
| 420 |
|
| 421 |
// Now create all transactional constructors and destructors, as well as the |
| 422 |
// two virtual what() functions. |
| 423 |
extern "C" { |
| 424 |
|
| 425 |
CTORDTOR(11logic_error, std::logic_error, logic_error) |
| 426 |
|
| 427 |
const char* |
| 428 |
_ZGTtNKSt11logic_error4whatEv(const std::logic_error* that) |
| 429 |
{ |
| 430 |
return _txnal_cow_string_c_str(_txnal_logic_error_get_msg( |
| 431 |
const_cast<std::logic_error*>(that))); |
| 432 |
} |
| 433 |
|
| 434 |
CTORDTOR(12domain_error, std::domain_error, logic_error) |
| 435 |
CTORDTOR(16invalid_argument, std::invalid_argument, logic_error) |
| 436 |
CTORDTOR(12length_error, std::length_error, logic_error) |
| 437 |
CTORDTOR(12out_of_range, std::out_of_range, logic_error) |
| 438 |
|
| 439 |
|
| 440 |
CTORDTOR(13runtime_error, std::runtime_error, runtime_error) |
| 441 |
|
| 442 |
const char* |
| 443 |
_ZGTtNKSt13runtime_error4whatEv(const std::runtime_error* that) |
| 444 |
{ |
| 445 |
return _txnal_cow_string_c_str(_txnal_runtime_error_get_msg( |
| 446 |
const_cast<std::runtime_error*>(that))); |
| 447 |
} |
| 448 |
|
| 449 |
CTORDTOR(11range_error, std::range_error, runtime_error) |
| 450 |
CTORDTOR(14overflow_error, std::overflow_error, runtime_error) |
| 451 |
CTORDTOR(15underflow_error, std::underflow_error, runtime_error) |
| 452 |
|
| 453 |
} |
| 454 |
|
| 455 |
#endif // _GLIBCXX_USE_C99_STDINT |
| 456 |
#endif // _GLIBCXX_USE_WEAK_REF |