1 /*
2  * Copyright (C) 2024 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 <stdio.h>
31 #include <stdlib.h>
32 
33 #include <functional>
34 #include <iostream>
35 
36 #include "bionic/pthread_internal.h"
37 
38 constexpr bool kDumpModulesForDebugging = false;
39 
40 // The old external/libcxx doesn't have operator<< for nullptr.
41 // TODO(b/175635923): Remove this hack after upgrading libc++.
42 template <class T>
fix_nullptr(T && arg)43 T fix_nullptr(T&& arg) {
44   return arg;
45 }
fix_nullptr(nullptr_t arg)46 void* fix_nullptr(nullptr_t arg) {
47   return static_cast<void*>(arg);
48 }
49 
50 template <class Val1, class Val2, class Compare>
check(int line,const char * val1_expr,Val1 && val1,const char * val2_expr,Val2 && val2,Compare compare)51 void check(int line, const char* val1_expr, Val1&& val1, const char* val2_expr, Val2&& val2,
52            Compare compare) {
53   if (!compare(val1, val2)) {
54     std::cerr << __FILE__ << ":" << line << ": assertion failed: LHS(" << val1_expr << ") is "
55               << fix_nullptr(val1) << ", RHS(" << val2_expr << ") is " << fix_nullptr(val2) << "\n"
56               << std::flush;
57     abort();
58   }
59 }
60 
61 #define ASSERT_EQ(val1, val2) check(__LINE__, #val1, val1, #val2, val2, std::equal_to())
62 #define ASSERT_NE(val1, val2) check(__LINE__, #val1, val1, #val2, val2, std::not_equal_to())
63 #define ASSERT_LT(val1, val2) check(__LINE__, #val1, val1, #val2, val2, std::less())
64 #define ASSERT_LE(val1, val2) check(__LINE__, #val1, val1, #val2, val2, std::less_equal())
65 
highest_loaded_modid()66 static size_t highest_loaded_modid() {
67   size_t result = 0;
68   auto update_result = [](struct dl_phdr_info* info, size_t size __unused, void* data) {
69     size_t& result = *reinterpret_cast<size_t*>(data);
70     if (kDumpModulesForDebugging) {
71       fprintf(stderr, "module %s: TLS modid %zu\n", info->dlpi_name, info->dlpi_tls_modid);
72     }
73     result = std::max(result, info->dlpi_tls_modid);
74     return 0;
75   };
76   dl_iterate_phdr(update_result, &result);
77   return result;
78 }
79 
dtv()80 static TlsDtv* dtv() {
81   return __get_tcb_dtv(__get_bionic_tcb());
82 }
83 
highest_modid_in_dtv()84 static size_t highest_modid_in_dtv() {
85   TlsDtv* current_dtv = dtv();
86   size_t result = 0;
87   for (size_t i = 0; i < current_dtv->count; ++i) {
88     if (current_dtv->modules[i] != nullptr) {
89       result = __tls_module_idx_to_id(i);
90     }
91   }
92   return result;
93 }
94 
95 // Unused, but ensures that the test executable has a TLS segment. With a
96 // new-enough libc++_static.a, the test executable will tend to has a TLS
97 // segment to hold the libc++ EH globals pointer.
98 __thread int g_tls_var_placeholder = 42;
99 
main()100 int main() {
101   // Prevent this TLS variable from being optimized away.
102   ASSERT_EQ(42, g_tls_var_placeholder);
103 
104   auto load_lib = [](const char* soname) {
105     void* lib = dlopen(soname, RTLD_LOCAL | RTLD_NOW);
106     ASSERT_NE(nullptr, lib);
107     auto func = reinterpret_cast<int (*)()>(dlsym(lib, "bump"));
108     ASSERT_NE(nullptr, func);
109     return func;
110   };
111 
112   static_assert(sizeof(TlsDtv) == 3 * sizeof(void*),
113                 "This test assumes that the Dtv has a 3-word header");
114 
115   // Initially there are 2-4 modules:
116   //  - 1: test executable
117   //  - 2: libc
118   //  - 3: libc++ (when using a new-enough libc++)
119   //  - 4: libclang_rt.hwasan (when running with HWASan)
120   size_t first_filler_modid = highest_loaded_modid() + 1;
121   ASSERT_LE(2, highest_loaded_modid());
122   ASSERT_LE(highest_loaded_modid(), 4);
123 
124   // The initial DTV is an empty DTV with no generation and a size of 0.
125   TlsDtv* zero_dtv = dtv();
126   ASSERT_EQ(0u, zero_dtv->count);
127   ASSERT_EQ(nullptr, zero_dtv->next);
128   ASSERT_EQ(kTlsGenerationNone, zero_dtv->generation);
129 
130   // Load a module. The DTV is still empty unless the TLS variable is accessed.
131   auto func1 = load_lib("libtest_elftls_dynamic_filler_1.so");
132   ASSERT_EQ(zero_dtv, dtv());
133   ASSERT_EQ(first_filler_modid, highest_loaded_modid());
134 
135   // After accessing a TLS variable, the DTV should be initialized. It should be
136   // 8 words in size, with a 5-entry capacity.
137   ASSERT_EQ(101, func1());
138   TlsDtv* initial_dtv = dtv();
139   ASSERT_EQ(5u, dtv()->count);
140   ASSERT_EQ(zero_dtv, initial_dtv->next);
141   ASSERT_LT(0u, initial_dtv->generation);
142   ASSERT_EQ(first_filler_modid, highest_modid_in_dtv());
143   ASSERT_NE(nullptr, initial_dtv->modules[__tls_module_id_to_idx(first_filler_modid)]);
144 
145   size_t current_generation = initial_dtv->generation;
146 
147   // Fill the rest of the DTV up. (i.e. Ensure that exactly 5 modules with TLS
148   // segments are loaded.)
149   auto fill_entry = [&](size_t modid, const char* soname, int tls_var_value) {
150     if (highest_modid_in_dtv() == modid - 1) {
151       auto func = load_lib(soname);
152 
153       // Loading the module doesn't affect the DTV yet.
154       ASSERT_EQ(initial_dtv, dtv());
155       ASSERT_EQ(modid, highest_loaded_modid());
156       ASSERT_EQ(modid - 1, highest_modid_in_dtv());
157       ASSERT_EQ(current_generation, initial_dtv->generation);
158 
159       // Access the TLS variable, which will allocate it in the DTV.
160       ASSERT_EQ(tls_var_value, func());
161 
162       // Verify allocation and a bumped generation.
163       ASSERT_EQ(initial_dtv, dtv());
164       ASSERT_EQ(modid, highest_modid_in_dtv());
165       ASSERT_LT(current_generation, initial_dtv->generation);
166       current_generation = initial_dtv->generation;
167     }
168   };
169 
170   fill_entry(4u, "libtest_elftls_dynamic_filler_2.so", 201);
171   fill_entry(5u, "libtest_elftls_dynamic_filler_3.so", 301);
172   ASSERT_EQ(5u, highest_modid_in_dtv());
173 
174   // Load module 6, which will require doubling the size of the DTV.
175   auto func4 = load_lib("libtest_elftls_dynamic_filler_4.so");
176   ASSERT_EQ(6u, highest_loaded_modid());
177   ASSERT_EQ(5u, highest_modid_in_dtv());
178   ASSERT_EQ(initial_dtv, dtv());
179 
180   // Access a TLS variable from the first filler module.
181   ASSERT_EQ(102, func1());
182   ASSERT_EQ(5u, highest_modid_in_dtv());
183 #if defined(__aarch64__) || defined(__riscv)
184   // The arm64 and riscv64 TLSDESC resolver doesn't update the DTV if it is new enough for
185   // the given access.
186   ASSERT_EQ(initial_dtv, dtv());
187   ASSERT_EQ(5u, dtv()->count);
188   ASSERT_EQ(current_generation, dtv()->generation);
189 #else
190   // __tls_get_addr updates the DTV anytime the generation counter changes, but
191   // the highest modid in the DTV is still 5, because module 6 hasn't been
192   // allocated yet.
193   ASSERT_NE(initial_dtv, dtv());
194   ASSERT_EQ(13u, dtv()->count);
195   ASSERT_LT(current_generation, dtv()->generation);
196 #endif
197 
198   // Accessing the TLS variable in the latest module will always expand the DTV.
199   ASSERT_EQ(401, func4());
200   TlsDtv* new_dtv = dtv();
201   ASSERT_NE(initial_dtv, new_dtv);
202   ASSERT_EQ(initial_dtv, new_dtv->next);
203   ASSERT_EQ(13u, new_dtv->count);
204   ASSERT_LT(current_generation, new_dtv->generation);
205   ASSERT_EQ(6u, highest_modid_in_dtv());
206   current_generation = new_dtv->generation;
207 
208   // Load one more filler, module 7.
209   auto func5 = load_lib("libtest_elftls_dynamic_filler_5.so");
210   ASSERT_EQ(103, func1());
211   ASSERT_EQ(402, func4());
212   ASSERT_EQ(6u, highest_modid_in_dtv());
213   ASSERT_EQ(501, func5());
214   ASSERT_EQ(7u, highest_modid_in_dtv());
215 
216   // Verify that no new DTV has been allocated.
217   ASSERT_EQ(new_dtv, dtv());
218   ASSERT_EQ(13u, new_dtv->count);
219   ASSERT_LT(current_generation, new_dtv->generation);
220 
221   return 0;
222 }
223