util_bug.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /* 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 ALL_BUGS_ARE_FATAL
  54. #define tor_assert_nonfatal_unreached() tor_assert(0)
  55. #define tor_assert_nonfatal(cond) tor_assert((cond))
  56. #define tor_assert_nonfatal_unreached_once() tor_assert(0)
  57. #define tor_assert_nonfatal_once(cond) tor_assert((cond))
  58. #define BUG(cond) \
  59. (PREDICT_UNLIKELY(cond) ? \
  60. (tor_assertion_failed_(SHORT_FILE__,__LINE__,__func__,#cond), abort(), 1) \
  61. : 0)
  62. #elif defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS)
  63. #define tor_assert_nonfatal_unreached() STMT_NIL
  64. #define tor_assert_nonfatal(cond) ((void)(cond))
  65. #define tor_assert_nonfatal_unreached_once() STMT_NIL
  66. #define tor_assert_nonfatal_once(cond) ((void)(cond))
  67. #define BUG(cond) (PREDICT_UNLIKELY(cond) ? 1 : 0)
  68. #else /* Normal case, !ALL_BUGS_ARE_FATAL, !DISABLE_ASSERTS_IN_UNIT_TESTS */
  69. #define tor_assert_nonfatal_unreached() STMT_BEGIN \
  70. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 0); \
  71. STMT_END
  72. #define tor_assert_nonfatal(cond) STMT_BEGIN \
  73. if (PREDICT_UNLIKELY(!(cond))) { \
  74. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 0); \
  75. } \
  76. STMT_END
  77. #define tor_assert_nonfatal_unreached_once() STMT_BEGIN \
  78. static int warning_logged__ = 0; \
  79. if (!warning_logged__) { \
  80. warning_logged__ = 1; \
  81. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 1); \
  82. } \
  83. STMT_END
  84. #define tor_assert_nonfatal_once(cond) STMT_BEGIN \
  85. static int warning_logged__ = 0; \
  86. if (!warning_logged__ && PREDICT_UNLIKELY(!(cond))) { \
  87. warning_logged__ = 1; \
  88. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1); \
  89. } \
  90. STMT_END
  91. #define BUG(cond) \
  92. (PREDICT_UNLIKELY(cond) ? \
  93. (tor_bug_occurred_(SHORT_FILE__,__LINE__,__func__,#cond,0), 1) \
  94. : 0)
  95. #endif
  96. #ifdef __GNUC__
  97. #define IF_BUG_ONCE__(cond,var) \
  98. if (( { \
  99. static int var = 0; \
  100. int bool_result = (cond); \
  101. if (PREDICT_UNLIKELY(bool_result) && !var) { \
  102. var = 1; \
  103. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1); \
  104. } \
  105. PREDICT_UNLIKELY(bool_result); } ))
  106. #else
  107. #define IF_BUG_ONCE__(cond,var) \
  108. static int var = 0; \
  109. if (PREDICT_UNLIKELY(cond)) ? \
  110. (var ? 1 : \
  111. (var=1, \
  112. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1), \
  113. 1)) \
  114. : 0)
  115. #endif
  116. #define IF_BUG_ONCE_VARNAME_(a) \
  117. warning_logged_on_ ## a ## __
  118. #define IF_BUG_ONCE_VARNAME__(a) \
  119. IF_BUG_ONCE_VARNAME_(a)
  120. /** This macro behaves as 'if (bug(x))', except that it only logs its
  121. * warning once, no matter how many times it triggers.
  122. */
  123. #define IF_BUG_ONCE(cond) \
  124. IF_BUG_ONCE__((cond), \
  125. IF_BUG_ONCE_VARNAME__(__LINE__))
  126. /** Define this if you want Tor to crash when any problem comes up,
  127. * so you can get a coredump and track things down. */
  128. // #define tor_fragile_assert() tor_assert_unreached(0)
  129. #define tor_fragile_assert() tor_assert_nonfatal_unreached_once()
  130. void tor_assertion_failed_(const char *fname, unsigned int line,
  131. const char *func, const char *expr);
  132. void tor_bug_occurred_(const char *fname, unsigned int line,
  133. const char *func, const char *expr,
  134. int once);
  135. #endif