/** Run unit tests for buffers.c */ static void test_oom_streambuf(void *arg) { or_options_t *options = get_options_mutable(); circuit_t *c1 = NULL, *c2 = NULL, *c3 = NULL, *c4 = NULL, *c5 = NULL; struct timeval tv = { 1389641159, 0 }; uint32_t tvms; int i; smartlist_t *edgeconns = smartlist_new(); (void) arg; MOCK(circuit_mark_for_close_, circuit_mark_for_close_dummy_); /* Far too low for real life. */ options->MaxMemInQueues = 81*packed_cell_mem_cost() + 4096 * 34; options->CellStatistics = 0; tt_int_op(cell_queues_check_size(), OP_EQ, 0); /* We don't start out OOM. */ tt_int_op(cell_queues_get_total_allocation(), OP_EQ, 0); tt_int_op(buf_get_total_allocation(), OP_EQ, 0); /* Start all circuits with a bit of data queued in cells */ tv.tv_usec = 500*1000; /* go halfway into the second. */ tor_gettimeofday_cache_set(&tv); c1 = dummy_or_circuit_new(10,10); tv.tv_usec = 510*1000; tor_gettimeofday_cache_set(&tv); c2 = dummy_origin_circuit_new(20); tv.tv_usec = 520*1000; tor_gettimeofday_cache_set(&tv); c3 = dummy_or_circuit_new(20,20); tv.tv_usec = 530*1000; tor_gettimeofday_cache_set(&tv); c4 = dummy_or_circuit_new(0,0); tt_int_op(cell_queues_get_total_allocation(), OP_EQ, packed_cell_mem_cost() * 80); tv.tv_usec = 600*1000; tor_gettimeofday_cache_set(&tv); /* Add some connections to c1...c4. */ for (i = 0; i < 4; ++i) { edge_connection_t *ec; /* link it to a circuit */ tv.tv_usec += 10*1000; tor_gettimeofday_cache_set(&tv); ec = dummy_edge_conn_new(c1, CONN_TYPE_EXIT, 1000, 1000); tt_assert(ec); smartlist_add(edgeconns, ec); tv.tv_usec += 10*1000; tor_gettimeofday_cache_set(&tv); ec = dummy_edge_conn_new(c2, CONN_TYPE_AP, 1000, 1000); tt_assert(ec); smartlist_add(edgeconns, ec); tv.tv_usec += 10*1000; tor_gettimeofday_cache_set(&tv); ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000); /* Yes, 4 twice*/ tt_assert(ec); smartlist_add(edgeconns, ec); tv.tv_usec += 10*1000; tor_gettimeofday_cache_set(&tv); ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000); smartlist_add(edgeconns, ec); tt_assert(ec); } tv.tv_sec += 1; tv.tv_usec = 0; tvms = (uint32_t) tv_to_msec(&tv); tt_int_op(circuit_max_queued_cell_age(c1, tvms), OP_EQ, 500); tt_int_op(circuit_max_queued_cell_age(c2, tvms), OP_EQ, 490); tt_int_op(circuit_max_queued_cell_age(c3, tvms), OP_EQ, 480); tt_int_op(circuit_max_queued_cell_age(c4, tvms), OP_EQ, 0); tt_int_op(circuit_max_queued_data_age(c1, tvms), OP_EQ, 390); tt_int_op(circuit_max_queued_data_age(c2, tvms), OP_EQ, 380); tt_int_op(circuit_max_queued_data_age(c3, tvms), OP_EQ, 0); tt_int_op(circuit_max_queued_data_age(c4, tvms), OP_EQ, 370); tt_int_op(circuit_max_queued_item_age(c1, tvms), OP_EQ, 500); tt_int_op(circuit_max_queued_item_age(c2, tvms), OP_EQ, 490); tt_int_op(circuit_max_queued_item_age(c3, tvms), OP_EQ, 480); tt_int_op(circuit_max_queued_item_age(c4, tvms), OP_EQ, 370); tt_int_op(cell_queues_get_total_allocation(), OP_EQ, packed_cell_mem_cost() * 80); tt_int_op(buf_get_total_allocation(), OP_EQ, 4096*16*2); /* Now give c4 a very old buffer of modest size */ { edge_connection_t *ec; tv.tv_sec -= 1; tv.tv_usec = 0; tor_gettimeofday_cache_set(&tv); ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000); tt_assert(ec); smartlist_add(edgeconns, ec); } tt_int_op(buf_get_total_allocation(), OP_EQ, 4096*17*2); tt_int_op(circuit_max_queued_item_age(c4, tvms), OP_EQ, 1000); tt_int_op(cell_queues_check_size(), OP_EQ, 0); /* And run over the limit. */ tv.tv_usec = 800*1000; tor_gettimeofday_cache_set(&tv); c5 = dummy_or_circuit_new(0,5); tt_int_op(cell_queues_get_total_allocation(), OP_EQ, packed_cell_mem_cost() * 85); tt_int_op(buf_get_total_allocation(), OP_EQ, 4096*17*2); tt_int_op(cell_queues_check_size(), OP_EQ, 1); /* We are now OOM */ /* C4 should have died. */ tt_assert(! c1->marked_for_close); tt_assert(! c2->marked_for_close); tt_assert(! c3->marked_for_close); tt_assert(c4->marked_for_close); tt_assert(! c5->marked_for_close); tt_int_op(cell_queues_get_total_allocation(), OP_EQ, packed_cell_mem_cost() * 85); tt_int_op(buf_get_total_allocation(), OP_EQ, 4096*8*2); done: circuit_free(c1); circuit_free(c2); circuit_free(c3); circuit_free(c4); circuit_free(c5); SMARTLIST_FOREACH(edgeconns, edge_connection_t *, ec, connection_free_(TO_CONN(ec))); smartlist_free(edgeconns); UNMOCK(circuit_mark_for_close_); }
/** Run unit tests for buffers.c */ static void test_oom_circbuf(void *arg) { or_options_t *options = get_options_mutable(); circuit_t *c1 = NULL, *c2 = NULL, *c3 = NULL, *c4 = NULL; struct timeval tv = { 1389631048, 0 }; (void) arg; MOCK(circuit_mark_for_close_, circuit_mark_for_close_dummy_); /* Far too low for real life. */ options->MaxMemInQueues = 256*packed_cell_mem_cost(); options->CellStatistics = 0; tt_int_op(cell_queues_check_size(), OP_EQ, 0); /* We don't start out OOM. */ tt_int_op(cell_queues_get_total_allocation(), OP_EQ, 0); tt_int_op(buf_get_total_allocation(), OP_EQ, 0); /* Now we're going to fake up some circuits and get them added to the global circuit list. */ tv.tv_usec = 0; tor_gettimeofday_cache_set(&tv); c1 = dummy_origin_circuit_new(30); tv.tv_usec = 10*1000; tor_gettimeofday_cache_set(&tv); c2 = dummy_or_circuit_new(20, 20); tt_int_op(packed_cell_mem_cost(), OP_EQ, sizeof(packed_cell_t)); tt_int_op(cell_queues_get_total_allocation(), OP_EQ, packed_cell_mem_cost() * 70); tt_int_op(cell_queues_check_size(), OP_EQ, 0); /* We are still not OOM */ tv.tv_usec = 20*1000; tor_gettimeofday_cache_set(&tv); c3 = dummy_or_circuit_new(100, 85); tt_int_op(cell_queues_check_size(), OP_EQ, 0); /* We are still not OOM */ tt_int_op(cell_queues_get_total_allocation(), OP_EQ, packed_cell_mem_cost() * 255); tv.tv_usec = 30*1000; tor_gettimeofday_cache_set(&tv); /* Adding this cell will trigger our OOM handler. */ c4 = dummy_or_circuit_new(2, 0); tt_int_op(cell_queues_get_total_allocation(), OP_EQ, packed_cell_mem_cost() * 257); tt_int_op(cell_queues_check_size(), OP_EQ, 1); /* We are now OOM */ tt_assert(c1->marked_for_close); tt_assert(! c2->marked_for_close); tt_assert(! c3->marked_for_close); tt_assert(! c4->marked_for_close); tt_int_op(cell_queues_get_total_allocation(), OP_EQ, packed_cell_mem_cost() * (257 - 30)); circuit_free(c1); tv.tv_usec = 0; tor_gettimeofday_cache_set(&tv); /* go back in time */ c1 = dummy_or_circuit_new(90, 0); tv.tv_usec = 40*1000; /* go back to the future */ tor_gettimeofday_cache_set(&tv); tt_int_op(cell_queues_check_size(), OP_EQ, 1); /* We are now OOM */ tt_assert(c1->marked_for_close); tt_assert(! c2->marked_for_close); tt_assert(! c3->marked_for_close); tt_assert(! c4->marked_for_close); tt_int_op(cell_queues_get_total_allocation(), OP_EQ, packed_cell_mem_cost() * (257 - 30)); done: circuit_free(c1); circuit_free(c2); circuit_free(c3); circuit_free(c4); UNMOCK(circuit_mark_for_close_); }
static void test_buffer_pullup(void *arg) { buf_t *buf; char *stuff, *tmp; const char *cp; size_t sz; (void)arg; stuff = tor_malloc(16384); tmp = tor_malloc(16384); buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */ tt_assert(buf); tt_int_op(buf_get_default_chunk_size(buf), OP_EQ, 4096); tt_int_op(buf_get_total_allocation(), OP_EQ, 0); /* There are a bunch of cases for pullup. One is the trivial case. Let's mess around with an empty buffer. */ buf_pullup(buf, 16); buf_get_first_chunk_data(buf, &cp, &sz); tt_ptr_op(cp, OP_EQ, NULL); tt_uint_op(sz, OP_EQ, 0); /* Let's make sure nothing got allocated */ tt_int_op(buf_get_total_allocation(), OP_EQ, 0); /* Case 1: everything puts into the first chunk with some moving. */ /* Let's add some data. */ crypto_rand(stuff, 16384); write_to_buf(stuff, 3000, buf); write_to_buf(stuff+3000, 3000, buf); buf_get_first_chunk_data(buf, &cp, &sz); tt_ptr_op(cp, OP_NE, NULL); tt_int_op(sz, OP_LE, 4096); /* Make room for 3000 bytes in the first chunk, so that the pullup-move code * can get tested. */ tt_int_op(fetch_from_buf(tmp, 3000, buf), OP_EQ, 3000); tt_mem_op(tmp,OP_EQ, stuff, 3000); buf_pullup(buf, 2048); assert_buf_ok(buf); buf_get_first_chunk_data(buf, &cp, &sz); tt_ptr_op(cp, OP_NE, NULL); tt_int_op(sz, OP_GE, 2048); tt_mem_op(cp,OP_EQ, stuff+3000, 2048); tt_int_op(3000, OP_EQ, buf_datalen(buf)); tt_int_op(fetch_from_buf(tmp, 3000, buf), OP_EQ, 0); tt_mem_op(tmp,OP_EQ, stuff+3000, 2048); buf_free(buf); /* Now try the large-chunk case. */ buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */ write_to_buf(stuff, 4000, buf); write_to_buf(stuff+4000, 4000, buf); write_to_buf(stuff+8000, 4000, buf); write_to_buf(stuff+12000, 4000, buf); tt_int_op(buf_datalen(buf), OP_EQ, 16000); buf_get_first_chunk_data(buf, &cp, &sz); tt_ptr_op(cp, OP_NE, NULL); tt_int_op(sz, OP_LE, 4096); buf_pullup(buf, 12500); assert_buf_ok(buf); buf_get_first_chunk_data(buf, &cp, &sz); tt_ptr_op(cp, OP_NE, NULL); tt_int_op(sz, OP_GE, 12500); tt_mem_op(cp,OP_EQ, stuff, 12500); tt_int_op(buf_datalen(buf), OP_EQ, 16000); fetch_from_buf(tmp, 12400, buf); tt_mem_op(tmp,OP_EQ, stuff, 12400); tt_int_op(buf_datalen(buf), OP_EQ, 3600); fetch_from_buf(tmp, 3500, buf); tt_mem_op(tmp,OP_EQ, stuff+12400, 3500); fetch_from_buf(tmp, 100, buf); tt_mem_op(tmp,OP_EQ, stuff+15900, 10); buf_free(buf); /* Make sure that the pull-up-whole-buffer case works */ buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */ write_to_buf(stuff, 4000, buf); write_to_buf(stuff+4000, 4000, buf); fetch_from_buf(tmp, 100, buf); /* dump 100 bytes from first chunk */ buf_pullup(buf, 16000); /* Way too much. */ assert_buf_ok(buf); buf_get_first_chunk_data(buf, &cp, &sz); tt_ptr_op(cp, OP_NE, NULL); tt_int_op(sz, OP_EQ, 7900); tt_mem_op(cp,OP_EQ, stuff+100, 7900); buf_free(buf); buf = NULL; tt_int_op(buf_get_total_allocation(), OP_EQ, 0); done: buf_free(buf); tor_free(stuff); tor_free(tmp); }
static void test_buffer_allocation_tracking(void *arg) { char *junk = tor_malloc(16384); buf_t *buf1 = NULL, *buf2 = NULL; int i; (void)arg; crypto_rand(junk, 16384); tt_int_op(buf_get_total_allocation(), OP_EQ, 0); buf1 = buf_new(); tt_assert(buf1); buf2 = buf_new(); tt_assert(buf2); tt_int_op(buf_allocation(buf1), OP_EQ, 0); tt_int_op(buf_get_total_allocation(), OP_EQ, 0); write_to_buf(junk, 4000, buf1); write_to_buf(junk, 4000, buf1); write_to_buf(junk, 4000, buf1); write_to_buf(junk, 4000, buf1); tt_int_op(buf_allocation(buf1), OP_EQ, 16384); fetch_from_buf(junk, 100, buf1); tt_int_op(buf_allocation(buf1), OP_EQ, 16384); /* still 4 4k chunks */ tt_int_op(buf_get_total_allocation(), OP_EQ, 16384); fetch_from_buf(junk, 4096, buf1); /* drop a 1k chunk... */ tt_int_op(buf_allocation(buf1), OP_EQ, 3*4096); /* now 3 4k chunks */ tt_int_op(buf_get_total_allocation(), OP_EQ, 12288); /* that chunk was really freed. */ write_to_buf(junk, 4000, buf2); tt_int_op(buf_allocation(buf2), OP_EQ, 4096); /* another 4k chunk. */ /* * We bounce back up to 16384 by allocating a new chunk. */ tt_int_op(buf_get_total_allocation(), OP_EQ, 16384); write_to_buf(junk, 4000, buf2); tt_int_op(buf_allocation(buf2), OP_EQ, 8192); /* another 4k chunk. */ tt_int_op(buf_get_total_allocation(), OP_EQ, 5*4096); /* that chunk was new. */ /* Make a really huge buffer */ for (i = 0; i < 1000; ++i) { write_to_buf(junk, 4000, buf2); } tt_int_op(buf_allocation(buf2), OP_GE, 4008000); tt_int_op(buf_get_total_allocation(), OP_GE, 4008000); buf_free(buf2); buf2 = NULL; tt_int_op(buf_get_total_allocation(), OP_LT, 4008000); tt_int_op(buf_get_total_allocation(), OP_EQ, buf_allocation(buf1)); buf_free(buf1); buf1 = NULL; tt_int_op(buf_get_total_allocation(), OP_EQ, 0); done: buf_free(buf1); buf_free(buf2); tor_free(junk); }
static void test_buffer_allocation_tracking(void *arg) { char *junk = tor_malloc(16384); buf_t *buf1 = NULL, *buf2 = NULL; int i; (void)arg; crypto_rand(junk, 16384); tt_int_op(buf_get_total_allocation(), OP_EQ, 0); buf1 = buf_new(); tt_assert(buf1); buf2 = buf_new(); tt_assert(buf2); tt_int_op(buf_allocation(buf1), OP_EQ, 0); tt_int_op(buf_get_total_allocation(), OP_EQ, 0); write_to_buf(junk, 4000, buf1); write_to_buf(junk, 4000, buf1); write_to_buf(junk, 4000, buf1); write_to_buf(junk, 4000, buf1); tt_int_op(buf_allocation(buf1), OP_EQ, 16384); fetch_from_buf(junk, 100, buf1); tt_int_op(buf_allocation(buf1), OP_EQ, 16384); /* still 4 4k chunks */ tt_int_op(buf_get_total_allocation(), OP_EQ, 16384); fetch_from_buf(junk, 4096, buf1); /* drop a 1k chunk... */ tt_int_op(buf_allocation(buf1), OP_EQ, 3*4096); /* now 3 4k chunks */ #ifdef ENABLE_BUF_FREELISTS tt_int_op(buf_get_total_allocation(), OP_EQ, 16384); /* that chunk went onto the freelist. */ #else tt_int_op(buf_get_total_allocation(), OP_EQ, 12288); /* that chunk was really freed. */ #endif write_to_buf(junk, 4000, buf2); tt_int_op(buf_allocation(buf2), OP_EQ, 4096); /* another 4k chunk. */ /* * If we're using freelists, size stays at 16384 because we just pulled a * chunk from the freelist. If we aren't, we bounce back up to 16384 by * allocating a new chunk. */ tt_int_op(buf_get_total_allocation(), OP_EQ, 16384); write_to_buf(junk, 4000, buf2); tt_int_op(buf_allocation(buf2), OP_EQ, 8192); /* another 4k chunk. */ tt_int_op(buf_get_total_allocation(), OP_EQ, 5*4096); /* that chunk was new. */ /* Make a really huge buffer */ for (i = 0; i < 1000; ++i) { write_to_buf(junk, 4000, buf2); } tt_int_op(buf_allocation(buf2), OP_GE, 4008000); tt_int_op(buf_get_total_allocation(), OP_GE, 4008000); buf_free(buf2); buf2 = NULL; tt_int_op(buf_get_total_allocation(), OP_LT, 4008000); buf_shrink_freelists(1); tt_int_op(buf_get_total_allocation(), OP_EQ, buf_allocation(buf1)); buf_free(buf1); buf1 = NULL; buf_shrink_freelists(1); tt_int_op(buf_get_total_allocation(), OP_EQ, 0); done: buf_free(buf1); buf_free(buf2); buf_shrink_freelists(1); tor_free(junk); }