1 /*
2 * Copyright 2006 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #ifndef RTC_BASE_CHECKS_H_
12 #define RTC_BASE_CHECKS_H_
13
14 // If you for some reson need to know if DCHECKs are on, test the value of
15 // RTC_DCHECK_IS_ON. (Test its value, not if it's defined; it'll always be
16 // defined, to either a true or a false value.)
17 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
18 #define RTC_DCHECK_IS_ON 1
19 #else
20 #define RTC_DCHECK_IS_ON 0
21 #endif
22
23 // Annotate a function that will not return control flow to the caller.
24 #if defined(_MSC_VER)
25 #define RTC_NORETURN __declspec(noreturn)
26 #elif defined(__GNUC__)
27 #define RTC_NORETURN __attribute__((__noreturn__))
28 #else
29 #define RTC_NORETURN
30 #endif
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 RTC_NORETURN void rtc_FatalMessage(const char* file, int line, const char* msg);
36 #ifdef __cplusplus
37 } // extern "C"
38 #endif
39
40 #ifdef RTC_DISABLE_CHECK_MSG
41 #define RTC_CHECK_MSG_ENABLED 0
42 #else
43 #define RTC_CHECK_MSG_ENABLED 1
44 #endif
45
46 #if RTC_CHECK_MSG_ENABLED
47 #define RTC_CHECK_EVAL_MESSAGE(message) message
48 #else
49 #define RTC_CHECK_EVAL_MESSAGE(message) ""
50 #endif
51
52 #ifdef __cplusplus
53 // C++ version.
54
55 #include <string>
56
57 #include "absl/meta/type_traits.h"
58 #include "absl/strings/string_view.h"
59 #include "api/scoped_refptr.h"
60 #include "rtc_base/numerics/safe_compare.h"
61 #include "rtc_base/system/inline.h"
62 #include "rtc_base/system/rtc_export.h"
63
64 // The macros here print a message to stderr and abort under various
65 // conditions. All will accept additional stream messages. For example:
66 // RTC_DCHECK_EQ(foo, bar) << "I'm printed when foo != bar.";
67 //
68 // - RTC_CHECK(x) is an assertion that x is always true, and that if it isn't,
69 // it's better to terminate the process than to continue. During development,
70 // the reason that it's better to terminate might simply be that the error
71 // handling code isn't in place yet; in production, the reason might be that
72 // the author of the code truly believes that x will always be true, but that
73 // they recognizes that if they are wrong, abrupt and unpleasant process
74 // termination is still better than carrying on with the assumption violated.
75 //
76 // RTC_CHECK always evaluates its argument, so it's OK for x to have side
77 // effects.
78 //
79 // - RTC_DCHECK(x) is the same as RTC_CHECK(x)---an assertion that x is always
80 // true---except that x will only be evaluated in debug builds; in production
81 // builds, x is simply assumed to be true. This is useful if evaluating x is
82 // expensive and the expected cost of failing to detect the violated
83 // assumption is acceptable. You should not handle cases where a production
84 // build fails to spot a violated condition, even those that would result in
85 // crashes. If the code needs to cope with the error, make it cope, but don't
86 // call RTC_DCHECK; if the condition really can't occur, but you'd sleep
87 // better at night knowing that the process will suicide instead of carrying
88 // on in case you were wrong, use RTC_CHECK instead of RTC_DCHECK.
89 //
90 // RTC_DCHECK only evaluates its argument in debug builds, so if x has visible
91 // side effects, you need to write e.g.
92 // bool w = x; RTC_DCHECK(w);
93 //
94 // - RTC_CHECK_EQ, _NE, _GT, ..., and RTC_DCHECK_EQ, _NE, _GT, ... are
95 // specialized variants of RTC_CHECK and RTC_DCHECK that print prettier
96 // messages if the condition doesn't hold. Prefer them to raw RTC_CHECK and
97 // RTC_DCHECK.
98 //
99 // - RTC_FATAL() aborts unconditionally.
100
101 namespace rtc {
102 namespace webrtc_checks_impl {
103 enum class CheckArgType : int8_t {
104 kEnd = 0,
105 kInt,
106 kLong,
107 kLongLong,
108 kUInt,
109 kULong,
110 kULongLong,
111 kDouble,
112 kLongDouble,
113 kCharP,
114 kStdString,
115 kStringView,
116 kVoidP,
117
118 // kCheckOp doesn't represent an argument type. Instead, it is sent as the
119 // first argument from RTC_CHECK_OP to make FatalLog use the next two
120 // arguments to build the special CHECK_OP error message
121 // (the "a == b (1 vs. 2)" bit).
122 kCheckOp,
123 };
124
125 // These two functions are public so they can be overridden from
126 // webrtc_overrides in chromium.
127 RTC_NORETURN void WriteFatalLog(const char* file,
128 int line,
129 absl::string_view output);
130 RTC_NORETURN void WriteFatalLog(absl::string_view output);
131
132 #if RTC_CHECK_MSG_ENABLED
133 RTC_NORETURN RTC_EXPORT void FatalLog(const char* file,
134 int line,
135 const char* message,
136 const CheckArgType* fmt,
137 ...);
138 #else
139 RTC_NORETURN RTC_EXPORT void FatalLog(const char* file, int line);
140 #endif
141
142 // Wrapper for log arguments. Only ever make values of this type with the
143 // MakeVal() functions.
144 template <CheckArgType N, typename T>
145 struct Val {
TypeVal146 static constexpr CheckArgType Type() { return N; }
GetValVal147 T GetVal() const { return val; }
148 T val;
149 };
150
151 // Case for when we need to construct a temp string and then print that.
152 // (We can't use Val<CheckArgType::kStdString, const std::string*>
153 // because we need somewhere to store the temp string.)
154 struct ToStringVal {
TypeToStringVal155 static constexpr CheckArgType Type() { return CheckArgType::kStdString; }
GetValToStringVal156 const std::string* GetVal() const { return &val; }
157 std::string val;
158 };
159
MakeVal(int x)160 inline Val<CheckArgType::kInt, int> MakeVal(int x) {
161 return {x};
162 }
MakeVal(long x)163 inline Val<CheckArgType::kLong, long> MakeVal(long x) {
164 return {x};
165 }
MakeVal(long long x)166 inline Val<CheckArgType::kLongLong, long long> MakeVal(long long x) {
167 return {x};
168 }
MakeVal(unsigned int x)169 inline Val<CheckArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
170 return {x};
171 }
MakeVal(unsigned long x)172 inline Val<CheckArgType::kULong, unsigned long> MakeVal(unsigned long x) {
173 return {x};
174 }
MakeVal(unsigned long long x)175 inline Val<CheckArgType::kULongLong, unsigned long long> MakeVal(
176 unsigned long long x) {
177 return {x};
178 }
179
MakeVal(double x)180 inline Val<CheckArgType::kDouble, double> MakeVal(double x) {
181 return {x};
182 }
MakeVal(long double x)183 inline Val<CheckArgType::kLongDouble, long double> MakeVal(long double x) {
184 return {x};
185 }
186
MakeVal(const char * x)187 inline Val<CheckArgType::kCharP, const char*> MakeVal(const char* x) {
188 return {x};
189 }
MakeVal(const std::string & x)190 inline Val<CheckArgType::kStdString, const std::string*> MakeVal(
191 const std::string& x) {
192 return {&x};
193 }
MakeVal(const absl::string_view & x)194 inline Val<CheckArgType::kStringView, const absl::string_view*> MakeVal(
195 const absl::string_view& x) {
196 return {&x};
197 }
198
MakeVal(const void * x)199 inline Val<CheckArgType::kVoidP, const void*> MakeVal(const void* x) {
200 return {x};
201 }
202
203 template <typename T>
MakeVal(const rtc::scoped_refptr<T> & p)204 inline Val<CheckArgType::kVoidP, const void*> MakeVal(
205 const rtc::scoped_refptr<T>& p) {
206 return {p.get()};
207 }
208
209 // The enum class types are not implicitly convertible to arithmetic types.
210 template <typename T,
211 absl::enable_if_t<std::is_enum<T>::value &&
212 !std::is_arithmetic<T>::value>* = nullptr>
decltype(MakeVal (std::declval<absl::underlying_type_t<T>> ()))213 inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal(
214 T x) {
215 return {static_cast<absl::underlying_type_t<T>>(x)};
216 }
217
218 template <typename T, decltype(ToLogString(std::declval<T>()))* = nullptr>
MakeVal(const T & x)219 ToStringVal MakeVal(const T& x) {
220 return {ToLogString(x)};
221 }
222
223 // Ephemeral type that represents the result of the logging << operator.
224 template <typename... Ts>
225 class LogStreamer;
226
227 // Base case: Before the first << argument.
228 template <>
229 class LogStreamer<> final {
230 public:
231 template <typename U,
232 typename V = decltype(MakeVal(std::declval<U>())),
233 absl::enable_if_t<std::is_arithmetic<U>::value ||
234 std::is_enum<U>::value>* = nullptr>
235 RTC_FORCE_INLINE LogStreamer<V> operator<<(U arg) const {
236 return LogStreamer<V>(MakeVal(arg), this);
237 }
238
239 template <typename U,
240 typename V = decltype(MakeVal(std::declval<U>())),
241 absl::enable_if_t<!std::is_arithmetic<U>::value &&
242 !std::is_enum<U>::value>* = nullptr>
243 RTC_FORCE_INLINE LogStreamer<V> operator<<(const U& arg) const {
244 return LogStreamer<V>(MakeVal(arg), this);
245 }
246
247 #if RTC_CHECK_MSG_ENABLED
248 template <typename... Us>
Call(const char * file,const int line,const char * message,const Us &...args)249 RTC_NORETURN RTC_FORCE_INLINE static void Call(const char* file,
250 const int line,
251 const char* message,
252 const Us&... args) {
253 static constexpr CheckArgType t[] = {Us::Type()..., CheckArgType::kEnd};
254 FatalLog(file, line, message, t, args.GetVal()...);
255 }
256
257 template <typename... Us>
CallCheckOp(const char * file,const int line,const char * message,const Us &...args)258 RTC_NORETURN RTC_FORCE_INLINE static void CallCheckOp(const char* file,
259 const int line,
260 const char* message,
261 const Us&... args) {
262 static constexpr CheckArgType t[] = {CheckArgType::kCheckOp, Us::Type()...,
263 CheckArgType::kEnd};
264 FatalLog(file, line, message, t, args.GetVal()...);
265 }
266 #else
267 template <typename... Us>
Call(const char * file,const int line)268 RTC_NORETURN RTC_FORCE_INLINE static void Call(const char* file,
269 const int line) {
270 FatalLog(file, line);
271 }
272 #endif
273 };
274
275 // Inductive case: We've already seen at least one << argument. The most recent
276 // one had type `T`, and the earlier ones had types `Ts`.
277 template <typename T, typename... Ts>
278 class LogStreamer<T, Ts...> final {
279 public:
LogStreamer(T arg,const LogStreamer<Ts...> * prior)280 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
281 : arg_(arg), prior_(prior) {}
282
283 template <typename U,
284 typename V = decltype(MakeVal(std::declval<U>())),
285 absl::enable_if_t<std::is_arithmetic<U>::value ||
286 std::is_enum<U>::value>* = nullptr>
287 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(U arg) const {
288 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
289 }
290
291 template <typename U,
292 typename V = decltype(MakeVal(std::declval<U>())),
293 absl::enable_if_t<!std::is_arithmetic<U>::value &&
294 !std::is_enum<U>::value>* = nullptr>
295 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(const U& arg) const {
296 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
297 }
298
299 #if RTC_CHECK_MSG_ENABLED
300 template <typename... Us>
Call(const char * file,const int line,const char * message,const Us &...args)301 RTC_NORETURN RTC_FORCE_INLINE void Call(const char* file,
302 const int line,
303 const char* message,
304 const Us&... args) const {
305 prior_->Call(file, line, message, arg_, args...);
306 }
307
308 template <typename... Us>
CallCheckOp(const char * file,const int line,const char * message,const Us &...args)309 RTC_NORETURN RTC_FORCE_INLINE void CallCheckOp(const char* file,
310 const int line,
311 const char* message,
312 const Us&... args) const {
313 prior_->CallCheckOp(file, line, message, arg_, args...);
314 }
315 #else
316 template <typename... Us>
Call(const char * file,const int line)317 RTC_NORETURN RTC_FORCE_INLINE void Call(const char* file,
318 const int line) const {
319 prior_->Call(file, line);
320 }
321 #endif
322
323 private:
324 // The most recent argument.
325 T arg_;
326
327 // Earlier arguments.
328 const LogStreamer<Ts...>* prior_;
329 };
330
331 template <bool isCheckOp>
332 class FatalLogCall final {
333 public:
FatalLogCall(const char * file,int line,const char * message)334 FatalLogCall(const char* file, int line, const char* message)
335 : file_(file), line_(line), message_(message) {}
336
337 // This can be any binary operator with precedence lower than <<.
338 template <typename... Ts>
339 RTC_NORETURN RTC_FORCE_INLINE void operator&(
340 const LogStreamer<Ts...>& streamer) {
341 #if RTC_CHECK_MSG_ENABLED
342 isCheckOp ? streamer.CallCheckOp(file_, line_, message_)
343 : streamer.Call(file_, line_, message_);
344 #else
345 streamer.Call(file_, line_);
346 #endif
347 }
348
349 private:
350 const char* file_;
351 int line_;
352 const char* message_;
353 };
354
355 #if RTC_DCHECK_IS_ON
356
357 // Be helpful, and include file and line in the RTC_CHECK_NOTREACHED error
358 // message.
359 #define RTC_UNREACHABLE_FILE_AND_LINE_CALL_ARGS __FILE__, __LINE__
360 RTC_NORETURN RTC_EXPORT void UnreachableCodeReached(const char* file, int line);
361
362 #else
363
364 // Be mindful of binary size, and don't include file and line in the
365 // RTC_CHECK_NOTREACHED error message.
366 #define RTC_UNREACHABLE_FILE_AND_LINE_CALL_ARGS
367 RTC_NORETURN RTC_EXPORT void UnreachableCodeReached();
368
369 #endif
370
371 } // namespace webrtc_checks_impl
372
373 // The actual stream used isn't important. We reference `ignored` in the code
374 // but don't evaluate it; this is to avoid "unused variable" warnings (we do so
375 // in a particularly convoluted way with an extra ?: because that appears to be
376 // the simplest construct that keeps Visual Studio from complaining about
377 // condition being unused).
378 #define RTC_EAT_STREAM_PARAMETERS(ignored) \
379 (true ? true : ((void)(ignored), true)) \
380 ? static_cast<void>(0) \
381 : ::rtc::webrtc_checks_impl::FatalLogCall<false>("", 0, "") & \
382 ::rtc::webrtc_checks_impl::LogStreamer<>()
383
384 // Call RTC_EAT_STREAM_PARAMETERS with an argument that fails to compile if
385 // values of the same types as `a` and `b` can't be compared with the given
386 // operation, and that would evaluate `a` and `b` if evaluated.
387 #define RTC_EAT_STREAM_PARAMETERS_OP(op, a, b) \
388 RTC_EAT_STREAM_PARAMETERS(((void)::rtc::Safe##op(a, b)))
389
390 // RTC_CHECK dies with a fatal error if condition is not true. It is *not*
391 // controlled by NDEBUG or anything else, so the check will be executed
392 // regardless of compilation mode.
393 //
394 // We make sure RTC_CHECK et al. always evaluates `condition`, as
395 // doing RTC_CHECK(FunctionWithSideEffect()) is a common idiom.
396 //
397 // RTC_CHECK_OP is a helper macro for binary operators.
398 // Don't use this macro directly in your code, use RTC_CHECK_EQ et al below.
399 #if RTC_CHECK_MSG_ENABLED
400 #define RTC_CHECK(condition) \
401 (condition) ? static_cast<void>(0) \
402 : ::rtc::webrtc_checks_impl::FatalLogCall<false>( \
403 __FILE__, __LINE__, #condition) & \
404 ::rtc::webrtc_checks_impl::LogStreamer<>()
405
406 #define RTC_CHECK_OP(name, op, val1, val2) \
407 ::rtc::Safe##name((val1), (val2)) \
408 ? static_cast<void>(0) \
409 : ::rtc::webrtc_checks_impl::FatalLogCall<true>( \
410 __FILE__, __LINE__, #val1 " " #op " " #val2) & \
411 ::rtc::webrtc_checks_impl::LogStreamer<>() << (val1) << (val2)
412 #else
413 #define RTC_CHECK(condition) \
414 (condition) \
415 ? static_cast<void>(0) \
416 : true ? ::rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, \
417 __LINE__, "") & \
418 ::rtc::webrtc_checks_impl::LogStreamer<>() \
419 : ::rtc::webrtc_checks_impl::FatalLogCall<false>("", 0, "") & \
420 ::rtc::webrtc_checks_impl::LogStreamer<>()
421
422 #define RTC_CHECK_OP(name, op, val1, val2) \
423 ::rtc::Safe##name((val1), (val2)) \
424 ? static_cast<void>(0) \
425 : true ? ::rtc::webrtc_checks_impl::FatalLogCall<true>(__FILE__, \
426 __LINE__, "") & \
427 ::rtc::webrtc_checks_impl::LogStreamer<>() \
428 : ::rtc::webrtc_checks_impl::FatalLogCall<false>("", 0, "") & \
429 ::rtc::webrtc_checks_impl::LogStreamer<>()
430 #endif
431
432 #define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(Eq, ==, val1, val2)
433 #define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(Ne, !=, val1, val2)
434 #define RTC_CHECK_LE(val1, val2) RTC_CHECK_OP(Le, <=, val1, val2)
435 #define RTC_CHECK_LT(val1, val2) RTC_CHECK_OP(Lt, <, val1, val2)
436 #define RTC_CHECK_GE(val1, val2) RTC_CHECK_OP(Ge, >=, val1, val2)
437 #define RTC_CHECK_GT(val1, val2) RTC_CHECK_OP(Gt, >, val1, val2)
438
439 // The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates
440 // code in debug builds. It does reference the condition parameter in all cases,
441 // though, so callers won't risk getting warnings about unused variables.
442 #if RTC_DCHECK_IS_ON
443 #define RTC_DCHECK(condition) RTC_CHECK(condition)
444 #define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2)
445 #define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2)
446 #define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2)
447 #define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2)
448 #define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2)
449 #define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2)
450 #else
451 #define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition)
452 #define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Eq, v1, v2)
453 #define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ne, v1, v2)
454 #define RTC_DCHECK_LE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Le, v1, v2)
455 #define RTC_DCHECK_LT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Lt, v1, v2)
456 #define RTC_DCHECK_GE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ge, v1, v2)
457 #define RTC_DCHECK_GT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Gt, v1, v2)
458 #endif
459
460 #define RTC_UNREACHABLE_CODE_HIT false
461 #define RTC_DCHECK_NOTREACHED() RTC_DCHECK(RTC_UNREACHABLE_CODE_HIT)
462
463 // Kills the process with an error message. Never returns. Use when you wish to
464 // assert that a point in the code is never reached.
465 #define RTC_CHECK_NOTREACHED() \
466 do { \
467 ::rtc::webrtc_checks_impl::UnreachableCodeReached( \
468 RTC_UNREACHABLE_FILE_AND_LINE_CALL_ARGS); \
469 } while (0)
470
471 #define RTC_FATAL() \
472 ::rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, \
473 "FATAL()") & \
474 ::rtc::webrtc_checks_impl::LogStreamer<>()
475
476 // Performs the integer division a/b and returns the result. CHECKs that the
477 // remainder is zero.
478 template <typename T>
CheckedDivExact(T a,T b)479 inline T CheckedDivExact(T a, T b) {
480 RTC_CHECK_EQ(a % b, 0) << a << " is not evenly divisible by " << b;
481 return a / b;
482 }
483
484 } // namespace rtc
485
486 #else // __cplusplus not defined
487 // C version. Lacks many features compared to the C++ version, but usage
488 // guidelines are the same.
489
490 #define RTC_CHECK(condition) \
491 do { \
492 if (!(condition)) { \
493 rtc_FatalMessage(__FILE__, __LINE__, \
494 RTC_CHECK_EVAL_MESSAGE("CHECK failed: " #condition)); \
495 } \
496 } while (0)
497
498 #define RTC_CHECK_EQ(a, b) RTC_CHECK((a) == (b))
499 #define RTC_CHECK_NE(a, b) RTC_CHECK((a) != (b))
500 #define RTC_CHECK_LE(a, b) RTC_CHECK((a) <= (b))
501 #define RTC_CHECK_LT(a, b) RTC_CHECK((a) < (b))
502 #define RTC_CHECK_GE(a, b) RTC_CHECK((a) >= (b))
503 #define RTC_CHECK_GT(a, b) RTC_CHECK((a) > (b))
504
505 #define RTC_DCHECK(condition) \
506 do { \
507 if (RTC_DCHECK_IS_ON && !(condition)) { \
508 rtc_FatalMessage(__FILE__, __LINE__, \
509 RTC_CHECK_EVAL_MESSAGE("DCHECK failed: " #condition)); \
510 } \
511 } while (0)
512
513 #define RTC_DCHECK_EQ(a, b) RTC_DCHECK((a) == (b))
514 #define RTC_DCHECK_NE(a, b) RTC_DCHECK((a) != (b))
515 #define RTC_DCHECK_LE(a, b) RTC_DCHECK((a) <= (b))
516 #define RTC_DCHECK_LT(a, b) RTC_DCHECK((a) < (b))
517 #define RTC_DCHECK_GE(a, b) RTC_DCHECK((a) >= (b))
518 #define RTC_DCHECK_GT(a, b) RTC_DCHECK((a) > (b))
519
520 #endif // __cplusplus
521
522 #endif // RTC_BASE_CHECKS_H_
523