xref: /freebsd/lib/libc/tests/stdlib/dynthr_test.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1 /*
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 2019 Andrew Gierth
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * Though this file is initially distributed under the 2-clause BSD license,
28  * the author grants permission for its redistribution under alternative
29  * licenses as set forth at <https://rhodiumtoad.github.io/RELICENSE.txt>.
30  * This paragraph and the RELICENSE.txt file are not part of the license and
31  * may be omitted in redistributions.
32  */
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <dlfcn.h>
40 
41 #include <atf-c.h>
42 
43 typedef void (modfunc_t)(int op);
44 
45 /*
46  * Minimal test case for PR 235158; mutual dependencies between jemalloc and
47  * libthr causing issues in thread creation.  Specifically to this case, libthr
48  * uses calloc to initialize pthread mutexes, and jemalloc uses pthread mutexes.
49  *
50  * Deferred initialization provided by jemalloc proved to be fragile, causing
51  * issues like in the referenced PR where thread creation in a shared object
52  * loaded via dlopen(3) would stall unless the calling application also linked
53  * against pthread.
54  */
55 ATF_TC(maintc);
ATF_TC_HEAD(maintc,tc)56 ATF_TC_HEAD(maintc, tc)
57 {
58 
59 	atf_tc_set_md_var(tc, "timeout", "3");
60 }
61 
ATF_TC_BODY(maintc,tc)62 ATF_TC_BODY(maintc, tc)
63 {
64 	char *libpath;
65 	modfunc_t *func;
66 	void *mod_handle;
67 	const char *srcdir;
68 	dlfunc_t rawfunc;
69 
70 	srcdir = atf_tc_get_config_var(tc, "srcdir");
71 	if (asprintf(&libpath, "%s/dynthr_mod.so", srcdir) < 0)
72 		atf_tc_fail("failed to construct path to libthr");
73 	mod_handle = dlopen(libpath, RTLD_LOCAL);
74 	free(libpath);
75 	if (mod_handle == NULL)
76 		atf_tc_fail("failed to open dynthr_mod.so: %s", dlerror());
77 	rawfunc = dlfunc(mod_handle, "mod_main");
78 	if (rawfunc == NULL)
79 		atf_tc_fail("failed to resolve function mod_main");
80 	func = (modfunc_t *)rawfunc;
81 	func(1);
82 	func(0);
83 }
84 
ATF_TP_ADD_TCS(tp)85 ATF_TP_ADD_TCS(tp)
86 {
87 
88 	ATF_TP_ADD_TC(tp, maintc);
89 	return (atf_no_error());
90 }
91