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 | |
| 35 | TEST(dlopen, 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); |
| 49 | } |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 50 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 51 | TEST(dlopen, dlopen_failure) { |
| 52 | void* self = dlopen("/does/not/exist", RTLD_NOW); |
| 53 | ASSERT_TRUE(self == NULL); |
| 54 | #if __BIONIC__ |
| 55 | ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror()); |
| 56 | #else |
| 57 | ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror()); |
| 58 | #endif |
| 59 | } |
| 60 | |
Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 61 | static void* ConcurrentDlErrorFn(void*) { |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 62 | dlopen("/child/thread", RTLD_NOW); |
| 63 | return reinterpret_cast<void*>(strdup(dlerror())); |
| 64 | } |
| 65 | |
| 66 | TEST(dlopen, dlerror_concurrent) { |
| 67 | dlopen("/main/thread", RTLD_NOW); |
| 68 | const char* main_thread_error = dlerror(); |
| 69 | ASSERT_SUBSTR("/main/thread", main_thread_error); |
| 70 | |
| 71 | pthread_t t; |
| 72 | ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL)); |
| 73 | void* result; |
| 74 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 75 | char* child_thread_error = static_cast<char*>(result); |
| 76 | ASSERT_SUBSTR("/child/thread", child_thread_error); |
| 77 | free(child_thread_error); |
| 78 | |
| 79 | ASSERT_SUBSTR("/main/thread", main_thread_error); |
| 80 | } |
| 81 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 82 | TEST(dlopen, dlsym_failures) { |
| 83 | dlerror(); // Clear any pending errors. |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 84 | void* self = dlopen(NULL, RTLD_NOW); |
| 85 | ASSERT_TRUE(self != NULL); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 86 | ASSERT_TRUE(dlerror() == NULL); |
| 87 | |
| 88 | void* sym; |
| 89 | |
| 90 | // NULL handle. |
| 91 | sym = dlsym(NULL, "test"); |
| 92 | ASSERT_TRUE(sym == NULL); |
| 93 | #if __BIONIC__ |
| 94 | ASSERT_SUBSTR("dlsym library handle is null", dlerror()); |
| 95 | #else |
| 96 | ASSERT_SUBSTR("undefined symbol: test", dlerror()); // glibc isn't specific about the failure. |
| 97 | #endif |
| 98 | |
| 99 | // NULL symbol name. |
| 100 | #if __BIONIC__ |
| 101 | // glibc marks this parameter non-null and SEGVs if you cheat. |
| 102 | sym = dlsym(self, NULL); |
| 103 | ASSERT_TRUE(sym == NULL); |
| 104 | ASSERT_SUBSTR("", dlerror()); |
| 105 | #endif |
| 106 | |
| 107 | // Symbol that doesn't exist. |
| 108 | sym = dlsym(self, "ThisSymbolDoesNotExist"); |
| 109 | ASSERT_TRUE(sym == NULL); |
| 110 | ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror()); |
| 111 | } |
| 112 | |
| 113 | TEST(dlopen, dladdr) { |
| 114 | dlerror(); // Clear any pending errors. |
| 115 | void* self = dlopen(NULL, RTLD_NOW); |
| 116 | ASSERT_TRUE(self != NULL); |
| 117 | ASSERT_TRUE(dlerror() == NULL); |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 118 | |
| 119 | void* sym = dlsym(self, "DlSymTestFunction"); |
| 120 | ASSERT_TRUE(sym != NULL); |
| 121 | |
| 122 | // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address. |
| 123 | void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2); |
| 124 | |
| 125 | Dl_info info; |
| 126 | int rc = dladdr(addr, &info); |
| 127 | ASSERT_NE(rc, 0); // Zero on error, non-zero on success. |
| 128 | |
| 129 | // Get the name of this executable. |
| 130 | char executable_path[PATH_MAX]; |
| 131 | rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path)); |
| 132 | ASSERT_NE(rc, -1); |
| 133 | executable_path[rc] = '\0'; |
| 134 | std::string executable_name(basename(executable_path)); |
| 135 | |
| 136 | // The filename should be that of this executable. |
| 137 | // Note that we don't know whether or not we have the full path, so we want an "ends_with" test. |
| 138 | std::string dli_fname(info.dli_fname); |
| 139 | dli_fname = basename(&dli_fname[0]); |
| 140 | ASSERT_EQ(dli_fname, executable_name); |
| 141 | |
| 142 | // The symbol name should be the symbol we looked up. |
| 143 | ASSERT_STREQ(info.dli_sname, "DlSymTestFunction"); |
| 144 | |
| 145 | // The address should be the exact address of the symbol. |
| 146 | ASSERT_EQ(info.dli_saddr, sym); |
| 147 | |
| 148 | // Look in /proc/pid/maps to find out what address we were loaded at. |
| 149 | // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic. |
| 150 | void* base_address = NULL; |
| 151 | char path[PATH_MAX]; |
| 152 | snprintf(path, sizeof(path), "/proc/%d/maps", getpid()); |
| 153 | char line[BUFSIZ]; |
| 154 | FILE* fp = fopen(path, "r"); |
| 155 | ASSERT_TRUE(fp != NULL); |
| 156 | while (fgets(line, sizeof(line), fp) != NULL) { |
| 157 | uintptr_t start = strtoul(line, 0, 16); |
| 158 | line[strlen(line) - 1] = '\0'; // Chomp the '\n'. |
| 159 | char* path = strchr(line, '/'); |
Elliott Hughes | 156da96 | 2012-10-09 17:14:56 -0700 | [diff] [blame] | 160 | if (path != NULL && strcmp(executable_path, path) == 0) { |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 161 | base_address = reinterpret_cast<void*>(start); |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | fclose(fp); |
| 166 | |
| 167 | // The base address should be the address we were loaded at. |
| 168 | ASSERT_EQ(info.dli_fbase, base_address); |
| 169 | } |
| 170 | |
| 171 | TEST(dlopen, dladdr_invalid) { |
| 172 | Dl_info info; |
| 173 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 174 | dlerror(); // Clear any pending errors. |
| 175 | |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 176 | // No symbol corresponding to NULL. |
| 177 | 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] | 178 | ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3). |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 179 | |
| 180 | // No symbol corresponding to a stack address. |
| 181 | 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] | 182 | ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3). |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 183 | } |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 184 | |
| 185 | #if __BIONIC__ |
| 186 | // Our dynamic linker doesn't support GNU hash tables. |
| 187 | TEST(dlopen, library_with_only_gnu_hash) { |
| 188 | dlerror(); // Clear any pending errors. |
| 189 | void* handle = dlopen("empty-library.so", RTLD_NOW); |
| 190 | ASSERT_TRUE(handle == NULL); |
| 191 | ASSERT_STREQ("dlopen failed: empty/missing DT_HASH in \"empty-library.so\" (built with --hash-style=gnu?)", dlerror()); |
| 192 | } |
| 193 | #endif |