xref: /llvm-project/libcxx/src/system_error.cpp (revision 9c92824dd63852f3e1145b383d7e6a0cdf1bb97d)
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 <__system_error/throw_system_error.h>
12 #include <__verbose_abort>
13 #include <cerrno>
14 #include <cstdio>
15 #include <cstdlib>
16 #include <cstring>
17 #include <optional>
18 #include <string.h>
19 #include <string>
20 #include <system_error>
21 
22 #include "include/config_elast.h"
23 
24 #if defined(_LIBCPP_WIN32API)
25 #  include <windows.h>
26 #  include <winerror.h>
27 #endif
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 #if defined(_LIBCPP_WIN32API)
32 
33 namespace {
34 std::optional<errc> __win_err_to_errc(int err) {
35   switch (err) {
36   case ERROR_ACCESS_DENIED:
37     return errc::permission_denied;
38   case ERROR_ALREADY_EXISTS:
39     return errc::file_exists;
40   case ERROR_BAD_NETPATH:
41     return errc::no_such_file_or_directory;
42   case ERROR_BAD_PATHNAME:
43     return errc::no_such_file_or_directory;
44   case ERROR_BAD_UNIT:
45     return errc::no_such_device;
46   case ERROR_BROKEN_PIPE:
47     return errc::broken_pipe;
48   case ERROR_BUFFER_OVERFLOW:
49     return errc::filename_too_long;
50   case ERROR_BUSY:
51     return errc::device_or_resource_busy;
52   case ERROR_BUSY_DRIVE:
53     return errc::device_or_resource_busy;
54   case ERROR_CANNOT_MAKE:
55     return errc::permission_denied;
56   case ERROR_CANTOPEN:
57     return errc::io_error;
58   case ERROR_CANTREAD:
59     return errc::io_error;
60   case ERROR_CANTWRITE:
61     return errc::io_error;
62   case ERROR_CURRENT_DIRECTORY:
63     return errc::permission_denied;
64   case ERROR_DEV_NOT_EXIST:
65     return errc::no_such_device;
66   case ERROR_DEVICE_IN_USE:
67     return errc::device_or_resource_busy;
68   case ERROR_DIR_NOT_EMPTY:
69     return errc::directory_not_empty;
70   case ERROR_DIRECTORY:
71     return errc::invalid_argument;
72   case ERROR_DISK_FULL:
73     return errc::no_space_on_device;
74   case ERROR_FILE_EXISTS:
75     return errc::file_exists;
76   case ERROR_FILE_NOT_FOUND:
77     return errc::no_such_file_or_directory;
78   case ERROR_HANDLE_DISK_FULL:
79     return errc::no_space_on_device;
80   case ERROR_INVALID_ACCESS:
81     return errc::permission_denied;
82   case ERROR_INVALID_DRIVE:
83     return errc::no_such_device;
84   case ERROR_INVALID_FUNCTION:
85     return errc::function_not_supported;
86   case ERROR_INVALID_HANDLE:
87     return errc::invalid_argument;
88   case ERROR_INVALID_NAME:
89     return errc::no_such_file_or_directory;
90   case ERROR_INVALID_PARAMETER:
91     return errc::invalid_argument;
92   case ERROR_LOCK_VIOLATION:
93     return errc::no_lock_available;
94   case ERROR_LOCKED:
95     return errc::no_lock_available;
96   case ERROR_NEGATIVE_SEEK:
97     return errc::invalid_argument;
98   case ERROR_NOACCESS:
99     return errc::permission_denied;
100   case ERROR_NOT_ENOUGH_MEMORY:
101     return errc::not_enough_memory;
102   case ERROR_NOT_READY:
103     return errc::resource_unavailable_try_again;
104   case ERROR_NOT_SAME_DEVICE:
105     return errc::cross_device_link;
106   case ERROR_NOT_SUPPORTED:
107     return errc::not_supported;
108   case ERROR_OPEN_FAILED:
109     return errc::io_error;
110   case ERROR_OPEN_FILES:
111     return errc::device_or_resource_busy;
112   case ERROR_OPERATION_ABORTED:
113     return errc::operation_canceled;
114   case ERROR_OUTOFMEMORY:
115     return errc::not_enough_memory;
116   case ERROR_PATH_NOT_FOUND:
117     return errc::no_such_file_or_directory;
118   case ERROR_READ_FAULT:
119     return errc::io_error;
120   case ERROR_REPARSE_TAG_INVALID:
121     return errc::invalid_argument;
122   case ERROR_RETRY:
123     return errc::resource_unavailable_try_again;
124   case ERROR_SEEK:
125     return errc::io_error;
126   case ERROR_SHARING_VIOLATION:
127     return errc::permission_denied;
128   case ERROR_TOO_MANY_OPEN_FILES:
129     return errc::too_many_files_open;
130   case ERROR_WRITE_FAULT:
131     return errc::io_error;
132   case ERROR_WRITE_PROTECT:
133     return errc::permission_denied;
134   default:
135     return {};
136   }
137 }
138 } // namespace
139 #endif
140 
141 namespace {
142 #if _LIBCPP_HAS_THREADS
143 
144 //  GLIBC also uses 1024 as the maximum buffer size internally.
145 constexpr size_t strerror_buff_size = 1024;
146 
147 string do_strerror_r(int ev);
148 
149 #  if defined(_LIBCPP_MSVCRT_LIKE)
150 string do_strerror_r(int ev) {
151   char buffer[strerror_buff_size];
152   if (::strerror_s(buffer, strerror_buff_size, ev) == 0)
153     return string(buffer);
154   std::snprintf(buffer, strerror_buff_size, "unknown error %d", ev);
155   return string(buffer);
156 }
157 #  else
158 
159 // Only one of the two following functions will be used, depending on
160 // the return type of strerror_r:
161 
162 // For the GNU variant, a char* return value:
163 __attribute__((unused)) const char* handle_strerror_r_return(char* strerror_return, char* buffer) {
164   // GNU always returns a string pointer in its return value. The
165   // string might point to either the input buffer, or a static
166   // buffer, but we don't care which.
167   return strerror_return;
168 }
169 
170 // For the POSIX variant: an int return value.
171 __attribute__((unused)) const char* handle_strerror_r_return(int strerror_return, char* buffer) {
172   // The POSIX variant either:
173   // - fills in the provided buffer and returns 0
174   // - returns a positive error value, or
175   // - returns -1 and fills in errno with an error value.
176   if (strerror_return == 0)
177     return buffer;
178 
179   // Only handle EINVAL. Other errors abort.
180   int new_errno = strerror_return == -1 ? errno : strerror_return;
181   if (new_errno == EINVAL)
182     return "";
183 
184   _LIBCPP_ASSERT_INTERNAL(new_errno == ERANGE, "unexpected error from ::strerror_r");
185   // FIXME maybe? 'strerror_buff_size' is likely to exceed the
186   // maximum error size so ERANGE shouldn't be returned.
187   std::abort();
188 }
189 
190 // This function handles both GNU and POSIX variants, dispatching to
191 // one of the two above functions.
192 string do_strerror_r(int ev) {
193   char buffer[strerror_buff_size];
194   // Preserve errno around the call. (The C++ standard requires that
195   // system_error functions not modify errno).
196   const int old_errno       = errno;
197   const char* error_message = handle_strerror_r_return(::strerror_r(ev, buffer, strerror_buff_size), buffer);
198   // If we didn't get any message, print one now.
199   if (!error_message[0]) {
200     std::snprintf(buffer, strerror_buff_size, "Unknown error %d", ev);
201     error_message = buffer;
202   }
203   errno = old_errno;
204   return string(error_message);
205 }
206 #  endif
207 
208 #endif // _LIBCPP_HAS_THREADS
209 
210 string make_error_str(const error_code& ec, string what_arg) {
211   if (ec) {
212     if (!what_arg.empty()) {
213       what_arg += ": ";
214     }
215     what_arg += ec.message();
216   }
217   return what_arg;
218 }
219 
220 string make_error_str(const error_code& ec) {
221   if (ec) {
222     return ec.message();
223   }
224   return string();
225 }
226 } // namespace
227 
228 string __do_message::message(int ev) const {
229 #if !_LIBCPP_HAS_THREADS
230   return string(::strerror(ev));
231 #else
232   return do_strerror_r(ev);
233 #endif
234 }
235 
236 class _LIBCPP_HIDDEN __generic_error_category : public __do_message {
237 public:
238   virtual const char* name() const noexcept;
239   virtual string message(int ev) const;
240 };
241 
242 const char* __generic_error_category::name() const noexcept { return "generic"; }
243 
244 string __generic_error_category::message(int ev) const {
245 #ifdef _LIBCPP_ELAST
246   if (ev > _LIBCPP_ELAST)
247     return string("unspecified generic_category error");
248 #endif // _LIBCPP_ELAST
249   return __do_message::message(ev);
250 }
251 
252 const error_category& generic_category() noexcept {
253   union AvoidDestroyingGenericCategory {
254     __generic_error_category generic_error_category;
255     constexpr explicit AvoidDestroyingGenericCategory() : generic_error_category() {}
256     ~AvoidDestroyingGenericCategory() {}
257   };
258   constinit static AvoidDestroyingGenericCategory helper;
259   return helper.generic_error_category;
260 }
261 
262 class _LIBCPP_HIDDEN __system_error_category : public __do_message {
263 public:
264   virtual const char* name() const noexcept;
265   virtual string message(int ev) const;
266   virtual error_condition default_error_condition(int ev) const noexcept;
267 };
268 
269 const char* __system_error_category::name() const noexcept { return "system"; }
270 
271 string __system_error_category::message(int ev) const {
272 #ifdef _LIBCPP_WIN32API
273   std::string result;
274   char* str               = nullptr;
275   unsigned long num_chars = ::FormatMessageA(
276       FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
277       nullptr,
278       ev,
279       0,
280       reinterpret_cast<char*>(&str),
281       0,
282       nullptr);
283   auto is_whitespace = [](char ch) { return ch == '\n' || ch == '\r' || ch == ' '; };
284   while (num_chars > 0 && is_whitespace(str[num_chars - 1]))
285     --num_chars;
286 
287   if (num_chars)
288     result = std::string(str, num_chars);
289   else
290     result = "Unknown error";
291 
292   LocalFree(str);
293   return result;
294 #else
295 #  ifdef _LIBCPP_ELAST
296   if (ev > _LIBCPP_ELAST)
297     return string("unspecified system_category error");
298 #  endif // _LIBCPP_ELAST
299   return __do_message::message(ev);
300 #endif
301 }
302 
303 error_condition __system_error_category::default_error_condition(int ev) const noexcept {
304 #ifdef _LIBCPP_WIN32API
305   // Remap windows error codes to generic error codes if possible.
306   if (ev == 0)
307     return error_condition(0, generic_category());
308   if (auto maybe_errc = __win_err_to_errc(ev))
309     return error_condition(static_cast<int>(*maybe_errc), generic_category());
310   return error_condition(ev, system_category());
311 #else
312 #  ifdef _LIBCPP_ELAST
313   if (ev > _LIBCPP_ELAST)
314     return error_condition(ev, system_category());
315 #  endif // _LIBCPP_ELAST
316   return error_condition(ev, generic_category());
317 #endif
318 }
319 
320 const error_category& system_category() noexcept {
321   union AvoidDestroyingSystemCategory {
322     __system_error_category system_error_category;
323     constexpr explicit AvoidDestroyingSystemCategory() : system_error_category() {}
324     ~AvoidDestroyingSystemCategory() {}
325   };
326   constinit static AvoidDestroyingSystemCategory helper;
327   return helper.system_error_category;
328 }
329 
330 // error_condition
331 
332 string error_condition::message() const { return __cat_->message(__val_); }
333 
334 // error_code
335 
336 string error_code::message() const { return __cat_->message(__val_); }
337 
338 // system_error
339 
340 system_error::system_error(error_code ec, const string& what_arg)
341     : runtime_error(make_error_str(ec, what_arg)), __ec_(ec) {}
342 
343 system_error::system_error(error_code ec, const char* what_arg)
344     : runtime_error(make_error_str(ec, what_arg)), __ec_(ec) {}
345 
346 system_error::system_error(error_code ec) : runtime_error(make_error_str(ec)), __ec_(ec) {}
347 
348 system_error::system_error(int ev, const error_category& ecat, const string& what_arg)
349     : runtime_error(make_error_str(error_code(ev, ecat), what_arg)), __ec_(error_code(ev, ecat)) {}
350 
351 system_error::system_error(int ev, const error_category& ecat, const char* what_arg)
352     : runtime_error(make_error_str(error_code(ev, ecat), what_arg)), __ec_(error_code(ev, ecat)) {}
353 
354 system_error::system_error(int ev, const error_category& ecat)
355     : runtime_error(make_error_str(error_code(ev, ecat))), __ec_(error_code(ev, ecat)) {}
356 
357 system_error::~system_error() noexcept {}
358 
359 void __throw_system_error(int ev, const char* what_arg) {
360 #if _LIBCPP_HAS_EXCEPTIONS
361   std::__throw_system_error(error_code(ev, generic_category()), what_arg);
362 #else
363   // The above could also handle the no-exception case, but for size, avoid referencing system_category() unnecessarily.
364   _LIBCPP_VERBOSE_ABORT(
365       "system_error was thrown in -fno-exceptions mode with error %i and message \"%s\"", ev, what_arg);
366 #endif
367 }
368 
369 _LIBCPP_END_NAMESPACE_STD
370