Thu Jun 1 22:24:52 2023 UTC ()
ld.elf_so: Factor out logic in TLS tests to make writing more easier.

No functional change intended.

XXX pullup-10


(riastradh)
diff -r1.4 -r1.5 src/tests/libexec/ld.elf_so/t_tls_extern.c

cvs diff -r1.4 -r1.5 src/tests/libexec/ld.elf_so/t_tls_extern.c (expand / switch to unified diff)

--- src/tests/libexec/ld.elf_so/t_tls_extern.c 2023/06/01 20:50:18 1.4
+++ src/tests/libexec/ld.elf_so/t_tls_extern.c 2023/06/01 22:24:52 1.5
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: t_tls_extern.c,v 1.4 2023/06/01 20:50:18 riastradh Exp $ */ 1/* $NetBSD: t_tls_extern.c,v 1.5 2023/06/01 22:24:52 riastradh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2023 The NetBSD Foundation, Inc. 4 * Copyright (c) 2023 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -23,190 +23,141 @@ @@ -23,190 +23,141 @@
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29#include <sys/types.h> 29#include <sys/types.h>
30 30
31#include <atf-c.h> 31#include <atf-c.h>
32#include <dlfcn.h> 32#include <dlfcn.h>
33 33
34#define ATF_REQUIRE_DL(x) ATF_REQUIRE_MSG(x, "%s: %s", #x, dlerror()) 34#define ATF_REQUIRE_DL(x) ATF_REQUIRE_MSG(x, "%s: %s", #x, dlerror())
35 35
36ATF_TC(tls_extern_dynamic_defuse); 36enum order {
37ATF_TC_HEAD(tls_extern_dynamic_defuse, tc) 37 DEF_USE,
38{ 38 USE_DEF,
39 atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works," 39 USE_DEF_NOLOAD,
40 " loading def then use"); 40};
41} 41
42ATF_TC_BODY(tls_extern_dynamic_defuse, tc) 42static void
 43tls_extern(const char *libdef, const char *libuse, enum order order,
 44 bool xfail)
43{ 45{
44 void *def, *use; 46 void *def, *use;
45 int *(*fdef)(void), *(*fuse)(void); 47 int *(*fdef)(void), *(*fuse)(void);
46 int *pdef, *puse; 48 int *pdef, *puse;
47 49
48 (void)dlerror(); 50 (void)dlerror();
49 51
50 ATF_REQUIRE_DL(def = dlopen("libh_def_dynamic.so", 0)); 52 switch (order) {
51 ATF_REQUIRE_DL(use = dlopen("libh_use_dynamic.so", 0)); 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 }
52 66
53 ATF_REQUIRE_DL(fdef = dlsym(def, "fdef")); 67 ATF_REQUIRE_DL(fdef = dlsym(def, "fdef"));
54 ATF_REQUIRE_DL(fuse = dlsym(use, "fuse")); 68 ATF_REQUIRE_DL(fuse = dlsym(use, "fuse"));
55 69
56 pdef = (*fdef)(); 70 pdef = (*fdef)();
57 puse = (*fuse)(); 71 puse = (*fuse)();
 72 if (xfail) {
 73 atf_tc_expect_fail("PR toolchain/50277:"
 74 " rtld relocation bug with thread local storage");
 75 }
58 ATF_CHECK_EQ_MSG(pdef, puse, 76 ATF_CHECK_EQ_MSG(pdef, puse,
59 "%p in defining library != %p in using library", 77 "%p in defining library != %p in using library",
60 pdef, puse); 78 pdef, puse);
61} 79}
62 80
 81ATF_TC(tls_extern_dynamic_defuse);
 82ATF_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}
 87ATF_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
63ATF_TC(tls_extern_dynamic_usedef); 93ATF_TC(tls_extern_dynamic_usedef);
64ATF_TC_HEAD(tls_extern_dynamic_usedef, tc) 94ATF_TC_HEAD(tls_extern_dynamic_usedef, tc)
65{ 95{
66 atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works," 96 atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
67 " loading use then def"); 97 " loading use then def");
68} 98}
69ATF_TC_BODY(tls_extern_dynamic_usedef, tc) 99ATF_TC_BODY(tls_extern_dynamic_usedef, tc)
70{ 100{
71 void *def, *use; 101 tls_extern("libh_def_dynamic.so", "libh_use_dynamic.so",
72 int *(*fdef)(void), *(*fuse)(void); 102 USE_DEF, /*xfail*/false);
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} 103}
89 104
90ATF_TC(tls_extern_dynamic_usedefnoload); 105ATF_TC(tls_extern_dynamic_usedefnoload);
91ATF_TC_HEAD(tls_extern_dynamic_usedefnoload, tc) 106ATF_TC_HEAD(tls_extern_dynamic_usedefnoload, tc)
92{ 107{
93 atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works," 108 atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
94 " loading use then def with RTLD_NOLOAD"); 109 " loading use then def with RTLD_NOLOAD");
95} 110}
96ATF_TC_BODY(tls_extern_dynamic_usedefnoload, tc) 111ATF_TC_BODY(tls_extern_dynamic_usedefnoload, tc)
97{ 112{
98 void *def, *use; 113 tls_extern("libh_def_dynamic.so", "libh_use_dynamic.so",
99 int *(*fdef)(void), *(*fuse)(void); 114 USE_DEF_NOLOAD, /*xfail*/false);
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} 115}
116 116
117ATF_TC(tls_extern_static_defuse); 117ATF_TC(tls_extern_static_defuse);
118ATF_TC_HEAD(tls_extern_static_defuse, tc) 118ATF_TC_HEAD(tls_extern_static_defuse, tc)
119{ 119{
120 atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works," 120 atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
121 " loading def then use"); 121 " loading def then use");
122} 122}
123ATF_TC_BODY(tls_extern_static_defuse, tc) 123ATF_TC_BODY(tls_extern_static_defuse, tc)
124{ 124{
125 void *def, *use; 125 tls_extern("libh_def_static.so", "libh_use_static.so",
126 int *(*fdef)(void), *(*fuse)(void); 126 DEF_USE, /*xfail*/false);
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} 127}
143 128
144ATF_TC(tls_extern_static_usedef); 129ATF_TC(tls_extern_static_usedef);
145ATF_TC_HEAD(tls_extern_static_usedef, tc) 130ATF_TC_HEAD(tls_extern_static_usedef, tc)
146{ 131{
147 atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works," 132 atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
148 " loading use then def"); 133 " loading use then def");
149} 134}
150ATF_TC_BODY(tls_extern_static_usedef, tc) 135ATF_TC_BODY(tls_extern_static_usedef, tc)
151{ 136{
152 void *def, *use; 137 tls_extern("libh_def_static.so", "libh_use_static.so",
153 int *(*fdef)(void), *(*fuse)(void); 138 USE_DEF, /*xfail*/true);
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} 139}
172 140
173ATF_TC(tls_extern_static_usedefnoload); 141ATF_TC(tls_extern_static_usedefnoload);
174ATF_TC_HEAD(tls_extern_static_usedefnoload, tc) 142ATF_TC_HEAD(tls_extern_static_usedefnoload, tc)
175{ 143{
176 atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works," 144 atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
177 " loading use then def with RTLD_NOLOAD"); 145 " loading use then def with RTLD_NOLOAD");
178} 146}
179ATF_TC_BODY(tls_extern_static_usedefnoload, tc) 147ATF_TC_BODY(tls_extern_static_usedefnoload, tc)
180{ 148{
181 void *def, *use; 149 tls_extern("libh_def_static.so", "libh_use_static.so",
182 int *(*fdef)(void), *(*fuse)(void); 150 USE_DEF_NOLOAD, /*xfail*/true);
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} 151}
201 152
202ATF_TP_ADD_TCS(tp) 153ATF_TP_ADD_TCS(tp)
203{ 154{
204 155
205 ATF_TP_ADD_TC(tp, tls_extern_dynamic_defuse); 156 ATF_TP_ADD_TC(tp, tls_extern_dynamic_defuse);
206 ATF_TP_ADD_TC(tp, tls_extern_dynamic_usedef); 157 ATF_TP_ADD_TC(tp, tls_extern_dynamic_usedef);
207 ATF_TP_ADD_TC(tp, tls_extern_dynamic_usedefnoload); 158 ATF_TP_ADD_TC(tp, tls_extern_dynamic_usedefnoload);
208 ATF_TP_ADD_TC(tp, tls_extern_static_defuse); 159 ATF_TP_ADD_TC(tp, tls_extern_static_defuse);
209 ATF_TP_ADD_TC(tp, tls_extern_static_usedef); 160 ATF_TP_ADD_TC(tp, tls_extern_static_usedef);
210 ATF_TP_ADD_TC(tp, tls_extern_static_usedefnoload); 161 ATF_TP_ADD_TC(tp, tls_extern_static_usedefnoload);
211 return atf_no_error(); 162 return atf_no_error();
212} 163}