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