Home | History | Annotate | Line # | Download | only in ld.elf_so
t_tls_extern.c revision 1.4
      1 /*	$NetBSD: t_tls_extern.c,v 1.4 2023/06/01 20:50:18 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2023 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/types.h>
     30 
     31 #include <atf-c.h>
     32 #include <dlfcn.h>
     33 
     34 #define	ATF_REQUIRE_DL(x)	ATF_REQUIRE_MSG(x, "%s: %s", #x, dlerror())
     35 
     36 ATF_TC(tls_extern_dynamic_defuse);
     37 ATF_TC_HEAD(tls_extern_dynamic_defuse, tc)
     38 {
     39 	atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
     40 	    " loading def then use");
     41 }
     42 ATF_TC_BODY(tls_extern_dynamic_defuse, tc)
     43 {
     44 	void *def, *use;
     45 	int *(*fdef)(void), *(*fuse)(void);
     46 	int *pdef, *puse;
     47 
     48 	(void)dlerror();
     49 
     50 	ATF_REQUIRE_DL(def = dlopen("libh_def_dynamic.so", 0));
     51 	ATF_REQUIRE_DL(use = dlopen("libh_use_dynamic.so", 0));
     52 
     53 	ATF_REQUIRE_DL(fdef = dlsym(def, "fdef"));
     54 	ATF_REQUIRE_DL(fuse = dlsym(use, "fuse"));
     55 
     56 	pdef = (*fdef)();
     57 	puse = (*fuse)();
     58 	ATF_CHECK_EQ_MSG(pdef, puse,
     59 	    "%p in defining library != %p in using library",
     60 	    pdef, puse);
     61 }
     62 
     63 ATF_TC(tls_extern_dynamic_usedef);
     64 ATF_TC_HEAD(tls_extern_dynamic_usedef, tc)
     65 {
     66 	atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
     67 	    " loading use then def");
     68 }
     69 ATF_TC_BODY(tls_extern_dynamic_usedef, tc)
     70 {
     71 	void *def, *use;
     72 	int *(*fdef)(void), *(*fuse)(void);
     73 	int *pdef, *puse;
     74 
     75 	(void)dlerror();
     76 
     77 	ATF_REQUIRE_DL(use = dlopen("libh_use_dynamic.so", 0));
     78 	ATF_REQUIRE_DL(def = dlopen("libh_def_dynamic.so", 0));
     79 
     80 	ATF_REQUIRE_DL(fdef = dlsym(def, "fdef"));
     81 	ATF_REQUIRE_DL(fuse = dlsym(use, "fuse"));
     82 
     83 	pdef = (*fdef)();
     84 	puse = (*fuse)();
     85 	ATF_CHECK_EQ_MSG(pdef, puse,
     86 	    "%p in defining library != %p in using library",
     87 	    pdef, puse);
     88 }
     89 
     90 ATF_TC(tls_extern_dynamic_usedefnoload);
     91 ATF_TC_HEAD(tls_extern_dynamic_usedefnoload, tc)
     92 {
     93 	atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
     94 	    " loading use then def with RTLD_NOLOAD");
     95 }
     96 ATF_TC_BODY(tls_extern_dynamic_usedefnoload, tc)
     97 {
     98 	void *def, *use;
     99 	int *(*fdef)(void), *(*fuse)(void);
    100 	int *pdef, *puse;
    101 
    102 	(void)dlerror();
    103 
    104 	ATF_REQUIRE_DL(use = dlopen("libh_use_dynamic.so", 0));
    105 	ATF_REQUIRE_DL(def = dlopen("libh_def_dynamic.so", RTLD_NOLOAD));
    106 
    107 	ATF_REQUIRE_DL(fdef = dlsym(def, "fdef"));
    108 	ATF_REQUIRE_DL(fuse = dlsym(use, "fuse"));
    109 
    110 	pdef = (*fdef)();
    111 	puse = (*fuse)();
    112 	ATF_CHECK_EQ_MSG(pdef, puse,
    113 	    "%p in defining library != %p in using library",
    114 	    pdef, puse);
    115 }
    116 
    117 ATF_TC(tls_extern_static_defuse);
    118 ATF_TC_HEAD(tls_extern_static_defuse, tc)
    119 {
    120 	atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
    121 	    " loading def then use");
    122 }
    123 ATF_TC_BODY(tls_extern_static_defuse, tc)
    124 {
    125 	void *def, *use;
    126 	int *(*fdef)(void), *(*fuse)(void);
    127 	int *pdef, *puse;
    128 
    129 	(void)dlerror();
    130 
    131 	ATF_REQUIRE_DL(def = dlopen("libh_def_static.so", 0));
    132 	ATF_REQUIRE_DL(use = dlopen("libh_use_static.so", 0));
    133 
    134 	ATF_REQUIRE_DL(fdef = dlsym(def, "fdef"));
    135 	ATF_REQUIRE_DL(fuse = dlsym(use, "fuse"));
    136 
    137 	pdef = (*fdef)();
    138 	puse = (*fuse)();
    139 	ATF_CHECK_EQ_MSG(pdef, puse,
    140 	    "%p in defining library != %p in using library",
    141 	    pdef, puse);
    142 }
    143 
    144 ATF_TC(tls_extern_static_usedef);
    145 ATF_TC_HEAD(tls_extern_static_usedef, tc)
    146 {
    147 	atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
    148 	    " loading use then def");
    149 }
    150 ATF_TC_BODY(tls_extern_static_usedef, tc)
    151 {
    152 	void *def, *use;
    153 	int *(*fdef)(void), *(*fuse)(void);
    154 	int *pdef, *puse;
    155 
    156 	(void)dlerror();
    157 
    158 	ATF_REQUIRE_DL(use = dlopen("libh_use_static.so", 0));
    159 	ATF_REQUIRE_DL(def = dlopen("libh_def_static.so", 0));
    160 
    161 	ATF_REQUIRE_DL(fdef = dlsym(def, "fdef"));
    162 	ATF_REQUIRE_DL(fuse = dlsym(use, "fuse"));
    163 
    164 	pdef = (*fdef)();
    165 	puse = (*fuse)();
    166 	atf_tc_expect_fail("PR toolchain/50277:"
    167 	    " rtld relocation bug with thread local storage");
    168 	ATF_CHECK_EQ_MSG(pdef, puse,
    169 	    "%p in defining library != %p in using library",
    170 	    pdef, puse);
    171 }
    172 
    173 ATF_TC(tls_extern_static_usedefnoload);
    174 ATF_TC_HEAD(tls_extern_static_usedefnoload, tc)
    175 {
    176 	atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
    177 	    " loading use then def with RTLD_NOLOAD");
    178 }
    179 ATF_TC_BODY(tls_extern_static_usedefnoload, tc)
    180 {
    181 	void *def, *use;
    182 	int *(*fdef)(void), *(*fuse)(void);
    183 	int *pdef, *puse;
    184 
    185 	(void)dlerror();
    186 
    187 	ATF_REQUIRE_DL(use = dlopen("libh_use_static.so", 0));
    188 	ATF_REQUIRE_DL(def = dlopen("libh_def_static.so", RTLD_NOLOAD));
    189 
    190 	ATF_REQUIRE_DL(fdef = dlsym(def, "fdef"));
    191 	ATF_REQUIRE_DL(fuse = dlsym(use, "fuse"));
    192 
    193 	pdef = (*fdef)();
    194 	puse = (*fuse)();
    195 	atf_tc_expect_fail("PR toolchain/50277:"
    196 	    " rtld relocation bug with thread local storage");
    197 	ATF_CHECK_EQ_MSG(pdef, puse,
    198 	    "%p in defining library != %p in using library",
    199 	    pdef, puse);
    200 }
    201 
    202 ATF_TP_ADD_TCS(tp)
    203 {
    204 
    205 	ATF_TP_ADD_TC(tp, tls_extern_dynamic_defuse);
    206 	ATF_TP_ADD_TC(tp, tls_extern_dynamic_usedef);
    207 	ATF_TP_ADD_TC(tp, tls_extern_dynamic_usedefnoload);
    208 	ATF_TP_ADD_TC(tp, tls_extern_static_defuse);
    209 	ATF_TP_ADD_TC(tp, tls_extern_static_usedef);
    210 	ATF_TP_ADD_TC(tp, tls_extern_static_usedefnoload);
    211 	return atf_no_error();
    212 }
    213