From 17435384c050b29cd3c70819ac7176c407f4d1bb Mon Sep 17 00:00:00 2001 From: Andrea Shepard Date: Mon, 12 May 2014 17:28:26 -0700 Subject: [PATCH 1/5] Turn --enable-buf-freelists off by default --- configure.ac | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index f921dd503f..936d61bb2e 100644 --- a/configure.ac +++ b/configure.ac @@ -24,7 +24,7 @@ CPPFLAGS="$CPPFLAGS -I\${top_srcdir}/src/common" #XXXX020 We should make these enabled or not, before 0.2.0.x-final AC_ARG_ENABLE(buf-freelists, - AS_HELP_STRING(--disable-buf-freelists, disable freelists for buffer RAM)) + AS_HELP_STRING(--enable-buf-freelists, enable freelists for buffer RAM)) AC_ARG_ENABLE(openbsd-malloc, AS_HELP_STRING(--enable-openbsd-malloc, Use malloc code from openbsd. Linux only)) AC_ARG_ENABLE(instrument-downloads, @@ -54,7 +54,7 @@ if test "$enable_static_tor" = "yes"; then CFLAGS="$CFLAGS -static" fi -if test x$enable_buf_freelists != xno; then +if test x$enable_buf_freelists = xyes; then AC_DEFINE(ENABLE_BUF_FREELISTS, 1, [Defined if we try to use freelists for buffer RAM chunks]) fi From 39d4e67be8283b2a7141a7aa8342d30e27f47e6f Mon Sep 17 00:00:00 2001 From: Andrea Shepard Date: Mon, 12 May 2014 18:23:34 -0700 Subject: [PATCH 2/5] Add --disable-mempools configure option --- configure.ac | 9 +++++++++ src/common/include.am | 16 ++++++++++++---- src/or/circuitlist.c | 2 ++ src/or/main.c | 6 ++++++ src/or/relay.c | 21 ++++++++++++++++++++- src/or/relay.h | 2 ++ src/test/test.c | 2 ++ src/test/test_cell_queue.c | 10 ++++++++++ src/test/test_circuitmux.c | 4 ++++ src/test/test_oom.c | 13 +++++++++++++ src/test/test_util.c | 8 ++++++++ 11 files changed, 88 insertions(+), 5 deletions(-) diff --git a/configure.ac b/configure.ac index 936d61bb2e..255dcea08c 100644 --- a/configure.ac +++ b/configure.ac @@ -25,6 +25,8 @@ CPPFLAGS="$CPPFLAGS -I\${top_srcdir}/src/common" #XXXX020 We should make these enabled or not, before 0.2.0.x-final AC_ARG_ENABLE(buf-freelists, AS_HELP_STRING(--enable-buf-freelists, enable freelists for buffer RAM)) +AC_ARG_ENABLE(mempools, + AS_HELP_STRING(--disable-mempools, disable mempools for relay cells)) AC_ARG_ENABLE(openbsd-malloc, AS_HELP_STRING(--enable-openbsd-malloc, Use malloc code from openbsd. Linux only)) AC_ARG_ENABLE(instrument-downloads, @@ -58,6 +60,13 @@ if test x$enable_buf_freelists = xyes; then AC_DEFINE(ENABLE_BUF_FREELISTS, 1, [Defined if we try to use freelists for buffer RAM chunks]) fi + +AM_CONDITIONAL(USE_MEMPOOLS, test x$enable_mempools != xno) +if test x$enable_mempools != xno; then + AC_DEFINE(ENABLE_MEMPOOLS, 1, + [Defined if we try to use mempools for cells being relayed]) +fi + AM_CONDITIONAL(USE_OPENBSD_MALLOC, test x$enable_openbsd_malloc = xyes) if test x$enable_instrument_downloads = xyes; then AC_DEFINE(INSTRUMENT_DOWNLOADS, 1, diff --git a/src/common/include.am b/src/common/include.am index 7b2465cd84..61a90cd355 100644 --- a/src/common/include.am +++ b/src/common/include.am @@ -24,6 +24,14 @@ else libor_extra_source= endif +if USE_MEMPOOLS +libor_mempool_source=src/common/mempool.c +libor_mempool_header=src/common/mempool.h +else +libor_mempool_source= +libor_mempool_header= +endif + src_common_libcurve25519_donna_a_CFLAGS= if BUILD_CURVE25519_DONNA @@ -56,13 +64,13 @@ LIBOR_A_SOURCES = \ src/common/di_ops.c \ src/common/log.c \ src/common/memarea.c \ - src/common/mempool.c \ src/common/procmon.c \ src/common/util.c \ src/common/util_codedigest.c \ src/common/sandbox.c \ src/ext/csiphash.c \ - $(libor_extra_source) + $(libor_extra_source) \ + $(libor_mempool_source) LIBOR_CRYPTO_A_SOURCES = \ src/common/aes.c \ @@ -102,7 +110,6 @@ COMMONHEADERS = \ src/common/crypto_curve25519.h \ src/common/di_ops.h \ src/common/memarea.h \ - src/common/mempool.h \ src/common/linux_syscalls.inc \ src/common/procmon.h \ src/common/sandbox.h \ @@ -111,7 +118,8 @@ COMMONHEADERS = \ src/common/torint.h \ src/common/torlog.h \ src/common/tortls.h \ - src/common/util.h + src/common/util.h \ + $(libor_mempool_header) noinst_HEADERS+= $(COMMONHEADERS) diff --git a/src/or/circuitlist.c b/src/or/circuitlist.c index c54a95419a..e482fa5358 100644 --- a/src/or/circuitlist.c +++ b/src/or/circuitlist.c @@ -1975,7 +1975,9 @@ circuits_handle_oom(size_t current_allocation) break; } SMARTLIST_FOREACH_END(circ); +#ifdef ENABLE_MEMPOOLS clean_cell_pool(); /* In case this helps. */ +#endif /* ENABLE_MEMPOOLS */ buf_shrink_freelists(1); /* This is necessary to actually release buffer chunks. */ diff --git a/src/or/main.c b/src/or/main.c index 6713d80368..e5a48cf98b 100644 --- a/src/or/main.c +++ b/src/or/main.c @@ -1522,7 +1522,9 @@ run_scheduled_events(time_t now) if (conn->inbuf) buf_shrink(conn->inbuf); }); +#ifdef ENABLE_MEMPOOL clean_cell_pool(); +#endif /* ENABLE_MEMPOOL */ buf_shrink_freelists(0); /** How often do we check buffers and pools for empty space that can be * deallocated? */ @@ -1927,8 +1929,10 @@ do_main_loop(void) } } +#ifdef ENABLE_MEMPOOLS /* Set up the packed_cell_t memory pool. */ init_cell_pool(); +#endif /* ENABLE_MEMPOOLS */ /* Set up our buckets */ connection_bucket_init(); @@ -2545,7 +2549,9 @@ tor_free_all(int postfork) router_free_all(); policies_free_all(); } +#ifdef ENABLE_MEMPOOLS free_cell_pool(); +#endif /* ENABLE_MEMPOOLS */ if (!postfork) { tor_tls_free_all(); } diff --git a/src/or/relay.c b/src/or/relay.c index f8b0deedb9..e593f87fcf 100644 --- a/src/or/relay.c +++ b/src/or/relay.c @@ -26,7 +26,9 @@ #include "control.h" #include "geoip.h" #include "main.h" +#ifdef ENABLE_MEMPOOLS #include "mempool.h" +#endif #include "networkstatus.h" #include "nodelist.h" #include "onion.h" @@ -2231,9 +2233,10 @@ circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint) #define assert_cmux_ok_paranoid(chan) #endif -/** The total number of cells we have allocated from the memory pool. */ +/** The total number of cells we have allocated. */ static size_t total_cells_allocated = 0; +#ifdef ENABLE_MEMPOOLS /** A memory pool to allocate packed_cell_t objects. */ static mp_pool_t *cell_pool = NULL; @@ -2265,12 +2268,18 @@ clean_cell_pool(void) mp_pool_clean(cell_pool, 0, 1); } +#endif /* ENABLE_MEMPOOLS */ + /** Release storage held by cell. */ static INLINE void packed_cell_free_unchecked(packed_cell_t *cell) { --total_cells_allocated; +#ifdef ENABLE_MEMPOOLS mp_pool_release(cell); +#else + tor_free(cell); +#endif /* ENABLE_MEMPOOLS */ } /** Allocate and return a new packed_cell_t. */ @@ -2278,7 +2287,11 @@ STATIC packed_cell_t * packed_cell_new(void) { ++total_cells_allocated; +#ifdef ENABLE_MEMPOOLS return mp_pool_get(cell_pool); +#else + return tor_malloc(sizeof(packed_cell_t)); +#endif } /** Return a packed cell used outside by channel_t lower layer */ @@ -2307,7 +2320,9 @@ dump_cell_pool_usage(int severity) tor_log(severity, LD_MM, "%d cells allocated on %d circuits. %d cells leaked.", n_cells, n_circs, (int)total_cells_allocated - n_cells); +#ifdef ENABLE_MEMPOOLS mp_pool_log_status(cell_pool, severity); +#endif } /** Allocate a new copy of packed cell. */ @@ -2387,7 +2402,11 @@ cell_queue_pop(cell_queue_t *queue) size_t packed_cell_mem_cost(void) { +#ifdef ENABLE_MEMPOOLS return sizeof(packed_cell_t) + MP_POOL_ITEM_OVERHEAD; +#else + return sizeof(packed_cell_t); +#endif /* ENABLE_MEMPOOLS */ } /** DOCDOC */ diff --git a/src/or/relay.h b/src/or/relay.h index 9c0e21c14f..479d474b35 100644 --- a/src/or/relay.h +++ b/src/or/relay.h @@ -42,9 +42,11 @@ extern uint64_t stats_n_data_bytes_packaged; extern uint64_t stats_n_data_cells_received; extern uint64_t stats_n_data_bytes_received; +#ifdef ENABLE_MEMPOOLS void init_cell_pool(void); void free_cell_pool(void); void clean_cell_pool(void); +#endif /* ENABLE_MEMPOOLS */ void dump_cell_pool_usage(int severity); size_t packed_cell_mem_cost(void); diff --git a/src/test/test.c b/src/test/test.c index 771725e231..b49f946827 100644 --- a/src/test/test.c +++ b/src/test/test.c @@ -51,7 +51,9 @@ double fabs(double x); #include "rendcommon.h" #include "test.h" #include "torgzip.h" +#ifdef ENABLE_MEMPOOLS #include "mempool.h" +#endif #include "memarea.h" #include "onion.h" #include "onion_ntor.h" diff --git a/src/test/test_cell_queue.c b/src/test/test_cell_queue.c index 1eac073105..92629823ec 100644 --- a/src/test/test_cell_queue.c +++ b/src/test/test_cell_queue.c @@ -16,7 +16,10 @@ test_cq_manip(void *arg) cell_t cell; (void) arg; +#ifdef ENABLE_MEMPOOLS init_cell_pool(); +#endif /* ENABLE_MEMPOOLS */ + cell_queue_init(&cq); tt_int_op(cq.n, ==, 0); @@ -96,7 +99,10 @@ test_cq_manip(void *arg) packed_cell_free(pc_tmp); cell_queue_clear(&cq); + +#ifdef ENABLE_MEMPOOLS free_cell_pool(); +#endif /* ENABLE_MEMPOOLS */ } static void @@ -108,7 +114,9 @@ test_circuit_n_cells(void *arg) (void)arg; +#ifdef ENABLE_MEMPOOLS init_cell_pool(); +#endif /* ENABLE_MEMPOOLS */ pc1 = packed_cell_new(); pc2 = packed_cell_new(); @@ -137,7 +145,9 @@ test_circuit_n_cells(void *arg) circuit_free(TO_CIRCUIT(or_c)); circuit_free(TO_CIRCUIT(origin_c)); +#ifdef ENABLE_MEMPOOLS free_cell_pool(); +#endif /* ENABLE_MEMPOOLS */ } struct testcase_t cell_queue_tests[] = { diff --git a/src/test/test_circuitmux.c b/src/test/test_circuitmux.c index 0f592001cb..b9c0436ebf 100644 --- a/src/test/test_circuitmux.c +++ b/src/test/test_circuitmux.c @@ -36,7 +36,9 @@ test_cmux_destroy_cell_queue(void *arg) cell_queue_t *cq = NULL; packed_cell_t *pc = NULL; +#ifdef ENABLE_MEMPOOLS init_cell_pool(); +#endif /* ENABLE_MEMPOOLS */ (void) arg; cmux = circuitmux_alloc(); @@ -74,7 +76,9 @@ test_cmux_destroy_cell_queue(void *arg) channel_free(ch); packed_cell_free(pc); +#ifdef ENABLE_MEMPOOLS free_cell_pool(); +#endif /* ENABLE_MEMPOOLS */ } struct testcase_t circuitmux_tests[] = { diff --git a/src/test/test_oom.c b/src/test/test_oom.c index 989ca1203b..1afe3fd162 100644 --- a/src/test/test_oom.c +++ b/src/test/test_oom.c @@ -12,7 +12,9 @@ #include "compat_libevent.h" #include "connection.h" #include "config.h" +#ifdef ENABLE_MEMPOOLS #include "mempool.h" +#endif #include "relay.h" #include "test.h" @@ -130,7 +132,10 @@ test_oom_circbuf(void *arg) (void) arg; MOCK(circuit_mark_for_close_, circuit_mark_for_close_dummy_); + +#ifdef ENABLE_MEMPOOLS init_cell_pool(); +#endif /* ENABLE_MEMPOOLS */ /* Far too low for real life. */ options->MaxMemInQueues = 256*packed_cell_mem_cost(); @@ -149,8 +154,13 @@ test_oom_circbuf(void *arg) tor_gettimeofday_cache_set(&tv); c2 = dummy_or_circuit_new(20, 20); +#ifdef ENABLE_MEMPOOLS tt_int_op(packed_cell_mem_cost(), ==, sizeof(packed_cell_t) + MP_POOL_ITEM_OVERHEAD); +#else + tt_int_op(packed_cell_mem_cost(), ==, + sizeof(packed_cell_t)); +#endif /* ENABLE_MEMPOOLS */ tt_int_op(cell_queues_get_total_allocation(), ==, packed_cell_mem_cost() * 70); tt_int_op(cell_queues_check_size(), ==, 0); /* We are still not OOM */ @@ -220,7 +230,10 @@ test_oom_streambuf(void *arg) (void) arg; MOCK(circuit_mark_for_close_, circuit_mark_for_close_dummy_); + +#ifdef ENABLE_MEMPOOLS init_cell_pool(); +#endif /* ENABLE_MEMPOOLS */ /* Far too low for real life. */ options->MaxMemInQueues = 81*packed_cell_mem_cost() + 4096 * 34; diff --git a/src/test/test_util.c b/src/test/test_util.c index 6d6b6dbdf8..eadbf730ea 100644 --- a/src/test/test_util.c +++ b/src/test/test_util.c @@ -12,7 +12,9 @@ #include "config.h" #include "control.h" #include "test.h" +#ifdef ENABLE_MEMPOOLS #include "mempool.h" +#endif /* ENABLE_MEMPOOLS */ #include "memarea.h" #ifdef _WIN32 @@ -1899,6 +1901,8 @@ test_util_path_is_relative(void) ; } +#ifdef ENABLE_MEMPOOLS + /** Run unittests for memory pool allocator */ static void test_util_mempool(void) @@ -1957,6 +1961,8 @@ test_util_mempool(void) mp_pool_destroy(pool); } +#endif /* ENABLE_MEMPOOLS */ + /** Run unittests for memory area allocator */ static void test_util_memarea(void) @@ -3656,7 +3662,9 @@ struct testcase_t util_tests[] = { UTIL_LEGACY(pow2), UTIL_LEGACY(gzip), UTIL_LEGACY(datadir), +#ifdef ENABLE_MEMPOOLS UTIL_LEGACY(mempool), +#endif UTIL_LEGACY(memarea), UTIL_LEGACY(control_formats), UTIL_LEGACY(mmap), From f7a55bc4b4a5f0e9323275a99f8c6ceec9f5f1d5 Mon Sep 17 00:00:00 2001 From: Andrea Shepard Date: Fri, 16 May 2014 08:51:51 -0700 Subject: [PATCH 3/5] Turn --enable-mempools off by default --- configure.ac | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index 255dcea08c..207763b7bb 100644 --- a/configure.ac +++ b/configure.ac @@ -26,7 +26,7 @@ CPPFLAGS="$CPPFLAGS -I\${top_srcdir}/src/common" AC_ARG_ENABLE(buf-freelists, AS_HELP_STRING(--enable-buf-freelists, enable freelists for buffer RAM)) AC_ARG_ENABLE(mempools, - AS_HELP_STRING(--disable-mempools, disable mempools for relay cells)) + AS_HELP_STRING(--enable-mempools, enable mempools for relay cells)) AC_ARG_ENABLE(openbsd-malloc, AS_HELP_STRING(--enable-openbsd-malloc, Use malloc code from openbsd. Linux only)) AC_ARG_ENABLE(instrument-downloads, @@ -61,8 +61,8 @@ if test x$enable_buf_freelists = xyes; then [Defined if we try to use freelists for buffer RAM chunks]) fi -AM_CONDITIONAL(USE_MEMPOOLS, test x$enable_mempools != xno) -if test x$enable_mempools != xno; then +AM_CONDITIONAL(USE_MEMPOOLS, test x$enable_mempools = xyes) +if test x$enable_mempools = xyes; then AC_DEFINE(ENABLE_MEMPOOLS, 1, [Defined if we try to use mempools for cells being relayed]) fi From b3edd0406541d427a12c4e34d9380573929d0182 Mon Sep 17 00:00:00 2001 From: Andrea Shepard Date: Fri, 16 May 2014 08:56:42 -0700 Subject: [PATCH 4/5] Add changes file for bug11476 --- changes/bug11476 | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changes/bug11476 diff --git a/changes/bug11476 b/changes/bug11476 new file mode 100644 index 0000000000..cb3d217cd9 --- /dev/null +++ b/changes/bug11476 @@ -0,0 +1,5 @@ + o Bugfixes: + - Add configure options controlling allocator tricks like mempools and + freelists, and turn them off by default; on most platforms malloc is + reasonable enough for this not to be necessary, and a similar feature + in OpenSSL exacerbated Heartbleed. Fixes bug #11476. From 170e0df7417e78f7b689f3189e7a0b5b8dfd2722 Mon Sep 17 00:00:00 2001 From: Andrea Shepard Date: Wed, 21 May 2014 10:53:25 -0700 Subject: [PATCH 5/5] Eliminate #ifdef ENABLE_MEMPOOLS in packed_cell_new/free() --- src/or/relay.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/or/relay.c b/src/or/relay.c index e593f87fcf..5c430a6d71 100644 --- a/src/or/relay.c +++ b/src/or/relay.c @@ -2268,6 +2268,22 @@ clean_cell_pool(void) mp_pool_clean(cell_pool, 0, 1); } +#define relay_alloc_cell() \ + mp_pool_get(cell_pool) +#define relay_free_cell(cell) \ + mp_pool_release(cell) + +#define RELAY_CELL_MEM_COST (sizeof(packed_cell_t) + MP_POOL_ITEM_OVERHEAD) + +#else /* !ENABLE_MEMPOOLS case */ + +#define relay_alloc_cell() \ + tor_malloc_zero(sizeof(packed_cell_t)) +#define relay_free_cell(cell) \ + tor_free(cell) + +#define RELAY_CELL_MEM_COST (sizeof(packed_cell_t)) + #endif /* ENABLE_MEMPOOLS */ /** Release storage held by cell. */ @@ -2275,11 +2291,7 @@ static INLINE void packed_cell_free_unchecked(packed_cell_t *cell) { --total_cells_allocated; -#ifdef ENABLE_MEMPOOLS - mp_pool_release(cell); -#else - tor_free(cell); -#endif /* ENABLE_MEMPOOLS */ + relay_free_cell(cell); } /** Allocate and return a new packed_cell_t. */ @@ -2287,11 +2299,7 @@ STATIC packed_cell_t * packed_cell_new(void) { ++total_cells_allocated; -#ifdef ENABLE_MEMPOOLS - return mp_pool_get(cell_pool); -#else - return tor_malloc(sizeof(packed_cell_t)); -#endif + return relay_alloc_cell(); } /** Return a packed cell used outside by channel_t lower layer */ @@ -2402,11 +2410,7 @@ cell_queue_pop(cell_queue_t *queue) size_t packed_cell_mem_cost(void) { -#ifdef ENABLE_MEMPOOLS - return sizeof(packed_cell_t) + MP_POOL_ITEM_OVERHEAD; -#else - return sizeof(packed_cell_t); -#endif /* ENABLE_MEMPOOLS */ + return RELAY_CELL_MEM_COST; } /** DOCDOC */