1*657871a7Schristos /* $NetBSD: regress_finalize.c,v 1.1.1.2 2021/04/07 02:43:15 christos Exp $ */
2805a1ce9Schristos /*
3805a1ce9Schristos * Copyright (c) 2013 Niels Provos and Nick Mathewson
4805a1ce9Schristos *
5805a1ce9Schristos * Redistribution and use in source and binary forms, with or without
6805a1ce9Schristos * modification, are permitted provided that the following conditions
7805a1ce9Schristos * are met:
8805a1ce9Schristos * 1. Redistributions of source code must retain the above copyright
9805a1ce9Schristos * notice, this list of conditions and the following disclaimer.
10805a1ce9Schristos * 2. Redistributions in binary form must reproduce the above copyright
11805a1ce9Schristos * notice, this list of conditions and the following disclaimer in the
12805a1ce9Schristos * documentation and/or other materials provided with the distribution.
13805a1ce9Schristos * 3. The name of the author may not be used to endorse or promote products
14805a1ce9Schristos * derived from this software without specific prior written permission.
15805a1ce9Schristos *
16805a1ce9Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17805a1ce9Schristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18805a1ce9Schristos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19805a1ce9Schristos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20805a1ce9Schristos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21805a1ce9Schristos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22805a1ce9Schristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23805a1ce9Schristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24805a1ce9Schristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25805a1ce9Schristos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26805a1ce9Schristos */
27805a1ce9Schristos
28805a1ce9Schristos #include "event2/event-config.h"
29805a1ce9Schristos #include <sys/cdefs.h>
30*657871a7Schristos __RCSID("$NetBSD: regress_finalize.c,v 1.1.1.2 2021/04/07 02:43:15 christos Exp $");
31805a1ce9Schristos #include "evconfig-private.h"
32805a1ce9Schristos #include "tinytest.h"
33805a1ce9Schristos #include "tinytest_macros.h"
34805a1ce9Schristos #include <stdlib.h>
35805a1ce9Schristos
36805a1ce9Schristos #include "event2/event.h"
37805a1ce9Schristos #include "event2/util.h"
38805a1ce9Schristos #include "event-internal.h"
39805a1ce9Schristos #include "defer-internal.h"
40805a1ce9Schristos
41805a1ce9Schristos #include "regress.h"
42805a1ce9Schristos #include "regress_thread.h"
43805a1ce9Schristos
44805a1ce9Schristos static void
timer_callback(evutil_socket_t fd,short what,void * arg)45805a1ce9Schristos timer_callback(evutil_socket_t fd, short what, void *arg)
46805a1ce9Schristos {
47805a1ce9Schristos int *int_arg = arg;
48805a1ce9Schristos *int_arg += 1;
49805a1ce9Schristos (void)fd;
50805a1ce9Schristos (void)what;
51805a1ce9Schristos }
52805a1ce9Schristos static void
simple_callback(struct event_callback * evcb,void * arg)53805a1ce9Schristos simple_callback(struct event_callback *evcb, void *arg)
54805a1ce9Schristos {
55805a1ce9Schristos int *int_arg = arg;
56805a1ce9Schristos *int_arg += 1;
57805a1ce9Schristos (void)evcb;
58805a1ce9Schristos }
59805a1ce9Schristos static void
event_finalize_callback_1(struct event * ev,void * arg)60805a1ce9Schristos event_finalize_callback_1(struct event *ev, void *arg)
61805a1ce9Schristos {
62805a1ce9Schristos int *int_arg = arg;
63805a1ce9Schristos *int_arg += 100;
64805a1ce9Schristos (void)ev;
65805a1ce9Schristos }
66805a1ce9Schristos static void
callback_finalize_callback_1(struct event_callback * evcb,void * arg)67805a1ce9Schristos callback_finalize_callback_1(struct event_callback *evcb, void *arg)
68805a1ce9Schristos {
69805a1ce9Schristos int *int_arg = arg;
70805a1ce9Schristos *int_arg += 100;
71805a1ce9Schristos (void)evcb;
72805a1ce9Schristos }
73805a1ce9Schristos
74805a1ce9Schristos
75805a1ce9Schristos static void
test_fin_cb_invoked(void * arg)76805a1ce9Schristos test_fin_cb_invoked(void *arg)
77805a1ce9Schristos {
78805a1ce9Schristos struct basic_test_data *data = arg;
79805a1ce9Schristos struct event_base *base = data->base;
80805a1ce9Schristos
81805a1ce9Schristos struct event *ev;
82805a1ce9Schristos struct event ev2;
83805a1ce9Schristos struct event_callback evcb;
84805a1ce9Schristos int cb_called = 0;
85805a1ce9Schristos int ev_called = 0;
86805a1ce9Schristos
87805a1ce9Schristos const struct timeval ten_sec = {10,0};
88805a1ce9Schristos
89805a1ce9Schristos event_deferred_cb_init_(&evcb, 0, simple_callback, &cb_called);
90805a1ce9Schristos ev = evtimer_new(base, timer_callback, &ev_called);
91805a1ce9Schristos /* Just finalize them; don't bother adding. */
92805a1ce9Schristos event_free_finalize(0, ev, event_finalize_callback_1);
93805a1ce9Schristos event_callback_finalize_(base, 0, &evcb, callback_finalize_callback_1);
94805a1ce9Schristos
95805a1ce9Schristos event_base_dispatch(base);
96805a1ce9Schristos
97805a1ce9Schristos tt_int_op(cb_called, ==, 100);
98805a1ce9Schristos tt_int_op(ev_called, ==, 100);
99805a1ce9Schristos
100805a1ce9Schristos ev_called = cb_called = 0;
101805a1ce9Schristos event_base_assert_ok_(base);
102805a1ce9Schristos
103805a1ce9Schristos /* Now try it when they're active. (actually, don't finalize: make
104805a1ce9Schristos * sure activation can happen! */
105805a1ce9Schristos ev = evtimer_new(base, timer_callback, &ev_called);
106805a1ce9Schristos event_deferred_cb_init_(&evcb, 0, simple_callback, &cb_called);
107805a1ce9Schristos
108805a1ce9Schristos event_active(ev, EV_TIMEOUT, 1);
109805a1ce9Schristos event_callback_activate_(base, &evcb);
110805a1ce9Schristos
111805a1ce9Schristos event_base_dispatch(base);
112805a1ce9Schristos tt_int_op(cb_called, ==, 1);
113805a1ce9Schristos tt_int_op(ev_called, ==, 1);
114805a1ce9Schristos
115805a1ce9Schristos ev_called = cb_called = 0;
116805a1ce9Schristos event_base_assert_ok_(base);
117805a1ce9Schristos
118805a1ce9Schristos /* Great, it worked. Now activate and finalize and make sure only
119805a1ce9Schristos * finalizing happens. */
120805a1ce9Schristos event_active(ev, EV_TIMEOUT, 1);
121805a1ce9Schristos event_callback_activate_(base, &evcb);
122805a1ce9Schristos event_free_finalize(0, ev, event_finalize_callback_1);
123805a1ce9Schristos event_callback_finalize_(base, 0, &evcb, callback_finalize_callback_1);
124805a1ce9Schristos
125805a1ce9Schristos event_base_dispatch(base);
126805a1ce9Schristos tt_int_op(cb_called, ==, 100);
127805a1ce9Schristos tt_int_op(ev_called, ==, 100);
128805a1ce9Schristos
129805a1ce9Schristos ev_called = 0;
130805a1ce9Schristos
131805a1ce9Schristos event_base_assert_ok_(base);
132805a1ce9Schristos
133805a1ce9Schristos /* Okay, now add but don't have it become active, and make sure *that*
134805a1ce9Schristos * works. */
135805a1ce9Schristos ev = evtimer_new(base, timer_callback, &ev_called);
136805a1ce9Schristos event_add(ev, &ten_sec);
137805a1ce9Schristos event_free_finalize(0, ev, event_finalize_callback_1);
138805a1ce9Schristos
139805a1ce9Schristos event_base_dispatch(base);
140805a1ce9Schristos tt_int_op(ev_called, ==, 100);
141805a1ce9Schristos
142805a1ce9Schristos ev_called = 0;
143805a1ce9Schristos event_base_assert_ok_(base);
144805a1ce9Schristos
145805a1ce9Schristos /* Now try adding and deleting after finalizing. */
146805a1ce9Schristos ev = evtimer_new(base, timer_callback, &ev_called);
147805a1ce9Schristos evtimer_assign(&ev2, base, timer_callback, &ev_called);
148805a1ce9Schristos event_add(ev, &ten_sec);
149805a1ce9Schristos event_free_finalize(0, ev, event_finalize_callback_1);
150805a1ce9Schristos event_finalize(0, &ev2, event_finalize_callback_1);
151805a1ce9Schristos
152805a1ce9Schristos event_add(&ev2, &ten_sec);
153805a1ce9Schristos event_del(ev);
154805a1ce9Schristos event_active(&ev2, EV_TIMEOUT, 1);
155805a1ce9Schristos
156805a1ce9Schristos event_base_dispatch(base);
157805a1ce9Schristos tt_int_op(ev_called, ==, 200);
158805a1ce9Schristos
159805a1ce9Schristos event_base_assert_ok_(base);
160805a1ce9Schristos
161805a1ce9Schristos end:
162805a1ce9Schristos ;
163805a1ce9Schristos }
164805a1ce9Schristos
165805a1ce9Schristos #ifndef EVENT__DISABLE_MM_REPLACEMENT
166805a1ce9Schristos static void *
tfff_malloc(size_t n)167805a1ce9Schristos tfff_malloc(size_t n)
168805a1ce9Schristos {
169805a1ce9Schristos return malloc(n);
170805a1ce9Schristos }
171805a1ce9Schristos static void *tfff_p1=NULL, *tfff_p2=NULL;
172805a1ce9Schristos static int tfff_p1_freed=0, tfff_p2_freed=0;
173805a1ce9Schristos static void
tfff_free(void * p)174805a1ce9Schristos tfff_free(void *p)
175805a1ce9Schristos {
176805a1ce9Schristos if (! p)
177805a1ce9Schristos return;
178805a1ce9Schristos if (p == tfff_p1)
179805a1ce9Schristos ++tfff_p1_freed;
180805a1ce9Schristos if (p == tfff_p2)
181805a1ce9Schristos ++tfff_p2_freed;
182805a1ce9Schristos free(p);
183805a1ce9Schristos }
184805a1ce9Schristos static void *
tfff_realloc(void * p,size_t sz)185805a1ce9Schristos tfff_realloc(void *p, size_t sz)
186805a1ce9Schristos {
187805a1ce9Schristos return realloc(p,sz);
188805a1ce9Schristos }
189805a1ce9Schristos #endif
190805a1ce9Schristos
191805a1ce9Schristos static void
test_fin_free_finalize(void * arg)192805a1ce9Schristos test_fin_free_finalize(void *arg)
193805a1ce9Schristos {
194805a1ce9Schristos #ifdef EVENT__DISABLE_MM_REPLACEMENT
195805a1ce9Schristos tinytest_set_test_skipped_();
196805a1ce9Schristos #else
197805a1ce9Schristos struct event_base *base = NULL;
198805a1ce9Schristos struct event *ev, *ev2;
199805a1ce9Schristos int ev_called = 0;
200805a1ce9Schristos int ev2_called = 0;
201805a1ce9Schristos
202805a1ce9Schristos (void)arg;
203805a1ce9Schristos
204805a1ce9Schristos event_set_mem_functions(tfff_malloc, tfff_realloc, tfff_free);
205805a1ce9Schristos
206805a1ce9Schristos base = event_base_new();
207805a1ce9Schristos tt_assert(base);
208805a1ce9Schristos
209805a1ce9Schristos ev = evtimer_new(base, timer_callback, &ev_called);
210805a1ce9Schristos ev2 = evtimer_new(base, timer_callback, &ev2_called);
211805a1ce9Schristos tfff_p1 = ev;
212805a1ce9Schristos tfff_p2 = ev2;
213805a1ce9Schristos event_free_finalize(0, ev, event_finalize_callback_1);
214805a1ce9Schristos event_finalize(0, ev2, event_finalize_callback_1);
215805a1ce9Schristos
216805a1ce9Schristos event_base_dispatch(base);
217805a1ce9Schristos
218805a1ce9Schristos tt_int_op(ev_called, ==, 100);
219805a1ce9Schristos tt_int_op(ev2_called, ==, 100);
220805a1ce9Schristos
221805a1ce9Schristos event_base_assert_ok_(base);
222805a1ce9Schristos tt_int_op(tfff_p1_freed, ==, 1);
223805a1ce9Schristos tt_int_op(tfff_p2_freed, ==, 0);
224805a1ce9Schristos
225805a1ce9Schristos event_free(ev2);
226805a1ce9Schristos
227805a1ce9Schristos end:
228805a1ce9Schristos if (base)
229805a1ce9Schristos event_base_free(base);
230805a1ce9Schristos #endif
231805a1ce9Schristos }
232805a1ce9Schristos
233805a1ce9Schristos /* For test_fin_within_cb */
234805a1ce9Schristos struct event_and_count {
235805a1ce9Schristos struct event *ev;
236805a1ce9Schristos struct event *ev2;
237805a1ce9Schristos int count;
238805a1ce9Schristos };
239805a1ce9Schristos static void
event_finalize_callback_2(struct event * ev,void * arg)240805a1ce9Schristos event_finalize_callback_2(struct event *ev, void *arg)
241805a1ce9Schristos {
242805a1ce9Schristos struct event_and_count *evc = arg;
243805a1ce9Schristos evc->count += 100;
244805a1ce9Schristos event_free(ev);
245805a1ce9Schristos }
246805a1ce9Schristos static void
timer_callback_2(evutil_socket_t fd,short what,void * arg)247805a1ce9Schristos timer_callback_2(evutil_socket_t fd, short what, void *arg)
248805a1ce9Schristos {
249805a1ce9Schristos struct event_and_count *evc = arg;
250805a1ce9Schristos event_finalize(0, evc->ev, event_finalize_callback_2);
251805a1ce9Schristos event_finalize(0, evc->ev2, event_finalize_callback_2);
252805a1ce9Schristos ++ evc->count;
253805a1ce9Schristos (void)fd;
254805a1ce9Schristos (void)what;
255805a1ce9Schristos }
256805a1ce9Schristos
257805a1ce9Schristos static void
test_fin_within_cb(void * arg)258805a1ce9Schristos test_fin_within_cb(void *arg)
259805a1ce9Schristos {
260805a1ce9Schristos struct basic_test_data *data = arg;
261805a1ce9Schristos struct event_base *base = data->base;
262805a1ce9Schristos
263805a1ce9Schristos struct event_and_count evc1, evc2;
264805a1ce9Schristos evc1.count = evc2.count = 0;
265805a1ce9Schristos evc2.ev2 = evc1.ev = evtimer_new(base, timer_callback_2, &evc1);
266805a1ce9Schristos evc1.ev2 = evc2.ev = evtimer_new(base, timer_callback_2, &evc2);
267805a1ce9Schristos
268805a1ce9Schristos /* Activate both. The first one will have its callback run, which
269805a1ce9Schristos * will finalize both of them, preventing the second one's callback
270805a1ce9Schristos * from running. */
271805a1ce9Schristos event_active(evc1.ev, EV_TIMEOUT, 1);
272805a1ce9Schristos event_active(evc2.ev, EV_TIMEOUT, 1);
273805a1ce9Schristos
274805a1ce9Schristos event_base_dispatch(base);
275805a1ce9Schristos tt_int_op(evc1.count, ==, 101);
276805a1ce9Schristos tt_int_op(evc2.count, ==, 100);
277805a1ce9Schristos
278805a1ce9Schristos event_base_assert_ok_(base);
279805a1ce9Schristos /* Now try with EV_PERSIST events. */
280805a1ce9Schristos evc1.count = evc2.count = 0;
281805a1ce9Schristos evc2.ev2 = evc1.ev = event_new(base, -1, EV_PERSIST, timer_callback_2, &evc1);
282805a1ce9Schristos evc1.ev2 = evc2.ev = event_new(base, -1, EV_PERSIST, timer_callback_2, &evc2);
283805a1ce9Schristos
284805a1ce9Schristos event_active(evc1.ev, EV_TIMEOUT, 1);
285805a1ce9Schristos event_active(evc2.ev, EV_TIMEOUT, 1);
286805a1ce9Schristos
287805a1ce9Schristos event_base_dispatch(base);
288805a1ce9Schristos tt_int_op(evc1.count, ==, 101);
289805a1ce9Schristos tt_int_op(evc2.count, ==, 100);
290805a1ce9Schristos
291805a1ce9Schristos event_base_assert_ok_(base);
292805a1ce9Schristos end:
293805a1ce9Schristos ;
294805a1ce9Schristos }
295805a1ce9Schristos
296*657871a7Schristos static void
event_finalize_callback_free(struct event * ev,void * arg)297*657871a7Schristos event_finalize_callback_free(struct event *ev, void *arg)
298*657871a7Schristos {
299*657871a7Schristos struct event_base *base = arg;
300*657871a7Schristos int err;
301*657871a7Schristos if (base) {
302*657871a7Schristos err = event_assign(ev, base, -1, EV_TIMEOUT, NULL, NULL);
303*657871a7Schristos tt_int_op(err, ==, 0);
304*657871a7Schristos test_ok += 1;
305*657871a7Schristos } else {
306*657871a7Schristos free(ev);
307*657871a7Schristos test_ok += 1;
308*657871a7Schristos }
309*657871a7Schristos
310*657871a7Schristos end:
311*657871a7Schristos ;
312*657871a7Schristos }
313*657871a7Schristos static void
test_fin_debug_use_after_free(void * arg)314*657871a7Schristos test_fin_debug_use_after_free(void *arg)
315*657871a7Schristos {
316*657871a7Schristos struct basic_test_data *data = arg;
317*657871a7Schristos struct event_base *base = data->base;
318*657871a7Schristos struct event *ev;
319*657871a7Schristos
320*657871a7Schristos tt_ptr_op(ev = event_new(base, -1, EV_TIMEOUT, NULL, base), !=, NULL);
321*657871a7Schristos tt_int_op(event_add(ev, NULL), ==, 0);
322*657871a7Schristos tt_int_op(event_finalize(0, ev, event_finalize_callback_free), ==, 0);
323*657871a7Schristos
324*657871a7Schristos // Dispatch base to trigger callbacks
325*657871a7Schristos event_base_dispatch(base);
326*657871a7Schristos event_base_assert_ok_(base);
327*657871a7Schristos tt_int_op(test_ok, ==, 1);
328*657871a7Schristos
329*657871a7Schristos // Now add again, since we did event_assign in event_finalize_callback_free
330*657871a7Schristos // This used to fail in event_debug_assert_is_setup_
331*657871a7Schristos tt_int_op(event_add(ev, NULL), ==, 0);
332*657871a7Schristos
333*657871a7Schristos // Finalize and dispatch again
334*657871a7Schristos tt_int_op(event_finalize(0, ev, event_finalize_callback_free), ==, 0);
335*657871a7Schristos event_base_dispatch(base);
336*657871a7Schristos event_base_assert_ok_(base);
337*657871a7Schristos tt_int_op(test_ok, ==, 2);
338*657871a7Schristos
339*657871a7Schristos end:
340*657871a7Schristos ;
341*657871a7Schristos }
342*657871a7Schristos
343805a1ce9Schristos #if 0
344805a1ce9Schristos static void
345805a1ce9Schristos timer_callback_3(evutil_socket_t *fd, short what, void *arg)
346805a1ce9Schristos {
347805a1ce9Schristos (void)fd;
348805a1ce9Schristos (void)what;
349805a1ce9Schristos
350805a1ce9Schristos }
351805a1ce9Schristos static void
352805a1ce9Schristos test_fin_many(void *arg)
353805a1ce9Schristos {
354805a1ce9Schristos struct basic_test_data *data = arg;
355805a1ce9Schristos struct event_base *base = data->base;
356805a1ce9Schristos
357805a1ce9Schristos struct event *ev1, *ev2;
358805a1ce9Schristos struct event_callback evcb1, evcb2;
359805a1ce9Schristos int ev1_count = 0, ev2_count = 0;
360805a1ce9Schristos int evcb1_count = 0, evcb2_count = 0;
361805a1ce9Schristos struct event_callback *array[4];
362805a1ce9Schristos
363805a1ce9Schristos int n;
364805a1ce9Schristos
365805a1ce9Schristos /* First attempt: call finalize_many with no events running */
366805a1ce9Schristos ev1 = evtimer_new(base, timer_callback, &ev1_count);
367805a1ce9Schristos ev1 = evtimer_new(base, timer_callback, &ev2_count);
368805a1ce9Schristos event_deferred_cb_init_(&evcb1, 0, simple_callback, &evcb1_called);
369805a1ce9Schristos event_deferred_cb_init_(&evcb2, 0, simple_callback, &evcb2_called);
370805a1ce9Schristos array[0] = &ev1->ev_evcallback;
371805a1ce9Schristos array[1] = &ev2->ev_evcallback;
372805a1ce9Schristos array[2] = &evcb1;
373805a1ce9Schristos array[3] = &evcb2;
374805a1ce9Schristos
375805a1ce9Schristos
376805a1ce9Schristos
377805a1ce9Schristos n = event_callback_finalize_many(base, 4, array,
378805a1ce9Schristos callback_finalize_callback_1);
379805a1ce9Schristos
380805a1ce9Schristos }
381805a1ce9Schristos #endif
382805a1ce9Schristos
383805a1ce9Schristos
384805a1ce9Schristos #define TEST(name, flags) \
385805a1ce9Schristos { #name, test_fin_##name, (flags), &basic_setup, NULL }
386805a1ce9Schristos
387805a1ce9Schristos struct testcase_t finalize_testcases[] = {
388805a1ce9Schristos
389805a1ce9Schristos TEST(cb_invoked, TT_FORK|TT_NEED_BASE),
390805a1ce9Schristos TEST(free_finalize, TT_FORK),
391805a1ce9Schristos TEST(within_cb, TT_FORK|TT_NEED_BASE),
392*657871a7Schristos TEST(debug_use_after_free, TT_FORK|TT_NEED_BASE|TT_ENABLE_DEBUG_MODE),
393805a1ce9Schristos // TEST(many, TT_FORK|TT_NEED_BASE),
394805a1ce9Schristos
395805a1ce9Schristos
396805a1ce9Schristos END_OF_TESTCASES
397805a1ce9Schristos };
398805a1ce9Schristos
399