test_oom.c 11 KB

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