blob: 0bf877507db027bfed16ac7e2f31dfcf81e08517 [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>
Tom Cherryb8ab6182017-04-05 16:20:29 -070036#include <android-base/scopeguard.h>
37
Dimitry Ivanov927877c2016-09-21 11:17:13 -070038#include "gtest_globals.h"
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -070039#include "gtest_utils.h"
Dimitry Ivanov708589f2016-09-19 10:50:28 -070040#include "dlfcn_symlink_support.h"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070041#include "utils.h"
42
Elliott Hughes3b297c42012-10-11 16:08:51 -070043#define ASSERT_SUBSTR(needle, haystack) \
44 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
45
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070046
Elliott Hughes1728b232014-05-14 10:02:03 -070047static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070048extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070049 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070050}
51
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070052static int g_ctor_function_called = 0;
Dimitry Ivanov55437462016-07-20 15:33:07 -070053static int g_ctor_argc = 0;
54static char** g_ctor_argv = reinterpret_cast<char**>(0xDEADBEEF);
55static char** g_ctor_envp = g_ctor_envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070056
Dimitry Ivanov55437462016-07-20 15:33:07 -070057extern "C" void ctor_function(int argc, char** argv, char** envp) __attribute__ ((constructor));
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070058
Dimitry Ivanov55437462016-07-20 15:33:07 -070059extern "C" void ctor_function(int argc, char** argv, char** envp) {
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070060 g_ctor_function_called = 17;
Dimitry Ivanov55437462016-07-20 15:33:07 -070061 g_ctor_argc = argc;
62 g_ctor_argv = argv;
63 g_ctor_envp = envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070064}
65
66TEST(dlfcn, ctor_function_call) {
67 ASSERT_EQ(17, g_ctor_function_called);
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -070068 ASSERT_TRUE(g_ctor_argc = GetArgc());
69 ASSERT_TRUE(g_ctor_argv = GetArgv());
70 ASSERT_TRUE(g_ctor_envp = GetEnvp());
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070071}
72
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070073TEST(dlfcn, dlsym_in_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070074 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070075 void* self = dlopen(nullptr, RTLD_NOW);
76 ASSERT_TRUE(self != nullptr);
77 ASSERT_TRUE(dlerror() == nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070078
79 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070080 ASSERT_TRUE(sym != nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070081
82 void (*function)() = reinterpret_cast<void(*)()>(sym);
83
Elliott Hughes1728b232014-05-14 10:02:03 -070084 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070085 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070086 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070087
88 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070089}
Elliott Hughes8e15b082012-09-26 11:44:01 -070090
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070091TEST(dlfcn, dlsym_from_sofile) {
92 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL);
93 ASSERT_TRUE(handle != nullptr) << dlerror();
94
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070095 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070096 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
97 ASSERT_TRUE(symbol == nullptr);
98 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
99
100 typedef int* (*fn_t)();
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700101 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
102 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
103 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700104
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700105 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700106 ASSERT_TRUE(ptr != nullptr) << dlerror();
107 ASSERT_EQ(42, *ptr);
108
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700109 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
110 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
111 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
112
113 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
114 ASSERT_TRUE(ptr != nullptr) << dlerror();
115 ASSERT_EQ(44, *ptr);
116
117 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
118 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
119 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
120
121 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
122 ASSERT_TRUE(ptr != nullptr) << dlerror();
123 ASSERT_EQ(43, *ptr);
124
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700125 dlclose(handle);
126}
127
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700128TEST(dlfcn, dlsym_from_sofile_with_preload) {
129 void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL);
130 ASSERT_TRUE(preload != nullptr) << dlerror();
131
132 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
133 ASSERT_TRUE(handle != nullptr) << dlerror();
134
135 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
136 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
137 ASSERT_TRUE(symbol == nullptr);
138 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
139
140 typedef int* (*fn_t)();
141 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
142 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
143 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
144
145 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
146 ASSERT_TRUE(ptr != nullptr) << dlerror();
147 ASSERT_EQ(42, *ptr);
148
149 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
150 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
151 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
152
153 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
154 ASSERT_TRUE(ptr != nullptr) << dlerror();
155 ASSERT_EQ(44, *ptr);
156
157 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
158 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
159 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
160
161 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
162 ASSERT_TRUE(ptr != nullptr) << dlerror();
163 ASSERT_EQ(43, *ptr);
164
165 dlclose(handle);
166 dlclose(preload);
167}
168
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700169TEST(dlfcn, dlsym_handle_global_sym) {
170 // check that we do not look into global group
171 // when looking up symbol by handle
172 void* handle = dlopen("libtest_empty.so", RTLD_NOW);
173 dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL);
174 void* sym = dlsym(handle, "getRandomNumber");
175 ASSERT_TRUE(sym == nullptr);
176 ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror());
177
178 sym = dlsym(handle, "DlSymTestFunction");
179 ASSERT_TRUE(sym == nullptr);
180 ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror());
181 dlclose(handle);
182}
183
Dimitry Ivanovd5b578a2016-12-14 15:16:56 -0800184TEST(dlfcn, dlsym_handle_empty_symbol) {
185 // check that dlsym of an empty symbol fails (see http://b/33530622)
186 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW);
187 ASSERT_TRUE(handle != nullptr) << dlerror();
188 void* sym = dlsym(handle, "");
189 ASSERT_TRUE(sym == nullptr);
190 ASSERT_SUBSTR("undefined symbol: ", dlerror());
191 dlclose(handle);
192}
193
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700194TEST(dlfcn, dlsym_with_dependencies) {
195 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700196 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700197 dlerror();
198 // This symbol is in DT_NEEDED library.
199 void* sym = dlsym(handle, "getRandomNumber");
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700200 ASSERT_TRUE(sym != nullptr) << dlerror();
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700201 int (*fn)(void);
202 fn = reinterpret_cast<int (*)(void)>(sym);
203 EXPECT_EQ(4, fn());
204 dlclose(handle);
205}
206
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700207TEST(dlfcn, dlopen_noload) {
208 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700209 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700210 handle = dlopen("libtest_simple.so", RTLD_NOW);
211 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700212 ASSERT_TRUE(handle != nullptr);
213 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700214 ASSERT_TRUE(handle == handle2);
215 ASSERT_EQ(0, dlclose(handle));
216 ASSERT_EQ(0, dlclose(handle2));
217}
218
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700219TEST(dlfcn, dlopen_by_soname) {
220 static const char* soname = "libdlext_test_soname.so";
221 static const char* filename = "libdlext_test_different_soname.so";
222 // 1. Make sure there is no library with soname in default search path
223 void* handle = dlopen(soname, RTLD_NOW);
224 ASSERT_TRUE(handle == nullptr);
225
226 // 2. Load a library using filename
227 handle = dlopen(filename, RTLD_NOW);
228 ASSERT_TRUE(handle != nullptr) << dlerror();
229
230 // 3. Find library by soname
231 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
232 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
233 ASSERT_EQ(handle, handle_soname);
234
235 // 4. RTLD_NOLOAD should still work with filename
236 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
237 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
238 ASSERT_EQ(handle, handle_filename);
239
240 dlclose(handle_filename);
241 dlclose(handle_soname);
242 dlclose(handle);
243}
244
dimitryc18de1b2017-09-26 14:31:35 +0200245TEST(dlfcn, dlopen_vdso) {
dimitryb9555a92017-10-11 18:04:05 +0200246#if __has_include(<sys/auxv.h>)
247 if (getauxval(AT_SYSINFO_EHDR) == 0) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800248 GTEST_SKIP() << "getauxval(AT_SYSINFO_EHDR) == 0, skipping this test";
dimitryb9555a92017-10-11 18:04:05 +0200249 }
250#endif
Elliott Hughesda1bb112018-02-16 10:05:08 -0800251
252 const char* vdso_name = "linux-vdso.so.1";
253#if defined(__i386__)
254 vdso_name = "linux-gate.so.1";
255#endif
256 void* handle = dlopen(vdso_name, RTLD_NOW);
dimitryc18de1b2017-09-26 14:31:35 +0200257 ASSERT_TRUE(handle != nullptr) << dlerror();
258 dlclose(handle);
259}
260
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700261TEST(dlfcn, ifunc_variable) {
262 typedef const char* (*fn_ptr)();
263
264 // ifunc's choice depends on whether IFUNC_CHOICE has a value
265 // first check the set case
266 setenv("IFUNC_CHOICE", "set", 1);
267 // preload libtest_ifunc_variable_impl.so
268 void* handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
269 void* handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
270 ASSERT_TRUE(handle != nullptr) << dlerror();
271 const char** foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
272 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
273 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
274 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
275 ASSERT_EQ(strncmp("set", *foo_ptr, 3), 0);
276 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
277 dlclose(handle);
278 dlclose(handle_impl);
279
280 // then check the unset case
281 unsetenv("IFUNC_CHOICE");
282 handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
283 handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
284 ASSERT_TRUE(handle != nullptr) << dlerror();
285 foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
286 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
287 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
288 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
289 ASSERT_EQ(strncmp("unset", *foo_ptr, 5), 0);
290 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
291 dlclose(handle);
292 dlclose(handle_impl);
293}
294
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700295TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700296 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700297
298 // ifunc's choice depends on whether IFUNC_CHOICE has a value
299 // first check the set case
300 setenv("IFUNC_CHOICE", "set", 1);
301 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700302 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700303 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
304 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700305 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
306 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700307 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
308 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700309 dlclose(handle);
310
311 // then check the unset case
312 unsetenv("IFUNC_CHOICE");
313 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700314 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700315 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
316 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700317 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
318 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700319 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700320 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700321 dlclose(handle);
322}
323
324TEST(dlfcn, ifunc_ctor_call) {
325 typedef const char* (*fn_ptr)();
326
327 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700328 ASSERT_TRUE(handle != nullptr) << dlerror();
329 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
330 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
331 ASSERT_STREQ("false", is_ctor_called());
332
333 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
334 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700335 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700336 dlclose(handle);
337}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700338
339TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
340 typedef const char* (*fn_ptr)();
341
342 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
343 ASSERT_TRUE(handle != nullptr) << dlerror();
344 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
345 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
346 ASSERT_STREQ("false", is_ctor_called());
347
348 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
349 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
350 ASSERT_STREQ("true", is_ctor_called());
351 dlclose(handle);
352}
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700353
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700354TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
355 // This is the structure of the test library and
356 // its dt_needed libraries
357 // libtest_relo_check_dt_needed_order.so
358 // |
359 // +-> libtest_relo_check_dt_needed_order_1.so
360 // |
361 // +-> libtest_relo_check_dt_needed_order_2.so
362 //
363 // The root library references relo_test_get_answer_lib - which is defined
364 // in both dt_needed libraries, the correct relocation should
365 // use the function defined in libtest_relo_check_dt_needed_order_1.so
366 void* handle = nullptr;
Tom Cherryb8ab6182017-04-05 16:20:29 -0700367 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700368
369 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
370 ASSERT_TRUE(handle != nullptr) << dlerror();
371
372 typedef int (*fn_t) (void);
373 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
374 ASSERT_TRUE(fn != nullptr) << dlerror();
375 ASSERT_EQ(1, fn());
376}
377
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700378TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700379 // Here is how the test library and its dt_needed
380 // libraries are arranged
381 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700382 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700383 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700384 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700385 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700386 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700387 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700388 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700389 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700390 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700391 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700392 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700393 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700394 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700395 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700396 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700397 //
398 // load order should be (1, 2, 3, a, b, d)
399 //
400 // get_answer() is defined in (2, 3, a, b, c)
401 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700402 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700403 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700404 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
405 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700406 typedef int (*fn_t) (void);
407 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700408 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700409 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700410 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700411 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700412
413 ASSERT_EQ(42, fn());
414 ASSERT_EQ(43, fn2());
415 dlclose(handle);
416}
417
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700418TEST(dlfcn, dlopen_check_order_reloc_siblings) {
419 // This is how this one works:
420 // we lookup and call get_answer which is defined in '_2.so'
421 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
422 // the correct _impl() is implemented by '_a.so';
423 //
424 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
425 //
426 // Here is the picture:
427 //
428 // libtest_check_order_reloc_siblings.so
429 // |
430 // +-> ..._1.so <- empty
431 // | |
432 // | +-> ..._a.so <- exports correct answer_impl()
433 // | |
434 // | +-> ..._b.so <- every other letter exporting incorrect one.
435 // |
436 // +-> ..._2.so <- empty
437 // | |
438 // | +-> ..._c.so
439 // | |
440 // | +-> ..._d.so
441 // |
442 // +-> ..._3.so <- empty
443 // |
444 // +-> ..._e.so
445 // |
446 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
447 // implements incorrect get_answer_impl()
448
449 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
450 ASSERT_TRUE(handle == nullptr);
451#ifdef __BIONIC__
452 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
453 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
454#endif
455
456 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
457 ASSERT_TRUE(handle != nullptr) << dlerror();
458
459 typedef int (*fn_t) (void);
460 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
461 ASSERT_TRUE(fn != nullptr) << dlerror();
462 ASSERT_EQ(42, fn());
463
464 ASSERT_EQ(0, dlclose(handle));
465}
466
467TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
468 // This test uses the same library as dlopen_check_order_reloc_siblings.
469 // Unlike dlopen_check_order_reloc_siblings it preloads
470 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
471 // dlopen(libtest_check_order_reloc_siblings.so)
472
473 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
474 ASSERT_TRUE(handle == nullptr);
475 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
476 ASSERT_TRUE(handle == nullptr);
477
478 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
479 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
480
481 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
482 ASSERT_TRUE(handle != nullptr) << dlerror();
483
484 ASSERT_EQ(0, dlclose(handle_for_1));
485
486 typedef int (*fn_t) (void);
487 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
488 ASSERT_TRUE(fn != nullptr) << dlerror();
489 ASSERT_EQ(42, fn());
490
491 ASSERT_EQ(0, dlclose(handle));
492}
493
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800494TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
495 // This is how this one works:
496 // we lookup and call grandchild_get_answer which is defined in '_2.so'
497 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
498 // the correct _impl() is implemented by '_c_1.so';
499 //
500 // Here is the picture of subtree:
501 //
502 // libtest_check_order_reloc_siblings.so
503 // |
504 // +-> ..._2.so <- grandchild_get_answer()
505 // |
506 // +-> ..._c.so <- empty
507 // | |
508 // | +-> _c_1.so <- exports correct answer_impl()
509 // | |
510 // | +-> _c_2.so <- exports incorrect answer_impl()
511 // |
512 // +-> ..._d.so <- empty
513
514 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
515 ASSERT_TRUE(handle == nullptr);
516#ifdef __BIONIC__
517 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
518 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
519#endif
520
521 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
522 ASSERT_TRUE(handle != nullptr) << dlerror();
523
524 typedef int (*fn_t) (void);
525 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
526 ASSERT_TRUE(fn != nullptr) << dlerror();
527 ASSERT_EQ(42, fn());
528
529 ASSERT_EQ(0, dlclose(handle));
530}
531
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700532TEST(dlfcn, dlopen_check_order_reloc_nephew) {
533 // This is how this one works:
534 // we lookup and call nephew_get_answer which is defined in '_2.so'
535 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
536 // the correct _impl() is implemented by '_a.so';
537 //
538 // Here is the picture:
539 //
540 // libtest_check_order_reloc_siblings.so
541 // |
542 // +-> ..._1.so <- empty
543 // | |
544 // | +-> ..._a.so <- exports correct answer_impl()
545 // | |
546 // | +-> ..._b.so <- every other letter exporting incorrect one.
547 // |
548 // +-> ..._2.so <- empty
549 // | |
550 // | +-> ..._c.so
551 // | |
552 // | +-> ..._d.so
553 // |
554 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
555 // |
556 // +-> ..._e.so
557 // |
558 // +-> ..._f.so
559
560 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
561 ASSERT_TRUE(handle == nullptr);
562#ifdef __BIONIC__
563 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
564 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
565#endif
566
567 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
568 ASSERT_TRUE(handle != nullptr) << dlerror();
569
570 typedef int (*fn_t) (void);
571 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
572 ASSERT_TRUE(fn != nullptr) << dlerror();
573 ASSERT_EQ(42, fn());
574
575 ASSERT_EQ(0, dlclose(handle));
576}
577
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800578TEST(dlfcn, check_unload_after_reloc) {
579 // This is how this one works:
580 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
581 // |
582 // +-> libtest_two_parents_child
583 //
584 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
585 // |
586 // +-> libtest_two_parents_child
587 //
588 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
589 // as a second step it dlopens parent2 and dlcloses parent1...
590
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700591 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
592 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800593
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700594 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
595 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800596
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700597 typedef int (*fn_t) (void);
598 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
599 ASSERT_TRUE(fn != nullptr) << dlerror();
600 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800601
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700602 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800603
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700604 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
605 ASSERT_TRUE(handle != nullptr);
606 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800607
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700608 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
609 ASSERT_TRUE(fn != nullptr) << dlerror();
610 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800611
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700612 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800613
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700614 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
615 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800616}
617
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700618extern "C" int check_order_reloc_root_get_answer_impl() {
619 return 42;
620}
621
622TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
623 // This is how this one works:
624 // we lookup and call get_answer3 which is defined in 'root.so'
625 // and in turn calls external root_get_answer_impl() defined in _2.so and
626 // above the correct _impl() is one in the executable.
627 //
628 // libtest_check_order_reloc_root.so
629 // |
630 // +-> ..._1.so <- empty
631 // |
632 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
633 //
634
635 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
636 ASSERT_TRUE(handle == nullptr);
637#ifdef __BIONIC__
638 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
639 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
640#endif
641
642 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
643 ASSERT_TRUE(handle != nullptr) << dlerror();
644
645 typedef int (*fn_t) (void);
646 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
647 ASSERT_TRUE(fn != nullptr) << dlerror();
648 ASSERT_EQ(42, fn());
649
650 ASSERT_EQ(0, dlclose(handle));
651}
652
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700653TEST(dlfcn, dlopen_check_rtld_local) {
654 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
655 ASSERT_TRUE(sym == nullptr);
656
657 // implicit RTLD_LOCAL
658 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
659 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
660 ASSERT_TRUE(sym == nullptr);
661 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
662 sym = dlsym(handle, "dlopen_testlib_simple_func");
663 ASSERT_TRUE(sym != nullptr);
664 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
665 dlclose(handle);
666
667 // explicit RTLD_LOCAL
668 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
669 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
670 ASSERT_TRUE(sym == nullptr);
671 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
672 sym = dlsym(handle, "dlopen_testlib_simple_func");
673 ASSERT_TRUE(sym != nullptr);
674 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
675 dlclose(handle);
676}
677
678TEST(dlfcn, dlopen_check_rtld_global) {
679 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
680 ASSERT_TRUE(sym == nullptr);
681
682 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700683 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700684 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
685 ASSERT_TRUE(sym != nullptr) << dlerror();
686 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
687 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700688
689 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
690 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
691 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700692
693 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
694 // shared libraries after symbol was not found in the main executable
695 // and dependent libraries.
696 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
697 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
698 ASSERT_TRUE(sym != nullptr) << dlerror();
699
700 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700701}
702
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700703// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
704// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
705// libtest_with_dependency_loop_a.so
706TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700707 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
708 ASSERT_TRUE(handle != nullptr) << dlerror();
709 void* f = dlsym(handle, "dlopen_test_loopy_function");
710 ASSERT_TRUE(f != nullptr) << dlerror();
711 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
712 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700713
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700714 // dlopen second time to make sure that the library was unloaded correctly
715 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
716 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800717#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700718 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 -0800719#else
720 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
721 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700722#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800723
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700724 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
725 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700726}
727
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700728TEST(dlfcn, dlopen_nodelete) {
729 static bool is_unloaded = false;
730
731 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
732 ASSERT_TRUE(handle != nullptr) << dlerror();
733 void (*set_unload_flag_ptr)(bool*);
734 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
735 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
736 set_unload_flag_ptr(&is_unloaded);
737
738 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
739 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
740 ASSERT_EQ(1729U, *taxicab_number);
741 *taxicab_number = 2;
742
743 dlclose(handle);
744 ASSERT_TRUE(!is_unloaded);
745
746 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
747 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
748 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
749
750
751 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
752 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
753 ASSERT_EQ(taxicab_number2, taxicab_number);
754
755 ASSERT_EQ(2U, *taxicab_number2);
756
757 dlclose(handle);
758 ASSERT_TRUE(!is_unloaded);
759}
760
761TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
762 static bool is_unloaded = false;
763
764 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
765 ASSERT_TRUE(handle != nullptr) << dlerror();
766 void (*set_unload_flag_ptr)(bool*);
767 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
768 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
769 set_unload_flag_ptr(&is_unloaded);
770
771 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
772 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
773
774 ASSERT_EQ(1729U, *taxicab_number);
775 *taxicab_number = 2;
776
777 // This RTLD_NODELETE should be ignored
778 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
779 ASSERT_TRUE(handle1 != nullptr) << dlerror();
780 ASSERT_EQ(handle, handle1);
781
782 dlclose(handle1);
783 dlclose(handle);
784
785 ASSERT_TRUE(is_unloaded);
786}
787
788TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
789 static bool is_unloaded = false;
790
791 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
792 ASSERT_TRUE(handle != nullptr) << dlerror();
793 void (*set_unload_flag_ptr)(bool*);
794 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
795 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
796 set_unload_flag_ptr(&is_unloaded);
797
798 dlclose(handle);
799 ASSERT_TRUE(!is_unloaded);
800}
801
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700802TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700803 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
804 ASSERT_TRUE(handle != nullptr) << dlerror();
805 int (*get_answer)();
806 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
807 ASSERT_TRUE(get_answer != nullptr) << dlerror();
808 ASSERT_EQ(42, get_answer());
809 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700810}
811
Elliott Hughese66190d2012-12-18 15:57:55 -0800812TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700813 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700814 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700815#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700816 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
817#else
818 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
819#endif
820}
821
dimitry109040c2017-11-03 13:49:07 +0100822TEST(dlfcn, dlclose_unload) {
823 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
824 ASSERT_TRUE(handle != nullptr) << dlerror();
825 uint32_t* taxicab_number = static_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
826 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
827 EXPECT_EQ(1729U, *taxicab_number);
828 dlclose(handle);
829 // Making sure that the library has been unmapped as part of library unload
830 // process. Note that mprotect somewhat counter-intuitively returns ENOMEM in
831 // this case.
832 uintptr_t page_start = reinterpret_cast<uintptr_t>(taxicab_number) & ~(PAGE_SIZE - 1);
833 ASSERT_TRUE(mprotect(reinterpret_cast<void*>(page_start), PAGE_SIZE, PROT_NONE) != 0);
834 ASSERT_EQ(ENOMEM, errno) << strerror(errno);
835}
836
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800837static void ConcurrentDlErrorFn(std::string& error) {
838 ASSERT_TRUE(dlerror() == nullptr);
839
840 void* handle = dlopen("/child/thread", RTLD_NOW);
841 ASSERT_TRUE(handle == nullptr);
842
843 const char* err = dlerror();
844 ASSERT_TRUE(err != nullptr);
845
846 error = err;
847}
848
849TEST(dlfcn, dlerror_concurrent_buffer) {
850 void* handle = dlopen("/main/thread", RTLD_NOW);
851 ASSERT_TRUE(handle == nullptr);
852 const char* main_thread_error = dlerror();
853 ASSERT_TRUE(main_thread_error != nullptr);
854 ASSERT_SUBSTR("/main/thread", main_thread_error);
855
856 std::string child_thread_error;
857 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
858 t.join();
859 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
860
861 // Check that main thread local buffer was not modified.
862 ASSERT_SUBSTR("/main/thread", main_thread_error);
Elliott Hughes5419b942012-10-16 15:54:46 -0700863}
864
Elliott Hughese66190d2012-12-18 15:57:55 -0800865TEST(dlfcn, dlerror_concurrent) {
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800866 void* handle = dlopen("/main/thread", RTLD_NOW);
867 ASSERT_TRUE(handle == nullptr);
868
869 std::string child_thread_error;
870 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
871 t.join();
872 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
873
Elliott Hughes5419b942012-10-16 15:54:46 -0700874 const char* main_thread_error = dlerror();
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800875 ASSERT_TRUE(main_thread_error != nullptr);
Elliott Hughes5419b942012-10-16 15:54:46 -0700876 ASSERT_SUBSTR("/main/thread", main_thread_error);
877}
878
Elliott Hughese66190d2012-12-18 15:57:55 -0800879TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700880 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700881 void* self = dlopen(nullptr, RTLD_NOW);
882 ASSERT_TRUE(self != nullptr);
883 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700884
885 void* sym;
886
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700887#if defined(__BIONIC__) && !defined(__LP64__)
888 // RTLD_DEFAULT in lp32 bionic is not (void*)0
889 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700890 sym = dlsym(nullptr, "test");
891 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800892 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700893#endif
894
Elliott Hughes3b297c42012-10-11 16:08:51 -0700895 // Symbol that doesn't exist.
896 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700897 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700898 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700899
900 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700901}
902
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700903TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700904 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700905 void* self = dlopen(nullptr, RTLD_NOW);
906 ASSERT_TRUE(self != nullptr);
907 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700908
909 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700910 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700911
912 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
913 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
914
915 Dl_info info;
916 int rc = dladdr(addr, &info);
917 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
918
919 // Get the name of this executable.
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700920 const std::string executable_path = android::base::GetExecutablePath();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700921
922 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700923 char dli_realpath[PATH_MAX];
924 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700925 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700926
927 // The symbol name should be the symbol we looked up.
928 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
929
930 // The address should be the exact address of the symbol.
931 ASSERT_EQ(info.dli_saddr, sym);
932
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700933 std::vector<map_record> maps;
934 ASSERT_TRUE(Maps::parse_maps(&maps));
935
936 void* base_address = nullptr;
937 for (const map_record& rec : maps) {
938 if (executable_path == rec.pathname) {
939 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700940 break;
941 }
942 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700943
944 // The base address should be the address we were loaded at.
945 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700946
947 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700948}
949
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700950TEST(dlfcn, dlopen_executable_by_absolute_path) {
951 void* handle1 = dlopen(nullptr, RTLD_NOW);
952 ASSERT_TRUE(handle1 != nullptr) << dlerror();
953
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700954 void* handle2 = dlopen(android::base::GetExecutablePath().c_str(), RTLD_NOW);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700955 ASSERT_TRUE(handle2 != nullptr) << dlerror();
956
957#if defined(__BIONIC__)
958 ASSERT_EQ(handle1, handle2);
959#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800960 GTEST_SKIP() << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
961 "it loads a separate copy of the main executable "
962 "on dlopen by absolute path";
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700963#endif
964}
965
Victor Khimenko14b9d712017-01-25 19:59:16 +0100966#if defined (__aarch64__)
Dmytro Chystiakove712cd12019-03-29 13:49:12 -0700967#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib64/arm64/"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100968#elif defined (__arm__)
969#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm/"
970#elif defined (__i386__)
971#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86/"
972#elif defined (__x86_64__)
Dmytro Chystiakove712cd12019-03-29 13:49:12 -0700973#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib64/x86_64/"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100974#else
975#error "Unknown architecture"
976#endif
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200977#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Peter Collingbourneea11be02019-04-30 16:05:13 -0700978#define PATH_TO_BOOTSTRAP_LIBC PATH_TO_SYSTEM_LIB "bootstrap/libc.so"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100979#define ALTERNATE_PATH_TO_LIBC ALTERNATE_PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700980
981TEST(dlfcn, dladdr_libc) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800982#if defined(__GLIBC__)
983 GTEST_SKIP() << "glibc returns libc.so's ldconfig path, which is a symlink (not a realpath)";
984#endif
985
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700986 Dl_info info;
987 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
988 ASSERT_TRUE(dladdr(addr, &info) != 0);
989
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700990 char libc_realpath[PATH_MAX];
Victor Khimenko14b9d712017-01-25 19:59:16 +0100991
992 // Check if libc is in canonical path or in alternate path.
993 if (strncmp(ALTERNATE_PATH_TO_SYSTEM_LIB,
994 info.dli_fname,
995 sizeof(ALTERNATE_PATH_TO_SYSTEM_LIB) - 1) == 0) {
996 // Platform with emulated architecture. Symlink on ARC++.
997 ASSERT_TRUE(realpath(ALTERNATE_PATH_TO_LIBC, libc_realpath) == libc_realpath);
Peter Collingbourneea11be02019-04-30 16:05:13 -0700998 } else if (strncmp(PATH_TO_BOOTSTRAP_LIBC, info.dli_fname,
999 sizeof(PATH_TO_BOOTSTRAP_LIBC) - 1) == 0) {
1000 ASSERT_TRUE(realpath(PATH_TO_BOOTSTRAP_LIBC, libc_realpath) == libc_realpath);
Victor Khimenko14b9d712017-01-25 19:59:16 +01001001 } else {
1002 // /system/lib is symlink when this test is executed on host.
1003 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
1004 }
Dmitriy Ivanovef255922015-04-08 11:53:08 -07001005
1006 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001007 // TODO: add check for dfi_fbase
1008 ASSERT_STREQ("puts", info.dli_sname);
1009 ASSERT_EQ(addr, info.dli_saddr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001010}
1011
Elliott Hughese66190d2012-12-18 15:57:55 -08001012TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -07001013 Dl_info info;
1014
Elliott Hughes3b297c42012-10-11 16:08:51 -07001015 dlerror(); // Clear any pending errors.
1016
Elliott Hughes8e15b082012-09-26 11:44:01 -07001017 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001018 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
1019 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
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
Jiyong Park01162f22017-10-16 15:31:09 +09001654TEST(dlfcn, dlopen_df_1_global) {
1655 void* handle = dlopen("libtest_dlopen_df_1_global.so", RTLD_NOW);
1656 ASSERT_TRUE(handle != nullptr) << dlerror();
1657}
1658
Peter Collingbourneb39cb3c2019-03-01 13:12:49 -08001659TEST(dlfcn, segment_gap) {
1660 void* handle = dlopen("libsegment_gap_outer.so", RTLD_NOW);
1661 ASSERT_TRUE(handle != nullptr) << dlerror();
1662
1663 auto get_inner = reinterpret_cast<void* (*)()>(dlsym(handle, "get_inner"));
1664 void* inner = get_inner();
1665 (void)inner;
1666
1667#if __arm__
1668 int count;
1669 _Unwind_Ptr outer_exidx = dl_unwind_find_exidx(reinterpret_cast<_Unwind_Ptr>(get_inner), &count);
1670 _Unwind_Ptr inner_exidx = dl_unwind_find_exidx(reinterpret_cast<_Unwind_Ptr>(inner), &count);
1671 EXPECT_NE(0u, outer_exidx);
1672 EXPECT_NE(0u, inner_exidx);
1673 EXPECT_NE(inner_exidx, outer_exidx);
1674#endif
1675
1676 Dl_info info;
1677 int rc = dladdr(inner, &info);
1678 ASSERT_NE(rc, 0);
1679
1680 EXPECT_NE(nullptr, strstr(info.dli_fname, "libsegment_gap_inner.so"));
1681}
1682
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001683#endif