blob: e908fb930887c2a53334a7cd4b6cbe03bac37143 [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>
30#include <gtest/gtest.h>
31
32#include <thread>
33
34#include "gtest_globals.h"
35#include "utils.h"
36
Ryan Prichard06d2d792019-01-23 23:19:19 -080037#if defined(__BIONIC__)
38#include "bionic/pthread_internal.h"
39#endif
40
Ryan Pricharde4ee12f2019-01-15 20:35:00 -080041// Access libtest_elftls_shared_var.so's TLS variable using an IE access.
42__attribute__((tls_model("initial-exec"))) extern "C" __thread int elftls_shared_var;
43
44TEST(elftls_dl, dlopen_shared_var_ie) {
45 // libtest_elftls_shared_var_ie.so can be dlopen'ed, even though it contains a
46 // TLS IE access, because its IE access references a TLS variable from
47 // libtest_elftls_shared_var.so, which is DT_NEEDED by the executable. This
48 // pattern appears in sanitizers, which use TLS IE instrumentation in shared
49 // objects to access special variables exported from the executable or from a
50 // preloaded solib.
51 void* lib = dlopen("libtest_elftls_shared_var_ie.so", RTLD_LOCAL | RTLD_NOW);
52 ASSERT_NE(nullptr, lib);
53
54 auto bump_shared_var = reinterpret_cast<int(*)()>(dlsym(lib, "bump_shared_var"));
55 ASSERT_NE(nullptr, bump_shared_var);
56
57 ASSERT_EQ(21, ++elftls_shared_var);
58 ASSERT_EQ(22, bump_shared_var());
59
60 std::thread([bump_shared_var] {
61 ASSERT_EQ(21, ++elftls_shared_var);
62 ASSERT_EQ(22, bump_shared_var());
63 }).join();
64}
65
66TEST(elftls_dl, dlopen_ie_error) {
67 std::string helper = GetTestlibRoot() +
68 "/elftls_dlopen_ie_error_helper/elftls_dlopen_ie_error_helper";
69 std::string src_path = GetTestlibRoot() + "/libtest_elftls_shared_var_ie.so";
70 std::string dst_path = GetTestlibRoot() + "/libtest_elftls_shared_var.so";
71#if defined(__BIONIC__)
72 std::string error =
73 "dlerror: dlopen failed: TLS symbol \"elftls_shared_var\" in dlopened \"" + dst_path + "\" " +
74 "referenced from \"" + src_path + "\" using IE access model\n";
75#else
76 // glibc will reserve some surplus static TLS memory, allowing this test to pass.
77 std::string error = "success\n";
78#endif
79
80 chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
81 ExecTestHelper eth;
82 eth.SetArgs({ helper.c_str(), nullptr });
83 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, error.c_str());
84}
Ryan Prichard06d2d792019-01-23 23:19:19 -080085
86// Use a GD access (__tls_get_addr or TLSDESC) to modify a variable in static
87// TLS memory.
88TEST(elftls_dl, access_static_tls) {
89 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
90 ASSERT_NE(nullptr, lib);
91
92 auto bump_shared_var = reinterpret_cast<int(*)()>(dlsym(lib, "bump_shared_var"));
93 ASSERT_NE(nullptr, bump_shared_var);
94
95 ASSERT_EQ(21, ++elftls_shared_var);
96 ASSERT_EQ(22, bump_shared_var());
97
98 std::thread([bump_shared_var] {
99 ASSERT_EQ(21, ++elftls_shared_var);
100 ASSERT_EQ(22, bump_shared_var());
101 }).join();
102}
103
104TEST(elftls_dl, bump_local_vars) {
105 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
106 ASSERT_NE(nullptr, lib);
107
108 auto bump_local_vars = reinterpret_cast<int(*)()>(dlsym(lib, "bump_local_vars"));
109 ASSERT_NE(nullptr, bump_local_vars);
110
111 ASSERT_EQ(42, bump_local_vars());
112 std::thread([bump_local_vars] {
113 ASSERT_EQ(42, bump_local_vars());
114 }).join();
115}
116
117// The behavior of accessing an unresolved weak TLS symbol using a dynamic TLS
118// relocation depends on which kind of implementation the target uses. With
119// TLSDESC, the result is NULL. With __tls_get_addr, the result is the
120// generation count (or maybe undefined behavior)? This test only tests TLSDESC.
121TEST(elftls_dl, missing_weak) {
122#if defined(__aarch64__)
123 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
124 ASSERT_NE(nullptr, lib);
125
126 auto missing_weak_dyn_tls_addr = reinterpret_cast<int*(*)()>(dlsym(lib, "missing_weak_dyn_tls_addr"));
127 ASSERT_NE(nullptr, missing_weak_dyn_tls_addr);
128
129 ASSERT_EQ(nullptr, missing_weak_dyn_tls_addr());
130 std::thread([missing_weak_dyn_tls_addr] {
131 ASSERT_EQ(nullptr, missing_weak_dyn_tls_addr());
132 }).join();
133#else
134 GTEST_LOG_(INFO) << "This test is only run on TLSDESC-based targets.\n";
135#endif
136}
137
138TEST(elftls_dl, dtv_resize) {
139#if defined(__BIONIC__)
140#define LOAD_LIB(soname) ({ \
141 auto lib = dlopen(soname, RTLD_LOCAL | RTLD_NOW); \
142 ASSERT_NE(nullptr, lib); \
143 reinterpret_cast<int(*)()>(dlsym(lib, "bump")); \
144 })
145
146 auto dtv = []() -> TlsDtv* { return __get_tcb_dtv(__get_bionic_tcb()); };
147
148 static_assert(sizeof(TlsDtv) == 3 * sizeof(void*),
149 "This test assumes that the Dtv has a 3-word header");
150
151 // Initially there are 3 modules:
152 // - the main test executable
153 // - libtest_elftls_shared_var
154 // - libtest_elftls_tprel
155
156 // The initial DTV is an empty DTV with no generation and a size of 0.
157 TlsDtv* zero_dtv = dtv();
158 ASSERT_EQ(0u, zero_dtv->count);
159 ASSERT_EQ(nullptr, zero_dtv->next);
160 ASSERT_EQ(kTlsGenerationNone, zero_dtv->generation);
161
162 // Load the fourth module.
163 auto func1 = LOAD_LIB("libtest_elftls_dynamic_filler_1.so");
164 ASSERT_EQ(101, func1());
165
166 // After loading one module, the DTV should be initialized to the next
167 // power-of-2 size (including the header).
168 TlsDtv* initial_dtv = dtv();
169 ASSERT_EQ(5u, initial_dtv->count);
170 ASSERT_EQ(zero_dtv, initial_dtv->next);
171 ASSERT_LT(0u, initial_dtv->generation);
172
173 // Load module 5.
174 auto func2 = LOAD_LIB("libtest_elftls_dynamic_filler_2.so");
175 ASSERT_EQ(102, func1());
176 ASSERT_EQ(201, func2());
177 ASSERT_EQ(initial_dtv, dtv());
178 ASSERT_EQ(5u, initial_dtv->count);
179
180 // Load module 6.
181 auto func3 = LOAD_LIB("libtest_elftls_dynamic_filler_3.so");
182 ASSERT_EQ(103, func1());
183 ASSERT_EQ(202, func2());
184
185#if defined(__aarch64__)
186 // The arm64 TLSDESC resolver doesn't update the DTV if it is new enough for
187 // the given access.
188 ASSERT_EQ(5u, dtv()->count);
189#else
190 // __tls_get_addr updates the DTV anytime the generation counter changes.
191 ASSERT_EQ(13u, dtv()->count);
192#endif
193
194 ASSERT_EQ(301, func3());
195
196 TlsDtv* new_dtv = dtv();
197 ASSERT_EQ(13u, new_dtv->count);
198 ASSERT_NE(initial_dtv, new_dtv);
199 ASSERT_EQ(initial_dtv, new_dtv->next);
200
201#undef LOAD_LIB
202#else
203 GTEST_LOG_(INFO) << "This test is skipped for glibc because it tests Bionic internals.";
204#endif
205}
206
207// Verify that variables are reset to their initial values after the library
208// containing them is closed.
209TEST(elftls_dl, dlclose_resets_values) {
210 for (int round = 0; round < 2; ++round) {
211 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
212 ASSERT_NE(nullptr, lib);
213
214 auto bump_local_vars = reinterpret_cast<int(*)()>(dlsym(lib, "bump_local_vars"));
215 ASSERT_NE(nullptr, bump_local_vars);
216
217 ASSERT_EQ(42, bump_local_vars());
218 ASSERT_EQ(44, bump_local_vars());
219
220 ASSERT_EQ(0, dlclose(lib));
221 }
222}
223
224// Calling dlclose should remove the entry for the solib from the global list of
225// ELF TLS modules. Test that repeatedly loading and unloading a library doesn't
226// increase the DTV size.
227TEST(elftls_dl, dlclose_removes_entry) {
228#if defined(__BIONIC__)
229 auto dtv = []() -> TlsDtv* { return __get_tcb_dtv(__get_bionic_tcb()); };
230
231 bool first = true;
232 size_t count = 0;
233
234 // Use a large number of rounds in case the DTV is initially larger than
235 // expected.
236 for (int round = 0; round < 32; ++round) {
237 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
238 ASSERT_NE(nullptr, lib);
239
240 auto bump_local_vars = reinterpret_cast<int(*)()>(dlsym(lib, "bump_local_vars"));
241 ASSERT_NE(nullptr, bump_local_vars);
242
243 ASSERT_EQ(42, bump_local_vars());
244 if (first) {
245 first = false;
246 count = dtv()->count;
247 } else {
248 ASSERT_EQ(count, dtv()->count);
249 }
250
251 dlclose(lib);
252 }
253#else
254 GTEST_LOG_(INFO) << "This test is skipped for glibc because it tests Bionic internals.";
255#endif
256}