test_oom.c 11 KB

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