test_oom.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* Copyright (c) 2014, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /* Unit tests for OOM handling logic */
  4. #define RELAY_PRIVATE
  5. #define BUFFERS_PRIVATE
  6. #define CIRCUITLIST_PRIVATE
  7. #include "or.h"
  8. #include "buffers.h"
  9. #include "circuitlist.h"
  10. #include "compat_libevent.h"
  11. #include "connection.h"
  12. #include "config.h"
  13. #include "mempool.h"
  14. #include "relay.h"
  15. #include "test.h"
  16. /* small replacement mock for circuit_mark_for_close_ to avoid doing all
  17. * the other bookkeeping that comes with marking circuits. */
  18. static void
  19. circuit_mark_for_close_dummy_(circuit_t *circ, int reason, int line,
  20. const char *file)
  21. {
  22. (void) reason;
  23. if (circ->marked_for_close) {
  24. TT_FAIL(("Circuit already marked for close at %s:%d, but we are marking "
  25. "it again at %s:%d",
  26. circ->marked_for_close_file, (int)circ->marked_for_close,
  27. file, line));
  28. }
  29. circ->marked_for_close = line;
  30. circ->marked_for_close_file = file;
  31. }
  32. static circuit_t *
  33. dummy_or_circuit_new(int n_p_cells, int n_n_cells)
  34. {
  35. or_circuit_t *circ = or_circuit_new(0, NULL);
  36. int i;
  37. cell_t cell;
  38. for (i=0; i < n_p_cells; ++i) {
  39. crypto_rand((void*)&cell, sizeof(cell));
  40. cell_queue_append_packed_copy(TO_CIRCUIT(circ), &circ->p_chan_cells,
  41. 0, &cell, 1, 0);
  42. }
  43. for (i=0; i < n_n_cells; ++i) {
  44. crypto_rand((void*)&cell, sizeof(cell));
  45. cell_queue_append_packed_copy(TO_CIRCUIT(circ),
  46. &TO_CIRCUIT(circ)->n_chan_cells,
  47. 1, &cell, 1, 0);
  48. }
  49. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_OR;
  50. return TO_CIRCUIT(circ);
  51. }
  52. static circuit_t *
  53. dummy_origin_circuit_new(int n_cells)
  54. {
  55. origin_circuit_t *circ = origin_circuit_new();
  56. int i;
  57. cell_t cell;
  58. for (i=0; i < n_cells; ++i) {
  59. crypto_rand((void*)&cell, sizeof(cell));
  60. cell_queue_append_packed_copy(TO_CIRCUIT(circ),
  61. &TO_CIRCUIT(circ)->n_chan_cells,
  62. 1, &cell, 1, 0);
  63. }
  64. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  65. return TO_CIRCUIT(circ);
  66. }
  67. #if 0
  68. static void
  69. add_bytes_to_buf(generic_buffer_t *buf, size_t n_bytes)
  70. {
  71. char b[3000];
  72. while (n_bytes) {
  73. size_t this_add = n_bytes > sizeof(buf) ? sizeof(buf) : n_bytes;
  74. crypto_rand(b, sizeof(b));
  75. generic_buffer_add(buf, b, this_add);
  76. n_bytes -= this_add;
  77. }
  78. }
  79. static edge_connection_t *
  80. dummy_edge_conn_new(int type, size_t in_bytes, size_t out_bytes)
  81. {
  82. edge_connection_t *conn = edge_connection_new(type, AF_INET);
  83. /* We add these bytes directly to the buffers, to avoid all the
  84. * edge connection read/write machinery. */
  85. add_bytes_to_buf(TO_CONN(conn)->inbuf, in_bytes);
  86. add_bytes_to_buf(TO_CONN(conn)->outbuf, out_bytes);
  87. return conn;
  88. }
  89. #endif
  90. /** Run unit tests for buffers.c */
  91. static void
  92. test_oom_circbuf(void *arg)
  93. {
  94. or_options_t *options = get_options_mutable();
  95. circuit_t *c1 = NULL, *c2 = NULL, *c3 = NULL, *c4 = NULL;
  96. struct timeval tv = { 1389631048, 0 };
  97. (void) arg;
  98. MOCK(circuit_mark_for_close_, circuit_mark_for_close_dummy_);
  99. init_cell_pool();
  100. /* Far too low for real life. */
  101. options->MaxMemInQueues = 256*packed_cell_mem_cost();
  102. options->CellStatistics = 0;
  103. tt_int_op(cell_queues_check_size(), ==, 0); /* We don't start out OOM. */
  104. tt_int_op(cell_queues_get_total_allocation(), ==, 0);
  105. tt_int_op(buf_get_total_allocation(), ==, 0);
  106. /* Now we're going to fake up some circuits and get them added to the global
  107. circuit list. */
  108. tv.tv_usec = 0;
  109. tor_gettimeofday_cache_set(&tv);
  110. c1 = dummy_origin_circuit_new(30);
  111. tv.tv_usec = 10*1000;
  112. tor_gettimeofday_cache_set(&tv);
  113. c2 = dummy_or_circuit_new(20, 20);
  114. tt_int_op(packed_cell_mem_cost(), ==,
  115. sizeof(packed_cell_t) + MP_POOL_ITEM_OVERHEAD);
  116. tt_int_op(cell_queues_get_total_allocation(), ==,
  117. packed_cell_mem_cost() * 70);
  118. tt_int_op(cell_queues_check_size(), ==, 0); /* We are still not OOM */
  119. tv.tv_usec = 20*1000;
  120. tor_gettimeofday_cache_set(&tv);
  121. c3 = dummy_or_circuit_new(100, 85);
  122. tt_int_op(cell_queues_check_size(), ==, 0); /* We are still not OOM */
  123. tt_int_op(cell_queues_get_total_allocation(), ==,
  124. packed_cell_mem_cost() * 255);
  125. tv.tv_usec = 30*1000;
  126. tor_gettimeofday_cache_set(&tv);
  127. /* Adding this cell will trigger our OOM handler. */
  128. c4 = dummy_or_circuit_new(2, 0);
  129. tt_int_op(cell_queues_get_total_allocation(), ==,
  130. packed_cell_mem_cost() * 257);
  131. tt_int_op(cell_queues_check_size(), ==, 1); /* We are now OOM */
  132. tt_assert(c1->marked_for_close);
  133. tt_assert(! c2->marked_for_close);
  134. tt_assert(! c3->marked_for_close);
  135. tt_assert(! c4->marked_for_close);
  136. tt_int_op(cell_queues_get_total_allocation(), ==,
  137. packed_cell_mem_cost() * (257 - 30));
  138. circuit_free(c1);
  139. tv.tv_usec = 0;
  140. tor_gettimeofday_cache_set(&tv); /* go back in time */
  141. c1 = dummy_or_circuit_new(90, 0);
  142. tv.tv_usec = 40*1000; /* go back to the future */
  143. tor_gettimeofday_cache_set(&tv);
  144. tt_int_op(cell_queues_check_size(), ==, 1); /* We are now OOM */
  145. tt_assert(c1->marked_for_close);
  146. tt_assert(! c2->marked_for_close);
  147. tt_assert(! c3->marked_for_close);
  148. tt_assert(! c4->marked_for_close);
  149. tt_int_op(cell_queues_get_total_allocation(), ==,
  150. packed_cell_mem_cost() * (257 - 30));
  151. done:
  152. circuit_free(c1);
  153. circuit_free(c2);
  154. circuit_free(c3);
  155. circuit_free(c4);
  156. UNMOCK(circuit_mark_for_close_);
  157. }
  158. struct testcase_t oom_tests[] = {
  159. { "circbuf", test_oom_circbuf, TT_FORK, NULL, NULL },
  160. END_OF_TESTCASES
  161. };