head	1.2;
access;
symbols
	netbsd-11-0-RC5:1.1.2.2
	netbsd-11-0-RC4:1.1.2.2
	netbsd-11-0-RC3:1.1.2.2
	netbsd-11-0-RC2:1.1.2.2
	netbsd-11-0-RC1:1.1.2.2
	netbsd-11:1.1.0.2;
locks; strict;
comment	@ * @;


1.2
date	2026.06.25.03.39.05;	author riastradh;	state Exp;
branches;
next	1.1;
commitid	JuhB3I3v1kpC78LG;

1.1
date	2025.11.23.22.01.13;	author riastradh;	state Exp;
branches
	1.1.2.1;
next	;
commitid	92vIlBZ1FZbb6JjG;

1.1.2.1
date	2025.11.23.22.01.13;	author martin;	state dead;
branches;
next	1.1.2.2;
commitid	RF4cReoYLvfQYUmG;

1.1.2.2
date	2025.12.18.18.03.33;	author martin;	state Exp;
branches;
next	;
commitid	RF4cReoYLvfQYUmG;


desc
@@


1.2
log
@ld.elf_so(1): Run concurrent dlopen/dlclose test a few more seconds.

More likely to provoke the problem this way.  Still not 100% reliable
because the problem is a race condition, but better than having the
test unexpectedly pass half the time.

Also set a timeout of 20sec, since I've seen the test get into an
infinite loop sometimes and it's now supposed to complete in 5sec +
epsilon.

PR lib/59751: dlclose is not MT-safe depending on the libraries unloaded
@
text
@/*	$NetBSD: t_dlclose_thread.c,v 1.1 2025/11/23 22:01:13 riastradh Exp $	*/

/*-
 * Copyright (c) 2025 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD: t_dlclose_thread.c,v 1.1 2025/11/23 22:01:13 riastradh Exp $");

#include <atf-c.h>
#include <dlfcn.h>
#include <pthread.h>
#include <stdatomic.h>
#include <unistd.h>

#include "h_macros.h"

pthread_barrier_t bar;
atomic_bool stop = false;

int sleep_init;
int sleep_fini;
int dlopen_cookie;
int dlclose_cookie;

static const char *const libh_helper_dso[] = {
	"libh_helper_dso1.so",
	"libh_helper_dso2.so",
	"libh_helper_dso3.so",
};

static void *
dlclose_thread(void *cookie)
{
	const unsigned i = (uintptr_t)cookie % __arraycount(libh_helper_dso);
	void *handle;

	(void)pthread_barrier_wait(&bar);
	while (!atomic_load_explicit(&stop, memory_order_relaxed)) {
		handle = dlopen(libh_helper_dso[i], RTLD_LAZY | RTLD_LOCAL);
		ATF_REQUIRE(handle != NULL);
		dlclose(handle);
	}
	return NULL;
}

ATF_TC(dlclose_thread);
ATF_TC_HEAD(dlclose_thread, tc)
{
	atf_tc_set_md_var(tc, "descr",
	    "Test concurrent dlopen and dlclose with destructors");
	atf_tc_set_md_var(tc, "timeout", "20");
}
ATF_TC_BODY(dlclose_thread, tc)
{
	pthread_t t[2*__arraycount(libh_helper_dso)];
	unsigned i;

	RZ(pthread_barrier_init(&bar, NULL, 1 + __arraycount(t)));
	for (i = 0; i < __arraycount(t); i++) {
		RZ(pthread_create(&t[i], NULL, &dlclose_thread,
			(void *)(uintptr_t)i));
	}
	atf_tc_expect_signal(-1, "PR lib/59751:"
	    " dlclose is not MT-safe depending on the libraries unloaded");
	(void)pthread_barrier_wait(&bar);
	(void)sleep(5);
	atomic_store_explicit(&stop, true, memory_order_relaxed);
	for (i = 0; i < __arraycount(t); i++)
		RZ(pthread_join(t[i], NULL));
}

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, dlclose_thread);
	return atf_no_error();
}
@


1.1
log
@ld.elf_so(1): Test concurrent dlopen/dlclose.

PR lib/59751: dlclose is not MT-safe depending on the libraries
unloaded
@
text
@d1 1
a1 1
/*	$NetBSD$	*/
d30 1
a30 1
__RCSID("$NetBSD$");
d74 1
d89 1
a89 1
	(void)sleep(1);
@


1.1.2.1
log
@file t_dlclose_thread.c was added on branch netbsd-11 on 2025-12-18 18:03:33 +0000
@
text
@d1 98
@


1.1.2.2
log
@Pull up following revision(s) (requested by riastradh in ticket #127):

	distrib/sets/lists/tests/mi: revision 1.1401
	tests/libexec/ld.elf_so/t_dlclose_thread.c: revision 1.1
	distrib/sets/lists/debug/mi: sort + revision 1.491
	tests/libexec/ld.elf_so/Makefile: revision 1.29

ld.elf_so(1): Test concurrent dlopen/dlclose.

PR lib/59751: dlclose is not MT-safe depending on the libraries
unloaded
@
text
@a0 98
/*	$NetBSD: t_dlclose_thread.c,v 1.1 2025/11/23 22:01:13 riastradh Exp $	*/

/*-
 * Copyright (c) 2025 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD: t_dlclose_thread.c,v 1.1 2025/11/23 22:01:13 riastradh Exp $");

#include <atf-c.h>
#include <dlfcn.h>
#include <pthread.h>
#include <stdatomic.h>
#include <unistd.h>

#include "h_macros.h"

pthread_barrier_t bar;
atomic_bool stop = false;

int sleep_init;
int sleep_fini;
int dlopen_cookie;
int dlclose_cookie;

static const char *const libh_helper_dso[] = {
	"libh_helper_dso1.so",
	"libh_helper_dso2.so",
	"libh_helper_dso3.so",
};

static void *
dlclose_thread(void *cookie)
{
	const unsigned i = (uintptr_t)cookie % __arraycount(libh_helper_dso);
	void *handle;

	(void)pthread_barrier_wait(&bar);
	while (!atomic_load_explicit(&stop, memory_order_relaxed)) {
		handle = dlopen(libh_helper_dso[i], RTLD_LAZY | RTLD_LOCAL);
		ATF_REQUIRE(handle != NULL);
		dlclose(handle);
	}
	return NULL;
}

ATF_TC(dlclose_thread);
ATF_TC_HEAD(dlclose_thread, tc)
{
	atf_tc_set_md_var(tc, "descr",
	    "Test concurrent dlopen and dlclose with destructors");
}
ATF_TC_BODY(dlclose_thread, tc)
{
	pthread_t t[2*__arraycount(libh_helper_dso)];
	unsigned i;

	RZ(pthread_barrier_init(&bar, NULL, 1 + __arraycount(t)));
	for (i = 0; i < __arraycount(t); i++) {
		RZ(pthread_create(&t[i], NULL, &dlclose_thread,
			(void *)(uintptr_t)i));
	}
	atf_tc_expect_signal(-1, "PR lib/59751:"
	    " dlclose is not MT-safe depending on the libraries unloaded");
	(void)pthread_barrier_wait(&bar);
	(void)sleep(1);
	atomic_store_explicit(&stop, true, memory_order_relaxed);
	for (i = 0; i < __arraycount(t); i++)
		RZ(pthread_join(t[i], NULL));
}

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, dlclose_thread);
	return atf_no_error();
}
@


