blob: 434e38f91133c933e2867baf1426aca188c128fa [file] [log] [blame]
jeffhaoacf5aa72012-09-12 17:25:30 -07001/*
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 Hughes8e15b082012-09-26 11:44:01 -070020#include <libgen.h>
21#include <limits.h>
22#include <stdio.h>
23#include <stdint.h>
24
25#include <string>
jeffhaoacf5aa72012-09-12 17:25:30 -070026
Elliott Hughes3b297c42012-10-11 16:08:51 -070027#define ASSERT_SUBSTR(needle, haystack) \
28 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
29
Elliott Hughes1728b232014-05-14 10:02:03 -070030static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070031extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070032 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070033}
34
Elliott Hughese66190d2012-12-18 15:57:55 -080035TEST(dlfcn, dlsym_in_self) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070036 dlerror(); // Clear any pending errors.
jeffhaoacf5aa72012-09-12 17:25:30 -070037 void* self = dlopen(NULL, RTLD_NOW);
38 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -070039 ASSERT_TRUE(dlerror() == NULL);
jeffhaoacf5aa72012-09-12 17:25:30 -070040
41 void* sym = dlsym(self, "DlSymTestFunction");
42 ASSERT_TRUE(sym != NULL);
43
44 void (*function)() = reinterpret_cast<void(*)()>(sym);
45
Elliott Hughes1728b232014-05-14 10:02:03 -070046 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070047 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070048 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070049
50 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070051}
Elliott Hughes8e15b082012-09-26 11:44:01 -070052
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -070053TEST(dlfcn, dlopen_noload) {
54 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
55 ASSERT_TRUE(handle == NULL);
56 handle = dlopen("libtest_simple.so", RTLD_NOW);
57 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
58 ASSERT_TRUE(handle != NULL);
59 ASSERT_TRUE(handle2 != NULL);
60 ASSERT_TRUE(handle == handle2);
61 ASSERT_EQ(0, dlclose(handle));
62 ASSERT_EQ(0, dlclose(handle2));
63}
64
Elliott Hughese66190d2012-12-18 15:57:55 -080065TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070066 void* self = dlopen("/does/not/exist", RTLD_NOW);
67 ASSERT_TRUE(self == NULL);
Elliott Hughes063525c2014-05-13 11:19:57 -070068#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -070069 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
70#else
71 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
72#endif
73}
74
Elliott Hughesad88a082012-10-24 18:37:21 -070075static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -070076 dlopen("/child/thread", RTLD_NOW);
77 return reinterpret_cast<void*>(strdup(dlerror()));
78}
79
Elliott Hughese66190d2012-12-18 15:57:55 -080080TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -070081 dlopen("/main/thread", RTLD_NOW);
82 const char* main_thread_error = dlerror();
83 ASSERT_SUBSTR("/main/thread", main_thread_error);
84
85 pthread_t t;
86 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
87 void* result;
88 ASSERT_EQ(0, pthread_join(t, &result));
89 char* child_thread_error = static_cast<char*>(result);
90 ASSERT_SUBSTR("/child/thread", child_thread_error);
91 free(child_thread_error);
92
93 ASSERT_SUBSTR("/main/thread", main_thread_error);
94}
95
Elliott Hughese66190d2012-12-18 15:57:55 -080096TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070097 dlerror(); // Clear any pending errors.
Elliott Hughes8e15b082012-09-26 11:44:01 -070098 void* self = dlopen(NULL, RTLD_NOW);
99 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700100 ASSERT_TRUE(dlerror() == NULL);
101
102 void* sym;
103
104 // NULL handle.
105 sym = dlsym(NULL, "test");
106 ASSERT_TRUE(sym == NULL);
Elliott Hughes063525c2014-05-13 11:19:57 -0700107#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700108 ASSERT_SUBSTR("dlsym library handle is null", dlerror());
109#else
110 ASSERT_SUBSTR("undefined symbol: test", dlerror()); // glibc isn't specific about the failure.
111#endif
112
113 // NULL symbol name.
Elliott Hughes063525c2014-05-13 11:19:57 -0700114#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700115 // glibc marks this parameter non-null and SEGVs if you cheat.
116 sym = dlsym(self, NULL);
117 ASSERT_TRUE(sym == NULL);
118 ASSERT_SUBSTR("", dlerror());
119#endif
120
121 // Symbol that doesn't exist.
122 sym = dlsym(self, "ThisSymbolDoesNotExist");
123 ASSERT_TRUE(sym == NULL);
124 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700125
126 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700127}
128
Elliott Hughese66190d2012-12-18 15:57:55 -0800129TEST(dlfcn, dladdr) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700130 dlerror(); // Clear any pending errors.
131 void* self = dlopen(NULL, RTLD_NOW);
132 ASSERT_TRUE(self != NULL);
133 ASSERT_TRUE(dlerror() == NULL);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700134
135 void* sym = dlsym(self, "DlSymTestFunction");
136 ASSERT_TRUE(sym != NULL);
137
138 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
139 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
140
141 Dl_info info;
142 int rc = dladdr(addr, &info);
143 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
144
145 // Get the name of this executable.
146 char executable_path[PATH_MAX];
147 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
148 ASSERT_NE(rc, -1);
149 executable_path[rc] = '\0';
150 std::string executable_name(basename(executable_path));
151
152 // The filename should be that of this executable.
153 // Note that we don't know whether or not we have the full path, so we want an "ends_with" test.
154 std::string dli_fname(info.dli_fname);
155 dli_fname = basename(&dli_fname[0]);
156 ASSERT_EQ(dli_fname, executable_name);
157
158 // The symbol name should be the symbol we looked up.
159 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
160
161 // The address should be the exact address of the symbol.
162 ASSERT_EQ(info.dli_saddr, sym);
163
164 // Look in /proc/pid/maps to find out what address we were loaded at.
165 // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic.
166 void* base_address = NULL;
167 char path[PATH_MAX];
168 snprintf(path, sizeof(path), "/proc/%d/maps", getpid());
169 char line[BUFSIZ];
170 FILE* fp = fopen(path, "r");
171 ASSERT_TRUE(fp != NULL);
172 while (fgets(line, sizeof(line), fp) != NULL) {
173 uintptr_t start = strtoul(line, 0, 16);
174 line[strlen(line) - 1] = '\0'; // Chomp the '\n'.
175 char* path = strchr(line, '/');
Elliott Hughes156da962012-10-09 17:14:56 -0700176 if (path != NULL && strcmp(executable_path, path) == 0) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700177 base_address = reinterpret_cast<void*>(start);
178 break;
179 }
180 }
181 fclose(fp);
182
183 // The base address should be the address we were loaded at.
184 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700185
186 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700187}
188
Elliott Hughese66190d2012-12-18 15:57:55 -0800189TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700190 Dl_info info;
191
Elliott Hughes3b297c42012-10-11 16:08:51 -0700192 dlerror(); // Clear any pending errors.
193
Elliott Hughes8e15b082012-09-26 11:44:01 -0700194 // No symbol corresponding to NULL.
195 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700196 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700197
198 // No symbol corresponding to a stack address.
199 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700200 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700201}
Elliott Hughes124fae92012-10-31 14:20:03 -0700202
Elliott Hughes124fae92012-10-31 14:20:03 -0700203// Our dynamic linker doesn't support GNU hash tables.
Elliott Hughesa43e9062013-01-07 14:18:22 -0800204#if defined(__BIONIC__)
205// GNU-style ELF hash tables are incompatible with the MIPS ABI.
206// MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code.
207#if !defined(__mips__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800208TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Elliott Hughes124fae92012-10-31 14:20:03 -0700209 dlerror(); // Clear any pending errors.
Elliott Hughes6e33b022012-11-07 18:16:02 -0800210 void* handle = dlopen("no-elf-hash-table-library.so", RTLD_NOW);
Elliott Hughes124fae92012-10-31 14:20:03 -0700211 ASSERT_TRUE(handle == NULL);
Elliott Hughes6e33b022012-11-07 18:16:02 -0800212 ASSERT_STREQ("dlopen failed: empty/missing DT_HASH in \"no-elf-hash-table-library.so\" (built with --hash-style=gnu?)", dlerror());
Elliott Hughes124fae92012-10-31 14:20:03 -0700213}
214#endif
Elliott Hughesa43e9062013-01-07 14:18:22 -0800215#endif
Elliott Hughese66190d2012-12-18 15:57:55 -0800216
217TEST(dlfcn, dlopen_bad_flags) {
218 dlerror(); // Clear any pending errors.
219 void* handle;
220
Elliott Hughes063525c2014-05-13 11:19:57 -0700221#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800222 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
223 handle = dlopen(NULL, 0);
224 ASSERT_TRUE(handle == NULL);
225 ASSERT_SUBSTR("invalid", dlerror());
226#endif
227
228 handle = dlopen(NULL, 0xffffffff);
229 ASSERT_TRUE(handle == NULL);
230 ASSERT_SUBSTR("invalid", dlerror());
231
232 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
233 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY);
234 ASSERT_TRUE(handle != NULL);
235 ASSERT_SUBSTR(NULL, dlerror());
236}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400237
238TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800239 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400240 ASSERT_TRUE(addr == NULL);
241}
242
243TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800244 void* addr = dlsym(RTLD_DEFAULT, "fopen");
245 ASSERT_TRUE(addr != NULL);
246}
247
248TEST(dlfcn, rtld_next_unknown_symbol) {
249 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
250 ASSERT_TRUE(addr == NULL);
251}
252
253TEST(dlfcn, rtld_next_known_symbol) {
254 void* addr = dlsym(RTLD_NEXT, "fopen");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400255 ASSERT_TRUE(addr != NULL);
256}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700257
258TEST(dlfcn, dlopen_symlink) {
259 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
260 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
261 ASSERT_TRUE(handle1 != NULL);
262 ASSERT_TRUE(handle2 != NULL);
263 ASSERT_EQ(handle1, handle2);
264}