blob: 3f70279c1d2d52b607969195f585b3a6eb1fea04 [file] [log] [blame]
jeffhaoacf5aa72012-09-12 17:25:30 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <dlfcn.h>
Elliott Hughes1ff7be02022-01-13 15:43:23 -080020#include <elf.h>
Elliott Hughes8e15b082012-09-26 11:44:01 -070021#include <limits.h>
Elliott Hughes1ff7be02022-01-13 15:43:23 -080022#include <link.h>
Elliott Hughes8e15b082012-09-26 11:44:01 -070023#include <stdint.h>
Colin Cross14d15072021-08-16 16:35:27 -070024#include <stdio.h>
Dimitry Ivanov708589f2016-09-19 10:50:28 -070025#include <string.h>
Colin Cross4c5595c2021-08-16 15:51:59 -070026#include <sys/cdefs.h>
dimitryb9555a92017-10-11 18:04:05 +020027#if __has_include(<sys/auxv.h>)
28#include <sys/auxv.h>
29#endif
dimitry109040c2017-11-03 13:49:07 +010030#include <sys/user.h>
Elliott Hughes8e15b082012-09-26 11:44:01 -070031
32#include <string>
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -080033#include <thread>
jeffhaoacf5aa72012-09-12 17:25:30 -070034
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -070035#include <android-base/file.h>
Elliott Hughes3f73ea62022-10-19 16:16:54 +000036#include <android-base/macros.h>
Tom Cherryb8ab6182017-04-05 16:20:29 -070037#include <android-base/scopeguard.h>
38
Dimitry Ivanov927877c2016-09-21 11:17:13 -070039#include "gtest_globals.h"
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -070040#include "gtest_utils.h"
Dimitry Ivanov708589f2016-09-19 10:50:28 -070041#include "dlfcn_symlink_support.h"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070042#include "utils.h"
43
Elliott Hughes3b297c42012-10-11 16:08:51 -070044#define ASSERT_SUBSTR(needle, haystack) \
45 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
46
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070047
Elliott Hughes1728b232014-05-14 10:02:03 -070048static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070049extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070050 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070051}
52
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070053static int g_ctor_function_called = 0;
Dimitry Ivanov55437462016-07-20 15:33:07 -070054static int g_ctor_argc = 0;
55static char** g_ctor_argv = reinterpret_cast<char**>(0xDEADBEEF);
56static char** g_ctor_envp = g_ctor_envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070057
Dimitry Ivanov55437462016-07-20 15:33:07 -070058extern "C" void ctor_function(int argc, char** argv, char** envp) __attribute__ ((constructor));
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070059
Dimitry Ivanov55437462016-07-20 15:33:07 -070060extern "C" void ctor_function(int argc, char** argv, char** envp) {
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070061 g_ctor_function_called = 17;
Dimitry Ivanov55437462016-07-20 15:33:07 -070062 g_ctor_argc = argc;
63 g_ctor_argv = argv;
64 g_ctor_envp = envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070065}
66
67TEST(dlfcn, ctor_function_call) {
68 ASSERT_EQ(17, g_ctor_function_called);
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -070069 ASSERT_TRUE(g_ctor_argc = GetArgc());
70 ASSERT_TRUE(g_ctor_argv = GetArgv());
71 ASSERT_TRUE(g_ctor_envp = GetEnvp());
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070072}
73
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070074TEST(dlfcn, dlsym_in_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070075 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070076 void* self = dlopen(nullptr, RTLD_NOW);
77 ASSERT_TRUE(self != nullptr);
78 ASSERT_TRUE(dlerror() == nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070079
80 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070081 ASSERT_TRUE(sym != nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070082
83 void (*function)() = reinterpret_cast<void(*)()>(sym);
84
Elliott Hughes1728b232014-05-14 10:02:03 -070085 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070086 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070087 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070088
89 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070090}
Elliott Hughes8e15b082012-09-26 11:44:01 -070091
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070092TEST(dlfcn, dlsym_from_sofile) {
93 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL);
94 ASSERT_TRUE(handle != nullptr) << dlerror();
95
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070096 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070097 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
98 ASSERT_TRUE(symbol == nullptr);
99 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
100
101 typedef int* (*fn_t)();
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700102 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
103 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
104 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700105
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700106 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700107 ASSERT_TRUE(ptr != nullptr) << dlerror();
108 ASSERT_EQ(42, *ptr);
109
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700110 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
111 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
112 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
113
114 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
115 ASSERT_TRUE(ptr != nullptr) << dlerror();
116 ASSERT_EQ(44, *ptr);
117
118 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
119 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
120 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
121
122 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
123 ASSERT_TRUE(ptr != nullptr) << dlerror();
124 ASSERT_EQ(43, *ptr);
125
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700126 dlclose(handle);
127}
128
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700129TEST(dlfcn, dlsym_from_sofile_with_preload) {
130 void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL);
131 ASSERT_TRUE(preload != nullptr) << dlerror();
132
133 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
134 ASSERT_TRUE(handle != nullptr) << dlerror();
135
136 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
137 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
138 ASSERT_TRUE(symbol == nullptr);
139 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
140
141 typedef int* (*fn_t)();
142 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
143 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
144 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
145
146 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
147 ASSERT_TRUE(ptr != nullptr) << dlerror();
148 ASSERT_EQ(42, *ptr);
149
150 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
151 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
152 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
153
154 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
155 ASSERT_TRUE(ptr != nullptr) << dlerror();
156 ASSERT_EQ(44, *ptr);
157
158 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
159 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
160 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
161
162 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
163 ASSERT_TRUE(ptr != nullptr) << dlerror();
164 ASSERT_EQ(43, *ptr);
165
166 dlclose(handle);
167 dlclose(preload);
168}
169
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700170TEST(dlfcn, dlsym_handle_global_sym) {
171 // check that we do not look into global group
172 // when looking up symbol by handle
173 void* handle = dlopen("libtest_empty.so", RTLD_NOW);
174 dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL);
175 void* sym = dlsym(handle, "getRandomNumber");
176 ASSERT_TRUE(sym == nullptr);
177 ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror());
178
179 sym = dlsym(handle, "DlSymTestFunction");
180 ASSERT_TRUE(sym == nullptr);
181 ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror());
182 dlclose(handle);
183}
184
Dimitry Ivanovd5b578a2016-12-14 15:16:56 -0800185TEST(dlfcn, dlsym_handle_empty_symbol) {
186 // check that dlsym of an empty symbol fails (see http://b/33530622)
187 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW);
188 ASSERT_TRUE(handle != nullptr) << dlerror();
189 void* sym = dlsym(handle, "");
190 ASSERT_TRUE(sym == nullptr);
191 ASSERT_SUBSTR("undefined symbol: ", dlerror());
192 dlclose(handle);
193}
194
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700195TEST(dlfcn, dlsym_with_dependencies) {
196 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700197 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700198 dlerror();
199 // This symbol is in DT_NEEDED library.
200 void* sym = dlsym(handle, "getRandomNumber");
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700201 ASSERT_TRUE(sym != nullptr) << dlerror();
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700202 int (*fn)(void);
203 fn = reinterpret_cast<int (*)(void)>(sym);
204 EXPECT_EQ(4, fn());
205 dlclose(handle);
206}
207
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700208TEST(dlfcn, dlopen_noload) {
209 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700210 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700211 handle = dlopen("libtest_simple.so", RTLD_NOW);
212 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700213 ASSERT_TRUE(handle != nullptr);
214 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700215 ASSERT_TRUE(handle == handle2);
216 ASSERT_EQ(0, dlclose(handle));
217 ASSERT_EQ(0, dlclose(handle2));
218}
219
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700220TEST(dlfcn, dlopen_by_soname) {
221 static const char* soname = "libdlext_test_soname.so";
222 static const char* filename = "libdlext_test_different_soname.so";
223 // 1. Make sure there is no library with soname in default search path
224 void* handle = dlopen(soname, RTLD_NOW);
225 ASSERT_TRUE(handle == nullptr);
226
227 // 2. Load a library using filename
228 handle = dlopen(filename, RTLD_NOW);
229 ASSERT_TRUE(handle != nullptr) << dlerror();
230
231 // 3. Find library by soname
232 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
233 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
234 ASSERT_EQ(handle, handle_soname);
235
236 // 4. RTLD_NOLOAD should still work with filename
237 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
238 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
239 ASSERT_EQ(handle, handle_filename);
240
241 dlclose(handle_filename);
242 dlclose(handle_soname);
243 dlclose(handle);
244}
245
dimitryc18de1b2017-09-26 14:31:35 +0200246TEST(dlfcn, dlopen_vdso) {
dimitryb9555a92017-10-11 18:04:05 +0200247#if __has_include(<sys/auxv.h>)
248 if (getauxval(AT_SYSINFO_EHDR) == 0) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800249 GTEST_SKIP() << "getauxval(AT_SYSINFO_EHDR) == 0, skipping this test";
dimitryb9555a92017-10-11 18:04:05 +0200250 }
251#endif
Elliott Hughesda1bb112018-02-16 10:05:08 -0800252
253 const char* vdso_name = "linux-vdso.so.1";
254#if defined(__i386__)
255 vdso_name = "linux-gate.so.1";
256#endif
257 void* handle = dlopen(vdso_name, RTLD_NOW);
dimitryc18de1b2017-09-26 14:31:35 +0200258 ASSERT_TRUE(handle != nullptr) << dlerror();
259 dlclose(handle);
260}
261
Elliott Hughes82c90722022-02-17 11:55:49 -0800262// HWASan uses an ifunc to describe the location of its shadow memory,
263// so even though it's an unusual case, Android needs to support
264// "ifunc variables".
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700265TEST(dlfcn, ifunc_variable) {
266 typedef const char* (*fn_ptr)();
267
268 // ifunc's choice depends on whether IFUNC_CHOICE has a value
269 // first check the set case
270 setenv("IFUNC_CHOICE", "set", 1);
271 // preload libtest_ifunc_variable_impl.so
272 void* handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
273 void* handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
274 ASSERT_TRUE(handle != nullptr) << dlerror();
275 const char** foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
276 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
277 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
278 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
279 ASSERT_EQ(strncmp("set", *foo_ptr, 3), 0);
280 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
281 dlclose(handle);
282 dlclose(handle_impl);
283
284 // then check the unset case
285 unsetenv("IFUNC_CHOICE");
286 handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
287 handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
288 ASSERT_TRUE(handle != nullptr) << dlerror();
289 foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
290 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
291 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
292 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
293 ASSERT_EQ(strncmp("unset", *foo_ptr, 5), 0);
294 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
295 dlclose(handle);
296 dlclose(handle_impl);
297}
298
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700299TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700300 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700301
302 // ifunc's choice depends on whether IFUNC_CHOICE has a value
303 // first check the set case
304 setenv("IFUNC_CHOICE", "set", 1);
305 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700306 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700307 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
308 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700309 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
310 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700311 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
312 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700313 dlclose(handle);
314
315 // then check the unset case
316 unsetenv("IFUNC_CHOICE");
317 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700318 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700319 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
320 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700321 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
322 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700323 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700324 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700325 dlclose(handle);
326}
327
328TEST(dlfcn, ifunc_ctor_call) {
329 typedef const char* (*fn_ptr)();
330
331 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700332 ASSERT_TRUE(handle != nullptr) << dlerror();
333 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
334 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
335 ASSERT_STREQ("false", is_ctor_called());
336
337 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
338 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700339 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700340 dlclose(handle);
341}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700342
343TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
344 typedef const char* (*fn_ptr)();
345
346 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
347 ASSERT_TRUE(handle != nullptr) << dlerror();
348 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
349 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
350 ASSERT_STREQ("false", is_ctor_called());
351
352 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
353 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
354 ASSERT_STREQ("true", is_ctor_called());
355 dlclose(handle);
356}
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700357
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700358TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
359 // This is the structure of the test library and
360 // its dt_needed libraries
361 // libtest_relo_check_dt_needed_order.so
362 // |
363 // +-> libtest_relo_check_dt_needed_order_1.so
364 // |
365 // +-> libtest_relo_check_dt_needed_order_2.so
366 //
367 // The root library references relo_test_get_answer_lib - which is defined
368 // in both dt_needed libraries, the correct relocation should
369 // use the function defined in libtest_relo_check_dt_needed_order_1.so
370 void* handle = nullptr;
Tom Cherryb8ab6182017-04-05 16:20:29 -0700371 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700372
373 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
374 ASSERT_TRUE(handle != nullptr) << dlerror();
375
376 typedef int (*fn_t) (void);
377 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
378 ASSERT_TRUE(fn != nullptr) << dlerror();
379 ASSERT_EQ(1, fn());
380}
381
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700382TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700383 // Here is how the test library and its dt_needed
384 // libraries are arranged
385 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700386 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700387 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700388 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700389 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700390 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700391 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700392 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700393 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700394 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700395 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700396 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700397 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700398 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700399 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700400 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700401 //
402 // load order should be (1, 2, 3, a, b, d)
403 //
404 // get_answer() is defined in (2, 3, a, b, c)
405 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700406 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700407 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700408 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
409 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700410 typedef int (*fn_t) (void);
411 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700412 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700413 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700414 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700415 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700416
417 ASSERT_EQ(42, fn());
418 ASSERT_EQ(43, fn2());
419 dlclose(handle);
420}
421
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700422TEST(dlfcn, dlopen_check_order_reloc_siblings) {
423 // This is how this one works:
424 // we lookup and call get_answer which is defined in '_2.so'
425 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
426 // the correct _impl() is implemented by '_a.so';
427 //
428 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
429 //
430 // Here is the picture:
431 //
432 // libtest_check_order_reloc_siblings.so
433 // |
434 // +-> ..._1.so <- empty
435 // | |
436 // | +-> ..._a.so <- exports correct answer_impl()
437 // | |
438 // | +-> ..._b.so <- every other letter exporting incorrect one.
439 // |
440 // +-> ..._2.so <- empty
441 // | |
442 // | +-> ..._c.so
443 // | |
444 // | +-> ..._d.so
445 // |
446 // +-> ..._3.so <- empty
447 // |
448 // +-> ..._e.so
449 // |
450 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
451 // implements incorrect get_answer_impl()
452
453 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
454 ASSERT_TRUE(handle == nullptr);
455#ifdef __BIONIC__
456 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
457 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
458#endif
459
460 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
461 ASSERT_TRUE(handle != nullptr) << dlerror();
462
463 typedef int (*fn_t) (void);
464 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
465 ASSERT_TRUE(fn != nullptr) << dlerror();
466 ASSERT_EQ(42, fn());
467
468 ASSERT_EQ(0, dlclose(handle));
469}
470
471TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
472 // This test uses the same library as dlopen_check_order_reloc_siblings.
473 // Unlike dlopen_check_order_reloc_siblings it preloads
474 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
475 // dlopen(libtest_check_order_reloc_siblings.so)
476
477 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
478 ASSERT_TRUE(handle == nullptr);
479 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
480 ASSERT_TRUE(handle == nullptr);
481
482 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
483 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
484
485 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
486 ASSERT_TRUE(handle != nullptr) << dlerror();
487
488 ASSERT_EQ(0, dlclose(handle_for_1));
489
490 typedef int (*fn_t) (void);
491 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
492 ASSERT_TRUE(fn != nullptr) << dlerror();
493 ASSERT_EQ(42, fn());
494
495 ASSERT_EQ(0, dlclose(handle));
496}
497
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800498TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
499 // This is how this one works:
500 // we lookup and call grandchild_get_answer which is defined in '_2.so'
501 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
502 // the correct _impl() is implemented by '_c_1.so';
503 //
504 // Here is the picture of subtree:
505 //
506 // libtest_check_order_reloc_siblings.so
507 // |
508 // +-> ..._2.so <- grandchild_get_answer()
509 // |
510 // +-> ..._c.so <- empty
511 // | |
512 // | +-> _c_1.so <- exports correct answer_impl()
513 // | |
514 // | +-> _c_2.so <- exports incorrect answer_impl()
515 // |
516 // +-> ..._d.so <- empty
517
518 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
519 ASSERT_TRUE(handle == nullptr);
520#ifdef __BIONIC__
521 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
522 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
523#endif
524
525 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
526 ASSERT_TRUE(handle != nullptr) << dlerror();
527
528 typedef int (*fn_t) (void);
529 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
530 ASSERT_TRUE(fn != nullptr) << dlerror();
531 ASSERT_EQ(42, fn());
532
533 ASSERT_EQ(0, dlclose(handle));
534}
535
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700536TEST(dlfcn, dlopen_check_order_reloc_nephew) {
537 // This is how this one works:
538 // we lookup and call nephew_get_answer which is defined in '_2.so'
539 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
540 // the correct _impl() is implemented by '_a.so';
541 //
542 // Here is the picture:
543 //
544 // libtest_check_order_reloc_siblings.so
545 // |
546 // +-> ..._1.so <- empty
547 // | |
548 // | +-> ..._a.so <- exports correct answer_impl()
549 // | |
550 // | +-> ..._b.so <- every other letter exporting incorrect one.
551 // |
552 // +-> ..._2.so <- empty
553 // | |
554 // | +-> ..._c.so
555 // | |
556 // | +-> ..._d.so
557 // |
558 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
559 // |
560 // +-> ..._e.so
561 // |
562 // +-> ..._f.so
563
564 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
565 ASSERT_TRUE(handle == nullptr);
566#ifdef __BIONIC__
567 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
568 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
569#endif
570
571 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
572 ASSERT_TRUE(handle != nullptr) << dlerror();
573
574 typedef int (*fn_t) (void);
575 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
576 ASSERT_TRUE(fn != nullptr) << dlerror();
577 ASSERT_EQ(42, fn());
578
579 ASSERT_EQ(0, dlclose(handle));
580}
581
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800582TEST(dlfcn, check_unload_after_reloc) {
583 // This is how this one works:
584 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
585 // |
586 // +-> libtest_two_parents_child
587 //
588 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
589 // |
590 // +-> libtest_two_parents_child
591 //
592 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
593 // as a second step it dlopens parent2 and dlcloses parent1...
594
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700595 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
596 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800597
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700598 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
599 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800600
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700601 typedef int (*fn_t) (void);
602 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
603 ASSERT_TRUE(fn != nullptr) << dlerror();
604 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800605
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700606 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800607
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700608 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
609 ASSERT_TRUE(handle != nullptr);
610 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800611
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700612 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
613 ASSERT_TRUE(fn != nullptr) << dlerror();
614 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800615
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700616 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800617
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700618 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
619 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800620}
621
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700622extern "C" int check_order_reloc_root_get_answer_impl() {
623 return 42;
624}
625
626TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
627 // This is how this one works:
628 // we lookup and call get_answer3 which is defined in 'root.so'
629 // and in turn calls external root_get_answer_impl() defined in _2.so and
630 // above the correct _impl() is one in the executable.
631 //
632 // libtest_check_order_reloc_root.so
633 // |
634 // +-> ..._1.so <- empty
635 // |
636 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
637 //
638
639 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
640 ASSERT_TRUE(handle == nullptr);
641#ifdef __BIONIC__
642 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
643 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
644#endif
645
646 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
647 ASSERT_TRUE(handle != nullptr) << dlerror();
648
649 typedef int (*fn_t) (void);
650 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
651 ASSERT_TRUE(fn != nullptr) << dlerror();
652 ASSERT_EQ(42, fn());
653
654 ASSERT_EQ(0, dlclose(handle));
655}
656
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700657TEST(dlfcn, dlopen_check_rtld_local) {
658 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
659 ASSERT_TRUE(sym == nullptr);
660
661 // implicit RTLD_LOCAL
662 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
663 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
664 ASSERT_TRUE(sym == nullptr);
665 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
666 sym = dlsym(handle, "dlopen_testlib_simple_func");
667 ASSERT_TRUE(sym != nullptr);
668 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
669 dlclose(handle);
670
671 // explicit RTLD_LOCAL
672 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
673 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
674 ASSERT_TRUE(sym == nullptr);
675 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
676 sym = dlsym(handle, "dlopen_testlib_simple_func");
677 ASSERT_TRUE(sym != nullptr);
678 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
679 dlclose(handle);
680}
681
682TEST(dlfcn, dlopen_check_rtld_global) {
683 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
684 ASSERT_TRUE(sym == nullptr);
685
686 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700687 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700688 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
689 ASSERT_TRUE(sym != nullptr) << dlerror();
690 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
691 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700692
693 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
694 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
695 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700696
697 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
698 // shared libraries after symbol was not found in the main executable
699 // and dependent libraries.
700 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
701 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
702 ASSERT_TRUE(sym != nullptr) << dlerror();
703
704 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700705}
706
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700707// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
708// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
709// libtest_with_dependency_loop_a.so
710TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700711 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
712 ASSERT_TRUE(handle != nullptr) << dlerror();
713 void* f = dlsym(handle, "dlopen_test_loopy_function");
714 ASSERT_TRUE(f != nullptr) << dlerror();
715 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
716 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700717
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700718 // dlopen second time to make sure that the library was unloaded correctly
719 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
720 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800721#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700722 ASSERT_STREQ("dlopen failed: library \"libtest_with_dependency_loop.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -0800723#else
724 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
725 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700726#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800727
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700728 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
729 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700730}
731
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700732TEST(dlfcn, dlopen_nodelete) {
733 static bool is_unloaded = false;
734
735 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
736 ASSERT_TRUE(handle != nullptr) << dlerror();
737 void (*set_unload_flag_ptr)(bool*);
738 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
739 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
740 set_unload_flag_ptr(&is_unloaded);
741
742 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
743 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
744 ASSERT_EQ(1729U, *taxicab_number);
745 *taxicab_number = 2;
746
747 dlclose(handle);
748 ASSERT_TRUE(!is_unloaded);
749
750 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
751 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
752 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
753
754
755 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
756 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
757 ASSERT_EQ(taxicab_number2, taxicab_number);
758
759 ASSERT_EQ(2U, *taxicab_number2);
760
761 dlclose(handle);
762 ASSERT_TRUE(!is_unloaded);
763}
764
765TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
766 static bool is_unloaded = false;
767
768 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
769 ASSERT_TRUE(handle != nullptr) << dlerror();
770 void (*set_unload_flag_ptr)(bool*);
771 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
772 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
773 set_unload_flag_ptr(&is_unloaded);
774
775 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
776 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
777
778 ASSERT_EQ(1729U, *taxicab_number);
779 *taxicab_number = 2;
780
781 // This RTLD_NODELETE should be ignored
782 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
783 ASSERT_TRUE(handle1 != nullptr) << dlerror();
784 ASSERT_EQ(handle, handle1);
785
786 dlclose(handle1);
787 dlclose(handle);
788
789 ASSERT_TRUE(is_unloaded);
790}
791
792TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
793 static bool is_unloaded = false;
794
795 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
796 ASSERT_TRUE(handle != nullptr) << dlerror();
797 void (*set_unload_flag_ptr)(bool*);
798 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
799 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
800 set_unload_flag_ptr(&is_unloaded);
801
802 dlclose(handle);
803 ASSERT_TRUE(!is_unloaded);
804}
805
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700806TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700807 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
808 ASSERT_TRUE(handle != nullptr) << dlerror();
809 int (*get_answer)();
810 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
811 ASSERT_TRUE(get_answer != nullptr) << dlerror();
812 ASSERT_EQ(42, get_answer());
813 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700814}
815
Elliott Hughese66190d2012-12-18 15:57:55 -0800816TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700817 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700818 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700819#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700820 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
821#else
822 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
823#endif
824}
825
dimitry109040c2017-11-03 13:49:07 +0100826TEST(dlfcn, dlclose_unload) {
827 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
828 ASSERT_TRUE(handle != nullptr) << dlerror();
829 uint32_t* taxicab_number = static_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
830 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
831 EXPECT_EQ(1729U, *taxicab_number);
832 dlclose(handle);
833 // Making sure that the library has been unmapped as part of library unload
834 // process. Note that mprotect somewhat counter-intuitively returns ENOMEM in
835 // this case.
836 uintptr_t page_start = reinterpret_cast<uintptr_t>(taxicab_number) & ~(PAGE_SIZE - 1);
837 ASSERT_TRUE(mprotect(reinterpret_cast<void*>(page_start), PAGE_SIZE, PROT_NONE) != 0);
838 ASSERT_EQ(ENOMEM, errno) << strerror(errno);
839}
840
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800841static void ConcurrentDlErrorFn(std::string& error) {
842 ASSERT_TRUE(dlerror() == nullptr);
843
844 void* handle = dlopen("/child/thread", RTLD_NOW);
845 ASSERT_TRUE(handle == nullptr);
846
847 const char* err = dlerror();
848 ASSERT_TRUE(err != nullptr);
849
850 error = err;
851}
852
853TEST(dlfcn, dlerror_concurrent_buffer) {
854 void* handle = dlopen("/main/thread", RTLD_NOW);
855 ASSERT_TRUE(handle == nullptr);
856 const char* main_thread_error = dlerror();
857 ASSERT_TRUE(main_thread_error != nullptr);
858 ASSERT_SUBSTR("/main/thread", main_thread_error);
859
860 std::string child_thread_error;
861 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
862 t.join();
863 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
864
865 // Check that main thread local buffer was not modified.
866 ASSERT_SUBSTR("/main/thread", main_thread_error);
Elliott Hughes5419b942012-10-16 15:54:46 -0700867}
868
Elliott Hughese66190d2012-12-18 15:57:55 -0800869TEST(dlfcn, dlerror_concurrent) {
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800870 void* handle = dlopen("/main/thread", RTLD_NOW);
871 ASSERT_TRUE(handle == nullptr);
872
873 std::string child_thread_error;
874 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
875 t.join();
876 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
877
Elliott Hughes5419b942012-10-16 15:54:46 -0700878 const char* main_thread_error = dlerror();
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800879 ASSERT_TRUE(main_thread_error != nullptr);
Elliott Hughes5419b942012-10-16 15:54:46 -0700880 ASSERT_SUBSTR("/main/thread", main_thread_error);
881}
882
Elliott Hughese66190d2012-12-18 15:57:55 -0800883TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700884 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700885 void* self = dlopen(nullptr, RTLD_NOW);
886 ASSERT_TRUE(self != nullptr);
887 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700888
889 void* sym;
890
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700891#if defined(__BIONIC__) && !defined(__LP64__)
zijunzhao447c3462023-03-01 01:32:08 +0000892#pragma clang diagnostic push
893#pragma clang diagnostic ignored "-Wnonnull"
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700894 // RTLD_DEFAULT in lp32 bionic is not (void*)0
895 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700896 sym = dlsym(nullptr, "test");
897 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800898 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
zijunzhao447c3462023-03-01 01:32:08 +0000899#pragma clang diagnostic pop
Elliott Hughes3b297c42012-10-11 16:08:51 -0700900#endif
901
Elliott Hughes3b297c42012-10-11 16:08:51 -0700902 // Symbol that doesn't exist.
903 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700904 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700905 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700906
907 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700908}
909
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700910TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700911 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700912 void* self = dlopen(nullptr, RTLD_NOW);
913 ASSERT_TRUE(self != nullptr);
914 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700915
916 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700917 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700918
919 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
920 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
921
922 Dl_info info;
923 int rc = dladdr(addr, &info);
924 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
925
926 // Get the name of this executable.
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700927 const std::string executable_path = android::base::GetExecutablePath();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700928
929 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700930 char dli_realpath[PATH_MAX];
931 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700932 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700933
934 // The symbol name should be the symbol we looked up.
935 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
936
937 // The address should be the exact address of the symbol.
938 ASSERT_EQ(info.dli_saddr, sym);
939
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700940 std::vector<map_record> maps;
941 ASSERT_TRUE(Maps::parse_maps(&maps));
942
943 void* base_address = nullptr;
944 for (const map_record& rec : maps) {
945 if (executable_path == rec.pathname) {
946 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700947 break;
948 }
949 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700950
951 // The base address should be the address we were loaded at.
952 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700953
954 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700955}
956
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700957TEST(dlfcn, dlopen_executable_by_absolute_path) {
958 void* handle1 = dlopen(nullptr, RTLD_NOW);
959 ASSERT_TRUE(handle1 != nullptr) << dlerror();
960
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700961 void* handle2 = dlopen(android::base::GetExecutablePath().c_str(), RTLD_NOW);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700962 ASSERT_TRUE(handle2 != nullptr) << dlerror();
963
964#if defined(__BIONIC__)
965 ASSERT_EQ(handle1, handle2);
966#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800967 GTEST_SKIP() << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
968 "it loads a separate copy of the main executable "
969 "on dlopen by absolute path";
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700970#endif
971}
972
Elliott Hughes3f73ea62022-10-19 16:16:54 +0000973#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib64/" ABI_STRING "/"
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200974#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Peter Collingbourneea11be02019-04-30 16:05:13 -0700975#define PATH_TO_BOOTSTRAP_LIBC PATH_TO_SYSTEM_LIB "bootstrap/libc.so"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100976#define ALTERNATE_PATH_TO_LIBC ALTERNATE_PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700977
978TEST(dlfcn, dladdr_libc) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800979#if defined(__GLIBC__)
980 GTEST_SKIP() << "glibc returns libc.so's ldconfig path, which is a symlink (not a realpath)";
981#endif
982
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700983 Dl_info info;
984 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
985 ASSERT_TRUE(dladdr(addr, &info) != 0);
986
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700987 char libc_realpath[PATH_MAX];
Victor Khimenko14b9d712017-01-25 19:59:16 +0100988
989 // Check if libc is in canonical path or in alternate path.
990 if (strncmp(ALTERNATE_PATH_TO_SYSTEM_LIB,
991 info.dli_fname,
992 sizeof(ALTERNATE_PATH_TO_SYSTEM_LIB) - 1) == 0) {
993 // Platform with emulated architecture. Symlink on ARC++.
994 ASSERT_TRUE(realpath(ALTERNATE_PATH_TO_LIBC, libc_realpath) == libc_realpath);
Peter Collingbourneea11be02019-04-30 16:05:13 -0700995 } else if (strncmp(PATH_TO_BOOTSTRAP_LIBC, info.dli_fname,
996 sizeof(PATH_TO_BOOTSTRAP_LIBC) - 1) == 0) {
997 ASSERT_TRUE(realpath(PATH_TO_BOOTSTRAP_LIBC, libc_realpath) == libc_realpath);
Victor Khimenko14b9d712017-01-25 19:59:16 +0100998 } else {
999 // /system/lib is symlink when this test is executed on host.
1000 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
1001 }
Dmitriy Ivanovef255922015-04-08 11:53:08 -07001002
1003 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001004 // TODO: add check for dfi_fbase
1005 ASSERT_STREQ("puts", info.dli_sname);
1006 ASSERT_EQ(addr, info.dli_saddr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001007}
1008
Elliott Hughese66190d2012-12-18 15:57:55 -08001009TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -07001010 Dl_info info;
1011
Elliott Hughes3b297c42012-10-11 16:08:51 -07001012 dlerror(); // Clear any pending errors.
1013
zijunzhao447c3462023-03-01 01:32:08 +00001014#pragma clang diagnostic push
1015#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughes8e15b082012-09-26 11:44:01 -07001016 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001017 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
1018 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
zijunzhao447c3462023-03-01 01:32:08 +00001019#pragma clang diagnostic pop
Elliott Hughes8e15b082012-09-26 11:44:01 -07001020
1021 // No symbol corresponding to a stack address.
1022 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001023 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001024}
Elliott Hughes124fae92012-10-31 14:20:03 -07001025
Elliott Hughese66190d2012-12-18 15:57:55 -08001026TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001027 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001028 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
1029 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001030 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001031 void* sym = dlsym(handle, "getRandomNumber");
1032 ASSERT_TRUE(sym != nullptr) << dlerror();
1033 int (*fn)(void);
1034 fn = reinterpret_cast<int (*)(void)>(sym);
1035 EXPECT_EQ(4, fn());
1036
1037 Dl_info dlinfo;
1038 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1039
1040 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1041 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001042 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Elliott Hughes124fae92012-10-31 14:20:03 -07001043}
Elliott Hughese66190d2012-12-18 15:57:55 -08001044
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001045TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
1046 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
1047 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001048 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001049 void* sym = dlsym(handle, "getRandomNumber");
1050 ASSERT_TRUE(sym != nullptr) << dlerror();
1051 int (*fn)(void);
1052 fn = reinterpret_cast<int (*)(void)>(sym);
1053 EXPECT_EQ(4, fn());
1054
1055 Dl_info dlinfo;
1056 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1057
1058 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1059 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
1060 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
1061}
1062
Elliott Hughese66190d2012-12-18 15:57:55 -08001063TEST(dlfcn, dlopen_bad_flags) {
1064 dlerror(); // Clear any pending errors.
1065 void* handle;
1066
Elliott Hughes063525c2014-05-13 11:19:57 -07001067#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -08001068 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001069 handle = dlopen(nullptr, 0);
1070 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001071 ASSERT_SUBSTR("invalid", dlerror());
1072#endif
1073
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001074 handle = dlopen(nullptr, 0xffffffff);
1075 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001076 ASSERT_SUBSTR("invalid", dlerror());
1077
1078 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001079 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
1080 ASSERT_TRUE(handle != nullptr);
1081 ASSERT_SUBSTR(nullptr, dlerror());
Elliott Hughese66190d2012-12-18 15:57:55 -08001082}
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001083
1084TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001085 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001086 ASSERT_TRUE(addr == nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001087}
1088
1089TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001090 void* addr = dlsym(RTLD_DEFAULT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001091 ASSERT_TRUE(addr != nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001092}
1093
1094TEST(dlfcn, rtld_next_unknown_symbol) {
1095 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001096 ASSERT_TRUE(addr == nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001097}
1098
1099TEST(dlfcn, rtld_next_known_symbol) {
1100 void* addr = dlsym(RTLD_NEXT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001101 ASSERT_TRUE(addr != nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001102}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001103
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001104// Check that RTLD_NEXT of a libc symbol works in dlopened library
1105TEST(dlfcn, rtld_next_from_library) {
dimitry153168c2018-02-20 16:51:41 +01001106 void* library_with_fclose = dlopen("libtest_check_rtld_next_from_library.so", RTLD_NOW | RTLD_GLOBAL);
Raj Mamadgi527229c2017-11-02 15:53:50 -07001107 ASSERT_TRUE(library_with_fclose != nullptr) << dlerror();
1108 void* expected_addr = dlsym(RTLD_DEFAULT, "fclose");
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001109 ASSERT_TRUE(expected_addr != nullptr) << dlerror();
Raj Mamadgi527229c2017-11-02 15:53:50 -07001110 typedef void* (*get_libc_fclose_ptr_fn_t)();
1111 get_libc_fclose_ptr_fn_t get_libc_fclose_ptr =
1112 reinterpret_cast<get_libc_fclose_ptr_fn_t>(dlsym(library_with_fclose, "get_libc_fclose_ptr"));
1113 ASSERT_TRUE(get_libc_fclose_ptr != nullptr) << dlerror();
1114 ASSERT_EQ(expected_addr, get_libc_fclose_ptr());
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001115
Raj Mamadgi527229c2017-11-02 15:53:50 -07001116 dlclose(library_with_fclose);
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001117}
1118
1119
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001120TEST(dlfcn, dlsym_weak_func) {
1121 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001122 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001123 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001124
1125 int (*weak_func)();
1126 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001127 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001128 EXPECT_EQ(42, weak_func());
1129 dlclose(handle);
1130}
1131
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001132TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001133 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
1134 ASSERT_TRUE(handle != nullptr) << dlerror();
1135 int (*weak_func)();
1136 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
1137 ASSERT_TRUE(weak_func != nullptr) << dlerror();
1138 EXPECT_EQ(6551, weak_func());
1139 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001140}
1141
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001142TEST(dlfcn, dlopen_symlink) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001143 DlfcnSymlink symlink("dlopen_symlink");
Colin Cross7da20342021-07-28 11:18:11 -07001144 const std::string symlink_name = android::base::Basename(symlink.get_symlink_path());
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001145 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001146 void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001147 ASSERT_TRUE(handle1 != nullptr);
1148 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001149 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -07001150 dlclose(handle1);
1151 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001152}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001153
1154// libtest_dlopen_from_ctor_main.so depends on
1155// libtest_dlopen_from_ctor.so which has a constructor
1156// that calls dlopen(libc...). This is to test the situation
1157// described in b/7941716.
1158TEST(dlfcn, dlopen_dlopen_from_ctor) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001159#if defined(__GLIBC__)
1160 GTEST_SKIP() << "glibc segfaults if you try to call dlopen from a constructor";
1161#endif
1162
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001163 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1164 ASSERT_TRUE(handle != nullptr) << dlerror();
1165 dlclose(handle);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001166}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001167
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001168static std::string g_fini_call_order_str;
1169
1170static void register_fini_call(const char* s) {
1171 g_fini_call_order_str += s;
1172}
1173
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001174static void test_init_fini_call_order_for(const char* libname) {
1175 g_fini_call_order_str.clear();
1176 void* handle = dlopen(libname, RTLD_NOW);
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001177 ASSERT_TRUE(handle != nullptr) << dlerror();
1178 typedef int (*get_init_order_number_t)();
1179 get_init_order_number_t get_init_order_number =
1180 reinterpret_cast<get_init_order_number_t>(dlsym(handle, "get_init_order_number"));
1181 ASSERT_EQ(321, get_init_order_number());
1182
1183 typedef void (*set_fini_callback_t)(void (*f)(const char*));
1184 set_fini_callback_t set_fini_callback =
1185 reinterpret_cast<set_fini_callback_t>(dlsym(handle, "set_fini_callback"));
1186 set_fini_callback(register_fini_call);
1187 dlclose(handle);
1188 ASSERT_EQ("(root)(child)(grandchild)", g_fini_call_order_str);
1189}
1190
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001191TEST(dlfcn, init_fini_call_order) {
1192 test_init_fini_call_order_for("libtest_init_fini_order_root.so");
1193 test_init_fini_call_order_for("libtest_init_fini_order_root2.so");
1194}
1195
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001196TEST(dlfcn, symbol_versioning_use_v1) {
1197 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1198 ASSERT_TRUE(handle != nullptr) << dlerror();
1199 typedef int (*fn_t)();
1200 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1201 ASSERT_TRUE(fn != nullptr) << dlerror();
1202 ASSERT_EQ(1, fn());
1203 dlclose(handle);
1204}
1205
1206TEST(dlfcn, symbol_versioning_use_v2) {
1207 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1208 ASSERT_TRUE(handle != nullptr) << dlerror();
1209 typedef int (*fn_t)();
1210 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1211 ASSERT_TRUE(fn != nullptr) << dlerror();
1212 ASSERT_EQ(2, fn());
1213 dlclose(handle);
1214}
1215
1216TEST(dlfcn, symbol_versioning_use_other_v2) {
1217 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1218 ASSERT_TRUE(handle != nullptr) << dlerror();
1219 typedef int (*fn_t)();
1220 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1221 ASSERT_TRUE(fn != nullptr) << dlerror();
1222 ASSERT_EQ(20, fn());
1223 dlclose(handle);
1224}
1225
1226TEST(dlfcn, symbol_versioning_use_other_v3) {
1227 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1228 ASSERT_TRUE(handle != nullptr) << dlerror();
1229 typedef int (*fn_t)();
1230 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1231 ASSERT_TRUE(fn != nullptr) << dlerror();
1232 ASSERT_EQ(3, fn());
1233 dlclose(handle);
1234}
1235
1236TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1237 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1238 ASSERT_TRUE(handle != nullptr) << dlerror();
1239 typedef int (*fn_t)();
1240 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1241 ASSERT_TRUE(fn != nullptr) << dlerror();
1242 ASSERT_EQ(3, fn()); // the default version is 3
1243 dlclose(handle);
1244}
1245
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001246TEST(dlfcn, dlvsym_smoke) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001247#if !defined(ANDROID_HOST_MUSL)
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001248 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1249 ASSERT_TRUE(handle != nullptr) << dlerror();
1250 typedef int (*fn_t)();
1251
1252 {
1253 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1254 ASSERT_TRUE(fn == nullptr);
1255 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1256 }
1257
1258 {
1259 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1260 ASSERT_TRUE(fn != nullptr) << dlerror();
1261 ASSERT_EQ(2, fn());
1262 }
1263
1264 dlclose(handle);
Colin Cross7da20342021-07-28 11:18:11 -07001265#else
1266 GTEST_SKIP() << "musl doesn't have dlvsym";
1267#endif
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001268}
1269
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001270// This preempts the implementation from libtest_versioned_lib.so
1271extern "C" int version_zero_function() {
1272 return 0;
1273}
1274
1275// This preempts the implementation from libtest_versioned_uselibv*.so
1276extern "C" int version_zero_function2() {
1277 return 0;
1278}
Evgenii Stepanov68650822015-06-10 13:38:39 -07001279
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001280TEST(dlfcn, dt_runpath_smoke) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001281 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1282 ASSERT_TRUE(handle != nullptr) << dlerror();
1283
1284 typedef void *(* dlopen_b_fn)();
1285 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1286 ASSERT_TRUE(fn != nullptr) << dlerror();
1287
1288 void *p = fn();
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001289 ASSERT_TRUE(p != nullptr);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001290
1291 dlclose(handle);
1292}
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001293
Dimitry Ivanovdb6ab3d2017-06-27 11:02:51 -07001294TEST(dlfcn, dt_runpath_absolute_path) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001295 std::string libpath = GetTestlibRoot() + "/libtest_dt_runpath_d.so";
Dimitry Ivanovdb6ab3d2017-06-27 11:02:51 -07001296 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1297 ASSERT_TRUE(handle != nullptr) << dlerror();
1298
1299 typedef void *(* dlopen_b_fn)();
1300 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1301 ASSERT_TRUE(fn != nullptr) << dlerror();
1302
1303 void *p = fn();
1304 ASSERT_TRUE(p != nullptr);
1305
1306 dlclose(handle);
1307}
1308
dimitry55547db2018-05-25 14:17:37 +02001309static void test_dlclose_after_thread_local_dtor(const char* library_name) {
dimitry06016f22018-01-05 11:39:28 +01001310 bool is_dtor_triggered = false;
1311
1312 auto f = [](void* handle, bool* is_dtor_triggered) {
1313 typedef void (*fn_t)(bool*);
1314 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1315 ASSERT_TRUE(fn != nullptr) << dlerror();
1316
1317 fn(is_dtor_triggered);
1318
1319 ASSERT_TRUE(!*is_dtor_triggered);
1320 };
1321
dimitry55547db2018-05-25 14:17:37 +02001322 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001323 ASSERT_TRUE(handle == nullptr);
1324
dimitry55547db2018-05-25 14:17:37 +02001325 handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001326 ASSERT_TRUE(handle != nullptr) << dlerror();
1327
1328 std::thread t(f, handle, &is_dtor_triggered);
1329 t.join();
1330
1331 ASSERT_TRUE(is_dtor_triggered);
1332 dlclose(handle);
1333
dimitry55547db2018-05-25 14:17:37 +02001334 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001335 ASSERT_TRUE(handle == nullptr);
1336}
1337
dimitry55547db2018-05-25 14:17:37 +02001338TEST(dlfcn, dlclose_after_thread_local_dtor) {
1339 test_dlclose_after_thread_local_dtor("libtest_thread_local_dtor.so");
1340}
1341
1342TEST(dlfcn, dlclose_after_thread_local_dtor_indirect) {
1343 test_dlclose_after_thread_local_dtor("libtest_indirect_thread_local_dtor.so");
1344}
1345
1346static void test_dlclose_before_thread_local_dtor(const char* library_name) {
dimitry06016f22018-01-05 11:39:28 +01001347 bool is_dtor_triggered = false;
1348
dimitry55547db2018-05-25 14:17:37 +02001349 auto f = [library_name](bool* is_dtor_triggered) {
1350 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001351 ASSERT_TRUE(handle == nullptr);
1352
dimitry55547db2018-05-25 14:17:37 +02001353 handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001354 ASSERT_TRUE(handle != nullptr) << dlerror();
1355
1356 typedef void (*fn_t)(bool*);
1357 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1358 ASSERT_TRUE(fn != nullptr) << dlerror();
1359
1360 fn(is_dtor_triggered);
1361
1362 dlclose(handle);
1363
1364 ASSERT_TRUE(!*is_dtor_triggered);
1365
1366 // Since we have thread_atexit dtors associated with handle - the library should
1367 // still be availabe.
dimitry55547db2018-05-25 14:17:37 +02001368 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001369 ASSERT_TRUE(handle != nullptr) << dlerror();
1370 dlclose(handle);
1371 };
1372
dimitry55547db2018-05-25 14:17:37 +02001373 void* handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001374 ASSERT_TRUE(handle != nullptr) << dlerror();
1375 dlclose(handle);
1376
dimitry55547db2018-05-25 14:17:37 +02001377 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001378 ASSERT_TRUE(handle == nullptr);
1379
1380 std::thread t(f, &is_dtor_triggered);
1381 t.join();
1382#if defined(__BIONIC__)
1383 // ld-android.so unloads unreferenced libraries on pthread_exit()
1384 ASSERT_TRUE(is_dtor_triggered);
dimitry55547db2018-05-25 14:17:37 +02001385 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001386 ASSERT_TRUE(handle == nullptr);
1387#else
1388 // GLIBC does not unload libraries with ref_count = 0 on pthread_exit
1389 ASSERT_TRUE(is_dtor_triggered);
dimitry55547db2018-05-25 14:17:37 +02001390 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1391 ASSERT_TRUE(handle != nullptr) << dlerror();
1392#endif
1393}
1394
1395TEST(dlfcn, dlclose_before_thread_local_dtor) {
1396 test_dlclose_before_thread_local_dtor("libtest_thread_local_dtor.so");
1397}
1398
1399TEST(dlfcn, dlclose_before_thread_local_dtor_indirect) {
1400 test_dlclose_before_thread_local_dtor("libtest_indirect_thread_local_dtor.so");
1401}
1402
1403TEST(dlfcn, dlclose_before_thread_local_dtor_multiple_dsos) {
1404 const constexpr char* library_name = "libtest_indirect_thread_local_dtor.so";
1405
1406 bool is_dtor1_triggered = false;
1407 bool is_dtor2_triggered = false;
1408
1409 std::mutex mtx;
1410 std::condition_variable cv;
1411 void* library_handle = nullptr;
1412 bool thread1_dlopen_complete = false;
1413 bool thread2_thread_local_dtor_initialized = false;
1414 bool thread1_complete = false;
1415
1416 auto f1 = [&]() {
1417 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1418 ASSERT_TRUE(handle == nullptr);
1419
1420 handle = dlopen(library_name, RTLD_NOW);
1421 ASSERT_TRUE(handle != nullptr) << dlerror();
1422 std::unique_lock<std::mutex> lock(mtx);
1423 thread1_dlopen_complete = true;
1424 library_handle = handle;
1425 lock.unlock();
1426 cv.notify_one();
1427
1428 typedef void (*fn_t)(bool*);
1429 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1430 ASSERT_TRUE(fn != nullptr) << dlerror();
1431
1432 fn(&is_dtor1_triggered);
1433
1434 lock.lock();
1435 cv.wait(lock, [&] { return thread2_thread_local_dtor_initialized; });
1436 lock.unlock();
1437
1438 dlclose(handle);
1439
1440 ASSERT_TRUE(!is_dtor1_triggered);
1441
1442 // Since we have thread_atexit dtors associated with handle - the library should
1443 // still be availabe.
1444 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1445 ASSERT_TRUE(handle != nullptr) << dlerror();
1446 dlclose(handle);
1447 };
1448
1449 auto f2 = [&]() {
1450 std::unique_lock<std::mutex> lock(mtx);
1451 cv.wait(lock, [&] { return thread1_dlopen_complete; });
1452 void* handle = library_handle;
1453 lock.unlock();
1454
1455 typedef void (*fn_t)(bool*);
1456 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable2"));
1457 ASSERT_TRUE(fn != nullptr) << dlerror();
1458
1459 fn(&is_dtor2_triggered);
1460
1461 lock.lock();
1462 thread2_thread_local_dtor_initialized = true;
1463 lock.unlock();
1464 cv.notify_one();
1465
1466 lock.lock();
1467 cv.wait(lock, [&] { return thread1_complete; });
1468 lock.unlock();
1469
1470 ASSERT_TRUE(!is_dtor2_triggered);
1471 };
1472
1473 void* handle = dlopen(library_name, RTLD_NOW);
1474 ASSERT_TRUE(handle != nullptr) << dlerror();
1475 dlclose(handle);
1476
1477 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1478 ASSERT_TRUE(handle == nullptr);
1479
1480 std::thread t1(f1);
1481 std::thread t2(f2);
1482 t1.join();
1483 ASSERT_TRUE(is_dtor1_triggered);
1484 ASSERT_TRUE(!is_dtor2_triggered);
1485
1486 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1487 ASSERT_TRUE(handle != nullptr) << dlerror();
1488 dlclose(handle);
1489
1490 std::unique_lock<std::mutex> lock(mtx);
1491 thread1_complete = true;
1492 lock.unlock();
1493 cv.notify_one();
1494
1495 t2.join();
1496 ASSERT_TRUE(is_dtor2_triggered);
1497
1498#if defined(__BIONIC__)
1499 // ld-android.so unloads unreferenced libraries on pthread_exit()
1500 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1501 ASSERT_TRUE(handle == nullptr);
1502#else
1503 // GLIBC does not unload libraries with ref_count = 0 on pthread_exit
1504 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001505 ASSERT_TRUE(handle != nullptr) << dlerror();
1506#endif
1507}
1508
Elliott Hughes8ad40932017-06-15 15:12:29 -07001509TEST(dlfcn, RTLD_macros) {
1510#if !defined(RTLD_LOCAL)
1511#error no RTLD_LOCAL
1512#elif !defined(RTLD_LAZY)
1513#error no RTLD_LAZY
1514#elif !defined(RTLD_NOW)
1515#error no RTLD_NOW
1516#elif !defined(RTLD_NOLOAD)
1517#error no RTLD_NOLOAD
1518#elif !defined(RTLD_GLOBAL)
1519#error no RTLD_GLOBAL
1520#elif !defined(RTLD_NODELETE)
1521#error no RTLD_NODELETE
1522#endif
1523}
1524
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001525// Bionic specific tests
1526#if defined(__BIONIC__)
1527
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001528#if defined(__arm__)
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001529
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001530void validate_compatibility_of_native_library(const std::string& soname, const std::string& path) {
1531 // Grab the dynamic section in text form...
1532 ExecTestHelper eth;
1533 eth.SetArgs({"readelf", "-dW", path.c_str(), nullptr});
1534 eth.Run([&]() { execvpe("readelf", eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
1535 std::string output = eth.GetOutput();
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001536
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001537 // Check that there *is* a legacy DT_HASH (not just a GNU hash)...
1538 ASSERT_TRUE(std::regex_search(output, std::regex("\\(HASH\\)"))) << output;
1539 // Check that there is no DT_ANDROID_REL or DT_ANDROID_RELA...
1540 ASSERT_FALSE(std::regex_search(output, std::regex("\\(ANDROID_REL\\)"))) << output;
1541 ASSERT_FALSE(std::regex_search(output, std::regex("\\(ANDROID_RELA\\)"))) << output;
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001542
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001543 // Check that we have regular non-packed relocations.
1544 // libdl.so is simple enough that it doesn't have any relocations.
1545 ASSERT_TRUE(std::regex_search(output, std::regex("\\(RELA?\\)")) || soname == "libdl.so")
1546 << output;
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001547}
1548
Ryan Prichard470b6662018-03-27 22:10:55 -07001549void validate_compatibility_of_native_library(const std::string& soname) {
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001550 // On the systems with emulation system libraries would be of different
1551 // architecture. Try to use alternate paths first.
1552 std::string path = std::string(ALTERNATE_PATH_TO_SYSTEM_LIB) + soname;
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001553 if (access(path.c_str(), R_OK) != 0) {
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001554 path = std::string(PATH_TO_SYSTEM_LIB) + soname;
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001555 ASSERT_EQ(0, access(path.c_str(), R_OK));
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001556 }
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001557 validate_compatibility_of_native_library(soname, path);
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001558}
1559
1560// This is a test for app compatibility workaround for arm apps
1561// affected by http://b/24465209
1562TEST(dlext, compat_elf_hash_and_relocation_tables) {
1563 validate_compatibility_of_native_library("libc.so");
1564 validate_compatibility_of_native_library("liblog.so");
1565 validate_compatibility_of_native_library("libstdc++.so");
1566 validate_compatibility_of_native_library("libdl.so");
1567 validate_compatibility_of_native_library("libm.so");
1568 validate_compatibility_of_native_library("libz.so");
1569 validate_compatibility_of_native_library("libjnigraphics.so");
1570}
1571
1572#endif // defined(__arm__)
1573
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001574TEST(dlfcn, dlopen_invalid_rw_load_segment) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001575 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-rw_load_segment.so";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001576 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1577 ASSERT_TRUE(handle == nullptr);
Elliott Hughes9076b0c2018-02-28 11:29:45 -08001578 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W+E load segments are not allowed";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001579 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1580}
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001581
1582TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001583 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-unaligned_shdr_offset.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001584
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001585 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1586 ASSERT_TRUE(handle == nullptr);
1587 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
1588 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1589}
1590
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001591TEST(dlfcn, dlopen_invalid_zero_shentsize) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001592 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-zero_shentsize.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001593
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001594 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1595 ASSERT_TRUE(handle == nullptr);
1596 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x";
1597 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1598}
1599
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001600TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001601 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-zero_shstrndx.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001602
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001603 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1604 ASSERT_TRUE(handle == nullptr);
1605 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx";
1606 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1607}
1608
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001609TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001610 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-empty_shdr_table.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001611
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001612 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1613 ASSERT_TRUE(handle == nullptr);
1614 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers";
1615 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1616}
1617
Dimitry Ivanov46230442016-08-15 13:40:53 -07001618TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001619 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-zero_shdr_table_offset.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001620
Dimitry Ivanov46230442016-08-15 13:40:53 -07001621 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1622 ASSERT_TRUE(handle == nullptr);
1623 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/";
1624 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1625}
1626
Dimitry Ivanov55958342016-08-15 14:06:04 -07001627TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001628 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-zero_shdr_table_content.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001629
Dimitry Ivanov55958342016-08-15 14:06:04 -07001630 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1631 ASSERT_TRUE(handle == nullptr);
1632 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
1633 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1634}
1635
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001636TEST(dlfcn, dlopen_invalid_textrels) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001637 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-textrels.so";
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001638
1639 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1640 ASSERT_TRUE(handle == nullptr);
1641 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1642 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1643}
1644
1645TEST(dlfcn, dlopen_invalid_textrels2) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001646 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-textrels2.so";
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001647
1648 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1649 ASSERT_TRUE(handle == nullptr);
1650 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1651 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1652}
1653
Ryan Prichard8ea6af52022-03-24 21:14:27 -07001654TEST(dlfcn, dlopen_invalid_local_tls) {
Elliott Hughesec9f0232022-11-16 00:23:21 +00001655#if defined(__riscv)
1656 // This is a test for bad gold behavior, and gold doesn't support riscv64.
1657#else
Ryan Prichard8ea6af52022-03-24 21:14:27 -07001658 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-local-tls.so";
1659
1660 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1661 ASSERT_TRUE(handle == nullptr);
1662#if defined(__arm__)
1663 const char* referent = "local section";
1664#else
1665 const char* referent = "local symbol \"tls_var_2\"";
1666#endif
1667 std::string expected_dlerror = std::string("dlopen failed: unexpected TLS reference to ") +
1668 referent + " in \"" + libpath + "\"";
1669 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
Elliott Hughesec9f0232022-11-16 00:23:21 +00001670#endif
Ryan Prichard8ea6af52022-03-24 21:14:27 -07001671}
1672
Jiyong Park01162f22017-10-16 15:31:09 +09001673TEST(dlfcn, dlopen_df_1_global) {
1674 void* handle = dlopen("libtest_dlopen_df_1_global.so", RTLD_NOW);
1675 ASSERT_TRUE(handle != nullptr) << dlerror();
1676}
1677
Peter Collingbourneb39cb3c2019-03-01 13:12:49 -08001678TEST(dlfcn, segment_gap) {
1679 void* handle = dlopen("libsegment_gap_outer.so", RTLD_NOW);
1680 ASSERT_TRUE(handle != nullptr) << dlerror();
1681
1682 auto get_inner = reinterpret_cast<void* (*)()>(dlsym(handle, "get_inner"));
1683 void* inner = get_inner();
1684 (void)inner;
1685
1686#if __arm__
1687 int count;
1688 _Unwind_Ptr outer_exidx = dl_unwind_find_exidx(reinterpret_cast<_Unwind_Ptr>(get_inner), &count);
1689 _Unwind_Ptr inner_exidx = dl_unwind_find_exidx(reinterpret_cast<_Unwind_Ptr>(inner), &count);
1690 EXPECT_NE(0u, outer_exidx);
1691 EXPECT_NE(0u, inner_exidx);
1692 EXPECT_NE(inner_exidx, outer_exidx);
1693#endif
1694
1695 Dl_info info;
1696 int rc = dladdr(inner, &info);
1697 ASSERT_NE(rc, 0);
1698
1699 EXPECT_NE(nullptr, strstr(info.dli_fname, "libsegment_gap_inner.so"));
1700}
1701
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001702#endif