util_bug.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. extern int bug_macro_deadcode_dummy__;
  55. #undef BUG
  56. // Coverity defines this in global headers; let's override it. This is a
  57. // magic coverity-only preprocessor thing.
  58. // We use this "deadcode_dummy__" trick to prevent coverity from
  59. // complaining about unreachable bug cases.
  60. #nodef BUG(x) ((x)?(__coverity_panic__(),1):(0+bug_macro_deadcode_dummy__))
  61. #endif
  62. #if defined(__COVERITY__) || defined(__clang_analyzer__)
  63. // We're running with a static analysis tool: let's treat even nonfatal
  64. // assertion failures as something that we need to avoid.
  65. #define ALL_BUGS_ARE_FATAL
  66. #endif
  67. #ifdef ALL_BUGS_ARE_FATAL
  68. #define tor_assert_nonfatal_unreached() tor_assert(0)
  69. #define tor_assert_nonfatal(cond) tor_assert((cond))
  70. #define tor_assert_nonfatal_unreached_once() tor_assert(0)
  71. #define tor_assert_nonfatal_once(cond) tor_assert((cond))
  72. #define BUG(cond) \
  73. (PREDICT_UNLIKELY(cond) ? \
  74. (tor_assertion_failed_(SHORT_FILE__,__LINE__,__func__,"!("#cond")"), \
  75. abort(), 1) \
  76. : 0)
  77. #elif defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS)
  78. #define tor_assert_nonfatal_unreached() STMT_NIL
  79. #define tor_assert_nonfatal(cond) ((void)(cond))
  80. #define tor_assert_nonfatal_unreached_once() STMT_NIL
  81. #define tor_assert_nonfatal_once(cond) ((void)(cond))
  82. #define BUG(cond) (PREDICT_UNLIKELY(cond) ? 1 : 0)
  83. #else /* Normal case, !ALL_BUGS_ARE_FATAL, !DISABLE_ASSERTS_IN_UNIT_TESTS */
  84. #define tor_assert_nonfatal_unreached() STMT_BEGIN \
  85. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 0); \
  86. STMT_END
  87. #define tor_assert_nonfatal(cond) STMT_BEGIN \
  88. if (PREDICT_UNLIKELY(!(cond))) { \
  89. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 0); \
  90. } \
  91. STMT_END
  92. #define tor_assert_nonfatal_unreached_once() STMT_BEGIN \
  93. static int warning_logged__ = 0; \
  94. if (!warning_logged__) { \
  95. warning_logged__ = 1; \
  96. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 1); \
  97. } \
  98. STMT_END
  99. #define tor_assert_nonfatal_once(cond) STMT_BEGIN \
  100. static int warning_logged__ = 0; \
  101. if (!warning_logged__ && PREDICT_UNLIKELY(!(cond))) { \
  102. warning_logged__ = 1; \
  103. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1); \
  104. } \
  105. STMT_END
  106. #define BUG(cond) \
  107. (PREDICT_UNLIKELY(cond) ? \
  108. (tor_bug_occurred_(SHORT_FILE__,__LINE__,__func__,"!("#cond")",0), 1) \
  109. : 0)
  110. #endif
  111. #ifdef __GNUC__
  112. #define IF_BUG_ONCE__(cond,var) \
  113. if (( { \
  114. static int var = 0; \
  115. int bool_result = (cond); \
  116. if (PREDICT_UNLIKELY(bool_result) && !var) { \
  117. var = 1; \
  118. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
  119. "!("#cond")", 1); \
  120. } \
  121. PREDICT_UNLIKELY(bool_result); } ))
  122. #else
  123. #define IF_BUG_ONCE__(cond,var) \
  124. static int var = 0; \
  125. if (PREDICT_UNLIKELY(cond) ? \
  126. (var ? 1 : \
  127. (var=1, \
  128. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
  129. "!("#cond")", 1), \
  130. 1)) \
  131. : 0)
  132. #endif
  133. #define IF_BUG_ONCE_VARNAME_(a) \
  134. warning_logged_on_ ## a ## __
  135. #define IF_BUG_ONCE_VARNAME__(a) \
  136. IF_BUG_ONCE_VARNAME_(a)
  137. /** This macro behaves as 'if (bug(x))', except that it only logs its
  138. * warning once, no matter how many times it triggers.
  139. */
  140. #define IF_BUG_ONCE(cond) \
  141. IF_BUG_ONCE__((cond), \
  142. IF_BUG_ONCE_VARNAME__(__LINE__))
  143. /** Define this if you want Tor to crash when any problem comes up,
  144. * so you can get a coredump and track things down. */
  145. // #define tor_fragile_assert() tor_assert_unreached(0)
  146. #define tor_fragile_assert() tor_assert_nonfatal_unreached_once()
  147. void tor_assertion_failed_(const char *fname, unsigned int line,
  148. const char *func, const char *expr);
  149. void tor_bug_occurred_(const char *fname, unsigned int line,
  150. const char *func, const char *expr,
  151. int once);
  152. #ifdef TOR_UNIT_TESTS
  153. void tor_capture_bugs_(int n);
  154. void tor_end_capture_bugs_(void);
  155. const struct smartlist_t *tor_get_captured_bug_log_(void);
  156. void tor_set_failed_assertion_callback(void (*fn)(void));
  157. #endif
  158. #endif