util_bug.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file util_bug.h
  7. **/
  8. #ifndef TOR_UTIL_BUG_H
  9. #define TOR_UTIL_BUG_H
  10. #include "orconfig.h"
  11. #include "compat.h"
  12. #include "testsupport.h"
  13. /* Replace assert() with a variant that sends failures to the log before
  14. * calling assert() normally.
  15. */
  16. #ifdef NDEBUG
  17. /* Nobody should ever want to build with NDEBUG set. 99% of our asserts will
  18. * be outside the critical path anyway, so it's silly to disable bug-checking
  19. * throughout the entire program just because a few asserts are slowing you
  20. * down. Profile, optimize the critical path, and keep debugging on.
  21. *
  22. * And I'm not just saying that because some of our asserts check
  23. * security-critical properties.
  24. */
  25. #error "Sorry; we don't support building with NDEBUG."
  26. #endif
  27. /* Sometimes we don't want to use assertions during branch coverage tests; it
  28. * leads to tons of unreached branches which in reality are only assertions we
  29. * didn't hit. */
  30. #if defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS)
  31. #define tor_assert(a) STMT_BEGIN \
  32. (void)(a); \
  33. STMT_END
  34. #else
  35. /** Like assert(3), but send assertion failures to the log as well as to
  36. * stderr. */
  37. #define tor_assert(expr) STMT_BEGIN \
  38. if (PREDICT_UNLIKELY(!(expr))) { \
  39. tor_assertion_failed_(SHORT_FILE__, __LINE__, __func__, #expr); \
  40. abort(); \
  41. } STMT_END
  42. #endif
  43. #define tor_assert_unreached() tor_assert(0)
  44. /* Non-fatal bug assertions. The "unreached" variants mean "this line should
  45. * never be reached." The "once" variants mean "Don't log a warning more than
  46. * once".
  47. *
  48. * The 'BUG' macro checks a boolean condition and logs an error message if it
  49. * is true. Example usage:
  50. * if (BUG(x == NULL))
  51. * return -1;
  52. */
  53. #ifdef __COVERITY__
  54. #undef BUG
  55. // Coverity defines this in global headers; let's override it. This is a
  56. // magic coverity-only preprocessor thing.
  57. #nodef BUG(x) ((x)?(__coverity_panic__(),1):0)
  58. #endif
  59. #if defined(__COVERITY__) || defined(__clang_analyzer__)
  60. // We're running with a static analysis tool: let's treat even nonfatal
  61. // assertion failures as something that we need to avoid.
  62. #define ALL_BUGS_ARE_FATAL
  63. #endif
  64. #ifdef ALL_BUGS_ARE_FATAL
  65. #define tor_assert_nonfatal_unreached() tor_assert(0)
  66. #define tor_assert_nonfatal(cond) tor_assert((cond))
  67. #define tor_assert_nonfatal_unreached_once() tor_assert(0)
  68. #define tor_assert_nonfatal_once(cond) tor_assert((cond))
  69. #define BUG(cond) \
  70. (PREDICT_UNLIKELY(cond) ? \
  71. (tor_assertion_failed_(SHORT_FILE__,__LINE__,__func__,"!("#cond")"), \
  72. abort(), 1) \
  73. : 0)
  74. #elif defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS)
  75. #define tor_assert_nonfatal_unreached() STMT_NIL
  76. #define tor_assert_nonfatal(cond) ((void)(cond))
  77. #define tor_assert_nonfatal_unreached_once() STMT_NIL
  78. #define tor_assert_nonfatal_once(cond) ((void)(cond))
  79. #define BUG(cond) (PREDICT_UNLIKELY(cond) ? 1 : 0)
  80. #else /* Normal case, !ALL_BUGS_ARE_FATAL, !DISABLE_ASSERTS_IN_UNIT_TESTS */
  81. #define tor_assert_nonfatal_unreached() STMT_BEGIN \
  82. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 0); \
  83. STMT_END
  84. #define tor_assert_nonfatal(cond) STMT_BEGIN \
  85. if (PREDICT_UNLIKELY(!(cond))) { \
  86. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 0); \
  87. } \
  88. STMT_END
  89. #define tor_assert_nonfatal_unreached_once() STMT_BEGIN \
  90. static int warning_logged__ = 0; \
  91. if (!warning_logged__) { \
  92. warning_logged__ = 1; \
  93. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 1); \
  94. } \
  95. STMT_END
  96. #define tor_assert_nonfatal_once(cond) STMT_BEGIN \
  97. static int warning_logged__ = 0; \
  98. if (!warning_logged__ && PREDICT_UNLIKELY(!(cond))) { \
  99. warning_logged__ = 1; \
  100. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1); \
  101. } \
  102. STMT_END
  103. #define BUG(cond) \
  104. (PREDICT_UNLIKELY(cond) ? \
  105. (tor_bug_occurred_(SHORT_FILE__,__LINE__,__func__,"!("#cond")",0), 1) \
  106. : 0)
  107. #endif
  108. #ifdef __GNUC__
  109. #define IF_BUG_ONCE__(cond,var) \
  110. if (( { \
  111. static int var = 0; \
  112. int bool_result = (cond); \
  113. if (PREDICT_UNLIKELY(bool_result) && !var) { \
  114. var = 1; \
  115. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
  116. "!("#cond")", 1); \
  117. } \
  118. PREDICT_UNLIKELY(bool_result); } ))
  119. #else
  120. #define IF_BUG_ONCE__(cond,var) \
  121. static int var = 0; \
  122. if (PREDICT_UNLIKELY(cond) ? \
  123. (var ? 1 : \
  124. (var=1, \
  125. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
  126. "!("#cond")", 1), \
  127. 1)) \
  128. : 0)
  129. #endif
  130. #define IF_BUG_ONCE_VARNAME_(a) \
  131. warning_logged_on_ ## a ## __
  132. #define IF_BUG_ONCE_VARNAME__(a) \
  133. IF_BUG_ONCE_VARNAME_(a)
  134. /** This macro behaves as 'if (bug(x))', except that it only logs its
  135. * warning once, no matter how many times it triggers.
  136. */
  137. #define IF_BUG_ONCE(cond) \
  138. IF_BUG_ONCE__((cond), \
  139. IF_BUG_ONCE_VARNAME__(__LINE__))
  140. /** Define this if you want Tor to crash when any problem comes up,
  141. * so you can get a coredump and track things down. */
  142. // #define tor_fragile_assert() tor_assert_unreached(0)
  143. #define tor_fragile_assert() tor_assert_nonfatal_unreached_once()
  144. void tor_assertion_failed_(const char *fname, unsigned int line,
  145. const char *func, const char *expr);
  146. void tor_bug_occurred_(const char *fname, unsigned int line,
  147. const char *func, const char *expr,
  148. int once);
  149. #ifdef TOR_UNIT_TESTS
  150. void tor_capture_bugs_(int n);
  151. void tor_end_capture_bugs_(void);
  152. const struct smartlist_t *tor_get_captured_bug_log_(void);
  153. void tor_set_failed_assertion_callback(void (*fn)(void));
  154. #endif
  155. #endif