libstdc++/71049 fix --disable-libstdcxx-dual-abi bootstrap
[official-gcc.git] / libstdc++-v3 / src / c++11 / cow-stdexcept.cc
blob31a89dfd5541aba4253f7d6de44012905780ab9f
1 // Methods for Exception Support for -*- C++ -*-
3 // Copyright (C) 2014-2016 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.
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.
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.
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/>.
26 // ISO C++ 14882: 19.1 Exception classes
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);
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 #include <system_error>
50 #undef __cow_string
52 namespace std _GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
56 // Copy constructors and assignment operators defined using COW std::string
58 logic_error::logic_error(const logic_error& e) noexcept
59 : _M_msg(e._M_msg) { }
61 logic_error& logic_error::operator=(const logic_error& e) noexcept
62 { _M_msg = e._M_msg; return *this; }
64 runtime_error::runtime_error(const runtime_error& e) noexcept
65 : _M_msg(e._M_msg) { }
67 runtime_error&
68 runtime_error::operator=(const runtime_error& e) noexcept
69 { _M_msg = e._M_msg; return *this; }
71 // New C++11 constructors:
73 logic_error::logic_error(const char* __arg)
74 : exception(), _M_msg(__arg) { }
76 domain_error::domain_error(const char* __arg)
77 : logic_error(__arg) { }
79 invalid_argument::invalid_argument(const char* __arg)
80 : logic_error(__arg) { }
82 length_error::length_error(const char* __arg)
83 : logic_error(__arg) { }
85 out_of_range::out_of_range(const char* __arg)
86 : logic_error(__arg) { }
88 runtime_error::runtime_error(const char* __arg)
89 : exception(), _M_msg(__arg) { }
91 range_error::range_error(const char* __arg)
92 : runtime_error(__arg) { }
94 overflow_error::overflow_error(const char* __arg)
95 : runtime_error(__arg) { }
97 underflow_error::underflow_error(const char* __arg)
98 : runtime_error(__arg) { }
100 #if _GLIBCXX_USE_DUAL_ABI
101 // Converting constructor from COW std::string to SSO string.
102 __sso_string::__sso_string(const string& s)
103 : __sso_string(s.c_str(), s.length()) { }
105 // Redefine __cow_string so that we can define and export its members
106 // in terms of the COW std::string.
107 struct __cow_string
109 union {
110 const char* _M_p;
111 char _M_bytes[sizeof(_M_p)];
112 std::string _M_str;
115 __cow_string();
116 __cow_string(const std::string& s);
117 __cow_string(const char*, size_t n);
118 __cow_string(const __cow_string&) noexcept;
119 __cow_string& operator=(const __cow_string&) noexcept;
120 ~__cow_string();
121 __cow_string(__cow_string&&) noexcept;
122 __cow_string& operator=(__cow_string&&) noexcept;
125 __cow_string::__cow_string() : _M_str() { }
127 __cow_string::__cow_string(const std::string& s) : _M_str(s) { }
129 __cow_string::__cow_string(const char* s, size_t n) : _M_str(s, n) { }
131 __cow_string::__cow_string(const __cow_string& s) noexcept
132 : _M_str(s._M_str) { }
134 __cow_string&
135 __cow_string::operator=(const __cow_string& s) noexcept
137 _M_str = s._M_str;
138 return *this;
141 __cow_string::~__cow_string() { _M_str.~basic_string(); }
143 __cow_string::__cow_string(__cow_string&& s) noexcept
144 : _M_str(std::move(s._M_str)) { }
146 __cow_string&
147 __cow_string::operator=(__cow_string&& s) noexcept
149 _M_str = std::move(s._M_str);
150 return *this;
153 static_assert(sizeof(__cow_string) == sizeof(std::string),
154 "sizeof(std::string) has changed");
155 static_assert(alignof(__cow_string) == alignof(std::string),
156 "alignof(std::string) has changed");
157 #endif
159 // Return error_category::message() as an SSO string
160 __sso_string
161 error_category::_M_message(int i) const
163 string msg = this->message(i);
164 return {msg.c_str(), msg.length()};
167 _GLIBCXX_END_NAMESPACE_VERSION
168 } // namespace
170 // Support for the Transactional Memory TS (N4514).
172 // logic_error and runtime_error both carry a message in the form of a COW
173 // string. This COW string is never made visible to users of the exception
174 // because what() returns a C string. The COW string can be constructed as
175 // either a copy of a COW string of another logic_error/runtime_error, or
176 // using a C string or SSO string; thus, the COW string's _Rep is only
177 // accessed by logic_error operations. We control all txnal clones of those
178 // operations and thus can ensure that _Rep is never accessed transactionally.
179 // Furthermore, _Rep will always have been allocated or deallocated via
180 // global new or delete, so nontransactional writes we do to _Rep cannot
181 // interfere with transactional accesses.
183 // We depend on having support for referencing functions declared weak that
184 // are not defined by us. Without such support, the exceptions will not be
185 // declared transaction-safe, so we just don't provide transactional clones
186 // in this case.
187 #if _GLIBCXX_USE_WEAK_REF
189 extern "C" {
191 #ifndef _GLIBCXX_MANGLE_SIZE_T
192 #error Mangled name of size_t type not defined.
193 #endif
194 #define CONCAT1(x,y) x##y
195 #define CONCAT(x,y) CONCAT1(x,y)
196 #define _ZGTtnaX CONCAT(_ZGTtna,_GLIBCXX_MANGLE_SIZE_T)
198 #ifdef __i386__
199 /* Only for 32-bit x86. */
200 # define ITM_REGPARM __attribute__((regparm(2)))
201 #else
202 # define ITM_REGPARM
203 #endif
205 // Declare all libitm symbols we rely on, but make them weak so that we do
206 // not depend on libitm.
207 extern void* _ZGTtnaX (size_t sz) __attribute__((weak));
208 extern void _ZGTtdlPv (void* ptr) __attribute__((weak));
209 extern uint8_t _ITM_RU1(const uint8_t *p)
210 ITM_REGPARM __attribute__((weak));
211 extern uint32_t _ITM_RU4(const uint32_t *p)
212 ITM_REGPARM __attribute__((weak));
213 extern uint64_t _ITM_RU8(const uint64_t *p)
214 ITM_REGPARM __attribute__((weak));
215 extern void _ITM_memcpyRtWn(void *, const void *, size_t)
216 ITM_REGPARM __attribute__((weak));
217 extern void _ITM_memcpyRnWt(void *, const void *, size_t)
218 ITM_REGPARM __attribute__((weak));
219 extern void _ITM_addUserCommitAction(void (*)(void *), uint64_t, void *)
220 ITM_REGPARM __attribute__((weak));
224 // A transactional version of basic_string::basic_string(const char *s)
225 // that also notifies the TM runtime about allocations belonging to this
226 // exception.
227 void
228 _txnal_cow_string_C1_for_exceptions(void* that, const char* s,
229 void *exc __attribute__((unused)))
231 typedef std::basic_string<char> bs_type;
232 bs_type *bs = (bs_type*) that;
234 // First, do a transactional strlen, but including the trailing zero.
235 bs_type::size_type len = 1;
236 for (const char *ss = s; _ITM_RU1((const uint8_t*) ss) != 0; ss++, len++);
239 // Allocate memory for the string and the refcount. We use the
240 // transactional clone of global new[]; if this throws, it will do so in a
241 // transaction-compatible way.
242 // The allocation belongs to this exception, so tell the runtime about it.
243 // TODO Once this is supported, link the following allocation to this
244 // exception: void *prev = _ITM_setAssociatedException(exc);
245 bs_type::_Rep *rep;
246 __try
248 rep = (bs_type::_Rep*) _ZGTtnaX (len + sizeof (bs_type::_Rep));
250 __catch (...)
252 // Pop the association with this exception.
253 // TODO Once this is supported, link the following allocation to this
254 // exception: _ITM_setAssociatedException(prev);
255 // We do not need to instrument a rethrow.
256 __throw_exception_again;
258 // Pop the association with this exception.
259 // TODO Once this is supported, link the following allocation to this
260 // exception: _ITM_setAssociatedException(prev);
262 // Now initialize the rest of the string and copy the C string. The memory
263 // will be freshly allocated, so nontransactional accesses are sufficient,
264 // including the writes when copying the string (see above).
265 rep->_M_set_sharable();
266 rep->_M_length = rep->_M_capacity = len - 1;
267 _ITM_memcpyRtWn(rep->_M_refdata(), s, len);
268 new (&bs->_M_dataplus) bs_type::_Alloc_hider(rep->_M_refdata(),
269 bs_type::allocator_type());
272 static void* txnal_read_ptr(void* const * ptr)
274 static_assert(sizeof(uint64_t) == sizeof(void*)
275 || sizeof(uint32_t) == sizeof(void*),
276 "Pointers must be 32 bits or 64 bits wide");
277 #if __UINTPTR_MAX__ == __UINT64_MAX__
278 return (void*)_ITM_RU8((const uint64_t*)ptr);
279 #else
280 return (void*)_ITM_RU4((const uint32_t*)ptr);
281 #endif
284 // We must access the data pointer in the COW string transactionally because
285 // another transaction can delete the string and reuse the memory.
286 const char*
287 _txnal_cow_string_c_str(const void* that)
289 typedef std::basic_string<char> bs_type;
290 const bs_type *bs = (const bs_type*) that;
292 return (const char*) txnal_read_ptr((void**)&bs->_M_dataplus._M_p);
295 #if _GLIBCXX_USE_DUAL_ABI
296 const char*
297 _txnal_sso_string_c_str(const void* that)
299 return (const char*) txnal_read_ptr(
300 (void* const*)const_cast<char* const*>(
301 &((const std::__sso_string*) that)->_M_s._M_p));
303 #endif
305 void
306 _txnal_cow_string_D1_commit(void* data)
308 typedef std::basic_string<char> bs_type;
309 bs_type::_Rep *rep = (bs_type::_Rep*) data;
310 rep->_M_dispose(bs_type::allocator_type());
313 void
314 _txnal_cow_string_D1(void* that)
316 typedef std::basic_string<char> bs_type;
317 bs_type::_Rep *rep = reinterpret_cast<bs_type::_Rep*>(
318 const_cast<char*>(_txnal_cow_string_c_str(that))) - 1;
320 // The string can be shared, in which case we would need to decrement the
321 // reference count. We cannot undo that because we might lose the string
322 // otherwise. Therefore, we register a commit action that will dispose of
323 // the string's _Rep.
324 enum {_ITM_noTransactionId = 1};
325 _ITM_addUserCommitAction(_txnal_cow_string_D1_commit, _ITM_noTransactionId,
326 rep);
329 void*
330 _txnal_logic_error_get_msg(void* e)
332 std::logic_error* le = (std::logic_error*) e;
333 return &le->_M_msg;
336 void*
337 _txnal_runtime_error_get_msg(void* e)
339 std::runtime_error* le = (std::runtime_error*) e;
340 return &le->_M_msg;
343 // The constructors are only declared transaction-safe if the C++11 ABI is
344 // used for std::string and the exception classes use a COW string internally.
345 // A user must not call these constructors otherwise; if they do, it will
346 // result in undefined behavior, which is in this case not initializing this
347 // string.
348 #if _GLIBCXX_USE_DUAL_ABI
349 #define CTORS_FROM_SSOSTRING(NAME, CLASS, BASE) \
350 void \
351 _ZGTtNSt##NAME##C1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE( \
352 CLASS* that, const std::__sso_string& s) \
354 CLASS e(""); \
355 _ITM_memcpyRnWt(that, &e, sizeof(CLASS)); \
356 /* Get the C string from the SSO string. */ \
357 _txnal_cow_string_C1_for_exceptions(_txnal_##BASE##_get_msg(that), \
358 _txnal_sso_string_c_str(&s), that); \
360 void \
361 _ZGTtNSt##NAME##C2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE( \
362 CLASS*, const std::__sso_string&) __attribute__((alias \
363 ("_ZGTtNSt" #NAME \
364 "C1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE")));
365 #else
366 #define CTORS_FROM_SSOSTRING(NAME, CLASS, BASE)
367 #endif
369 // This macro defines transaction constructors and destructors for a specific
370 // exception class. NAME is the variable part of the mangled name, CLASS is
371 // the class name, and BASE must be logic_error or runtime_error (which is
372 // then used to call the proper friend function that can return a pointer to
373 // the _M_msg member declared by the given (base) class).
374 #define CTORDTOR(NAME, CLASS, BASE) \
375 void \
376 _ZGTtNSt##NAME##C1EPKc (CLASS* that, const char* s) \
378 /* This will use the singleton _Rep for an empty string and just \
379 point to it instead of allocating memory. Thus, we can use it as \
380 source, copy it into the object we are constructing, and then \
381 construct the COW string in the latter manually. Note that the \
382 exception classes will not be declared transaction_safe if the \
383 shared empty _Rep is disabled with --enable-fully-dynamic-string \
384 (in which case _GLIBCXX_FULLY_DYNAMIC_STRING is nonzero). */ \
385 CLASS e(""); \
386 _ITM_memcpyRnWt(that, &e, sizeof(CLASS)); \
387 _txnal_cow_string_C1_for_exceptions(_txnal_##BASE##_get_msg(that), \
388 s, that); \
390 void \
391 _ZGTtNSt##NAME##C2EPKc (CLASS*, const char*) \
392 __attribute__((alias ("_ZGTtNSt" #NAME "C1EPKc"))); \
393 CTORS_FROM_SSOSTRING(NAME, CLASS, BASE) \
394 void \
395 _ZGTtNSt##NAME##D1Ev(CLASS* that) \
396 { _txnal_cow_string_D1(_txnal_##BASE##_get_msg(that)); } \
397 void \
398 _ZGTtNSt##NAME##D2Ev(CLASS*) \
399 __attribute__((alias ("_ZGTtNSt" #NAME "D1Ev"))); \
400 void \
401 _ZGTtNSt##NAME##D0Ev(CLASS* that) \
403 _ZGTtNSt##NAME##D1Ev(that); \
404 _ZGTtdlPv(that); \
407 // Now create all transactional constructors and destructors, as well as the
408 // two virtual what() functions.
409 extern "C" {
411 CTORDTOR(11logic_error, std::logic_error, logic_error)
413 const char*
414 _ZGTtNKSt11logic_error4whatEv(const std::logic_error* that)
416 return _txnal_cow_string_c_str(_txnal_logic_error_get_msg(
417 const_cast<std::logic_error*>(that)));
420 CTORDTOR(12domain_error, std::domain_error, logic_error)
421 CTORDTOR(16invalid_argument, std::invalid_argument, logic_error)
422 CTORDTOR(12length_error, std::length_error, logic_error)
423 CTORDTOR(12out_of_range, std::out_of_range, logic_error)
426 CTORDTOR(13runtime_error, std::runtime_error, runtime_error)
428 const char*
429 _ZGTtNKSt13runtime_error4whatEv(const std::runtime_error* that)
431 return _txnal_cow_string_c_str(_txnal_runtime_error_get_msg(
432 const_cast<std::runtime_error*>(that)));
435 CTORDTOR(11range_error, std::range_error, runtime_error)
436 CTORDTOR(14overflow_error, std::overflow_error, runtime_error)
437 CTORDTOR(15underflow_error, std::underflow_error, runtime_error)
441 #endif // _GLIBCXX_USE_WEAK_REF