1/*	$NetBSD: t_dlclose_thread.c,v 1.1 2025/11/23 22:01:13 riastradh Exp $	*/
2
3/*-
4 * Copyright (c) 2025 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__RCSID("$NetBSD: t_dlclose_thread.c,v 1.1 2025/11/23 22:01:13 riastradh Exp $");
31
32#include <atf-c.h>
33#include <dlfcn.h>
34#include <pthread.h>
35#include <stdatomic.h>
36#include <unistd.h>
37
38#include "h_macros.h"
39
40pthread_barrier_t bar;
41atomic_bool stop = false;
42
43int sleep_init;
44int sleep_fini;
45int dlopen_cookie;
46int dlclose_cookie;
47
48static const char *const libh_helper_dso[] = {
49	"libh_helper_dso1.so",
50	"libh_helper_dso2.so",
51	"libh_helper_dso3.so",
52};
53
54static void *
55dlclose_thread(void *cookie)
56{
57	const unsigned i = (uintptr_t)cookie % __arraycount(libh_helper_dso);
58	void *handle;
59
60	(void)pthread_barrier_wait(&bar);
61	while (!atomic_load_explicit(&stop, memory_order_relaxed)) {
62		handle = dlopen(libh_helper_dso[i], RTLD_LAZY | RTLD_LOCAL);
63		ATF_REQUIRE(handle != NULL);
64		dlclose(handle);
65	}
66	return NULL;
67}
68
69ATF_TC(dlclose_thread);
70ATF_TC_HEAD(dlclose_thread, tc)
71{
72	atf_tc_set_md_var(tc, "descr",
73	    "Test concurrent dlopen and dlclose with destructors");
74}
75ATF_TC_BODY(dlclose_thread, tc)
76{
77	pthread_t t[2*__arraycount(libh_helper_dso)];
78	unsigned i;
79
80	RZ(pthread_barrier_init(&bar, NULL, 1 + __arraycount(t)));
81	for (i = 0; i < __arraycount(t); i++) {
82		RZ(pthread_create(&t[i], NULL, &dlclose_thread,
83			(void *)(uintptr_t)i));
84	}
85	atf_tc_expect_signal(-1, "PR lib/59751:"
86	    " dlclose is not MT-safe depending on the libraries unloaded");
87	(void)pthread_barrier_wait(&bar);
88	(void)sleep(1);
89	atomic_store_explicit(&stop, true, memory_order_relaxed);
90	for (i = 0; i < __arraycount(t); i++)
91		RZ(pthread_join(t[i], NULL));
92}
93
94ATF_TP_ADD_TCS(tp)
95{
96	ATF_TP_ADD_TC(tp, dlclose_thread);
97	return atf_no_error();
98}
99