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