blob: ff5b1a988f18448128a2c3153ae2fe8692ec1803 [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) {
263 GTEST_LOG_(INFO) << "getauxval(AT_SYSINFO_EHDR) == 0, skipping this test.";
264 return;
265 }
266#endif
Elliott Hughesda1bb112018-02-16 10:05:08 -0800267
268 const char* vdso_name = "linux-vdso.so.1";
269#if defined(__i386__)
270 vdso_name = "linux-gate.so.1";
271#endif
272 void* handle = dlopen(vdso_name, RTLD_NOW);
dimitryc18de1b2017-09-26 14:31:35 +0200273 ASSERT_TRUE(handle != nullptr) << dlerror();
274 dlclose(handle);
275}
276
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700277// mips doesn't support ifuncs
278#if !defined(__mips__)
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700279TEST(dlfcn, ifunc_variable) {
280 typedef const char* (*fn_ptr)();
281
282 // ifunc's choice depends on whether IFUNC_CHOICE has a value
283 // first check the set case
284 setenv("IFUNC_CHOICE", "set", 1);
285 // preload libtest_ifunc_variable_impl.so
286 void* handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
287 void* handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
288 ASSERT_TRUE(handle != nullptr) << dlerror();
289 const char** foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
290 fn_ptr 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("set", *foo_ptr, 3), 0);
294 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
295 dlclose(handle);
296 dlclose(handle_impl);
297
298 // then check the unset case
299 unsetenv("IFUNC_CHOICE");
300 handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
301 handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
302 ASSERT_TRUE(handle != nullptr) << dlerror();
303 foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
304 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
305 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
306 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
307 ASSERT_EQ(strncmp("unset", *foo_ptr, 5), 0);
308 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
309 dlclose(handle);
310 dlclose(handle_impl);
311}
312
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700313TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700314 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700315
316 // ifunc's choice depends on whether IFUNC_CHOICE has a value
317 // first check the set case
318 setenv("IFUNC_CHOICE", "set", 1);
319 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700320 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700321 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
322 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700323 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
324 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700325 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
326 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700327 dlclose(handle);
328
329 // then check the unset case
330 unsetenv("IFUNC_CHOICE");
331 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700332 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700333 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
334 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700335 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
336 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700337 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700338 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700339 dlclose(handle);
340}
341
342TEST(dlfcn, ifunc_ctor_call) {
343 typedef const char* (*fn_ptr)();
344
345 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700346 ASSERT_TRUE(handle != nullptr) << dlerror();
347 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
348 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
349 ASSERT_STREQ("false", is_ctor_called());
350
351 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
352 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700353 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700354 dlclose(handle);
355}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700356
357TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
358 typedef const char* (*fn_ptr)();
359
360 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
361 ASSERT_TRUE(handle != nullptr) << dlerror();
362 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
363 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
364 ASSERT_STREQ("false", is_ctor_called());
365
366 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
367 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
368 ASSERT_STREQ("true", is_ctor_called());
369 dlclose(handle);
370}
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700371#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700372
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700373TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
374 // This is the structure of the test library and
375 // its dt_needed libraries
376 // libtest_relo_check_dt_needed_order.so
377 // |
378 // +-> libtest_relo_check_dt_needed_order_1.so
379 // |
380 // +-> libtest_relo_check_dt_needed_order_2.so
381 //
382 // The root library references relo_test_get_answer_lib - which is defined
383 // in both dt_needed libraries, the correct relocation should
384 // use the function defined in libtest_relo_check_dt_needed_order_1.so
385 void* handle = nullptr;
Tom Cherryb8ab6182017-04-05 16:20:29 -0700386 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700387
388 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
389 ASSERT_TRUE(handle != nullptr) << dlerror();
390
391 typedef int (*fn_t) (void);
392 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
393 ASSERT_TRUE(fn != nullptr) << dlerror();
394 ASSERT_EQ(1, fn());
395}
396
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700397TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700398 // Here is how the test library and its dt_needed
399 // libraries are arranged
400 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700401 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700402 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700403 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700404 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700405 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700406 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700407 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700408 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700409 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700410 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700411 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700412 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700413 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700414 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700415 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700416 //
417 // load order should be (1, 2, 3, a, b, d)
418 //
419 // get_answer() is defined in (2, 3, a, b, c)
420 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700421 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700422 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700423 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
424 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700425 typedef int (*fn_t) (void);
426 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700427 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700428 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700429 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700430 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700431
432 ASSERT_EQ(42, fn());
433 ASSERT_EQ(43, fn2());
434 dlclose(handle);
435}
436
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700437TEST(dlfcn, dlopen_check_order_reloc_siblings) {
438 // This is how this one works:
439 // we lookup and call get_answer which is defined in '_2.so'
440 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
441 // the correct _impl() is implemented by '_a.so';
442 //
443 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
444 //
445 // Here is the picture:
446 //
447 // libtest_check_order_reloc_siblings.so
448 // |
449 // +-> ..._1.so <- empty
450 // | |
451 // | +-> ..._a.so <- exports correct answer_impl()
452 // | |
453 // | +-> ..._b.so <- every other letter exporting incorrect one.
454 // |
455 // +-> ..._2.so <- empty
456 // | |
457 // | +-> ..._c.so
458 // | |
459 // | +-> ..._d.so
460 // |
461 // +-> ..._3.so <- empty
462 // |
463 // +-> ..._e.so
464 // |
465 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
466 // implements incorrect get_answer_impl()
467
468 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
469 ASSERT_TRUE(handle == nullptr);
470#ifdef __BIONIC__
471 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
472 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
473#endif
474
475 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
476 ASSERT_TRUE(handle != nullptr) << dlerror();
477
478 typedef int (*fn_t) (void);
479 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
480 ASSERT_TRUE(fn != nullptr) << dlerror();
481 ASSERT_EQ(42, fn());
482
483 ASSERT_EQ(0, dlclose(handle));
484}
485
486TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
487 // This test uses the same library as dlopen_check_order_reloc_siblings.
488 // Unlike dlopen_check_order_reloc_siblings it preloads
489 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
490 // dlopen(libtest_check_order_reloc_siblings.so)
491
492 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
493 ASSERT_TRUE(handle == nullptr);
494 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
495 ASSERT_TRUE(handle == nullptr);
496
497 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
498 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
499
500 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
501 ASSERT_TRUE(handle != nullptr) << dlerror();
502
503 ASSERT_EQ(0, dlclose(handle_for_1));
504
505 typedef int (*fn_t) (void);
506 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
507 ASSERT_TRUE(fn != nullptr) << dlerror();
508 ASSERT_EQ(42, fn());
509
510 ASSERT_EQ(0, dlclose(handle));
511}
512
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800513TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
514 // This is how this one works:
515 // we lookup and call grandchild_get_answer which is defined in '_2.so'
516 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
517 // the correct _impl() is implemented by '_c_1.so';
518 //
519 // Here is the picture of subtree:
520 //
521 // libtest_check_order_reloc_siblings.so
522 // |
523 // +-> ..._2.so <- grandchild_get_answer()
524 // |
525 // +-> ..._c.so <- empty
526 // | |
527 // | +-> _c_1.so <- exports correct answer_impl()
528 // | |
529 // | +-> _c_2.so <- exports incorrect answer_impl()
530 // |
531 // +-> ..._d.so <- empty
532
533 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
534 ASSERT_TRUE(handle == nullptr);
535#ifdef __BIONIC__
536 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
537 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
538#endif
539
540 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
541 ASSERT_TRUE(handle != nullptr) << dlerror();
542
543 typedef int (*fn_t) (void);
544 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
545 ASSERT_TRUE(fn != nullptr) << dlerror();
546 ASSERT_EQ(42, fn());
547
548 ASSERT_EQ(0, dlclose(handle));
549}
550
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700551TEST(dlfcn, dlopen_check_order_reloc_nephew) {
552 // This is how this one works:
553 // we lookup and call nephew_get_answer which is defined in '_2.so'
554 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
555 // the correct _impl() is implemented by '_a.so';
556 //
557 // Here is the picture:
558 //
559 // libtest_check_order_reloc_siblings.so
560 // |
561 // +-> ..._1.so <- empty
562 // | |
563 // | +-> ..._a.so <- exports correct answer_impl()
564 // | |
565 // | +-> ..._b.so <- every other letter exporting incorrect one.
566 // |
567 // +-> ..._2.so <- empty
568 // | |
569 // | +-> ..._c.so
570 // | |
571 // | +-> ..._d.so
572 // |
573 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
574 // |
575 // +-> ..._e.so
576 // |
577 // +-> ..._f.so
578
579 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
580 ASSERT_TRUE(handle == nullptr);
581#ifdef __BIONIC__
582 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
583 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
584#endif
585
586 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
587 ASSERT_TRUE(handle != nullptr) << dlerror();
588
589 typedef int (*fn_t) (void);
590 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
591 ASSERT_TRUE(fn != nullptr) << dlerror();
592 ASSERT_EQ(42, fn());
593
594 ASSERT_EQ(0, dlclose(handle));
595}
596
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800597TEST(dlfcn, check_unload_after_reloc) {
598 // This is how this one works:
599 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
600 // |
601 // +-> libtest_two_parents_child
602 //
603 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
604 // |
605 // +-> libtest_two_parents_child
606 //
607 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
608 // as a second step it dlopens parent2 and dlcloses parent1...
609
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700610 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
611 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800612
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700613 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
614 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800615
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700616 typedef int (*fn_t) (void);
617 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
618 ASSERT_TRUE(fn != nullptr) << dlerror();
619 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800620
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700621 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800622
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700623 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
624 ASSERT_TRUE(handle != nullptr);
625 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800626
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700627 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
628 ASSERT_TRUE(fn != nullptr) << dlerror();
629 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800630
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700631 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800632
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700633 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
634 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800635}
636
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700637extern "C" int check_order_reloc_root_get_answer_impl() {
638 return 42;
639}
640
641TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
642 // This is how this one works:
643 // we lookup and call get_answer3 which is defined in 'root.so'
644 // and in turn calls external root_get_answer_impl() defined in _2.so and
645 // above the correct _impl() is one in the executable.
646 //
647 // libtest_check_order_reloc_root.so
648 // |
649 // +-> ..._1.so <- empty
650 // |
651 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
652 //
653
654 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
655 ASSERT_TRUE(handle == nullptr);
656#ifdef __BIONIC__
657 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
658 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
659#endif
660
661 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
662 ASSERT_TRUE(handle != nullptr) << dlerror();
663
664 typedef int (*fn_t) (void);
665 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
666 ASSERT_TRUE(fn != nullptr) << dlerror();
667 ASSERT_EQ(42, fn());
668
669 ASSERT_EQ(0, dlclose(handle));
670}
671
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700672TEST(dlfcn, dlopen_check_rtld_local) {
673 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
674 ASSERT_TRUE(sym == nullptr);
675
676 // implicit RTLD_LOCAL
677 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
678 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
679 ASSERT_TRUE(sym == nullptr);
680 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
681 sym = dlsym(handle, "dlopen_testlib_simple_func");
682 ASSERT_TRUE(sym != nullptr);
683 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
684 dlclose(handle);
685
686 // explicit RTLD_LOCAL
687 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
688 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
689 ASSERT_TRUE(sym == nullptr);
690 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
691 sym = dlsym(handle, "dlopen_testlib_simple_func");
692 ASSERT_TRUE(sym != nullptr);
693 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
694 dlclose(handle);
695}
696
697TEST(dlfcn, dlopen_check_rtld_global) {
698 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
699 ASSERT_TRUE(sym == nullptr);
700
701 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700702 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700703 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
704 ASSERT_TRUE(sym != nullptr) << dlerror();
705 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
706 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700707
708 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
709 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
710 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700711
712 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
713 // shared libraries after symbol was not found in the main executable
714 // and dependent libraries.
715 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
716 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
717 ASSERT_TRUE(sym != nullptr) << dlerror();
718
719 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700720}
721
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700722// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
723// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
724// libtest_with_dependency_loop_a.so
725TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700726 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
727 ASSERT_TRUE(handle != nullptr) << dlerror();
728 void* f = dlsym(handle, "dlopen_test_loopy_function");
729 ASSERT_TRUE(f != nullptr) << dlerror();
730 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
731 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700732
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700733 // dlopen second time to make sure that the library was unloaded correctly
734 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
735 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800736#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700737 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 -0800738#else
739 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
740 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700741#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800742
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700743 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
744 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700745}
746
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700747TEST(dlfcn, dlopen_nodelete) {
748 static bool is_unloaded = false;
749
750 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
751 ASSERT_TRUE(handle != nullptr) << dlerror();
752 void (*set_unload_flag_ptr)(bool*);
753 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
754 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
755 set_unload_flag_ptr(&is_unloaded);
756
757 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
758 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
759 ASSERT_EQ(1729U, *taxicab_number);
760 *taxicab_number = 2;
761
762 dlclose(handle);
763 ASSERT_TRUE(!is_unloaded);
764
765 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
766 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
767 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
768
769
770 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
771 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
772 ASSERT_EQ(taxicab_number2, taxicab_number);
773
774 ASSERT_EQ(2U, *taxicab_number2);
775
776 dlclose(handle);
777 ASSERT_TRUE(!is_unloaded);
778}
779
780TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
781 static bool is_unloaded = false;
782
783 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
784 ASSERT_TRUE(handle != nullptr) << dlerror();
785 void (*set_unload_flag_ptr)(bool*);
786 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
787 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
788 set_unload_flag_ptr(&is_unloaded);
789
790 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
791 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
792
793 ASSERT_EQ(1729U, *taxicab_number);
794 *taxicab_number = 2;
795
796 // This RTLD_NODELETE should be ignored
797 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
798 ASSERT_TRUE(handle1 != nullptr) << dlerror();
799 ASSERT_EQ(handle, handle1);
800
801 dlclose(handle1);
802 dlclose(handle);
803
804 ASSERT_TRUE(is_unloaded);
805}
806
807TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
808 static bool is_unloaded = false;
809
810 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
811 ASSERT_TRUE(handle != nullptr) << dlerror();
812 void (*set_unload_flag_ptr)(bool*);
813 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
814 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
815 set_unload_flag_ptr(&is_unloaded);
816
817 dlclose(handle);
818 ASSERT_TRUE(!is_unloaded);
819}
820
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700821TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700822 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
823 ASSERT_TRUE(handle != nullptr) << dlerror();
824 int (*get_answer)();
825 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
826 ASSERT_TRUE(get_answer != nullptr) << dlerror();
827 ASSERT_EQ(42, get_answer());
828 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700829}
830
Elliott Hughese66190d2012-12-18 15:57:55 -0800831TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700832 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700833 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700834#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700835 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
836#else
837 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
838#endif
839}
840
dimitry109040c2017-11-03 13:49:07 +0100841TEST(dlfcn, dlclose_unload) {
842 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
843 ASSERT_TRUE(handle != nullptr) << dlerror();
844 uint32_t* taxicab_number = static_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
845 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
846 EXPECT_EQ(1729U, *taxicab_number);
847 dlclose(handle);
848 // Making sure that the library has been unmapped as part of library unload
849 // process. Note that mprotect somewhat counter-intuitively returns ENOMEM in
850 // this case.
851 uintptr_t page_start = reinterpret_cast<uintptr_t>(taxicab_number) & ~(PAGE_SIZE - 1);
852 ASSERT_TRUE(mprotect(reinterpret_cast<void*>(page_start), PAGE_SIZE, PROT_NONE) != 0);
853 ASSERT_EQ(ENOMEM, errno) << strerror(errno);
854}
855
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800856static void ConcurrentDlErrorFn(std::string& error) {
857 ASSERT_TRUE(dlerror() == nullptr);
858
859 void* handle = dlopen("/child/thread", RTLD_NOW);
860 ASSERT_TRUE(handle == nullptr);
861
862 const char* err = dlerror();
863 ASSERT_TRUE(err != nullptr);
864
865 error = err;
866}
867
868TEST(dlfcn, dlerror_concurrent_buffer) {
869 void* handle = dlopen("/main/thread", RTLD_NOW);
870 ASSERT_TRUE(handle == nullptr);
871 const char* main_thread_error = dlerror();
872 ASSERT_TRUE(main_thread_error != nullptr);
873 ASSERT_SUBSTR("/main/thread", main_thread_error);
874
875 std::string child_thread_error;
876 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
877 t.join();
878 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
879
880 // Check that main thread local buffer was not modified.
881 ASSERT_SUBSTR("/main/thread", main_thread_error);
Elliott Hughes5419b942012-10-16 15:54:46 -0700882}
883
Elliott Hughese66190d2012-12-18 15:57:55 -0800884TEST(dlfcn, dlerror_concurrent) {
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800885 void* handle = dlopen("/main/thread", RTLD_NOW);
886 ASSERT_TRUE(handle == nullptr);
887
888 std::string child_thread_error;
889 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
890 t.join();
891 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
892
Elliott Hughes5419b942012-10-16 15:54:46 -0700893 const char* main_thread_error = dlerror();
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800894 ASSERT_TRUE(main_thread_error != nullptr);
Elliott Hughes5419b942012-10-16 15:54:46 -0700895 ASSERT_SUBSTR("/main/thread", main_thread_error);
896}
897
Elliott Hughese66190d2012-12-18 15:57:55 -0800898TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700899 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700900 void* self = dlopen(nullptr, RTLD_NOW);
901 ASSERT_TRUE(self != nullptr);
902 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700903
904 void* sym;
905
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700906#if defined(__BIONIC__) && !defined(__LP64__)
907 // RTLD_DEFAULT in lp32 bionic is not (void*)0
908 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700909 sym = dlsym(nullptr, "test");
910 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800911 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700912#endif
913
Elliott Hughes3b297c42012-10-11 16:08:51 -0700914 // Symbol that doesn't exist.
915 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700916 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700917 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700918
919 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700920}
921
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700922TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700923 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700924 void* self = dlopen(nullptr, RTLD_NOW);
925 ASSERT_TRUE(self != nullptr);
926 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700927
928 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700929 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700930
931 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
932 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
933
934 Dl_info info;
935 int rc = dladdr(addr, &info);
936 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
937
938 // Get the name of this executable.
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700939 const std::string executable_path = android::base::GetExecutablePath();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700940
941 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700942 char dli_realpath[PATH_MAX];
943 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700944 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700945
946 // The symbol name should be the symbol we looked up.
947 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
948
949 // The address should be the exact address of the symbol.
950 ASSERT_EQ(info.dli_saddr, sym);
951
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700952 std::vector<map_record> maps;
953 ASSERT_TRUE(Maps::parse_maps(&maps));
954
955 void* base_address = nullptr;
956 for (const map_record& rec : maps) {
957 if (executable_path == rec.pathname) {
958 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700959 break;
960 }
961 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700962
963 // The base address should be the address we were loaded at.
964 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700965
966 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700967}
968
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700969TEST(dlfcn, dlopen_executable_by_absolute_path) {
970 void* handle1 = dlopen(nullptr, RTLD_NOW);
971 ASSERT_TRUE(handle1 != nullptr) << dlerror();
972
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700973 void* handle2 = dlopen(android::base::GetExecutablePath().c_str(), RTLD_NOW);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700974 ASSERT_TRUE(handle2 != nullptr) << dlerror();
975
976#if defined(__BIONIC__)
977 ASSERT_EQ(handle1, handle2);
978#else
979 GTEST_LOG_(INFO) << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
980 "it loads a separate copy of the main executable "
981 "on dlopen by absolute path.";
982#endif
983}
984
Victor Khimenko14b9d712017-01-25 19:59:16 +0100985#if defined (__aarch64__)
986#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm64/"
987#elif defined (__arm__)
988#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm/"
989#elif defined (__i386__)
990#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86/"
991#elif defined (__x86_64__)
992#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86_64/"
993#elif defined (__mips__)
994#if defined(__LP64__)
995#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips64/"
996#else
997#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips/"
998#endif
999#else
1000#error "Unknown architecture"
1001#endif
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001002#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Victor Khimenko14b9d712017-01-25 19:59:16 +01001003#define ALTERNATE_PATH_TO_LIBC ALTERNATE_PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001004
1005TEST(dlfcn, dladdr_libc) {
1006#if defined(__BIONIC__)
1007 Dl_info info;
1008 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
1009 ASSERT_TRUE(dladdr(addr, &info) != 0);
1010
Dmitriy Ivanovef255922015-04-08 11:53:08 -07001011 char libc_realpath[PATH_MAX];
Victor Khimenko14b9d712017-01-25 19:59:16 +01001012
1013 // Check if libc is in canonical path or in alternate path.
1014 if (strncmp(ALTERNATE_PATH_TO_SYSTEM_LIB,
1015 info.dli_fname,
1016 sizeof(ALTERNATE_PATH_TO_SYSTEM_LIB) - 1) == 0) {
1017 // Platform with emulated architecture. Symlink on ARC++.
1018 ASSERT_TRUE(realpath(ALTERNATE_PATH_TO_LIBC, libc_realpath) == libc_realpath);
1019 } else {
1020 // /system/lib is symlink when this test is executed on host.
1021 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
1022 }
Dmitriy Ivanovef255922015-04-08 11:53:08 -07001023
1024 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001025 // TODO: add check for dfi_fbase
1026 ASSERT_STREQ("puts", info.dli_sname);
1027 ASSERT_EQ(addr, info.dli_saddr);
1028#else
1029 GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig "
1030 "for libc.so, which is symlink itself (not a realpath).\n";
1031#endif
1032}
1033
Elliott Hughese66190d2012-12-18 15:57:55 -08001034TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -07001035 Dl_info info;
1036
Elliott Hughes3b297c42012-10-11 16:08:51 -07001037 dlerror(); // Clear any pending errors.
1038
Elliott Hughes8e15b082012-09-26 11:44:01 -07001039 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001040 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
1041 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001042
1043 // No symbol corresponding to a stack address.
1044 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001045 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001046}
Elliott Hughes124fae92012-10-31 14:20:03 -07001047
Elliott Hughesa43e9062013-01-07 14:18:22 -08001048// GNU-style ELF hash tables are incompatible with the MIPS ABI.
1049// MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code.
Elliott Hughese66190d2012-12-18 15:57:55 -08001050TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001051#if !defined(__mips__)
Elliott Hughes124fae92012-10-31 14:20:03 -07001052 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001053 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
1054 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001055 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001056 void* sym = dlsym(handle, "getRandomNumber");
1057 ASSERT_TRUE(sym != nullptr) << dlerror();
1058 int (*fn)(void);
1059 fn = reinterpret_cast<int (*)(void)>(sym);
1060 EXPECT_EQ(4, fn());
1061
1062 Dl_info dlinfo;
1063 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1064
1065 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1066 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001067 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001068#else
1069 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
1070#endif
Elliott Hughes124fae92012-10-31 14:20:03 -07001071}
Elliott Hughese66190d2012-12-18 15:57:55 -08001072
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001073TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
1074 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
1075 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001076 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001077 void* sym = dlsym(handle, "getRandomNumber");
1078 ASSERT_TRUE(sym != nullptr) << dlerror();
1079 int (*fn)(void);
1080 fn = reinterpret_cast<int (*)(void)>(sym);
1081 EXPECT_EQ(4, fn());
1082
1083 Dl_info dlinfo;
1084 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1085
1086 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1087 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
1088 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
1089}
1090
Elliott Hughese66190d2012-12-18 15:57:55 -08001091TEST(dlfcn, dlopen_bad_flags) {
1092 dlerror(); // Clear any pending errors.
1093 void* handle;
1094
Elliott Hughes063525c2014-05-13 11:19:57 -07001095#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -08001096 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001097 handle = dlopen(nullptr, 0);
1098 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001099 ASSERT_SUBSTR("invalid", dlerror());
1100#endif
1101
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001102 handle = dlopen(nullptr, 0xffffffff);
1103 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001104 ASSERT_SUBSTR("invalid", dlerror());
1105
1106 // 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 -07001107 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
1108 ASSERT_TRUE(handle != nullptr);
1109 ASSERT_SUBSTR(nullptr, dlerror());
Elliott Hughese66190d2012-12-18 15:57:55 -08001110}
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001111
1112TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001113 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001114 ASSERT_TRUE(addr == nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001115}
1116
1117TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001118 void* addr = dlsym(RTLD_DEFAULT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001119 ASSERT_TRUE(addr != nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001120}
1121
1122TEST(dlfcn, rtld_next_unknown_symbol) {
1123 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001124 ASSERT_TRUE(addr == nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001125}
1126
1127TEST(dlfcn, rtld_next_known_symbol) {
1128 void* addr = dlsym(RTLD_NEXT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001129 ASSERT_TRUE(addr != nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001130}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001131
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001132// Check that RTLD_NEXT of a libc symbol works in dlopened library
1133TEST(dlfcn, rtld_next_from_library) {
dimitry153168c2018-02-20 16:51:41 +01001134 void* library_with_fclose = dlopen("libtest_check_rtld_next_from_library.so", RTLD_NOW | RTLD_GLOBAL);
Raj Mamadgi527229c2017-11-02 15:53:50 -07001135 ASSERT_TRUE(library_with_fclose != nullptr) << dlerror();
1136 void* expected_addr = dlsym(RTLD_DEFAULT, "fclose");
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001137 ASSERT_TRUE(expected_addr != nullptr) << dlerror();
Raj Mamadgi527229c2017-11-02 15:53:50 -07001138 typedef void* (*get_libc_fclose_ptr_fn_t)();
1139 get_libc_fclose_ptr_fn_t get_libc_fclose_ptr =
1140 reinterpret_cast<get_libc_fclose_ptr_fn_t>(dlsym(library_with_fclose, "get_libc_fclose_ptr"));
1141 ASSERT_TRUE(get_libc_fclose_ptr != nullptr) << dlerror();
1142 ASSERT_EQ(expected_addr, get_libc_fclose_ptr());
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001143
Raj Mamadgi527229c2017-11-02 15:53:50 -07001144 dlclose(library_with_fclose);
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001145}
1146
1147
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001148TEST(dlfcn, dlsym_weak_func) {
1149 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001150 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001151 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001152
1153 int (*weak_func)();
1154 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001155 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001156 EXPECT_EQ(42, weak_func());
1157 dlclose(handle);
1158}
1159
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001160TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001161 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
1162 ASSERT_TRUE(handle != nullptr) << dlerror();
1163 int (*weak_func)();
1164 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
1165 ASSERT_TRUE(weak_func != nullptr) << dlerror();
1166 EXPECT_EQ(6551, weak_func());
1167 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001168}
1169
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001170TEST(dlfcn, dlopen_symlink) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001171 DlfcnSymlink symlink("dlopen_symlink");
1172 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001173 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001174 void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001175 ASSERT_TRUE(handle1 != nullptr);
1176 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001177 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -07001178 dlclose(handle1);
1179 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001180}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001181
1182// libtest_dlopen_from_ctor_main.so depends on
1183// libtest_dlopen_from_ctor.so which has a constructor
1184// that calls dlopen(libc...). This is to test the situation
1185// described in b/7941716.
1186TEST(dlfcn, dlopen_dlopen_from_ctor) {
1187#if defined(__BIONIC__)
1188 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1189 ASSERT_TRUE(handle != nullptr) << dlerror();
1190 dlclose(handle);
1191#else
1192 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
1193#endif
1194}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001195
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001196static std::string g_fini_call_order_str;
1197
1198static void register_fini_call(const char* s) {
1199 g_fini_call_order_str += s;
1200}
1201
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001202static void test_init_fini_call_order_for(const char* libname) {
1203 g_fini_call_order_str.clear();
1204 void* handle = dlopen(libname, RTLD_NOW);
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001205 ASSERT_TRUE(handle != nullptr) << dlerror();
1206 typedef int (*get_init_order_number_t)();
1207 get_init_order_number_t get_init_order_number =
1208 reinterpret_cast<get_init_order_number_t>(dlsym(handle, "get_init_order_number"));
1209 ASSERT_EQ(321, get_init_order_number());
1210
1211 typedef void (*set_fini_callback_t)(void (*f)(const char*));
1212 set_fini_callback_t set_fini_callback =
1213 reinterpret_cast<set_fini_callback_t>(dlsym(handle, "set_fini_callback"));
1214 set_fini_callback(register_fini_call);
1215 dlclose(handle);
1216 ASSERT_EQ("(root)(child)(grandchild)", g_fini_call_order_str);
1217}
1218
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001219TEST(dlfcn, init_fini_call_order) {
1220 test_init_fini_call_order_for("libtest_init_fini_order_root.so");
1221 test_init_fini_call_order_for("libtest_init_fini_order_root2.so");
1222}
1223
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001224TEST(dlfcn, symbol_versioning_use_v1) {
1225 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1226 ASSERT_TRUE(handle != nullptr) << dlerror();
1227 typedef int (*fn_t)();
1228 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1229 ASSERT_TRUE(fn != nullptr) << dlerror();
1230 ASSERT_EQ(1, fn());
1231 dlclose(handle);
1232}
1233
1234TEST(dlfcn, symbol_versioning_use_v2) {
1235 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1236 ASSERT_TRUE(handle != nullptr) << dlerror();
1237 typedef int (*fn_t)();
1238 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1239 ASSERT_TRUE(fn != nullptr) << dlerror();
1240 ASSERT_EQ(2, fn());
1241 dlclose(handle);
1242}
1243
1244TEST(dlfcn, symbol_versioning_use_other_v2) {
1245 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1246 ASSERT_TRUE(handle != nullptr) << dlerror();
1247 typedef int (*fn_t)();
1248 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1249 ASSERT_TRUE(fn != nullptr) << dlerror();
1250 ASSERT_EQ(20, fn());
1251 dlclose(handle);
1252}
1253
1254TEST(dlfcn, symbol_versioning_use_other_v3) {
1255 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1256 ASSERT_TRUE(handle != nullptr) << dlerror();
1257 typedef int (*fn_t)();
1258 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1259 ASSERT_TRUE(fn != nullptr) << dlerror();
1260 ASSERT_EQ(3, fn());
1261 dlclose(handle);
1262}
1263
1264TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1265 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1266 ASSERT_TRUE(handle != nullptr) << dlerror();
1267 typedef int (*fn_t)();
1268 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1269 ASSERT_TRUE(fn != nullptr) << dlerror();
1270 ASSERT_EQ(3, fn()); // the default version is 3
1271 dlclose(handle);
1272}
1273
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001274TEST(dlfcn, dlvsym_smoke) {
1275 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1276 ASSERT_TRUE(handle != nullptr) << dlerror();
1277 typedef int (*fn_t)();
1278
1279 {
1280 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1281 ASSERT_TRUE(fn == nullptr);
1282 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1283 }
1284
1285 {
1286 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1287 ASSERT_TRUE(fn != nullptr) << dlerror();
1288 ASSERT_EQ(2, fn());
1289 }
1290
1291 dlclose(handle);
1292}
1293
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001294// This preempts the implementation from libtest_versioned_lib.so
1295extern "C" int version_zero_function() {
1296 return 0;
1297}
1298
1299// This preempts the implementation from libtest_versioned_uselibv*.so
1300extern "C" int version_zero_function2() {
1301 return 0;
1302}
Evgenii Stepanov68650822015-06-10 13:38:39 -07001303
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001304TEST(dlfcn, dt_runpath_smoke) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001305 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1306 ASSERT_TRUE(handle != nullptr) << dlerror();
1307
1308 typedef void *(* dlopen_b_fn)();
1309 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1310 ASSERT_TRUE(fn != nullptr) << dlerror();
1311
1312 void *p = fn();
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001313 ASSERT_TRUE(p != nullptr);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001314
1315 dlclose(handle);
1316}
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001317
Dimitry Ivanovdb6ab3d2017-06-27 11:02:51 -07001318TEST(dlfcn, dt_runpath_absolute_path) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001319 std::string libpath = GetTestlibRoot() + "/libtest_dt_runpath_d.so";
Dimitry Ivanovdb6ab3d2017-06-27 11:02:51 -07001320 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1321 ASSERT_TRUE(handle != nullptr) << dlerror();
1322
1323 typedef void *(* dlopen_b_fn)();
1324 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1325 ASSERT_TRUE(fn != nullptr) << dlerror();
1326
1327 void *p = fn();
1328 ASSERT_TRUE(p != nullptr);
1329
1330 dlclose(handle);
1331}
1332
dimitry55547db2018-05-25 14:17:37 +02001333static void test_dlclose_after_thread_local_dtor(const char* library_name) {
dimitry06016f22018-01-05 11:39:28 +01001334 bool is_dtor_triggered = false;
1335
1336 auto f = [](void* handle, bool* is_dtor_triggered) {
1337 typedef void (*fn_t)(bool*);
1338 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1339 ASSERT_TRUE(fn != nullptr) << dlerror();
1340
1341 fn(is_dtor_triggered);
1342
1343 ASSERT_TRUE(!*is_dtor_triggered);
1344 };
1345
dimitry55547db2018-05-25 14:17:37 +02001346 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001347 ASSERT_TRUE(handle == nullptr);
1348
dimitry55547db2018-05-25 14:17:37 +02001349 handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001350 ASSERT_TRUE(handle != nullptr) << dlerror();
1351
1352 std::thread t(f, handle, &is_dtor_triggered);
1353 t.join();
1354
1355 ASSERT_TRUE(is_dtor_triggered);
1356 dlclose(handle);
1357
dimitry55547db2018-05-25 14:17:37 +02001358 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001359 ASSERT_TRUE(handle == nullptr);
1360}
1361
dimitry55547db2018-05-25 14:17:37 +02001362TEST(dlfcn, dlclose_after_thread_local_dtor) {
1363 test_dlclose_after_thread_local_dtor("libtest_thread_local_dtor.so");
1364}
1365
1366TEST(dlfcn, dlclose_after_thread_local_dtor_indirect) {
1367 test_dlclose_after_thread_local_dtor("libtest_indirect_thread_local_dtor.so");
1368}
1369
1370static void test_dlclose_before_thread_local_dtor(const char* library_name) {
dimitry06016f22018-01-05 11:39:28 +01001371 bool is_dtor_triggered = false;
1372
dimitry55547db2018-05-25 14:17:37 +02001373 auto f = [library_name](bool* is_dtor_triggered) {
1374 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001375 ASSERT_TRUE(handle == nullptr);
1376
dimitry55547db2018-05-25 14:17:37 +02001377 handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001378 ASSERT_TRUE(handle != nullptr) << dlerror();
1379
1380 typedef void (*fn_t)(bool*);
1381 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1382 ASSERT_TRUE(fn != nullptr) << dlerror();
1383
1384 fn(is_dtor_triggered);
1385
1386 dlclose(handle);
1387
1388 ASSERT_TRUE(!*is_dtor_triggered);
1389
1390 // Since we have thread_atexit dtors associated with handle - the library should
1391 // still be availabe.
dimitry55547db2018-05-25 14:17:37 +02001392 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001393 ASSERT_TRUE(handle != nullptr) << dlerror();
1394 dlclose(handle);
1395 };
1396
dimitry55547db2018-05-25 14:17:37 +02001397 void* handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001398 ASSERT_TRUE(handle != nullptr) << dlerror();
1399 dlclose(handle);
1400
dimitry55547db2018-05-25 14:17:37 +02001401 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001402 ASSERT_TRUE(handle == nullptr);
1403
1404 std::thread t(f, &is_dtor_triggered);
1405 t.join();
1406#if defined(__BIONIC__)
1407 // ld-android.so unloads unreferenced libraries on pthread_exit()
1408 ASSERT_TRUE(is_dtor_triggered);
dimitry55547db2018-05-25 14:17:37 +02001409 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001410 ASSERT_TRUE(handle == nullptr);
1411#else
1412 // GLIBC does not unload libraries with ref_count = 0 on pthread_exit
1413 ASSERT_TRUE(is_dtor_triggered);
dimitry55547db2018-05-25 14:17:37 +02001414 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1415 ASSERT_TRUE(handle != nullptr) << dlerror();
1416#endif
1417}
1418
1419TEST(dlfcn, dlclose_before_thread_local_dtor) {
1420 test_dlclose_before_thread_local_dtor("libtest_thread_local_dtor.so");
1421}
1422
1423TEST(dlfcn, dlclose_before_thread_local_dtor_indirect) {
1424 test_dlclose_before_thread_local_dtor("libtest_indirect_thread_local_dtor.so");
1425}
1426
1427TEST(dlfcn, dlclose_before_thread_local_dtor_multiple_dsos) {
1428 const constexpr char* library_name = "libtest_indirect_thread_local_dtor.so";
1429
1430 bool is_dtor1_triggered = false;
1431 bool is_dtor2_triggered = false;
1432
1433 std::mutex mtx;
1434 std::condition_variable cv;
1435 void* library_handle = nullptr;
1436 bool thread1_dlopen_complete = false;
1437 bool thread2_thread_local_dtor_initialized = false;
1438 bool thread1_complete = false;
1439
1440 auto f1 = [&]() {
1441 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1442 ASSERT_TRUE(handle == nullptr);
1443
1444 handle = dlopen(library_name, RTLD_NOW);
1445 ASSERT_TRUE(handle != nullptr) << dlerror();
1446 std::unique_lock<std::mutex> lock(mtx);
1447 thread1_dlopen_complete = true;
1448 library_handle = handle;
1449 lock.unlock();
1450 cv.notify_one();
1451
1452 typedef void (*fn_t)(bool*);
1453 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1454 ASSERT_TRUE(fn != nullptr) << dlerror();
1455
1456 fn(&is_dtor1_triggered);
1457
1458 lock.lock();
1459 cv.wait(lock, [&] { return thread2_thread_local_dtor_initialized; });
1460 lock.unlock();
1461
1462 dlclose(handle);
1463
1464 ASSERT_TRUE(!is_dtor1_triggered);
1465
1466 // Since we have thread_atexit dtors associated with handle - the library should
1467 // still be availabe.
1468 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1469 ASSERT_TRUE(handle != nullptr) << dlerror();
1470 dlclose(handle);
1471 };
1472
1473 auto f2 = [&]() {
1474 std::unique_lock<std::mutex> lock(mtx);
1475 cv.wait(lock, [&] { return thread1_dlopen_complete; });
1476 void* handle = library_handle;
1477 lock.unlock();
1478
1479 typedef void (*fn_t)(bool*);
1480 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable2"));
1481 ASSERT_TRUE(fn != nullptr) << dlerror();
1482
1483 fn(&is_dtor2_triggered);
1484
1485 lock.lock();
1486 thread2_thread_local_dtor_initialized = true;
1487 lock.unlock();
1488 cv.notify_one();
1489
1490 lock.lock();
1491 cv.wait(lock, [&] { return thread1_complete; });
1492 lock.unlock();
1493
1494 ASSERT_TRUE(!is_dtor2_triggered);
1495 };
1496
1497 void* handle = dlopen(library_name, RTLD_NOW);
1498 ASSERT_TRUE(handle != nullptr) << dlerror();
1499 dlclose(handle);
1500
1501 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1502 ASSERT_TRUE(handle == nullptr);
1503
1504 std::thread t1(f1);
1505 std::thread t2(f2);
1506 t1.join();
1507 ASSERT_TRUE(is_dtor1_triggered);
1508 ASSERT_TRUE(!is_dtor2_triggered);
1509
1510 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1511 ASSERT_TRUE(handle != nullptr) << dlerror();
1512 dlclose(handle);
1513
1514 std::unique_lock<std::mutex> lock(mtx);
1515 thread1_complete = true;
1516 lock.unlock();
1517 cv.notify_one();
1518
1519 t2.join();
1520 ASSERT_TRUE(is_dtor2_triggered);
1521
1522#if defined(__BIONIC__)
1523 // ld-android.so unloads unreferenced libraries on pthread_exit()
1524 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1525 ASSERT_TRUE(handle == nullptr);
1526#else
1527 // GLIBC does not unload libraries with ref_count = 0 on pthread_exit
1528 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001529 ASSERT_TRUE(handle != nullptr) << dlerror();
1530#endif
1531}
1532
Elliott Hughes8ad40932017-06-15 15:12:29 -07001533TEST(dlfcn, RTLD_macros) {
1534#if !defined(RTLD_LOCAL)
1535#error no RTLD_LOCAL
1536#elif !defined(RTLD_LAZY)
1537#error no RTLD_LAZY
1538#elif !defined(RTLD_NOW)
1539#error no RTLD_NOW
1540#elif !defined(RTLD_NOLOAD)
1541#error no RTLD_NOLOAD
1542#elif !defined(RTLD_GLOBAL)
1543#error no RTLD_GLOBAL
1544#elif !defined(RTLD_NODELETE)
1545#error no RTLD_NODELETE
1546#endif
1547}
1548
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001549// Bionic specific tests
1550#if defined(__BIONIC__)
1551
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001552#if defined(__arm__)
1553const llvm::ELF::Elf32_Dyn* to_dynamic_table(const char* p) {
1554 return reinterpret_cast<const llvm::ELF::Elf32_Dyn*>(p);
1555}
1556
1557// Duplicate these definitions here because they are android specific
Ryan Prichard68140282018-05-23 14:20:29 -07001558// - note that we cannot include <elf.h> because #defines conflict with
1559// enum names provided by LLVM.
1560// - we also don't use llvm::ELF::DT_LOOS because its value is 0x60000000
1561// rather than the 0x6000000d we expect
1562#define DT_LOOS 0x6000000d
1563#define DT_ANDROID_REL (DT_LOOS + 2)
1564#define DT_ANDROID_RELA (DT_LOOS + 4)
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001565
1566template<typename ELFT>
Ryan Prichard470b6662018-03-27 22:10:55 -07001567void validate_compatibility_of_native_library(const std::string& soname,
1568 const std::string& path, ELFT* elf) {
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001569 bool has_elf_hash = false;
1570 bool has_android_rel = false;
1571 bool has_rel = false;
1572 // Find dynamic section and check that DT_HASH and there is no DT_ANDROID_REL
1573 for (auto it = elf->section_begin(); it != elf->section_end(); ++it) {
1574 const llvm::object::ELFSectionRef& section_ref = *it;
1575 if (section_ref.getType() == llvm::ELF::SHT_DYNAMIC) {
1576 llvm::StringRef data;
1577 ASSERT_TRUE(!it->getContents(data)) << "unable to get SHT_DYNAMIC section data";
1578 for (auto d = to_dynamic_table(data.data()); d->d_tag != llvm::ELF::DT_NULL; ++d) {
1579 if (d->d_tag == llvm::ELF::DT_HASH) {
1580 has_elf_hash = true;
1581 } else if (d->d_tag == DT_ANDROID_REL || d->d_tag == DT_ANDROID_RELA) {
1582 has_android_rel = true;
1583 } else if (d->d_tag == llvm::ELF::DT_REL || d->d_tag == llvm::ELF::DT_RELA) {
1584 has_rel = true;
1585 }
1586 }
1587
1588 break;
1589 }
1590 }
1591
1592 ASSERT_TRUE(has_elf_hash) << path.c_str() << ": missing elf hash (DT_HASH)";
1593 ASSERT_TRUE(!has_android_rel) << path.c_str() << ": has packed relocations";
Ryan Prichard470b6662018-03-27 22:10:55 -07001594 // libdl.so is simple enough that it might not have any relocations, so
1595 // exempt it from the DT_REL/DT_RELA check.
1596 if (soname != "libdl.so") {
1597 ASSERT_TRUE(has_rel) << path.c_str() << ": missing DT_REL/DT_RELA";
1598 }
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001599}
1600
Ryan Prichard470b6662018-03-27 22:10:55 -07001601void validate_compatibility_of_native_library(const std::string& soname) {
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001602 // On the systems with emulation system libraries would be of different
1603 // architecture. Try to use alternate paths first.
1604 std::string path = std::string(ALTERNATE_PATH_TO_SYSTEM_LIB) + soname;
1605 auto binary_or_error = llvm::object::createBinary(path);
1606 if (!binary_or_error) {
1607 path = std::string(PATH_TO_SYSTEM_LIB) + soname;
1608 binary_or_error = llvm::object::createBinary(path);
1609 }
1610 ASSERT_FALSE(!binary_or_error);
1611
1612 llvm::object::Binary* binary = binary_or_error.get().getBinary();
1613
1614 auto obj = llvm::dyn_cast<llvm::object::ObjectFile>(binary);
1615 ASSERT_TRUE(obj != nullptr);
1616
1617 auto elf = llvm::dyn_cast<llvm::object::ELF32LEObjectFile>(obj);
1618
1619 ASSERT_TRUE(elf != nullptr);
1620
Ryan Prichard470b6662018-03-27 22:10:55 -07001621 validate_compatibility_of_native_library(soname, path, elf);
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001622}
1623
1624// This is a test for app compatibility workaround for arm apps
1625// affected by http://b/24465209
1626TEST(dlext, compat_elf_hash_and_relocation_tables) {
1627 validate_compatibility_of_native_library("libc.so");
1628 validate_compatibility_of_native_library("liblog.so");
1629 validate_compatibility_of_native_library("libstdc++.so");
1630 validate_compatibility_of_native_library("libdl.so");
1631 validate_compatibility_of_native_library("libm.so");
1632 validate_compatibility_of_native_library("libz.so");
1633 validate_compatibility_of_native_library("libjnigraphics.so");
1634}
1635
1636#endif // defined(__arm__)
1637
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001638TEST(dlfcn, dlopen_invalid_rw_load_segment) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001639 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001640 "/" + kPrebuiltElfDir +
1641 "/libtest_invalid-rw_load_segment.so";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001642 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1643 ASSERT_TRUE(handle == nullptr);
Elliott Hughes9076b0c2018-02-28 11:29:45 -08001644 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W+E load segments are not allowed";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001645 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1646}
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001647
1648TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001649 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001650 "/" + kPrebuiltElfDir +
1651 "/libtest_invalid-unaligned_shdr_offset.so";
1652
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001653 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1654 ASSERT_TRUE(handle == nullptr);
1655 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
1656 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1657}
1658
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001659TEST(dlfcn, dlopen_invalid_zero_shentsize) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001660 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001661 "/" + kPrebuiltElfDir +
1662 "/libtest_invalid-zero_shentsize.so";
1663
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001664 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1665 ASSERT_TRUE(handle == nullptr);
1666 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x";
1667 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1668}
1669
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001670TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001671 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001672 "/" + kPrebuiltElfDir +
1673 "/libtest_invalid-zero_shstrndx.so";
1674
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001675 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1676 ASSERT_TRUE(handle == nullptr);
1677 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx";
1678 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1679}
1680
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001681TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001682 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001683 "/" + kPrebuiltElfDir +
1684 "/libtest_invalid-empty_shdr_table.so";
1685
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001686 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1687 ASSERT_TRUE(handle == nullptr);
1688 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers";
1689 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1690}
1691
Dimitry Ivanov46230442016-08-15 13:40:53 -07001692TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001693 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001694 "/" + kPrebuiltElfDir +
1695 "/libtest_invalid-zero_shdr_table_offset.so";
1696
Dimitry Ivanov46230442016-08-15 13:40:53 -07001697 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1698 ASSERT_TRUE(handle == nullptr);
1699 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/";
1700 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1701}
1702
Dimitry Ivanov55958342016-08-15 14:06:04 -07001703TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001704 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001705 "/" + kPrebuiltElfDir +
1706 "/libtest_invalid-zero_shdr_table_content.so";
1707
Dimitry Ivanov55958342016-08-15 14:06:04 -07001708 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1709 ASSERT_TRUE(handle == nullptr);
1710 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
1711 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1712}
1713
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001714TEST(dlfcn, dlopen_invalid_textrels) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001715 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001716 "/" + kPrebuiltElfDir +
1717 "/libtest_invalid-textrels.so";
1718
1719 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1720 ASSERT_TRUE(handle == nullptr);
1721 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1722 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1723}
1724
1725TEST(dlfcn, dlopen_invalid_textrels2) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001726 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001727 "/" + kPrebuiltElfDir +
1728 "/libtest_invalid-textrels2.so";
1729
1730 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1731 ASSERT_TRUE(handle == nullptr);
1732 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1733 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1734}
1735
Jiyong Park01162f22017-10-16 15:31:09 +09001736TEST(dlfcn, dlopen_df_1_global) {
1737 void* handle = dlopen("libtest_dlopen_df_1_global.so", RTLD_NOW);
1738 ASSERT_TRUE(handle != nullptr) << dlerror();
1739}
1740
Peter Collingbourneb39cb3c2019-03-01 13:12:49 -08001741TEST(dlfcn, segment_gap) {
1742 void* handle = dlopen("libsegment_gap_outer.so", RTLD_NOW);
1743 ASSERT_TRUE(handle != nullptr) << dlerror();
1744
1745 auto get_inner = reinterpret_cast<void* (*)()>(dlsym(handle, "get_inner"));
1746 void* inner = get_inner();
1747 (void)inner;
1748
1749#if __arm__
1750 int count;
1751 _Unwind_Ptr outer_exidx = dl_unwind_find_exidx(reinterpret_cast<_Unwind_Ptr>(get_inner), &count);
1752 _Unwind_Ptr inner_exidx = dl_unwind_find_exidx(reinterpret_cast<_Unwind_Ptr>(inner), &count);
1753 EXPECT_NE(0u, outer_exidx);
1754 EXPECT_NE(0u, inner_exidx);
1755 EXPECT_NE(inner_exidx, outer_exidx);
1756#endif
1757
1758 Dl_info info;
1759 int rc = dladdr(inner, &info);
1760 ASSERT_NE(rc, 0);
1761
1762 EXPECT_NE(nullptr, strstr(info.dli_fname, "libsegment_gap_inner.so"));
1763}
1764
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001765#endif