blob: 939c092b5598a146c8fb3047310ee397662013f2 [file] [log] [blame]
jeffhaoacf5aa72012-09-12 17:25:30 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <dlfcn.h>
Elliott Hughes1ff7be02022-01-13 15:43:23 -080020#include <elf.h>
Elliott Hughes8e15b082012-09-26 11:44:01 -070021#include <limits.h>
Elliott Hughes1ff7be02022-01-13 15:43:23 -080022#include <link.h>
Elliott Hughes8e15b082012-09-26 11:44:01 -070023#include <stdint.h>
Colin Cross14d15072021-08-16 16:35:27 -070024#include <stdio.h>
Dimitry Ivanov708589f2016-09-19 10:50:28 -070025#include <string.h>
Colin Cross4c5595c2021-08-16 15:51:59 -070026#include <sys/cdefs.h>
dimitryb9555a92017-10-11 18:04:05 +020027#if __has_include(<sys/auxv.h>)
28#include <sys/auxv.h>
29#endif
dimitry109040c2017-11-03 13:49:07 +010030#include <sys/user.h>
Elliott Hughes8e15b082012-09-26 11:44:01 -070031
32#include <string>
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -080033#include <thread>
jeffhaoacf5aa72012-09-12 17:25:30 -070034
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -070035#include <android-base/file.h>
Elliott Hughes3f73ea62022-10-19 16:16:54 +000036#include <android-base/macros.h>
Tom Cherryb8ab6182017-04-05 16:20:29 -070037#include <android-base/scopeguard.h>
38
Dimitry Ivanov927877c2016-09-21 11:17:13 -070039#include "gtest_globals.h"
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -070040#include "gtest_utils.h"
Dimitry Ivanov708589f2016-09-19 10:50:28 -070041#include "dlfcn_symlink_support.h"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070042#include "utils.h"
43
Elliott Hughes3b297c42012-10-11 16:08:51 -070044#define ASSERT_SUBSTR(needle, haystack) \
45 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
46
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070047
Elliott Hughes1728b232014-05-14 10:02:03 -070048static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070049extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070050 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070051}
52
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070053static int g_ctor_function_called = 0;
Dimitry Ivanov55437462016-07-20 15:33:07 -070054static int g_ctor_argc = 0;
55static char** g_ctor_argv = reinterpret_cast<char**>(0xDEADBEEF);
56static char** g_ctor_envp = g_ctor_envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070057
Dimitry Ivanov55437462016-07-20 15:33:07 -070058extern "C" void ctor_function(int argc, char** argv, char** envp) __attribute__ ((constructor));
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070059
Dimitry Ivanov55437462016-07-20 15:33:07 -070060extern "C" void ctor_function(int argc, char** argv, char** envp) {
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070061 g_ctor_function_called = 17;
Dimitry Ivanov55437462016-07-20 15:33:07 -070062 g_ctor_argc = argc;
63 g_ctor_argv = argv;
64 g_ctor_envp = envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070065}
66
67TEST(dlfcn, ctor_function_call) {
68 ASSERT_EQ(17, g_ctor_function_called);
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -070069 ASSERT_TRUE(g_ctor_argc = GetArgc());
70 ASSERT_TRUE(g_ctor_argv = GetArgv());
71 ASSERT_TRUE(g_ctor_envp = GetEnvp());
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070072}
73
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070074TEST(dlfcn, dlsym_in_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070075 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070076 void* self = dlopen(nullptr, RTLD_NOW);
77 ASSERT_TRUE(self != nullptr);
78 ASSERT_TRUE(dlerror() == nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070079
80 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070081 ASSERT_TRUE(sym != nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070082
83 void (*function)() = reinterpret_cast<void(*)()>(sym);
84
Elliott Hughes1728b232014-05-14 10:02:03 -070085 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070086 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070087 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070088
89 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070090}
Elliott Hughes8e15b082012-09-26 11:44:01 -070091
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070092TEST(dlfcn, dlsym_from_sofile) {
93 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL);
94 ASSERT_TRUE(handle != nullptr) << dlerror();
95
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070096 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070097 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
98 ASSERT_TRUE(symbol == nullptr);
99 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
100
101 typedef int* (*fn_t)();
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700102 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
103 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
104 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700105
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700106 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700107 ASSERT_TRUE(ptr != nullptr) << dlerror();
108 ASSERT_EQ(42, *ptr);
109
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700110 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
111 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
112 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
113
114 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
115 ASSERT_TRUE(ptr != nullptr) << dlerror();
116 ASSERT_EQ(44, *ptr);
117
118 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
119 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
120 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
121
122 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
123 ASSERT_TRUE(ptr != nullptr) << dlerror();
124 ASSERT_EQ(43, *ptr);
125
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700126 dlclose(handle);
127}
128
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700129TEST(dlfcn, dlsym_from_sofile_with_preload) {
130 void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL);
131 ASSERT_TRUE(preload != nullptr) << dlerror();
132
133 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
134 ASSERT_TRUE(handle != nullptr) << dlerror();
135
136 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
137 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
138 ASSERT_TRUE(symbol == nullptr);
139 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
140
141 typedef int* (*fn_t)();
142 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
143 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
144 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
145
146 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
147 ASSERT_TRUE(ptr != nullptr) << dlerror();
148 ASSERT_EQ(42, *ptr);
149
150 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
151 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
152 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
153
154 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
155 ASSERT_TRUE(ptr != nullptr) << dlerror();
156 ASSERT_EQ(44, *ptr);
157
158 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
159 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
160 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
161
162 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
163 ASSERT_TRUE(ptr != nullptr) << dlerror();
164 ASSERT_EQ(43, *ptr);
165
166 dlclose(handle);
167 dlclose(preload);
168}
169
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700170TEST(dlfcn, dlsym_handle_global_sym) {
171 // check that we do not look into global group
172 // when looking up symbol by handle
173 void* handle = dlopen("libtest_empty.so", RTLD_NOW);
174 dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL);
175 void* sym = dlsym(handle, "getRandomNumber");
176 ASSERT_TRUE(sym == nullptr);
177 ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror());
178
179 sym = dlsym(handle, "DlSymTestFunction");
180 ASSERT_TRUE(sym == nullptr);
181 ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror());
182 dlclose(handle);
183}
184
Dimitry Ivanovd5b578a2016-12-14 15:16:56 -0800185TEST(dlfcn, dlsym_handle_empty_symbol) {
186 // check that dlsym of an empty symbol fails (see http://b/33530622)
187 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW);
188 ASSERT_TRUE(handle != nullptr) << dlerror();
189 void* sym = dlsym(handle, "");
190 ASSERT_TRUE(sym == nullptr);
191 ASSERT_SUBSTR("undefined symbol: ", dlerror());
192 dlclose(handle);
193}
194
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700195TEST(dlfcn, dlsym_with_dependencies) {
196 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700197 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700198 dlerror();
199 // This symbol is in DT_NEEDED library.
200 void* sym = dlsym(handle, "getRandomNumber");
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700201 ASSERT_TRUE(sym != nullptr) << dlerror();
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700202 int (*fn)(void);
203 fn = reinterpret_cast<int (*)(void)>(sym);
204 EXPECT_EQ(4, fn());
205 dlclose(handle);
206}
207
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700208TEST(dlfcn, dlopen_noload) {
209 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700210 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700211 handle = dlopen("libtest_simple.so", RTLD_NOW);
212 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700213 ASSERT_TRUE(handle != nullptr);
214 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700215 ASSERT_TRUE(handle == handle2);
216 ASSERT_EQ(0, dlclose(handle));
217 ASSERT_EQ(0, dlclose(handle2));
218}
219
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700220TEST(dlfcn, dlopen_by_soname) {
221 static const char* soname = "libdlext_test_soname.so";
222 static const char* filename = "libdlext_test_different_soname.so";
223 // 1. Make sure there is no library with soname in default search path
224 void* handle = dlopen(soname, RTLD_NOW);
225 ASSERT_TRUE(handle == nullptr);
226
227 // 2. Load a library using filename
228 handle = dlopen(filename, RTLD_NOW);
229 ASSERT_TRUE(handle != nullptr) << dlerror();
230
231 // 3. Find library by soname
232 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
233 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
234 ASSERT_EQ(handle, handle_soname);
235
236 // 4. RTLD_NOLOAD should still work with filename
237 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
238 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
239 ASSERT_EQ(handle, handle_filename);
240
241 dlclose(handle_filename);
242 dlclose(handle_soname);
243 dlclose(handle);
244}
245
dimitryc18de1b2017-09-26 14:31:35 +0200246TEST(dlfcn, dlopen_vdso) {
dimitryb9555a92017-10-11 18:04:05 +0200247#if __has_include(<sys/auxv.h>)
248 if (getauxval(AT_SYSINFO_EHDR) == 0) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800249 GTEST_SKIP() << "getauxval(AT_SYSINFO_EHDR) == 0, skipping this test";
dimitryb9555a92017-10-11 18:04:05 +0200250 }
251#endif
Elliott Hughesda1bb112018-02-16 10:05:08 -0800252
253 const char* vdso_name = "linux-vdso.so.1";
254#if defined(__i386__)
255 vdso_name = "linux-gate.so.1";
256#endif
257 void* handle = dlopen(vdso_name, RTLD_NOW);
dimitryc18de1b2017-09-26 14:31:35 +0200258 ASSERT_TRUE(handle != nullptr) << dlerror();
259 dlclose(handle);
260}
261
Elliott Hughes82c90722022-02-17 11:55:49 -0800262// HWASan uses an ifunc to describe the location of its shadow memory,
263// so even though it's an unusual case, Android needs to support
264// "ifunc variables".
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700265TEST(dlfcn, ifunc_variable) {
266 typedef const char* (*fn_ptr)();
267
268 // ifunc's choice depends on whether IFUNC_CHOICE has a value
269 // first check the set case
270 setenv("IFUNC_CHOICE", "set", 1);
271 // preload libtest_ifunc_variable_impl.so
272 void* handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
273 void* handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
274 ASSERT_TRUE(handle != nullptr) << dlerror();
275 const char** foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
276 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
277 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
278 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
279 ASSERT_EQ(strncmp("set", *foo_ptr, 3), 0);
280 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
281 dlclose(handle);
282 dlclose(handle_impl);
283
284 // then check the unset case
285 unsetenv("IFUNC_CHOICE");
286 handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
287 handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
288 ASSERT_TRUE(handle != nullptr) << dlerror();
289 foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
290 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
291 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
292 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
293 ASSERT_EQ(strncmp("unset", *foo_ptr, 5), 0);
294 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
295 dlclose(handle);
296 dlclose(handle_impl);
297}
298
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700299TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700300 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700301
302 // ifunc's choice depends on whether IFUNC_CHOICE has a value
303 // first check the set case
304 setenv("IFUNC_CHOICE", "set", 1);
305 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700306 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700307 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
308 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700309 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
310 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700311 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
312 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700313 dlclose(handle);
314
315 // then check the unset case
316 unsetenv("IFUNC_CHOICE");
317 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700318 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700319 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
320 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700321 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
322 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700323 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700324 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700325 dlclose(handle);
326}
327
328TEST(dlfcn, ifunc_ctor_call) {
329 typedef const char* (*fn_ptr)();
330
331 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700332 ASSERT_TRUE(handle != nullptr) << dlerror();
333 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
334 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
335 ASSERT_STREQ("false", is_ctor_called());
336
337 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
338 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700339 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700340 dlclose(handle);
341}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700342
343TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
344 typedef const char* (*fn_ptr)();
345
346 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
347 ASSERT_TRUE(handle != nullptr) << dlerror();
348 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
349 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
350 ASSERT_STREQ("false", is_ctor_called());
351
352 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
353 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
354 ASSERT_STREQ("true", is_ctor_called());
355 dlclose(handle);
356}
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700357
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700358TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
359 // This is the structure of the test library and
360 // its dt_needed libraries
361 // libtest_relo_check_dt_needed_order.so
362 // |
363 // +-> libtest_relo_check_dt_needed_order_1.so
364 // |
365 // +-> libtest_relo_check_dt_needed_order_2.so
366 //
367 // The root library references relo_test_get_answer_lib - which is defined
368 // in both dt_needed libraries, the correct relocation should
369 // use the function defined in libtest_relo_check_dt_needed_order_1.so
370 void* handle = nullptr;
Tom Cherryb8ab6182017-04-05 16:20:29 -0700371 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700372
373 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
374 ASSERT_TRUE(handle != nullptr) << dlerror();
375
376 typedef int (*fn_t) (void);
377 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
378 ASSERT_TRUE(fn != nullptr) << dlerror();
379 ASSERT_EQ(1, fn());
380}
381
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700382TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700383 // Here is how the test library and its dt_needed
384 // libraries are arranged
385 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700386 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700387 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700388 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700389 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700390 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700391 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700392 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700393 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700394 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700395 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700396 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700397 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700398 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700399 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700400 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700401 //
402 // load order should be (1, 2, 3, a, b, d)
403 //
404 // get_answer() is defined in (2, 3, a, b, c)
405 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700406 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700407 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700408 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
409 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700410 typedef int (*fn_t) (void);
411 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700412 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700413 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700414 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700415 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700416
417 ASSERT_EQ(42, fn());
418 ASSERT_EQ(43, fn2());
419 dlclose(handle);
420}
421
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700422TEST(dlfcn, dlopen_check_order_reloc_siblings) {
423 // This is how this one works:
424 // we lookup and call get_answer which is defined in '_2.so'
425 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
426 // the correct _impl() is implemented by '_a.so';
427 //
428 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
429 //
430 // Here is the picture:
431 //
432 // libtest_check_order_reloc_siblings.so
433 // |
434 // +-> ..._1.so <- empty
435 // | |
436 // | +-> ..._a.so <- exports correct answer_impl()
437 // | |
438 // | +-> ..._b.so <- every other letter exporting incorrect one.
439 // |
440 // +-> ..._2.so <- empty
441 // | |
442 // | +-> ..._c.so
443 // | |
444 // | +-> ..._d.so
445 // |
446 // +-> ..._3.so <- empty
447 // |
448 // +-> ..._e.so
449 // |
450 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
451 // implements incorrect get_answer_impl()
452
453 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
454 ASSERT_TRUE(handle == nullptr);
455#ifdef __BIONIC__
456 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
457 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
458#endif
459
460 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
461 ASSERT_TRUE(handle != nullptr) << dlerror();
462
463 typedef int (*fn_t) (void);
464 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
465 ASSERT_TRUE(fn != nullptr) << dlerror();
466 ASSERT_EQ(42, fn());
467
468 ASSERT_EQ(0, dlclose(handle));
469}
470
471TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
472 // This test uses the same library as dlopen_check_order_reloc_siblings.
473 // Unlike dlopen_check_order_reloc_siblings it preloads
474 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
475 // dlopen(libtest_check_order_reloc_siblings.so)
476
477 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
478 ASSERT_TRUE(handle == nullptr);
479 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
480 ASSERT_TRUE(handle == nullptr);
481
482 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
483 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
484
485 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
486 ASSERT_TRUE(handle != nullptr) << dlerror();
487
488 ASSERT_EQ(0, dlclose(handle_for_1));
489
490 typedef int (*fn_t) (void);
491 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
492 ASSERT_TRUE(fn != nullptr) << dlerror();
493 ASSERT_EQ(42, fn());
494
495 ASSERT_EQ(0, dlclose(handle));
496}
497
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800498TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
499 // This is how this one works:
500 // we lookup and call grandchild_get_answer which is defined in '_2.so'
501 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
502 // the correct _impl() is implemented by '_c_1.so';
503 //
504 // Here is the picture of subtree:
505 //
506 // libtest_check_order_reloc_siblings.so
507 // |
508 // +-> ..._2.so <- grandchild_get_answer()
509 // |
510 // +-> ..._c.so <- empty
511 // | |
512 // | +-> _c_1.so <- exports correct answer_impl()
513 // | |
514 // | +-> _c_2.so <- exports incorrect answer_impl()
515 // |
516 // +-> ..._d.so <- empty
517
518 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
519 ASSERT_TRUE(handle == nullptr);
520#ifdef __BIONIC__
521 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
522 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
523#endif
524
525 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
526 ASSERT_TRUE(handle != nullptr) << dlerror();
527
528 typedef int (*fn_t) (void);
529 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
530 ASSERT_TRUE(fn != nullptr) << dlerror();
531 ASSERT_EQ(42, fn());
532
533 ASSERT_EQ(0, dlclose(handle));
534}
535
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700536TEST(dlfcn, dlopen_check_order_reloc_nephew) {
537 // This is how this one works:
538 // we lookup and call nephew_get_answer which is defined in '_2.so'
539 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
540 // the correct _impl() is implemented by '_a.so';
541 //
542 // Here is the picture:
543 //
544 // libtest_check_order_reloc_siblings.so
545 // |
546 // +-> ..._1.so <- empty
547 // | |
548 // | +-> ..._a.so <- exports correct answer_impl()
549 // | |
550 // | +-> ..._b.so <- every other letter exporting incorrect one.
551 // |
552 // +-> ..._2.so <- empty
553 // | |
554 // | +-> ..._c.so
555 // | |
556 // | +-> ..._d.so
557 // |
558 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
559 // |
560 // +-> ..._e.so
561 // |
562 // +-> ..._f.so
563
564 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
565 ASSERT_TRUE(handle == nullptr);
566#ifdef __BIONIC__
567 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
568 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
569#endif
570
571 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
572 ASSERT_TRUE(handle != nullptr) << dlerror();
573
574 typedef int (*fn_t) (void);
575 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
576 ASSERT_TRUE(fn != nullptr) << dlerror();
577 ASSERT_EQ(42, fn());
578
579 ASSERT_EQ(0, dlclose(handle));
580}
581
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800582TEST(dlfcn, check_unload_after_reloc) {
583 // This is how this one works:
584 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
585 // |
586 // +-> libtest_two_parents_child
587 //
588 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
589 // |
590 // +-> libtest_two_parents_child
591 //
592 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
593 // as a second step it dlopens parent2 and dlcloses parent1...
594
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700595 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
596 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800597
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700598 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
599 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800600
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700601 typedef int (*fn_t) (void);
602 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
603 ASSERT_TRUE(fn != nullptr) << dlerror();
604 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800605
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700606 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800607
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700608 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
609 ASSERT_TRUE(handle != nullptr);
610 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800611
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700612 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
613 ASSERT_TRUE(fn != nullptr) << dlerror();
614 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800615
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700616 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800617
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700618 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
619 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800620}
621
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700622extern "C" int check_order_reloc_root_get_answer_impl() {
623 return 42;
624}
625
626TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
627 // This is how this one works:
628 // we lookup and call get_answer3 which is defined in 'root.so'
629 // and in turn calls external root_get_answer_impl() defined in _2.so and
630 // above the correct _impl() is one in the executable.
631 //
632 // libtest_check_order_reloc_root.so
633 // |
634 // +-> ..._1.so <- empty
635 // |
636 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
637 //
638
639 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
640 ASSERT_TRUE(handle == nullptr);
641#ifdef __BIONIC__
642 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
643 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
644#endif
645
646 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
647 ASSERT_TRUE(handle != nullptr) << dlerror();
648
649 typedef int (*fn_t) (void);
650 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
651 ASSERT_TRUE(fn != nullptr) << dlerror();
652 ASSERT_EQ(42, fn());
653
654 ASSERT_EQ(0, dlclose(handle));
655}
656
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700657TEST(dlfcn, dlopen_check_rtld_local) {
658 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
659 ASSERT_TRUE(sym == nullptr);
660
661 // implicit RTLD_LOCAL
662 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
663 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
664 ASSERT_TRUE(sym == nullptr);
665 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
666 sym = dlsym(handle, "dlopen_testlib_simple_func");
667 ASSERT_TRUE(sym != nullptr);
668 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
669 dlclose(handle);
670
671 // explicit RTLD_LOCAL
672 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
673 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
674 ASSERT_TRUE(sym == nullptr);
675 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
676 sym = dlsym(handle, "dlopen_testlib_simple_func");
677 ASSERT_TRUE(sym != nullptr);
678 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
679 dlclose(handle);
680}
681
682TEST(dlfcn, dlopen_check_rtld_global) {
683 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
684 ASSERT_TRUE(sym == nullptr);
685
686 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700687 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700688 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
689 ASSERT_TRUE(sym != nullptr) << dlerror();
690 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
691 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700692
693 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
694 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
695 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700696
697 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
698 // shared libraries after symbol was not found in the main executable
699 // and dependent libraries.
700 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
701 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
702 ASSERT_TRUE(sym != nullptr) << dlerror();
703
704 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700705}
706
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700707// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
708// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
709// libtest_with_dependency_loop_a.so
710TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700711 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
712 ASSERT_TRUE(handle != nullptr) << dlerror();
713 void* f = dlsym(handle, "dlopen_test_loopy_function");
714 ASSERT_TRUE(f != nullptr) << dlerror();
715 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
716 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700717
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700718 // dlopen second time to make sure that the library was unloaded correctly
719 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
720 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800721#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700722 ASSERT_STREQ("dlopen failed: library \"libtest_with_dependency_loop.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -0800723#else
724 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
725 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700726#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800727
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700728 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
729 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700730}
731
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700732TEST(dlfcn, dlopen_nodelete) {
733 static bool is_unloaded = false;
734
735 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
736 ASSERT_TRUE(handle != nullptr) << dlerror();
737 void (*set_unload_flag_ptr)(bool*);
738 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
739 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
740 set_unload_flag_ptr(&is_unloaded);
741
742 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
743 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
744 ASSERT_EQ(1729U, *taxicab_number);
745 *taxicab_number = 2;
746
747 dlclose(handle);
748 ASSERT_TRUE(!is_unloaded);
749
750 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
751 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
752 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
753
754
755 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
756 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
757 ASSERT_EQ(taxicab_number2, taxicab_number);
758
759 ASSERT_EQ(2U, *taxicab_number2);
760
761 dlclose(handle);
762 ASSERT_TRUE(!is_unloaded);
763}
764
765TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
766 static bool is_unloaded = false;
767
768 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
769 ASSERT_TRUE(handle != nullptr) << dlerror();
770 void (*set_unload_flag_ptr)(bool*);
771 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
772 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
773 set_unload_flag_ptr(&is_unloaded);
774
775 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
776 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
777
778 ASSERT_EQ(1729U, *taxicab_number);
779 *taxicab_number = 2;
780
781 // This RTLD_NODELETE should be ignored
782 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
783 ASSERT_TRUE(handle1 != nullptr) << dlerror();
784 ASSERT_EQ(handle, handle1);
785
786 dlclose(handle1);
787 dlclose(handle);
788
789 ASSERT_TRUE(is_unloaded);
790}
791
792TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
793 static bool is_unloaded = false;
794
795 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
796 ASSERT_TRUE(handle != nullptr) << dlerror();
797 void (*set_unload_flag_ptr)(bool*);
798 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
799 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
800 set_unload_flag_ptr(&is_unloaded);
801
802 dlclose(handle);
803 ASSERT_TRUE(!is_unloaded);
804}
805
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700806TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700807 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
808 ASSERT_TRUE(handle != nullptr) << dlerror();
809 int (*get_answer)();
810 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
811 ASSERT_TRUE(get_answer != nullptr) << dlerror();
812 ASSERT_EQ(42, get_answer());
813 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700814}
815
Elliott Hughese66190d2012-12-18 15:57:55 -0800816TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700817 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700818 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700819#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700820 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
821#else
822 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
823#endif
824}
825
dimitry109040c2017-11-03 13:49:07 +0100826TEST(dlfcn, dlclose_unload) {
827 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
828 ASSERT_TRUE(handle != nullptr) << dlerror();
829 uint32_t* taxicab_number = static_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
830 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
831 EXPECT_EQ(1729U, *taxicab_number);
832 dlclose(handle);
833 // Making sure that the library has been unmapped as part of library unload
834 // process. Note that mprotect somewhat counter-intuitively returns ENOMEM in
835 // this case.
836 uintptr_t page_start = reinterpret_cast<uintptr_t>(taxicab_number) & ~(PAGE_SIZE - 1);
837 ASSERT_TRUE(mprotect(reinterpret_cast<void*>(page_start), PAGE_SIZE, PROT_NONE) != 0);
838 ASSERT_EQ(ENOMEM, errno) << strerror(errno);
839}
840
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800841static void ConcurrentDlErrorFn(std::string& error) {
842 ASSERT_TRUE(dlerror() == nullptr);
843
844 void* handle = dlopen("/child/thread", RTLD_NOW);
845 ASSERT_TRUE(handle == nullptr);
846
847 const char* err = dlerror();
848 ASSERT_TRUE(err != nullptr);
849
850 error = err;
851}
852
853TEST(dlfcn, dlerror_concurrent_buffer) {
854 void* handle = dlopen("/main/thread", RTLD_NOW);
855 ASSERT_TRUE(handle == nullptr);
856 const char* main_thread_error = dlerror();
857 ASSERT_TRUE(main_thread_error != nullptr);
858 ASSERT_SUBSTR("/main/thread", main_thread_error);
859
860 std::string child_thread_error;
861 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
862 t.join();
863 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
864
865 // Check that main thread local buffer was not modified.
866 ASSERT_SUBSTR("/main/thread", main_thread_error);
Elliott Hughes5419b942012-10-16 15:54:46 -0700867}
868
Elliott Hughese66190d2012-12-18 15:57:55 -0800869TEST(dlfcn, dlerror_concurrent) {
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800870 void* handle = dlopen("/main/thread", RTLD_NOW);
871 ASSERT_TRUE(handle == nullptr);
872
873 std::string child_thread_error;
874 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
875 t.join();
876 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
877
Elliott Hughes5419b942012-10-16 15:54:46 -0700878 const char* main_thread_error = dlerror();
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800879 ASSERT_TRUE(main_thread_error != nullptr);
Elliott Hughes5419b942012-10-16 15:54:46 -0700880 ASSERT_SUBSTR("/main/thread", main_thread_error);
881}
882
Elliott Hughese66190d2012-12-18 15:57:55 -0800883TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700884 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700885 void* self = dlopen(nullptr, RTLD_NOW);
886 ASSERT_TRUE(self != nullptr);
887 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700888
889 void* sym;
890
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700891#if defined(__BIONIC__) && !defined(__LP64__)
892 // RTLD_DEFAULT in lp32 bionic is not (void*)0
893 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700894 sym = dlsym(nullptr, "test");
895 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800896 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700897#endif
898
Elliott Hughes3b297c42012-10-11 16:08:51 -0700899 // Symbol that doesn't exist.
900 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700901 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700902 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700903
904 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700905}
906
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700907TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700908 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700909 void* self = dlopen(nullptr, RTLD_NOW);
910 ASSERT_TRUE(self != nullptr);
911 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700912
913 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700914 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700915
916 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
917 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
918
919 Dl_info info;
920 int rc = dladdr(addr, &info);
921 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
922
923 // Get the name of this executable.
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700924 const std::string executable_path = android::base::GetExecutablePath();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700925
926 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700927 char dli_realpath[PATH_MAX];
928 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700929 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700930
931 // The symbol name should be the symbol we looked up.
932 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
933
934 // The address should be the exact address of the symbol.
935 ASSERT_EQ(info.dli_saddr, sym);
936
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700937 std::vector<map_record> maps;
938 ASSERT_TRUE(Maps::parse_maps(&maps));
939
940 void* base_address = nullptr;
941 for (const map_record& rec : maps) {
942 if (executable_path == rec.pathname) {
943 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700944 break;
945 }
946 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700947
948 // The base address should be the address we were loaded at.
949 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700950
951 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700952}
953
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700954TEST(dlfcn, dlopen_executable_by_absolute_path) {
955 void* handle1 = dlopen(nullptr, RTLD_NOW);
956 ASSERT_TRUE(handle1 != nullptr) << dlerror();
957
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700958 void* handle2 = dlopen(android::base::GetExecutablePath().c_str(), RTLD_NOW);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700959 ASSERT_TRUE(handle2 != nullptr) << dlerror();
960
961#if defined(__BIONIC__)
962 ASSERT_EQ(handle1, handle2);
963#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800964 GTEST_SKIP() << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
965 "it loads a separate copy of the main executable "
966 "on dlopen by absolute path";
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700967#endif
968}
969
Elliott Hughes3f73ea62022-10-19 16:16:54 +0000970#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib64/" ABI_STRING "/"
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200971#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Peter Collingbourneea11be02019-04-30 16:05:13 -0700972#define PATH_TO_BOOTSTRAP_LIBC PATH_TO_SYSTEM_LIB "bootstrap/libc.so"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100973#define ALTERNATE_PATH_TO_LIBC ALTERNATE_PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700974
975TEST(dlfcn, dladdr_libc) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800976#if defined(__GLIBC__)
977 GTEST_SKIP() << "glibc returns libc.so's ldconfig path, which is a symlink (not a realpath)";
978#endif
979
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700980 Dl_info info;
981 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
982 ASSERT_TRUE(dladdr(addr, &info) != 0);
983
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700984 char libc_realpath[PATH_MAX];
Victor Khimenko14b9d712017-01-25 19:59:16 +0100985
986 // Check if libc is in canonical path or in alternate path.
987 if (strncmp(ALTERNATE_PATH_TO_SYSTEM_LIB,
988 info.dli_fname,
989 sizeof(ALTERNATE_PATH_TO_SYSTEM_LIB) - 1) == 0) {
990 // Platform with emulated architecture. Symlink on ARC++.
991 ASSERT_TRUE(realpath(ALTERNATE_PATH_TO_LIBC, libc_realpath) == libc_realpath);
Peter Collingbourneea11be02019-04-30 16:05:13 -0700992 } else if (strncmp(PATH_TO_BOOTSTRAP_LIBC, info.dli_fname,
993 sizeof(PATH_TO_BOOTSTRAP_LIBC) - 1) == 0) {
994 ASSERT_TRUE(realpath(PATH_TO_BOOTSTRAP_LIBC, libc_realpath) == libc_realpath);
Victor Khimenko14b9d712017-01-25 19:59:16 +0100995 } else {
996 // /system/lib is symlink when this test is executed on host.
997 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
998 }
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700999
1000 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001001 // TODO: add check for dfi_fbase
1002 ASSERT_STREQ("puts", info.dli_sname);
1003 ASSERT_EQ(addr, info.dli_saddr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001004}
1005
Elliott Hughese66190d2012-12-18 15:57:55 -08001006TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -07001007 Dl_info info;
1008
Elliott Hughes3b297c42012-10-11 16:08:51 -07001009 dlerror(); // Clear any pending errors.
1010
Elliott Hughes8e15b082012-09-26 11:44:01 -07001011 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001012 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
1013 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001014
1015 // No symbol corresponding to a stack address.
1016 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001017 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001018}
Elliott Hughes124fae92012-10-31 14:20:03 -07001019
Elliott Hughese66190d2012-12-18 15:57:55 -08001020TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001021 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001022 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
1023 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001024 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001025 void* sym = dlsym(handle, "getRandomNumber");
1026 ASSERT_TRUE(sym != nullptr) << dlerror();
1027 int (*fn)(void);
1028 fn = reinterpret_cast<int (*)(void)>(sym);
1029 EXPECT_EQ(4, fn());
1030
1031 Dl_info dlinfo;
1032 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1033
1034 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1035 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001036 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Elliott Hughes124fae92012-10-31 14:20:03 -07001037}
Elliott Hughese66190d2012-12-18 15:57:55 -08001038
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001039TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
1040 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
1041 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001042 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001043 void* sym = dlsym(handle, "getRandomNumber");
1044 ASSERT_TRUE(sym != nullptr) << dlerror();
1045 int (*fn)(void);
1046 fn = reinterpret_cast<int (*)(void)>(sym);
1047 EXPECT_EQ(4, fn());
1048
1049 Dl_info dlinfo;
1050 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1051
1052 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1053 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
1054 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
1055}
1056
Elliott Hughese66190d2012-12-18 15:57:55 -08001057TEST(dlfcn, dlopen_bad_flags) {
1058 dlerror(); // Clear any pending errors.
1059 void* handle;
1060
Elliott Hughes063525c2014-05-13 11:19:57 -07001061#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -08001062 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001063 handle = dlopen(nullptr, 0);
1064 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001065 ASSERT_SUBSTR("invalid", dlerror());
1066#endif
1067
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001068 handle = dlopen(nullptr, 0xffffffff);
1069 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001070 ASSERT_SUBSTR("invalid", dlerror());
1071
1072 // 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 -07001073 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
1074 ASSERT_TRUE(handle != nullptr);
1075 ASSERT_SUBSTR(nullptr, dlerror());
Elliott Hughese66190d2012-12-18 15:57:55 -08001076}
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001077
1078TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001079 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001080 ASSERT_TRUE(addr == nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001081}
1082
1083TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001084 void* addr = dlsym(RTLD_DEFAULT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001085 ASSERT_TRUE(addr != nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001086}
1087
1088TEST(dlfcn, rtld_next_unknown_symbol) {
1089 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001090 ASSERT_TRUE(addr == nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001091}
1092
1093TEST(dlfcn, rtld_next_known_symbol) {
1094 void* addr = dlsym(RTLD_NEXT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001095 ASSERT_TRUE(addr != nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001096}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001097
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001098// Check that RTLD_NEXT of a libc symbol works in dlopened library
1099TEST(dlfcn, rtld_next_from_library) {
dimitry153168c2018-02-20 16:51:41 +01001100 void* library_with_fclose = dlopen("libtest_check_rtld_next_from_library.so", RTLD_NOW | RTLD_GLOBAL);
Raj Mamadgi527229c2017-11-02 15:53:50 -07001101 ASSERT_TRUE(library_with_fclose != nullptr) << dlerror();
1102 void* expected_addr = dlsym(RTLD_DEFAULT, "fclose");
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001103 ASSERT_TRUE(expected_addr != nullptr) << dlerror();
Raj Mamadgi527229c2017-11-02 15:53:50 -07001104 typedef void* (*get_libc_fclose_ptr_fn_t)();
1105 get_libc_fclose_ptr_fn_t get_libc_fclose_ptr =
1106 reinterpret_cast<get_libc_fclose_ptr_fn_t>(dlsym(library_with_fclose, "get_libc_fclose_ptr"));
1107 ASSERT_TRUE(get_libc_fclose_ptr != nullptr) << dlerror();
1108 ASSERT_EQ(expected_addr, get_libc_fclose_ptr());
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001109
Raj Mamadgi527229c2017-11-02 15:53:50 -07001110 dlclose(library_with_fclose);
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001111}
1112
1113
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001114TEST(dlfcn, dlsym_weak_func) {
1115 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001116 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001117 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001118
1119 int (*weak_func)();
1120 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001121 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001122 EXPECT_EQ(42, weak_func());
1123 dlclose(handle);
1124}
1125
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001126TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001127 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
1128 ASSERT_TRUE(handle != nullptr) << dlerror();
1129 int (*weak_func)();
1130 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
1131 ASSERT_TRUE(weak_func != nullptr) << dlerror();
1132 EXPECT_EQ(6551, weak_func());
1133 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001134}
1135
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001136TEST(dlfcn, dlopen_symlink) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001137 DlfcnSymlink symlink("dlopen_symlink");
Colin Cross7da20342021-07-28 11:18:11 -07001138 const std::string symlink_name = android::base::Basename(symlink.get_symlink_path());
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001139 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001140 void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001141 ASSERT_TRUE(handle1 != nullptr);
1142 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001143 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -07001144 dlclose(handle1);
1145 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001146}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001147
1148// libtest_dlopen_from_ctor_main.so depends on
1149// libtest_dlopen_from_ctor.so which has a constructor
1150// that calls dlopen(libc...). This is to test the situation
1151// described in b/7941716.
1152TEST(dlfcn, dlopen_dlopen_from_ctor) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001153#if defined(__GLIBC__)
1154 GTEST_SKIP() << "glibc segfaults if you try to call dlopen from a constructor";
1155#endif
1156
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001157 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1158 ASSERT_TRUE(handle != nullptr) << dlerror();
1159 dlclose(handle);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001160}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001161
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001162static std::string g_fini_call_order_str;
1163
1164static void register_fini_call(const char* s) {
1165 g_fini_call_order_str += s;
1166}
1167
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001168static void test_init_fini_call_order_for(const char* libname) {
1169 g_fini_call_order_str.clear();
1170 void* handle = dlopen(libname, RTLD_NOW);
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001171 ASSERT_TRUE(handle != nullptr) << dlerror();
1172 typedef int (*get_init_order_number_t)();
1173 get_init_order_number_t get_init_order_number =
1174 reinterpret_cast<get_init_order_number_t>(dlsym(handle, "get_init_order_number"));
1175 ASSERT_EQ(321, get_init_order_number());
1176
1177 typedef void (*set_fini_callback_t)(void (*f)(const char*));
1178 set_fini_callback_t set_fini_callback =
1179 reinterpret_cast<set_fini_callback_t>(dlsym(handle, "set_fini_callback"));
1180 set_fini_callback(register_fini_call);
1181 dlclose(handle);
1182 ASSERT_EQ("(root)(child)(grandchild)", g_fini_call_order_str);
1183}
1184
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001185TEST(dlfcn, init_fini_call_order) {
1186 test_init_fini_call_order_for("libtest_init_fini_order_root.so");
1187 test_init_fini_call_order_for("libtest_init_fini_order_root2.so");
1188}
1189
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001190TEST(dlfcn, symbol_versioning_use_v1) {
1191 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1192 ASSERT_TRUE(handle != nullptr) << dlerror();
1193 typedef int (*fn_t)();
1194 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1195 ASSERT_TRUE(fn != nullptr) << dlerror();
1196 ASSERT_EQ(1, fn());
1197 dlclose(handle);
1198}
1199
1200TEST(dlfcn, symbol_versioning_use_v2) {
1201 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1202 ASSERT_TRUE(handle != nullptr) << dlerror();
1203 typedef int (*fn_t)();
1204 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1205 ASSERT_TRUE(fn != nullptr) << dlerror();
1206 ASSERT_EQ(2, fn());
1207 dlclose(handle);
1208}
1209
1210TEST(dlfcn, symbol_versioning_use_other_v2) {
1211 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1212 ASSERT_TRUE(handle != nullptr) << dlerror();
1213 typedef int (*fn_t)();
1214 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1215 ASSERT_TRUE(fn != nullptr) << dlerror();
1216 ASSERT_EQ(20, fn());
1217 dlclose(handle);
1218}
1219
1220TEST(dlfcn, symbol_versioning_use_other_v3) {
1221 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1222 ASSERT_TRUE(handle != nullptr) << dlerror();
1223 typedef int (*fn_t)();
1224 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1225 ASSERT_TRUE(fn != nullptr) << dlerror();
1226 ASSERT_EQ(3, fn());
1227 dlclose(handle);
1228}
1229
1230TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1231 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1232 ASSERT_TRUE(handle != nullptr) << dlerror();
1233 typedef int (*fn_t)();
1234 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1235 ASSERT_TRUE(fn != nullptr) << dlerror();
1236 ASSERT_EQ(3, fn()); // the default version is 3
1237 dlclose(handle);
1238}
1239
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001240TEST(dlfcn, dlvsym_smoke) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001241#if !defined(ANDROID_HOST_MUSL)
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001242 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1243 ASSERT_TRUE(handle != nullptr) << dlerror();
1244 typedef int (*fn_t)();
1245
1246 {
1247 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1248 ASSERT_TRUE(fn == nullptr);
1249 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1250 }
1251
1252 {
1253 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1254 ASSERT_TRUE(fn != nullptr) << dlerror();
1255 ASSERT_EQ(2, fn());
1256 }
1257
1258 dlclose(handle);
Colin Cross7da20342021-07-28 11:18:11 -07001259#else
1260 GTEST_SKIP() << "musl doesn't have dlvsym";
1261#endif
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001262}
1263
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001264// This preempts the implementation from libtest_versioned_lib.so
1265extern "C" int version_zero_function() {
1266 return 0;
1267}
1268
1269// This preempts the implementation from libtest_versioned_uselibv*.so
1270extern "C" int version_zero_function2() {
1271 return 0;
1272}
Evgenii Stepanov68650822015-06-10 13:38:39 -07001273
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001274TEST(dlfcn, dt_runpath_smoke) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001275 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1276 ASSERT_TRUE(handle != nullptr) << dlerror();
1277
1278 typedef void *(* dlopen_b_fn)();
1279 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1280 ASSERT_TRUE(fn != nullptr) << dlerror();
1281
1282 void *p = fn();
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001283 ASSERT_TRUE(p != nullptr);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001284
1285 dlclose(handle);
1286}
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001287
Dimitry Ivanovdb6ab3d2017-06-27 11:02:51 -07001288TEST(dlfcn, dt_runpath_absolute_path) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001289 std::string libpath = GetTestlibRoot() + "/libtest_dt_runpath_d.so";
Dimitry Ivanovdb6ab3d2017-06-27 11:02:51 -07001290 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1291 ASSERT_TRUE(handle != nullptr) << dlerror();
1292
1293 typedef void *(* dlopen_b_fn)();
1294 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1295 ASSERT_TRUE(fn != nullptr) << dlerror();
1296
1297 void *p = fn();
1298 ASSERT_TRUE(p != nullptr);
1299
1300 dlclose(handle);
1301}
1302
dimitry55547db2018-05-25 14:17:37 +02001303static void test_dlclose_after_thread_local_dtor(const char* library_name) {
dimitry06016f22018-01-05 11:39:28 +01001304 bool is_dtor_triggered = false;
1305
1306 auto f = [](void* handle, bool* is_dtor_triggered) {
1307 typedef void (*fn_t)(bool*);
1308 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1309 ASSERT_TRUE(fn != nullptr) << dlerror();
1310
1311 fn(is_dtor_triggered);
1312
1313 ASSERT_TRUE(!*is_dtor_triggered);
1314 };
1315
dimitry55547db2018-05-25 14:17:37 +02001316 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001317 ASSERT_TRUE(handle == nullptr);
1318
dimitry55547db2018-05-25 14:17:37 +02001319 handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001320 ASSERT_TRUE(handle != nullptr) << dlerror();
1321
1322 std::thread t(f, handle, &is_dtor_triggered);
1323 t.join();
1324
1325 ASSERT_TRUE(is_dtor_triggered);
1326 dlclose(handle);
1327
dimitry55547db2018-05-25 14:17:37 +02001328 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001329 ASSERT_TRUE(handle == nullptr);
1330}
1331
dimitry55547db2018-05-25 14:17:37 +02001332TEST(dlfcn, dlclose_after_thread_local_dtor) {
1333 test_dlclose_after_thread_local_dtor("libtest_thread_local_dtor.so");
1334}
1335
1336TEST(dlfcn, dlclose_after_thread_local_dtor_indirect) {
1337 test_dlclose_after_thread_local_dtor("libtest_indirect_thread_local_dtor.so");
1338}
1339
1340static void test_dlclose_before_thread_local_dtor(const char* library_name) {
dimitry06016f22018-01-05 11:39:28 +01001341 bool is_dtor_triggered = false;
1342
dimitry55547db2018-05-25 14:17:37 +02001343 auto f = [library_name](bool* is_dtor_triggered) {
1344 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001345 ASSERT_TRUE(handle == nullptr);
1346
dimitry55547db2018-05-25 14:17:37 +02001347 handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001348 ASSERT_TRUE(handle != nullptr) << dlerror();
1349
1350 typedef void (*fn_t)(bool*);
1351 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1352 ASSERT_TRUE(fn != nullptr) << dlerror();
1353
1354 fn(is_dtor_triggered);
1355
1356 dlclose(handle);
1357
1358 ASSERT_TRUE(!*is_dtor_triggered);
1359
1360 // Since we have thread_atexit dtors associated with handle - the library should
1361 // still be availabe.
dimitry55547db2018-05-25 14:17:37 +02001362 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001363 ASSERT_TRUE(handle != nullptr) << dlerror();
1364 dlclose(handle);
1365 };
1366
dimitry55547db2018-05-25 14:17:37 +02001367 void* handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001368 ASSERT_TRUE(handle != nullptr) << dlerror();
1369 dlclose(handle);
1370
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);
1373
1374 std::thread t(f, &is_dtor_triggered);
1375 t.join();
1376#if defined(__BIONIC__)
1377 // ld-android.so unloads unreferenced libraries on pthread_exit()
1378 ASSERT_TRUE(is_dtor_triggered);
dimitry55547db2018-05-25 14:17:37 +02001379 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001380 ASSERT_TRUE(handle == nullptr);
1381#else
1382 // GLIBC does not unload libraries with ref_count = 0 on pthread_exit
1383 ASSERT_TRUE(is_dtor_triggered);
dimitry55547db2018-05-25 14:17:37 +02001384 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1385 ASSERT_TRUE(handle != nullptr) << dlerror();
1386#endif
1387}
1388
1389TEST(dlfcn, dlclose_before_thread_local_dtor) {
1390 test_dlclose_before_thread_local_dtor("libtest_thread_local_dtor.so");
1391}
1392
1393TEST(dlfcn, dlclose_before_thread_local_dtor_indirect) {
1394 test_dlclose_before_thread_local_dtor("libtest_indirect_thread_local_dtor.so");
1395}
1396
1397TEST(dlfcn, dlclose_before_thread_local_dtor_multiple_dsos) {
1398 const constexpr char* library_name = "libtest_indirect_thread_local_dtor.so";
1399
1400 bool is_dtor1_triggered = false;
1401 bool is_dtor2_triggered = false;
1402
1403 std::mutex mtx;
1404 std::condition_variable cv;
1405 void* library_handle = nullptr;
1406 bool thread1_dlopen_complete = false;
1407 bool thread2_thread_local_dtor_initialized = false;
1408 bool thread1_complete = false;
1409
1410 auto f1 = [&]() {
1411 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1412 ASSERT_TRUE(handle == nullptr);
1413
1414 handle = dlopen(library_name, RTLD_NOW);
1415 ASSERT_TRUE(handle != nullptr) << dlerror();
1416 std::unique_lock<std::mutex> lock(mtx);
1417 thread1_dlopen_complete = true;
1418 library_handle = handle;
1419 lock.unlock();
1420 cv.notify_one();
1421
1422 typedef void (*fn_t)(bool*);
1423 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1424 ASSERT_TRUE(fn != nullptr) << dlerror();
1425
1426 fn(&is_dtor1_triggered);
1427
1428 lock.lock();
1429 cv.wait(lock, [&] { return thread2_thread_local_dtor_initialized; });
1430 lock.unlock();
1431
1432 dlclose(handle);
1433
1434 ASSERT_TRUE(!is_dtor1_triggered);
1435
1436 // Since we have thread_atexit dtors associated with handle - the library should
1437 // still be availabe.
1438 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1439 ASSERT_TRUE(handle != nullptr) << dlerror();
1440 dlclose(handle);
1441 };
1442
1443 auto f2 = [&]() {
1444 std::unique_lock<std::mutex> lock(mtx);
1445 cv.wait(lock, [&] { return thread1_dlopen_complete; });
1446 void* handle = library_handle;
1447 lock.unlock();
1448
1449 typedef void (*fn_t)(bool*);
1450 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable2"));
1451 ASSERT_TRUE(fn != nullptr) << dlerror();
1452
1453 fn(&is_dtor2_triggered);
1454
1455 lock.lock();
1456 thread2_thread_local_dtor_initialized = true;
1457 lock.unlock();
1458 cv.notify_one();
1459
1460 lock.lock();
1461 cv.wait(lock, [&] { return thread1_complete; });
1462 lock.unlock();
1463
1464 ASSERT_TRUE(!is_dtor2_triggered);
1465 };
1466
1467 void* handle = dlopen(library_name, RTLD_NOW);
1468 ASSERT_TRUE(handle != nullptr) << dlerror();
1469 dlclose(handle);
1470
1471 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1472 ASSERT_TRUE(handle == nullptr);
1473
1474 std::thread t1(f1);
1475 std::thread t2(f2);
1476 t1.join();
1477 ASSERT_TRUE(is_dtor1_triggered);
1478 ASSERT_TRUE(!is_dtor2_triggered);
1479
1480 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1481 ASSERT_TRUE(handle != nullptr) << dlerror();
1482 dlclose(handle);
1483
1484 std::unique_lock<std::mutex> lock(mtx);
1485 thread1_complete = true;
1486 lock.unlock();
1487 cv.notify_one();
1488
1489 t2.join();
1490 ASSERT_TRUE(is_dtor2_triggered);
1491
1492#if defined(__BIONIC__)
1493 // ld-android.so unloads unreferenced libraries on pthread_exit()
1494 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1495 ASSERT_TRUE(handle == nullptr);
1496#else
1497 // GLIBC does not unload libraries with ref_count = 0 on pthread_exit
1498 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001499 ASSERT_TRUE(handle != nullptr) << dlerror();
1500#endif
1501}
1502
Elliott Hughes8ad40932017-06-15 15:12:29 -07001503TEST(dlfcn, RTLD_macros) {
1504#if !defined(RTLD_LOCAL)
1505#error no RTLD_LOCAL
1506#elif !defined(RTLD_LAZY)
1507#error no RTLD_LAZY
1508#elif !defined(RTLD_NOW)
1509#error no RTLD_NOW
1510#elif !defined(RTLD_NOLOAD)
1511#error no RTLD_NOLOAD
1512#elif !defined(RTLD_GLOBAL)
1513#error no RTLD_GLOBAL
1514#elif !defined(RTLD_NODELETE)
1515#error no RTLD_NODELETE
1516#endif
1517}
1518
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001519// Bionic specific tests
1520#if defined(__BIONIC__)
1521
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001522#if defined(__arm__)
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001523
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001524void validate_compatibility_of_native_library(const std::string& soname, const std::string& path) {
1525 // Grab the dynamic section in text form...
1526 ExecTestHelper eth;
1527 eth.SetArgs({"readelf", "-dW", path.c_str(), nullptr});
1528 eth.Run([&]() { execvpe("readelf", eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
1529 std::string output = eth.GetOutput();
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001530
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001531 // Check that there *is* a legacy DT_HASH (not just a GNU hash)...
1532 ASSERT_TRUE(std::regex_search(output, std::regex("\\(HASH\\)"))) << output;
1533 // Check that there is no DT_ANDROID_REL or DT_ANDROID_RELA...
1534 ASSERT_FALSE(std::regex_search(output, std::regex("\\(ANDROID_REL\\)"))) << output;
1535 ASSERT_FALSE(std::regex_search(output, std::regex("\\(ANDROID_RELA\\)"))) << output;
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001536
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001537 // Check that we have regular non-packed relocations.
1538 // libdl.so is simple enough that it doesn't have any relocations.
1539 ASSERT_TRUE(std::regex_search(output, std::regex("\\(RELA?\\)")) || soname == "libdl.so")
1540 << output;
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001541}
1542
Ryan Prichard470b6662018-03-27 22:10:55 -07001543void validate_compatibility_of_native_library(const std::string& soname) {
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001544 // On the systems with emulation system libraries would be of different
1545 // architecture. Try to use alternate paths first.
1546 std::string path = std::string(ALTERNATE_PATH_TO_SYSTEM_LIB) + soname;
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001547 if (access(path.c_str(), R_OK) != 0) {
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001548 path = std::string(PATH_TO_SYSTEM_LIB) + soname;
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001549 ASSERT_EQ(0, access(path.c_str(), R_OK));
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001550 }
Elliott Hughes1ff7be02022-01-13 15:43:23 -08001551 validate_compatibility_of_native_library(soname, path);
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001552}
1553
1554// This is a test for app compatibility workaround for arm apps
1555// affected by http://b/24465209
1556TEST(dlext, compat_elf_hash_and_relocation_tables) {
1557 validate_compatibility_of_native_library("libc.so");
1558 validate_compatibility_of_native_library("liblog.so");
1559 validate_compatibility_of_native_library("libstdc++.so");
1560 validate_compatibility_of_native_library("libdl.so");
1561 validate_compatibility_of_native_library("libm.so");
1562 validate_compatibility_of_native_library("libz.so");
1563 validate_compatibility_of_native_library("libjnigraphics.so");
1564}
1565
1566#endif // defined(__arm__)
1567
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001568TEST(dlfcn, dlopen_invalid_rw_load_segment) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001569 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-rw_load_segment.so";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001570 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1571 ASSERT_TRUE(handle == nullptr);
Elliott Hughes9076b0c2018-02-28 11:29:45 -08001572 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W+E load segments are not allowed";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001573 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1574}
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001575
1576TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001577 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-unaligned_shdr_offset.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001578
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001579 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1580 ASSERT_TRUE(handle == nullptr);
1581 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
1582 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1583}
1584
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001585TEST(dlfcn, dlopen_invalid_zero_shentsize) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001586 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-zero_shentsize.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001587
Dimitry Ivanovcb86c312016-08-12 15:28:42 -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 unsupported e_shentsize: 0x0 (expected 0x";
1591 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1592}
1593
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001594TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001595 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-zero_shstrndx.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001596
Dimitry Ivanovc9a95612016-08-15 10:27:47 -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 invalid e_shstrndx";
1600 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1601}
1602
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001603TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001604 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-empty_shdr_table.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001605
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -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 no section headers";
1609 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1610}
1611
Dimitry Ivanov46230442016-08-15 13:40:53 -07001612TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001613 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-zero_shdr_table_offset.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001614
Dimitry Ivanov46230442016-08-15 13:40:53 -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 invalid shdr offset/size: 0/";
1618 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1619}
1620
Dimitry Ivanov55958342016-08-15 14:06:04 -07001621TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001622 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-zero_shdr_table_content.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001623
Dimitry Ivanov55958342016-08-15 14:06:04 -07001624 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1625 ASSERT_TRUE(handle == nullptr);
1626 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
1627 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1628}
1629
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001630TEST(dlfcn, dlopen_invalid_textrels) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001631 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-textrels.so";
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001632
1633 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1634 ASSERT_TRUE(handle == nullptr);
1635 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1636 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1637}
1638
1639TEST(dlfcn, dlopen_invalid_textrels2) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001640 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-textrels2.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
Ryan Prichard8ea6af52022-03-24 21:14:27 -07001648TEST(dlfcn, dlopen_invalid_local_tls) {
Elliott Hughesec9f0232022-11-16 00:23:21 +00001649#if defined(__riscv)
1650 // This is a test for bad gold behavior, and gold doesn't support riscv64.
1651#else
Ryan Prichard8ea6af52022-03-24 21:14:27 -07001652 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-local-tls.so";
1653
1654 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1655 ASSERT_TRUE(handle == nullptr);
1656#if defined(__arm__)
1657 const char* referent = "local section";
1658#else
1659 const char* referent = "local symbol \"tls_var_2\"";
1660#endif
1661 std::string expected_dlerror = std::string("dlopen failed: unexpected TLS reference to ") +
1662 referent + " in \"" + libpath + "\"";
1663 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
Elliott Hughesec9f0232022-11-16 00:23:21 +00001664#endif
Ryan Prichard8ea6af52022-03-24 21:14:27 -07001665}
1666
Jiyong Park01162f22017-10-16 15:31:09 +09001667TEST(dlfcn, dlopen_df_1_global) {
1668 void* handle = dlopen("libtest_dlopen_df_1_global.so", RTLD_NOW);
1669 ASSERT_TRUE(handle != nullptr) << dlerror();
1670}
1671
Peter Collingbourneb39cb3c2019-03-01 13:12:49 -08001672TEST(dlfcn, segment_gap) {
1673 void* handle = dlopen("libsegment_gap_outer.so", RTLD_NOW);
1674 ASSERT_TRUE(handle != nullptr) << dlerror();
1675
1676 auto get_inner = reinterpret_cast<void* (*)()>(dlsym(handle, "get_inner"));
1677 void* inner = get_inner();
1678 (void)inner;
1679
1680#if __arm__
1681 int count;
1682 _Unwind_Ptr outer_exidx = dl_unwind_find_exidx(reinterpret_cast<_Unwind_Ptr>(get_inner), &count);
1683 _Unwind_Ptr inner_exidx = dl_unwind_find_exidx(reinterpret_cast<_Unwind_Ptr>(inner), &count);
1684 EXPECT_NE(0u, outer_exidx);
1685 EXPECT_NE(0u, inner_exidx);
1686 EXPECT_NE(inner_exidx, outer_exidx);
1687#endif
1688
1689 Dl_info info;
1690 int rc = dladdr(inner, &info);
1691 ASSERT_NE(rc, 0);
1692
1693 EXPECT_NE(nullptr, strstr(info.dli_fname, "libsegment_gap_inner.so"));
1694}
1695
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001696#endif