xref: /oneTBB/src/tbb/exception.cpp (revision c21e688a)
1 /*
2     Copyright (c) 2005-2022 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #include "oneapi/tbb/detail/_exception.h"
18 #include "oneapi/tbb/detail/_assert.h"
19 #include "oneapi/tbb/detail/_template_helpers.h"
20 
21 #include <cstring>
22 #include <cstdio>
23 #include <stdexcept> // std::runtime_error
24 #include <new>
25 #include <stdexcept>
26 
27 #define __TBB_STD_RETHROW_EXCEPTION_POSSIBLY_BROKEN                             \
28     (__GLIBCXX__ && __TBB_GLIBCXX_VERSION>=40700 && __TBB_GLIBCXX_VERSION<60000 && TBB_USE_EXCEPTIONS)
29 
30 #if __TBB_STD_RETHROW_EXCEPTION_POSSIBLY_BROKEN
31 // GCC ABI declarations necessary for a workaround
32 #include <cxxabi.h>
33 #endif
34 
35 namespace tbb {
36 namespace detail {
37 namespace r1 {
38 
what() const39 const char* bad_last_alloc::what() const noexcept(true) { return "bad allocation in previous or concurrent attempt"; }
what() const40 const char* user_abort::what() const noexcept(true) { return "User-initiated abort has terminated this operation"; }
what() const41 const char* missing_wait::what() const noexcept(true) { return "wait() was not called on the structured_task_group"; }
42 
43 #if TBB_USE_EXCEPTIONS
44     template <typename F>
do_throw_noexcept(F throw_func)45     /*[[noreturn]]*/ void do_throw_noexcept(F throw_func) noexcept {
46         throw_func();
47     }
48 
do_throw_noexcept(void (* throw_func)())49     /*[[noreturn]]*/ void do_throw_noexcept(void (*throw_func)()) noexcept {
50         throw_func();
51 #if __GNUC__ == 7
52         // In release, GCC 7 loses noexcept attribute during tail call optimization.
53         // The following statement prevents tail call optimization.
54         volatile bool reach_this_point = true;
55         suppress_unused_warning(reach_this_point);
56 #endif
57     }
58 
59     bool terminate_on_exception(); // defined in global_control.cpp and ipc_server.cpp
60 
61     template <typename F>
do_throw(F throw_func)62     /*[[noreturn]]*/ void do_throw(F throw_func) {
63         if (terminate_on_exception()) {
64             do_throw_noexcept(throw_func);
65         }
66         throw_func();
67     }
68 
69     #define DO_THROW(exc, init_args) do_throw( []{ throw exc init_args; } );
70 #else /* !TBB_USE_EXCEPTIONS */
71     #define PRINT_ERROR_AND_ABORT(exc_name, msg) \
72         std::fprintf (stderr, "Exception %s with message %s would have been thrown, "  \
73             "if exception handling had not been disabled. Aborting.\n", exc_name, msg); \
74         std::fflush(stderr); \
75         std::abort();
76     #define DO_THROW(exc, init_args) PRINT_ERROR_AND_ABORT(#exc, #init_args)
77 #endif /* !TBB_USE_EXCEPTIONS */
78 
throw_exception(exception_id eid)79 void throw_exception ( exception_id eid ) {
80     switch ( eid ) {
81     case exception_id::bad_alloc: DO_THROW(std::bad_alloc, ()); break;
82     case exception_id::bad_last_alloc: DO_THROW(bad_last_alloc, ()); break;
83     case exception_id::user_abort: DO_THROW( user_abort, () ); break;
84     case exception_id::nonpositive_step: DO_THROW(std::invalid_argument, ("Step must be positive") ); break;
85     case exception_id::out_of_range: DO_THROW(std::out_of_range, ("Index out of requested size range")); break;
86     case exception_id::reservation_length_error: DO_THROW(std::length_error, ("Attempt to exceed implementation defined length limits")); break;
87     case exception_id::missing_wait: DO_THROW(missing_wait, ()); break;
88     case exception_id::invalid_load_factor: DO_THROW(std::out_of_range, ("Invalid hash load factor")); break;
89     case exception_id::invalid_key: DO_THROW(std::out_of_range, ("invalid key")); break;
90     case exception_id::bad_tagged_msg_cast: DO_THROW(std::runtime_error, ("Illegal tagged_msg cast")); break;
91     case exception_id::unsafe_wait: DO_THROW(unsafe_wait, ("Unsafe to wait further")); break;
92     default: __TBB_ASSERT ( false, "Unknown exception ID" );
93     }
94     __TBB_ASSERT(false, "Unreachable code");
95 }
96 
97 /* The "what" should be fairly short, not more than about 128 characters.
98    Because we control all the call sites to handle_perror, it is pointless
99    to bullet-proof it for very long strings.
100 
101    Design note: ADR put this routine off to the side in tbb_misc.cpp instead of
102    Task.cpp because the throw generates a pathetic lot of code, and ADR wanted
103    this large chunk of code to be placed on a cold page. */
handle_perror(int error_code,const char * what)104 void handle_perror( int error_code, const char* what ) {
105     const int BUF_SIZE = 255;
106     char buf[BUF_SIZE + 1] = { 0 };
107     std::strncat(buf, what, BUF_SIZE);
108     std::size_t buf_len = std::strlen(buf);
109     if (error_code) {
110         std::strncat(buf, ": ", BUF_SIZE - buf_len);
111         buf_len = std::strlen(buf);
112         std::strncat(buf, std::strerror(error_code), BUF_SIZE - buf_len);
113         buf_len = std::strlen(buf);
114     }
115     __TBB_ASSERT(buf_len <= BUF_SIZE && buf[buf_len] == 0, nullptr);
116 #if TBB_USE_EXCEPTIONS
117     do_throw([&buf] { throw std::runtime_error(buf); });
118 #else
119     PRINT_ERROR_AND_ABORT( "runtime_error", buf);
120 #endif /* !TBB_USE_EXCEPTIONS */
121 }
122 
123 #if __TBB_STD_RETHROW_EXCEPTION_POSSIBLY_BROKEN
124 // Runtime detection and workaround for the GCC bug 62258.
125 // The problem is that std::rethrow_exception() does not increment a counter
126 // of active exceptions, causing std::uncaught_exception() to return a wrong value.
127 // The code is created after, and roughly reflects, the workaround
128 // at https://gcc.gnu.org/bugzilla/attachment.cgi?id=34683
129 
fix_broken_rethrow()130 void fix_broken_rethrow() {
131     struct gcc_eh_data {
132         void *       caughtExceptions;
133         unsigned int uncaughtExceptions;
134     };
135     gcc_eh_data* eh_data = punned_cast<gcc_eh_data*>( abi::__cxa_get_globals() );
136     ++eh_data->uncaughtExceptions;
137 }
138 
gcc_rethrow_exception_broken()139 bool gcc_rethrow_exception_broken() {
140     bool is_broken;
141     __TBB_ASSERT( !std::uncaught_exception(),
142         "gcc_rethrow_exception_broken() must not be called when an exception is active" );
143     try {
144         // Throw, catch, and rethrow an exception
145         try {
146             throw __TBB_GLIBCXX_VERSION;
147         } catch(...) {
148             std::rethrow_exception( std::current_exception() );
149         }
150     } catch(...) {
151         // Check the bug presence
152         is_broken = std::uncaught_exception();
153     }
154     if( is_broken ) fix_broken_rethrow();
155     __TBB_ASSERT( !std::uncaught_exception(), nullptr);
156     return is_broken;
157 }
158 #else
fix_broken_rethrow()159 void fix_broken_rethrow() {}
gcc_rethrow_exception_broken()160 bool gcc_rethrow_exception_broken() { return false; }
161 #endif /* __TBB_STD_RETHROW_EXCEPTION_POSSIBLY_BROKEN */
162 
163 } // namespace r1
164 } // namespace detail
165 } // namespace tbb
166 
167