dlfcn_test.cpp revision ce44166c737b6737c3a0820bef10b074a5e36cf8
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 <string> 26 27#define ASSERT_SUBSTR(needle, haystack) \ 28 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack) 29 30static bool g_called = false; 31extern "C" void DlSymTestFunction() { 32 g_called = true; 33} 34 35TEST(dlfcn, dlsym_in_self) { 36 dlerror(); // Clear any pending errors. 37 void* self = dlopen(NULL, RTLD_NOW); 38 ASSERT_TRUE(self != NULL); 39 ASSERT_TRUE(dlerror() == NULL); 40 41 void* sym = dlsym(self, "DlSymTestFunction"); 42 ASSERT_TRUE(sym != NULL); 43 44 void (*function)() = reinterpret_cast<void(*)()>(sym); 45 46 g_called = false; 47 function(); 48 ASSERT_TRUE(g_called); 49 50 ASSERT_EQ(0, dlclose(self)); 51} 52 53TEST(dlfcn, dlopen_noload) { 54 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD); 55 ASSERT_TRUE(handle == NULL); 56 handle = dlopen("libtest_simple.so", RTLD_NOW); 57 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD); 58 ASSERT_TRUE(handle != NULL); 59 ASSERT_TRUE(handle2 != NULL); 60 ASSERT_TRUE(handle == handle2); 61 ASSERT_EQ(0, dlclose(handle)); 62 ASSERT_EQ(0, dlclose(handle2)); 63} 64 65TEST(dlfcn, dlopen_failure) { 66 void* self = dlopen("/does/not/exist", RTLD_NOW); 67 ASSERT_TRUE(self == NULL); 68#if defined(__BIONIC__) 69 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror()); 70#else 71 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror()); 72#endif 73} 74 75static void* ConcurrentDlErrorFn(void*) { 76 dlopen("/child/thread", RTLD_NOW); 77 return reinterpret_cast<void*>(strdup(dlerror())); 78} 79 80TEST(dlfcn, dlerror_concurrent) { 81 dlopen("/main/thread", RTLD_NOW); 82 const char* main_thread_error = dlerror(); 83 ASSERT_SUBSTR("/main/thread", main_thread_error); 84 85 pthread_t t; 86 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL)); 87 void* result; 88 ASSERT_EQ(0, pthread_join(t, &result)); 89 char* child_thread_error = static_cast<char*>(result); 90 ASSERT_SUBSTR("/child/thread", child_thread_error); 91 free(child_thread_error); 92 93 ASSERT_SUBSTR("/main/thread", main_thread_error); 94} 95 96TEST(dlfcn, dlsym_failures) { 97 dlerror(); // Clear any pending errors. 98 void* self = dlopen(NULL, RTLD_NOW); 99 ASSERT_TRUE(self != NULL); 100 ASSERT_TRUE(dlerror() == NULL); 101 102 void* sym; 103 104#if defined(__BIONIC__) && !defined(__LP64__) 105 // RTLD_DEFAULT in lp32 bionic is not (void*)0 106 // so it can be distinguished from the NULL handle. 107 sym = dlsym(NULL, "test"); 108 ASSERT_TRUE(sym == NULL); 109 ASSERT_SUBSTR("dlsym library handle is null", dlerror()); 110#endif 111 112 // NULL symbol name. 113#if defined(__BIONIC__) 114 // glibc marks this parameter non-null and SEGVs if you cheat. 115 sym = dlsym(self, NULL); 116 ASSERT_TRUE(sym == NULL); 117 ASSERT_SUBSTR("", dlerror()); 118#endif 119 120 // Symbol that doesn't exist. 121 sym = dlsym(self, "ThisSymbolDoesNotExist"); 122 ASSERT_TRUE(sym == NULL); 123 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror()); 124 125 ASSERT_EQ(0, dlclose(self)); 126} 127 128TEST(dlfcn, dladdr) { 129 dlerror(); // Clear any pending errors. 130 void* self = dlopen(NULL, RTLD_NOW); 131 ASSERT_TRUE(self != NULL); 132 ASSERT_TRUE(dlerror() == NULL); 133 134 void* sym = dlsym(self, "DlSymTestFunction"); 135 ASSERT_TRUE(sym != NULL); 136 137 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address. 138 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2); 139 140 Dl_info info; 141 int rc = dladdr(addr, &info); 142 ASSERT_NE(rc, 0); // Zero on error, non-zero on success. 143 144 // Get the name of this executable. 145 char executable_path[PATH_MAX]; 146 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path)); 147 ASSERT_NE(rc, -1); 148 executable_path[rc] = '\0'; 149 std::string executable_name(basename(executable_path)); 150 151 // The filename should be that of this executable. 152 // Note that we don't know whether or not we have the full path, so we want an "ends_with" test. 153 std::string dli_fname(info.dli_fname); 154 dli_fname = basename(&dli_fname[0]); 155 ASSERT_EQ(dli_fname, executable_name); 156 157 // The symbol name should be the symbol we looked up. 158 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction"); 159 160 // The address should be the exact address of the symbol. 161 ASSERT_EQ(info.dli_saddr, sym); 162 163 // Look in /proc/pid/maps to find out what address we were loaded at. 164 // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic. 165 void* base_address = NULL; 166 char path[PATH_MAX]; 167 snprintf(path, sizeof(path), "/proc/%d/maps", getpid()); 168 char line[BUFSIZ]; 169 FILE* fp = fopen(path, "r"); 170 ASSERT_TRUE(fp != NULL); 171 while (fgets(line, sizeof(line), fp) != NULL) { 172 uintptr_t start = strtoul(line, 0, 16); 173 line[strlen(line) - 1] = '\0'; // Chomp the '\n'. 174 char* path = strchr(line, '/'); 175 if (path != NULL && strcmp(executable_path, path) == 0) { 176 base_address = reinterpret_cast<void*>(start); 177 break; 178 } 179 } 180 fclose(fp); 181 182 // The base address should be the address we were loaded at. 183 ASSERT_EQ(info.dli_fbase, base_address); 184 185 ASSERT_EQ(0, dlclose(self)); 186} 187 188TEST(dlfcn, dladdr_invalid) { 189 Dl_info info; 190 191 dlerror(); // Clear any pending errors. 192 193 // No symbol corresponding to NULL. 194 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success. 195 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3). 196 197 // No symbol corresponding to a stack address. 198 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success. 199 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3). 200} 201 202// Our dynamic linker doesn't support GNU hash tables. 203#if defined(__BIONIC__) 204// GNU-style ELF hash tables are incompatible with the MIPS ABI. 205// MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code. 206#if !defined(__mips__) 207TEST(dlfcn, dlopen_library_with_only_gnu_hash) { 208 dlerror(); // Clear any pending errors. 209 void* handle = dlopen("no-elf-hash-table-library.so", RTLD_NOW); 210 ASSERT_TRUE(handle == NULL); 211 ASSERT_STREQ("dlopen failed: empty/missing DT_HASH in \"no-elf-hash-table-library.so\" (built with --hash-style=gnu?)", dlerror()); 212} 213#endif 214#endif 215 216TEST(dlfcn, dlopen_bad_flags) { 217 dlerror(); // Clear any pending errors. 218 void* handle; 219 220#if defined(__GLIBC__) 221 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags. 222 handle = dlopen(NULL, 0); 223 ASSERT_TRUE(handle == NULL); 224 ASSERT_SUBSTR("invalid", dlerror()); 225#endif 226 227 handle = dlopen(NULL, 0xffffffff); 228 ASSERT_TRUE(handle == NULL); 229 ASSERT_SUBSTR("invalid", dlerror()); 230 231 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we. 232 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY); 233 ASSERT_TRUE(handle != NULL); 234 ASSERT_SUBSTR(NULL, dlerror()); 235} 236 237TEST(dlfcn, rtld_default_unknown_symbol) { 238 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME"); 239 ASSERT_TRUE(addr == NULL); 240} 241 242TEST(dlfcn, rtld_default_known_symbol) { 243 void* addr = dlsym(RTLD_DEFAULT, "fopen"); 244 ASSERT_TRUE(addr != NULL); 245} 246 247TEST(dlfcn, rtld_next_unknown_symbol) { 248 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME"); 249 ASSERT_TRUE(addr == NULL); 250} 251 252TEST(dlfcn, rtld_next_known_symbol) { 253 void* addr = dlsym(RTLD_NEXT, "fopen"); 254 ASSERT_TRUE(addr != NULL); 255} 256 257TEST(dlfcn, dlsym_weak_func) { 258 dlerror(); 259 void* handle = dlopen("libtest_dlsym_weak_func.so",RTLD_NOW); 260 ASSERT_TRUE(handle != NULL); 261 262 int (*weak_func)(); 263 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func")); 264 ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror(); 265 EXPECT_EQ(42, weak_func()); 266 dlclose(handle); 267} 268 269TEST(dlfcn, dlopen_symlink) { 270 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW); 271 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW); 272 ASSERT_TRUE(handle1 != NULL); 273 ASSERT_TRUE(handle2 != NULL); 274 ASSERT_EQ(handle1, handle2); 275} 276