test_oom.c 11 KB

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