PageRenderTime 41ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/tor/src/test/test_oom.c

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