util_bug.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, 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. #if defined(TOR_UNIT_TESTS) && defined(__GNUC__)
  28. /* We define this GCC macro as a replacement for PREDICT_UNLIKELY() in this
  29. * header, so that in our unit test builds, we'll get compiler warnings about
  30. * stuff like tor_assert(n = 5).
  31. *
  32. * The key here is that (e) is wrapped in exactly one layer of parentheses,
  33. * and then passed right to a conditional. If you do anything else to the
  34. * expression here, or introduce any more parentheses, the compiler won't
  35. * help you.
  36. *
  37. * We only do this for the unit-test build case because it interferes with
  38. * the likely-branch labeling. Note below that in the other case, we define
  39. * these macros to just be synonyms for PREDICT_(UN)LIKELY.
  40. */
  41. #define ASSERT_PREDICT_UNLIKELY_(e) \
  42. ( { \
  43. int tor__assert_tmp_value__; \
  44. if (e) \
  45. tor__assert_tmp_value__ = 1; \
  46. else \
  47. tor__assert_tmp_value__ = 0; \
  48. tor__assert_tmp_value__; \
  49. } )
  50. #define ASSERT_PREDICT_LIKELY_(e) ASSERT_PREDICT_UNLIKELY_(e)
  51. #else
  52. #define ASSERT_PREDICT_UNLIKELY_(e) PREDICT_UNLIKELY(e)
  53. #define ASSERT_PREDICT_LIKELY_(e) PREDICT_LIKELY(e)
  54. #endif
  55. /* Sometimes we don't want to use assertions during branch coverage tests; it
  56. * leads to tons of unreached branches which in reality are only assertions we
  57. * didn't hit. */
  58. #if defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS)
  59. #define tor_assert(a) STMT_BEGIN \
  60. (void)(a); \
  61. STMT_END
  62. #else
  63. /** Like assert(3), but send assertion failures to the log as well as to
  64. * stderr. */
  65. #define tor_assert(expr) STMT_BEGIN \
  66. if (ASSERT_PREDICT_LIKELY_(expr)) { \
  67. } else { \
  68. tor_assertion_failed_(SHORT_FILE__, __LINE__, __func__, #expr); \
  69. abort(); \
  70. } STMT_END
  71. #endif
  72. #define tor_assert_unreached() tor_assert(0)
  73. /* Non-fatal bug assertions. The "unreached" variants mean "this line should
  74. * never be reached." The "once" variants mean "Don't log a warning more than
  75. * once".
  76. *
  77. * The 'BUG' macro checks a boolean condition and logs an error message if it
  78. * is true. Example usage:
  79. * if (BUG(x == NULL))
  80. * return -1;
  81. */
  82. #ifdef __COVERITY__
  83. #undef BUG
  84. // Coverity defines this in global headers; let's override it. This is a
  85. // magic coverity-only preprocessor thing.
  86. #nodef BUG(x) ((x)?(__coverity_panic__(),1):0)
  87. #endif
  88. #if defined(__COVERITY__) || defined(__clang_analyzer__)
  89. // We're running with a static analysis tool: let's treat even nonfatal
  90. // assertion failures as something that we need to avoid.
  91. #define ALL_BUGS_ARE_FATAL
  92. #endif
  93. #ifdef ALL_BUGS_ARE_FATAL
  94. #define tor_assert_nonfatal_unreached() tor_assert(0)
  95. #define tor_assert_nonfatal(cond) tor_assert((cond))
  96. #define tor_assert_nonfatal_unreached_once() tor_assert(0)
  97. #define tor_assert_nonfatal_once(cond) tor_assert((cond))
  98. #define BUG(cond) \
  99. (ASSERT_PREDICT_UNLIKELY_(cond) ? \
  100. (tor_assertion_failed_(SHORT_FILE__,__LINE__,__func__,"!("#cond")"), \
  101. abort(), 1) \
  102. : 0)
  103. #elif defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS)
  104. #define tor_assert_nonfatal_unreached() STMT_NIL
  105. #define tor_assert_nonfatal(cond) ((void)(cond))
  106. #define tor_assert_nonfatal_unreached_once() STMT_NIL
  107. #define tor_assert_nonfatal_once(cond) ((void)(cond))
  108. #define BUG(cond) (ASSERT_PREDICT_UNLIKELY_(cond) ? 1 : 0)
  109. #else /* Normal case, !ALL_BUGS_ARE_FATAL, !DISABLE_ASSERTS_IN_UNIT_TESTS */
  110. #define tor_assert_nonfatal_unreached() STMT_BEGIN \
  111. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 0); \
  112. STMT_END
  113. #define tor_assert_nonfatal(cond) STMT_BEGIN \
  114. if (ASSERT_PREDICT_LIKELY_(cond)) { \
  115. } else { \
  116. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 0); \
  117. } \
  118. STMT_END
  119. #define tor_assert_nonfatal_unreached_once() STMT_BEGIN \
  120. static int warning_logged__ = 0; \
  121. if (!warning_logged__) { \
  122. warning_logged__ = 1; \
  123. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 1); \
  124. } \
  125. STMT_END
  126. #define tor_assert_nonfatal_once(cond) STMT_BEGIN \
  127. static int warning_logged__ = 0; \
  128. if (ASSERT_PREDICT_LIKELY_(cond)) { \
  129. } else if (!warning_logged__) { \
  130. warning_logged__ = 1; \
  131. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1); \
  132. } \
  133. STMT_END
  134. #define BUG(cond) \
  135. (ASSERT_PREDICT_UNLIKELY_(cond) ? \
  136. (tor_bug_occurred_(SHORT_FILE__,__LINE__,__func__,"!("#cond")",0), 1) \
  137. : 0)
  138. #endif
  139. #ifdef __GNUC__
  140. #define IF_BUG_ONCE__(cond,var) \
  141. if (( { \
  142. static int var = 0; \
  143. int bool_result = !!(cond); \
  144. if (bool_result && !var) { \
  145. var = 1; \
  146. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
  147. "!("#cond")", 1); \
  148. } \
  149. bool_result; } ))
  150. #else
  151. #define IF_BUG_ONCE__(cond,var) \
  152. static int var = 0; \
  153. if ((cond) ? \
  154. (var ? 1 : \
  155. (var=1, \
  156. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
  157. "!("#cond")", 1), \
  158. 1)) \
  159. : 0)
  160. #endif
  161. #define IF_BUG_ONCE_VARNAME_(a) \
  162. warning_logged_on_ ## a ## __
  163. #define IF_BUG_ONCE_VARNAME__(a) \
  164. IF_BUG_ONCE_VARNAME_(a)
  165. /** This macro behaves as 'if (bug(x))', except that it only logs its
  166. * warning once, no matter how many times it triggers.
  167. */
  168. #define IF_BUG_ONCE(cond) \
  169. IF_BUG_ONCE__(ASSERT_PREDICT_UNLIKELY_(cond), \
  170. IF_BUG_ONCE_VARNAME__(__LINE__))
  171. /** Define this if you want Tor to crash when any problem comes up,
  172. * so you can get a coredump and track things down. */
  173. // #define tor_fragile_assert() tor_assert_unreached(0)
  174. #define tor_fragile_assert() tor_assert_nonfatal_unreached_once()
  175. void tor_assertion_failed_(const char *fname, unsigned int line,
  176. const char *func, const char *expr);
  177. void tor_bug_occurred_(const char *fname, unsigned int line,
  178. const char *func, const char *expr,
  179. int once);
  180. #ifdef TOR_UNIT_TESTS
  181. void tor_capture_bugs_(int n);
  182. void tor_end_capture_bugs_(void);
  183. const struct smartlist_t *tor_get_captured_bug_log_(void);
  184. void tor_set_failed_assertion_callback(void (*fn)(void));
  185. #endif
  186. #endif