blob: e759e1565f93f57389066fc901b0604729292451 [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) {
Elliott Hughes8d8138a2024-03-12 22:37:13 +000071 std::string helper = GetTestLibRoot() + "/elftls_dlopen_ie_error_helper";
72 std::string src_path = GetTestLibRoot() + "/libtest_elftls_shared_var_ie.so";
73 std::string dst_path = GetTestLibRoot() + "/libtest_elftls_shared_var.so";
Ryan Pricharde4ee12f2019-01-15 20:35:00 -080074#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
Ryan Pricharde4ee12f2019-01-15 20:35:00 -080083 ExecTestHelper eth;
84 eth.SetArgs({ helper.c_str(), nullptr });
85 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, error.c_str());
86}
Ryan Prichard06d2d792019-01-23 23:19:19 -080087
88// Use a GD access (__tls_get_addr or TLSDESC) to modify a variable in static
89// TLS memory.
90TEST(elftls_dl, access_static_tls) {
91 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
92 ASSERT_NE(nullptr, lib);
93
94 auto bump_shared_var = reinterpret_cast<int(*)()>(dlsym(lib, "bump_shared_var"));
95 ASSERT_NE(nullptr, bump_shared_var);
96
97 ASSERT_EQ(21, ++elftls_shared_var);
98 ASSERT_EQ(22, bump_shared_var());
99
100 std::thread([bump_shared_var] {
101 ASSERT_EQ(21, ++elftls_shared_var);
102 ASSERT_EQ(22, bump_shared_var());
103 }).join();
104}
105
106TEST(elftls_dl, bump_local_vars) {
107 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
108 ASSERT_NE(nullptr, lib);
109
110 auto bump_local_vars = reinterpret_cast<int(*)()>(dlsym(lib, "bump_local_vars"));
111 ASSERT_NE(nullptr, bump_local_vars);
112
113 ASSERT_EQ(42, bump_local_vars());
114 std::thread([bump_local_vars] {
115 ASSERT_EQ(42, bump_local_vars());
116 }).join();
117}
118
Ryan Prichard7fda2c92019-02-11 18:25:41 -0800119extern "C" int* missing_weak_tls_addr();
120
121// The Bionic linker resolves a TPREL relocation to an unresolved weak TLS
122// symbol to 0, which is added to the thread pointer. N.B.: A TPREL relocation
123// in a static executable is resolved by the static linker instead, and static
124// linker behavior varies (especially with bfd and gold). See
125// https://bugs.llvm.org/show_bug.cgi?id=40570.
126TEST(elftls_dl, tprel_missing_weak) {
127 ASSERT_EQ(static_cast<void*>(__get_tls()), missing_weak_tls_addr());
128 std::thread([] {
129 ASSERT_EQ(static_cast<void*>(__get_tls()), missing_weak_tls_addr());
130 }).join();
131}
132
Ryan Prichard06d2d792019-01-23 23:19:19 -0800133// The behavior of accessing an unresolved weak TLS symbol using a dynamic TLS
134// relocation depends on which kind of implementation the target uses. With
135// TLSDESC, the result is NULL. With __tls_get_addr, the result is the
136// generation count (or maybe undefined behavior)? This test only tests TLSDESC.
Ryan Prichard7fda2c92019-02-11 18:25:41 -0800137TEST(elftls_dl, tlsdesc_missing_weak) {
Ryan Prichard06d2d792019-01-23 23:19:19 -0800138#if defined(__aarch64__)
139 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
140 ASSERT_NE(nullptr, lib);
141
142 auto missing_weak_dyn_tls_addr = reinterpret_cast<int*(*)()>(dlsym(lib, "missing_weak_dyn_tls_addr"));
143 ASSERT_NE(nullptr, missing_weak_dyn_tls_addr);
144
145 ASSERT_EQ(nullptr, missing_weak_dyn_tls_addr());
146 std::thread([missing_weak_dyn_tls_addr] {
147 ASSERT_EQ(nullptr, missing_weak_dyn_tls_addr());
148 }).join();
149#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800150 GTEST_SKIP() << "This test is only run on TLSDESC-based targets";
Ryan Prichard06d2d792019-01-23 23:19:19 -0800151#endif
152}
153
154TEST(elftls_dl, dtv_resize) {
155#if defined(__BIONIC__)
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000156 std::string helper = GetTestLibRoot() + "/elftls_dtv_resize_helper";
Ryan Prichard98731dc2024-02-29 22:56:36 -0800157 ExecTestHelper eth;
158 eth.SetArgs({helper.c_str(), nullptr});
159 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
Ryan Prichard06d2d792019-01-23 23:19:19 -0800160#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800161 GTEST_SKIP() << "test doesn't apply to glibc";
Ryan Prichard06d2d792019-01-23 23:19:19 -0800162#endif
163}
164
165// Verify that variables are reset to their initial values after the library
166// containing them is closed.
167TEST(elftls_dl, dlclose_resets_values) {
168 for (int round = 0; round < 2; ++round) {
169 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
170 ASSERT_NE(nullptr, lib);
171
172 auto bump_local_vars = reinterpret_cast<int(*)()>(dlsym(lib, "bump_local_vars"));
173 ASSERT_NE(nullptr, bump_local_vars);
174
175 ASSERT_EQ(42, bump_local_vars());
176 ASSERT_EQ(44, bump_local_vars());
177
178 ASSERT_EQ(0, dlclose(lib));
179 }
180}
181
182// Calling dlclose should remove the entry for the solib from the global list of
183// ELF TLS modules. Test that repeatedly loading and unloading a library doesn't
184// increase the DTV size.
185TEST(elftls_dl, dlclose_removes_entry) {
186#if defined(__BIONIC__)
187 auto dtv = []() -> TlsDtv* { return __get_tcb_dtv(__get_bionic_tcb()); };
188
189 bool first = true;
190 size_t count = 0;
191
192 // Use a large number of rounds in case the DTV is initially larger than
193 // expected.
194 for (int round = 0; round < 32; ++round) {
195 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
196 ASSERT_NE(nullptr, lib);
197
198 auto bump_local_vars = reinterpret_cast<int(*)()>(dlsym(lib, "bump_local_vars"));
199 ASSERT_NE(nullptr, bump_local_vars);
200
201 ASSERT_EQ(42, bump_local_vars());
202 if (first) {
203 first = false;
204 count = dtv()->count;
205 } else {
206 ASSERT_EQ(count, dtv()->count);
207 }
208
209 dlclose(lib);
210 }
211#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800212 GTEST_SKIP() << "test doesn't apply to glibc";
Ryan Prichard06d2d792019-01-23 23:19:19 -0800213#endif
214}
Ryan Pricharde4d620b2019-04-01 17:42:14 -0700215
216// Use dlsym to get the address of a TLS variable in static TLS and compare it
217// against the ordinary address of the variable.
218TEST(elftls_dl, dlsym_static_tls) {
219 void* lib = dlopen("libtest_elftls_shared_var.so", RTLD_LOCAL | RTLD_NOW);
220 ASSERT_NE(nullptr, lib);
221
222 int* var_addr = static_cast<int*>(dlsym(lib, "elftls_shared_var"));
223 ASSERT_EQ(&elftls_shared_var, var_addr);
224
225 std::thread([lib] {
226 int* var_addr = static_cast<int*>(dlsym(lib, "elftls_shared_var"));
227 ASSERT_EQ(&elftls_shared_var, var_addr);
228 }).join();
229}
230
231// Use dlsym to get the address of a TLS variable in dynamic TLS and compare it
232// against the ordinary address of the variable.
233TEST(elftls_dl, dlsym_dynamic_tls) {
234 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
235 ASSERT_NE(nullptr, lib);
236 auto get_var_addr = reinterpret_cast<int*(*)()>(dlsym(lib, "get_large_tls_var_addr"));
237 ASSERT_NE(nullptr, get_var_addr);
238
239 int* var_addr = static_cast<int*>(dlsym(lib, "large_tls_var"));
240 ASSERT_EQ(get_var_addr(), var_addr);
241
242 std::thread([lib, get_var_addr] {
243 int* var_addr = static_cast<int*>(dlsym(lib, "large_tls_var"));
244 ASSERT_EQ(get_var_addr(), var_addr);
245 }).join();
246}
247
248// Calling dladdr on a TLS variable's address doesn't find anything.
249TEST(elftls_dl, dladdr_on_tls_var) {
250 Dl_info info;
251
252 // Static TLS variable
253 ASSERT_EQ(0, dladdr(&elftls_shared_var, &info));
254
255 // Dynamic TLS variable
256 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
257 ASSERT_NE(nullptr, lib);
258 int* var_addr = static_cast<int*>(dlsym(lib, "large_tls_var"));
259 ASSERT_EQ(0, dladdr(var_addr, &info));
260}
261
262// Verify that dladdr does not misinterpret a TLS symbol's value as a virtual
263// address.
264TEST(elftls_dl, dladdr_skip_tls_symbol) {
265 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
266
267 auto get_local_addr = reinterpret_cast<void*(*)()>(dlsym(lib, "get_local_addr"));
268 ASSERT_NE(nullptr, get_local_addr);
269 void* local_addr = get_local_addr();
270
271 Dl_info info;
272 ASSERT_NE(0, dladdr(local_addr, &info));
273
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000274 std::string libpath = GetTestLibRoot() + "/libtest_elftls_dynamic.so";
Ryan Pricharde4d620b2019-04-01 17:42:14 -0700275 char dli_realpath[PATH_MAX];
276 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath));
277 ASSERT_STREQ(libpath.c_str(), dli_realpath);
278 ASSERT_STREQ(nullptr, info.dli_sname);
279 ASSERT_EQ(nullptr, info.dli_saddr);
280}
Ryan Pricharda2e83ab2019-08-16 17:25:43 -0700281
282TEST(elftls_dl, dl_iterate_phdr) {
283 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
284
285 auto get_var_addr = reinterpret_cast<void*(*)()>(dlsym(lib, "get_large_tls_var_addr"));
286 ASSERT_NE(nullptr, get_var_addr);
287
288 struct TlsInfo {
289 bool found;
290 size_t modid;
291 void* data;
292 size_t memsz;
293 };
294
295 auto get_tls_info = []() {
296 auto callback = [](dl_phdr_info* info, size_t, void* data) {
297 TlsInfo& tls_info = *static_cast<TlsInfo*>(data);
298
299 // This test is also run with glibc, where dlpi_name may have relative path components, so
300 // examine just the basename when searching for the library.
Colin Cross7da20342021-07-28 11:18:11 -0700301 if (strcmp(android::base::Basename(info->dlpi_name).c_str(), "libtest_elftls_dynamic.so") != 0) return 0;
Ryan Pricharda2e83ab2019-08-16 17:25:43 -0700302
303 tls_info.found = true;
304 tls_info.modid = info->dlpi_tls_modid;
305 tls_info.data = info->dlpi_tls_data;
306 for (ElfW(Half) i = 0; i < info->dlpi_phnum; ++i) {
307 if (info->dlpi_phdr[i].p_type == PT_TLS) {
308 tls_info.memsz = info->dlpi_phdr[i].p_memsz;
309 }
310 }
311 EXPECT_NE(static_cast<size_t>(0), tls_info.memsz);
312 return 1;
313 };
314
315 TlsInfo result {};
316 dl_iterate_phdr(callback, &result);
317 return result;
318 };
319
320 // The executable has a TLS segment, so it will use module ID #1, and the DSO's ID will be larger
321 // than 1. Initially, the data field is nullptr, because this thread's instance hasn't been
322 // allocated yet.
323 TlsInfo tls_info = get_tls_info();
324 ASSERT_TRUE(tls_info.found);
325 ASSERT_GT(tls_info.modid, static_cast<size_t>(1));
326 ASSERT_EQ(nullptr, tls_info.data);
327
328 void* var_addr = get_var_addr();
329
330 // Verify that dl_iterate_phdr returns a range of memory covering the allocated TLS variable.
331 tls_info = get_tls_info();
332 ASSERT_TRUE(tls_info.found);
333 ASSERT_GE(var_addr, tls_info.data);
334 ASSERT_LT(var_addr, static_cast<char*>(tls_info.data) + tls_info.memsz);
335}