From c595f6d25e9cda58f5327c5806e2c9a534c454f9 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 4 Feb 2016 12:37:00 -0500 Subject: Add an assertion to tor_libevent_get_base() Closes ticket 18241. --- changes/assert_event_base | 5 +++++ src/common/compat_libevent.c | 1 + 2 files changed, 6 insertions(+) create mode 100644 changes/assert_event_base diff --git a/changes/assert_event_base b/changes/assert_event_base new file mode 100644 index 0000000000..b887795518 --- /dev/null +++ b/changes/assert_event_base @@ -0,0 +1,5 @@ + o Minor features (robustness): + - Exit immediately with an error message if the code attempts to + use libevent without having initialized it. This should resolve + some frequently-made mistakes in our unit tests. Closes ticket + 18241. diff --git a/src/common/compat_libevent.c b/src/common/compat_libevent.c index 29e5c5f63c..c367ee4edb 100644 --- a/src/common/compat_libevent.c +++ b/src/common/compat_libevent.c @@ -247,6 +247,7 @@ tor_libevent_initialize(tor_libevent_cfg *torcfg) MOCK_IMPL(struct event_base *, tor_libevent_get_base, (void)) { + tor_assert(the_event_base != NULL); return the_event_base; } -- cgit v1.2.3-54-g00ecf From db72b509d17311d289b140afb3456376c5525ad7 Mon Sep 17 00:00:00 2001 From: "teor (Tim Wilson-Brown)" Date: Fri, 5 Feb 2016 14:08:58 +1100 Subject: Check that the log mutex is initialised before trying to lock or unlock it --- src/common/log.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/common/log.c b/src/common/log.c index 4a8a7b1165..f71583fd63 100644 --- a/src/common/log.c +++ b/src/common/log.c @@ -149,10 +149,14 @@ static int pretty_fn_has_parens = 0; /** Lock the log_mutex to prevent others from changing the logfile_t list */ #define LOCK_LOGS() STMT_BEGIN \ + tor_assert(log_mutex_initialized); \ tor_mutex_acquire(&log_mutex); \ STMT_END /** Unlock the log_mutex */ -#define UNLOCK_LOGS() STMT_BEGIN tor_mutex_release(&log_mutex); STMT_END +#define UNLOCK_LOGS() STMT_BEGIN \ + tor_assert(log_mutex_initialized); \ + tor_mutex_release(&log_mutex); \ + STMT_END /** What's the lowest log level anybody cares about? Checking this lets us * bail out early from log_debug if we aren't debugging. */ @@ -482,6 +486,8 @@ logv,(int severity, log_domain_mask_t domain, const char *funcname, /* check that severity is sane. Overrunning the masks array leads to * interesting and hard to diagnose effects */ assert(severity >= LOG_ERR && severity <= LOG_DEBUG); + /* check that we've initialised the log mutex before we try to lock it */ + assert(log_mutex_initialized); LOCK_LOGS(); if ((! (domain & LD_NOCB)) && smartlist_len(pending_cb_messages)) -- cgit v1.2.3-54-g00ecf From add8acf42882a13af610ace6005d52544509a823 Mon Sep 17 00:00:00 2001 From: "teor (Tim Wilson-Brown)" Date: Fri, 5 Feb 2016 14:14:17 +1100 Subject: Avoid calling log functions in logv when SMARTLIST_DEBUG is defined --- src/common/log.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/common/log.c b/src/common/log.c index f71583fd63..4779751543 100644 --- a/src/common/log.c +++ b/src/common/log.c @@ -490,7 +490,8 @@ logv,(int severity, log_domain_mask_t domain, const char *funcname, assert(log_mutex_initialized); LOCK_LOGS(); - if ((! (domain & LD_NOCB)) && smartlist_len(pending_cb_messages)) + if ((! (domain & LD_NOCB)) && pending_cb_messages + && smartlist_len(pending_cb_messages)) flush_pending_log_callbacks(); if (queue_startup_messages && @@ -945,7 +946,7 @@ flush_pending_log_callbacks(void) smartlist_t *messages, *messages_tmp; LOCK_LOGS(); - if (0 == smartlist_len(pending_cb_messages)) { + if (!pending_cb_messages || 0 == smartlist_len(pending_cb_messages)) { UNLOCK_LOGS(); return; } -- cgit v1.2.3-54-g00ecf From a7a98e27eadff634655a7845976adc7a23dcdc3f Mon Sep 17 00:00:00 2001 From: "teor (Tim Wilson-Brown)" Date: Fri, 5 Feb 2016 14:28:53 +1100 Subject: Initialise logging before trying to use it in unit tests --- src/test/testing_common.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/testing_common.c b/src/test/testing_common.c index e20e9e6095..fc4e05c4e8 100644 --- a/src/test/testing_common.c +++ b/src/test/testing_common.c @@ -228,6 +228,9 @@ main(int c, const char **v) int loglevel = LOG_ERR; int accel_crypto = 0; + /* We must initialise logs before we call tor_assert() */ + init_logging(1); + #ifdef USE_DMALLOC { int r = CRYPTO_set_mem_ex_functions(tor_malloc_, tor_realloc_, tor_free_); @@ -239,7 +242,6 @@ main(int c, const char **v) options = options_new(); tor_threads_init(); control_initialize_event_queue(); - init_logging(1); configure_backtrace_handler(get_version()); for (i_out = i = 1; i < c; ++i) { -- cgit v1.2.3-54-g00ecf