test_oom.c 11 KB

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