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