test_oom.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. #ifdef ENABLE_MEMPOOLS
  14. #include "mempool.h"
  15. #endif
  16. #include "relay.h"
  17. #include "test.h"
  18. /* small replacement mock for circuit_mark_for_close_ to avoid doing all
  19. * the other bookkeeping that comes with marking circuits. */
  20. static void
  21. circuit_mark_for_close_dummy_(circuit_t *circ, int reason, int line,
  22. const char *file)
  23. {
  24. (void) reason;
  25. if (circ->marked_for_close) {
  26. TT_FAIL(("Circuit already marked for close at %s:%d, but we are marking "
  27. "it again at %s:%d",
  28. circ->marked_for_close_file, (int)circ->marked_for_close,
  29. file, line));
  30. }
  31. circ->marked_for_close = line;
  32. circ->marked_for_close_file = file;
  33. }
  34. static circuit_t *
  35. dummy_or_circuit_new(int n_p_cells, int n_n_cells)
  36. {
  37. or_circuit_t *circ = or_circuit_new(0, NULL);
  38. int i;
  39. cell_t cell;
  40. for (i=0; i < n_p_cells; ++i) {
  41. crypto_rand((void*)&cell, sizeof(cell));
  42. cell_queue_append_packed_copy(TO_CIRCUIT(circ), &circ->p_chan_cells,
  43. 0, &cell, 1, 0);
  44. }
  45. for (i=0; i < n_n_cells; ++i) {
  46. crypto_rand((void*)&cell, sizeof(cell));
  47. cell_queue_append_packed_copy(TO_CIRCUIT(circ),
  48. &TO_CIRCUIT(circ)->n_chan_cells,
  49. 1, &cell, 1, 0);
  50. }
  51. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_OR;
  52. return TO_CIRCUIT(circ);
  53. }
  54. static circuit_t *
  55. dummy_origin_circuit_new(int n_cells)
  56. {
  57. origin_circuit_t *circ = origin_circuit_new();
  58. int i;
  59. cell_t cell;
  60. for (i=0; i < n_cells; ++i) {
  61. crypto_rand((void*)&cell, sizeof(cell));
  62. cell_queue_append_packed_copy(TO_CIRCUIT(circ),
  63. &TO_CIRCUIT(circ)->n_chan_cells,
  64. 1, &cell, 1, 0);
  65. }
  66. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  67. return TO_CIRCUIT(circ);
  68. }
  69. static void
  70. add_bytes_to_buf(generic_buffer_t *buf, size_t n_bytes)
  71. {
  72. char b[3000];
  73. while (n_bytes) {
  74. size_t this_add = n_bytes > sizeof(b) ? sizeof(b) : n_bytes;
  75. crypto_rand(b, this_add);
  76. generic_buffer_add(buf, b, this_add);
  77. n_bytes -= this_add;
  78. }
  79. }
  80. static edge_connection_t *
  81. dummy_edge_conn_new(circuit_t *circ,
  82. int type, size_t in_bytes, size_t out_bytes)
  83. {
  84. edge_connection_t *conn;
  85. if (type == CONN_TYPE_EXIT)
  86. conn = edge_connection_new(type, AF_INET);
  87. else
  88. conn = ENTRY_TO_EDGE_CONN(entry_connection_new(type, AF_INET));
  89. /* We add these bytes directly to the buffers, to avoid all the
  90. * edge connection read/write machinery. */
  91. add_bytes_to_buf(TO_CONN(conn)->inbuf, in_bytes);
  92. add_bytes_to_buf(TO_CONN(conn)->outbuf, out_bytes);
  93. conn->on_circuit = circ;
  94. if (type == CONN_TYPE_EXIT) {
  95. or_circuit_t *oc = TO_OR_CIRCUIT(circ);
  96. conn->next_stream = oc->n_streams;
  97. oc->n_streams = conn;
  98. } else {
  99. origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(circ);
  100. conn->next_stream = oc->p_streams;
  101. oc->p_streams = conn;
  102. }
  103. return conn;
  104. }
  105. /** Run unit tests for buffers.c */
  106. static void
  107. test_oom_circbuf(void *arg)
  108. {
  109. or_options_t *options = get_options_mutable();
  110. circuit_t *c1 = NULL, *c2 = NULL, *c3 = NULL, *c4 = NULL;
  111. struct timeval tv = { 1389631048, 0 };
  112. (void) arg;
  113. MOCK(circuit_mark_for_close_, circuit_mark_for_close_dummy_);
  114. #ifdef ENABLE_MEMPOOLS
  115. init_cell_pool();
  116. #endif /* ENABLE_MEMPOOLS */
  117. /* Far too low for real life. */
  118. options->MaxMemInQueues = 256*packed_cell_mem_cost();
  119. options->CellStatistics = 0;
  120. tt_int_op(cell_queues_check_size(), ==, 0); /* We don't start out OOM. */
  121. tt_int_op(cell_queues_get_total_allocation(), ==, 0);
  122. tt_int_op(buf_get_total_allocation(), ==, 0);
  123. /* Now we're going to fake up some circuits and get them added to the global
  124. circuit list. */
  125. tv.tv_usec = 0;
  126. tor_gettimeofday_cache_set(&tv);
  127. c1 = dummy_origin_circuit_new(30);
  128. tv.tv_usec = 10*1000;
  129. tor_gettimeofday_cache_set(&tv);
  130. c2 = dummy_or_circuit_new(20, 20);
  131. #ifdef ENABLE_MEMPOOLS
  132. tt_int_op(packed_cell_mem_cost(), ==,
  133. sizeof(packed_cell_t) + MP_POOL_ITEM_OVERHEAD);
  134. #else
  135. tt_int_op(packed_cell_mem_cost(), ==,
  136. sizeof(packed_cell_t));
  137. #endif /* ENABLE_MEMPOOLS */
  138. tt_int_op(cell_queues_get_total_allocation(), ==,
  139. packed_cell_mem_cost() * 70);
  140. tt_int_op(cell_queues_check_size(), ==, 0); /* We are still not OOM */
  141. tv.tv_usec = 20*1000;
  142. tor_gettimeofday_cache_set(&tv);
  143. c3 = dummy_or_circuit_new(100, 85);
  144. tt_int_op(cell_queues_check_size(), ==, 0); /* We are still not OOM */
  145. tt_int_op(cell_queues_get_total_allocation(), ==,
  146. packed_cell_mem_cost() * 255);
  147. tv.tv_usec = 30*1000;
  148. tor_gettimeofday_cache_set(&tv);
  149. /* Adding this cell will trigger our OOM handler. */
  150. c4 = dummy_or_circuit_new(2, 0);
  151. tt_int_op(cell_queues_get_total_allocation(), ==,
  152. packed_cell_mem_cost() * 257);
  153. tt_int_op(cell_queues_check_size(), ==, 1); /* We are now OOM */
  154. tt_assert(c1->marked_for_close);
  155. tt_assert(! c2->marked_for_close);
  156. tt_assert(! c3->marked_for_close);
  157. tt_assert(! c4->marked_for_close);
  158. tt_int_op(cell_queues_get_total_allocation(), ==,
  159. packed_cell_mem_cost() * (257 - 30));
  160. circuit_free(c1);
  161. tv.tv_usec = 0;
  162. tor_gettimeofday_cache_set(&tv); /* go back in time */
  163. c1 = dummy_or_circuit_new(90, 0);
  164. tv.tv_usec = 40*1000; /* go back to the future */
  165. tor_gettimeofday_cache_set(&tv);
  166. tt_int_op(cell_queues_check_size(), ==, 1); /* We are now OOM */
  167. tt_assert(c1->marked_for_close);
  168. tt_assert(! c2->marked_for_close);
  169. tt_assert(! c3->marked_for_close);
  170. tt_assert(! c4->marked_for_close);
  171. tt_int_op(cell_queues_get_total_allocation(), ==,
  172. packed_cell_mem_cost() * (257 - 30));
  173. done:
  174. circuit_free(c1);
  175. circuit_free(c2);
  176. circuit_free(c3);
  177. circuit_free(c4);
  178. UNMOCK(circuit_mark_for_close_);
  179. }
  180. /** Run unit tests for buffers.c */
  181. static void
  182. test_oom_streambuf(void *arg)
  183. {
  184. or_options_t *options = get_options_mutable();
  185. circuit_t *c1 = NULL, *c2 = NULL, *c3 = NULL, *c4 = NULL, *c5 = NULL;
  186. struct timeval tv = { 1389641159, 0 };
  187. uint32_t tvms;
  188. int i;
  189. (void) arg;
  190. MOCK(circuit_mark_for_close_, circuit_mark_for_close_dummy_);
  191. #ifdef ENABLE_MEMPOOLS
  192. init_cell_pool();
  193. #endif /* ENABLE_MEMPOOLS */
  194. /* Far too low for real life. */
  195. options->MaxMemInQueues = 81*packed_cell_mem_cost() + 4096 * 34;
  196. options->CellStatistics = 0;
  197. tt_int_op(cell_queues_check_size(), ==, 0); /* We don't start out OOM. */
  198. tt_int_op(cell_queues_get_total_allocation(), ==, 0);
  199. tt_int_op(buf_get_total_allocation(), ==, 0);
  200. /* Start all circuits with a bit of data queued in cells */
  201. tv.tv_usec = 500*1000; /* go halfway into the second. */
  202. tor_gettimeofday_cache_set(&tv);
  203. c1 = dummy_or_circuit_new(10,10);
  204. tv.tv_usec = 510*1000;
  205. tor_gettimeofday_cache_set(&tv);
  206. c2 = dummy_origin_circuit_new(20);
  207. tv.tv_usec = 520*1000;
  208. tor_gettimeofday_cache_set(&tv);
  209. c3 = dummy_or_circuit_new(20,20);
  210. tv.tv_usec = 530*1000;
  211. tor_gettimeofday_cache_set(&tv);
  212. c4 = dummy_or_circuit_new(0,0);
  213. tt_int_op(cell_queues_get_total_allocation(), ==,
  214. packed_cell_mem_cost() * 80);
  215. tv.tv_usec = 600*1000;
  216. tor_gettimeofday_cache_set(&tv);
  217. /* Add some connections to c1...c4. */
  218. for (i = 0; i < 4; ++i) {
  219. edge_connection_t *ec;
  220. /* link it to a circuit */
  221. tv.tv_usec += 10*1000;
  222. tor_gettimeofday_cache_set(&tv);
  223. ec = dummy_edge_conn_new(c1, CONN_TYPE_EXIT, 1000, 1000);
  224. tt_assert(ec);
  225. tv.tv_usec += 10*1000;
  226. tor_gettimeofday_cache_set(&tv);
  227. ec = dummy_edge_conn_new(c2, CONN_TYPE_AP, 1000, 1000);
  228. tt_assert(ec);
  229. tv.tv_usec += 10*1000;
  230. tor_gettimeofday_cache_set(&tv);
  231. ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000); /* Yes, 4 twice*/
  232. tt_assert(ec);
  233. tv.tv_usec += 10*1000;
  234. tor_gettimeofday_cache_set(&tv);
  235. ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000);
  236. tt_assert(ec);
  237. }
  238. tv.tv_sec += 1;
  239. tv.tv_usec = 0;
  240. tvms = (uint32_t) tv_to_msec(&tv);
  241. tt_int_op(circuit_max_queued_cell_age(c1, tvms), ==, 500);
  242. tt_int_op(circuit_max_queued_cell_age(c2, tvms), ==, 490);
  243. tt_int_op(circuit_max_queued_cell_age(c3, tvms), ==, 480);
  244. tt_int_op(circuit_max_queued_cell_age(c4, tvms), ==, 0);
  245. tt_int_op(circuit_max_queued_data_age(c1, tvms), ==, 390);
  246. tt_int_op(circuit_max_queued_data_age(c2, tvms), ==, 380);
  247. tt_int_op(circuit_max_queued_data_age(c3, tvms), ==, 0);
  248. tt_int_op(circuit_max_queued_data_age(c4, tvms), ==, 370);
  249. tt_int_op(circuit_max_queued_item_age(c1, tvms), ==, 500);
  250. tt_int_op(circuit_max_queued_item_age(c2, tvms), ==, 490);
  251. tt_int_op(circuit_max_queued_item_age(c3, tvms), ==, 480);
  252. tt_int_op(circuit_max_queued_item_age(c4, tvms), ==, 370);
  253. tt_int_op(cell_queues_get_total_allocation(), ==,
  254. packed_cell_mem_cost() * 80);
  255. tt_int_op(buf_get_total_allocation(), ==, 4096*16*2);
  256. /* Now give c4 a very old buffer of modest size */
  257. {
  258. edge_connection_t *ec;
  259. tv.tv_sec -= 1;
  260. tv.tv_usec = 0;
  261. tor_gettimeofday_cache_set(&tv);
  262. ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000);
  263. tt_assert(ec);
  264. }
  265. tt_int_op(buf_get_total_allocation(), ==, 4096*17*2);
  266. tt_int_op(circuit_max_queued_item_age(c4, tvms), ==, 1000);
  267. tt_int_op(cell_queues_check_size(), ==, 0);
  268. /* And run over the limit. */
  269. tv.tv_usec = 800*1000;
  270. tor_gettimeofday_cache_set(&tv);
  271. c5 = dummy_or_circuit_new(0,5);
  272. tt_int_op(cell_queues_get_total_allocation(), ==,
  273. packed_cell_mem_cost() * 85);
  274. tt_int_op(buf_get_total_allocation(), ==, 4096*17*2);
  275. tt_int_op(cell_queues_check_size(), ==, 1); /* We are now OOM */
  276. /* C4 should have died. */
  277. tt_assert(! c1->marked_for_close);
  278. tt_assert(! c2->marked_for_close);
  279. tt_assert(! c3->marked_for_close);
  280. tt_assert(c4->marked_for_close);
  281. tt_assert(! c5->marked_for_close);
  282. tt_int_op(cell_queues_get_total_allocation(), ==,
  283. packed_cell_mem_cost() * 85);
  284. tt_int_op(buf_get_total_allocation(), ==, 4096*8*2);
  285. done:
  286. circuit_free(c1);
  287. circuit_free(c2);
  288. circuit_free(c3);
  289. circuit_free(c4);
  290. circuit_free(c5);
  291. UNMOCK(circuit_mark_for_close_);
  292. }
  293. struct testcase_t oom_tests[] = {
  294. { "circbuf", test_oom_circbuf, TT_FORK, NULL, NULL },
  295. { "streambuf", test_oom_streambuf, TT_FORK, NULL, NULL },
  296. END_OF_TESTCASES
  297. };