util_bug.h 11 KB

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