blob: bcb2b40b328fd93ec43d86be8785b15ed934fb7e [file] [log] [blame]
Ryan Pricharde4ee12f2019-01-15 20:35:00 -08001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <dlfcn.h>
Ryan Pricharda2e83ab2019-08-16 17:25:43 -070030#include <link.h>
Ryan Pricharde4ee12f2019-01-15 20:35:00 -080031
Colin Cross7da20342021-07-28 11:18:11 -070032#include <gtest/gtest.h>
33
Ryan Prichard98731dc2024-02-29 22:56:36 -080034#include <string>
Ryan Pricharde4ee12f2019-01-15 20:35:00 -080035#include <thread>
36
37#include "gtest_globals.h"
Christopher Ferrisc5d3a432019-09-25 17:50:36 -070038#include "platform/bionic/tls.h"
Ryan Pricharde4ee12f2019-01-15 20:35:00 -080039#include "utils.h"
40
Ryan Prichard06d2d792019-01-23 23:19:19 -080041#if defined(__BIONIC__)
42#include "bionic/pthread_internal.h"
43#endif
44
Ryan Pricharde4ee12f2019-01-15 20:35:00 -080045// Access libtest_elftls_shared_var.so's TLS variable using an IE access.
46__attribute__((tls_model("initial-exec"))) extern "C" __thread int elftls_shared_var;
47
48TEST(elftls_dl, dlopen_shared_var_ie) {
49 // libtest_elftls_shared_var_ie.so can be dlopen'ed, even though it contains a
50 // TLS IE access, because its IE access references a TLS variable from
51 // libtest_elftls_shared_var.so, which is DT_NEEDED by the executable. This
52 // pattern appears in sanitizers, which use TLS IE instrumentation in shared
53 // objects to access special variables exported from the executable or from a
54 // preloaded solib.
55 void* lib = dlopen("libtest_elftls_shared_var_ie.so", RTLD_LOCAL | RTLD_NOW);
56 ASSERT_NE(nullptr, lib);
57
58 auto bump_shared_var = reinterpret_cast<int(*)()>(dlsym(lib, "bump_shared_var"));
59 ASSERT_NE(nullptr, bump_shared_var);
60
61 ASSERT_EQ(21, ++elftls_shared_var);
62 ASSERT_EQ(22, bump_shared_var());
63
64 std::thread([bump_shared_var] {
65 ASSERT_EQ(21, ++elftls_shared_var);
66 ASSERT_EQ(22, bump_shared_var());
67 }).join();
68}
69
70TEST(elftls_dl, dlopen_ie_error) {
Colin Crossbadcb382021-09-24 17:49:58 -070071 std::string helper = GetTestlibRoot() + "/elftls_dlopen_ie_error_helper";
Ryan Pricharde4ee12f2019-01-15 20:35:00 -080072 std::string src_path = GetTestlibRoot() + "/libtest_elftls_shared_var_ie.so";
73 std::string dst_path = GetTestlibRoot() + "/libtest_elftls_shared_var.so";
74#if defined(__BIONIC__)
75 std::string error =
76 "dlerror: dlopen failed: TLS symbol \"elftls_shared_var\" in dlopened \"" + dst_path + "\" " +
77 "referenced from \"" + src_path + "\" using IE access model\n";
78#else
79 // glibc will reserve some surplus static TLS memory, allowing this test to pass.
80 std::string error = "success\n";
81#endif
82
83 chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
84 ExecTestHelper eth;
85 eth.SetArgs({ helper.c_str(), nullptr });
86 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, error.c_str());
87}
Ryan Prichard06d2d792019-01-23 23:19:19 -080088
89// Use a GD access (__tls_get_addr or TLSDESC) to modify a variable in static
90// TLS memory.
91TEST(elftls_dl, access_static_tls) {
92 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
93 ASSERT_NE(nullptr, lib);
94
95 auto bump_shared_var = reinterpret_cast<int(*)()>(dlsym(lib, "bump_shared_var"));
96 ASSERT_NE(nullptr, bump_shared_var);
97
98 ASSERT_EQ(21, ++elftls_shared_var);
99 ASSERT_EQ(22, bump_shared_var());
100
101 std::thread([bump_shared_var] {
102 ASSERT_EQ(21, ++elftls_shared_var);
103 ASSERT_EQ(22, bump_shared_var());
104 }).join();
105}
106
107TEST(elftls_dl, bump_local_vars) {
108 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
109 ASSERT_NE(nullptr, lib);
110
111 auto bump_local_vars = reinterpret_cast<int(*)()>(dlsym(lib, "bump_local_vars"));
112 ASSERT_NE(nullptr, bump_local_vars);
113
114 ASSERT_EQ(42, bump_local_vars());
115 std::thread([bump_local_vars] {
116 ASSERT_EQ(42, bump_local_vars());
117 }).join();
118}
119
Ryan Prichard7fda2c92019-02-11 18:25:41 -0800120extern "C" int* missing_weak_tls_addr();
121
122// The Bionic linker resolves a TPREL relocation to an unresolved weak TLS
123// symbol to 0, which is added to the thread pointer. N.B.: A TPREL relocation
124// in a static executable is resolved by the static linker instead, and static
125// linker behavior varies (especially with bfd and gold). See
126// https://bugs.llvm.org/show_bug.cgi?id=40570.
127TEST(elftls_dl, tprel_missing_weak) {
128 ASSERT_EQ(static_cast<void*>(__get_tls()), missing_weak_tls_addr());
129 std::thread([] {
130 ASSERT_EQ(static_cast<void*>(__get_tls()), missing_weak_tls_addr());
131 }).join();
132}
133
Ryan Prichard06d2d792019-01-23 23:19:19 -0800134// The behavior of accessing an unresolved weak TLS symbol using a dynamic TLS
135// relocation depends on which kind of implementation the target uses. With
136// TLSDESC, the result is NULL. With __tls_get_addr, the result is the
137// generation count (or maybe undefined behavior)? This test only tests TLSDESC.
Ryan Prichard7fda2c92019-02-11 18:25:41 -0800138TEST(elftls_dl, tlsdesc_missing_weak) {
Ryan Prichard06d2d792019-01-23 23:19:19 -0800139#if defined(__aarch64__)
140 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
141 ASSERT_NE(nullptr, lib);
142
143 auto missing_weak_dyn_tls_addr = reinterpret_cast<int*(*)()>(dlsym(lib, "missing_weak_dyn_tls_addr"));
144 ASSERT_NE(nullptr, missing_weak_dyn_tls_addr);
145
146 ASSERT_EQ(nullptr, missing_weak_dyn_tls_addr());
147 std::thread([missing_weak_dyn_tls_addr] {
148 ASSERT_EQ(nullptr, missing_weak_dyn_tls_addr());
149 }).join();
150#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800151 GTEST_SKIP() << "This test is only run on TLSDESC-based targets";
Ryan Prichard06d2d792019-01-23 23:19:19 -0800152#endif
153}
154
155TEST(elftls_dl, dtv_resize) {
156#if defined(__BIONIC__)
Ryan Prichard98731dc2024-02-29 22:56:36 -0800157 std::string helper = GetTestlibRoot() + "/elftls_dtv_resize_helper";
158 chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
159 ExecTestHelper eth;
160 eth.SetArgs({helper.c_str(), nullptr});
161 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
Ryan Prichard06d2d792019-01-23 23:19:19 -0800162#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800163 GTEST_SKIP() << "test doesn't apply to glibc";
Ryan Prichard06d2d792019-01-23 23:19:19 -0800164#endif
165}
166
167// Verify that variables are reset to their initial values after the library
168// containing them is closed.
169TEST(elftls_dl, dlclose_resets_values) {
170 for (int round = 0; round < 2; ++round) {
171 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
172 ASSERT_NE(nullptr, lib);
173
174 auto bump_local_vars = reinterpret_cast<int(*)()>(dlsym(lib, "bump_local_vars"));
175 ASSERT_NE(nullptr, bump_local_vars);
176
177 ASSERT_EQ(42, bump_local_vars());
178 ASSERT_EQ(44, bump_local_vars());
179
180 ASSERT_EQ(0, dlclose(lib));
181 }
182}
183
184// Calling dlclose should remove the entry for the solib from the global list of
185// ELF TLS modules. Test that repeatedly loading and unloading a library doesn't
186// increase the DTV size.
187TEST(elftls_dl, dlclose_removes_entry) {
188#if defined(__BIONIC__)
189 auto dtv = []() -> TlsDtv* { return __get_tcb_dtv(__get_bionic_tcb()); };
190
191 bool first = true;
192 size_t count = 0;
193
194 // Use a large number of rounds in case the DTV is initially larger than
195 // expected.
196 for (int round = 0; round < 32; ++round) {
197 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
198 ASSERT_NE(nullptr, lib);
199
200 auto bump_local_vars = reinterpret_cast<int(*)()>(dlsym(lib, "bump_local_vars"));
201 ASSERT_NE(nullptr, bump_local_vars);
202
203 ASSERT_EQ(42, bump_local_vars());
204 if (first) {
205 first = false;
206 count = dtv()->count;
207 } else {
208 ASSERT_EQ(count, dtv()->count);
209 }
210
211 dlclose(lib);
212 }
213#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800214 GTEST_SKIP() << "test doesn't apply to glibc";
Ryan Prichard06d2d792019-01-23 23:19:19 -0800215#endif
216}
Ryan Pricharde4d620b2019-04-01 17:42:14 -0700217
218// Use dlsym to get the address of a TLS variable in static TLS and compare it
219// against the ordinary address of the variable.
220TEST(elftls_dl, dlsym_static_tls) {
221 void* lib = dlopen("libtest_elftls_shared_var.so", RTLD_LOCAL | RTLD_NOW);
222 ASSERT_NE(nullptr, lib);
223
224 int* var_addr = static_cast<int*>(dlsym(lib, "elftls_shared_var"));
225 ASSERT_EQ(&elftls_shared_var, var_addr);
226
227 std::thread([lib] {
228 int* var_addr = static_cast<int*>(dlsym(lib, "elftls_shared_var"));
229 ASSERT_EQ(&elftls_shared_var, var_addr);
230 }).join();
231}
232
233// Use dlsym to get the address of a TLS variable in dynamic TLS and compare it
234// against the ordinary address of the variable.
235TEST(elftls_dl, dlsym_dynamic_tls) {
236 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
237 ASSERT_NE(nullptr, lib);
238 auto get_var_addr = reinterpret_cast<int*(*)()>(dlsym(lib, "get_large_tls_var_addr"));
239 ASSERT_NE(nullptr, get_var_addr);
240
241 int* var_addr = static_cast<int*>(dlsym(lib, "large_tls_var"));
242 ASSERT_EQ(get_var_addr(), var_addr);
243
244 std::thread([lib, get_var_addr] {
245 int* var_addr = static_cast<int*>(dlsym(lib, "large_tls_var"));
246 ASSERT_EQ(get_var_addr(), var_addr);
247 }).join();
248}
249
250// Calling dladdr on a TLS variable's address doesn't find anything.
251TEST(elftls_dl, dladdr_on_tls_var) {
252 Dl_info info;
253
254 // Static TLS variable
255 ASSERT_EQ(0, dladdr(&elftls_shared_var, &info));
256
257 // Dynamic TLS variable
258 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
259 ASSERT_NE(nullptr, lib);
260 int* var_addr = static_cast<int*>(dlsym(lib, "large_tls_var"));
261 ASSERT_EQ(0, dladdr(var_addr, &info));
262}
263
264// Verify that dladdr does not misinterpret a TLS symbol's value as a virtual
265// address.
266TEST(elftls_dl, dladdr_skip_tls_symbol) {
267 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
268
269 auto get_local_addr = reinterpret_cast<void*(*)()>(dlsym(lib, "get_local_addr"));
270 ASSERT_NE(nullptr, get_local_addr);
271 void* local_addr = get_local_addr();
272
273 Dl_info info;
274 ASSERT_NE(0, dladdr(local_addr, &info));
275
276 std::string libpath = GetTestlibRoot() + "/libtest_elftls_dynamic.so";
277 char dli_realpath[PATH_MAX];
278 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath));
279 ASSERT_STREQ(libpath.c_str(), dli_realpath);
280 ASSERT_STREQ(nullptr, info.dli_sname);
281 ASSERT_EQ(nullptr, info.dli_saddr);
282}
Ryan Pricharda2e83ab2019-08-16 17:25:43 -0700283
284TEST(elftls_dl, dl_iterate_phdr) {
285 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
286
287 auto get_var_addr = reinterpret_cast<void*(*)()>(dlsym(lib, "get_large_tls_var_addr"));
288 ASSERT_NE(nullptr, get_var_addr);
289
290 struct TlsInfo {
291 bool found;
292 size_t modid;
293 void* data;
294 size_t memsz;
295 };
296
297 auto get_tls_info = []() {
298 auto callback = [](dl_phdr_info* info, size_t, void* data) {
299 TlsInfo& tls_info = *static_cast<TlsInfo*>(data);
300
301 // This test is also run with glibc, where dlpi_name may have relative path components, so
302 // examine just the basename when searching for the library.
Colin Cross7da20342021-07-28 11:18:11 -0700303 if (strcmp(android::base::Basename(info->dlpi_name).c_str(), "libtest_elftls_dynamic.so") != 0) return 0;
Ryan Pricharda2e83ab2019-08-16 17:25:43 -0700304
305 tls_info.found = true;
306 tls_info.modid = info->dlpi_tls_modid;
307 tls_info.data = info->dlpi_tls_data;
308 for (ElfW(Half) i = 0; i < info->dlpi_phnum; ++i) {
309 if (info->dlpi_phdr[i].p_type == PT_TLS) {
310 tls_info.memsz = info->dlpi_phdr[i].p_memsz;
311 }
312 }
313 EXPECT_NE(static_cast<size_t>(0), tls_info.memsz);
314 return 1;
315 };
316
317 TlsInfo result {};
318 dl_iterate_phdr(callback, &result);
319 return result;
320 };
321
322 // The executable has a TLS segment, so it will use module ID #1, and the DSO's ID will be larger
323 // than 1. Initially, the data field is nullptr, because this thread's instance hasn't been
324 // allocated yet.
325 TlsInfo tls_info = get_tls_info();
326 ASSERT_TRUE(tls_info.found);
327 ASSERT_GT(tls_info.modid, static_cast<size_t>(1));
328 ASSERT_EQ(nullptr, tls_info.data);
329
330 void* var_addr = get_var_addr();
331
332 // Verify that dl_iterate_phdr returns a range of memory covering the allocated TLS variable.
333 tls_info = get_tls_info();
334 ASSERT_TRUE(tls_info.found);
335 ASSERT_GE(var_addr, tls_info.data);
336 ASSERT_LT(var_addr, static_cast<char*>(tls_info.data) + tls_info.memsz);
337}