test_oom.c 11 KB

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