blob: 2bd9476fa9ce7aff9b4858524c936accf5ff6095 [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 <libgen.h>
21#include <limits.h>
22#include <stdio.h>
23#include <stdint.h>
24
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -070025#include "private/ScopeGuard.h"
26
Elliott Hughes8e15b082012-09-26 11:44:01 -070027#include <string>
jeffhaoacf5aa72012-09-12 17:25:30 -070028
Elliott Hughes3b297c42012-10-11 16:08:51 -070029#define ASSERT_SUBSTR(needle, haystack) \
30 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
31
Simon Baldwinaef71952015-01-16 13:22:54 +000032#if defined(__LP64__)
33#define LIBPATH_PREFIX "/nativetest64/libdlext_test_fd/"
34#else
35#define LIBPATH_PREFIX "/nativetest/libdlext_test_fd/"
36#endif
37
38#define LIBZIPPATH LIBPATH_PREFIX "libdlext_test_fd_zipaligned.zip"
39
Elliott Hughes1728b232014-05-14 10:02:03 -070040static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070041extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070042 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070043}
44
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070045static int g_ctor_function_called = 0;
46
47extern "C" void ctor_function() __attribute__ ((constructor));
48
49extern "C" void ctor_function() {
50 g_ctor_function_called = 17;
51}
52
53TEST(dlfcn, ctor_function_call) {
54 ASSERT_EQ(17, g_ctor_function_called);
55}
56
Elliott Hughese66190d2012-12-18 15:57:55 -080057TEST(dlfcn, dlsym_in_self) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070058 dlerror(); // Clear any pending errors.
jeffhaoacf5aa72012-09-12 17:25:30 -070059 void* self = dlopen(NULL, RTLD_NOW);
60 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -070061 ASSERT_TRUE(dlerror() == NULL);
jeffhaoacf5aa72012-09-12 17:25:30 -070062
63 void* sym = dlsym(self, "DlSymTestFunction");
64 ASSERT_TRUE(sym != NULL);
65
66 void (*function)() = reinterpret_cast<void(*)()>(sym);
67
Elliott Hughes1728b232014-05-14 10:02:03 -070068 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070069 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070070 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070071
72 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070073}
Elliott Hughes8e15b082012-09-26 11:44:01 -070074
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -070075TEST(dlfcn, dlsym_with_dependencies) {
76 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
77 ASSERT_TRUE(handle != NULL);
78 dlerror();
79 // This symbol is in DT_NEEDED library.
80 void* sym = dlsym(handle, "getRandomNumber");
81 ASSERT_TRUE(sym != NULL);
82 int (*fn)(void);
83 fn = reinterpret_cast<int (*)(void)>(sym);
84 EXPECT_EQ(4, fn());
85 dlclose(handle);
86}
87
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -070088TEST(dlfcn, dlopen_noload) {
89 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
90 ASSERT_TRUE(handle == NULL);
91 handle = dlopen("libtest_simple.so", RTLD_NOW);
92 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
93 ASSERT_TRUE(handle != NULL);
94 ASSERT_TRUE(handle2 != NULL);
95 ASSERT_TRUE(handle == handle2);
96 ASSERT_EQ(0, dlclose(handle));
97 ASSERT_EQ(0, dlclose(handle2));
98}
99
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700100TEST(dlfcn, dlopen_by_soname) {
101 static const char* soname = "libdlext_test_soname.so";
102 static const char* filename = "libdlext_test_different_soname.so";
103 // 1. Make sure there is no library with soname in default search path
104 void* handle = dlopen(soname, RTLD_NOW);
105 ASSERT_TRUE(handle == nullptr);
106
107 // 2. Load a library using filename
108 handle = dlopen(filename, RTLD_NOW);
109 ASSERT_TRUE(handle != nullptr) << dlerror();
110
111 // 3. Find library by soname
112 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
113 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
114 ASSERT_EQ(handle, handle_soname);
115
116 // 4. RTLD_NOLOAD should still work with filename
117 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
118 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
119 ASSERT_EQ(handle, handle_filename);
120
121 dlclose(handle_filename);
122 dlclose(handle_soname);
123 dlclose(handle);
124}
125
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700126// ifuncs are only supported on intel and arm64 for now
127#if defined (__aarch64__) || defined(__i386__) || defined(__x86_64__)
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700128TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700129 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700130
131 // ifunc's choice depends on whether IFUNC_CHOICE has a value
132 // first check the set case
133 setenv("IFUNC_CHOICE", "set", 1);
134 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
135 ASSERT_TRUE(handle != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700136 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
137 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700138 ASSERT_TRUE(foo_ptr != NULL);
139 ASSERT_TRUE(foo_library_ptr != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700140 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
141 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700142 dlclose(handle);
143
144 // then check the unset case
145 unsetenv("IFUNC_CHOICE");
146 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
147 ASSERT_TRUE(handle != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700148 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
149 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700150 ASSERT_TRUE(foo_ptr != NULL);
151 ASSERT_TRUE(foo_library_ptr != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700152 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
153 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0);
154 dlclose(handle);
155}
156
157TEST(dlfcn, ifunc_ctor_call) {
158 typedef const char* (*fn_ptr)();
159
160 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700161 ASSERT_TRUE(handle != nullptr) << dlerror();
162 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
163 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
164 ASSERT_STREQ("false", is_ctor_called());
165
166 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
167 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700168 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700169 dlclose(handle);
170}
171#endif
172
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700173TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
174 // This is the structure of the test library and
175 // its dt_needed libraries
176 // libtest_relo_check_dt_needed_order.so
177 // |
178 // +-> libtest_relo_check_dt_needed_order_1.so
179 // |
180 // +-> libtest_relo_check_dt_needed_order_2.so
181 //
182 // The root library references relo_test_get_answer_lib - which is defined
183 // in both dt_needed libraries, the correct relocation should
184 // use the function defined in libtest_relo_check_dt_needed_order_1.so
185 void* handle = nullptr;
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -0700186 auto guard = make_scope_guard([&]() {
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700187 dlclose(handle);
188 });
189
190 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
191 ASSERT_TRUE(handle != nullptr) << dlerror();
192
193 typedef int (*fn_t) (void);
194 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
195 ASSERT_TRUE(fn != nullptr) << dlerror();
196 ASSERT_EQ(1, fn());
197}
198
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700199TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700200 // Here is how the test library and its dt_needed
201 // libraries are arranged
202 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700203 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700204 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700205 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700206 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700207 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700208 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700209 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700210 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700211 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700212 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700213 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700214 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700215 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700216 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700217 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700218 //
219 // load order should be (1, 2, 3, a, b, d)
220 //
221 // get_answer() is defined in (2, 3, a, b, c)
222 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700223 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700224 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700225 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
226 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700227 typedef int (*fn_t) (void);
228 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700229 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700230 ASSERT_TRUE(fn != NULL) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700231 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700232 ASSERT_TRUE(fn2 != NULL) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700233
234 ASSERT_EQ(42, fn());
235 ASSERT_EQ(43, fn2());
236 dlclose(handle);
237}
238
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700239TEST(dlfcn, dlopen_check_order_reloc_siblings) {
240 // This is how this one works:
241 // we lookup and call get_answer which is defined in '_2.so'
242 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
243 // the correct _impl() is implemented by '_a.so';
244 //
245 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
246 //
247 // Here is the picture:
248 //
249 // libtest_check_order_reloc_siblings.so
250 // |
251 // +-> ..._1.so <- empty
252 // | |
253 // | +-> ..._a.so <- exports correct answer_impl()
254 // | |
255 // | +-> ..._b.so <- every other letter exporting incorrect one.
256 // |
257 // +-> ..._2.so <- empty
258 // | |
259 // | +-> ..._c.so
260 // | |
261 // | +-> ..._d.so
262 // |
263 // +-> ..._3.so <- empty
264 // |
265 // +-> ..._e.so
266 // |
267 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
268 // implements incorrect get_answer_impl()
269
270 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
271 ASSERT_TRUE(handle == nullptr);
272#ifdef __BIONIC__
273 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
274 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
275#endif
276
277 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
278 ASSERT_TRUE(handle != nullptr) << dlerror();
279
280 typedef int (*fn_t) (void);
281 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
282 ASSERT_TRUE(fn != nullptr) << dlerror();
283 ASSERT_EQ(42, fn());
284
285 ASSERT_EQ(0, dlclose(handle));
286}
287
288TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
289 // This test uses the same library as dlopen_check_order_reloc_siblings.
290 // Unlike dlopen_check_order_reloc_siblings it preloads
291 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
292 // dlopen(libtest_check_order_reloc_siblings.so)
293
294 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
295 ASSERT_TRUE(handle == nullptr);
296 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
297 ASSERT_TRUE(handle == nullptr);
298
299 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
300 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
301
302 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
303 ASSERT_TRUE(handle != nullptr) << dlerror();
304
305 ASSERT_EQ(0, dlclose(handle_for_1));
306
307 typedef int (*fn_t) (void);
308 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
309 ASSERT_TRUE(fn != nullptr) << dlerror();
310 ASSERT_EQ(42, fn());
311
312 ASSERT_EQ(0, dlclose(handle));
313}
314
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800315TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
316 // This is how this one works:
317 // we lookup and call grandchild_get_answer which is defined in '_2.so'
318 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
319 // the correct _impl() is implemented by '_c_1.so';
320 //
321 // Here is the picture of subtree:
322 //
323 // libtest_check_order_reloc_siblings.so
324 // |
325 // +-> ..._2.so <- grandchild_get_answer()
326 // |
327 // +-> ..._c.so <- empty
328 // | |
329 // | +-> _c_1.so <- exports correct answer_impl()
330 // | |
331 // | +-> _c_2.so <- exports incorrect answer_impl()
332 // |
333 // +-> ..._d.so <- empty
334
335 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
336 ASSERT_TRUE(handle == nullptr);
337#ifdef __BIONIC__
338 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
339 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
340#endif
341
342 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
343 ASSERT_TRUE(handle != nullptr) << dlerror();
344
345 typedef int (*fn_t) (void);
346 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
347 ASSERT_TRUE(fn != nullptr) << dlerror();
348 ASSERT_EQ(42, fn());
349
350 ASSERT_EQ(0, dlclose(handle));
351}
352
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700353TEST(dlfcn, dlopen_check_order_reloc_nephew) {
354 // This is how this one works:
355 // we lookup and call nephew_get_answer which is defined in '_2.so'
356 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
357 // the correct _impl() is implemented by '_a.so';
358 //
359 // Here is the picture:
360 //
361 // libtest_check_order_reloc_siblings.so
362 // |
363 // +-> ..._1.so <- empty
364 // | |
365 // | +-> ..._a.so <- exports correct answer_impl()
366 // | |
367 // | +-> ..._b.so <- every other letter exporting incorrect one.
368 // |
369 // +-> ..._2.so <- empty
370 // | |
371 // | +-> ..._c.so
372 // | |
373 // | +-> ..._d.so
374 // |
375 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
376 // |
377 // +-> ..._e.so
378 // |
379 // +-> ..._f.so
380
381 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
382 ASSERT_TRUE(handle == nullptr);
383#ifdef __BIONIC__
384 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
385 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
386#endif
387
388 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
389 ASSERT_TRUE(handle != nullptr) << dlerror();
390
391 typedef int (*fn_t) (void);
392 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
393 ASSERT_TRUE(fn != nullptr) << dlerror();
394 ASSERT_EQ(42, fn());
395
396 ASSERT_EQ(0, dlclose(handle));
397}
398
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800399TEST(dlfcn, check_unload_after_reloc) {
400 // This is how this one works:
401 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
402 // |
403 // +-> libtest_two_parents_child
404 //
405 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
406 // |
407 // +-> libtest_two_parents_child
408 //
409 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
410 // as a second step it dlopens parent2 and dlcloses parent1...
411
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700412 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
413 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800414
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700415 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
416 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800417
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700418 typedef int (*fn_t) (void);
419 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
420 ASSERT_TRUE(fn != nullptr) << dlerror();
421 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800422
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700423 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800424
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700425 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
426 ASSERT_TRUE(handle != nullptr);
427 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800428
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700429 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
430 ASSERT_TRUE(fn != nullptr) << dlerror();
431 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800432
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700433 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800434
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700435 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
436 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800437}
438
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700439extern "C" int check_order_reloc_root_get_answer_impl() {
440 return 42;
441}
442
443TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
444 // This is how this one works:
445 // we lookup and call get_answer3 which is defined in 'root.so'
446 // and in turn calls external root_get_answer_impl() defined in _2.so and
447 // above the correct _impl() is one in the executable.
448 //
449 // libtest_check_order_reloc_root.so
450 // |
451 // +-> ..._1.so <- empty
452 // |
453 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
454 //
455
456 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
457 ASSERT_TRUE(handle == nullptr);
458#ifdef __BIONIC__
459 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
460 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
461#endif
462
463 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
464 ASSERT_TRUE(handle != nullptr) << dlerror();
465
466 typedef int (*fn_t) (void);
467 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
468 ASSERT_TRUE(fn != nullptr) << dlerror();
469 ASSERT_EQ(42, fn());
470
471 ASSERT_EQ(0, dlclose(handle));
472}
473
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700474TEST(dlfcn, dlopen_check_rtld_local) {
475 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
476 ASSERT_TRUE(sym == nullptr);
477
478 // implicit RTLD_LOCAL
479 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
480 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
481 ASSERT_TRUE(sym == nullptr);
482 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
483 sym = dlsym(handle, "dlopen_testlib_simple_func");
484 ASSERT_TRUE(sym != nullptr);
485 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
486 dlclose(handle);
487
488 // explicit RTLD_LOCAL
489 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
490 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
491 ASSERT_TRUE(sym == nullptr);
492 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
493 sym = dlsym(handle, "dlopen_testlib_simple_func");
494 ASSERT_TRUE(sym != nullptr);
495 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
496 dlclose(handle);
497}
498
499TEST(dlfcn, dlopen_check_rtld_global) {
500 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
501 ASSERT_TRUE(sym == nullptr);
502
503 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700504 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700505 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
506 ASSERT_TRUE(sym != nullptr) << dlerror();
507 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
508 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700509
510 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
511 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
512 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700513}
514
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700515// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
516// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
517// libtest_with_dependency_loop_a.so
518TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700519 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
520 ASSERT_TRUE(handle != nullptr) << dlerror();
521 void* f = dlsym(handle, "dlopen_test_loopy_function");
522 ASSERT_TRUE(f != nullptr) << dlerror();
523 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
524 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700525
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700526 // dlopen second time to make sure that the library was unloaded correctly
527 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
528 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800529#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700530 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
531 ASSERT_STREQ("dlopen failed: library \"libtest_with_dependency_loop.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700532#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800533
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700534 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
535 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700536}
537
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700538TEST(dlfcn, dlopen_nodelete) {
539 static bool is_unloaded = false;
540
541 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
542 ASSERT_TRUE(handle != nullptr) << dlerror();
543 void (*set_unload_flag_ptr)(bool*);
544 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
545 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
546 set_unload_flag_ptr(&is_unloaded);
547
548 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
549 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
550 ASSERT_EQ(1729U, *taxicab_number);
551 *taxicab_number = 2;
552
553 dlclose(handle);
554 ASSERT_TRUE(!is_unloaded);
555
556 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
557 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
558 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
559
560
561 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
562 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
563 ASSERT_EQ(taxicab_number2, taxicab_number);
564
565 ASSERT_EQ(2U, *taxicab_number2);
566
567 dlclose(handle);
568 ASSERT_TRUE(!is_unloaded);
569}
570
571TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
572 static bool is_unloaded = false;
573
574 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
575 ASSERT_TRUE(handle != nullptr) << dlerror();
576 void (*set_unload_flag_ptr)(bool*);
577 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
578 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
579 set_unload_flag_ptr(&is_unloaded);
580
581 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
582 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
583
584 ASSERT_EQ(1729U, *taxicab_number);
585 *taxicab_number = 2;
586
587 // This RTLD_NODELETE should be ignored
588 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
589 ASSERT_TRUE(handle1 != nullptr) << dlerror();
590 ASSERT_EQ(handle, handle1);
591
592 dlclose(handle1);
593 dlclose(handle);
594
595 ASSERT_TRUE(is_unloaded);
596}
597
598TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
599 static bool is_unloaded = false;
600
601 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
602 ASSERT_TRUE(handle != nullptr) << dlerror();
603 void (*set_unload_flag_ptr)(bool*);
604 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
605 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
606 set_unload_flag_ptr(&is_unloaded);
607
608 dlclose(handle);
609 ASSERT_TRUE(!is_unloaded);
610}
611
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700612TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanov4e446b12014-10-31 17:27:02 -0700613#if !defined(__arm__) && !defined(__aarch64__)
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700614 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
615 ASSERT_TRUE(handle != nullptr) << dlerror();
616 int (*get_answer)();
617 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
618 ASSERT_TRUE(get_answer != nullptr) << dlerror();
619 ASSERT_EQ(42, get_answer());
620 ASSERT_EQ(0, dlclose(handle));
621#else
Dmitriy Ivanov4e446b12014-10-31 17:27:02 -0700622 GTEST_LOG_(INFO) << "This test does nothing on arm/arm64 (to be reenabled once b/18137520 or b/18130452 are fixed).\n";
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700623#endif
624}
625
Elliott Hughese66190d2012-12-18 15:57:55 -0800626TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700627 void* self = dlopen("/does/not/exist", RTLD_NOW);
628 ASSERT_TRUE(self == NULL);
Elliott Hughes063525c2014-05-13 11:19:57 -0700629#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700630 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
631#else
632 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
633#endif
634}
635
Elliott Hughesad88a082012-10-24 18:37:21 -0700636static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700637 dlopen("/child/thread", RTLD_NOW);
638 return reinterpret_cast<void*>(strdup(dlerror()));
639}
640
Elliott Hughese66190d2012-12-18 15:57:55 -0800641TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700642 dlopen("/main/thread", RTLD_NOW);
643 const char* main_thread_error = dlerror();
644 ASSERT_SUBSTR("/main/thread", main_thread_error);
645
646 pthread_t t;
647 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
648 void* result;
649 ASSERT_EQ(0, pthread_join(t, &result));
650 char* child_thread_error = static_cast<char*>(result);
651 ASSERT_SUBSTR("/child/thread", child_thread_error);
652 free(child_thread_error);
653
654 ASSERT_SUBSTR("/main/thread", main_thread_error);
655}
656
Elliott Hughese66190d2012-12-18 15:57:55 -0800657TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700658 dlerror(); // Clear any pending errors.
Elliott Hughes8e15b082012-09-26 11:44:01 -0700659 void* self = dlopen(NULL, RTLD_NOW);
660 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700661 ASSERT_TRUE(dlerror() == NULL);
662
663 void* sym;
664
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700665#if defined(__BIONIC__) && !defined(__LP64__)
666 // RTLD_DEFAULT in lp32 bionic is not (void*)0
667 // so it can be distinguished from the NULL handle.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700668 sym = dlsym(NULL, "test");
669 ASSERT_TRUE(sym == NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700670 ASSERT_SUBSTR("dlsym library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700671#endif
672
673 // NULL symbol name.
Elliott Hughes063525c2014-05-13 11:19:57 -0700674#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700675 // glibc marks this parameter non-null and SEGVs if you cheat.
676 sym = dlsym(self, NULL);
677 ASSERT_TRUE(sym == NULL);
678 ASSERT_SUBSTR("", dlerror());
679#endif
680
681 // Symbol that doesn't exist.
682 sym = dlsym(self, "ThisSymbolDoesNotExist");
683 ASSERT_TRUE(sym == NULL);
684 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700685
686 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700687}
688
Elliott Hughese66190d2012-12-18 15:57:55 -0800689TEST(dlfcn, dladdr) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700690 dlerror(); // Clear any pending errors.
691 void* self = dlopen(NULL, RTLD_NOW);
692 ASSERT_TRUE(self != NULL);
693 ASSERT_TRUE(dlerror() == NULL);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700694
695 void* sym = dlsym(self, "DlSymTestFunction");
696 ASSERT_TRUE(sym != NULL);
697
698 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
699 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
700
701 Dl_info info;
702 int rc = dladdr(addr, &info);
703 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
704
705 // Get the name of this executable.
706 char executable_path[PATH_MAX];
707 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
708 ASSERT_NE(rc, -1);
709 executable_path[rc] = '\0';
710 std::string executable_name(basename(executable_path));
711
712 // The filename should be that of this executable.
713 // Note that we don't know whether or not we have the full path, so we want an "ends_with" test.
714 std::string dli_fname(info.dli_fname);
715 dli_fname = basename(&dli_fname[0]);
716 ASSERT_EQ(dli_fname, executable_name);
717
718 // The symbol name should be the symbol we looked up.
719 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
720
721 // The address should be the exact address of the symbol.
722 ASSERT_EQ(info.dli_saddr, sym);
723
724 // Look in /proc/pid/maps to find out what address we were loaded at.
725 // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic.
726 void* base_address = NULL;
Elliott Hughes8e15b082012-09-26 11:44:01 -0700727 char line[BUFSIZ];
Elliott Hughes57b7a612014-08-25 17:26:50 -0700728 FILE* fp = fopen("/proc/self/maps", "r");
Elliott Hughes8e15b082012-09-26 11:44:01 -0700729 ASSERT_TRUE(fp != NULL);
730 while (fgets(line, sizeof(line), fp) != NULL) {
731 uintptr_t start = strtoul(line, 0, 16);
732 line[strlen(line) - 1] = '\0'; // Chomp the '\n'.
733 char* path = strchr(line, '/');
Elliott Hughes156da962012-10-09 17:14:56 -0700734 if (path != NULL && strcmp(executable_path, path) == 0) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700735 base_address = reinterpret_cast<void*>(start);
736 break;
737 }
738 }
739 fclose(fp);
740
741 // The base address should be the address we were loaded at.
742 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700743
744 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700745}
746
Elliott Hughese66190d2012-12-18 15:57:55 -0800747TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700748 Dl_info info;
749
Elliott Hughes3b297c42012-10-11 16:08:51 -0700750 dlerror(); // Clear any pending errors.
751
Elliott Hughes8e15b082012-09-26 11:44:01 -0700752 // No symbol corresponding to NULL.
753 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700754 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700755
756 // No symbol corresponding to a stack address.
757 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700758 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700759}
Elliott Hughes124fae92012-10-31 14:20:03 -0700760
Elliott Hughesa43e9062013-01-07 14:18:22 -0800761// GNU-style ELF hash tables are incompatible with the MIPS ABI.
762// 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 -0800763TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800764#if !defined(__mips__)
Elliott Hughes124fae92012-10-31 14:20:03 -0700765 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800766 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
767 ASSERT_TRUE(handle != nullptr) << dlerror();
768 auto guard = make_scope_guard([&]() {
769 dlclose(handle);
770 });
771 void* sym = dlsym(handle, "getRandomNumber");
772 ASSERT_TRUE(sym != nullptr) << dlerror();
773 int (*fn)(void);
774 fn = reinterpret_cast<int (*)(void)>(sym);
775 EXPECT_EQ(4, fn());
776
777 Dl_info dlinfo;
778 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
779
780 ASSERT_TRUE(fn == dlinfo.dli_saddr);
781 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800782 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800783#else
784 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
785#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700786}
Elliott Hughese66190d2012-12-18 15:57:55 -0800787
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800788TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
789 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
790 ASSERT_TRUE(handle != nullptr) << dlerror();
791 auto guard = make_scope_guard([&]() {
792 dlclose(handle);
793 });
794 void* sym = dlsym(handle, "getRandomNumber");
795 ASSERT_TRUE(sym != nullptr) << dlerror();
796 int (*fn)(void);
797 fn = reinterpret_cast<int (*)(void)>(sym);
798 EXPECT_EQ(4, fn());
799
800 Dl_info dlinfo;
801 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
802
803 ASSERT_TRUE(fn == dlinfo.dli_saddr);
804 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
805 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
806}
807
Elliott Hughese66190d2012-12-18 15:57:55 -0800808TEST(dlfcn, dlopen_bad_flags) {
809 dlerror(); // Clear any pending errors.
810 void* handle;
811
Elliott Hughes063525c2014-05-13 11:19:57 -0700812#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800813 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
814 handle = dlopen(NULL, 0);
815 ASSERT_TRUE(handle == NULL);
816 ASSERT_SUBSTR("invalid", dlerror());
817#endif
818
819 handle = dlopen(NULL, 0xffffffff);
820 ASSERT_TRUE(handle == NULL);
821 ASSERT_SUBSTR("invalid", dlerror());
822
823 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
824 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY);
825 ASSERT_TRUE(handle != NULL);
826 ASSERT_SUBSTR(NULL, dlerror());
827}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400828
829TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800830 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400831 ASSERT_TRUE(addr == NULL);
832}
833
834TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800835 void* addr = dlsym(RTLD_DEFAULT, "fopen");
836 ASSERT_TRUE(addr != NULL);
837}
838
839TEST(dlfcn, rtld_next_unknown_symbol) {
840 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
841 ASSERT_TRUE(addr == NULL);
842}
843
844TEST(dlfcn, rtld_next_known_symbol) {
845 void* addr = dlsym(RTLD_NEXT, "fopen");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400846 ASSERT_TRUE(addr != NULL);
847}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700848
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700849TEST(dlfcn, dlsym_weak_func) {
850 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800851 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700852 ASSERT_TRUE(handle != NULL);
853
854 int (*weak_func)();
855 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
856 ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror();
857 EXPECT_EQ(42, weak_func());
858 dlclose(handle);
859}
860
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800861TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700862 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
863 ASSERT_TRUE(handle != nullptr) << dlerror();
864 int (*weak_func)();
865 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
866 ASSERT_TRUE(weak_func != nullptr) << dlerror();
867 EXPECT_EQ(6551, weak_func());
868 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800869}
870
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700871TEST(dlfcn, dlopen_symlink) {
872 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
873 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
874 ASSERT_TRUE(handle1 != NULL);
875 ASSERT_TRUE(handle2 != NULL);
876 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -0700877 dlclose(handle1);
878 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700879}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800880
Simon Baldwinaef71952015-01-16 13:22:54 +0000881TEST(dlfcn, dlopen_from_zip_absolute_path) {
882 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
883
884 void* handle = dlopen((lib_path + "!libdir/libdlext_test_fd.so").c_str(), RTLD_NOW);
885 ASSERT_TRUE(handle != nullptr) << dlerror();
886
887 int (*fn)(void);
888 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
889 ASSERT_TRUE(fn != nullptr);
890 EXPECT_EQ(4, fn());
891
892 dlclose(handle);
893}
894
895TEST(dlfcn, dlopen_from_zip_ld_library_path) {
896 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH + "!libdir";
897
898 typedef void (*fn_t)(const char*);
899 fn_t android_update_LD_LIBRARY_PATH =
900 reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"));
901
902 ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror();
903
904 void* handle = dlopen("libdlext_test_fd.so", RTLD_NOW);
905 ASSERT_TRUE(handle == nullptr);
906
907 android_update_LD_LIBRARY_PATH(lib_path.c_str());
908
909 handle = dlopen("libdlext_test_fd.so", RTLD_NOW);
910 ASSERT_TRUE(handle != nullptr) << dlerror();
911
912 int (*fn)(void);
913 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
914 ASSERT_TRUE(fn != nullptr);
915 EXPECT_EQ(4, fn());
916
917 dlclose(handle);
918}
919
920
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800921// libtest_dlopen_from_ctor_main.so depends on
922// libtest_dlopen_from_ctor.so which has a constructor
923// that calls dlopen(libc...). This is to test the situation
924// described in b/7941716.
925TEST(dlfcn, dlopen_dlopen_from_ctor) {
926#if defined(__BIONIC__)
927 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
928 ASSERT_TRUE(handle != nullptr) << dlerror();
929 dlclose(handle);
930#else
931 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
932#endif
933}