test_oom.c 11 KB

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