util_bug.h 8.8 KB

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