dlfcn_test.cpp revision 7ca96a075b778f1fa2ad265350879238cbcb4d09
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 */ 16 17#include <gtest/gtest.h> 18 19#include <dlfcn.h> 20#include <libgen.h> 21#include <limits.h> 22#include <stdio.h> 23#include <stdint.h> 24 25#include "private/ScopeGuard.h" 26 27#include <string> 28 29#define ASSERT_SUBSTR(needle, haystack) \ 30 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack) 31 32static bool g_called = false; 33extern "C" void DlSymTestFunction() { 34 g_called = true; 35} 36 37static int g_ctor_function_called = 0; 38 39extern "C" void ctor_function() __attribute__ ((constructor)); 40 41extern "C" void ctor_function() { 42 g_ctor_function_called = 17; 43} 44 45TEST(dlfcn, ctor_function_call) { 46 ASSERT_EQ(17, g_ctor_function_called); 47} 48 49TEST(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); 54 55 void* sym = dlsym(self, "DlSymTestFunction"); 56 ASSERT_TRUE(sym != NULL); 57 58 void (*function)() = reinterpret_cast<void(*)()>(sym); 59 60 g_called = false; 61 function(); 62 ASSERT_TRUE(g_called); 63 64 ASSERT_EQ(0, dlclose(self)); 65} 66 67TEST(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} 79 80TEST(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} 91 92// ifuncs are only supported on intel and arm64 for now 93#if defined (__aarch64__) || defined(__i386__) || defined(__x86_64__) 94TEST(dlfcn, ifunc) { 95 typedef const char* (*fn_ptr)(); 96 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); 109 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} 122 123TEST(dlfcn, ifunc_ctor_call) { 124 typedef const char* (*fn_ptr)(); 125 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()); 131 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 138 139TEST(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 }); 155 156 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW); 157 ASSERT_TRUE(handle != nullptr) << dlerror(); 158 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} 164 165TEST(dlfcn, dlopen_check_order) { 166 // Here is how the test library and its dt_needed 167 // libraries are arranged 168 // 169 // libtest_check_order.so 170 // | 171 // +-> libtest_check_order_1_left.so 172 // | | 173 // | +-> libtest_check_order_a.so 174 // | | 175 // | +-> libtest_check_order_b.so 176 // | 177 // +-> libtest_check_order_2_right.so 178 // | | 179 // | +-> libtest_check_order_d.so 180 // | | 181 // | +-> libtest_check_order_b.so 182 // | 183 // +-> libtest_check_order_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, "dlopen_test_get_answer"); 190 ASSERT_TRUE(sym == nullptr); 191 void* handle = dlopen("libtest_check_order.so", RTLD_NOW | RTLD_GLOBAL); 192 ASSERT_TRUE(handle != nullptr); 193 typedef int (*fn_t) (void); 194 fn_t fn, fn2; 195 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "dlopen_test_get_answer")); 196 ASSERT_TRUE(fn != NULL) << dlerror(); 197 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "dlopen_test_get_answer2")); 198 ASSERT_TRUE(fn2 != NULL) << dlerror(); 199 200 ASSERT_EQ(42, fn()); 201 ASSERT_EQ(43, fn2()); 202 dlclose(handle); 203} 204 205TEST(dlfcn, dlopen_check_rtld_local) { 206 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); 207 ASSERT_TRUE(sym == nullptr); 208 209 // implicit RTLD_LOCAL 210 void* handle = dlopen("libtest_simple.so", RTLD_NOW); 211 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); 212 ASSERT_TRUE(sym == nullptr); 213 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror()); 214 sym = dlsym(handle, "dlopen_testlib_simple_func"); 215 ASSERT_TRUE(sym != nullptr); 216 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)()); 217 dlclose(handle); 218 219 // explicit RTLD_LOCAL 220 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL); 221 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); 222 ASSERT_TRUE(sym == nullptr); 223 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror()); 224 sym = dlsym(handle, "dlopen_testlib_simple_func"); 225 ASSERT_TRUE(sym != nullptr); 226 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)()); 227 dlclose(handle); 228} 229 230TEST(dlfcn, dlopen_check_rtld_global) { 231 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); 232 ASSERT_TRUE(sym == nullptr); 233 234 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL); 235 ASSERT_TRUE(handle != nullptr) << dlerror(); 236 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); 237 ASSERT_TRUE(sym != nullptr) << dlerror(); 238 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)()); 239 dlclose(handle); 240 241 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that 242 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); 243 ASSERT_EQ(sym, sym_after_dlclose); 244} 245 246// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so -> 247// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so -> 248// libtest_with_dependency_loop_a.so 249TEST(dlfcn, dlopen_check_loop) { 250 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW); 251#if defined(__BIONIC__) 252 ASSERT_TRUE(handle == nullptr); 253 ASSERT_STREQ("dlopen failed: recursive link to \"libtest_with_dependency_loop_a.so\"", dlerror()); 254 // This symbol should never be exposed 255 void* f = dlsym(RTLD_DEFAULT, "dlopen_test_invalid_function"); 256 ASSERT_TRUE(f == nullptr); 257 ASSERT_SUBSTR("undefined symbol: dlopen_test_invalid_function", dlerror()); 258 259 // dlopen second time to make sure that the library wasn't loaded even though dlopen returned null. 260 // This may happen if during cleanup the root library or one of the depended libs were not removed 261 // from soinfo list. 262 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD); 263 ASSERT_TRUE(handle == nullptr); 264 ASSERT_STREQ("dlopen failed: library \"libtest_with_dependency_loop.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); 265#else // glibc allows recursive links 266 ASSERT_TRUE(handle != nullptr); 267 dlclose(handle); 268#endif 269} 270 271TEST(dlfcn, dlopen_nodelete) { 272 static bool is_unloaded = false; 273 274 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE); 275 ASSERT_TRUE(handle != nullptr) << dlerror(); 276 void (*set_unload_flag_ptr)(bool*); 277 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr")); 278 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror(); 279 set_unload_flag_ptr(&is_unloaded); 280 281 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number")); 282 ASSERT_TRUE(taxicab_number != nullptr) << dlerror(); 283 ASSERT_EQ(1729U, *taxicab_number); 284 *taxicab_number = 2; 285 286 dlclose(handle); 287 ASSERT_TRUE(!is_unloaded); 288 289 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number")); 290 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number); 291 ASSERT_EQ(2U, *taxicab_number_after_dlclose); 292 293 294 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW); 295 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number")); 296 ASSERT_EQ(taxicab_number2, taxicab_number); 297 298 ASSERT_EQ(2U, *taxicab_number2); 299 300 dlclose(handle); 301 ASSERT_TRUE(!is_unloaded); 302} 303 304TEST(dlfcn, dlopen_nodelete_on_second_dlopen) { 305 static bool is_unloaded = false; 306 307 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW); 308 ASSERT_TRUE(handle != nullptr) << dlerror(); 309 void (*set_unload_flag_ptr)(bool*); 310 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr")); 311 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror(); 312 set_unload_flag_ptr(&is_unloaded); 313 314 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number")); 315 ASSERT_TRUE(taxicab_number != nullptr) << dlerror(); 316 317 ASSERT_EQ(1729U, *taxicab_number); 318 *taxicab_number = 2; 319 320 // This RTLD_NODELETE should be ignored 321 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE); 322 ASSERT_TRUE(handle1 != nullptr) << dlerror(); 323 ASSERT_EQ(handle, handle1); 324 325 dlclose(handle1); 326 dlclose(handle); 327 328 ASSERT_TRUE(is_unloaded); 329} 330 331TEST(dlfcn, dlopen_nodelete_dt_flags_1) { 332 static bool is_unloaded = false; 333 334 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW); 335 ASSERT_TRUE(handle != nullptr) << dlerror(); 336 void (*set_unload_flag_ptr)(bool*); 337 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr")); 338 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror(); 339 set_unload_flag_ptr(&is_unloaded); 340 341 dlclose(handle); 342 ASSERT_TRUE(!is_unloaded); 343} 344 345 346TEST(dlfcn, dlopen_failure) { 347 void* self = dlopen("/does/not/exist", RTLD_NOW); 348 ASSERT_TRUE(self == NULL); 349#if defined(__BIONIC__) 350 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror()); 351#else 352 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror()); 353#endif 354} 355 356static void* ConcurrentDlErrorFn(void*) { 357 dlopen("/child/thread", RTLD_NOW); 358 return reinterpret_cast<void*>(strdup(dlerror())); 359} 360 361TEST(dlfcn, dlerror_concurrent) { 362 dlopen("/main/thread", RTLD_NOW); 363 const char* main_thread_error = dlerror(); 364 ASSERT_SUBSTR("/main/thread", main_thread_error); 365 366 pthread_t t; 367 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL)); 368 void* result; 369 ASSERT_EQ(0, pthread_join(t, &result)); 370 char* child_thread_error = static_cast<char*>(result); 371 ASSERT_SUBSTR("/child/thread", child_thread_error); 372 free(child_thread_error); 373 374 ASSERT_SUBSTR("/main/thread", main_thread_error); 375} 376 377TEST(dlfcn, dlsym_failures) { 378 dlerror(); // Clear any pending errors. 379 void* self = dlopen(NULL, RTLD_NOW); 380 ASSERT_TRUE(self != NULL); 381 ASSERT_TRUE(dlerror() == NULL); 382 383 void* sym; 384 385#if defined(__BIONIC__) && !defined(__LP64__) 386 // RTLD_DEFAULT in lp32 bionic is not (void*)0 387 // so it can be distinguished from the NULL handle. 388 sym = dlsym(NULL, "test"); 389 ASSERT_TRUE(sym == NULL); 390 ASSERT_SUBSTR("dlsym library handle is null", dlerror()); 391#endif 392 393 // NULL symbol name. 394#if defined(__BIONIC__) 395 // glibc marks this parameter non-null and SEGVs if you cheat. 396 sym = dlsym(self, NULL); 397 ASSERT_TRUE(sym == NULL); 398 ASSERT_SUBSTR("", dlerror()); 399#endif 400 401 // Symbol that doesn't exist. 402 sym = dlsym(self, "ThisSymbolDoesNotExist"); 403 ASSERT_TRUE(sym == NULL); 404 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror()); 405 406 ASSERT_EQ(0, dlclose(self)); 407} 408 409TEST(dlfcn, dladdr) { 410 dlerror(); // Clear any pending errors. 411 void* self = dlopen(NULL, RTLD_NOW); 412 ASSERT_TRUE(self != NULL); 413 ASSERT_TRUE(dlerror() == NULL); 414 415 void* sym = dlsym(self, "DlSymTestFunction"); 416 ASSERT_TRUE(sym != NULL); 417 418 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address. 419 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2); 420 421 Dl_info info; 422 int rc = dladdr(addr, &info); 423 ASSERT_NE(rc, 0); // Zero on error, non-zero on success. 424 425 // Get the name of this executable. 426 char executable_path[PATH_MAX]; 427 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path)); 428 ASSERT_NE(rc, -1); 429 executable_path[rc] = '\0'; 430 std::string executable_name(basename(executable_path)); 431 432 // The filename should be that of this executable. 433 // Note that we don't know whether or not we have the full path, so we want an "ends_with" test. 434 std::string dli_fname(info.dli_fname); 435 dli_fname = basename(&dli_fname[0]); 436 ASSERT_EQ(dli_fname, executable_name); 437 438 // The symbol name should be the symbol we looked up. 439 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction"); 440 441 // The address should be the exact address of the symbol. 442 ASSERT_EQ(info.dli_saddr, sym); 443 444 // Look in /proc/pid/maps to find out what address we were loaded at. 445 // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic. 446 void* base_address = NULL; 447 char line[BUFSIZ]; 448 FILE* fp = fopen("/proc/self/maps", "r"); 449 ASSERT_TRUE(fp != NULL); 450 while (fgets(line, sizeof(line), fp) != NULL) { 451 uintptr_t start = strtoul(line, 0, 16); 452 line[strlen(line) - 1] = '\0'; // Chomp the '\n'. 453 char* path = strchr(line, '/'); 454 if (path != NULL && strcmp(executable_path, path) == 0) { 455 base_address = reinterpret_cast<void*>(start); 456 break; 457 } 458 } 459 fclose(fp); 460 461 // The base address should be the address we were loaded at. 462 ASSERT_EQ(info.dli_fbase, base_address); 463 464 ASSERT_EQ(0, dlclose(self)); 465} 466 467TEST(dlfcn, dladdr_invalid) { 468 Dl_info info; 469 470 dlerror(); // Clear any pending errors. 471 472 // No symbol corresponding to NULL. 473 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success. 474 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3). 475 476 // No symbol corresponding to a stack address. 477 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success. 478 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3). 479} 480 481// Our dynamic linker doesn't support GNU hash tables. 482#if defined(__BIONIC__) 483// GNU-style ELF hash tables are incompatible with the MIPS ABI. 484// MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code. 485#if !defined(__mips__) 486TEST(dlfcn, dlopen_library_with_only_gnu_hash) { 487 dlerror(); // Clear any pending errors. 488 void* handle = dlopen("no-elf-hash-table-library.so", RTLD_NOW); 489 ASSERT_TRUE(handle == NULL); 490 ASSERT_STREQ("dlopen failed: empty/missing DT_HASH in \"no-elf-hash-table-library.so\" (built with --hash-style=gnu?)", dlerror()); 491} 492#endif 493#endif 494 495TEST(dlfcn, dlopen_bad_flags) { 496 dlerror(); // Clear any pending errors. 497 void* handle; 498 499#if defined(__GLIBC__) 500 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags. 501 handle = dlopen(NULL, 0); 502 ASSERT_TRUE(handle == NULL); 503 ASSERT_SUBSTR("invalid", dlerror()); 504#endif 505 506 handle = dlopen(NULL, 0xffffffff); 507 ASSERT_TRUE(handle == NULL); 508 ASSERT_SUBSTR("invalid", dlerror()); 509 510 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we. 511 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY); 512 ASSERT_TRUE(handle != NULL); 513 ASSERT_SUBSTR(NULL, dlerror()); 514} 515 516TEST(dlfcn, rtld_default_unknown_symbol) { 517 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME"); 518 ASSERT_TRUE(addr == NULL); 519} 520 521TEST(dlfcn, rtld_default_known_symbol) { 522 void* addr = dlsym(RTLD_DEFAULT, "fopen"); 523 ASSERT_TRUE(addr != NULL); 524} 525 526TEST(dlfcn, rtld_next_unknown_symbol) { 527 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME"); 528 ASSERT_TRUE(addr == NULL); 529} 530 531TEST(dlfcn, rtld_next_known_symbol) { 532 void* addr = dlsym(RTLD_NEXT, "fopen"); 533 ASSERT_TRUE(addr != NULL); 534} 535 536TEST(dlfcn, dlsym_weak_func) { 537 dlerror(); 538 void* handle = dlopen("libtest_dlsym_weak_func.so",RTLD_NOW); 539 ASSERT_TRUE(handle != NULL); 540 541 int (*weak_func)(); 542 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func")); 543 ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror(); 544 EXPECT_EQ(42, weak_func()); 545 dlclose(handle); 546} 547 548TEST(dlfcn, dlopen_symlink) { 549 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW); 550 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW); 551 ASSERT_TRUE(handle1 != NULL); 552 ASSERT_TRUE(handle2 != NULL); 553 ASSERT_EQ(handle1, handle2); 554 dlclose(handle1); 555 dlclose(handle2); 556} 557