util_bug.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file util_bug.h
  7. *
  8. * \brief Macros to manage assertions, fatal and non-fatal.
  9. *
  10. * Guidelines: All the different kinds of assertion in this file are for
  11. * bug-checking only. Don't write code that can assert based on bad inputs.
  12. *
  13. * We provide two kinds of assertion here: "fatal" and "nonfatal". Use
  14. * nonfatal assertions for any bug you can reasonably recover from -- and
  15. * please, try to recover! Many severe bugs in Tor have been caused by using
  16. * a regular assertion when a nonfatal assertion would have been better.
  17. *
  18. * If you need to check a condition with a nonfatal assertion, AND recover
  19. * from that same condition, consider using the BUG() macro inside a
  20. * conditional. For example:
  21. *
  22. * <code>
  23. * // wrong -- use tor_assert_nonfatal() if you just want an assertion.
  24. * BUG(ptr == NULL);
  25. *
  26. * // okay, but needlessly verbose
  27. * tor_assert_nonfatal(ptr != NULL);
  28. * if (ptr == NULL) { ... }
  29. *
  30. * // this is how we do it:
  31. * if (BUG(ptr == NULL)) { ... }
  32. * </code>
  33. **/
  34. #ifndef TOR_UTIL_BUG_H
  35. #define TOR_UTIL_BUG_H
  36. #include "orconfig.h"
  37. #include "common/compat.h"
  38. #include "common/testsupport.h"
  39. /* Replace assert() with a variant that sends failures to the log before
  40. * calling assert() normally.
  41. */
  42. #ifdef NDEBUG
  43. /* Nobody should ever want to build with NDEBUG set. 99% of our asserts will
  44. * be outside the critical path anyway, so it's silly to disable bug-checking
  45. * throughout the entire program just because a few asserts are slowing you
  46. * down. Profile, optimize the critical path, and keep debugging on.
  47. *
  48. * And I'm not just saying that because some of our asserts check
  49. * security-critical properties.
  50. */
  51. #error "Sorry; we don't support building with NDEBUG."
  52. #endif /* defined(NDEBUG) */
  53. /* The raw_assert...() variants are for use within code that can't call
  54. * tor_assertion_failed_() because of call circularity issues. */
  55. #define raw_assert(expr) STMT_BEGIN \
  56. if (!(expr)) { \
  57. fprintf(stderr, "RAW ASSERTION FAILURE AT %s:%d: %s\n", \
  58. __FILE__, __LINE__, #expr); \
  59. abort(); \
  60. } \
  61. STMT_END
  62. #define raw_assert_unreached(expr) raw_assert(0)
  63. #define raw_assert_unreached_msg(msg) STMT_BEGIN \
  64. fprintf(stderr, "ERROR: %s\n", (msg)); \
  65. raw_assert_unreached(); \
  66. STMT_END
  67. /* Sometimes we don't want to use assertions during branch coverage tests; it
  68. * leads to tons of unreached branches which in reality are only assertions we
  69. * didn't hit. */
  70. #if defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS)
  71. #define tor_assert(a) STMT_BEGIN \
  72. (void)(a); \
  73. STMT_END
  74. #else
  75. /** Like assert(3), but send assertion failures to the log as well as to
  76. * stderr. */
  77. #define tor_assert(expr) STMT_BEGIN \
  78. if (PREDICT_UNLIKELY(!(expr))) { \
  79. tor_assertion_failed_(SHORT_FILE__, __LINE__, __func__, #expr); \
  80. abort(); \
  81. } STMT_END
  82. #endif /* defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS) */
  83. #define tor_assert_unreached() tor_assert(0)
  84. /* Non-fatal bug assertions. The "unreached" variants mean "this line should
  85. * never be reached." The "once" variants mean "Don't log a warning more than
  86. * once".
  87. *
  88. * The 'BUG' macro checks a boolean condition and logs an error message if it
  89. * is true. Example usage:
  90. * if (BUG(x == NULL))
  91. * return -1;
  92. */
  93. #ifdef __COVERITY__
  94. extern int bug_macro_deadcode_dummy__;
  95. #undef BUG
  96. // Coverity defines this in global headers; let's override it. This is a
  97. // magic coverity-only preprocessor thing.
  98. // We use this "deadcode_dummy__" trick to prevent coverity from
  99. // complaining about unreachable bug cases.
  100. #nodef BUG(x) ((x)?(__coverity_panic__(),1):(0+bug_macro_deadcode_dummy__))
  101. #endif /* defined(__COVERITY__) */
  102. #if defined(__COVERITY__) || defined(__clang_analyzer__)
  103. // We're running with a static analysis tool: let's treat even nonfatal
  104. // assertion failures as something that we need to avoid.
  105. #define ALL_BUGS_ARE_FATAL
  106. #endif
  107. #ifdef ALL_BUGS_ARE_FATAL
  108. #define tor_assert_nonfatal_unreached() tor_assert(0)
  109. #define tor_assert_nonfatal(cond) tor_assert((cond))
  110. #define tor_assert_nonfatal_unreached_once() tor_assert(0)
  111. #define tor_assert_nonfatal_once(cond) tor_assert((cond))
  112. #define BUG(cond) \
  113. (PREDICT_UNLIKELY(cond) ? \
  114. (tor_assertion_failed_(SHORT_FILE__,__LINE__,__func__,"!("#cond")"), \
  115. abort(), 1) \
  116. : 0)
  117. #elif defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS)
  118. #define tor_assert_nonfatal_unreached() STMT_NIL
  119. #define tor_assert_nonfatal(cond) ((void)(cond))
  120. #define tor_assert_nonfatal_unreached_once() STMT_NIL
  121. #define tor_assert_nonfatal_once(cond) ((void)(cond))
  122. #define BUG(cond) (PREDICT_UNLIKELY(cond) ? 1 : 0)
  123. #else /* Normal case, !ALL_BUGS_ARE_FATAL, !DISABLE_ASSERTS_IN_UNIT_TESTS */
  124. #define tor_assert_nonfatal_unreached() STMT_BEGIN \
  125. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 0); \
  126. STMT_END
  127. #define tor_assert_nonfatal(cond) STMT_BEGIN \
  128. if (PREDICT_UNLIKELY(!(cond))) { \
  129. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 0); \
  130. } \
  131. STMT_END
  132. #define tor_assert_nonfatal_unreached_once() STMT_BEGIN \
  133. static int warning_logged__ = 0; \
  134. if (!warning_logged__) { \
  135. warning_logged__ = 1; \
  136. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 1); \
  137. } \
  138. STMT_END
  139. #define tor_assert_nonfatal_once(cond) STMT_BEGIN \
  140. static int warning_logged__ = 0; \
  141. if (!warning_logged__ && PREDICT_UNLIKELY(!(cond))) { \
  142. warning_logged__ = 1; \
  143. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1); \
  144. } \
  145. STMT_END
  146. #define BUG(cond) \
  147. (PREDICT_UNLIKELY(cond) ? \
  148. (tor_bug_occurred_(SHORT_FILE__,__LINE__,__func__,"!("#cond")",0), 1) \
  149. : 0)
  150. #endif /* defined(ALL_BUGS_ARE_FATAL) || ... */
  151. #ifdef __GNUC__
  152. #define IF_BUG_ONCE__(cond,var) \
  153. if (( { \
  154. static int var = 0; \
  155. int bool_result = (cond); \
  156. if (PREDICT_UNLIKELY(bool_result) && !var) { \
  157. var = 1; \
  158. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
  159. "!("#cond")", 1); \
  160. } \
  161. PREDICT_UNLIKELY(bool_result); } ))
  162. #else /* !(defined(__GNUC__)) */
  163. #define IF_BUG_ONCE__(cond,var) \
  164. static int var = 0; \
  165. if (PREDICT_UNLIKELY(cond) ? \
  166. (var ? 1 : \
  167. (var=1, \
  168. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
  169. "!("#cond")", 1), \
  170. 1)) \
  171. : 0)
  172. #endif /* defined(__GNUC__) */
  173. #define IF_BUG_ONCE_VARNAME_(a) \
  174. warning_logged_on_ ## a ## __
  175. #define IF_BUG_ONCE_VARNAME__(a) \
  176. IF_BUG_ONCE_VARNAME_(a)
  177. /** This macro behaves as 'if (bug(x))', except that it only logs its
  178. * warning once, no matter how many times it triggers.
  179. */
  180. #define IF_BUG_ONCE(cond) \
  181. IF_BUG_ONCE__((cond), \
  182. IF_BUG_ONCE_VARNAME__(__LINE__))
  183. /** Define this if you want Tor to crash when any problem comes up,
  184. * so you can get a coredump and track things down. */
  185. // #define tor_fragile_assert() tor_assert_unreached(0)
  186. #define tor_fragile_assert() tor_assert_nonfatal_unreached_once()
  187. void tor_assertion_failed_(const char *fname, unsigned int line,
  188. const char *func, const char *expr);
  189. void tor_bug_occurred_(const char *fname, unsigned int line,
  190. const char *func, const char *expr,
  191. int once);
  192. #ifdef TOR_UNIT_TESTS
  193. void tor_capture_bugs_(int n);
  194. void tor_end_capture_bugs_(void);
  195. const struct smartlist_t *tor_get_captured_bug_log_(void);
  196. void tor_set_failed_assertion_callback(void (*fn)(void));
  197. #endif /* defined(TOR_UNIT_TESTS) */
  198. #endif /* !defined(TOR_UTIL_BUG_H) */