1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <__assert>
10 #include <__config>
11 #include <__verbose_abort>
12 #include <cerrno>
13 #include <cstdio>
14 #include <cstdlib>
15 #include <cstring>
16 #include <string.h>
17 #include <string>
18 #include <system_error>
19 
20 #include "include/config_elast.h"
21 
22 #if defined(__ANDROID__)
23 #  include <android/api-level.h>
24 #endif
25 
26 _LIBCPP_BEGIN_NAMESPACE_STD
27 
28 namespace {
29 #if !defined(_LIBCPP_HAS_NO_THREADS)
30 
31 //  GLIBC also uses 1024 as the maximum buffer size internally.
32 constexpr size_t strerror_buff_size = 1024;
33 
34 string do_strerror_r(int ev);
35 
36 #  if defined(_LIBCPP_MSVCRT_LIKE)
37 string do_strerror_r(int ev) {
38   char buffer[strerror_buff_size];
39   if (::strerror_s(buffer, strerror_buff_size, ev) == 0)
40     return string(buffer);
41   std::snprintf(buffer, strerror_buff_size, "unknown error %d", ev);
42   return string(buffer);
43 }
44 #  else
45 
46 // Only one of the two following functions will be used, depending on
47 // the return type of strerror_r:
48 
49 // For the GNU variant, a char* return value:
50 __attribute__((unused)) const char* handle_strerror_r_return(char* strerror_return, char* buffer) {
51   // GNU always returns a string pointer in its return value. The
52   // string might point to either the input buffer, or a static
53   // buffer, but we don't care which.
54   return strerror_return;
55 }
56 
57 // For the POSIX variant: an int return value.
58 __attribute__((unused)) const char* handle_strerror_r_return(int strerror_return, char* buffer) {
59   // The POSIX variant either:
60   // - fills in the provided buffer and returns 0
61   // - returns a positive error value, or
62   // - returns -1 and fills in errno with an error value.
63   if (strerror_return == 0)
64     return buffer;
65 
66   // Only handle EINVAL. Other errors abort.
67   int new_errno = strerror_return == -1 ? errno : strerror_return;
68   if (new_errno == EINVAL)
69     return "";
70 
71   _LIBCPP_ASSERT_INTERNAL(new_errno == ERANGE, "unexpected error from ::strerror_r");
72   // FIXME maybe? 'strerror_buff_size' is likely to exceed the
73   // maximum error size so ERANGE shouldn't be returned.
74   std::abort();
75 }
76 
77 // This function handles both GNU and POSIX variants, dispatching to
78 // one of the two above functions.
79 string do_strerror_r(int ev) {
80   char buffer[strerror_buff_size];
81   // Preserve errno around the call. (The C++ standard requires that
82   // system_error functions not modify errno).
83   const int old_errno       = errno;
84   const char* error_message = handle_strerror_r_return(::strerror_r(ev, buffer, strerror_buff_size), buffer);
85   // If we didn't get any message, print one now.
86   if (!error_message[0]) {
87     std::snprintf(buffer, strerror_buff_size, "Unknown error %d", ev);
88     error_message = buffer;
89   }
90   errno = old_errno;
91   return string(error_message);
92 }
93 #  endif
94 
95 #endif // !defined(_LIBCPP_HAS_NO_THREADS)
96 
97 string make_error_str(const error_code& ec, string what_arg) {
98   if (ec) {
99     if (!what_arg.empty()) {
100       what_arg += ": ";
101     }
102     what_arg += ec.message();
103   }
104   return what_arg;
105 }
106 
107 string make_error_str(const error_code& ec) {
108   if (ec) {
109     return ec.message();
110   }
111   return string();
112 }
113 } // end namespace
114 
115 string __do_message::message(int ev) const {
116 #if defined(_LIBCPP_HAS_NO_THREADS)
117   return string(::strerror(ev));
118 #else
119   return do_strerror_r(ev);
120 #endif
121 }
122 
123 class _LIBCPP_HIDDEN __generic_error_category : public __do_message {
124 public:
125   virtual const char* name() const noexcept;
126   virtual string message(int ev) const;
127 };
128 
129 const char* __generic_error_category::name() const noexcept { return "generic"; }
130 
131 string __generic_error_category::message(int ev) const {
132 #ifdef _LIBCPP_ELAST
133   if (ev > _LIBCPP_ELAST)
134     return string("unspecified generic_category error");
135 #endif // _LIBCPP_ELAST
136   return __do_message::message(ev);
137 }
138 
139 const error_category& generic_category() noexcept {
140   union AvoidDestroyingGenericCategory {
141     __generic_error_category generic_error_category;
142     constexpr explicit AvoidDestroyingGenericCategory() : generic_error_category() {}
143     ~AvoidDestroyingGenericCategory() {}
144   };
145   constinit static AvoidDestroyingGenericCategory helper;
146   return helper.generic_error_category;
147 }
148 
149 class _LIBCPP_HIDDEN __system_error_category : public __do_message {
150 public:
151   virtual const char* name() const noexcept;
152   virtual string message(int ev) const;
153   virtual error_condition default_error_condition(int ev) const noexcept;
154 };
155 
156 const char* __system_error_category::name() const noexcept { return "system"; }
157 
158 string __system_error_category::message(int ev) const {
159 #ifdef _LIBCPP_ELAST
160   if (ev > _LIBCPP_ELAST)
161     return string("unspecified system_category error");
162 #endif // _LIBCPP_ELAST
163   return __do_message::message(ev);
164 }
165 
166 error_condition __system_error_category::default_error_condition(int ev) const noexcept {
167 #ifdef _LIBCPP_ELAST
168   if (ev > _LIBCPP_ELAST)
169     return error_condition(ev, system_category());
170 #endif // _LIBCPP_ELAST
171   return error_condition(ev, generic_category());
172 }
173 
174 const error_category& system_category() noexcept {
175   union AvoidDestroyingSystemCategory {
176     __system_error_category system_error_category;
177     constexpr explicit AvoidDestroyingSystemCategory() : system_error_category() {}
178     ~AvoidDestroyingSystemCategory() {}
179   };
180   constinit static AvoidDestroyingSystemCategory helper;
181   return helper.system_error_category;
182 }
183 
184 // error_condition
185 
186 string error_condition::message() const { return __cat_->message(__val_); }
187 
188 // error_code
189 
190 string error_code::message() const { return __cat_->message(__val_); }
191 
192 // system_error
193 
194 system_error::system_error(error_code ec, const string& what_arg)
195     : runtime_error(make_error_str(ec, what_arg)), __ec_(ec) {}
196 
197 system_error::system_error(error_code ec, const char* what_arg)
198     : runtime_error(make_error_str(ec, what_arg)), __ec_(ec) {}
199 
200 system_error::system_error(error_code ec) : runtime_error(make_error_str(ec)), __ec_(ec) {}
201 
202 system_error::system_error(int ev, const error_category& ecat, const string& what_arg)
203     : runtime_error(make_error_str(error_code(ev, ecat), what_arg)), __ec_(error_code(ev, ecat)) {}
204 
205 system_error::system_error(int ev, const error_category& ecat, const char* what_arg)
206     : runtime_error(make_error_str(error_code(ev, ecat), what_arg)), __ec_(error_code(ev, ecat)) {}
207 
208 system_error::system_error(int ev, const error_category& ecat)
209     : runtime_error(make_error_str(error_code(ev, ecat))), __ec_(error_code(ev, ecat)) {}
210 
211 system_error::~system_error() noexcept {}
212 
213 void __throw_system_error(int ev, const char* what_arg) {
214 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
215   std::__throw_system_error(error_code(ev, system_category()), what_arg);
216 #else
217   // The above could also handle the no-exception case, but for size, avoid referencing system_category() unnecessarily.
218   _LIBCPP_VERBOSE_ABORT(
219       "system_error was thrown in -fno-exceptions mode with error %i and message \"%s\"", ev, what_arg);
220 #endif
221 }
222 
223 _LIBCPP_END_NAMESPACE_STD
224