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