t_tls_extern.c revision 1.5 1 /* $NetBSD: t_tls_extern.c,v 1.5 2023/06/01 22:24:52 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 enum order {
37 DEF_USE,
38 USE_DEF,
39 USE_DEF_NOLOAD,
40 };
41
42 static void
43 tls_extern(const char *libdef, const char *libuse, enum order order,
44 bool xfail)
45 {
46 void *def, *use;
47 int *(*fdef)(void), *(*fuse)(void);
48 int *pdef, *puse;
49
50 (void)dlerror();
51
52 switch (order) {
53 case DEF_USE:
54 ATF_REQUIRE_DL(def = dlopen(libdef, 0));
55 ATF_REQUIRE_DL(use = dlopen(libuse, 0));
56 break;
57 case USE_DEF:
58 ATF_REQUIRE_DL(use = dlopen(libuse, 0));
59 ATF_REQUIRE_DL(def = dlopen(libdef, 0));
60 break;
61 case USE_DEF_NOLOAD:
62 ATF_REQUIRE_DL(use = dlopen(libuse, 0));
63 ATF_REQUIRE_DL(def = dlopen(libdef, RTLD_NOLOAD));
64 break;
65 }
66
67 ATF_REQUIRE_DL(fdef = dlsym(def, "fdef"));
68 ATF_REQUIRE_DL(fuse = dlsym(use, "fuse"));
69
70 pdef = (*fdef)();
71 puse = (*fuse)();
72 if (xfail) {
73 atf_tc_expect_fail("PR toolchain/50277:"
74 " rtld relocation bug with thread local storage");
75 }
76 ATF_CHECK_EQ_MSG(pdef, puse,
77 "%p in defining library != %p in using library",
78 pdef, puse);
79 }
80
81 ATF_TC(tls_extern_dynamic_defuse);
82 ATF_TC_HEAD(tls_extern_dynamic_defuse, tc)
83 {
84 atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
85 " loading def then use");
86 }
87 ATF_TC_BODY(tls_extern_dynamic_defuse, tc)
88 {
89 tls_extern("libh_def_dynamic.so", "libh_use_dynamic.so",
90 DEF_USE, /*xfail*/false);
91 }
92
93 ATF_TC(tls_extern_dynamic_usedef);
94 ATF_TC_HEAD(tls_extern_dynamic_usedef, tc)
95 {
96 atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
97 " loading use then def");
98 }
99 ATF_TC_BODY(tls_extern_dynamic_usedef, tc)
100 {
101 tls_extern("libh_def_dynamic.so", "libh_use_dynamic.so",
102 USE_DEF, /*xfail*/false);
103 }
104
105 ATF_TC(tls_extern_dynamic_usedefnoload);
106 ATF_TC_HEAD(tls_extern_dynamic_usedefnoload, tc)
107 {
108 atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
109 " loading use then def with RTLD_NOLOAD");
110 }
111 ATF_TC_BODY(tls_extern_dynamic_usedefnoload, tc)
112 {
113 tls_extern("libh_def_dynamic.so", "libh_use_dynamic.so",
114 USE_DEF_NOLOAD, /*xfail*/false);
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 tls_extern("libh_def_static.so", "libh_use_static.so",
126 DEF_USE, /*xfail*/false);
127 }
128
129 ATF_TC(tls_extern_static_usedef);
130 ATF_TC_HEAD(tls_extern_static_usedef, tc)
131 {
132 atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
133 " loading use then def");
134 }
135 ATF_TC_BODY(tls_extern_static_usedef, tc)
136 {
137 tls_extern("libh_def_static.so", "libh_use_static.so",
138 USE_DEF, /*xfail*/true);
139 }
140
141 ATF_TC(tls_extern_static_usedefnoload);
142 ATF_TC_HEAD(tls_extern_static_usedefnoload, tc)
143 {
144 atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
145 " loading use then def with RTLD_NOLOAD");
146 }
147 ATF_TC_BODY(tls_extern_static_usedefnoload, tc)
148 {
149 tls_extern("libh_def_static.so", "libh_use_static.so",
150 USE_DEF_NOLOAD, /*xfail*/true);
151 }
152
153 ATF_TP_ADD_TCS(tp)
154 {
155
156 ATF_TP_ADD_TC(tp, tls_extern_dynamic_defuse);
157 ATF_TP_ADD_TC(tp, tls_extern_dynamic_usedef);
158 ATF_TP_ADD_TC(tp, tls_extern_dynamic_usedefnoload);
159 ATF_TP_ADD_TC(tp, tls_extern_static_defuse);
160 ATF_TP_ADD_TC(tp, tls_extern_static_usedef);
161 ATF_TP_ADD_TC(tp, tls_extern_static_usedefnoload);
162 return atf_no_error();
163 }
164