blob: adc5ee4b1e1cfe63c87c37beb93bd7f9a06512bf [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
Elliott Hughes8e15b082012-09-26 11:44:01 -070027
28#include <string>
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -080029#include <thread>
jeffhaoacf5aa72012-09-12 17:25:30 -070030
Tom Cherryb8ab6182017-04-05 16:20:29 -070031#include <android-base/scopeguard.h>
32
Dimitry Ivanov927877c2016-09-21 11:17:13 -070033#include "gtest_globals.h"
Dimitry Ivanov708589f2016-09-19 10:50:28 -070034#include "dlfcn_symlink_support.h"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070035#include "utils.h"
36
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -080037#if defined(__BIONIC__) && (defined(__arm__) || defined(__i386__))
38#pragma clang diagnostic push
39#pragma clang diagnostic ignored "-Wunused-parameter"
40
41#include <llvm/ADT/StringRef.h>
42#include <llvm/Object/Binary.h>
43#include <llvm/Object/ELFObjectFile.h>
44#include <llvm/Object/ObjectFile.h>
45
46#pragma clang diagnostic pop
47#endif // defined(__ANDROID__) && (defined(__arm__) || defined(__i386__))
48
Elliott Hughes3b297c42012-10-11 16:08:51 -070049#define ASSERT_SUBSTR(needle, haystack) \
50 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
51
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070052
Elliott Hughes1728b232014-05-14 10:02:03 -070053static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070054extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070055 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070056}
57
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070058static int g_ctor_function_called = 0;
Dimitry Ivanov55437462016-07-20 15:33:07 -070059static int g_ctor_argc = 0;
60static char** g_ctor_argv = reinterpret_cast<char**>(0xDEADBEEF);
61static char** g_ctor_envp = g_ctor_envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070062
Dimitry Ivanov55437462016-07-20 15:33:07 -070063extern "C" void ctor_function(int argc, char** argv, char** envp) __attribute__ ((constructor));
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070064
Dimitry Ivanov55437462016-07-20 15:33:07 -070065extern "C" void ctor_function(int argc, char** argv, char** envp) {
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070066 g_ctor_function_called = 17;
Dimitry Ivanov55437462016-07-20 15:33:07 -070067 g_ctor_argc = argc;
68 g_ctor_argv = argv;
69 g_ctor_envp = envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070070}
71
72TEST(dlfcn, ctor_function_call) {
73 ASSERT_EQ(17, g_ctor_function_called);
Dimitry Ivanov55437462016-07-20 15:33:07 -070074 ASSERT_TRUE(g_ctor_argc = get_argc());
75 ASSERT_TRUE(g_ctor_argv = get_argv());
76 ASSERT_TRUE(g_ctor_envp = get_envp());
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070077}
78
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070079TEST(dlfcn, dlsym_in_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070080 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070081 void* self = dlopen(nullptr, RTLD_NOW);
82 ASSERT_TRUE(self != nullptr);
83 ASSERT_TRUE(dlerror() == nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070084
85 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070086 ASSERT_TRUE(sym != nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070087
88 void (*function)() = reinterpret_cast<void(*)()>(sym);
89
Elliott Hughes1728b232014-05-14 10:02:03 -070090 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070091 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070092 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070093
94 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070095}
Elliott Hughes8e15b082012-09-26 11:44:01 -070096
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070097TEST(dlfcn, dlsym_from_sofile) {
98 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL);
99 ASSERT_TRUE(handle != nullptr) << dlerror();
100
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700101 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700102 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
103 ASSERT_TRUE(symbol == nullptr);
104 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
105
106 typedef int* (*fn_t)();
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700107 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
108 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
109 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700110
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700111 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700112 ASSERT_TRUE(ptr != nullptr) << dlerror();
113 ASSERT_EQ(42, *ptr);
114
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700115 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
116 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
117 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
118
119 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
120 ASSERT_TRUE(ptr != nullptr) << dlerror();
121 ASSERT_EQ(44, *ptr);
122
123 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
124 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
125 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
126
127 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
128 ASSERT_TRUE(ptr != nullptr) << dlerror();
129 ASSERT_EQ(43, *ptr);
130
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700131 dlclose(handle);
132}
133
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700134TEST(dlfcn, dlsym_from_sofile_with_preload) {
135 void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL);
136 ASSERT_TRUE(preload != nullptr) << dlerror();
137
138 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
139 ASSERT_TRUE(handle != nullptr) << dlerror();
140
141 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
142 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
143 ASSERT_TRUE(symbol == nullptr);
144 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
145
146 typedef int* (*fn_t)();
147 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
148 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
149 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
150
151 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
152 ASSERT_TRUE(ptr != nullptr) << dlerror();
153 ASSERT_EQ(42, *ptr);
154
155 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
156 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
157 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
158
159 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
160 ASSERT_TRUE(ptr != nullptr) << dlerror();
161 ASSERT_EQ(44, *ptr);
162
163 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
164 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
165 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
166
167 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
168 ASSERT_TRUE(ptr != nullptr) << dlerror();
169 ASSERT_EQ(43, *ptr);
170
171 dlclose(handle);
172 dlclose(preload);
173}
174
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700175TEST(dlfcn, dlsym_handle_global_sym) {
176 // check that we do not look into global group
177 // when looking up symbol by handle
178 void* handle = dlopen("libtest_empty.so", RTLD_NOW);
179 dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL);
180 void* sym = dlsym(handle, "getRandomNumber");
181 ASSERT_TRUE(sym == nullptr);
182 ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror());
183
184 sym = dlsym(handle, "DlSymTestFunction");
185 ASSERT_TRUE(sym == nullptr);
186 ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror());
187 dlclose(handle);
188}
189
Dimitry Ivanovd5b578a2016-12-14 15:16:56 -0800190TEST(dlfcn, dlsym_handle_empty_symbol) {
191 // check that dlsym of an empty symbol fails (see http://b/33530622)
192 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW);
193 ASSERT_TRUE(handle != nullptr) << dlerror();
194 void* sym = dlsym(handle, "");
195 ASSERT_TRUE(sym == nullptr);
196 ASSERT_SUBSTR("undefined symbol: ", dlerror());
197 dlclose(handle);
198}
199
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700200TEST(dlfcn, dlsym_with_dependencies) {
201 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700202 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700203 dlerror();
204 // This symbol is in DT_NEEDED library.
205 void* sym = dlsym(handle, "getRandomNumber");
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700206 ASSERT_TRUE(sym != nullptr) << dlerror();
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700207 int (*fn)(void);
208 fn = reinterpret_cast<int (*)(void)>(sym);
209 EXPECT_EQ(4, fn());
210 dlclose(handle);
211}
212
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700213TEST(dlfcn, dlopen_noload) {
214 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700215 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700216 handle = dlopen("libtest_simple.so", RTLD_NOW);
217 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700218 ASSERT_TRUE(handle != nullptr);
219 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700220 ASSERT_TRUE(handle == handle2);
221 ASSERT_EQ(0, dlclose(handle));
222 ASSERT_EQ(0, dlclose(handle2));
223}
224
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700225TEST(dlfcn, dlopen_by_soname) {
226 static const char* soname = "libdlext_test_soname.so";
227 static const char* filename = "libdlext_test_different_soname.so";
228 // 1. Make sure there is no library with soname in default search path
229 void* handle = dlopen(soname, RTLD_NOW);
230 ASSERT_TRUE(handle == nullptr);
231
232 // 2. Load a library using filename
233 handle = dlopen(filename, RTLD_NOW);
234 ASSERT_TRUE(handle != nullptr) << dlerror();
235
236 // 3. Find library by soname
237 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
238 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
239 ASSERT_EQ(handle, handle_soname);
240
241 // 4. RTLD_NOLOAD should still work with filename
242 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
243 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
244 ASSERT_EQ(handle, handle_filename);
245
246 dlclose(handle_filename);
247 dlclose(handle_soname);
248 dlclose(handle);
249}
250
dimitryc18de1b2017-09-26 14:31:35 +0200251TEST(dlfcn, dlopen_vdso) {
dimitryb9555a92017-10-11 18:04:05 +0200252#if __has_include(<sys/auxv.h>)
253 if (getauxval(AT_SYSINFO_EHDR) == 0) {
254 GTEST_LOG_(INFO) << "getauxval(AT_SYSINFO_EHDR) == 0, skipping this test.";
255 return;
256 }
257#endif
dimitryc18de1b2017-09-26 14:31:35 +0200258 void* handle = dlopen("linux-vdso.so.1", RTLD_NOW);
259 ASSERT_TRUE(handle != nullptr) << dlerror();
260 dlclose(handle);
261}
262
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700263// mips doesn't support ifuncs
264#if !defined(__mips__)
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700265TEST(dlfcn, ifunc_variable) {
266 typedef const char* (*fn_ptr)();
267
268 // ifunc's choice depends on whether IFUNC_CHOICE has a value
269 // first check the set case
270 setenv("IFUNC_CHOICE", "set", 1);
271 // preload libtest_ifunc_variable_impl.so
272 void* handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
273 void* handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
274 ASSERT_TRUE(handle != nullptr) << dlerror();
275 const char** foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
276 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
277 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
278 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
279 ASSERT_EQ(strncmp("set", *foo_ptr, 3), 0);
280 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
281 dlclose(handle);
282 dlclose(handle_impl);
283
284 // then check the unset case
285 unsetenv("IFUNC_CHOICE");
286 handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
287 handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
288 ASSERT_TRUE(handle != nullptr) << dlerror();
289 foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
290 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
291 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
292 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
293 ASSERT_EQ(strncmp("unset", *foo_ptr, 5), 0);
294 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
295 dlclose(handle);
296 dlclose(handle_impl);
297}
298
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700299TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700300 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700301
302 // ifunc's choice depends on whether IFUNC_CHOICE has a value
303 // first check the set case
304 setenv("IFUNC_CHOICE", "set", 1);
305 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700306 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700307 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
308 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700309 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
310 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700311 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
312 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700313 dlclose(handle);
314
315 // then check the unset case
316 unsetenv("IFUNC_CHOICE");
317 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700318 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700319 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
320 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700321 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
322 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700323 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700324 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700325 dlclose(handle);
326}
327
328TEST(dlfcn, ifunc_ctor_call) {
329 typedef const char* (*fn_ptr)();
330
331 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700332 ASSERT_TRUE(handle != nullptr) << dlerror();
333 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
334 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
335 ASSERT_STREQ("false", is_ctor_called());
336
337 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
338 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700339 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700340 dlclose(handle);
341}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700342
343TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
344 typedef const char* (*fn_ptr)();
345
346 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
347 ASSERT_TRUE(handle != nullptr) << dlerror();
348 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
349 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
350 ASSERT_STREQ("false", is_ctor_called());
351
352 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
353 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
354 ASSERT_STREQ("true", is_ctor_called());
355 dlclose(handle);
356}
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700357#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700358
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700359TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
360 // This is the structure of the test library and
361 // its dt_needed libraries
362 // libtest_relo_check_dt_needed_order.so
363 // |
364 // +-> libtest_relo_check_dt_needed_order_1.so
365 // |
366 // +-> libtest_relo_check_dt_needed_order_2.so
367 //
368 // The root library references relo_test_get_answer_lib - which is defined
369 // in both dt_needed libraries, the correct relocation should
370 // use the function defined in libtest_relo_check_dt_needed_order_1.so
371 void* handle = nullptr;
Tom Cherryb8ab6182017-04-05 16:20:29 -0700372 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700373
374 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
375 ASSERT_TRUE(handle != nullptr) << dlerror();
376
377 typedef int (*fn_t) (void);
378 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
379 ASSERT_TRUE(fn != nullptr) << dlerror();
380 ASSERT_EQ(1, fn());
381}
382
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700383TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700384 // Here is how the test library and its dt_needed
385 // libraries are arranged
386 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700387 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700388 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700389 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700390 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700391 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700392 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700393 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700394 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700395 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700396 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700397 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700398 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700399 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700400 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700401 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700402 //
403 // load order should be (1, 2, 3, a, b, d)
404 //
405 // get_answer() is defined in (2, 3, a, b, c)
406 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700407 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700408 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700409 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
410 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700411 typedef int (*fn_t) (void);
412 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700413 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700414 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700415 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700416 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700417
418 ASSERT_EQ(42, fn());
419 ASSERT_EQ(43, fn2());
420 dlclose(handle);
421}
422
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700423TEST(dlfcn, dlopen_check_order_reloc_siblings) {
424 // This is how this one works:
425 // we lookup and call get_answer which is defined in '_2.so'
426 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
427 // the correct _impl() is implemented by '_a.so';
428 //
429 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
430 //
431 // Here is the picture:
432 //
433 // libtest_check_order_reloc_siblings.so
434 // |
435 // +-> ..._1.so <- empty
436 // | |
437 // | +-> ..._a.so <- exports correct answer_impl()
438 // | |
439 // | +-> ..._b.so <- every other letter exporting incorrect one.
440 // |
441 // +-> ..._2.so <- empty
442 // | |
443 // | +-> ..._c.so
444 // | |
445 // | +-> ..._d.so
446 // |
447 // +-> ..._3.so <- empty
448 // |
449 // +-> ..._e.so
450 // |
451 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
452 // implements incorrect get_answer_impl()
453
454 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
455 ASSERT_TRUE(handle == nullptr);
456#ifdef __BIONIC__
457 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
458 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
459#endif
460
461 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
462 ASSERT_TRUE(handle != nullptr) << dlerror();
463
464 typedef int (*fn_t) (void);
465 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
466 ASSERT_TRUE(fn != nullptr) << dlerror();
467 ASSERT_EQ(42, fn());
468
469 ASSERT_EQ(0, dlclose(handle));
470}
471
472TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
473 // This test uses the same library as dlopen_check_order_reloc_siblings.
474 // Unlike dlopen_check_order_reloc_siblings it preloads
475 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
476 // dlopen(libtest_check_order_reloc_siblings.so)
477
478 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
479 ASSERT_TRUE(handle == nullptr);
480 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
481 ASSERT_TRUE(handle == nullptr);
482
483 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
484 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
485
486 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
487 ASSERT_TRUE(handle != nullptr) << dlerror();
488
489 ASSERT_EQ(0, dlclose(handle_for_1));
490
491 typedef int (*fn_t) (void);
492 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
493 ASSERT_TRUE(fn != nullptr) << dlerror();
494 ASSERT_EQ(42, fn());
495
496 ASSERT_EQ(0, dlclose(handle));
497}
498
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800499TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
500 // This is how this one works:
501 // we lookup and call grandchild_get_answer which is defined in '_2.so'
502 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
503 // the correct _impl() is implemented by '_c_1.so';
504 //
505 // Here is the picture of subtree:
506 //
507 // libtest_check_order_reloc_siblings.so
508 // |
509 // +-> ..._2.so <- grandchild_get_answer()
510 // |
511 // +-> ..._c.so <- empty
512 // | |
513 // | +-> _c_1.so <- exports correct answer_impl()
514 // | |
515 // | +-> _c_2.so <- exports incorrect answer_impl()
516 // |
517 // +-> ..._d.so <- empty
518
519 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
520 ASSERT_TRUE(handle == nullptr);
521#ifdef __BIONIC__
522 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
523 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
524#endif
525
526 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
527 ASSERT_TRUE(handle != nullptr) << dlerror();
528
529 typedef int (*fn_t) (void);
530 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
531 ASSERT_TRUE(fn != nullptr) << dlerror();
532 ASSERT_EQ(42, fn());
533
534 ASSERT_EQ(0, dlclose(handle));
535}
536
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700537TEST(dlfcn, dlopen_check_order_reloc_nephew) {
538 // This is how this one works:
539 // we lookup and call nephew_get_answer which is defined in '_2.so'
540 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
541 // the correct _impl() is implemented by '_a.so';
542 //
543 // Here is the picture:
544 //
545 // libtest_check_order_reloc_siblings.so
546 // |
547 // +-> ..._1.so <- empty
548 // | |
549 // | +-> ..._a.so <- exports correct answer_impl()
550 // | |
551 // | +-> ..._b.so <- every other letter exporting incorrect one.
552 // |
553 // +-> ..._2.so <- empty
554 // | |
555 // | +-> ..._c.so
556 // | |
557 // | +-> ..._d.so
558 // |
559 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
560 // |
561 // +-> ..._e.so
562 // |
563 // +-> ..._f.so
564
565 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
566 ASSERT_TRUE(handle == nullptr);
567#ifdef __BIONIC__
568 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
569 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
570#endif
571
572 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
573 ASSERT_TRUE(handle != nullptr) << dlerror();
574
575 typedef int (*fn_t) (void);
576 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
577 ASSERT_TRUE(fn != nullptr) << dlerror();
578 ASSERT_EQ(42, fn());
579
580 ASSERT_EQ(0, dlclose(handle));
581}
582
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800583TEST(dlfcn, check_unload_after_reloc) {
584 // This is how this one works:
585 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
586 // |
587 // +-> libtest_two_parents_child
588 //
589 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
590 // |
591 // +-> libtest_two_parents_child
592 //
593 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
594 // as a second step it dlopens parent2 and dlcloses parent1...
595
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700596 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
597 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800598
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700599 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
600 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800601
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700602 typedef int (*fn_t) (void);
603 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
604 ASSERT_TRUE(fn != nullptr) << dlerror();
605 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800606
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700607 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800608
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700609 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
610 ASSERT_TRUE(handle != nullptr);
611 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800612
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700613 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(handle2));
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);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800621}
622
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700623extern "C" int check_order_reloc_root_get_answer_impl() {
624 return 42;
625}
626
627TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
628 // This is how this one works:
629 // we lookup and call get_answer3 which is defined in 'root.so'
630 // and in turn calls external root_get_answer_impl() defined in _2.so and
631 // above the correct _impl() is one in the executable.
632 //
633 // libtest_check_order_reloc_root.so
634 // |
635 // +-> ..._1.so <- empty
636 // |
637 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
638 //
639
640 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
641 ASSERT_TRUE(handle == nullptr);
642#ifdef __BIONIC__
643 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
644 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
645#endif
646
647 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
648 ASSERT_TRUE(handle != nullptr) << dlerror();
649
650 typedef int (*fn_t) (void);
651 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
652 ASSERT_TRUE(fn != nullptr) << dlerror();
653 ASSERT_EQ(42, fn());
654
655 ASSERT_EQ(0, dlclose(handle));
656}
657
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700658TEST(dlfcn, dlopen_check_rtld_local) {
659 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
660 ASSERT_TRUE(sym == nullptr);
661
662 // implicit RTLD_LOCAL
663 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
664 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
665 ASSERT_TRUE(sym == nullptr);
666 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
667 sym = dlsym(handle, "dlopen_testlib_simple_func");
668 ASSERT_TRUE(sym != nullptr);
669 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
670 dlclose(handle);
671
672 // explicit RTLD_LOCAL
673 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
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
683TEST(dlfcn, dlopen_check_rtld_global) {
684 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
685 ASSERT_TRUE(sym == nullptr);
686
687 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700688 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700689 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
690 ASSERT_TRUE(sym != nullptr) << dlerror();
691 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
692 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700693
694 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
695 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
696 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700697
698 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
699 // shared libraries after symbol was not found in the main executable
700 // and dependent libraries.
701 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
702 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
703 ASSERT_TRUE(sym != nullptr) << dlerror();
704
705 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700706}
707
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700708// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
709// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
710// libtest_with_dependency_loop_a.so
711TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700712 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
713 ASSERT_TRUE(handle != nullptr) << dlerror();
714 void* f = dlsym(handle, "dlopen_test_loopy_function");
715 ASSERT_TRUE(f != nullptr) << dlerror();
716 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
717 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700718
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700719 // dlopen second time to make sure that the library was unloaded correctly
720 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
721 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800722#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700723 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 -0800724#else
725 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
726 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700727#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800728
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700729 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
730 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700731}
732
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700733TEST(dlfcn, dlopen_nodelete) {
734 static bool is_unloaded = false;
735
736 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
737 ASSERT_TRUE(handle != nullptr) << dlerror();
738 void (*set_unload_flag_ptr)(bool*);
739 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
740 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
741 set_unload_flag_ptr(&is_unloaded);
742
743 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
744 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
745 ASSERT_EQ(1729U, *taxicab_number);
746 *taxicab_number = 2;
747
748 dlclose(handle);
749 ASSERT_TRUE(!is_unloaded);
750
751 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
752 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
753 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
754
755
756 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
757 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
758 ASSERT_EQ(taxicab_number2, taxicab_number);
759
760 ASSERT_EQ(2U, *taxicab_number2);
761
762 dlclose(handle);
763 ASSERT_TRUE(!is_unloaded);
764}
765
766TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
767 static bool is_unloaded = false;
768
769 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
770 ASSERT_TRUE(handle != nullptr) << dlerror();
771 void (*set_unload_flag_ptr)(bool*);
772 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
773 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
774 set_unload_flag_ptr(&is_unloaded);
775
776 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
777 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
778
779 ASSERT_EQ(1729U, *taxicab_number);
780 *taxicab_number = 2;
781
782 // This RTLD_NODELETE should be ignored
783 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
784 ASSERT_TRUE(handle1 != nullptr) << dlerror();
785 ASSERT_EQ(handle, handle1);
786
787 dlclose(handle1);
788 dlclose(handle);
789
790 ASSERT_TRUE(is_unloaded);
791}
792
793TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
794 static bool is_unloaded = false;
795
796 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
797 ASSERT_TRUE(handle != nullptr) << dlerror();
798 void (*set_unload_flag_ptr)(bool*);
799 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
800 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
801 set_unload_flag_ptr(&is_unloaded);
802
803 dlclose(handle);
804 ASSERT_TRUE(!is_unloaded);
805}
806
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700807TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700808 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
809 ASSERT_TRUE(handle != nullptr) << dlerror();
810 int (*get_answer)();
811 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
812 ASSERT_TRUE(get_answer != nullptr) << dlerror();
813 ASSERT_EQ(42, get_answer());
814 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700815}
816
Elliott Hughese66190d2012-12-18 15:57:55 -0800817TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700818 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700819 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700820#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700821 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
822#else
823 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
824#endif
825}
826
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800827static void ConcurrentDlErrorFn(std::string& error) {
828 ASSERT_TRUE(dlerror() == nullptr);
829
830 void* handle = dlopen("/child/thread", RTLD_NOW);
831 ASSERT_TRUE(handle == nullptr);
832
833 const char* err = dlerror();
834 ASSERT_TRUE(err != nullptr);
835
836 error = err;
837}
838
839TEST(dlfcn, dlerror_concurrent_buffer) {
840 void* handle = dlopen("/main/thread", RTLD_NOW);
841 ASSERT_TRUE(handle == nullptr);
842 const char* main_thread_error = dlerror();
843 ASSERT_TRUE(main_thread_error != nullptr);
844 ASSERT_SUBSTR("/main/thread", main_thread_error);
845
846 std::string child_thread_error;
847 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
848 t.join();
849 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
850
851 // Check that main thread local buffer was not modified.
852 ASSERT_SUBSTR("/main/thread", main_thread_error);
Elliott Hughes5419b942012-10-16 15:54:46 -0700853}
854
Elliott Hughese66190d2012-12-18 15:57:55 -0800855TEST(dlfcn, dlerror_concurrent) {
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800856 void* handle = dlopen("/main/thread", RTLD_NOW);
857 ASSERT_TRUE(handle == nullptr);
858
859 std::string child_thread_error;
860 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
861 t.join();
862 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
863
Elliott Hughes5419b942012-10-16 15:54:46 -0700864 const char* main_thread_error = dlerror();
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800865 ASSERT_TRUE(main_thread_error != nullptr);
Elliott Hughes5419b942012-10-16 15:54:46 -0700866 ASSERT_SUBSTR("/main/thread", main_thread_error);
867}
868
Elliott Hughese66190d2012-12-18 15:57:55 -0800869TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700870 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700871 void* self = dlopen(nullptr, RTLD_NOW);
872 ASSERT_TRUE(self != nullptr);
873 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700874
875 void* sym;
876
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700877#if defined(__BIONIC__) && !defined(__LP64__)
878 // RTLD_DEFAULT in lp32 bionic is not (void*)0
879 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700880 sym = dlsym(nullptr, "test");
881 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800882 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700883#endif
884
Elliott Hughes3b297c42012-10-11 16:08:51 -0700885 // Symbol that doesn't exist.
886 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700887 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700888 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700889
890 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700891}
892
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700893TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700894 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700895 void* self = dlopen(nullptr, RTLD_NOW);
896 ASSERT_TRUE(self != nullptr);
897 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700898
899 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700900 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700901
902 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
903 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
904
905 Dl_info info;
906 int rc = dladdr(addr, &info);
907 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
908
909 // Get the name of this executable.
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700910 const std::string& executable_path = get_executable_path();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700911
912 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700913 char dli_realpath[PATH_MAX];
914 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700915 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700916
917 // The symbol name should be the symbol we looked up.
918 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
919
920 // The address should be the exact address of the symbol.
921 ASSERT_EQ(info.dli_saddr, sym);
922
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700923 std::vector<map_record> maps;
924 ASSERT_TRUE(Maps::parse_maps(&maps));
925
926 void* base_address = nullptr;
927 for (const map_record& rec : maps) {
928 if (executable_path == rec.pathname) {
929 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700930 break;
931 }
932 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700933
934 // The base address should be the address we were loaded at.
935 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700936
937 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700938}
939
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700940TEST(dlfcn, dlopen_executable_by_absolute_path) {
941 void* handle1 = dlopen(nullptr, RTLD_NOW);
942 ASSERT_TRUE(handle1 != nullptr) << dlerror();
943
944 void* handle2 = dlopen(get_executable_path().c_str(), RTLD_NOW);
945 ASSERT_TRUE(handle2 != nullptr) << dlerror();
946
947#if defined(__BIONIC__)
948 ASSERT_EQ(handle1, handle2);
949#else
950 GTEST_LOG_(INFO) << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
951 "it loads a separate copy of the main executable "
952 "on dlopen by absolute path.";
953#endif
954}
955
Victor Khimenko14b9d712017-01-25 19:59:16 +0100956#if defined (__aarch64__)
957#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm64/"
958#elif defined (__arm__)
959#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm/"
960#elif defined (__i386__)
961#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86/"
962#elif defined (__x86_64__)
963#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86_64/"
964#elif defined (__mips__)
965#if defined(__LP64__)
966#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips64/"
967#else
968#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips/"
969#endif
970#else
971#error "Unknown architecture"
972#endif
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200973#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100974#define ALTERNATE_PATH_TO_LIBC ALTERNATE_PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700975
976TEST(dlfcn, dladdr_libc) {
977#if defined(__BIONIC__)
978 Dl_info info;
979 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
980 ASSERT_TRUE(dladdr(addr, &info) != 0);
981
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700982 char libc_realpath[PATH_MAX];
Victor Khimenko14b9d712017-01-25 19:59:16 +0100983
984 // Check if libc is in canonical path or in alternate path.
985 if (strncmp(ALTERNATE_PATH_TO_SYSTEM_LIB,
986 info.dli_fname,
987 sizeof(ALTERNATE_PATH_TO_SYSTEM_LIB) - 1) == 0) {
988 // Platform with emulated architecture. Symlink on ARC++.
989 ASSERT_TRUE(realpath(ALTERNATE_PATH_TO_LIBC, libc_realpath) == libc_realpath);
990 } else {
991 // /system/lib is symlink when this test is executed on host.
992 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
993 }
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700994
995 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700996 // TODO: add check for dfi_fbase
997 ASSERT_STREQ("puts", info.dli_sname);
998 ASSERT_EQ(addr, info.dli_saddr);
999#else
1000 GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig "
1001 "for libc.so, which is symlink itself (not a realpath).\n";
1002#endif
1003}
1004
Elliott Hughese66190d2012-12-18 15:57:55 -08001005TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -07001006 Dl_info info;
1007
Elliott Hughes3b297c42012-10-11 16:08:51 -07001008 dlerror(); // Clear any pending errors.
1009
Elliott Hughes8e15b082012-09-26 11:44:01 -07001010 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001011 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
1012 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001013
1014 // No symbol corresponding to a stack address.
1015 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001016 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001017}
Elliott Hughes124fae92012-10-31 14:20:03 -07001018
Elliott Hughesa43e9062013-01-07 14:18:22 -08001019// GNU-style ELF hash tables are incompatible with the MIPS ABI.
1020// 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 -08001021TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001022#if !defined(__mips__)
Elliott Hughes124fae92012-10-31 14:20:03 -07001023 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001024 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
1025 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001026 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001027 void* sym = dlsym(handle, "getRandomNumber");
1028 ASSERT_TRUE(sym != nullptr) << dlerror();
1029 int (*fn)(void);
1030 fn = reinterpret_cast<int (*)(void)>(sym);
1031 EXPECT_EQ(4, fn());
1032
1033 Dl_info dlinfo;
1034 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1035
1036 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1037 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001038 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001039#else
1040 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
1041#endif
Elliott Hughes124fae92012-10-31 14:20:03 -07001042}
Elliott Hughese66190d2012-12-18 15:57:55 -08001043
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001044TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
1045 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
1046 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001047 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001048 void* sym = dlsym(handle, "getRandomNumber");
1049 ASSERT_TRUE(sym != nullptr) << dlerror();
1050 int (*fn)(void);
1051 fn = reinterpret_cast<int (*)(void)>(sym);
1052 EXPECT_EQ(4, fn());
1053
1054 Dl_info dlinfo;
1055 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1056
1057 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1058 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
1059 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
1060}
1061
Elliott Hughese66190d2012-12-18 15:57:55 -08001062TEST(dlfcn, dlopen_bad_flags) {
1063 dlerror(); // Clear any pending errors.
1064 void* handle;
1065
Elliott Hughes063525c2014-05-13 11:19:57 -07001066#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -08001067 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001068 handle = dlopen(nullptr, 0);
1069 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001070 ASSERT_SUBSTR("invalid", dlerror());
1071#endif
1072
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001073 handle = dlopen(nullptr, 0xffffffff);
1074 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001075 ASSERT_SUBSTR("invalid", dlerror());
1076
1077 // 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 -07001078 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
1079 ASSERT_TRUE(handle != nullptr);
1080 ASSERT_SUBSTR(nullptr, dlerror());
Elliott Hughese66190d2012-12-18 15:57:55 -08001081}
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001082
1083TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001084 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001085 ASSERT_TRUE(addr == nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001086}
1087
1088TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001089 void* addr = dlsym(RTLD_DEFAULT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001090 ASSERT_TRUE(addr != nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001091}
1092
1093TEST(dlfcn, rtld_next_unknown_symbol) {
1094 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001095 ASSERT_TRUE(addr == nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001096}
1097
1098TEST(dlfcn, rtld_next_known_symbol) {
1099 void* addr = dlsym(RTLD_NEXT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001100 ASSERT_TRUE(addr != nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001101}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001102
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001103// Check that RTLD_NEXT of a libc symbol works in dlopened library
1104TEST(dlfcn, rtld_next_from_library) {
1105 void* library_with_close = dlopen("libtest_check_rtld_next_from_library.so", RTLD_NOW);
1106 ASSERT_TRUE(library_with_close != nullptr) << dlerror();
1107 void* expected_addr = dlsym(RTLD_DEFAULT, "close");
1108 ASSERT_TRUE(expected_addr != nullptr) << dlerror();
1109 typedef void* (*get_libc_close_ptr_fn_t)();
1110 get_libc_close_ptr_fn_t get_libc_close_ptr =
1111 reinterpret_cast<get_libc_close_ptr_fn_t>(dlsym(library_with_close, "get_libc_close_ptr"));
1112 ASSERT_TRUE(get_libc_close_ptr != nullptr) << dlerror();
1113 ASSERT_EQ(expected_addr, get_libc_close_ptr());
1114
1115 dlclose(library_with_close);
1116}
1117
1118
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001119TEST(dlfcn, dlsym_weak_func) {
1120 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001121 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001122 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001123
1124 int (*weak_func)();
1125 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001126 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001127 EXPECT_EQ(42, weak_func());
1128 dlclose(handle);
1129}
1130
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001131TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001132 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
1133 ASSERT_TRUE(handle != nullptr) << dlerror();
1134 int (*weak_func)();
1135 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
1136 ASSERT_TRUE(weak_func != nullptr) << dlerror();
1137 EXPECT_EQ(6551, weak_func());
1138 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001139}
1140
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001141TEST(dlfcn, dlopen_symlink) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001142 DlfcnSymlink symlink("dlopen_symlink");
1143 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001144 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001145 void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001146 ASSERT_TRUE(handle1 != nullptr);
1147 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001148 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -07001149 dlclose(handle1);
1150 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001151}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001152
1153// libtest_dlopen_from_ctor_main.so depends on
1154// libtest_dlopen_from_ctor.so which has a constructor
1155// that calls dlopen(libc...). This is to test the situation
1156// described in b/7941716.
1157TEST(dlfcn, dlopen_dlopen_from_ctor) {
1158#if defined(__BIONIC__)
1159 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1160 ASSERT_TRUE(handle != nullptr) << dlerror();
1161 dlclose(handle);
1162#else
1163 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
1164#endif
1165}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001166
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001167static std::string g_fini_call_order_str;
1168
1169static void register_fini_call(const char* s) {
1170 g_fini_call_order_str += s;
1171}
1172
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001173static void test_init_fini_call_order_for(const char* libname) {
1174 g_fini_call_order_str.clear();
1175 void* handle = dlopen(libname, RTLD_NOW);
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001176 ASSERT_TRUE(handle != nullptr) << dlerror();
1177 typedef int (*get_init_order_number_t)();
1178 get_init_order_number_t get_init_order_number =
1179 reinterpret_cast<get_init_order_number_t>(dlsym(handle, "get_init_order_number"));
1180 ASSERT_EQ(321, get_init_order_number());
1181
1182 typedef void (*set_fini_callback_t)(void (*f)(const char*));
1183 set_fini_callback_t set_fini_callback =
1184 reinterpret_cast<set_fini_callback_t>(dlsym(handle, "set_fini_callback"));
1185 set_fini_callback(register_fini_call);
1186 dlclose(handle);
1187 ASSERT_EQ("(root)(child)(grandchild)", g_fini_call_order_str);
1188}
1189
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001190TEST(dlfcn, init_fini_call_order) {
1191 test_init_fini_call_order_for("libtest_init_fini_order_root.so");
1192 test_init_fini_call_order_for("libtest_init_fini_order_root2.so");
1193}
1194
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001195TEST(dlfcn, symbol_versioning_use_v1) {
1196 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1197 ASSERT_TRUE(handle != nullptr) << dlerror();
1198 typedef int (*fn_t)();
1199 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1200 ASSERT_TRUE(fn != nullptr) << dlerror();
1201 ASSERT_EQ(1, fn());
1202 dlclose(handle);
1203}
1204
1205TEST(dlfcn, symbol_versioning_use_v2) {
1206 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1207 ASSERT_TRUE(handle != nullptr) << dlerror();
1208 typedef int (*fn_t)();
1209 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1210 ASSERT_TRUE(fn != nullptr) << dlerror();
1211 ASSERT_EQ(2, fn());
1212 dlclose(handle);
1213}
1214
1215TEST(dlfcn, symbol_versioning_use_other_v2) {
1216 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1217 ASSERT_TRUE(handle != nullptr) << dlerror();
1218 typedef int (*fn_t)();
1219 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1220 ASSERT_TRUE(fn != nullptr) << dlerror();
1221 ASSERT_EQ(20, fn());
1222 dlclose(handle);
1223}
1224
1225TEST(dlfcn, symbol_versioning_use_other_v3) {
1226 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1227 ASSERT_TRUE(handle != nullptr) << dlerror();
1228 typedef int (*fn_t)();
1229 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1230 ASSERT_TRUE(fn != nullptr) << dlerror();
1231 ASSERT_EQ(3, fn());
1232 dlclose(handle);
1233}
1234
1235TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1236 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1237 ASSERT_TRUE(handle != nullptr) << dlerror();
1238 typedef int (*fn_t)();
1239 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1240 ASSERT_TRUE(fn != nullptr) << dlerror();
1241 ASSERT_EQ(3, fn()); // the default version is 3
1242 dlclose(handle);
1243}
1244
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001245TEST(dlfcn, dlvsym_smoke) {
1246 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1247 ASSERT_TRUE(handle != nullptr) << dlerror();
1248 typedef int (*fn_t)();
1249
1250 {
1251 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1252 ASSERT_TRUE(fn == nullptr);
1253 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1254 }
1255
1256 {
1257 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1258 ASSERT_TRUE(fn != nullptr) << dlerror();
1259 ASSERT_EQ(2, fn());
1260 }
1261
1262 dlclose(handle);
1263}
1264
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001265// This preempts the implementation from libtest_versioned_lib.so
1266extern "C" int version_zero_function() {
1267 return 0;
1268}
1269
1270// This preempts the implementation from libtest_versioned_uselibv*.so
1271extern "C" int version_zero_function2() {
1272 return 0;
1273}
Evgenii Stepanov68650822015-06-10 13:38:39 -07001274
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001275TEST(dlfcn, dt_runpath_smoke) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001276 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1277 ASSERT_TRUE(handle != nullptr) << dlerror();
1278
1279 typedef void *(* dlopen_b_fn)();
1280 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1281 ASSERT_TRUE(fn != nullptr) << dlerror();
1282
1283 void *p = fn();
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001284 ASSERT_TRUE(p != nullptr);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001285
1286 dlclose(handle);
1287}
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001288
Dimitry Ivanovdb6ab3d2017-06-27 11:02:51 -07001289TEST(dlfcn, dt_runpath_absolute_path) {
1290 std::string libpath = get_testlib_root() + "/libtest_dt_runpath_d.so";
1291 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1292 ASSERT_TRUE(handle != nullptr) << dlerror();
1293
1294 typedef void *(* dlopen_b_fn)();
1295 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1296 ASSERT_TRUE(fn != nullptr) << dlerror();
1297
1298 void *p = fn();
1299 ASSERT_TRUE(p != nullptr);
1300
1301 dlclose(handle);
1302}
1303
Elliott Hughes8ad40932017-06-15 15:12:29 -07001304TEST(dlfcn, RTLD_macros) {
1305#if !defined(RTLD_LOCAL)
1306#error no RTLD_LOCAL
1307#elif !defined(RTLD_LAZY)
1308#error no RTLD_LAZY
1309#elif !defined(RTLD_NOW)
1310#error no RTLD_NOW
1311#elif !defined(RTLD_NOLOAD)
1312#error no RTLD_NOLOAD
1313#elif !defined(RTLD_GLOBAL)
1314#error no RTLD_GLOBAL
1315#elif !defined(RTLD_NODELETE)
1316#error no RTLD_NODELETE
1317#endif
1318}
1319
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001320// Bionic specific tests
1321#if defined(__BIONIC__)
1322
Dimitry Ivanovfc32dcb2017-03-24 10:58:23 -07001323#if defined(__arm__)
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001324const llvm::ELF::Elf32_Dyn* to_dynamic_table(const char* p) {
1325 return reinterpret_cast<const llvm::ELF::Elf32_Dyn*>(p);
1326}
1327
1328// Duplicate these definitions here because they are android specific
1329// note that we cannot include <elf.h> because #defines conflict with
1330// enum names provided by LLVM.
1331#define DT_ANDROID_REL (llvm::ELF::DT_LOOS + 2)
1332#define DT_ANDROID_RELA (llvm::ELF::DT_LOOS + 4)
1333
1334template<typename ELFT>
1335void validate_compatibility_of_native_library(const std::string& path, ELFT* elf) {
1336 bool has_elf_hash = false;
1337 bool has_android_rel = false;
1338 bool has_rel = false;
1339 // Find dynamic section and check that DT_HASH and there is no DT_ANDROID_REL
1340 for (auto it = elf->section_begin(); it != elf->section_end(); ++it) {
1341 const llvm::object::ELFSectionRef& section_ref = *it;
1342 if (section_ref.getType() == llvm::ELF::SHT_DYNAMIC) {
1343 llvm::StringRef data;
1344 ASSERT_TRUE(!it->getContents(data)) << "unable to get SHT_DYNAMIC section data";
1345 for (auto d = to_dynamic_table(data.data()); d->d_tag != llvm::ELF::DT_NULL; ++d) {
1346 if (d->d_tag == llvm::ELF::DT_HASH) {
1347 has_elf_hash = true;
1348 } else if (d->d_tag == DT_ANDROID_REL || d->d_tag == DT_ANDROID_RELA) {
1349 has_android_rel = true;
1350 } else if (d->d_tag == llvm::ELF::DT_REL || d->d_tag == llvm::ELF::DT_RELA) {
1351 has_rel = true;
1352 }
1353 }
1354
1355 break;
1356 }
1357 }
1358
1359 ASSERT_TRUE(has_elf_hash) << path.c_str() << ": missing elf hash (DT_HASH)";
1360 ASSERT_TRUE(!has_android_rel) << path.c_str() << ": has packed relocations";
1361 ASSERT_TRUE(has_rel) << path.c_str() << ": missing DT_REL/DT_RELA";
1362}
1363
1364void validate_compatibility_of_native_library(const char* soname) {
Victor Khimenko14b9d712017-01-25 19:59:16 +01001365 // On the systems with emulation system libraries would be of different
1366 // architecture. Try to use alternate paths first.
1367 std::string path = std::string(ALTERNATE_PATH_TO_SYSTEM_LIB) + soname;
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001368 auto binary_or_error = llvm::object::createBinary(path);
Victor Khimenko14b9d712017-01-25 19:59:16 +01001369 if (!binary_or_error) {
1370 path = std::string(PATH_TO_SYSTEM_LIB) + soname;
1371 binary_or_error = llvm::object::createBinary(path);
1372 }
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001373 ASSERT_FALSE(!binary_or_error);
1374
1375 llvm::object::Binary* binary = binary_or_error.get().getBinary();
1376
1377 auto obj = llvm::dyn_cast<llvm::object::ObjectFile>(binary);
1378 ASSERT_TRUE(obj != nullptr);
1379
1380 auto elf = llvm::dyn_cast<llvm::object::ELF32LEObjectFile>(obj);
1381
1382 ASSERT_TRUE(elf != nullptr);
1383
1384 validate_compatibility_of_native_library(path, elf);
1385}
1386
Dimitry Ivanovfc32dcb2017-03-24 10:58:23 -07001387// This is a test for app compatibility workaround for arm apps
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001388// affected by http://b/24465209
1389TEST(dlext, compat_elf_hash_and_relocation_tables) {
1390 validate_compatibility_of_native_library("libc.so");
1391 validate_compatibility_of_native_library("liblog.so");
1392 validate_compatibility_of_native_library("libstdc++.so");
1393 validate_compatibility_of_native_library("libdl.so");
1394 validate_compatibility_of_native_library("libm.so");
1395 validate_compatibility_of_native_library("libz.so");
1396 validate_compatibility_of_native_library("libjnigraphics.so");
1397}
1398
Dimitry Ivanovfc32dcb2017-03-24 10:58:23 -07001399#endif // defined(__arm__)
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001400
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001401TEST(dlfcn, dlopen_invalid_rw_load_segment) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001402 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001403 "/" + kPrebuiltElfDir +
1404 "/libtest_invalid-rw_load_segment.so";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001405 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1406 ASSERT_TRUE(handle == nullptr);
1407 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W + E load segments are not allowed";
1408 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1409}
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001410
1411TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001412 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001413 "/" + kPrebuiltElfDir +
1414 "/libtest_invalid-unaligned_shdr_offset.so";
1415
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001416 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1417 ASSERT_TRUE(handle == nullptr);
1418 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
1419 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1420}
1421
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001422TEST(dlfcn, dlopen_invalid_zero_shentsize) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001423 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001424 "/" + kPrebuiltElfDir +
1425 "/libtest_invalid-zero_shentsize.so";
1426
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001427 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1428 ASSERT_TRUE(handle == nullptr);
1429 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x";
1430 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1431}
1432
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001433TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001434 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001435 "/" + kPrebuiltElfDir +
1436 "/libtest_invalid-zero_shstrndx.so";
1437
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001438 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1439 ASSERT_TRUE(handle == nullptr);
1440 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx";
1441 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1442}
1443
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001444TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001445 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001446 "/" + kPrebuiltElfDir +
1447 "/libtest_invalid-empty_shdr_table.so";
1448
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001449 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1450 ASSERT_TRUE(handle == nullptr);
1451 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers";
1452 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1453}
1454
Dimitry Ivanov46230442016-08-15 13:40:53 -07001455TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001456 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001457 "/" + kPrebuiltElfDir +
1458 "/libtest_invalid-zero_shdr_table_offset.so";
1459
Dimitry Ivanov46230442016-08-15 13:40:53 -07001460 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1461 ASSERT_TRUE(handle == nullptr);
1462 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/";
1463 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1464}
1465
Dimitry Ivanov55958342016-08-15 14:06:04 -07001466TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001467 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001468 "/" + kPrebuiltElfDir +
1469 "/libtest_invalid-zero_shdr_table_content.so";
1470
Dimitry Ivanov55958342016-08-15 14:06:04 -07001471 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1472 ASSERT_TRUE(handle == nullptr);
1473 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
1474 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1475}
1476
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001477TEST(dlfcn, dlopen_invalid_textrels) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001478 const std::string libpath = get_testlib_root() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001479 "/" + kPrebuiltElfDir +
1480 "/libtest_invalid-textrels.so";
1481
1482 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1483 ASSERT_TRUE(handle == nullptr);
1484 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1485 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1486}
1487
1488TEST(dlfcn, dlopen_invalid_textrels2) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001489 const std::string libpath = get_testlib_root() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001490 "/" + kPrebuiltElfDir +
1491 "/libtest_invalid-textrels2.so";
1492
1493 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1494 ASSERT_TRUE(handle == nullptr);
1495 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1496 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1497}
1498
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001499#endif