blob: e3664fd97713116a51f9e4832d9ba4117eebc5b3 [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
Elliott Hughes82c90722022-02-17 11:55:49 -0800261// HWASan uses an ifunc to describe the location of its shadow memory,
262// so even though it's an unusual case, Android needs to support
263// "ifunc variables".
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700264TEST(dlfcn, ifunc_variable) {
265 typedef const char* (*fn_ptr)();
266
267 // ifunc's choice depends on whether IFUNC_CHOICE has a value
268 // first check the set case
269 setenv("IFUNC_CHOICE", "set", 1);
270 // preload libtest_ifunc_variable_impl.so
271 void* handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
272 void* handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
273 ASSERT_TRUE(handle != nullptr) << dlerror();
274 const char** foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
275 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
276 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
277 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
278 ASSERT_EQ(strncmp("set", *foo_ptr, 3), 0);
279 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
280 dlclose(handle);
281 dlclose(handle_impl);
282
283 // then check the unset case
284 unsetenv("IFUNC_CHOICE");
285 handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
286 handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
287 ASSERT_TRUE(handle != nullptr) << dlerror();
288 foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
289 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
290 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
291 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
292 ASSERT_EQ(strncmp("unset", *foo_ptr, 5), 0);
293 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
294 dlclose(handle);
295 dlclose(handle_impl);
296}
297
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700298TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700299 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700300
301 // ifunc's choice depends on whether IFUNC_CHOICE has a value
302 // first check the set case
303 setenv("IFUNC_CHOICE", "set", 1);
304 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700305 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700306 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
307 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700308 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
309 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700310 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
311 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700312 dlclose(handle);
313
314 // then check the unset case
315 unsetenv("IFUNC_CHOICE");
316 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700317 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700318 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
319 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700320 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
321 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700322 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700323 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700324 dlclose(handle);
325}
326
327TEST(dlfcn, ifunc_ctor_call) {
328 typedef const char* (*fn_ptr)();
329
330 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700331 ASSERT_TRUE(handle != nullptr) << dlerror();
332 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
333 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
334 ASSERT_STREQ("false", is_ctor_called());
335
336 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
337 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700338 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700339 dlclose(handle);
340}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700341
342TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
343 typedef const char* (*fn_ptr)();
344
345 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
346 ASSERT_TRUE(handle != nullptr) << dlerror();
347 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
348 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
349 ASSERT_STREQ("false", is_ctor_called());
350
351 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
352 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
353 ASSERT_STREQ("true", is_ctor_called());
354 dlclose(handle);
355}
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700356
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700357TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
358 // This is the structure of the test library and
359 // its dt_needed libraries
360 // libtest_relo_check_dt_needed_order.so
361 // |
362 // +-> libtest_relo_check_dt_needed_order_1.so
363 // |
364 // +-> libtest_relo_check_dt_needed_order_2.so
365 //
366 // The root library references relo_test_get_answer_lib - which is defined
367 // in both dt_needed libraries, the correct relocation should
368 // use the function defined in libtest_relo_check_dt_needed_order_1.so
369 void* handle = nullptr;
Tom Cherryb8ab6182017-04-05 16:20:29 -0700370 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700371
372 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
373 ASSERT_TRUE(handle != nullptr) << dlerror();
374
375 typedef int (*fn_t) (void);
376 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
377 ASSERT_TRUE(fn != nullptr) << dlerror();
378 ASSERT_EQ(1, fn());
379}
380
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700381TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700382 // Here is how the test library and its dt_needed
383 // libraries are arranged
384 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700385 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700386 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700387 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700388 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700389 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700390 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700391 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700392 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700393 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700394 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700395 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700396 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700397 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700398 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700399 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700400 //
401 // load order should be (1, 2, 3, a, b, d)
402 //
403 // get_answer() is defined in (2, 3, a, b, c)
404 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700405 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700406 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700407 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
408 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700409 typedef int (*fn_t) (void);
410 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700411 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700412 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700413 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700414 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700415
416 ASSERT_EQ(42, fn());
417 ASSERT_EQ(43, fn2());
418 dlclose(handle);
419}
420
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700421TEST(dlfcn, dlopen_check_order_reloc_siblings) {
422 // This is how this one works:
423 // we lookup and call get_answer which is defined in '_2.so'
424 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
425 // the correct _impl() is implemented by '_a.so';
426 //
427 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
428 //
429 // Here is the picture:
430 //
431 // libtest_check_order_reloc_siblings.so
432 // |
433 // +-> ..._1.so <- empty
434 // | |
435 // | +-> ..._a.so <- exports correct answer_impl()
436 // | |
437 // | +-> ..._b.so <- every other letter exporting incorrect one.
438 // |
439 // +-> ..._2.so <- empty
440 // | |
441 // | +-> ..._c.so
442 // | |
443 // | +-> ..._d.so
444 // |
445 // +-> ..._3.so <- empty
446 // |
447 // +-> ..._e.so
448 // |
449 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
450 // implements incorrect get_answer_impl()
451
452 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
453 ASSERT_TRUE(handle == nullptr);
454#ifdef __BIONIC__
455 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
456 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
457#endif
458
459 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
460 ASSERT_TRUE(handle != nullptr) << dlerror();
461
462 typedef int (*fn_t) (void);
463 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
464 ASSERT_TRUE(fn != nullptr) << dlerror();
465 ASSERT_EQ(42, fn());
466
467 ASSERT_EQ(0, dlclose(handle));
468}
469
470TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
471 // This test uses the same library as dlopen_check_order_reloc_siblings.
472 // Unlike dlopen_check_order_reloc_siblings it preloads
473 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
474 // dlopen(libtest_check_order_reloc_siblings.so)
475
476 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
477 ASSERT_TRUE(handle == nullptr);
478 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
479 ASSERT_TRUE(handle == nullptr);
480
481 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
482 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
483
484 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
485 ASSERT_TRUE(handle != nullptr) << dlerror();
486
487 ASSERT_EQ(0, dlclose(handle_for_1));
488
489 typedef int (*fn_t) (void);
490 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
491 ASSERT_TRUE(fn != nullptr) << dlerror();
492 ASSERT_EQ(42, fn());
493
494 ASSERT_EQ(0, dlclose(handle));
495}
496
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800497TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
498 // This is how this one works:
499 // we lookup and call grandchild_get_answer which is defined in '_2.so'
500 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
501 // the correct _impl() is implemented by '_c_1.so';
502 //
503 // Here is the picture of subtree:
504 //
505 // libtest_check_order_reloc_siblings.so
506 // |
507 // +-> ..._2.so <- grandchild_get_answer()
508 // |
509 // +-> ..._c.so <- empty
510 // | |
511 // | +-> _c_1.so <- exports correct answer_impl()
512 // | |
513 // | +-> _c_2.so <- exports incorrect answer_impl()
514 // |
515 // +-> ..._d.so <- empty
516
517 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
518 ASSERT_TRUE(handle == nullptr);
519#ifdef __BIONIC__
520 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
521 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
522#endif
523
524 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
525 ASSERT_TRUE(handle != nullptr) << dlerror();
526
527 typedef int (*fn_t) (void);
528 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
529 ASSERT_TRUE(fn != nullptr) << dlerror();
530 ASSERT_EQ(42, fn());
531
532 ASSERT_EQ(0, dlclose(handle));
533}
534
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700535TEST(dlfcn, dlopen_check_order_reloc_nephew) {
536 // This is how this one works:
537 // we lookup and call nephew_get_answer which is defined in '_2.so'
538 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
539 // the correct _impl() is implemented by '_a.so';
540 //
541 // Here is the picture:
542 //
543 // libtest_check_order_reloc_siblings.so
544 // |
545 // +-> ..._1.so <- empty
546 // | |
547 // | +-> ..._a.so <- exports correct answer_impl()
548 // | |
549 // | +-> ..._b.so <- every other letter exporting incorrect one.
550 // |
551 // +-> ..._2.so <- empty
552 // | |
553 // | +-> ..._c.so
554 // | |
555 // | +-> ..._d.so
556 // |
557 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
558 // |
559 // +-> ..._e.so
560 // |
561 // +-> ..._f.so
562
563 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
564 ASSERT_TRUE(handle == nullptr);
565#ifdef __BIONIC__
566 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
567 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
568#endif
569
570 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
571 ASSERT_TRUE(handle != nullptr) << dlerror();
572
573 typedef int (*fn_t) (void);
574 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
575 ASSERT_TRUE(fn != nullptr) << dlerror();
576 ASSERT_EQ(42, fn());
577
578 ASSERT_EQ(0, dlclose(handle));
579}
580
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800581TEST(dlfcn, check_unload_after_reloc) {
582 // This is how this one works:
583 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
584 // |
585 // +-> libtest_two_parents_child
586 //
587 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
588 // |
589 // +-> libtest_two_parents_child
590 //
591 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
592 // as a second step it dlopens parent2 and dlcloses parent1...
593
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700594 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
595 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800596
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700597 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
598 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800599
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700600 typedef int (*fn_t) (void);
601 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
602 ASSERT_TRUE(fn != nullptr) << dlerror();
603 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800604
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700605 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800606
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700607 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
608 ASSERT_TRUE(handle != nullptr);
609 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800610
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700611 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
612 ASSERT_TRUE(fn != nullptr) << dlerror();
613 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800614
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700615 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800616
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700617 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
618 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800619}
620
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700621extern "C" int check_order_reloc_root_get_answer_impl() {
622 return 42;
623}
624
625TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
626 // This is how this one works:
627 // we lookup and call get_answer3 which is defined in 'root.so'
628 // and in turn calls external root_get_answer_impl() defined in _2.so and
629 // above the correct _impl() is one in the executable.
630 //
631 // libtest_check_order_reloc_root.so
632 // |
633 // +-> ..._1.so <- empty
634 // |
635 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
636 //
637
638 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
639 ASSERT_TRUE(handle == nullptr);
640#ifdef __BIONIC__
641 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
642 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
643#endif
644
645 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
646 ASSERT_TRUE(handle != nullptr) << dlerror();
647
648 typedef int (*fn_t) (void);
649 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
650 ASSERT_TRUE(fn != nullptr) << dlerror();
651 ASSERT_EQ(42, fn());
652
653 ASSERT_EQ(0, dlclose(handle));
654}
655
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700656TEST(dlfcn, dlopen_check_rtld_local) {
657 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
658 ASSERT_TRUE(sym == nullptr);
659
660 // implicit RTLD_LOCAL
661 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
662 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
663 ASSERT_TRUE(sym == nullptr);
664 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
665 sym = dlsym(handle, "dlopen_testlib_simple_func");
666 ASSERT_TRUE(sym != nullptr);
667 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
668 dlclose(handle);
669
670 // explicit RTLD_LOCAL
671 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
672 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
673 ASSERT_TRUE(sym == nullptr);
674 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
675 sym = dlsym(handle, "dlopen_testlib_simple_func");
676 ASSERT_TRUE(sym != nullptr);
677 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
678 dlclose(handle);
679}
680
681TEST(dlfcn, dlopen_check_rtld_global) {
682 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
683 ASSERT_TRUE(sym == nullptr);
684
685 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700686 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700687 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
688 ASSERT_TRUE(sym != nullptr) << dlerror();
689 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
690 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700691
692 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
693 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
694 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700695
696 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
697 // shared libraries after symbol was not found in the main executable
698 // and dependent libraries.
699 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
700 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
701 ASSERT_TRUE(sym != nullptr) << dlerror();
702
703 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700704}
705
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700706// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
707// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
708// libtest_with_dependency_loop_a.so
709TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700710 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
711 ASSERT_TRUE(handle != nullptr) << dlerror();
712 void* f = dlsym(handle, "dlopen_test_loopy_function");
713 ASSERT_TRUE(f != nullptr) << dlerror();
714 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
715 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700716
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700717 // dlopen second time to make sure that the library was unloaded correctly
718 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
719 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800720#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700721 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 -0800722#else
723 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
724 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700725#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800726
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700727 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
728 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700729}
730
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700731TEST(dlfcn, dlopen_nodelete) {
732 static bool is_unloaded = false;
733
734 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
735 ASSERT_TRUE(handle != nullptr) << dlerror();
736 void (*set_unload_flag_ptr)(bool*);
737 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
738 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
739 set_unload_flag_ptr(&is_unloaded);
740
741 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
742 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
743 ASSERT_EQ(1729U, *taxicab_number);
744 *taxicab_number = 2;
745
746 dlclose(handle);
747 ASSERT_TRUE(!is_unloaded);
748
749 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
750 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
751 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
752
753
754 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
755 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
756 ASSERT_EQ(taxicab_number2, taxicab_number);
757
758 ASSERT_EQ(2U, *taxicab_number2);
759
760 dlclose(handle);
761 ASSERT_TRUE(!is_unloaded);
762}
763
764TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
765 static bool is_unloaded = false;
766
767 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
768 ASSERT_TRUE(handle != nullptr) << dlerror();
769 void (*set_unload_flag_ptr)(bool*);
770 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
771 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
772 set_unload_flag_ptr(&is_unloaded);
773
774 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
775 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
776
777 ASSERT_EQ(1729U, *taxicab_number);
778 *taxicab_number = 2;
779
780 // This RTLD_NODELETE should be ignored
781 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
782 ASSERT_TRUE(handle1 != nullptr) << dlerror();
783 ASSERT_EQ(handle, handle1);
784
785 dlclose(handle1);
786 dlclose(handle);
787
788 ASSERT_TRUE(is_unloaded);
789}
790
791TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
792 static bool is_unloaded = false;
793
794 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
795 ASSERT_TRUE(handle != nullptr) << dlerror();
796 void (*set_unload_flag_ptr)(bool*);
797 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
798 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
799 set_unload_flag_ptr(&is_unloaded);
800
801 dlclose(handle);
802 ASSERT_TRUE(!is_unloaded);
803}
804
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700805TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700806 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
807 ASSERT_TRUE(handle != nullptr) << dlerror();
808 int (*get_answer)();
809 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
810 ASSERT_TRUE(get_answer != nullptr) << dlerror();
811 ASSERT_EQ(42, get_answer());
812 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700813}
814
Elliott Hughese66190d2012-12-18 15:57:55 -0800815TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700816 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700817 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700818#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700819 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
820#else
821 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
822#endif
823}
824
dimitry109040c2017-11-03 13:49:07 +0100825TEST(dlfcn, dlclose_unload) {
826 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
827 ASSERT_TRUE(handle != nullptr) << dlerror();
828 uint32_t* taxicab_number = static_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
829 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
830 EXPECT_EQ(1729U, *taxicab_number);
831 dlclose(handle);
832 // Making sure that the library has been unmapped as part of library unload
833 // process. Note that mprotect somewhat counter-intuitively returns ENOMEM in
834 // this case.
835 uintptr_t page_start = reinterpret_cast<uintptr_t>(taxicab_number) & ~(PAGE_SIZE - 1);
836 ASSERT_TRUE(mprotect(reinterpret_cast<void*>(page_start), PAGE_SIZE, PROT_NONE) != 0);
837 ASSERT_EQ(ENOMEM, errno) << strerror(errno);
838}
839
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800840static void ConcurrentDlErrorFn(std::string& error) {
841 ASSERT_TRUE(dlerror() == nullptr);
842
843 void* handle = dlopen("/child/thread", RTLD_NOW);
844 ASSERT_TRUE(handle == nullptr);
845
846 const char* err = dlerror();
847 ASSERT_TRUE(err != nullptr);
848
849 error = err;
850}
851
852TEST(dlfcn, dlerror_concurrent_buffer) {
853 void* handle = dlopen("/main/thread", RTLD_NOW);
854 ASSERT_TRUE(handle == nullptr);
855 const char* main_thread_error = dlerror();
856 ASSERT_TRUE(main_thread_error != nullptr);
857 ASSERT_SUBSTR("/main/thread", main_thread_error);
858
859 std::string child_thread_error;
860 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
861 t.join();
862 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
863
864 // Check that main thread local buffer was not modified.
865 ASSERT_SUBSTR("/main/thread", main_thread_error);
Elliott Hughes5419b942012-10-16 15:54:46 -0700866}
867
Elliott Hughese66190d2012-12-18 15:57:55 -0800868TEST(dlfcn, dlerror_concurrent) {
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800869 void* handle = dlopen("/main/thread", RTLD_NOW);
870 ASSERT_TRUE(handle == nullptr);
871
872 std::string child_thread_error;
873 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
874 t.join();
875 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
876
Elliott Hughes5419b942012-10-16 15:54:46 -0700877 const char* main_thread_error = dlerror();
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800878 ASSERT_TRUE(main_thread_error != nullptr);
Elliott Hughes5419b942012-10-16 15:54:46 -0700879 ASSERT_SUBSTR("/main/thread", main_thread_error);
880}
881
Elliott Hughese66190d2012-12-18 15:57:55 -0800882TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700883 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700884 void* self = dlopen(nullptr, RTLD_NOW);
885 ASSERT_TRUE(self != nullptr);
886 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700887
888 void* sym;
889
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700890#if defined(__BIONIC__) && !defined(__LP64__)
891 // RTLD_DEFAULT in lp32 bionic is not (void*)0
892 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700893 sym = dlsym(nullptr, "test");
894 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800895 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700896#endif
897
Elliott Hughes3b297c42012-10-11 16:08:51 -0700898 // Symbol that doesn't exist.
899 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700900 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700901 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700902
903 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700904}
905
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700906TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700907 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700908 void* self = dlopen(nullptr, RTLD_NOW);
909 ASSERT_TRUE(self != nullptr);
910 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700911
912 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700913 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700914
915 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
916 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
917
918 Dl_info info;
919 int rc = dladdr(addr, &info);
920 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
921
922 // Get the name of this executable.
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700923 const std::string executable_path = android::base::GetExecutablePath();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700924
925 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700926 char dli_realpath[PATH_MAX];
927 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700928 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700929
930 // The symbol name should be the symbol we looked up.
931 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
932
933 // The address should be the exact address of the symbol.
934 ASSERT_EQ(info.dli_saddr, sym);
935
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700936 std::vector<map_record> maps;
937 ASSERT_TRUE(Maps::parse_maps(&maps));
938
939 void* base_address = nullptr;
940 for (const map_record& rec : maps) {
941 if (executable_path == rec.pathname) {
942 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700943 break;
944 }
945 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700946
947 // The base address should be the address we were loaded at.
948 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700949
950 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700951}
952
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700953TEST(dlfcn, dlopen_executable_by_absolute_path) {
954 void* handle1 = dlopen(nullptr, RTLD_NOW);
955 ASSERT_TRUE(handle1 != nullptr) << dlerror();
956
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700957 void* handle2 = dlopen(android::base::GetExecutablePath().c_str(), RTLD_NOW);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700958 ASSERT_TRUE(handle2 != nullptr) << dlerror();
959
960#if defined(__BIONIC__)
961 ASSERT_EQ(handle1, handle2);
962#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800963 GTEST_SKIP() << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
964 "it loads a separate copy of the main executable "
965 "on dlopen by absolute path";
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700966#endif
967}
968
Victor Khimenko14b9d712017-01-25 19:59:16 +0100969#if defined (__aarch64__)
Dmytro Chystiakove712cd12019-03-29 13:49:12 -0700970#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib64/arm64/"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100971#elif defined (__arm__)
972#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm/"
973#elif defined (__i386__)
974#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86/"
975#elif defined (__x86_64__)
Dmytro Chystiakove712cd12019-03-29 13:49:12 -0700976#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib64/x86_64/"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100977#else
978#error "Unknown architecture"
979#endif
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200980#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Peter Collingbourneea11be02019-04-30 16:05:13 -0700981#define PATH_TO_BOOTSTRAP_LIBC PATH_TO_SYSTEM_LIB "bootstrap/libc.so"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100982#define ALTERNATE_PATH_TO_LIBC ALTERNATE_PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700983
984TEST(dlfcn, dladdr_libc) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800985#if defined(__GLIBC__)
986 GTEST_SKIP() << "glibc returns libc.so's ldconfig path, which is a symlink (not a realpath)";
987#endif
988
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700989 Dl_info info;
990 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
991 ASSERT_TRUE(dladdr(addr, &info) != 0);
992
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700993 char libc_realpath[PATH_MAX];
Victor Khimenko14b9d712017-01-25 19:59:16 +0100994
995 // Check if libc is in canonical path or in alternate path.
996 if (strncmp(ALTERNATE_PATH_TO_SYSTEM_LIB,
997 info.dli_fname,
998 sizeof(ALTERNATE_PATH_TO_SYSTEM_LIB) - 1) == 0) {
999 // Platform with emulated architecture. Symlink on ARC++.
1000 ASSERT_TRUE(realpath(ALTERNATE_PATH_TO_LIBC, libc_realpath) == libc_realpath);
Peter Collingbourneea11be02019-04-30 16:05:13 -07001001 } else if (strncmp(PATH_TO_BOOTSTRAP_LIBC, info.dli_fname,
1002 sizeof(PATH_TO_BOOTSTRAP_LIBC) - 1) == 0) {
1003 ASSERT_TRUE(realpath(PATH_TO_BOOTSTRAP_LIBC, libc_realpath) == libc_realpath);
Victor Khimenko14b9d712017-01-25 19:59:16 +01001004 } else {
1005 // /system/lib is symlink when this test is executed on host.
1006 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
1007 }
Dmitriy Ivanovef255922015-04-08 11:53:08 -07001008
1009 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001010 // TODO: add check for dfi_fbase
1011 ASSERT_STREQ("puts", info.dli_sname);
1012 ASSERT_EQ(addr, info.dli_saddr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001013}
1014
Elliott Hughese66190d2012-12-18 15:57:55 -08001015TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -07001016 Dl_info info;
1017
Elliott Hughes3b297c42012-10-11 16:08:51 -07001018 dlerror(); // Clear any pending errors.
1019
Elliott Hughes8e15b082012-09-26 11:44:01 -07001020 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001021 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
1022 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001023
1024 // No symbol corresponding to a stack address.
1025 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001026 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001027}
Elliott Hughes124fae92012-10-31 14:20:03 -07001028
Elliott Hughese66190d2012-12-18 15:57:55 -08001029TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001030 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001031 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
1032 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001033 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001034 void* sym = dlsym(handle, "getRandomNumber");
1035 ASSERT_TRUE(sym != nullptr) << dlerror();
1036 int (*fn)(void);
1037 fn = reinterpret_cast<int (*)(void)>(sym);
1038 EXPECT_EQ(4, fn());
1039
1040 Dl_info dlinfo;
1041 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1042
1043 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1044 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001045 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Elliott Hughes124fae92012-10-31 14:20:03 -07001046}
Elliott Hughese66190d2012-12-18 15:57:55 -08001047
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001048TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
1049 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
1050 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001051 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001052 void* sym = dlsym(handle, "getRandomNumber");
1053 ASSERT_TRUE(sym != nullptr) << dlerror();
1054 int (*fn)(void);
1055 fn = reinterpret_cast<int (*)(void)>(sym);
1056 EXPECT_EQ(4, fn());
1057
1058 Dl_info dlinfo;
1059 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1060
1061 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1062 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
1063 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
1064}
1065
Elliott Hughese66190d2012-12-18 15:57:55 -08001066TEST(dlfcn, dlopen_bad_flags) {
1067 dlerror(); // Clear any pending errors.
1068 void* handle;
1069
Elliott Hughes063525c2014-05-13 11:19:57 -07001070#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -08001071 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001072 handle = dlopen(nullptr, 0);
1073 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001074 ASSERT_SUBSTR("invalid", dlerror());
1075#endif
1076
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001077 handle = dlopen(nullptr, 0xffffffff);
1078 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001079 ASSERT_SUBSTR("invalid", dlerror());
1080
1081 // 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 -07001082 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
1083 ASSERT_TRUE(handle != nullptr);
1084 ASSERT_SUBSTR(nullptr, dlerror());
Elliott Hughese66190d2012-12-18 15:57:55 -08001085}
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001086
1087TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001088 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001089 ASSERT_TRUE(addr == nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001090}
1091
1092TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001093 void* addr = dlsym(RTLD_DEFAULT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001094 ASSERT_TRUE(addr != nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001095}
1096
1097TEST(dlfcn, rtld_next_unknown_symbol) {
1098 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001099 ASSERT_TRUE(addr == nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001100}
1101
1102TEST(dlfcn, rtld_next_known_symbol) {
1103 void* addr = dlsym(RTLD_NEXT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001104 ASSERT_TRUE(addr != nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001105}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001106
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001107// Check that RTLD_NEXT of a libc symbol works in dlopened library
1108TEST(dlfcn, rtld_next_from_library) {
dimitry153168c2018-02-20 16:51:41 +01001109 void* library_with_fclose = dlopen("libtest_check_rtld_next_from_library.so", RTLD_NOW | RTLD_GLOBAL);
Raj Mamadgi527229c2017-11-02 15:53:50 -07001110 ASSERT_TRUE(library_with_fclose != nullptr) << dlerror();
1111 void* expected_addr = dlsym(RTLD_DEFAULT, "fclose");
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001112 ASSERT_TRUE(expected_addr != nullptr) << dlerror();
Raj Mamadgi527229c2017-11-02 15:53:50 -07001113 typedef void* (*get_libc_fclose_ptr_fn_t)();
1114 get_libc_fclose_ptr_fn_t get_libc_fclose_ptr =
1115 reinterpret_cast<get_libc_fclose_ptr_fn_t>(dlsym(library_with_fclose, "get_libc_fclose_ptr"));
1116 ASSERT_TRUE(get_libc_fclose_ptr != nullptr) << dlerror();
1117 ASSERT_EQ(expected_addr, get_libc_fclose_ptr());
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001118
Raj Mamadgi527229c2017-11-02 15:53:50 -07001119 dlclose(library_with_fclose);
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001120}
1121
1122
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001123TEST(dlfcn, dlsym_weak_func) {
1124 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001125 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001126 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001127
1128 int (*weak_func)();
1129 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001130 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001131 EXPECT_EQ(42, weak_func());
1132 dlclose(handle);
1133}
1134
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001135TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001136 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
1137 ASSERT_TRUE(handle != nullptr) << dlerror();
1138 int (*weak_func)();
1139 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
1140 ASSERT_TRUE(weak_func != nullptr) << dlerror();
1141 EXPECT_EQ(6551, weak_func());
1142 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001143}
1144
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001145TEST(dlfcn, dlopen_symlink) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001146 DlfcnSymlink symlink("dlopen_symlink");
Colin Cross7da20342021-07-28 11:18:11 -07001147 const std::string symlink_name = android::base::Basename(symlink.get_symlink_path());
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001148 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001149 void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001150 ASSERT_TRUE(handle1 != nullptr);
1151 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001152 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -07001153 dlclose(handle1);
1154 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001155}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001156
1157// libtest_dlopen_from_ctor_main.so depends on
1158// libtest_dlopen_from_ctor.so which has a constructor
1159// that calls dlopen(libc...). This is to test the situation
1160// described in b/7941716.
1161TEST(dlfcn, dlopen_dlopen_from_ctor) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001162#if defined(__GLIBC__)
1163 GTEST_SKIP() << "glibc segfaults if you try to call dlopen from a constructor";
1164#endif
1165
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001166 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1167 ASSERT_TRUE(handle != nullptr) << dlerror();
1168 dlclose(handle);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001169}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001170
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001171static std::string g_fini_call_order_str;
1172
1173static void register_fini_call(const char* s) {
1174 g_fini_call_order_str += s;
1175}
1176
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001177static void test_init_fini_call_order_for(const char* libname) {
1178 g_fini_call_order_str.clear();
1179 void* handle = dlopen(libname, RTLD_NOW);
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001180 ASSERT_TRUE(handle != nullptr) << dlerror();
1181 typedef int (*get_init_order_number_t)();
1182 get_init_order_number_t get_init_order_number =
1183 reinterpret_cast<get_init_order_number_t>(dlsym(handle, "get_init_order_number"));
1184 ASSERT_EQ(321, get_init_order_number());
1185
1186 typedef void (*set_fini_callback_t)(void (*f)(const char*));
1187 set_fini_callback_t set_fini_callback =
1188 reinterpret_cast<set_fini_callback_t>(dlsym(handle, "set_fini_callback"));
1189 set_fini_callback(register_fini_call);
1190 dlclose(handle);
1191 ASSERT_EQ("(root)(child)(grandchild)", g_fini_call_order_str);
1192}
1193
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001194TEST(dlfcn, init_fini_call_order) {
1195 test_init_fini_call_order_for("libtest_init_fini_order_root.so");
1196 test_init_fini_call_order_for("libtest_init_fini_order_root2.so");
1197}
1198
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001199TEST(dlfcn, symbol_versioning_use_v1) {
1200 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1201 ASSERT_TRUE(handle != nullptr) << dlerror();
1202 typedef int (*fn_t)();
1203 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1204 ASSERT_TRUE(fn != nullptr) << dlerror();
1205 ASSERT_EQ(1, fn());
1206 dlclose(handle);
1207}
1208
1209TEST(dlfcn, symbol_versioning_use_v2) {
1210 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1211 ASSERT_TRUE(handle != nullptr) << dlerror();
1212 typedef int (*fn_t)();
1213 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1214 ASSERT_TRUE(fn != nullptr) << dlerror();
1215 ASSERT_EQ(2, fn());
1216 dlclose(handle);
1217}
1218
1219TEST(dlfcn, symbol_versioning_use_other_v2) {
1220 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1221 ASSERT_TRUE(handle != nullptr) << dlerror();
1222 typedef int (*fn_t)();
1223 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1224 ASSERT_TRUE(fn != nullptr) << dlerror();
1225 ASSERT_EQ(20, fn());
1226 dlclose(handle);
1227}
1228
1229TEST(dlfcn, symbol_versioning_use_other_v3) {
1230 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1231 ASSERT_TRUE(handle != nullptr) << dlerror();
1232 typedef int (*fn_t)();
1233 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1234 ASSERT_TRUE(fn != nullptr) << dlerror();
1235 ASSERT_EQ(3, fn());
1236 dlclose(handle);
1237}
1238
1239TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1240 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1241 ASSERT_TRUE(handle != nullptr) << dlerror();
1242 typedef int (*fn_t)();
1243 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1244 ASSERT_TRUE(fn != nullptr) << dlerror();
1245 ASSERT_EQ(3, fn()); // the default version is 3
1246 dlclose(handle);
1247}
1248
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001249TEST(dlfcn, dlvsym_smoke) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001250#if !defined(ANDROID_HOST_MUSL)
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001251 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1252 ASSERT_TRUE(handle != nullptr) << dlerror();
1253 typedef int (*fn_t)();
1254
1255 {
1256 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1257 ASSERT_TRUE(fn == nullptr);
1258 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1259 }
1260
1261 {
1262 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1263 ASSERT_TRUE(fn != nullptr) << dlerror();
1264 ASSERT_EQ(2, fn());
1265 }
1266
1267 dlclose(handle);
Colin Cross7da20342021-07-28 11:18:11 -07001268#else
1269 GTEST_SKIP() << "musl doesn't have dlvsym";
1270#endif
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001271}
1272
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001273// This preempts the implementation from libtest_versioned_lib.so
1274extern "C" int version_zero_function() {
1275 return 0;
1276}
1277
1278// This preempts the implementation from libtest_versioned_uselibv*.so
1279extern "C" int version_zero_function2() {
1280 return 0;
1281}
Evgenii Stepanov68650822015-06-10 13:38:39 -07001282
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001283TEST(dlfcn, dt_runpath_smoke) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001284 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1285 ASSERT_TRUE(handle != nullptr) << dlerror();
1286
1287 typedef void *(* dlopen_b_fn)();
1288 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1289 ASSERT_TRUE(fn != nullptr) << dlerror();
1290
1291 void *p = fn();
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001292 ASSERT_TRUE(p != nullptr);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001293
1294 dlclose(handle);
1295}
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001296
Dimitry Ivanovdb6ab3d2017-06-27 11:02:51 -07001297TEST(dlfcn, dt_runpath_absolute_path) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001298 std::string libpath = GetTestlibRoot() + "/libtest_dt_runpath_d.so";
Dimitry Ivanovdb6ab3d2017-06-27 11:02:51 -07001299 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1300 ASSERT_TRUE(handle != nullptr) << dlerror();
1301
1302 typedef void *(* dlopen_b_fn)();
1303 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1304 ASSERT_TRUE(fn != nullptr) << dlerror();
1305
1306 void *p = fn();
1307 ASSERT_TRUE(p != nullptr);
1308
1309 dlclose(handle);
1310}
1311
dimitry55547db2018-05-25 14:17:37 +02001312static void test_dlclose_after_thread_local_dtor(const char* library_name) {
dimitry06016f22018-01-05 11:39:28 +01001313 bool is_dtor_triggered = false;
1314
1315 auto f = [](void* handle, bool* is_dtor_triggered) {
1316 typedef void (*fn_t)(bool*);
1317 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1318 ASSERT_TRUE(fn != nullptr) << dlerror();
1319
1320 fn(is_dtor_triggered);
1321
1322 ASSERT_TRUE(!*is_dtor_triggered);
1323 };
1324
dimitry55547db2018-05-25 14:17:37 +02001325 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001326 ASSERT_TRUE(handle == nullptr);
1327
dimitry55547db2018-05-25 14:17:37 +02001328 handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001329 ASSERT_TRUE(handle != nullptr) << dlerror();
1330
1331 std::thread t(f, handle, &is_dtor_triggered);
1332 t.join();
1333
1334 ASSERT_TRUE(is_dtor_triggered);
1335 dlclose(handle);
1336
dimitry55547db2018-05-25 14:17:37 +02001337 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001338 ASSERT_TRUE(handle == nullptr);
1339}
1340
dimitry55547db2018-05-25 14:17:37 +02001341TEST(dlfcn, dlclose_after_thread_local_dtor) {
1342 test_dlclose_after_thread_local_dtor("libtest_thread_local_dtor.so");
1343}
1344
1345TEST(dlfcn, dlclose_after_thread_local_dtor_indirect) {
1346 test_dlclose_after_thread_local_dtor("libtest_indirect_thread_local_dtor.so");
1347}
1348
1349static void test_dlclose_before_thread_local_dtor(const char* library_name) {
dimitry06016f22018-01-05 11:39:28 +01001350 bool is_dtor_triggered = false;
1351
dimitry55547db2018-05-25 14:17:37 +02001352 auto f = [library_name](bool* is_dtor_triggered) {
1353 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001354 ASSERT_TRUE(handle == nullptr);
1355
dimitry55547db2018-05-25 14:17:37 +02001356 handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001357 ASSERT_TRUE(handle != nullptr) << dlerror();
1358
1359 typedef void (*fn_t)(bool*);
1360 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1361 ASSERT_TRUE(fn != nullptr) << dlerror();
1362
1363 fn(is_dtor_triggered);
1364
1365 dlclose(handle);
1366
1367 ASSERT_TRUE(!*is_dtor_triggered);
1368
1369 // Since we have thread_atexit dtors associated with handle - the library should
1370 // still be availabe.
dimitry55547db2018-05-25 14:17:37 +02001371 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001372 ASSERT_TRUE(handle != nullptr) << dlerror();
1373 dlclose(handle);
1374 };
1375
dimitry55547db2018-05-25 14:17:37 +02001376 void* handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001377 ASSERT_TRUE(handle != nullptr) << dlerror();
1378 dlclose(handle);
1379
dimitry55547db2018-05-25 14:17:37 +02001380 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001381 ASSERT_TRUE(handle == nullptr);
1382
1383 std::thread t(f, &is_dtor_triggered);
1384 t.join();
1385#if defined(__BIONIC__)
1386 // ld-android.so unloads unreferenced libraries on pthread_exit()
1387 ASSERT_TRUE(is_dtor_triggered);
dimitry55547db2018-05-25 14:17:37 +02001388 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001389 ASSERT_TRUE(handle == nullptr);
1390#else
1391 // GLIBC does not unload libraries with ref_count = 0 on pthread_exit
1392 ASSERT_TRUE(is_dtor_triggered);
dimitry55547db2018-05-25 14:17:37 +02001393 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1394 ASSERT_TRUE(handle != nullptr) << dlerror();
1395#endif
1396}
1397
1398TEST(dlfcn, dlclose_before_thread_local_dtor) {
1399 test_dlclose_before_thread_local_dtor("libtest_thread_local_dtor.so");
1400}
1401
1402TEST(dlfcn, dlclose_before_thread_local_dtor_indirect) {
1403 test_dlclose_before_thread_local_dtor("libtest_indirect_thread_local_dtor.so");
1404}
1405
1406TEST(dlfcn, dlclose_before_thread_local_dtor_multiple_dsos) {
1407 const constexpr char* library_name = "libtest_indirect_thread_local_dtor.so";
1408
1409 bool is_dtor1_triggered = false;
1410 bool is_dtor2_triggered = false;
1411
1412 std::mutex mtx;
1413 std::condition_variable cv;
1414 void* library_handle = nullptr;
1415 bool thread1_dlopen_complete = false;
1416 bool thread2_thread_local_dtor_initialized = false;
1417 bool thread1_complete = false;
1418
1419 auto f1 = [&]() {
1420 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1421 ASSERT_TRUE(handle == nullptr);
1422
1423 handle = dlopen(library_name, RTLD_NOW);
1424 ASSERT_TRUE(handle != nullptr) << dlerror();
1425 std::unique_lock<std::mutex> lock(mtx);
1426 thread1_dlopen_complete = true;
1427 library_handle = handle;
1428 lock.unlock();
1429 cv.notify_one();
1430
1431 typedef void (*fn_t)(bool*);
1432 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1433 ASSERT_TRUE(fn != nullptr) << dlerror();
1434
1435 fn(&is_dtor1_triggered);
1436
1437 lock.lock();
1438 cv.wait(lock, [&] { return thread2_thread_local_dtor_initialized; });
1439 lock.unlock();
1440
1441 dlclose(handle);
1442
1443 ASSERT_TRUE(!is_dtor1_triggered);
1444
1445 // Since we have thread_atexit dtors associated with handle - the library should
1446 // still be availabe.
1447 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1448 ASSERT_TRUE(handle != nullptr) << dlerror();
1449 dlclose(handle);
1450 };
1451
1452 auto f2 = [&]() {
1453 std::unique_lock<std::mutex> lock(mtx);
1454 cv.wait(lock, [&] { return thread1_dlopen_complete; });
1455 void* handle = library_handle;
1456 lock.unlock();
1457
1458 typedef void (*fn_t)(bool*);
1459 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable2"));
1460 ASSERT_TRUE(fn != nullptr) << dlerror();
1461
1462 fn(&is_dtor2_triggered);
1463
1464 lock.lock();
1465 thread2_thread_local_dtor_initialized = true;
1466 lock.unlock();
1467 cv.notify_one();
1468
1469 lock.lock();
1470 cv.wait(lock, [&] { return thread1_complete; });
1471 lock.unlock();
1472
1473 ASSERT_TRUE(!is_dtor2_triggered);
1474 };
1475
1476 void* handle = dlopen(library_name, RTLD_NOW);
1477 ASSERT_TRUE(handle != nullptr) << dlerror();
1478 dlclose(handle);
1479
1480 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1481 ASSERT_TRUE(handle == nullptr);
1482
1483 std::thread t1(f1);
1484 std::thread t2(f2);
1485 t1.join();
1486 ASSERT_TRUE(is_dtor1_triggered);
1487 ASSERT_TRUE(!is_dtor2_triggered);
1488
1489 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1490 ASSERT_TRUE(handle != nullptr) << dlerror();
1491 dlclose(handle);
1492
1493 std::unique_lock<std::mutex> lock(mtx);
1494 thread1_complete = true;
1495 lock.unlock();
1496 cv.notify_one();
1497
1498 t2.join();
1499 ASSERT_TRUE(is_dtor2_triggered);
1500
1501#if defined(__BIONIC__)
1502 // ld-android.so unloads unreferenced libraries on pthread_exit()
1503 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1504 ASSERT_TRUE(handle == nullptr);
1505#else
1506 // GLIBC does not unload libraries with ref_count = 0 on pthread_exit
1507 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001508 ASSERT_TRUE(handle != nullptr) << dlerror();
1509#endif
1510}
1511
Elliott Hughes8ad40932017-06-15 15:12:29 -07001512TEST(dlfcn, RTLD_macros) {
1513#if !defined(RTLD_LOCAL)
1514#error no RTLD_LOCAL
1515#elif !defined(RTLD_LAZY)
1516#error no RTLD_LAZY
1517#elif !defined(RTLD_NOW)
1518#error no RTLD_NOW
1519#elif !defined(RTLD_NOLOAD)
1520#error no RTLD_NOLOAD
1521#elif !defined(RTLD_GLOBAL)
1522#error no RTLD_GLOBAL
1523#elif !defined(RTLD_NODELETE)
1524#error no RTLD_NODELETE
1525#endif
1526}
1527
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001528// Bionic specific tests
1529#if defined(__BIONIC__)
1530
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001531#if defined(__arm__)
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001532
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001533void validate_compatibility_of_native_library(const std::string& soname, const std::string& path) {
1534 // Grab the dynamic section in text form...
1535 ExecTestHelper eth;
1536 eth.SetArgs({"readelf", "-dW", path.c_str(), nullptr});
1537 eth.Run([&]() { execvpe("readelf", eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
1538 std::string output = eth.GetOutput();
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001539
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001540 // Check that there *is* a legacy DT_HASH (not just a GNU hash)...
1541 ASSERT_TRUE(std::regex_search(output, std::regex("\\(HASH\\)"))) << output;
1542 // Check that there is no DT_ANDROID_REL or DT_ANDROID_RELA...
1543 ASSERT_FALSE(std::regex_search(output, std::regex("\\(ANDROID_REL\\)"))) << output;
1544 ASSERT_FALSE(std::regex_search(output, std::regex("\\(ANDROID_RELA\\)"))) << output;
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001545
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001546 // Check that we have regular non-packed relocations.
1547 // libdl.so is simple enough that it doesn't have any relocations.
1548 ASSERT_TRUE(std::regex_search(output, std::regex("\\(RELA?\\)")) || soname == "libdl.so")
1549 << output;
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001550}
1551
Ryan Prichard470b6662018-03-27 22:10:55 -07001552void validate_compatibility_of_native_library(const std::string& soname) {
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001553 // On the systems with emulation system libraries would be of different
1554 // architecture. Try to use alternate paths first.
1555 std::string path = std::string(ALTERNATE_PATH_TO_SYSTEM_LIB) + soname;
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001556 if (access(path.c_str(), R_OK) != 0) {
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001557 path = std::string(PATH_TO_SYSTEM_LIB) + soname;
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001558 ASSERT_EQ(0, access(path.c_str(), R_OK));
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001559 }
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001560 validate_compatibility_of_native_library(soname, path);
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001561}
1562
1563// This is a test for app compatibility workaround for arm apps
1564// affected by http://b/24465209
1565TEST(dlext, compat_elf_hash_and_relocation_tables) {
1566 validate_compatibility_of_native_library("libc.so");
1567 validate_compatibility_of_native_library("liblog.so");
1568 validate_compatibility_of_native_library("libstdc++.so");
1569 validate_compatibility_of_native_library("libdl.so");
1570 validate_compatibility_of_native_library("libm.so");
1571 validate_compatibility_of_native_library("libz.so");
1572 validate_compatibility_of_native_library("libjnigraphics.so");
1573}
1574
1575#endif // defined(__arm__)
1576
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001577TEST(dlfcn, dlopen_invalid_rw_load_segment) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001578 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-rw_load_segment.so";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001579 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1580 ASSERT_TRUE(handle == nullptr);
Elliott Hughes9076b0c2018-02-28 11:29:45 -08001581 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W+E load segments are not allowed";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001582 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1583}
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001584
1585TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001586 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-unaligned_shdr_offset.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001587
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001588 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1589 ASSERT_TRUE(handle == nullptr);
1590 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
1591 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1592}
1593
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001594TEST(dlfcn, dlopen_invalid_zero_shentsize) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001595 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-zero_shentsize.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001596
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001597 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1598 ASSERT_TRUE(handle == nullptr);
1599 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x";
1600 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1601}
1602
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001603TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001604 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-zero_shstrndx.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001605
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001606 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1607 ASSERT_TRUE(handle == nullptr);
1608 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx";
1609 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1610}
1611
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001612TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001613 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-empty_shdr_table.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001614
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001615 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1616 ASSERT_TRUE(handle == nullptr);
1617 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers";
1618 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1619}
1620
Dimitry Ivanov46230442016-08-15 13:40:53 -07001621TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001622 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-zero_shdr_table_offset.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001623
Dimitry Ivanov46230442016-08-15 13:40:53 -07001624 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1625 ASSERT_TRUE(handle == nullptr);
1626 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/";
1627 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1628}
1629
Dimitry Ivanov55958342016-08-15 14:06:04 -07001630TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001631 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-zero_shdr_table_content.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001632
Dimitry Ivanov55958342016-08-15 14:06:04 -07001633 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1634 ASSERT_TRUE(handle == nullptr);
1635 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
1636 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1637}
1638
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001639TEST(dlfcn, dlopen_invalid_textrels) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001640 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-textrels.so";
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001641
1642 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1643 ASSERT_TRUE(handle == nullptr);
1644 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1645 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1646}
1647
1648TEST(dlfcn, dlopen_invalid_textrels2) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001649 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-textrels2.so";
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001650
1651 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1652 ASSERT_TRUE(handle == nullptr);
1653 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1654 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1655}
1656
Jiyong Park01162f22017-10-16 15:31:09 +09001657TEST(dlfcn, dlopen_df_1_global) {
1658 void* handle = dlopen("libtest_dlopen_df_1_global.so", RTLD_NOW);
1659 ASSERT_TRUE(handle != nullptr) << dlerror();
1660}
1661
Peter Collingbourneb39cb3c2019-03-01 13:12:49 -08001662TEST(dlfcn, segment_gap) {
1663 void* handle = dlopen("libsegment_gap_outer.so", RTLD_NOW);
1664 ASSERT_TRUE(handle != nullptr) << dlerror();
1665
1666 auto get_inner = reinterpret_cast<void* (*)()>(dlsym(handle, "get_inner"));
1667 void* inner = get_inner();
1668 (void)inner;
1669
1670#if __arm__
1671 int count;
1672 _Unwind_Ptr outer_exidx = dl_unwind_find_exidx(reinterpret_cast<_Unwind_Ptr>(get_inner), &count);
1673 _Unwind_Ptr inner_exidx = dl_unwind_find_exidx(reinterpret_cast<_Unwind_Ptr>(inner), &count);
1674 EXPECT_NE(0u, outer_exidx);
1675 EXPECT_NE(0u, inner_exidx);
1676 EXPECT_NE(inner_exidx, outer_exidx);
1677#endif
1678
1679 Dl_info info;
1680 int rc = dladdr(inner, &info);
1681 ASSERT_NE(rc, 0);
1682
1683 EXPECT_NE(nullptr, strstr(info.dli_fname, "libsegment_gap_inner.so"));
1684}
1685
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001686#endif