57#error "Sorry; we don't support building with NDEBUG."
60#if defined(TOR_UNIT_TESTS) && defined(__GNUC__)
74#define ASSERT_PREDICT_UNLIKELY_(e) \
76 int tor__assert_tmp_value__; \
78 tor__assert_tmp_value__ = 1; \
80 tor__assert_tmp_value__ = 0; \
81 tor__assert_tmp_value__; \
83#define ASSERT_PREDICT_LIKELY_(e) ASSERT_PREDICT_UNLIKELY_(e)
85#define ASSERT_PREDICT_UNLIKELY_(e) PREDICT_UNLIKELY(e)
86#define ASSERT_PREDICT_LIKELY_(e) PREDICT_LIKELY(e)
92#if defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS)
93#define tor_assert(a) STMT_BEGIN \
96#define tor_assertf(a, fmt, ...) STMT_BEGIN \
103#define tor_assert(expr) tor_assertf(expr, NULL)
105#define tor_assertf(expr, fmt, ...) STMT_BEGIN \
106 if (ASSERT_PREDICT_LIKELY_(expr)) { \
108 tor_assertion_failed_(SHORT_FILE__, __LINE__, __func__, #expr, \
109 fmt, ##__VA_ARGS__); \
114#define tor_assert_unreached() \
116 tor_assertion_failed_(SHORT_FILE__, __LINE__, __func__, \
117 "line should be unreached", NULL); \
140#if defined(__COVERITY__) || defined(__clang_analyzer__)
143#define ALL_BUGS_ARE_FATAL
148#ifdef ALL_BUGS_ARE_FATAL
149#define tor_assert_nonfatal_unreached() tor_assert(0)
150#define tor_assert_nonfatal(cond) tor_assert((cond))
151#define tor_assertf_nonfatal(cond, fmt, ...) \
152 tor_assertf(cond, fmt, ##__VA_ARGS__)
153#define tor_assert_nonfatal_unreached_once() tor_assert(0)
154#define tor_assert_nonfatal_once(cond) tor_assert((cond))
156 (ASSERT_PREDICT_UNLIKELY_(cond) ? \
157 (tor_assertion_failed_(SHORT_FILE__,__LINE__,__func__,"!("#cond")",NULL), \
161#define IF_BUG_ONCE(cond) if (BUG(cond))
163#elif defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS)
164#define tor_assert_nonfatal_unreached() STMT_NIL
165#define tor_assert_nonfatal(cond) ((void)(cond))
166#define tor_assertf_nonfatal(cond, fmt, ...) STMT_BEGIN \
170#define tor_assert_nonfatal_unreached_once() STMT_NIL
171#define tor_assert_nonfatal_once(cond) ((void)(cond))
172#define BUG(cond) (ASSERT_PREDICT_UNLIKELY_(cond) ? 1 : 0)
174#define IF_BUG_ONCE(cond) if (BUG(cond))
177#define tor_assert_nonfatal_unreached() STMT_BEGIN \
178 tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 0, NULL); \
180#define tor_assert_nonfatal(cond) STMT_BEGIN \
181 if (ASSERT_PREDICT_LIKELY_(cond)) { \
183 tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 0, NULL);\
186#define tor_assertf_nonfatal(cond, fmt, ...) STMT_BEGIN \
187 if (ASSERT_PREDICT_UNLIKELY_(cond)) { \
189 tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 0, \
190 fmt, ##__VA_ARGS__); \
193#define tor_assert_nonfatal_unreached_once() STMT_BEGIN \
194 static int warning_logged__ = 0; \
195 tor_bug_increment_count_(); \
196 if (!warning_logged__) { \
197 warning_logged__ = 1; \
198 tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 1, NULL); \
201#define tor_assert_nonfatal_once(cond) STMT_BEGIN \
202 static int warning_logged__ = 0; \
203 if (!ASSERT_PREDICT_LIKELY_(cond)) { \
204 tor_bug_increment_count_(); \
205 if (!warning_logged__) { \
206 warning_logged__ = 1; \
207 tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1, NULL);\
212 (ASSERT_PREDICT_UNLIKELY_(cond) ? \
213 (tor_bug_occurred_(SHORT_FILE__,__LINE__,__func__,"!("#cond")",0,NULL),1) \
218#define IF_BUG_ONCE__(cond,var) \
220 static int var = 0; \
221 int bool_result = !!(cond); \
223 tor_bug_increment_count_(); \
226 tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
227 ("!("#cond")"), 1, NULL); \
232#define IF_BUG_ONCE__(cond,var) \
233 static int var = 0; \
235 (var ? (tor_bug_increment_count_(), 1) : \
237 tor_bug_increment_count_(), \
238 tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
239 ("!("#cond")"), 1, NULL), \
245#define IF_BUG_ONCE_VARNAME_(a) \
246 warning_logged_on_ ## a ## __
247#define IF_BUG_ONCE_VARNAME__(a) \
248 IF_BUG_ONCE_VARNAME_(a)
254#define IF_BUG_ONCE(cond) \
255 IF_BUG_ONCE__(ASSERT_PREDICT_UNLIKELY_(cond), \
256 IF_BUG_ONCE_VARNAME__(__LINE__))
264#ifdef ALL_BUGS_ARE_FATAL
265#define FALLTHROUGH_UNLESS_ALL_BUGS_ARE_FATAL \
268#define FALLTHROUGH_UNLESS_ALL_BUGS_ARE_FATAL FALLTHROUGH
278#define tor_fragile_assert() tor_assert_nonfatal_unreached_once()
281 const char *func,
const char *expr,
282 const char *fmt, ...)
285size_t tor_bug_get_count(
void);
287 const
char *func, const
char *expr,
288 int once, const
char *fmt, ...)
292void tor_bug_init_counter(
void);
295#define SHORT_FILE__ (tor_fix_source_file(__FILE__))
296const char *tor_fix_source_file(
const char *fname);
298#define SHORT_FILE__ (__FILE__)
299#define tor_fix_source_file(s) (s)
303void tor_capture_bugs_(
int n);
304void tor_end_capture_bugs_(
void);
305const struct smartlist_t *tor_get_captured_bug_log_(
void);
306void tor_set_failed_assertion_callback(
void (*fn)(
void));
Utility macros to handle different features and behavior in different compilers.
Top-level declarations for the smartlist_t dynamic array type.
Macros to implement mocking and selective exposure for the test code.
void tor_bug_occurred_(const char *fname, unsigned int line, const char *func, const char *expr, int once, const char *fmt,...)
void tor_bug_increment_count_(void)
void tor_abort_(void) ATTR_NORETURN
void tor_assertion_failed_(const char *fname, unsigned int line, const char *func, const char *expr, const char *fmt,...)