Harden failure signal handler in the face of memory corruptions
[folly.git] / folly / Exception.h
1 /*
2  * Copyright 2014 Facebook, Inc.
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 #ifndef FOLLY_EXCEPTION_H_
18 #define FOLLY_EXCEPTION_H_
19
20 #include <errno.h>
21
22 #include <cstdio>
23 #include <stdexcept>
24 #include <system_error>
25
26 #include "folly/Conv.h"
27 #include "folly/FBString.h"
28 #include "folly/Likely.h"
29 #include "folly/Portability.h"
30
31 namespace folly {
32
33 // Various helpers to throw appropriate std::system_error exceptions from C
34 // library errors (returned in errno, as positive return values (many POSIX
35 // functions), or as negative return values (Linux syscalls))
36 //
37 // The *Explicit functions take an explicit value for errno.
38
39 // Helper to throw std::system_error
40 void throwSystemErrorExplicit(int err, const char*) FOLLY_NORETURN;
41 inline void throwSystemErrorExplicit(int err, const char* msg) {
42   throw std::system_error(err, std::system_category(), msg);
43 }
44
45 template <class... Args>
46 void throwSystemErrorExplicit(int, Args&&... args) FOLLY_NORETURN;
47 template <class... Args>
48 void throwSystemErrorExplicit(int err, Args&&... args) {
49   throwSystemErrorExplicit(
50       err, to<fbstring>(std::forward<Args>(args)...).c_str());
51 }
52
53 // Helper to throw std::system_error from errno and components of a string
54 template <class... Args>
55 void throwSystemError(Args&&... args) FOLLY_NORETURN;
56 template <class... Args>
57 void throwSystemError(Args&&... args) {
58   throwSystemErrorExplicit(errno, std::forward<Args>(args)...);
59 }
60
61 // Check a Posix return code (0 on success, error number on error), throw
62 // on error.
63 template <class... Args>
64 void checkPosixError(int err, Args&&... args) {
65   if (UNLIKELY(err != 0)) {
66     throwSystemErrorExplicit(err, std::forward<Args>(args)...);
67   }
68 }
69
70 // Check a Linux kernel-style return code (>= 0 on success, negative error
71 // number on error), throw on error.
72 template <class... Args>
73 void checkKernelError(ssize_t ret, Args&&... args) {
74   if (UNLIKELY(ret < 0)) {
75     throwSystemErrorExplicit(-ret, std::forward<Args>(args)...);
76   }
77 }
78
79 // Check a traditional Unix return code (-1 and sets errno on error), throw
80 // on error.
81 template <class... Args>
82 void checkUnixError(ssize_t ret, Args&&... args) {
83   if (UNLIKELY(ret == -1)) {
84     throwSystemError(std::forward<Args>(args)...);
85   }
86 }
87
88 template <class... Args>
89 void checkUnixErrorExplicit(ssize_t ret, int savedErrno, Args&&... args) {
90   if (UNLIKELY(ret == -1)) {
91     throwSystemErrorExplicit(savedErrno, std::forward<Args>(args)...);
92   }
93 }
94
95 // Check the return code from a fopen-style function (returns a non-nullptr
96 // FILE* on success, nullptr on error, sets errno).  Works with fopen, fdopen,
97 // freopen, tmpfile, etc.
98 template <class... Args>
99 void checkFopenError(FILE* fp, Args&&... args) {
100   if (UNLIKELY(!fp)) {
101     throwSystemError(std::forward<Args>(args)...);
102   }
103 }
104
105 template <class... Args>
106 void checkFopenErrorExplicit(FILE* fp, int savedErrno, Args&&... args) {
107   if (UNLIKELY(!fp)) {
108     throwSystemErrorExplicit(savedErrno, std::forward<Args>(args)...);
109   }
110 }
111
112 }  // namespace folly
113
114 #endif /* FOLLY_EXCEPTION_H_ */
115