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