You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

424 lines
16 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef RTC_BASE_CHECKS_H_
  11. #define RTC_BASE_CHECKS_H_
  12. // If you for some reson need to know if DCHECKs are on, test the value of
  13. // RTC_DCHECK_IS_ON. (Test its value, not if it's defined; it'll always be
  14. // defined, to either a true or a false value.)
  15. #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
  16. #define RTC_DCHECK_IS_ON 1
  17. #else
  18. #define RTC_DCHECK_IS_ON 0
  19. #endif
  20. // Annotate a function that will not return control flow to the caller.
  21. #if defined(_MSC_VER)
  22. #define RTC_NORETURN __declspec(noreturn)
  23. #elif defined(__GNUC__)
  24. #define RTC_NORETURN __attribute__((__noreturn__))
  25. #else
  26. #define RTC_NORETURN
  27. #endif
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. RTC_NORETURN void rtc_FatalMessage(const char* file, int line, const char* msg);
  32. #ifdef __cplusplus
  33. } // extern "C"
  34. #endif
  35. #ifdef __cplusplus
  36. // C++ version.
  37. #include <string>
  38. #include "absl/meta/type_traits.h"
  39. #include "absl/strings/string_view.h"
  40. #include "rtc_base/numerics/safe_compare.h"
  41. #include "rtc_base/system/inline.h"
  42. #include "rtc_base/system/rtc_export.h"
  43. // The macros here print a message to stderr and abort under various
  44. // conditions. All will accept additional stream messages. For example:
  45. // RTC_DCHECK_EQ(foo, bar) << "I'm printed when foo != bar.";
  46. //
  47. // - RTC_CHECK(x) is an assertion that x is always true, and that if it isn't,
  48. // it's better to terminate the process than to continue. During development,
  49. // the reason that it's better to terminate might simply be that the error
  50. // handling code isn't in place yet; in production, the reason might be that
  51. // the author of the code truly believes that x will always be true, but that
  52. // she recognizes that if she is wrong, abrupt and unpleasant process
  53. // termination is still better than carrying on with the assumption violated.
  54. //
  55. // RTC_CHECK always evaluates its argument, so it's OK for x to have side
  56. // effects.
  57. //
  58. // - RTC_DCHECK(x) is the same as RTC_CHECK(x)---an assertion that x is always
  59. // true---except that x will only be evaluated in debug builds; in production
  60. // builds, x is simply assumed to be true. This is useful if evaluating x is
  61. // expensive and the expected cost of failing to detect the violated
  62. // assumption is acceptable. You should not handle cases where a production
  63. // build fails to spot a violated condition, even those that would result in
  64. // crashes. If the code needs to cope with the error, make it cope, but don't
  65. // call RTC_DCHECK; if the condition really can't occur, but you'd sleep
  66. // better at night knowing that the process will suicide instead of carrying
  67. // on in case you were wrong, use RTC_CHECK instead of RTC_DCHECK.
  68. //
  69. // RTC_DCHECK only evaluates its argument in debug builds, so if x has visible
  70. // side effects, you need to write e.g.
  71. // bool w = x; RTC_DCHECK(w);
  72. //
  73. // - RTC_CHECK_EQ, _NE, _GT, ..., and RTC_DCHECK_EQ, _NE, _GT, ... are
  74. // specialized variants of RTC_CHECK and RTC_DCHECK that print prettier
  75. // messages if the condition doesn't hold. Prefer them to raw RTC_CHECK and
  76. // RTC_DCHECK.
  77. //
  78. // - FATAL() aborts unconditionally.
  79. namespace rtc {
  80. namespace webrtc_checks_impl {
  81. enum class CheckArgType : int8_t {
  82. kEnd = 0,
  83. kInt,
  84. kLong,
  85. kLongLong,
  86. kUInt,
  87. kULong,
  88. kULongLong,
  89. kDouble,
  90. kLongDouble,
  91. kCharP,
  92. kStdString,
  93. kStringView,
  94. kVoidP,
  95. // kCheckOp doesn't represent an argument type. Instead, it is sent as the
  96. // first argument from RTC_CHECK_OP to make FatalLog use the next two
  97. // arguments to build the special CHECK_OP error message
  98. // (the "a == b (1 vs. 2)" bit).
  99. kCheckOp,
  100. };
  101. RTC_NORETURN RTC_EXPORT void FatalLog(const char* file,
  102. int line,
  103. const char* message,
  104. const CheckArgType* fmt,
  105. ...);
  106. // Wrapper for log arguments. Only ever make values of this type with the
  107. // MakeVal() functions.
  108. template <CheckArgType N, typename T>
  109. struct Val {
  110. static constexpr CheckArgType Type() { return N; }
  111. T GetVal() const { return val; }
  112. T val;
  113. };
  114. // Case for when we need to construct a temp string and then print that.
  115. // (We can't use Val<CheckArgType::kStdString, const std::string*>
  116. // because we need somewhere to store the temp string.)
  117. struct ToStringVal {
  118. static constexpr CheckArgType Type() { return CheckArgType::kStdString; }
  119. const std::string* GetVal() const { return &val; }
  120. std::string val;
  121. };
  122. inline Val<CheckArgType::kInt, int> MakeVal(int x) {
  123. return {x};
  124. }
  125. inline Val<CheckArgType::kLong, long> MakeVal(long x) {
  126. return {x};
  127. }
  128. inline Val<CheckArgType::kLongLong, long long> MakeVal(long long x) {
  129. return {x};
  130. }
  131. inline Val<CheckArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
  132. return {x};
  133. }
  134. inline Val<CheckArgType::kULong, unsigned long> MakeVal(unsigned long x) {
  135. return {x};
  136. }
  137. inline Val<CheckArgType::kULongLong, unsigned long long> MakeVal(
  138. unsigned long long x) {
  139. return {x};
  140. }
  141. inline Val<CheckArgType::kDouble, double> MakeVal(double x) {
  142. return {x};
  143. }
  144. inline Val<CheckArgType::kLongDouble, long double> MakeVal(long double x) {
  145. return {x};
  146. }
  147. inline Val<CheckArgType::kCharP, const char*> MakeVal(const char* x) {
  148. return {x};
  149. }
  150. inline Val<CheckArgType::kStdString, const std::string*> MakeVal(
  151. const std::string& x) {
  152. return {&x};
  153. }
  154. inline Val<CheckArgType::kStringView, const absl::string_view*> MakeVal(
  155. const absl::string_view& x) {
  156. return {&x};
  157. }
  158. inline Val<CheckArgType::kVoidP, const void*> MakeVal(const void* x) {
  159. return {x};
  160. }
  161. // The enum class types are not implicitly convertible to arithmetic types.
  162. template <typename T,
  163. absl::enable_if_t<std::is_enum<T>::value &&
  164. !std::is_arithmetic<T>::value>* = nullptr>
  165. inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal(
  166. T x) {
  167. return {static_cast<absl::underlying_type_t<T>>(x)};
  168. }
  169. template <typename T, decltype(ToLogString(std::declval<T>()))* = nullptr>
  170. ToStringVal MakeVal(const T& x) {
  171. return {ToLogString(x)};
  172. }
  173. // Ephemeral type that represents the result of the logging << operator.
  174. template <typename... Ts>
  175. class LogStreamer;
  176. // Base case: Before the first << argument.
  177. template <>
  178. class LogStreamer<> final {
  179. public:
  180. template <typename U,
  181. typename V = decltype(MakeVal(std::declval<U>())),
  182. absl::enable_if_t<std::is_arithmetic<U>::value ||
  183. std::is_enum<U>::value>* = nullptr>
  184. RTC_FORCE_INLINE LogStreamer<V> operator<<(U arg) const {
  185. return LogStreamer<V>(MakeVal(arg), this);
  186. }
  187. template <typename U,
  188. typename V = decltype(MakeVal(std::declval<U>())),
  189. absl::enable_if_t<!std::is_arithmetic<U>::value &&
  190. !std::is_enum<U>::value>* = nullptr>
  191. RTC_FORCE_INLINE LogStreamer<V> operator<<(const U& arg) const {
  192. return LogStreamer<V>(MakeVal(arg), this);
  193. }
  194. template <typename... Us>
  195. RTC_NORETURN RTC_FORCE_INLINE static void Call(const char* file,
  196. const int line,
  197. const char* message,
  198. const Us&... args) {
  199. static constexpr CheckArgType t[] = {Us::Type()..., CheckArgType::kEnd};
  200. FatalLog(file, line, message, t, args.GetVal()...);
  201. }
  202. template <typename... Us>
  203. RTC_NORETURN RTC_FORCE_INLINE static void CallCheckOp(const char* file,
  204. const int line,
  205. const char* message,
  206. const Us&... args) {
  207. static constexpr CheckArgType t[] = {CheckArgType::kCheckOp, Us::Type()...,
  208. CheckArgType::kEnd};
  209. FatalLog(file, line, message, t, args.GetVal()...);
  210. }
  211. };
  212. // Inductive case: We've already seen at least one << argument. The most recent
  213. // one had type `T`, and the earlier ones had types `Ts`.
  214. template <typename T, typename... Ts>
  215. class LogStreamer<T, Ts...> final {
  216. public:
  217. RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
  218. : arg_(arg), prior_(prior) {}
  219. template <typename U,
  220. typename V = decltype(MakeVal(std::declval<U>())),
  221. absl::enable_if_t<std::is_arithmetic<U>::value ||
  222. std::is_enum<U>::value>* = nullptr>
  223. RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(U arg) const {
  224. return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
  225. }
  226. template <typename U,
  227. typename V = decltype(MakeVal(std::declval<U>())),
  228. absl::enable_if_t<!std::is_arithmetic<U>::value &&
  229. !std::is_enum<U>::value>* = nullptr>
  230. RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(const U& arg) const {
  231. return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
  232. }
  233. template <typename... Us>
  234. RTC_NORETURN RTC_FORCE_INLINE void Call(const char* file,
  235. const int line,
  236. const char* message,
  237. const Us&... args) const {
  238. prior_->Call(file, line, message, arg_, args...);
  239. }
  240. template <typename... Us>
  241. RTC_NORETURN RTC_FORCE_INLINE void CallCheckOp(const char* file,
  242. const int line,
  243. const char* message,
  244. const Us&... args) const {
  245. prior_->CallCheckOp(file, line, message, arg_, args...);
  246. }
  247. private:
  248. // The most recent argument.
  249. T arg_;
  250. // Earlier arguments.
  251. const LogStreamer<Ts...>* prior_;
  252. };
  253. template <bool isCheckOp>
  254. class FatalLogCall final {
  255. public:
  256. FatalLogCall(const char* file, int line, const char* message)
  257. : file_(file), line_(line), message_(message) {}
  258. // This can be any binary operator with precedence lower than <<.
  259. template <typename... Ts>
  260. RTC_NORETURN RTC_FORCE_INLINE void operator&(
  261. const LogStreamer<Ts...>& streamer) {
  262. isCheckOp ? streamer.CallCheckOp(file_, line_, message_)
  263. : streamer.Call(file_, line_, message_);
  264. }
  265. private:
  266. const char* file_;
  267. int line_;
  268. const char* message_;
  269. };
  270. } // namespace webrtc_checks_impl
  271. // The actual stream used isn't important. We reference |ignored| in the code
  272. // but don't evaluate it; this is to avoid "unused variable" warnings (we do so
  273. // in a particularly convoluted way with an extra ?: because that appears to be
  274. // the simplest construct that keeps Visual Studio from complaining about
  275. // condition being unused).
  276. #define RTC_EAT_STREAM_PARAMETERS(ignored) \
  277. (true ? true : ((void)(ignored), true)) \
  278. ? static_cast<void>(0) \
  279. : rtc::webrtc_checks_impl::FatalLogCall<false>("", 0, "") & \
  280. rtc::webrtc_checks_impl::LogStreamer<>()
  281. // Call RTC_EAT_STREAM_PARAMETERS with an argument that fails to compile if
  282. // values of the same types as |a| and |b| can't be compared with the given
  283. // operation, and that would evaluate |a| and |b| if evaluated.
  284. #define RTC_EAT_STREAM_PARAMETERS_OP(op, a, b) \
  285. RTC_EAT_STREAM_PARAMETERS(((void)rtc::Safe##op(a, b)))
  286. // RTC_CHECK dies with a fatal error if condition is not true. It is *not*
  287. // controlled by NDEBUG or anything else, so the check will be executed
  288. // regardless of compilation mode.
  289. //
  290. // We make sure RTC_CHECK et al. always evaluates |condition|, as
  291. // doing RTC_CHECK(FunctionWithSideEffect()) is a common idiom.
  292. #define RTC_CHECK(condition) \
  293. while (!(condition)) \
  294. rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, \
  295. #condition) & \
  296. rtc::webrtc_checks_impl::LogStreamer<>()
  297. // Helper macro for binary operators.
  298. // Don't use this macro directly in your code, use RTC_CHECK_EQ et al below.
  299. #define RTC_CHECK_OP(name, op, val1, val2) \
  300. while (!rtc::Safe##name((val1), (val2))) \
  301. rtc::webrtc_checks_impl::FatalLogCall<true>(__FILE__, __LINE__, \
  302. #val1 " " #op " " #val2) & \
  303. rtc::webrtc_checks_impl::LogStreamer<>() << (val1) << (val2)
  304. #define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(Eq, ==, val1, val2)
  305. #define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(Ne, !=, val1, val2)
  306. #define RTC_CHECK_LE(val1, val2) RTC_CHECK_OP(Le, <=, val1, val2)
  307. #define RTC_CHECK_LT(val1, val2) RTC_CHECK_OP(Lt, <, val1, val2)
  308. #define RTC_CHECK_GE(val1, val2) RTC_CHECK_OP(Ge, >=, val1, val2)
  309. #define RTC_CHECK_GT(val1, val2) RTC_CHECK_OP(Gt, >, val1, val2)
  310. // The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates
  311. // code in debug builds. It does reference the condition parameter in all cases,
  312. // though, so callers won't risk getting warnings about unused variables.
  313. #if RTC_DCHECK_IS_ON
  314. #define RTC_DCHECK(condition) RTC_CHECK(condition)
  315. #define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2)
  316. #define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2)
  317. #define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2)
  318. #define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2)
  319. #define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2)
  320. #define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2)
  321. #else
  322. #define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition)
  323. #define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Eq, v1, v2)
  324. #define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ne, v1, v2)
  325. #define RTC_DCHECK_LE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Le, v1, v2)
  326. #define RTC_DCHECK_LT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Lt, v1, v2)
  327. #define RTC_DCHECK_GE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ge, v1, v2)
  328. #define RTC_DCHECK_GT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Gt, v1, v2)
  329. #endif
  330. #define RTC_UNREACHABLE_CODE_HIT false
  331. #define RTC_NOTREACHED() RTC_DCHECK(RTC_UNREACHABLE_CODE_HIT)
  332. // TODO(bugs.webrtc.org/8454): Add an RTC_ prefix or rename differently.
  333. #define FATAL() \
  334. rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, \
  335. "FATAL()") & \
  336. rtc::webrtc_checks_impl::LogStreamer<>()
  337. // Performs the integer division a/b and returns the result. CHECKs that the
  338. // remainder is zero.
  339. template <typename T>
  340. inline T CheckedDivExact(T a, T b) {
  341. RTC_CHECK_EQ(a % b, 0) << a << " is not evenly divisible by " << b;
  342. return a / b;
  343. }
  344. } // namespace rtc
  345. #else // __cplusplus not defined
  346. // C version. Lacks many features compared to the C++ version, but usage
  347. // guidelines are the same.
  348. #define RTC_CHECK(condition) \
  349. do { \
  350. if (!(condition)) { \
  351. rtc_FatalMessage(__FILE__, __LINE__, "CHECK failed: " #condition); \
  352. } \
  353. } while (0)
  354. #define RTC_CHECK_EQ(a, b) RTC_CHECK((a) == (b))
  355. #define RTC_CHECK_NE(a, b) RTC_CHECK((a) != (b))
  356. #define RTC_CHECK_LE(a, b) RTC_CHECK((a) <= (b))
  357. #define RTC_CHECK_LT(a, b) RTC_CHECK((a) < (b))
  358. #define RTC_CHECK_GE(a, b) RTC_CHECK((a) >= (b))
  359. #define RTC_CHECK_GT(a, b) RTC_CHECK((a) > (b))
  360. #define RTC_DCHECK(condition) \
  361. do { \
  362. if (RTC_DCHECK_IS_ON && !(condition)) { \
  363. rtc_FatalMessage(__FILE__, __LINE__, "DCHECK failed: " #condition); \
  364. } \
  365. } while (0)
  366. #define RTC_DCHECK_EQ(a, b) RTC_DCHECK((a) == (b))
  367. #define RTC_DCHECK_NE(a, b) RTC_DCHECK((a) != (b))
  368. #define RTC_DCHECK_LE(a, b) RTC_DCHECK((a) <= (b))
  369. #define RTC_DCHECK_LT(a, b) RTC_DCHECK((a) < (b))
  370. #define RTC_DCHECK_GE(a, b) RTC_DCHECK((a) >= (b))
  371. #define RTC_DCHECK_GT(a, b) RTC_DCHECK((a) > (b))
  372. #endif // __cplusplus
  373. #endif // RTC_BASE_CHECKS_H_