blob: 35e12eb4aa45c90cb6f57149f183d4488c28dfcc [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 Hughes8e15b082012-09-26 11:44:01 -070020#include <limits.h>
21#include <stdio.h>
22#include <stdint.h>
Dimitry Ivanov708589f2016-09-19 10:50:28 -070023#include <string.h>
dimitryb9555a92017-10-11 18:04:05 +020024#if __has_include(<sys/auxv.h>)
25#include <sys/auxv.h>
26#endif
dimitry109040c2017-11-03 13:49:07 +010027#include <sys/user.h>
Elliott Hughes8e15b082012-09-26 11:44:01 -070028
29#include <string>
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -080030#include <thread>
jeffhaoacf5aa72012-09-12 17:25:30 -070031
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -070032#include <android-base/file.h>
Tom Cherryb8ab6182017-04-05 16:20:29 -070033#include <android-base/scopeguard.h>
34
Dimitry Ivanov927877c2016-09-21 11:17:13 -070035#include "gtest_globals.h"
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -070036#include "gtest_utils.h"
Dimitry Ivanov708589f2016-09-19 10:50:28 -070037#include "dlfcn_symlink_support.h"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070038#include "utils.h"
39
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -080040#if defined(__BIONIC__) && (defined(__arm__) || defined(__i386__))
41#pragma clang diagnostic push
42#pragma clang diagnostic ignored "-Wunused-parameter"
43
44#include <llvm/ADT/StringRef.h>
45#include <llvm/Object/Binary.h>
46#include <llvm/Object/ELFObjectFile.h>
47#include <llvm/Object/ObjectFile.h>
48
49#pragma clang diagnostic pop
50#endif // defined(__ANDROID__) && (defined(__arm__) || defined(__i386__))
51
Peter Collingbourneb39cb3c2019-03-01 13:12:49 -080052// Declared manually because the macro definitions in <elf.h> conflict with LLVM headers.
53#ifdef __arm__
54typedef uintptr_t _Unwind_Ptr;
55extern "C" _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr, int*);
56#endif
57
Elliott Hughes3b297c42012-10-11 16:08:51 -070058#define ASSERT_SUBSTR(needle, haystack) \
59 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
60
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070061
Elliott Hughes1728b232014-05-14 10:02:03 -070062static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070063extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070064 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070065}
66
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070067static int g_ctor_function_called = 0;
Dimitry Ivanov55437462016-07-20 15:33:07 -070068static int g_ctor_argc = 0;
69static char** g_ctor_argv = reinterpret_cast<char**>(0xDEADBEEF);
70static char** g_ctor_envp = g_ctor_envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070071
Dimitry Ivanov55437462016-07-20 15:33:07 -070072extern "C" void ctor_function(int argc, char** argv, char** envp) __attribute__ ((constructor));
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070073
Dimitry Ivanov55437462016-07-20 15:33:07 -070074extern "C" void ctor_function(int argc, char** argv, char** envp) {
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070075 g_ctor_function_called = 17;
Dimitry Ivanov55437462016-07-20 15:33:07 -070076 g_ctor_argc = argc;
77 g_ctor_argv = argv;
78 g_ctor_envp = envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070079}
80
81TEST(dlfcn, ctor_function_call) {
82 ASSERT_EQ(17, g_ctor_function_called);
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -070083 ASSERT_TRUE(g_ctor_argc = GetArgc());
84 ASSERT_TRUE(g_ctor_argv = GetArgv());
85 ASSERT_TRUE(g_ctor_envp = GetEnvp());
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070086}
87
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070088TEST(dlfcn, dlsym_in_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070089 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070090 void* self = dlopen(nullptr, RTLD_NOW);
91 ASSERT_TRUE(self != nullptr);
92 ASSERT_TRUE(dlerror() == nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070093
94 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070095 ASSERT_TRUE(sym != nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070096
97 void (*function)() = reinterpret_cast<void(*)()>(sym);
98
Elliott Hughes1728b232014-05-14 10:02:03 -070099 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -0700100 function();
Elliott Hughes1728b232014-05-14 10:02:03 -0700101 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -0700102
103 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -0700104}
Elliott Hughes8e15b082012-09-26 11:44:01 -0700105
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700106TEST(dlfcn, dlsym_from_sofile) {
107 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL);
108 ASSERT_TRUE(handle != nullptr) << dlerror();
109
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700110 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700111 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
112 ASSERT_TRUE(symbol == nullptr);
113 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
114
115 typedef int* (*fn_t)();
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700116 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
117 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
118 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700119
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700120 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700121 ASSERT_TRUE(ptr != nullptr) << dlerror();
122 ASSERT_EQ(42, *ptr);
123
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700124 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
125 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
126 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
127
128 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
129 ASSERT_TRUE(ptr != nullptr) << dlerror();
130 ASSERT_EQ(44, *ptr);
131
132 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
133 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
134 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
135
136 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
137 ASSERT_TRUE(ptr != nullptr) << dlerror();
138 ASSERT_EQ(43, *ptr);
139
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700140 dlclose(handle);
141}
142
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700143TEST(dlfcn, dlsym_from_sofile_with_preload) {
144 void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL);
145 ASSERT_TRUE(preload != nullptr) << dlerror();
146
147 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
148 ASSERT_TRUE(handle != nullptr) << dlerror();
149
150 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
151 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
152 ASSERT_TRUE(symbol == nullptr);
153 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
154
155 typedef int* (*fn_t)();
156 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
157 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
158 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
159
160 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
161 ASSERT_TRUE(ptr != nullptr) << dlerror();
162 ASSERT_EQ(42, *ptr);
163
164 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
165 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
166 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
167
168 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
169 ASSERT_TRUE(ptr != nullptr) << dlerror();
170 ASSERT_EQ(44, *ptr);
171
172 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
173 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
174 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
175
176 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
177 ASSERT_TRUE(ptr != nullptr) << dlerror();
178 ASSERT_EQ(43, *ptr);
179
180 dlclose(handle);
181 dlclose(preload);
182}
183
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700184TEST(dlfcn, dlsym_handle_global_sym) {
185 // check that we do not look into global group
186 // when looking up symbol by handle
187 void* handle = dlopen("libtest_empty.so", RTLD_NOW);
188 dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL);
189 void* sym = dlsym(handle, "getRandomNumber");
190 ASSERT_TRUE(sym == nullptr);
191 ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror());
192
193 sym = dlsym(handle, "DlSymTestFunction");
194 ASSERT_TRUE(sym == nullptr);
195 ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror());
196 dlclose(handle);
197}
198
Dimitry Ivanovd5b578a2016-12-14 15:16:56 -0800199TEST(dlfcn, dlsym_handle_empty_symbol) {
200 // check that dlsym of an empty symbol fails (see http://b/33530622)
201 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW);
202 ASSERT_TRUE(handle != nullptr) << dlerror();
203 void* sym = dlsym(handle, "");
204 ASSERT_TRUE(sym == nullptr);
205 ASSERT_SUBSTR("undefined symbol: ", dlerror());
206 dlclose(handle);
207}
208
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700209TEST(dlfcn, dlsym_with_dependencies) {
210 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700211 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700212 dlerror();
213 // This symbol is in DT_NEEDED library.
214 void* sym = dlsym(handle, "getRandomNumber");
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700215 ASSERT_TRUE(sym != nullptr) << dlerror();
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700216 int (*fn)(void);
217 fn = reinterpret_cast<int (*)(void)>(sym);
218 EXPECT_EQ(4, fn());
219 dlclose(handle);
220}
221
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700222TEST(dlfcn, dlopen_noload) {
223 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700224 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700225 handle = dlopen("libtest_simple.so", RTLD_NOW);
226 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700227 ASSERT_TRUE(handle != nullptr);
228 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700229 ASSERT_TRUE(handle == handle2);
230 ASSERT_EQ(0, dlclose(handle));
231 ASSERT_EQ(0, dlclose(handle2));
232}
233
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700234TEST(dlfcn, dlopen_by_soname) {
235 static const char* soname = "libdlext_test_soname.so";
236 static const char* filename = "libdlext_test_different_soname.so";
237 // 1. Make sure there is no library with soname in default search path
238 void* handle = dlopen(soname, RTLD_NOW);
239 ASSERT_TRUE(handle == nullptr);
240
241 // 2. Load a library using filename
242 handle = dlopen(filename, RTLD_NOW);
243 ASSERT_TRUE(handle != nullptr) << dlerror();
244
245 // 3. Find library by soname
246 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
247 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
248 ASSERT_EQ(handle, handle_soname);
249
250 // 4. RTLD_NOLOAD should still work with filename
251 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
252 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
253 ASSERT_EQ(handle, handle_filename);
254
255 dlclose(handle_filename);
256 dlclose(handle_soname);
257 dlclose(handle);
258}
259
dimitryc18de1b2017-09-26 14:31:35 +0200260TEST(dlfcn, dlopen_vdso) {
dimitryb9555a92017-10-11 18:04:05 +0200261#if __has_include(<sys/auxv.h>)
262 if (getauxval(AT_SYSINFO_EHDR) == 0) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800263 GTEST_SKIP() << "getauxval(AT_SYSINFO_EHDR) == 0, skipping this test";
dimitryb9555a92017-10-11 18:04:05 +0200264 }
265#endif
Elliott Hughesda1bb112018-02-16 10:05:08 -0800266
267 const char* vdso_name = "linux-vdso.so.1";
268#if defined(__i386__)
269 vdso_name = "linux-gate.so.1";
270#endif
271 void* handle = dlopen(vdso_name, RTLD_NOW);
dimitryc18de1b2017-09-26 14:31:35 +0200272 ASSERT_TRUE(handle != nullptr) << dlerror();
273 dlclose(handle);
274}
275
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700276TEST(dlfcn, ifunc_variable) {
277 typedef const char* (*fn_ptr)();
278
279 // ifunc's choice depends on whether IFUNC_CHOICE has a value
280 // first check the set case
281 setenv("IFUNC_CHOICE", "set", 1);
282 // preload libtest_ifunc_variable_impl.so
283 void* handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
284 void* handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
285 ASSERT_TRUE(handle != nullptr) << dlerror();
286 const char** foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
287 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
288 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
289 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
290 ASSERT_EQ(strncmp("set", *foo_ptr, 3), 0);
291 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
292 dlclose(handle);
293 dlclose(handle_impl);
294
295 // then check the unset case
296 unsetenv("IFUNC_CHOICE");
297 handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
298 handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
299 ASSERT_TRUE(handle != nullptr) << dlerror();
300 foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
301 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
302 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
303 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
304 ASSERT_EQ(strncmp("unset", *foo_ptr, 5), 0);
305 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
306 dlclose(handle);
307 dlclose(handle_impl);
308}
309
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700310TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700311 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700312
313 // ifunc's choice depends on whether IFUNC_CHOICE has a value
314 // first check the set case
315 setenv("IFUNC_CHOICE", "set", 1);
316 void* 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 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
319 fn_ptr 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("set", foo_ptr(), 3), 0);
323 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700324 dlclose(handle);
325
326 // then check the unset case
327 unsetenv("IFUNC_CHOICE");
328 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700329 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700330 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
331 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700332 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
333 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700334 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700335 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700336 dlclose(handle);
337}
338
339TEST(dlfcn, ifunc_ctor_call) {
340 typedef const char* (*fn_ptr)();
341
342 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700343 ASSERT_TRUE(handle != nullptr) << dlerror();
344 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
345 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
346 ASSERT_STREQ("false", is_ctor_called());
347
348 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
349 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700350 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700351 dlclose(handle);
352}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700353
354TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
355 typedef const char* (*fn_ptr)();
356
357 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
358 ASSERT_TRUE(handle != nullptr) << dlerror();
359 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
360 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
361 ASSERT_STREQ("false", is_ctor_called());
362
363 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
364 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
365 ASSERT_STREQ("true", is_ctor_called());
366 dlclose(handle);
367}
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700368
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700369TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
370 // This is the structure of the test library and
371 // its dt_needed libraries
372 // libtest_relo_check_dt_needed_order.so
373 // |
374 // +-> libtest_relo_check_dt_needed_order_1.so
375 // |
376 // +-> libtest_relo_check_dt_needed_order_2.so
377 //
378 // The root library references relo_test_get_answer_lib - which is defined
379 // in both dt_needed libraries, the correct relocation should
380 // use the function defined in libtest_relo_check_dt_needed_order_1.so
381 void* handle = nullptr;
Tom Cherryb8ab6182017-04-05 16:20:29 -0700382 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700383
384 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
385 ASSERT_TRUE(handle != nullptr) << dlerror();
386
387 typedef int (*fn_t) (void);
388 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
389 ASSERT_TRUE(fn != nullptr) << dlerror();
390 ASSERT_EQ(1, fn());
391}
392
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700393TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700394 // Here is how the test library and its dt_needed
395 // libraries are arranged
396 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700397 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700398 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700399 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700400 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700401 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700402 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700403 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700404 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700405 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700406 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700407 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700408 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700409 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700410 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700411 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700412 //
413 // load order should be (1, 2, 3, a, b, d)
414 //
415 // get_answer() is defined in (2, 3, a, b, c)
416 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700417 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700418 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700419 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
420 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700421 typedef int (*fn_t) (void);
422 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700423 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700424 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700425 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700426 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700427
428 ASSERT_EQ(42, fn());
429 ASSERT_EQ(43, fn2());
430 dlclose(handle);
431}
432
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700433TEST(dlfcn, dlopen_check_order_reloc_siblings) {
434 // This is how this one works:
435 // we lookup and call get_answer which is defined in '_2.so'
436 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
437 // the correct _impl() is implemented by '_a.so';
438 //
439 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
440 //
441 // Here is the picture:
442 //
443 // libtest_check_order_reloc_siblings.so
444 // |
445 // +-> ..._1.so <- empty
446 // | |
447 // | +-> ..._a.so <- exports correct answer_impl()
448 // | |
449 // | +-> ..._b.so <- every other letter exporting incorrect one.
450 // |
451 // +-> ..._2.so <- empty
452 // | |
453 // | +-> ..._c.so
454 // | |
455 // | +-> ..._d.so
456 // |
457 // +-> ..._3.so <- empty
458 // |
459 // +-> ..._e.so
460 // |
461 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
462 // implements incorrect get_answer_impl()
463
464 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
465 ASSERT_TRUE(handle == nullptr);
466#ifdef __BIONIC__
467 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
468 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
469#endif
470
471 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
472 ASSERT_TRUE(handle != nullptr) << dlerror();
473
474 typedef int (*fn_t) (void);
475 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
476 ASSERT_TRUE(fn != nullptr) << dlerror();
477 ASSERT_EQ(42, fn());
478
479 ASSERT_EQ(0, dlclose(handle));
480}
481
482TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
483 // This test uses the same library as dlopen_check_order_reloc_siblings.
484 // Unlike dlopen_check_order_reloc_siblings it preloads
485 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
486 // dlopen(libtest_check_order_reloc_siblings.so)
487
488 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
489 ASSERT_TRUE(handle == nullptr);
490 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
491 ASSERT_TRUE(handle == nullptr);
492
493 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
494 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
495
496 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
497 ASSERT_TRUE(handle != nullptr) << dlerror();
498
499 ASSERT_EQ(0, dlclose(handle_for_1));
500
501 typedef int (*fn_t) (void);
502 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
503 ASSERT_TRUE(fn != nullptr) << dlerror();
504 ASSERT_EQ(42, fn());
505
506 ASSERT_EQ(0, dlclose(handle));
507}
508
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800509TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
510 // This is how this one works:
511 // we lookup and call grandchild_get_answer which is defined in '_2.so'
512 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
513 // the correct _impl() is implemented by '_c_1.so';
514 //
515 // Here is the picture of subtree:
516 //
517 // libtest_check_order_reloc_siblings.so
518 // |
519 // +-> ..._2.so <- grandchild_get_answer()
520 // |
521 // +-> ..._c.so <- empty
522 // | |
523 // | +-> _c_1.so <- exports correct answer_impl()
524 // | |
525 // | +-> _c_2.so <- exports incorrect answer_impl()
526 // |
527 // +-> ..._d.so <- empty
528
529 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
530 ASSERT_TRUE(handle == nullptr);
531#ifdef __BIONIC__
532 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
533 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
534#endif
535
536 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
537 ASSERT_TRUE(handle != nullptr) << dlerror();
538
539 typedef int (*fn_t) (void);
540 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
541 ASSERT_TRUE(fn != nullptr) << dlerror();
542 ASSERT_EQ(42, fn());
543
544 ASSERT_EQ(0, dlclose(handle));
545}
546
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700547TEST(dlfcn, dlopen_check_order_reloc_nephew) {
548 // This is how this one works:
549 // we lookup and call nephew_get_answer which is defined in '_2.so'
550 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
551 // the correct _impl() is implemented by '_a.so';
552 //
553 // Here is the picture:
554 //
555 // libtest_check_order_reloc_siblings.so
556 // |
557 // +-> ..._1.so <- empty
558 // | |
559 // | +-> ..._a.so <- exports correct answer_impl()
560 // | |
561 // | +-> ..._b.so <- every other letter exporting incorrect one.
562 // |
563 // +-> ..._2.so <- empty
564 // | |
565 // | +-> ..._c.so
566 // | |
567 // | +-> ..._d.so
568 // |
569 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
570 // |
571 // +-> ..._e.so
572 // |
573 // +-> ..._f.so
574
575 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
576 ASSERT_TRUE(handle == nullptr);
577#ifdef __BIONIC__
578 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
579 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
580#endif
581
582 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
583 ASSERT_TRUE(handle != nullptr) << dlerror();
584
585 typedef int (*fn_t) (void);
586 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
587 ASSERT_TRUE(fn != nullptr) << dlerror();
588 ASSERT_EQ(42, fn());
589
590 ASSERT_EQ(0, dlclose(handle));
591}
592
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800593TEST(dlfcn, check_unload_after_reloc) {
594 // This is how this one works:
595 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
596 // |
597 // +-> libtest_two_parents_child
598 //
599 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
600 // |
601 // +-> libtest_two_parents_child
602 //
603 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
604 // as a second step it dlopens parent2 and dlcloses parent1...
605
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700606 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
607 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800608
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700609 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
610 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800611
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700612 typedef int (*fn_t) (void);
613 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
614 ASSERT_TRUE(fn != nullptr) << dlerror();
615 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800616
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700617 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800618
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700619 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
620 ASSERT_TRUE(handle != nullptr);
621 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800622
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700623 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
624 ASSERT_TRUE(fn != nullptr) << dlerror();
625 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800626
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700627 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800628
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700629 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
630 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800631}
632
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700633extern "C" int check_order_reloc_root_get_answer_impl() {
634 return 42;
635}
636
637TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
638 // This is how this one works:
639 // we lookup and call get_answer3 which is defined in 'root.so'
640 // and in turn calls external root_get_answer_impl() defined in _2.so and
641 // above the correct _impl() is one in the executable.
642 //
643 // libtest_check_order_reloc_root.so
644 // |
645 // +-> ..._1.so <- empty
646 // |
647 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
648 //
649
650 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
651 ASSERT_TRUE(handle == nullptr);
652#ifdef __BIONIC__
653 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
654 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
655#endif
656
657 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
658 ASSERT_TRUE(handle != nullptr) << dlerror();
659
660 typedef int (*fn_t) (void);
661 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
662 ASSERT_TRUE(fn != nullptr) << dlerror();
663 ASSERT_EQ(42, fn());
664
665 ASSERT_EQ(0, dlclose(handle));
666}
667
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700668TEST(dlfcn, dlopen_check_rtld_local) {
669 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
670 ASSERT_TRUE(sym == nullptr);
671
672 // implicit RTLD_LOCAL
673 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
674 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
675 ASSERT_TRUE(sym == nullptr);
676 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
677 sym = dlsym(handle, "dlopen_testlib_simple_func");
678 ASSERT_TRUE(sym != nullptr);
679 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
680 dlclose(handle);
681
682 // explicit RTLD_LOCAL
683 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
684 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
685 ASSERT_TRUE(sym == nullptr);
686 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
687 sym = dlsym(handle, "dlopen_testlib_simple_func");
688 ASSERT_TRUE(sym != nullptr);
689 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
690 dlclose(handle);
691}
692
693TEST(dlfcn, dlopen_check_rtld_global) {
694 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
695 ASSERT_TRUE(sym == nullptr);
696
697 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700698 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700699 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
700 ASSERT_TRUE(sym != nullptr) << dlerror();
701 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
702 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700703
704 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
705 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
706 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700707
708 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
709 // shared libraries after symbol was not found in the main executable
710 // and dependent libraries.
711 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
712 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
713 ASSERT_TRUE(sym != nullptr) << dlerror();
714
715 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700716}
717
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700718// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
719// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
720// libtest_with_dependency_loop_a.so
721TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700722 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
723 ASSERT_TRUE(handle != nullptr) << dlerror();
724 void* f = dlsym(handle, "dlopen_test_loopy_function");
725 ASSERT_TRUE(f != nullptr) << dlerror();
726 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
727 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700728
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700729 // dlopen second time to make sure that the library was unloaded correctly
730 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
731 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800732#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700733 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 -0800734#else
735 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
736 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700737#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800738
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700739 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
740 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700741}
742
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700743TEST(dlfcn, dlopen_nodelete) {
744 static bool is_unloaded = false;
745
746 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
747 ASSERT_TRUE(handle != nullptr) << dlerror();
748 void (*set_unload_flag_ptr)(bool*);
749 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
750 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
751 set_unload_flag_ptr(&is_unloaded);
752
753 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
754 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
755 ASSERT_EQ(1729U, *taxicab_number);
756 *taxicab_number = 2;
757
758 dlclose(handle);
759 ASSERT_TRUE(!is_unloaded);
760
761 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
762 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
763 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
764
765
766 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
767 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
768 ASSERT_EQ(taxicab_number2, taxicab_number);
769
770 ASSERT_EQ(2U, *taxicab_number2);
771
772 dlclose(handle);
773 ASSERT_TRUE(!is_unloaded);
774}
775
776TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
777 static bool is_unloaded = false;
778
779 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
780 ASSERT_TRUE(handle != nullptr) << dlerror();
781 void (*set_unload_flag_ptr)(bool*);
782 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
783 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
784 set_unload_flag_ptr(&is_unloaded);
785
786 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
787 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
788
789 ASSERT_EQ(1729U, *taxicab_number);
790 *taxicab_number = 2;
791
792 // This RTLD_NODELETE should be ignored
793 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
794 ASSERT_TRUE(handle1 != nullptr) << dlerror();
795 ASSERT_EQ(handle, handle1);
796
797 dlclose(handle1);
798 dlclose(handle);
799
800 ASSERT_TRUE(is_unloaded);
801}
802
803TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
804 static bool is_unloaded = false;
805
806 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
807 ASSERT_TRUE(handle != nullptr) << dlerror();
808 void (*set_unload_flag_ptr)(bool*);
809 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
810 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
811 set_unload_flag_ptr(&is_unloaded);
812
813 dlclose(handle);
814 ASSERT_TRUE(!is_unloaded);
815}
816
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700817TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700818 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
819 ASSERT_TRUE(handle != nullptr) << dlerror();
820 int (*get_answer)();
821 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
822 ASSERT_TRUE(get_answer != nullptr) << dlerror();
823 ASSERT_EQ(42, get_answer());
824 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700825}
826
Elliott Hughese66190d2012-12-18 15:57:55 -0800827TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700828 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700829 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700830#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700831 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
832#else
833 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
834#endif
835}
836
dimitry109040c2017-11-03 13:49:07 +0100837TEST(dlfcn, dlclose_unload) {
838 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
839 ASSERT_TRUE(handle != nullptr) << dlerror();
840 uint32_t* taxicab_number = static_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
841 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
842 EXPECT_EQ(1729U, *taxicab_number);
843 dlclose(handle);
844 // Making sure that the library has been unmapped as part of library unload
845 // process. Note that mprotect somewhat counter-intuitively returns ENOMEM in
846 // this case.
847 uintptr_t page_start = reinterpret_cast<uintptr_t>(taxicab_number) & ~(PAGE_SIZE - 1);
848 ASSERT_TRUE(mprotect(reinterpret_cast<void*>(page_start), PAGE_SIZE, PROT_NONE) != 0);
849 ASSERT_EQ(ENOMEM, errno) << strerror(errno);
850}
851
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800852static void ConcurrentDlErrorFn(std::string& error) {
853 ASSERT_TRUE(dlerror() == nullptr);
854
855 void* handle = dlopen("/child/thread", RTLD_NOW);
856 ASSERT_TRUE(handle == nullptr);
857
858 const char* err = dlerror();
859 ASSERT_TRUE(err != nullptr);
860
861 error = err;
862}
863
864TEST(dlfcn, dlerror_concurrent_buffer) {
865 void* handle = dlopen("/main/thread", RTLD_NOW);
866 ASSERT_TRUE(handle == nullptr);
867 const char* main_thread_error = dlerror();
868 ASSERT_TRUE(main_thread_error != nullptr);
869 ASSERT_SUBSTR("/main/thread", main_thread_error);
870
871 std::string child_thread_error;
872 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
873 t.join();
874 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
875
876 // Check that main thread local buffer was not modified.
877 ASSERT_SUBSTR("/main/thread", main_thread_error);
Elliott Hughes5419b942012-10-16 15:54:46 -0700878}
879
Elliott Hughese66190d2012-12-18 15:57:55 -0800880TEST(dlfcn, dlerror_concurrent) {
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800881 void* handle = dlopen("/main/thread", RTLD_NOW);
882 ASSERT_TRUE(handle == nullptr);
883
884 std::string child_thread_error;
885 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
886 t.join();
887 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
888
Elliott Hughes5419b942012-10-16 15:54:46 -0700889 const char* main_thread_error = dlerror();
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800890 ASSERT_TRUE(main_thread_error != nullptr);
Elliott Hughes5419b942012-10-16 15:54:46 -0700891 ASSERT_SUBSTR("/main/thread", main_thread_error);
892}
893
Elliott Hughese66190d2012-12-18 15:57:55 -0800894TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700895 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700896 void* self = dlopen(nullptr, RTLD_NOW);
897 ASSERT_TRUE(self != nullptr);
898 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700899
900 void* sym;
901
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700902#if defined(__BIONIC__) && !defined(__LP64__)
903 // RTLD_DEFAULT in lp32 bionic is not (void*)0
904 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700905 sym = dlsym(nullptr, "test");
906 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800907 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700908#endif
909
Elliott Hughes3b297c42012-10-11 16:08:51 -0700910 // Symbol that doesn't exist.
911 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700912 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700913 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700914
915 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700916}
917
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700918TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700919 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700920 void* self = dlopen(nullptr, RTLD_NOW);
921 ASSERT_TRUE(self != nullptr);
922 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700923
924 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700925 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700926
927 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
928 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
929
930 Dl_info info;
931 int rc = dladdr(addr, &info);
932 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
933
934 // Get the name of this executable.
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700935 const std::string executable_path = android::base::GetExecutablePath();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700936
937 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700938 char dli_realpath[PATH_MAX];
939 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700940 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700941
942 // The symbol name should be the symbol we looked up.
943 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
944
945 // The address should be the exact address of the symbol.
946 ASSERT_EQ(info.dli_saddr, sym);
947
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700948 std::vector<map_record> maps;
949 ASSERT_TRUE(Maps::parse_maps(&maps));
950
951 void* base_address = nullptr;
952 for (const map_record& rec : maps) {
953 if (executable_path == rec.pathname) {
954 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700955 break;
956 }
957 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700958
959 // The base address should be the address we were loaded at.
960 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700961
962 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700963}
964
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700965TEST(dlfcn, dlopen_executable_by_absolute_path) {
966 void* handle1 = dlopen(nullptr, RTLD_NOW);
967 ASSERT_TRUE(handle1 != nullptr) << dlerror();
968
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700969 void* handle2 = dlopen(android::base::GetExecutablePath().c_str(), RTLD_NOW);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700970 ASSERT_TRUE(handle2 != nullptr) << dlerror();
971
972#if defined(__BIONIC__)
973 ASSERT_EQ(handle1, handle2);
974#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800975 GTEST_SKIP() << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
976 "it loads a separate copy of the main executable "
977 "on dlopen by absolute path";
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700978#endif
979}
980
Victor Khimenko14b9d712017-01-25 19:59:16 +0100981#if defined (__aarch64__)
Dmytro Chystiakove712cd12019-03-29 13:49:12 -0700982#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib64/arm64/"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100983#elif defined (__arm__)
984#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm/"
985#elif defined (__i386__)
986#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86/"
987#elif defined (__x86_64__)
Dmytro Chystiakove712cd12019-03-29 13:49:12 -0700988#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib64/x86_64/"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100989#else
990#error "Unknown architecture"
991#endif
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200992#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Peter Collingbourneea11be02019-04-30 16:05:13 -0700993#define PATH_TO_BOOTSTRAP_LIBC PATH_TO_SYSTEM_LIB "bootstrap/libc.so"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100994#define ALTERNATE_PATH_TO_LIBC ALTERNATE_PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700995
996TEST(dlfcn, dladdr_libc) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800997#if defined(__GLIBC__)
998 GTEST_SKIP() << "glibc returns libc.so's ldconfig path, which is a symlink (not a realpath)";
999#endif
1000
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001001 Dl_info info;
1002 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
1003 ASSERT_TRUE(dladdr(addr, &info) != 0);
1004
Dmitriy Ivanovef255922015-04-08 11:53:08 -07001005 char libc_realpath[PATH_MAX];
Victor Khimenko14b9d712017-01-25 19:59:16 +01001006
1007 // Check if libc is in canonical path or in alternate path.
1008 if (strncmp(ALTERNATE_PATH_TO_SYSTEM_LIB,
1009 info.dli_fname,
1010 sizeof(ALTERNATE_PATH_TO_SYSTEM_LIB) - 1) == 0) {
1011 // Platform with emulated architecture. Symlink on ARC++.
1012 ASSERT_TRUE(realpath(ALTERNATE_PATH_TO_LIBC, libc_realpath) == libc_realpath);
Peter Collingbourneea11be02019-04-30 16:05:13 -07001013 } else if (strncmp(PATH_TO_BOOTSTRAP_LIBC, info.dli_fname,
1014 sizeof(PATH_TO_BOOTSTRAP_LIBC) - 1) == 0) {
1015 ASSERT_TRUE(realpath(PATH_TO_BOOTSTRAP_LIBC, libc_realpath) == libc_realpath);
Victor Khimenko14b9d712017-01-25 19:59:16 +01001016 } else {
1017 // /system/lib is symlink when this test is executed on host.
1018 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
1019 }
Dmitriy Ivanovef255922015-04-08 11:53:08 -07001020
1021 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001022 // TODO: add check for dfi_fbase
1023 ASSERT_STREQ("puts", info.dli_sname);
1024 ASSERT_EQ(addr, info.dli_saddr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001025}
1026
Elliott Hughese66190d2012-12-18 15:57:55 -08001027TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -07001028 Dl_info info;
1029
Elliott Hughes3b297c42012-10-11 16:08:51 -07001030 dlerror(); // Clear any pending errors.
1031
Elliott Hughes8e15b082012-09-26 11:44:01 -07001032 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001033 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
1034 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001035
1036 // No symbol corresponding to a stack address.
1037 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001038 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001039}
Elliott Hughes124fae92012-10-31 14:20:03 -07001040
Elliott Hughese66190d2012-12-18 15:57:55 -08001041TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001042 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001043 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
1044 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001045 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001046 void* sym = dlsym(handle, "getRandomNumber");
1047 ASSERT_TRUE(sym != nullptr) << dlerror();
1048 int (*fn)(void);
1049 fn = reinterpret_cast<int (*)(void)>(sym);
1050 EXPECT_EQ(4, fn());
1051
1052 Dl_info dlinfo;
1053 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1054
1055 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1056 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001057 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Elliott Hughes124fae92012-10-31 14:20:03 -07001058}
Elliott Hughese66190d2012-12-18 15:57:55 -08001059
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001060TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
1061 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
1062 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001063 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001064 void* sym = dlsym(handle, "getRandomNumber");
1065 ASSERT_TRUE(sym != nullptr) << dlerror();
1066 int (*fn)(void);
1067 fn = reinterpret_cast<int (*)(void)>(sym);
1068 EXPECT_EQ(4, fn());
1069
1070 Dl_info dlinfo;
1071 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1072
1073 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1074 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
1075 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
1076}
1077
Elliott Hughese66190d2012-12-18 15:57:55 -08001078TEST(dlfcn, dlopen_bad_flags) {
1079 dlerror(); // Clear any pending errors.
1080 void* handle;
1081
Elliott Hughes063525c2014-05-13 11:19:57 -07001082#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -08001083 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001084 handle = dlopen(nullptr, 0);
1085 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001086 ASSERT_SUBSTR("invalid", dlerror());
1087#endif
1088
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001089 handle = dlopen(nullptr, 0xffffffff);
1090 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001091 ASSERT_SUBSTR("invalid", dlerror());
1092
1093 // 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 -07001094 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
1095 ASSERT_TRUE(handle != nullptr);
1096 ASSERT_SUBSTR(nullptr, dlerror());
Elliott Hughese66190d2012-12-18 15:57:55 -08001097}
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001098
1099TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001100 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001101 ASSERT_TRUE(addr == nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001102}
1103
1104TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001105 void* addr = dlsym(RTLD_DEFAULT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001106 ASSERT_TRUE(addr != nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001107}
1108
1109TEST(dlfcn, rtld_next_unknown_symbol) {
1110 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001111 ASSERT_TRUE(addr == nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001112}
1113
1114TEST(dlfcn, rtld_next_known_symbol) {
1115 void* addr = dlsym(RTLD_NEXT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001116 ASSERT_TRUE(addr != nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001117}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001118
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001119// Check that RTLD_NEXT of a libc symbol works in dlopened library
1120TEST(dlfcn, rtld_next_from_library) {
dimitry153168c2018-02-20 16:51:41 +01001121 void* library_with_fclose = dlopen("libtest_check_rtld_next_from_library.so", RTLD_NOW | RTLD_GLOBAL);
Raj Mamadgi527229c2017-11-02 15:53:50 -07001122 ASSERT_TRUE(library_with_fclose != nullptr) << dlerror();
1123 void* expected_addr = dlsym(RTLD_DEFAULT, "fclose");
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001124 ASSERT_TRUE(expected_addr != nullptr) << dlerror();
Raj Mamadgi527229c2017-11-02 15:53:50 -07001125 typedef void* (*get_libc_fclose_ptr_fn_t)();
1126 get_libc_fclose_ptr_fn_t get_libc_fclose_ptr =
1127 reinterpret_cast<get_libc_fclose_ptr_fn_t>(dlsym(library_with_fclose, "get_libc_fclose_ptr"));
1128 ASSERT_TRUE(get_libc_fclose_ptr != nullptr) << dlerror();
1129 ASSERT_EQ(expected_addr, get_libc_fclose_ptr());
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001130
Raj Mamadgi527229c2017-11-02 15:53:50 -07001131 dlclose(library_with_fclose);
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001132}
1133
1134
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001135TEST(dlfcn, dlsym_weak_func) {
1136 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001137 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001138 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001139
1140 int (*weak_func)();
1141 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001142 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001143 EXPECT_EQ(42, weak_func());
1144 dlclose(handle);
1145}
1146
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001147TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001148 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
1149 ASSERT_TRUE(handle != nullptr) << dlerror();
1150 int (*weak_func)();
1151 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
1152 ASSERT_TRUE(weak_func != nullptr) << dlerror();
1153 EXPECT_EQ(6551, weak_func());
1154 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001155}
1156
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001157TEST(dlfcn, dlopen_symlink) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001158 DlfcnSymlink symlink("dlopen_symlink");
1159 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001160 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001161 void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001162 ASSERT_TRUE(handle1 != nullptr);
1163 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001164 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -07001165 dlclose(handle1);
1166 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001167}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001168
1169// libtest_dlopen_from_ctor_main.so depends on
1170// libtest_dlopen_from_ctor.so which has a constructor
1171// that calls dlopen(libc...). This is to test the situation
1172// described in b/7941716.
1173TEST(dlfcn, dlopen_dlopen_from_ctor) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001174#if defined(__GLIBC__)
1175 GTEST_SKIP() << "glibc segfaults if you try to call dlopen from a constructor";
1176#endif
1177
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001178 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1179 ASSERT_TRUE(handle != nullptr) << dlerror();
1180 dlclose(handle);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001181}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001182
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001183static std::string g_fini_call_order_str;
1184
1185static void register_fini_call(const char* s) {
1186 g_fini_call_order_str += s;
1187}
1188
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001189static void test_init_fini_call_order_for(const char* libname) {
1190 g_fini_call_order_str.clear();
1191 void* handle = dlopen(libname, RTLD_NOW);
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001192 ASSERT_TRUE(handle != nullptr) << dlerror();
1193 typedef int (*get_init_order_number_t)();
1194 get_init_order_number_t get_init_order_number =
1195 reinterpret_cast<get_init_order_number_t>(dlsym(handle, "get_init_order_number"));
1196 ASSERT_EQ(321, get_init_order_number());
1197
1198 typedef void (*set_fini_callback_t)(void (*f)(const char*));
1199 set_fini_callback_t set_fini_callback =
1200 reinterpret_cast<set_fini_callback_t>(dlsym(handle, "set_fini_callback"));
1201 set_fini_callback(register_fini_call);
1202 dlclose(handle);
1203 ASSERT_EQ("(root)(child)(grandchild)", g_fini_call_order_str);
1204}
1205
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001206TEST(dlfcn, init_fini_call_order) {
1207 test_init_fini_call_order_for("libtest_init_fini_order_root.so");
1208 test_init_fini_call_order_for("libtest_init_fini_order_root2.so");
1209}
1210
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001211TEST(dlfcn, symbol_versioning_use_v1) {
1212 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1213 ASSERT_TRUE(handle != nullptr) << dlerror();
1214 typedef int (*fn_t)();
1215 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1216 ASSERT_TRUE(fn != nullptr) << dlerror();
1217 ASSERT_EQ(1, fn());
1218 dlclose(handle);
1219}
1220
1221TEST(dlfcn, symbol_versioning_use_v2) {
1222 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1223 ASSERT_TRUE(handle != nullptr) << dlerror();
1224 typedef int (*fn_t)();
1225 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1226 ASSERT_TRUE(fn != nullptr) << dlerror();
1227 ASSERT_EQ(2, fn());
1228 dlclose(handle);
1229}
1230
1231TEST(dlfcn, symbol_versioning_use_other_v2) {
1232 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1233 ASSERT_TRUE(handle != nullptr) << dlerror();
1234 typedef int (*fn_t)();
1235 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1236 ASSERT_TRUE(fn != nullptr) << dlerror();
1237 ASSERT_EQ(20, fn());
1238 dlclose(handle);
1239}
1240
1241TEST(dlfcn, symbol_versioning_use_other_v3) {
1242 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1243 ASSERT_TRUE(handle != nullptr) << dlerror();
1244 typedef int (*fn_t)();
1245 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1246 ASSERT_TRUE(fn != nullptr) << dlerror();
1247 ASSERT_EQ(3, fn());
1248 dlclose(handle);
1249}
1250
1251TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1252 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1253 ASSERT_TRUE(handle != nullptr) << dlerror();
1254 typedef int (*fn_t)();
1255 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1256 ASSERT_TRUE(fn != nullptr) << dlerror();
1257 ASSERT_EQ(3, fn()); // the default version is 3
1258 dlclose(handle);
1259}
1260
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001261TEST(dlfcn, dlvsym_smoke) {
1262 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1263 ASSERT_TRUE(handle != nullptr) << dlerror();
1264 typedef int (*fn_t)();
1265
1266 {
1267 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1268 ASSERT_TRUE(fn == nullptr);
1269 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1270 }
1271
1272 {
1273 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1274 ASSERT_TRUE(fn != nullptr) << dlerror();
1275 ASSERT_EQ(2, fn());
1276 }
1277
1278 dlclose(handle);
1279}
1280
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001281// This preempts the implementation from libtest_versioned_lib.so
1282extern "C" int version_zero_function() {
1283 return 0;
1284}
1285
1286// This preempts the implementation from libtest_versioned_uselibv*.so
1287extern "C" int version_zero_function2() {
1288 return 0;
1289}
Evgenii Stepanov68650822015-06-10 13:38:39 -07001290
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001291TEST(dlfcn, dt_runpath_smoke) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001292 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1293 ASSERT_TRUE(handle != nullptr) << dlerror();
1294
1295 typedef void *(* dlopen_b_fn)();
1296 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1297 ASSERT_TRUE(fn != nullptr) << dlerror();
1298
1299 void *p = fn();
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001300 ASSERT_TRUE(p != nullptr);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001301
1302 dlclose(handle);
1303}
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001304
Dimitry Ivanovdb6ab3d2017-06-27 11:02:51 -07001305TEST(dlfcn, dt_runpath_absolute_path) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001306 std::string libpath = GetTestlibRoot() + "/libtest_dt_runpath_d.so";
Dimitry Ivanovdb6ab3d2017-06-27 11:02:51 -07001307 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1308 ASSERT_TRUE(handle != nullptr) << dlerror();
1309
1310 typedef void *(* dlopen_b_fn)();
1311 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1312 ASSERT_TRUE(fn != nullptr) << dlerror();
1313
1314 void *p = fn();
1315 ASSERT_TRUE(p != nullptr);
1316
1317 dlclose(handle);
1318}
1319
dimitry55547db2018-05-25 14:17:37 +02001320static void test_dlclose_after_thread_local_dtor(const char* library_name) {
dimitry06016f22018-01-05 11:39:28 +01001321 bool is_dtor_triggered = false;
1322
1323 auto f = [](void* handle, bool* is_dtor_triggered) {
1324 typedef void (*fn_t)(bool*);
1325 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1326 ASSERT_TRUE(fn != nullptr) << dlerror();
1327
1328 fn(is_dtor_triggered);
1329
1330 ASSERT_TRUE(!*is_dtor_triggered);
1331 };
1332
dimitry55547db2018-05-25 14:17:37 +02001333 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001334 ASSERT_TRUE(handle == nullptr);
1335
dimitry55547db2018-05-25 14:17:37 +02001336 handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001337 ASSERT_TRUE(handle != nullptr) << dlerror();
1338
1339 std::thread t(f, handle, &is_dtor_triggered);
1340 t.join();
1341
1342 ASSERT_TRUE(is_dtor_triggered);
1343 dlclose(handle);
1344
dimitry55547db2018-05-25 14:17:37 +02001345 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001346 ASSERT_TRUE(handle == nullptr);
1347}
1348
dimitry55547db2018-05-25 14:17:37 +02001349TEST(dlfcn, dlclose_after_thread_local_dtor) {
1350 test_dlclose_after_thread_local_dtor("libtest_thread_local_dtor.so");
1351}
1352
1353TEST(dlfcn, dlclose_after_thread_local_dtor_indirect) {
1354 test_dlclose_after_thread_local_dtor("libtest_indirect_thread_local_dtor.so");
1355}
1356
1357static void test_dlclose_before_thread_local_dtor(const char* library_name) {
dimitry06016f22018-01-05 11:39:28 +01001358 bool is_dtor_triggered = false;
1359
dimitry55547db2018-05-25 14:17:37 +02001360 auto f = [library_name](bool* is_dtor_triggered) {
1361 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001362 ASSERT_TRUE(handle == nullptr);
1363
dimitry55547db2018-05-25 14:17:37 +02001364 handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001365 ASSERT_TRUE(handle != nullptr) << dlerror();
1366
1367 typedef void (*fn_t)(bool*);
1368 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1369 ASSERT_TRUE(fn != nullptr) << dlerror();
1370
1371 fn(is_dtor_triggered);
1372
1373 dlclose(handle);
1374
1375 ASSERT_TRUE(!*is_dtor_triggered);
1376
1377 // Since we have thread_atexit dtors associated with handle - the library should
1378 // still be availabe.
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) << dlerror();
1381 dlclose(handle);
1382 };
1383
dimitry55547db2018-05-25 14:17:37 +02001384 void* handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001385 ASSERT_TRUE(handle != nullptr) << dlerror();
1386 dlclose(handle);
1387
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
1391 std::thread t(f, &is_dtor_triggered);
1392 t.join();
1393#if defined(__BIONIC__)
1394 // ld-android.so unloads unreferenced libraries on pthread_exit()
1395 ASSERT_TRUE(is_dtor_triggered);
dimitry55547db2018-05-25 14:17:37 +02001396 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001397 ASSERT_TRUE(handle == nullptr);
1398#else
1399 // GLIBC does not unload libraries with ref_count = 0 on pthread_exit
1400 ASSERT_TRUE(is_dtor_triggered);
dimitry55547db2018-05-25 14:17:37 +02001401 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1402 ASSERT_TRUE(handle != nullptr) << dlerror();
1403#endif
1404}
1405
1406TEST(dlfcn, dlclose_before_thread_local_dtor) {
1407 test_dlclose_before_thread_local_dtor("libtest_thread_local_dtor.so");
1408}
1409
1410TEST(dlfcn, dlclose_before_thread_local_dtor_indirect) {
1411 test_dlclose_before_thread_local_dtor("libtest_indirect_thread_local_dtor.so");
1412}
1413
1414TEST(dlfcn, dlclose_before_thread_local_dtor_multiple_dsos) {
1415 const constexpr char* library_name = "libtest_indirect_thread_local_dtor.so";
1416
1417 bool is_dtor1_triggered = false;
1418 bool is_dtor2_triggered = false;
1419
1420 std::mutex mtx;
1421 std::condition_variable cv;
1422 void* library_handle = nullptr;
1423 bool thread1_dlopen_complete = false;
1424 bool thread2_thread_local_dtor_initialized = false;
1425 bool thread1_complete = false;
1426
1427 auto f1 = [&]() {
1428 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1429 ASSERT_TRUE(handle == nullptr);
1430
1431 handle = dlopen(library_name, RTLD_NOW);
1432 ASSERT_TRUE(handle != nullptr) << dlerror();
1433 std::unique_lock<std::mutex> lock(mtx);
1434 thread1_dlopen_complete = true;
1435 library_handle = handle;
1436 lock.unlock();
1437 cv.notify_one();
1438
1439 typedef void (*fn_t)(bool*);
1440 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1441 ASSERT_TRUE(fn != nullptr) << dlerror();
1442
1443 fn(&is_dtor1_triggered);
1444
1445 lock.lock();
1446 cv.wait(lock, [&] { return thread2_thread_local_dtor_initialized; });
1447 lock.unlock();
1448
1449 dlclose(handle);
1450
1451 ASSERT_TRUE(!is_dtor1_triggered);
1452
1453 // Since we have thread_atexit dtors associated with handle - the library should
1454 // still be availabe.
1455 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1456 ASSERT_TRUE(handle != nullptr) << dlerror();
1457 dlclose(handle);
1458 };
1459
1460 auto f2 = [&]() {
1461 std::unique_lock<std::mutex> lock(mtx);
1462 cv.wait(lock, [&] { return thread1_dlopen_complete; });
1463 void* handle = library_handle;
1464 lock.unlock();
1465
1466 typedef void (*fn_t)(bool*);
1467 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable2"));
1468 ASSERT_TRUE(fn != nullptr) << dlerror();
1469
1470 fn(&is_dtor2_triggered);
1471
1472 lock.lock();
1473 thread2_thread_local_dtor_initialized = true;
1474 lock.unlock();
1475 cv.notify_one();
1476
1477 lock.lock();
1478 cv.wait(lock, [&] { return thread1_complete; });
1479 lock.unlock();
1480
1481 ASSERT_TRUE(!is_dtor2_triggered);
1482 };
1483
1484 void* handle = dlopen(library_name, RTLD_NOW);
1485 ASSERT_TRUE(handle != nullptr) << dlerror();
1486 dlclose(handle);
1487
1488 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1489 ASSERT_TRUE(handle == nullptr);
1490
1491 std::thread t1(f1);
1492 std::thread t2(f2);
1493 t1.join();
1494 ASSERT_TRUE(is_dtor1_triggered);
1495 ASSERT_TRUE(!is_dtor2_triggered);
1496
1497 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1498 ASSERT_TRUE(handle != nullptr) << dlerror();
1499 dlclose(handle);
1500
1501 std::unique_lock<std::mutex> lock(mtx);
1502 thread1_complete = true;
1503 lock.unlock();
1504 cv.notify_one();
1505
1506 t2.join();
1507 ASSERT_TRUE(is_dtor2_triggered);
1508
1509#if defined(__BIONIC__)
1510 // ld-android.so unloads unreferenced libraries on pthread_exit()
1511 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1512 ASSERT_TRUE(handle == nullptr);
1513#else
1514 // GLIBC does not unload libraries with ref_count = 0 on pthread_exit
1515 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001516 ASSERT_TRUE(handle != nullptr) << dlerror();
1517#endif
1518}
1519
Elliott Hughes8ad40932017-06-15 15:12:29 -07001520TEST(dlfcn, RTLD_macros) {
1521#if !defined(RTLD_LOCAL)
1522#error no RTLD_LOCAL
1523#elif !defined(RTLD_LAZY)
1524#error no RTLD_LAZY
1525#elif !defined(RTLD_NOW)
1526#error no RTLD_NOW
1527#elif !defined(RTLD_NOLOAD)
1528#error no RTLD_NOLOAD
1529#elif !defined(RTLD_GLOBAL)
1530#error no RTLD_GLOBAL
1531#elif !defined(RTLD_NODELETE)
1532#error no RTLD_NODELETE
1533#endif
1534}
1535
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001536// Bionic specific tests
1537#if defined(__BIONIC__)
1538
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001539#if defined(__arm__)
1540const llvm::ELF::Elf32_Dyn* to_dynamic_table(const char* p) {
1541 return reinterpret_cast<const llvm::ELF::Elf32_Dyn*>(p);
1542}
1543
1544// Duplicate these definitions here because they are android specific
Ryan Prichard68140282018-05-23 14:20:29 -07001545// - note that we cannot include <elf.h> because #defines conflict with
1546// enum names provided by LLVM.
1547// - we also don't use llvm::ELF::DT_LOOS because its value is 0x60000000
1548// rather than the 0x6000000d we expect
1549#define DT_LOOS 0x6000000d
1550#define DT_ANDROID_REL (DT_LOOS + 2)
1551#define DT_ANDROID_RELA (DT_LOOS + 4)
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001552
1553template<typename ELFT>
Ryan Prichard470b6662018-03-27 22:10:55 -07001554void validate_compatibility_of_native_library(const std::string& soname,
1555 const std::string& path, ELFT* elf) {
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001556 bool has_elf_hash = false;
1557 bool has_android_rel = false;
1558 bool has_rel = false;
1559 // Find dynamic section and check that DT_HASH and there is no DT_ANDROID_REL
1560 for (auto it = elf->section_begin(); it != elf->section_end(); ++it) {
1561 const llvm::object::ELFSectionRef& section_ref = *it;
1562 if (section_ref.getType() == llvm::ELF::SHT_DYNAMIC) {
1563 llvm::StringRef data;
1564 ASSERT_TRUE(!it->getContents(data)) << "unable to get SHT_DYNAMIC section data";
1565 for (auto d = to_dynamic_table(data.data()); d->d_tag != llvm::ELF::DT_NULL; ++d) {
1566 if (d->d_tag == llvm::ELF::DT_HASH) {
1567 has_elf_hash = true;
1568 } else if (d->d_tag == DT_ANDROID_REL || d->d_tag == DT_ANDROID_RELA) {
1569 has_android_rel = true;
1570 } else if (d->d_tag == llvm::ELF::DT_REL || d->d_tag == llvm::ELF::DT_RELA) {
1571 has_rel = true;
1572 }
1573 }
1574
1575 break;
1576 }
1577 }
1578
1579 ASSERT_TRUE(has_elf_hash) << path.c_str() << ": missing elf hash (DT_HASH)";
1580 ASSERT_TRUE(!has_android_rel) << path.c_str() << ": has packed relocations";
Ryan Prichard470b6662018-03-27 22:10:55 -07001581 // libdl.so is simple enough that it might not have any relocations, so
1582 // exempt it from the DT_REL/DT_RELA check.
1583 if (soname != "libdl.so") {
1584 ASSERT_TRUE(has_rel) << path.c_str() << ": missing DT_REL/DT_RELA";
1585 }
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001586}
1587
Ryan Prichard470b6662018-03-27 22:10:55 -07001588void validate_compatibility_of_native_library(const std::string& soname) {
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001589 // On the systems with emulation system libraries would be of different
1590 // architecture. Try to use alternate paths first.
1591 std::string path = std::string(ALTERNATE_PATH_TO_SYSTEM_LIB) + soname;
1592 auto binary_or_error = llvm::object::createBinary(path);
1593 if (!binary_or_error) {
1594 path = std::string(PATH_TO_SYSTEM_LIB) + soname;
1595 binary_or_error = llvm::object::createBinary(path);
1596 }
1597 ASSERT_FALSE(!binary_or_error);
1598
1599 llvm::object::Binary* binary = binary_or_error.get().getBinary();
1600
1601 auto obj = llvm::dyn_cast<llvm::object::ObjectFile>(binary);
1602 ASSERT_TRUE(obj != nullptr);
1603
1604 auto elf = llvm::dyn_cast<llvm::object::ELF32LEObjectFile>(obj);
1605
1606 ASSERT_TRUE(elf != nullptr);
1607
Ryan Prichard470b6662018-03-27 22:10:55 -07001608 validate_compatibility_of_native_library(soname, path, elf);
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001609}
1610
1611// This is a test for app compatibility workaround for arm apps
1612// affected by http://b/24465209
1613TEST(dlext, compat_elf_hash_and_relocation_tables) {
1614 validate_compatibility_of_native_library("libc.so");
1615 validate_compatibility_of_native_library("liblog.so");
1616 validate_compatibility_of_native_library("libstdc++.so");
1617 validate_compatibility_of_native_library("libdl.so");
1618 validate_compatibility_of_native_library("libm.so");
1619 validate_compatibility_of_native_library("libz.so");
1620 validate_compatibility_of_native_library("libjnigraphics.so");
1621}
1622
1623#endif // defined(__arm__)
1624
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001625TEST(dlfcn, dlopen_invalid_rw_load_segment) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001626 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-rw_load_segment.so";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001627 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1628 ASSERT_TRUE(handle == nullptr);
Elliott Hughes9076b0c2018-02-28 11:29:45 -08001629 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W+E load segments are not allowed";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001630 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1631}
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001632
1633TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001634 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-unaligned_shdr_offset.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001635
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001636 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1637 ASSERT_TRUE(handle == nullptr);
1638 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
1639 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1640}
1641
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001642TEST(dlfcn, dlopen_invalid_zero_shentsize) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001643 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-zero_shentsize.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001644
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001645 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1646 ASSERT_TRUE(handle == nullptr);
1647 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x";
1648 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1649}
1650
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001651TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001652 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-zero_shstrndx.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001653
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001654 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1655 ASSERT_TRUE(handle == nullptr);
1656 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx";
1657 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1658}
1659
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001660TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001661 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-empty_shdr_table.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001662
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001663 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1664 ASSERT_TRUE(handle == nullptr);
1665 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers";
1666 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1667}
1668
Dimitry Ivanov46230442016-08-15 13:40:53 -07001669TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001670 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-zero_shdr_table_offset.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001671
Dimitry Ivanov46230442016-08-15 13:40:53 -07001672 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1673 ASSERT_TRUE(handle == nullptr);
1674 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/";
1675 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1676}
1677
Dimitry Ivanov55958342016-08-15 14:06:04 -07001678TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001679 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-zero_shdr_table_content.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001680
Dimitry Ivanov55958342016-08-15 14:06:04 -07001681 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1682 ASSERT_TRUE(handle == nullptr);
1683 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
1684 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1685}
1686
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001687TEST(dlfcn, dlopen_invalid_textrels) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001688 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-textrels.so";
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001689
1690 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1691 ASSERT_TRUE(handle == nullptr);
1692 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1693 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1694}
1695
1696TEST(dlfcn, dlopen_invalid_textrels2) {
Chris Parsonscab794c2020-06-15 18:22:10 -04001697 const std::string libpath = GetPrebuiltElfDir() + "/libtest_invalid-textrels2.so";
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001698
1699 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1700 ASSERT_TRUE(handle == nullptr);
1701 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1702 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1703}
1704
Jiyong Park01162f22017-10-16 15:31:09 +09001705TEST(dlfcn, dlopen_df_1_global) {
1706 void* handle = dlopen("libtest_dlopen_df_1_global.so", RTLD_NOW);
1707 ASSERT_TRUE(handle != nullptr) << dlerror();
1708}
1709
Peter Collingbourneb39cb3c2019-03-01 13:12:49 -08001710TEST(dlfcn, segment_gap) {
1711 void* handle = dlopen("libsegment_gap_outer.so", RTLD_NOW);
1712 ASSERT_TRUE(handle != nullptr) << dlerror();
1713
1714 auto get_inner = reinterpret_cast<void* (*)()>(dlsym(handle, "get_inner"));
1715 void* inner = get_inner();
1716 (void)inner;
1717
1718#if __arm__
1719 int count;
1720 _Unwind_Ptr outer_exidx = dl_unwind_find_exidx(reinterpret_cast<_Unwind_Ptr>(get_inner), &count);
1721 _Unwind_Ptr inner_exidx = dl_unwind_find_exidx(reinterpret_cast<_Unwind_Ptr>(inner), &count);
1722 EXPECT_NE(0u, outer_exidx);
1723 EXPECT_NE(0u, inner_exidx);
1724 EXPECT_NE(inner_exidx, outer_exidx);
1725#endif
1726
1727 Dl_info info;
1728 int rc = dladdr(inner, &info);
1729 ASSERT_NE(rc, 0);
1730
1731 EXPECT_NE(nullptr, strstr(info.dli_fname, "libsegment_gap_inner.so"));
1732}
1733
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001734#endif