test_oom.c 11 KB

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