ea20869d01a10f026b7f96ab9734926c61d9f19b
1 /*
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 */
17 #include <gtest/gtest.h>
19 #include <dlfcn.h>
20 #include <libgen.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdint.h>
25 #include "private/ScopeGuard.h"
27 #include <string>
29 #define ASSERT_SUBSTR(needle, haystack) \
30 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
32 static bool g_called = false;
33 extern "C" void DlSymTestFunction() {
34 g_called = true;
35 }
37 static int g_ctor_function_called = 0;
39 extern "C" void ctor_function() __attribute__ ((constructor));
41 extern "C" void ctor_function() {
42 g_ctor_function_called = 17;
43 }
45 TEST(dlfcn, ctor_function_call) {
46 ASSERT_EQ(17, g_ctor_function_called);
47 }
49 TEST(dlfcn, dlsym_in_self) {
50 dlerror(); // Clear any pending errors.
51 void* self = dlopen(NULL, RTLD_NOW);
52 ASSERT_TRUE(self != NULL);
53 ASSERT_TRUE(dlerror() == NULL);
55 void* sym = dlsym(self, "DlSymTestFunction");
56 ASSERT_TRUE(sym != NULL);
58 void (*function)() = reinterpret_cast<void(*)()>(sym);
60 g_called = false;
61 function();
62 ASSERT_TRUE(g_called);
64 ASSERT_EQ(0, dlclose(self));
65 }
67 TEST(dlfcn, dlsym_with_dependencies) {
68 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
69 ASSERT_TRUE(handle != NULL);
70 dlerror();
71 // This symbol is in DT_NEEDED library.
72 void* sym = dlsym(handle, "getRandomNumber");
73 ASSERT_TRUE(sym != NULL);
74 int (*fn)(void);
75 fn = reinterpret_cast<int (*)(void)>(sym);
76 EXPECT_EQ(4, fn());
77 dlclose(handle);
78 }
80 TEST(dlfcn, dlopen_noload) {
81 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
82 ASSERT_TRUE(handle == NULL);
83 handle = dlopen("libtest_simple.so", RTLD_NOW);
84 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
85 ASSERT_TRUE(handle != NULL);
86 ASSERT_TRUE(handle2 != NULL);
87 ASSERT_TRUE(handle == handle2);
88 ASSERT_EQ(0, dlclose(handle));
89 ASSERT_EQ(0, dlclose(handle2));
90 }
92 // ifuncs are only supported on intel and arm64 for now
93 #if defined (__aarch64__) || defined(__i386__) || defined(__x86_64__)
94 TEST(dlfcn, ifunc) {
95 typedef const char* (*fn_ptr)();
97 // ifunc's choice depends on whether IFUNC_CHOICE has a value
98 // first check the set case
99 setenv("IFUNC_CHOICE", "set", 1);
100 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
101 ASSERT_TRUE(handle != NULL);
102 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
103 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
104 ASSERT_TRUE(foo_ptr != NULL);
105 ASSERT_TRUE(foo_library_ptr != NULL);
106 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
107 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
108 dlclose(handle);
110 // then check the unset case
111 unsetenv("IFUNC_CHOICE");
112 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
113 ASSERT_TRUE(handle != NULL);
114 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
115 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
116 ASSERT_TRUE(foo_ptr != NULL);
117 ASSERT_TRUE(foo_library_ptr != NULL);
118 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
119 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0);
120 dlclose(handle);
121 }
123 TEST(dlfcn, ifunc_ctor_call) {
124 typedef const char* (*fn_ptr)();
126 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
127 ASSERT_TRUE(handle != nullptr) << dlerror();
128 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
129 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
130 ASSERT_STREQ("false", is_ctor_called());
132 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
133 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
134 ASSERT_STREQ("true", is_ctor_called());
135 dlclose(handle);
136 }
137 #endif
139 TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
140 // This is the structure of the test library and
141 // its dt_needed libraries
142 // libtest_relo_check_dt_needed_order.so
143 // |
144 // +-> libtest_relo_check_dt_needed_order_1.so
145 // |
146 // +-> libtest_relo_check_dt_needed_order_2.so
147 //
148 // The root library references relo_test_get_answer_lib - which is defined
149 // in both dt_needed libraries, the correct relocation should
150 // use the function defined in libtest_relo_check_dt_needed_order_1.so
151 void* handle = nullptr;
152 auto guard = make_scope_guard([&]() {
153 dlclose(handle);
154 });
156 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
157 ASSERT_TRUE(handle != nullptr) << dlerror();
159 typedef int (*fn_t) (void);
160 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
161 ASSERT_TRUE(fn != nullptr) << dlerror();
162 ASSERT_EQ(1, fn());
163 }
165 TEST(dlfcn, dlopen_check_order_dlsym) {
166 // Here is how the test library and its dt_needed
167 // libraries are arranged
168 //
169 // libtest_check_order_children.so
170 // |
171 // +-> ..._1_left.so
172 // | |
173 // | +-> ..._a.so
174 // | |
175 // | +-> ...r_b.so
176 // |
177 // +-> ..._2_right.so
178 // | |
179 // | +-> ..._d.so
180 // | |
181 // | +-> ..._b.so
182 // |
183 // +-> ..._3_c.so
184 //
185 // load order should be (1, 2, 3, a, b, d)
186 //
187 // get_answer() is defined in (2, 3, a, b, c)
188 // get_answer2() is defined in (b, d)
189 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
190 ASSERT_TRUE(sym == nullptr);
191 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
192 ASSERT_TRUE(handle != nullptr) << dlerror();
193 typedef int (*fn_t) (void);
194 fn_t fn, fn2;
195 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
196 ASSERT_TRUE(fn != NULL) << dlerror();
197 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
198 ASSERT_TRUE(fn2 != NULL) << dlerror();
200 ASSERT_EQ(42, fn());
201 ASSERT_EQ(43, fn2());
202 dlclose(handle);
203 }
205 TEST(dlfcn, dlopen_check_order_reloc_siblings) {
206 // This is how this one works:
207 // we lookup and call get_answer which is defined in '_2.so'
208 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
209 // the correct _impl() is implemented by '_a.so';
210 //
211 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
212 //
213 // Here is the picture:
214 //
215 // libtest_check_order_reloc_siblings.so
216 // |
217 // +-> ..._1.so <- empty
218 // | |
219 // | +-> ..._a.so <- exports correct answer_impl()
220 // | |
221 // | +-> ..._b.so <- every other letter exporting incorrect one.
222 // |
223 // +-> ..._2.so <- empty
224 // | |
225 // | +-> ..._c.so
226 // | |
227 // | +-> ..._d.so
228 // |
229 // +-> ..._3.so <- empty
230 // |
231 // +-> ..._e.so
232 // |
233 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
234 // implements incorrect get_answer_impl()
236 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
237 ASSERT_TRUE(handle == nullptr);
238 #ifdef __BIONIC__
239 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
240 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
241 #endif
243 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
244 ASSERT_TRUE(handle != nullptr) << dlerror();
246 typedef int (*fn_t) (void);
247 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
248 ASSERT_TRUE(fn != nullptr) << dlerror();
249 ASSERT_EQ(42, fn());
251 ASSERT_EQ(0, dlclose(handle));
252 }
254 TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
255 // This test uses the same library as dlopen_check_order_reloc_siblings.
256 // Unlike dlopen_check_order_reloc_siblings it preloads
257 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
258 // dlopen(libtest_check_order_reloc_siblings.so)
260 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
261 ASSERT_TRUE(handle == nullptr);
262 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
263 ASSERT_TRUE(handle == nullptr);
265 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
266 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
268 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
269 ASSERT_TRUE(handle != nullptr) << dlerror();
271 ASSERT_EQ(0, dlclose(handle_for_1));
273 typedef int (*fn_t) (void);
274 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
275 ASSERT_TRUE(fn != nullptr) << dlerror();
276 ASSERT_EQ(42, fn());
278 ASSERT_EQ(0, dlclose(handle));
279 }
281 TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
282 // This is how this one works:
283 // we lookup and call grandchild_get_answer which is defined in '_2.so'
284 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
285 // the correct _impl() is implemented by '_c_1.so';
286 //
287 // Here is the picture of subtree:
288 //
289 // libtest_check_order_reloc_siblings.so
290 // |
291 // +-> ..._2.so <- grandchild_get_answer()
292 // |
293 // +-> ..._c.so <- empty
294 // | |
295 // | +-> _c_1.so <- exports correct answer_impl()
296 // | |
297 // | +-> _c_2.so <- exports incorrect answer_impl()
298 // |
299 // +-> ..._d.so <- empty
301 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
302 ASSERT_TRUE(handle == nullptr);
303 #ifdef __BIONIC__
304 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
305 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
306 #endif
308 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
309 ASSERT_TRUE(handle != nullptr) << dlerror();
311 typedef int (*fn_t) (void);
312 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
313 ASSERT_TRUE(fn != nullptr) << dlerror();
314 ASSERT_EQ(42, fn());
316 ASSERT_EQ(0, dlclose(handle));
317 }
319 TEST(dlfcn, dlopen_check_order_reloc_nephew) {
320 // This is how this one works:
321 // we lookup and call nephew_get_answer which is defined in '_2.so'
322 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
323 // the correct _impl() is implemented by '_a.so';
324 //
325 // Here is the picture:
326 //
327 // libtest_check_order_reloc_siblings.so
328 // |
329 // +-> ..._1.so <- empty
330 // | |
331 // | +-> ..._a.so <- exports correct answer_impl()
332 // | |
333 // | +-> ..._b.so <- every other letter exporting incorrect one.
334 // |
335 // +-> ..._2.so <- empty
336 // | |
337 // | +-> ..._c.so
338 // | |
339 // | +-> ..._d.so
340 // |
341 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
342 // |
343 // +-> ..._e.so
344 // |
345 // +-> ..._f.so
347 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
348 ASSERT_TRUE(handle == nullptr);
349 #ifdef __BIONIC__
350 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
351 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
352 #endif
354 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
355 ASSERT_TRUE(handle != nullptr) << dlerror();
357 typedef int (*fn_t) (void);
358 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
359 ASSERT_TRUE(fn != nullptr) << dlerror();
360 ASSERT_EQ(42, fn());
362 ASSERT_EQ(0, dlclose(handle));
363 }
365 extern "C" int check_order_reloc_root_get_answer_impl() {
366 return 42;
367 }
369 TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
370 // This is how this one works:
371 // we lookup and call get_answer3 which is defined in 'root.so'
372 // and in turn calls external root_get_answer_impl() defined in _2.so and
373 // above the correct _impl() is one in the executable.
374 //
375 // libtest_check_order_reloc_root.so
376 // |
377 // +-> ..._1.so <- empty
378 // |
379 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
380 //
382 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
383 ASSERT_TRUE(handle == nullptr);
384 #ifdef __BIONIC__
385 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
386 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
387 #endif
389 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
390 ASSERT_TRUE(handle != nullptr) << dlerror();
392 typedef int (*fn_t) (void);
393 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
394 ASSERT_TRUE(fn != nullptr) << dlerror();
395 ASSERT_EQ(42, fn());
397 ASSERT_EQ(0, dlclose(handle));
398 }
400 TEST(dlfcn, dlopen_check_rtld_local) {
401 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
402 ASSERT_TRUE(sym == nullptr);
404 // implicit RTLD_LOCAL
405 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
406 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
407 ASSERT_TRUE(sym == nullptr);
408 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
409 sym = dlsym(handle, "dlopen_testlib_simple_func");
410 ASSERT_TRUE(sym != nullptr);
411 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
412 dlclose(handle);
414 // explicit RTLD_LOCAL
415 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
416 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
417 ASSERT_TRUE(sym == nullptr);
418 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
419 sym = dlsym(handle, "dlopen_testlib_simple_func");
420 ASSERT_TRUE(sym != nullptr);
421 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
422 dlclose(handle);
423 }
425 TEST(dlfcn, dlopen_check_rtld_global) {
426 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
427 ASSERT_TRUE(sym == nullptr);
429 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
430 ASSERT_TRUE(handle != nullptr) << dlerror();
431 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
432 ASSERT_TRUE(sym != nullptr) << dlerror();
433 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
434 dlclose(handle);
436 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
437 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
438 ASSERT_EQ(sym, sym_after_dlclose);
439 }
441 // libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
442 // libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
443 // libtest_with_dependency_loop_a.so
444 TEST(dlfcn, dlopen_check_loop) {
445 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
446 #if defined(__BIONIC__)
447 ASSERT_TRUE(handle == nullptr);
448 ASSERT_STREQ("dlopen failed: recursive link to \"libtest_with_dependency_loop_a.so\"", dlerror());
449 // This symbol should never be exposed
450 void* f = dlsym(RTLD_DEFAULT, "dlopen_test_invalid_function");
451 ASSERT_TRUE(f == nullptr);
452 ASSERT_SUBSTR("undefined symbol: dlopen_test_invalid_function", dlerror());
454 // dlopen second time to make sure that the library wasn't loaded even though dlopen returned null.
455 // This may happen if during cleanup the root library or one of the depended libs were not removed
456 // from soinfo list.
457 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
458 ASSERT_TRUE(handle == nullptr);
459 ASSERT_STREQ("dlopen failed: library \"libtest_with_dependency_loop.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
460 #else // glibc allows recursive links
461 ASSERT_TRUE(handle != nullptr);
462 dlclose(handle);
463 #endif
464 }
466 TEST(dlfcn, dlopen_nodelete) {
467 static bool is_unloaded = false;
469 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
470 ASSERT_TRUE(handle != nullptr) << dlerror();
471 void (*set_unload_flag_ptr)(bool*);
472 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
473 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
474 set_unload_flag_ptr(&is_unloaded);
476 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
477 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
478 ASSERT_EQ(1729U, *taxicab_number);
479 *taxicab_number = 2;
481 dlclose(handle);
482 ASSERT_TRUE(!is_unloaded);
484 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
485 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
486 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
489 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
490 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
491 ASSERT_EQ(taxicab_number2, taxicab_number);
493 ASSERT_EQ(2U, *taxicab_number2);
495 dlclose(handle);
496 ASSERT_TRUE(!is_unloaded);
497 }
499 TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
500 static bool is_unloaded = false;
502 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
503 ASSERT_TRUE(handle != nullptr) << dlerror();
504 void (*set_unload_flag_ptr)(bool*);
505 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
506 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
507 set_unload_flag_ptr(&is_unloaded);
509 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
510 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
512 ASSERT_EQ(1729U, *taxicab_number);
513 *taxicab_number = 2;
515 // This RTLD_NODELETE should be ignored
516 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
517 ASSERT_TRUE(handle1 != nullptr) << dlerror();
518 ASSERT_EQ(handle, handle1);
520 dlclose(handle1);
521 dlclose(handle);
523 ASSERT_TRUE(is_unloaded);
524 }
526 TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
527 static bool is_unloaded = false;
529 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
530 ASSERT_TRUE(handle != nullptr) << dlerror();
531 void (*set_unload_flag_ptr)(bool*);
532 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
533 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
534 set_unload_flag_ptr(&is_unloaded);
536 dlclose(handle);
537 ASSERT_TRUE(!is_unloaded);
538 }
540 TEST(dlfcn, dlsym_df_1_global) {
541 #if !defined(__arm__) && !defined(__aarch64__)
542 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
543 ASSERT_TRUE(handle != nullptr) << dlerror();
544 int (*get_answer)();
545 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
546 ASSERT_TRUE(get_answer != nullptr) << dlerror();
547 ASSERT_EQ(42, get_answer());
548 ASSERT_EQ(0, dlclose(handle));
549 #else
550 GTEST_LOG_(INFO) << "This test does nothing on arm/arm64 (to be reenabled once b/18137520 or b/18130452 are fixed).\n";
551 #endif
552 }
554 TEST(dlfcn, dlopen_failure) {
555 void* self = dlopen("/does/not/exist", RTLD_NOW);
556 ASSERT_TRUE(self == NULL);
557 #if defined(__BIONIC__)
558 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
559 #else
560 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
561 #endif
562 }
564 static void* ConcurrentDlErrorFn(void*) {
565 dlopen("/child/thread", RTLD_NOW);
566 return reinterpret_cast<void*>(strdup(dlerror()));
567 }
569 TEST(dlfcn, dlerror_concurrent) {
570 dlopen("/main/thread", RTLD_NOW);
571 const char* main_thread_error = dlerror();
572 ASSERT_SUBSTR("/main/thread", main_thread_error);
574 pthread_t t;
575 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
576 void* result;
577 ASSERT_EQ(0, pthread_join(t, &result));
578 char* child_thread_error = static_cast<char*>(result);
579 ASSERT_SUBSTR("/child/thread", child_thread_error);
580 free(child_thread_error);
582 ASSERT_SUBSTR("/main/thread", main_thread_error);
583 }
585 TEST(dlfcn, dlsym_failures) {
586 dlerror(); // Clear any pending errors.
587 void* self = dlopen(NULL, RTLD_NOW);
588 ASSERT_TRUE(self != NULL);
589 ASSERT_TRUE(dlerror() == NULL);
591 void* sym;
593 #if defined(__BIONIC__) && !defined(__LP64__)
594 // RTLD_DEFAULT in lp32 bionic is not (void*)0
595 // so it can be distinguished from the NULL handle.
596 sym = dlsym(NULL, "test");
597 ASSERT_TRUE(sym == NULL);
598 ASSERT_SUBSTR("dlsym library handle is null", dlerror());
599 #endif
601 // NULL symbol name.
602 #if defined(__BIONIC__)
603 // glibc marks this parameter non-null and SEGVs if you cheat.
604 sym = dlsym(self, NULL);
605 ASSERT_TRUE(sym == NULL);
606 ASSERT_SUBSTR("", dlerror());
607 #endif
609 // Symbol that doesn't exist.
610 sym = dlsym(self, "ThisSymbolDoesNotExist");
611 ASSERT_TRUE(sym == NULL);
612 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
614 ASSERT_EQ(0, dlclose(self));
615 }
617 TEST(dlfcn, dladdr) {
618 dlerror(); // Clear any pending errors.
619 void* self = dlopen(NULL, RTLD_NOW);
620 ASSERT_TRUE(self != NULL);
621 ASSERT_TRUE(dlerror() == NULL);
623 void* sym = dlsym(self, "DlSymTestFunction");
624 ASSERT_TRUE(sym != NULL);
626 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
627 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
629 Dl_info info;
630 int rc = dladdr(addr, &info);
631 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
633 // Get the name of this executable.
634 char executable_path[PATH_MAX];
635 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
636 ASSERT_NE(rc, -1);
637 executable_path[rc] = '\0';
638 std::string executable_name(basename(executable_path));
640 // The filename should be that of this executable.
641 // Note that we don't know whether or not we have the full path, so we want an "ends_with" test.
642 std::string dli_fname(info.dli_fname);
643 dli_fname = basename(&dli_fname[0]);
644 ASSERT_EQ(dli_fname, executable_name);
646 // The symbol name should be the symbol we looked up.
647 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
649 // The address should be the exact address of the symbol.
650 ASSERT_EQ(info.dli_saddr, sym);
652 // Look in /proc/pid/maps to find out what address we were loaded at.
653 // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic.
654 void* base_address = NULL;
655 char line[BUFSIZ];
656 FILE* fp = fopen("/proc/self/maps", "r");
657 ASSERT_TRUE(fp != NULL);
658 while (fgets(line, sizeof(line), fp) != NULL) {
659 uintptr_t start = strtoul(line, 0, 16);
660 line[strlen(line) - 1] = '\0'; // Chomp the '\n'.
661 char* path = strchr(line, '/');
662 if (path != NULL && strcmp(executable_path, path) == 0) {
663 base_address = reinterpret_cast<void*>(start);
664 break;
665 }
666 }
667 fclose(fp);
669 // The base address should be the address we were loaded at.
670 ASSERT_EQ(info.dli_fbase, base_address);
672 ASSERT_EQ(0, dlclose(self));
673 }
675 TEST(dlfcn, dladdr_invalid) {
676 Dl_info info;
678 dlerror(); // Clear any pending errors.
680 // No symbol corresponding to NULL.
681 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
682 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
684 // No symbol corresponding to a stack address.
685 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
686 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
687 }
689 // GNU-style ELF hash tables are incompatible with the MIPS ABI.
690 // MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code.
691 TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
692 #if !defined(__mips__)
693 dlerror(); // Clear any pending errors.
694 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
695 ASSERT_TRUE(handle != nullptr) << dlerror();
696 auto guard = make_scope_guard([&]() {
697 dlclose(handle);
698 });
699 void* sym = dlsym(handle, "getRandomNumber");
700 ASSERT_TRUE(sym != nullptr) << dlerror();
701 int (*fn)(void);
702 fn = reinterpret_cast<int (*)(void)>(sym);
703 EXPECT_EQ(4, fn());
705 Dl_info dlinfo;
706 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
708 ASSERT_TRUE(fn == dlinfo.dli_saddr);
709 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
710 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
711 #else
712 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
713 #endif
714 }
716 TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
717 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
718 ASSERT_TRUE(handle != nullptr) << dlerror();
719 auto guard = make_scope_guard([&]() {
720 dlclose(handle);
721 });
722 void* sym = dlsym(handle, "getRandomNumber");
723 ASSERT_TRUE(sym != nullptr) << dlerror();
724 int (*fn)(void);
725 fn = reinterpret_cast<int (*)(void)>(sym);
726 EXPECT_EQ(4, fn());
728 Dl_info dlinfo;
729 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
731 ASSERT_TRUE(fn == dlinfo.dli_saddr);
732 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
733 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
734 }
736 TEST(dlfcn, dlopen_bad_flags) {
737 dlerror(); // Clear any pending errors.
738 void* handle;
740 #if defined(__GLIBC__)
741 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
742 handle = dlopen(NULL, 0);
743 ASSERT_TRUE(handle == NULL);
744 ASSERT_SUBSTR("invalid", dlerror());
745 #endif
747 handle = dlopen(NULL, 0xffffffff);
748 ASSERT_TRUE(handle == NULL);
749 ASSERT_SUBSTR("invalid", dlerror());
751 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
752 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY);
753 ASSERT_TRUE(handle != NULL);
754 ASSERT_SUBSTR(NULL, dlerror());
755 }
757 TEST(dlfcn, rtld_default_unknown_symbol) {
758 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
759 ASSERT_TRUE(addr == NULL);
760 }
762 TEST(dlfcn, rtld_default_known_symbol) {
763 void* addr = dlsym(RTLD_DEFAULT, "fopen");
764 ASSERT_TRUE(addr != NULL);
765 }
767 TEST(dlfcn, rtld_next_unknown_symbol) {
768 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
769 ASSERT_TRUE(addr == NULL);
770 }
772 TEST(dlfcn, rtld_next_known_symbol) {
773 void* addr = dlsym(RTLD_NEXT, "fopen");
774 ASSERT_TRUE(addr != NULL);
775 }
777 TEST(dlfcn, dlsym_weak_func) {
778 dlerror();
779 void* handle = dlopen("libtest_dlsym_weak_func.so",RTLD_NOW);
780 ASSERT_TRUE(handle != NULL);
782 int (*weak_func)();
783 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
784 ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror();
785 EXPECT_EQ(42, weak_func());
786 dlclose(handle);
787 }
789 TEST(dlfcn, dlopen_symlink) {
790 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
791 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
792 ASSERT_TRUE(handle1 != NULL);
793 ASSERT_TRUE(handle2 != NULL);
794 ASSERT_EQ(handle1, handle2);
795 dlclose(handle1);
796 dlclose(handle2);
797 }