Home | History | Annotate | Line # | Download | only in ld.elf_so
t_tls_extern.c revision 1.7
      1  1.7  riastrad /*	$NetBSD: t_tls_extern.c,v 1.7 2023/06/01 22:26:51 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2023 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
      8  1.1  riastrad  * modification, are permitted provided that the following conditions
      9  1.1  riastrad  * are met:
     10  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     11  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     12  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  riastrad  */
     28  1.1  riastrad 
     29  1.1  riastrad #include <sys/types.h>
     30  1.1  riastrad 
     31  1.1  riastrad #include <atf-c.h>
     32  1.1  riastrad #include <dlfcn.h>
     33  1.1  riastrad 
     34  1.2  riastrad #define	ATF_REQUIRE_DL(x)	ATF_REQUIRE_MSG(x, "%s: %s", #x, dlerror())
     35  1.1  riastrad 
     36  1.5  riastrad enum order {
     37  1.5  riastrad 	DEF_USE,
     38  1.5  riastrad 	USE_DEF,
     39  1.5  riastrad 	USE_DEF_NOLOAD,
     40  1.5  riastrad };
     41  1.5  riastrad 
     42  1.5  riastrad static void
     43  1.5  riastrad tls_extern(const char *libdef, const char *libuse, enum order order,
     44  1.5  riastrad     bool xfail)
     45  1.4  riastrad {
     46  1.4  riastrad 	void *def, *use;
     47  1.4  riastrad 	int *(*fdef)(void), *(*fuse)(void);
     48  1.4  riastrad 	int *pdef, *puse;
     49  1.4  riastrad 
     50  1.4  riastrad 	(void)dlerror();
     51  1.4  riastrad 
     52  1.5  riastrad 	switch (order) {
     53  1.5  riastrad 	case DEF_USE:
     54  1.5  riastrad 		ATF_REQUIRE_DL(def = dlopen(libdef, 0));
     55  1.5  riastrad 		ATF_REQUIRE_DL(use = dlopen(libuse, 0));
     56  1.5  riastrad 		break;
     57  1.5  riastrad 	case USE_DEF:
     58  1.5  riastrad 		ATF_REQUIRE_DL(use = dlopen(libuse, 0));
     59  1.5  riastrad 		ATF_REQUIRE_DL(def = dlopen(libdef, 0));
     60  1.5  riastrad 		break;
     61  1.5  riastrad 	case USE_DEF_NOLOAD:
     62  1.5  riastrad 		ATF_REQUIRE_DL(use = dlopen(libuse, 0));
     63  1.5  riastrad 		ATF_REQUIRE_DL(def = dlopen(libdef, RTLD_NOLOAD));
     64  1.5  riastrad 		break;
     65  1.5  riastrad 	}
     66  1.4  riastrad 
     67  1.4  riastrad 	ATF_REQUIRE_DL(fdef = dlsym(def, "fdef"));
     68  1.4  riastrad 	ATF_REQUIRE_DL(fuse = dlsym(use, "fuse"));
     69  1.4  riastrad 
     70  1.4  riastrad 	pdef = (*fdef)();
     71  1.4  riastrad 	puse = (*fuse)();
     72  1.5  riastrad 	if (xfail) {
     73  1.5  riastrad 		atf_tc_expect_fail("PR toolchain/50277:"
     74  1.5  riastrad 		    " rtld relocation bug with thread local storage");
     75  1.5  riastrad 	}
     76  1.4  riastrad 	ATF_CHECK_EQ_MSG(pdef, puse,
     77  1.4  riastrad 	    "%p in defining library != %p in using library",
     78  1.4  riastrad 	    pdef, puse);
     79  1.4  riastrad }
     80  1.4  riastrad 
     81  1.7  riastrad ATF_TC(dynamic_abusedef);
     82  1.7  riastrad ATF_TC_HEAD(dynamic_abusedef, tc)
     83  1.6  riastrad {
     84  1.6  riastrad 	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
     85  1.6  riastrad 	    " loading static use than dynamic def");
     86  1.6  riastrad }
     87  1.7  riastrad ATF_TC_BODY(dynamic_abusedef, tc)
     88  1.6  riastrad {
     89  1.6  riastrad 	tls_extern("libh_def_dynamic.so", "libh_abuse_dynamic.so",
     90  1.6  riastrad 	    USE_DEF, /*xfail*/true);
     91  1.6  riastrad }
     92  1.6  riastrad 
     93  1.7  riastrad ATF_TC(dynamic_abusedefnoload);
     94  1.7  riastrad ATF_TC_HEAD(dynamic_abusedefnoload, tc)
     95  1.6  riastrad {
     96  1.6  riastrad 	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
     97  1.6  riastrad 	    " loading static use then dynamic def with RTLD_NOLOAD");
     98  1.6  riastrad }
     99  1.7  riastrad ATF_TC_BODY(dynamic_abusedefnoload, tc)
    100  1.6  riastrad {
    101  1.6  riastrad 	tls_extern("libh_def_dynamic.so", "libh_abuse_dynamic.so",
    102  1.6  riastrad 	    USE_DEF_NOLOAD, /*xfail*/true);
    103  1.6  riastrad }
    104  1.6  riastrad 
    105  1.7  riastrad ATF_TC(dynamic_defabuse);
    106  1.7  riastrad ATF_TC_HEAD(dynamic_defabuse, tc)
    107  1.6  riastrad {
    108  1.6  riastrad 	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
    109  1.6  riastrad 	    " loading dynamic def then static use");
    110  1.6  riastrad }
    111  1.7  riastrad ATF_TC_BODY(dynamic_defabuse, tc)
    112  1.6  riastrad {
    113  1.6  riastrad 	tls_extern("libh_def_dynamic.so", "libh_abuse_dynamic.so",
    114  1.6  riastrad 	    DEF_USE, /*xfail*/true);
    115  1.6  riastrad }
    116  1.6  riastrad 
    117  1.7  riastrad ATF_TC(dynamic_defuse);
    118  1.7  riastrad ATF_TC_HEAD(dynamic_defuse, tc)
    119  1.5  riastrad {
    120  1.5  riastrad 	atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
    121  1.5  riastrad 	    " loading def then use");
    122  1.5  riastrad }
    123  1.7  riastrad ATF_TC_BODY(dynamic_defuse, tc)
    124  1.5  riastrad {
    125  1.5  riastrad 	tls_extern("libh_def_dynamic.so", "libh_use_dynamic.so",
    126  1.5  riastrad 	    DEF_USE, /*xfail*/false);
    127  1.5  riastrad }
    128  1.5  riastrad 
    129  1.7  riastrad ATF_TC(dynamic_usedef);
    130  1.7  riastrad ATF_TC_HEAD(dynamic_usedef, tc)
    131  1.4  riastrad {
    132  1.4  riastrad 	atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
    133  1.4  riastrad 	    " loading use then def");
    134  1.4  riastrad }
    135  1.7  riastrad ATF_TC_BODY(dynamic_usedef, tc)
    136  1.4  riastrad {
    137  1.5  riastrad 	tls_extern("libh_def_dynamic.so", "libh_use_dynamic.so",
    138  1.5  riastrad 	    USE_DEF, /*xfail*/false);
    139  1.4  riastrad }
    140  1.4  riastrad 
    141  1.7  riastrad ATF_TC(dynamic_usedefnoload);
    142  1.7  riastrad ATF_TC_HEAD(dynamic_usedefnoload, tc)
    143  1.4  riastrad {
    144  1.4  riastrad 	atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
    145  1.4  riastrad 	    " loading use then def with RTLD_NOLOAD");
    146  1.4  riastrad }
    147  1.7  riastrad ATF_TC_BODY(dynamic_usedefnoload, tc)
    148  1.4  riastrad {
    149  1.5  riastrad 	tls_extern("libh_def_dynamic.so", "libh_use_dynamic.so",
    150  1.5  riastrad 	    USE_DEF_NOLOAD, /*xfail*/false);
    151  1.4  riastrad }
    152  1.4  riastrad 
    153  1.7  riastrad ATF_TC(static_abusedef);
    154  1.7  riastrad ATF_TC_HEAD(static_abusedef, tc)
    155  1.6  riastrad {
    156  1.6  riastrad 	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
    157  1.6  riastrad 	    " loading dynamic use then static def");
    158  1.6  riastrad }
    159  1.7  riastrad ATF_TC_BODY(static_abusedef, tc)
    160  1.6  riastrad {
    161  1.6  riastrad 	tls_extern("libh_def_static.so", "libh_abuse_static.so",
    162  1.6  riastrad 	    USE_DEF, /*xfail*/true);
    163  1.6  riastrad }
    164  1.6  riastrad 
    165  1.7  riastrad ATF_TC(static_abusedefnoload);
    166  1.7  riastrad ATF_TC_HEAD(static_abusedefnoload, tc)
    167  1.6  riastrad {
    168  1.6  riastrad 	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
    169  1.6  riastrad 	    " loading dynamic use then static def with RTLD_NOLOAD");
    170  1.6  riastrad }
    171  1.7  riastrad ATF_TC_BODY(static_abusedefnoload, tc)
    172  1.6  riastrad {
    173  1.6  riastrad 	tls_extern("libh_def_static.so", "libh_abuse_static.so",
    174  1.6  riastrad 	    USE_DEF_NOLOAD, /*xfail*/true);
    175  1.6  riastrad }
    176  1.6  riastrad 
    177  1.7  riastrad ATF_TC(static_defabuse);
    178  1.7  riastrad ATF_TC_HEAD(static_defabuse, tc)
    179  1.6  riastrad {
    180  1.6  riastrad 	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
    181  1.6  riastrad 	    " loading static def then dynamic use");
    182  1.6  riastrad }
    183  1.7  riastrad ATF_TC_BODY(static_defabuse, tc)
    184  1.6  riastrad {
    185  1.6  riastrad 	tls_extern("libh_def_static.so", "libh_abuse_static.so",
    186  1.6  riastrad 	    DEF_USE, /*xfail*/true);
    187  1.6  riastrad }
    188  1.6  riastrad 
    189  1.7  riastrad ATF_TC(static_defuse);
    190  1.7  riastrad ATF_TC_HEAD(static_defuse, tc)
    191  1.1  riastrad {
    192  1.3  riastrad 	atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
    193  1.3  riastrad 	    " loading def then use");
    194  1.1  riastrad }
    195  1.7  riastrad ATF_TC_BODY(static_defuse, tc)
    196  1.1  riastrad {
    197  1.5  riastrad 	tls_extern("libh_def_static.so", "libh_use_static.so",
    198  1.5  riastrad 	    DEF_USE, /*xfail*/false);
    199  1.3  riastrad }
    200  1.3  riastrad 
    201  1.7  riastrad ATF_TC(static_usedef);
    202  1.7  riastrad ATF_TC_HEAD(static_usedef, tc)
    203  1.3  riastrad {
    204  1.3  riastrad 	atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
    205  1.3  riastrad 	    " loading use then def");
    206  1.3  riastrad }
    207  1.7  riastrad ATF_TC_BODY(static_usedef, tc)
    208  1.3  riastrad {
    209  1.5  riastrad 	tls_extern("libh_def_static.so", "libh_use_static.so",
    210  1.5  riastrad 	    USE_DEF, /*xfail*/true);
    211  1.3  riastrad }
    212  1.3  riastrad 
    213  1.7  riastrad ATF_TC(static_usedefnoload);
    214  1.7  riastrad ATF_TC_HEAD(static_usedefnoload, tc)
    215  1.3  riastrad {
    216  1.3  riastrad 	atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
    217  1.3  riastrad 	    " loading use then def with RTLD_NOLOAD");
    218  1.3  riastrad }
    219  1.7  riastrad ATF_TC_BODY(static_usedefnoload, tc)
    220  1.3  riastrad {
    221  1.5  riastrad 	tls_extern("libh_def_static.so", "libh_use_static.so",
    222  1.5  riastrad 	    USE_DEF_NOLOAD, /*xfail*/true);
    223  1.1  riastrad }
    224  1.1  riastrad 
    225  1.1  riastrad ATF_TP_ADD_TCS(tp)
    226  1.1  riastrad {
    227  1.3  riastrad 
    228  1.7  riastrad 	ATF_TP_ADD_TC(tp, dynamic_abusedef);
    229  1.7  riastrad 	ATF_TP_ADD_TC(tp, dynamic_abusedefnoload);
    230  1.7  riastrad 	ATF_TP_ADD_TC(tp, dynamic_defabuse);
    231  1.7  riastrad 	ATF_TP_ADD_TC(tp, dynamic_defuse);
    232  1.7  riastrad 	ATF_TP_ADD_TC(tp, dynamic_usedef);
    233  1.7  riastrad 	ATF_TP_ADD_TC(tp, dynamic_usedefnoload);
    234  1.7  riastrad 	ATF_TP_ADD_TC(tp, static_abusedef);
    235  1.7  riastrad 	ATF_TP_ADD_TC(tp, static_abusedefnoload);
    236  1.7  riastrad 	ATF_TP_ADD_TC(tp, static_defabuse);
    237  1.7  riastrad 	ATF_TP_ADD_TC(tp, static_defuse);
    238  1.7  riastrad 	ATF_TP_ADD_TC(tp, static_usedef);
    239  1.7  riastrad 	ATF_TP_ADD_TC(tp, static_usedefnoload);
    240  1.1  riastrad 	return atf_no_error();
    241  1.1  riastrad }
    242