test_oom.c 11 KB

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