1 /* Copyright (c) 2014-2016, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /* Unit tests for OOM handling logic */
7 #define BUFFERS_PRIVATE
8 #define CIRCUITLIST_PRIVATE
9 #define CONNECTION_PRIVATE
12 #include "circuitlist.h"
13 #include "compat_libevent.h"
14 #include "connection.h"
19 /* small replacement mock for circuit_mark_for_close_ to avoid doing all
20 * the other bookkeeping that comes with marking circuits. */
22 circuit_mark_for_close_dummy_(circuit_t
*circ
, int reason
, int line
,
26 if (circ
->marked_for_close
) {
27 TT_FAIL(("Circuit already marked for close at %s:%d, but we are marking "
29 circ
->marked_for_close_file
, (int)circ
->marked_for_close
,
33 circ
->marked_for_close
= line
;
34 circ
->marked_for_close_file
= file
;
38 dummy_or_circuit_new(int n_p_cells
, int n_n_cells
)
40 or_circuit_t
*circ
= or_circuit_new(0, NULL
);
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
,
50 for (i
=0; i
< n_n_cells
; ++i
) {
51 crypto_rand((void*)&cell
, sizeof(cell
));
52 cell_queue_append_packed_copy(TO_CIRCUIT(circ
),
53 &TO_CIRCUIT(circ
)->n_chan_cells
,
57 TO_CIRCUIT(circ
)->purpose
= CIRCUIT_PURPOSE_OR
;
58 return TO_CIRCUIT(circ
);
62 dummy_origin_circuit_new(int n_cells
)
64 origin_circuit_t
*circ
= origin_circuit_new();
68 for (i
=0; i
< n_cells
; ++i
) {
69 crypto_rand((void*)&cell
, sizeof(cell
));
70 cell_queue_append_packed_copy(TO_CIRCUIT(circ
),
71 &TO_CIRCUIT(circ
)->n_chan_cells
,
75 TO_CIRCUIT(circ
)->purpose
= CIRCUIT_PURPOSE_C_GENERAL
;
76 return TO_CIRCUIT(circ
);
80 add_bytes_to_buf(generic_buffer_t
*buf
, size_t n_bytes
)
85 size_t this_add
= n_bytes
> sizeof(b
) ? sizeof(b
) : n_bytes
;
86 crypto_rand(b
, this_add
);
87 generic_buffer_add(buf
, b
, this_add
);
92 static edge_connection_t
*
93 dummy_edge_conn_new(circuit_t
*circ
,
94 int type
, size_t in_bytes
, size_t out_bytes
)
96 edge_connection_t
*conn
;
97 generic_buffer_t
*inbuf
, *outbuf
;
99 if (type
== CONN_TYPE_EXIT
)
100 conn
= edge_connection_new(type
, AF_INET
);
102 conn
= ENTRY_TO_EDGE_CONN(entry_connection_new(type
, AF_INET
));
104 #ifdef USE_BUFFEREVENTS
105 inbuf
= bufferevent_get_input(TO_CONN(conn
)->bufev
);
106 outbuf
= bufferevent_get_output(TO_CONN(conn
)->bufev
);
108 inbuf
= TO_CONN(conn
)->inbuf
;
109 outbuf
= TO_CONN(conn
)->outbuf
;
112 /* We add these bytes directly to the buffers, to avoid all the
113 * edge connection read/write machinery. */
114 add_bytes_to_buf(inbuf
, in_bytes
);
115 add_bytes_to_buf(outbuf
, out_bytes
);
117 conn
->on_circuit
= circ
;
118 if (type
== CONN_TYPE_EXIT
) {
119 or_circuit_t
*oc
= TO_OR_CIRCUIT(circ
);
120 conn
->next_stream
= oc
->n_streams
;
121 oc
->n_streams
= conn
;
123 origin_circuit_t
*oc
= TO_ORIGIN_CIRCUIT(circ
);
124 conn
->next_stream
= oc
->p_streams
;
125 oc
->p_streams
= conn
;
131 /** Run unit tests for buffers.c */
133 test_oom_circbuf(void *arg
)
135 or_options_t
*options
= get_options_mutable();
136 circuit_t
*c1
= NULL
, *c2
= NULL
, *c3
= NULL
, *c4
= NULL
;
137 uint64_t now_ns
= 1389631048 * (uint64_t)1000000000;
138 const uint64_t start_ns
= now_ns
;
142 monotime_enable_test_mocking();
143 MOCK(circuit_mark_for_close_
, circuit_mark_for_close_dummy_
);
145 /* Far too low for real life. */
146 options
->MaxMemInQueues
= 256*packed_cell_mem_cost();
147 options
->CellStatistics
= 0;
149 tt_int_op(cell_queues_check_size(), OP_EQ
, 0); /* We don't start out OOM. */
150 tt_int_op(cell_queues_get_total_allocation(), OP_EQ
, 0);
151 tt_int_op(buf_get_total_allocation(), OP_EQ
, 0);
153 /* Now we're going to fake up some circuits and get them added to the global
155 monotime_coarse_set_mock_time_nsec(now_ns
);
156 c1
= dummy_origin_circuit_new(30);
158 now_ns
+= 10 * 1000000;
159 monotime_coarse_set_mock_time_nsec(now_ns
);
160 c2
= dummy_or_circuit_new(20, 20);
162 tt_int_op(packed_cell_mem_cost(), OP_EQ
,
163 sizeof(packed_cell_t
));
164 tt_int_op(cell_queues_get_total_allocation(), OP_EQ
,
165 packed_cell_mem_cost() * 70);
166 tt_int_op(cell_queues_check_size(), OP_EQ
, 0); /* We are still not OOM */
168 now_ns
+= 10 * 1000000;
169 monotime_coarse_set_mock_time_nsec(now_ns
);
170 c3
= dummy_or_circuit_new(100, 85);
171 tt_int_op(cell_queues_check_size(), OP_EQ
, 0); /* We are still not OOM */
172 tt_int_op(cell_queues_get_total_allocation(), OP_EQ
,
173 packed_cell_mem_cost() * 255);
175 now_ns
+= 10 * 1000000;
176 monotime_coarse_set_mock_time_nsec(now_ns
);
177 /* Adding this cell will trigger our OOM handler. */
178 c4
= dummy_or_circuit_new(2, 0);
180 tt_int_op(cell_queues_get_total_allocation(), OP_EQ
,
181 packed_cell_mem_cost() * 257);
183 tt_int_op(cell_queues_check_size(), OP_EQ
, 1); /* We are now OOM */
185 tt_assert(c1
->marked_for_close
);
186 tt_assert(! c2
->marked_for_close
);
187 tt_assert(! c3
->marked_for_close
);
188 tt_assert(! c4
->marked_for_close
);
190 tt_int_op(cell_queues_get_total_allocation(), OP_EQ
,
191 packed_cell_mem_cost() * (257 - 30));
195 monotime_coarse_set_mock_time_nsec(start_ns
); /* go back in time */
196 c1
= dummy_or_circuit_new(90, 0);
198 now_ns
+= 10 * 1000000;
199 monotime_coarse_set_mock_time_nsec(now_ns
);
201 tt_int_op(cell_queues_check_size(), OP_EQ
, 1); /* We are now OOM */
203 tt_assert(c1
->marked_for_close
);
204 tt_assert(! c2
->marked_for_close
);
205 tt_assert(! c3
->marked_for_close
);
206 tt_assert(! c4
->marked_for_close
);
208 tt_int_op(cell_queues_get_total_allocation(), OP_EQ
,
209 packed_cell_mem_cost() * (257 - 30));
217 UNMOCK(circuit_mark_for_close_
);
218 monotime_disable_test_mocking();
221 /** Run unit tests for buffers.c */
223 test_oom_streambuf(void *arg
)
225 or_options_t
*options
= get_options_mutable();
226 circuit_t
*c1
= NULL
, *c2
= NULL
, *c3
= NULL
, *c4
= NULL
, *c5
= NULL
;
229 smartlist_t
*edgeconns
= smartlist_new();
230 const uint64_t start_ns
= 1389641159 * (uint64_t)1000000000;
231 uint64_t now_ns
= start_ns
;
234 monotime_enable_test_mocking();
236 MOCK(circuit_mark_for_close_
, circuit_mark_for_close_dummy_
);
238 /* Far too low for real life. */
239 options
->MaxMemInQueues
= 81*packed_cell_mem_cost() + 4096 * 34;
240 options
->CellStatistics
= 0;
242 tt_int_op(cell_queues_check_size(), OP_EQ
, 0); /* We don't start out OOM. */
243 tt_int_op(cell_queues_get_total_allocation(), OP_EQ
, 0);
244 tt_int_op(buf_get_total_allocation(), OP_EQ
, 0);
246 monotime_coarse_set_mock_time_nsec(start_ns
);
248 /* Start all circuits with a bit of data queued in cells */
250 /* go halfway into the second. */
251 monotime_coarse_set_mock_time_nsec(start_ns
+ 500 * 1000000);
252 c1
= dummy_or_circuit_new(10,10);
254 monotime_coarse_set_mock_time_nsec(start_ns
+ 510 * 1000000);
255 c2
= dummy_origin_circuit_new(20);
256 monotime_coarse_set_mock_time_nsec(start_ns
+ 520 * 1000000);
257 c3
= dummy_or_circuit_new(20,20);
258 monotime_coarse_set_mock_time_nsec(start_ns
+ 530 * 1000000);
259 c4
= dummy_or_circuit_new(0,0);
260 tt_int_op(cell_queues_get_total_allocation(), OP_EQ
,
261 packed_cell_mem_cost() * 80);
263 now_ns
= start_ns
+ 600 * 1000000;
264 monotime_coarse_set_mock_time_nsec(now_ns
);
266 /* Add some connections to c1...c4. */
267 for (i
= 0; i
< 4; ++i
) {
268 edge_connection_t
*ec
;
269 /* link it to a circuit */
270 now_ns
+= 10 * 1000000;
271 monotime_coarse_set_mock_time_nsec(now_ns
);
272 ec
= dummy_edge_conn_new(c1
, CONN_TYPE_EXIT
, 1000, 1000);
274 smartlist_add(edgeconns
, ec
);
275 now_ns
+= 10 * 1000000;
276 monotime_coarse_set_mock_time_nsec(now_ns
);
277 ec
= dummy_edge_conn_new(c2
, CONN_TYPE_AP
, 1000, 1000);
279 smartlist_add(edgeconns
, ec
);
280 now_ns
+= 10 * 1000000;
281 monotime_coarse_set_mock_time_nsec(now_ns
);
282 ec
= dummy_edge_conn_new(c4
, CONN_TYPE_EXIT
, 1000, 1000); /* Yes, 4 twice*/
284 smartlist_add(edgeconns
, ec
);
285 now_ns
+= 10 * 1000000;
286 monotime_coarse_set_mock_time_nsec(now_ns
);
287 ec
= dummy_edge_conn_new(c4
, CONN_TYPE_EXIT
, 1000, 1000);
288 smartlist_add(edgeconns
, ec
);
292 now_ns
-= now_ns
% 1000000000;
293 now_ns
+= 1000000000;
294 monotime_coarse_set_mock_time_nsec(now_ns
);
295 tvms
= (uint32_t) monotime_coarse_absolute_msec();
297 tt_int_op(circuit_max_queued_cell_age(c1
, tvms
), OP_EQ
, 500);
298 tt_int_op(circuit_max_queued_cell_age(c2
, tvms
), OP_EQ
, 490);
299 tt_int_op(circuit_max_queued_cell_age(c3
, tvms
), OP_EQ
, 480);
300 tt_int_op(circuit_max_queued_cell_age(c4
, tvms
), OP_EQ
, 0);
302 tt_int_op(circuit_max_queued_data_age(c1
, tvms
), OP_EQ
, 390);
303 tt_int_op(circuit_max_queued_data_age(c2
, tvms
), OP_EQ
, 380);
304 tt_int_op(circuit_max_queued_data_age(c3
, tvms
), OP_EQ
, 0);
305 tt_int_op(circuit_max_queued_data_age(c4
, tvms
), OP_EQ
, 370);
307 tt_int_op(circuit_max_queued_item_age(c1
, tvms
), OP_EQ
, 500);
308 tt_int_op(circuit_max_queued_item_age(c2
, tvms
), OP_EQ
, 490);
309 tt_int_op(circuit_max_queued_item_age(c3
, tvms
), OP_EQ
, 480);
310 tt_int_op(circuit_max_queued_item_age(c4
, tvms
), OP_EQ
, 370);
312 tt_int_op(cell_queues_get_total_allocation(), OP_EQ
,
313 packed_cell_mem_cost() * 80);
314 tt_int_op(buf_get_total_allocation(), OP_EQ
, 4096*16*2);
316 /* Now give c4 a very old buffer of modest size */
318 edge_connection_t
*ec
;
319 now_ns
-= 1000000000;
320 monotime_coarse_set_mock_time_nsec(now_ns
);
321 ec
= dummy_edge_conn_new(c4
, CONN_TYPE_EXIT
, 1000, 1000);
323 smartlist_add(edgeconns
, ec
);
325 tt_int_op(buf_get_total_allocation(), OP_EQ
, 4096*17*2);
326 tt_int_op(circuit_max_queued_item_age(c4
, tvms
), OP_EQ
, 1000);
328 tt_int_op(cell_queues_check_size(), OP_EQ
, 0);
330 /* And run over the limit. */
331 now_ns
+= 800*1000000;
332 monotime_coarse_set_mock_time_nsec(now_ns
);
333 c5
= dummy_or_circuit_new(0,5);
335 tt_int_op(cell_queues_get_total_allocation(), OP_EQ
,
336 packed_cell_mem_cost() * 85);
337 tt_int_op(buf_get_total_allocation(), OP_EQ
, 4096*17*2);
339 tt_int_op(cell_queues_check_size(), OP_EQ
, 1); /* We are now OOM */
341 /* C4 should have died. */
342 tt_assert(! c1
->marked_for_close
);
343 tt_assert(! c2
->marked_for_close
);
344 tt_assert(! c3
->marked_for_close
);
345 tt_assert(c4
->marked_for_close
);
346 tt_assert(! c5
->marked_for_close
);
348 tt_int_op(cell_queues_get_total_allocation(), OP_EQ
,
349 packed_cell_mem_cost() * 85);
350 tt_int_op(buf_get_total_allocation(), OP_EQ
, 4096*8*2);
359 SMARTLIST_FOREACH(edgeconns
, edge_connection_t
*, ec
,
360 connection_free_(TO_CONN(ec
)));
361 smartlist_free(edgeconns
);
363 UNMOCK(circuit_mark_for_close_
);
364 monotime_disable_test_mocking();
367 struct testcase_t oom_tests
[] = {
368 { "circbuf", test_oom_circbuf
, TT_FORK
, NULL
, NULL
},
369 { "streambuf", test_oom_streambuf
, TT_FORK
, NULL
, NULL
},