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