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 | |
| 61 | TEST(dlopen, dlsym_failures) { |
| 62 | dlerror(); // Clear any pending errors. |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 63 | void* self = dlopen(NULL, RTLD_NOW); |
| 64 | ASSERT_TRUE(self != NULL); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 65 | ASSERT_TRUE(dlerror() == NULL); |
| 66 | |
| 67 | void* sym; |
| 68 | |
| 69 | // NULL handle. |
| 70 | sym = dlsym(NULL, "test"); |
| 71 | ASSERT_TRUE(sym == NULL); |
| 72 | #if __BIONIC__ |
| 73 | ASSERT_SUBSTR("dlsym library handle is null", dlerror()); |
| 74 | #else |
| 75 | ASSERT_SUBSTR("undefined symbol: test", dlerror()); // glibc isn't specific about the failure. |
| 76 | #endif |
| 77 | |
| 78 | // NULL symbol name. |
| 79 | #if __BIONIC__ |
| 80 | // glibc marks this parameter non-null and SEGVs if you cheat. |
| 81 | sym = dlsym(self, NULL); |
| 82 | ASSERT_TRUE(sym == NULL); |
| 83 | ASSERT_SUBSTR("", dlerror()); |
| 84 | #endif |
| 85 | |
| 86 | // Symbol that doesn't exist. |
| 87 | sym = dlsym(self, "ThisSymbolDoesNotExist"); |
| 88 | ASSERT_TRUE(sym == NULL); |
| 89 | ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror()); |
| 90 | } |
| 91 | |
| 92 | TEST(dlopen, dladdr) { |
| 93 | dlerror(); // Clear any pending errors. |
| 94 | void* self = dlopen(NULL, RTLD_NOW); |
| 95 | ASSERT_TRUE(self != NULL); |
| 96 | ASSERT_TRUE(dlerror() == NULL); |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 97 | |
| 98 | void* sym = dlsym(self, "DlSymTestFunction"); |
| 99 | ASSERT_TRUE(sym != NULL); |
| 100 | |
| 101 | // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address. |
| 102 | void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2); |
| 103 | |
| 104 | Dl_info info; |
| 105 | int rc = dladdr(addr, &info); |
| 106 | ASSERT_NE(rc, 0); // Zero on error, non-zero on success. |
| 107 | |
| 108 | // Get the name of this executable. |
| 109 | char executable_path[PATH_MAX]; |
| 110 | rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path)); |
| 111 | ASSERT_NE(rc, -1); |
| 112 | executable_path[rc] = '\0'; |
| 113 | std::string executable_name(basename(executable_path)); |
| 114 | |
| 115 | // The filename should be that of this executable. |
| 116 | // Note that we don't know whether or not we have the full path, so we want an "ends_with" test. |
| 117 | std::string dli_fname(info.dli_fname); |
| 118 | dli_fname = basename(&dli_fname[0]); |
| 119 | ASSERT_EQ(dli_fname, executable_name); |
| 120 | |
| 121 | // The symbol name should be the symbol we looked up. |
| 122 | ASSERT_STREQ(info.dli_sname, "DlSymTestFunction"); |
| 123 | |
| 124 | // The address should be the exact address of the symbol. |
| 125 | ASSERT_EQ(info.dli_saddr, sym); |
| 126 | |
| 127 | // Look in /proc/pid/maps to find out what address we were loaded at. |
| 128 | // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic. |
| 129 | void* base_address = NULL; |
| 130 | char path[PATH_MAX]; |
| 131 | snprintf(path, sizeof(path), "/proc/%d/maps", getpid()); |
| 132 | char line[BUFSIZ]; |
| 133 | FILE* fp = fopen(path, "r"); |
| 134 | ASSERT_TRUE(fp != NULL); |
| 135 | while (fgets(line, sizeof(line), fp) != NULL) { |
| 136 | uintptr_t start = strtoul(line, 0, 16); |
| 137 | line[strlen(line) - 1] = '\0'; // Chomp the '\n'. |
| 138 | char* path = strchr(line, '/'); |
Elliott Hughes | 156da96 | 2012-10-09 17:14:56 -0700 | [diff] [blame] | 139 | if (path != NULL && strcmp(executable_path, path) == 0) { |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 140 | base_address = reinterpret_cast<void*>(start); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | fclose(fp); |
| 145 | |
| 146 | // The base address should be the address we were loaded at. |
| 147 | ASSERT_EQ(info.dli_fbase, base_address); |
| 148 | } |
| 149 | |
| 150 | TEST(dlopen, dladdr_invalid) { |
| 151 | Dl_info info; |
| 152 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 153 | dlerror(); // Clear any pending errors. |
| 154 | |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 155 | // No symbol corresponding to NULL. |
| 156 | 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] | 157 | ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3). |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 158 | |
| 159 | // No symbol corresponding to a stack address. |
| 160 | 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] | 161 | ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3). |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 162 | } |