dlfcn_test.cpp revision ef1306d77718cc74a8df5673a15649dea317571d
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 for now 93#if 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 != NULL) << dlerror(); 128 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called")); 129 ASSERT_TRUE(is_ctor_called != NULL) << dlerror(); 130 ASSERT_STREQ("true", is_ctor_called()); 131 dlclose(handle); 132} 133#endif 134 135TEST(dlfcn, dlopen_check_relocation_dt_needed_order) { 136 // This is the structure of the test library and 137 // its dt_needed libraries 138 // libtest_relo_check_dt_needed_order.so 139 // | 140 // +-> libtest_relo_check_dt_needed_order_1.so 141 // | 142 // +-> libtest_relo_check_dt_needed_order_2.so 143 // 144 // The root library references relo_test_get_answer_lib - which is defined 145 // in both dt_needed libraries, the correct relocation should 146 // use the function defined in libtest_relo_check_dt_needed_order_1.so 147 void* handle = nullptr; 148 auto guard = make_scope_guard([&]() { 149 dlclose(handle); 150 }); 151 152 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW); 153 ASSERT_TRUE(handle != nullptr) << dlerror(); 154 155 typedef int (*fn_t) (void); 156 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer")); 157 ASSERT_TRUE(fn != nullptr) << dlerror(); 158 ASSERT_EQ(1, fn()); 159} 160 161TEST(dlfcn, dlopen_check_order) { 162 // Here is how the test library and its dt_needed 163 // libraries are arranged 164 // 165 // libtest_check_order.so 166 // | 167 // +-> libtest_check_order_1_left.so 168 // | | 169 // | +-> libtest_check_order_a.so 170 // | | 171 // | +-> libtest_check_order_b.so 172 // | 173 // +-> libtest_check_order_2_right.so 174 // | | 175 // | +-> libtest_check_order_d.so 176 // | | 177 // | +-> libtest_check_order_b.so 178 // | 179 // +-> libtest_check_order_3_c.so 180 // 181 // load order should be (1, 2, 3, a, b, d) 182 // 183 // get_answer() is defined in (2, 3, a, b, c) 184 // get_answer2() is defined in (b, d) 185 void* sym = dlsym(RTLD_DEFAULT, "dlopen_test_get_answer"); 186 ASSERT_TRUE(sym == nullptr); 187 void* handle = dlopen("libtest_check_order.so", RTLD_NOW); 188 ASSERT_TRUE(handle != nullptr); 189 typedef int (*fn_t) (void); 190 fn_t fn, fn2; 191 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "dlopen_test_get_answer")); 192 ASSERT_TRUE(fn != NULL); 193 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "dlopen_test_get_answer2")); 194 ASSERT_TRUE(fn2 != NULL); 195 196 ASSERT_EQ(42, fn()); 197 ASSERT_EQ(43, fn2()); 198 dlclose(handle); 199} 200 201// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so -> 202// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so -> 203// libtest_with_dependency_loop_a.so 204TEST(dlfcn, dlopen_check_loop) { 205 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW); 206 ASSERT_TRUE(handle == NULL); 207 ASSERT_STREQ("dlopen failed: recursive link to \"libtest_with_dependency_loop_a.so\"", dlerror()); 208} 209 210TEST(dlfcn, dlopen_failure) { 211 void* self = dlopen("/does/not/exist", RTLD_NOW); 212 ASSERT_TRUE(self == NULL); 213#if defined(__BIONIC__) 214 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror()); 215#else 216 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror()); 217#endif 218} 219 220static void* ConcurrentDlErrorFn(void*) { 221 dlopen("/child/thread", RTLD_NOW); 222 return reinterpret_cast<void*>(strdup(dlerror())); 223} 224 225TEST(dlfcn, dlerror_concurrent) { 226 dlopen("/main/thread", RTLD_NOW); 227 const char* main_thread_error = dlerror(); 228 ASSERT_SUBSTR("/main/thread", main_thread_error); 229 230 pthread_t t; 231 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL)); 232 void* result; 233 ASSERT_EQ(0, pthread_join(t, &result)); 234 char* child_thread_error = static_cast<char*>(result); 235 ASSERT_SUBSTR("/child/thread", child_thread_error); 236 free(child_thread_error); 237 238 ASSERT_SUBSTR("/main/thread", main_thread_error); 239} 240 241TEST(dlfcn, dlsym_failures) { 242 dlerror(); // Clear any pending errors. 243 void* self = dlopen(NULL, RTLD_NOW); 244 ASSERT_TRUE(self != NULL); 245 ASSERT_TRUE(dlerror() == NULL); 246 247 void* sym; 248 249#if defined(__BIONIC__) && !defined(__LP64__) 250 // RTLD_DEFAULT in lp32 bionic is not (void*)0 251 // so it can be distinguished from the NULL handle. 252 sym = dlsym(NULL, "test"); 253 ASSERT_TRUE(sym == NULL); 254 ASSERT_SUBSTR("dlsym library handle is null", dlerror()); 255#endif 256 257 // NULL symbol name. 258#if defined(__BIONIC__) 259 // glibc marks this parameter non-null and SEGVs if you cheat. 260 sym = dlsym(self, NULL); 261 ASSERT_TRUE(sym == NULL); 262 ASSERT_SUBSTR("", dlerror()); 263#endif 264 265 // Symbol that doesn't exist. 266 sym = dlsym(self, "ThisSymbolDoesNotExist"); 267 ASSERT_TRUE(sym == NULL); 268 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror()); 269 270 ASSERT_EQ(0, dlclose(self)); 271} 272 273TEST(dlfcn, dladdr) { 274 dlerror(); // Clear any pending errors. 275 void* self = dlopen(NULL, RTLD_NOW); 276 ASSERT_TRUE(self != NULL); 277 ASSERT_TRUE(dlerror() == NULL); 278 279 void* sym = dlsym(self, "DlSymTestFunction"); 280 ASSERT_TRUE(sym != NULL); 281 282 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address. 283 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2); 284 285 Dl_info info; 286 int rc = dladdr(addr, &info); 287 ASSERT_NE(rc, 0); // Zero on error, non-zero on success. 288 289 // Get the name of this executable. 290 char executable_path[PATH_MAX]; 291 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path)); 292 ASSERT_NE(rc, -1); 293 executable_path[rc] = '\0'; 294 std::string executable_name(basename(executable_path)); 295 296 // The filename should be that of this executable. 297 // Note that we don't know whether or not we have the full path, so we want an "ends_with" test. 298 std::string dli_fname(info.dli_fname); 299 dli_fname = basename(&dli_fname[0]); 300 ASSERT_EQ(dli_fname, executable_name); 301 302 // The symbol name should be the symbol we looked up. 303 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction"); 304 305 // The address should be the exact address of the symbol. 306 ASSERT_EQ(info.dli_saddr, sym); 307 308 // Look in /proc/pid/maps to find out what address we were loaded at. 309 // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic. 310 void* base_address = NULL; 311 char line[BUFSIZ]; 312 FILE* fp = fopen("/proc/self/maps", "r"); 313 ASSERT_TRUE(fp != NULL); 314 while (fgets(line, sizeof(line), fp) != NULL) { 315 uintptr_t start = strtoul(line, 0, 16); 316 line[strlen(line) - 1] = '\0'; // Chomp the '\n'. 317 char* path = strchr(line, '/'); 318 if (path != NULL && strcmp(executable_path, path) == 0) { 319 base_address = reinterpret_cast<void*>(start); 320 break; 321 } 322 } 323 fclose(fp); 324 325 // The base address should be the address we were loaded at. 326 ASSERT_EQ(info.dli_fbase, base_address); 327 328 ASSERT_EQ(0, dlclose(self)); 329} 330 331TEST(dlfcn, dladdr_invalid) { 332 Dl_info info; 333 334 dlerror(); // Clear any pending errors. 335 336 // No symbol corresponding to NULL. 337 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success. 338 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3). 339 340 // No symbol corresponding to a stack address. 341 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success. 342 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3). 343} 344 345// Our dynamic linker doesn't support GNU hash tables. 346#if defined(__BIONIC__) 347// GNU-style ELF hash tables are incompatible with the MIPS ABI. 348// MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code. 349#if !defined(__mips__) 350TEST(dlfcn, dlopen_library_with_only_gnu_hash) { 351 dlerror(); // Clear any pending errors. 352 void* handle = dlopen("no-elf-hash-table-library.so", RTLD_NOW); 353 ASSERT_TRUE(handle == NULL); 354 ASSERT_STREQ("dlopen failed: empty/missing DT_HASH in \"no-elf-hash-table-library.so\" (built with --hash-style=gnu?)", dlerror()); 355} 356#endif 357#endif 358 359TEST(dlfcn, dlopen_bad_flags) { 360 dlerror(); // Clear any pending errors. 361 void* handle; 362 363#if defined(__GLIBC__) 364 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags. 365 handle = dlopen(NULL, 0); 366 ASSERT_TRUE(handle == NULL); 367 ASSERT_SUBSTR("invalid", dlerror()); 368#endif 369 370 handle = dlopen(NULL, 0xffffffff); 371 ASSERT_TRUE(handle == NULL); 372 ASSERT_SUBSTR("invalid", dlerror()); 373 374 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we. 375 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY); 376 ASSERT_TRUE(handle != NULL); 377 ASSERT_SUBSTR(NULL, dlerror()); 378} 379 380TEST(dlfcn, rtld_default_unknown_symbol) { 381 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME"); 382 ASSERT_TRUE(addr == NULL); 383} 384 385TEST(dlfcn, rtld_default_known_symbol) { 386 void* addr = dlsym(RTLD_DEFAULT, "fopen"); 387 ASSERT_TRUE(addr != NULL); 388} 389 390TEST(dlfcn, rtld_next_unknown_symbol) { 391 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME"); 392 ASSERT_TRUE(addr == NULL); 393} 394 395TEST(dlfcn, rtld_next_known_symbol) { 396 void* addr = dlsym(RTLD_NEXT, "fopen"); 397 ASSERT_TRUE(addr != NULL); 398} 399 400TEST(dlfcn, dlsym_weak_func) { 401 dlerror(); 402 void* handle = dlopen("libtest_dlsym_weak_func.so",RTLD_NOW); 403 ASSERT_TRUE(handle != NULL); 404 405 int (*weak_func)(); 406 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func")); 407 ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror(); 408 EXPECT_EQ(42, weak_func()); 409 dlclose(handle); 410} 411 412TEST(dlfcn, dlopen_symlink) { 413 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW); 414 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW); 415 ASSERT_TRUE(handle1 != NULL); 416 ASSERT_TRUE(handle2 != NULL); 417 ASSERT_EQ(handle1, handle2); 418} 419