blob: 9bc25574b3139aaa1803c441e65797c9d6e5befb [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
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070035static int g_ctor_function_called = 0;
36
37extern "C" void ctor_function() __attribute__ ((constructor));
38
39extern "C" void ctor_function() {
40 g_ctor_function_called = 17;
41}
42
43TEST(dlfcn, ctor_function_call) {
44 ASSERT_EQ(17, g_ctor_function_called);
45}
46
Elliott Hughese66190d2012-12-18 15:57:55 -080047TEST(dlfcn, dlsym_in_self) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070048 dlerror(); // Clear any pending errors.
jeffhaoacf5aa72012-09-12 17:25:30 -070049 void* self = dlopen(NULL, RTLD_NOW);
50 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -070051 ASSERT_TRUE(dlerror() == NULL);
jeffhaoacf5aa72012-09-12 17:25:30 -070052
53 void* sym = dlsym(self, "DlSymTestFunction");
54 ASSERT_TRUE(sym != NULL);
55
56 void (*function)() = reinterpret_cast<void(*)()>(sym);
57
Elliott Hughes1728b232014-05-14 10:02:03 -070058 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070059 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070060 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070061
62 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070063}
Elliott Hughes8e15b082012-09-26 11:44:01 -070064
Dmitriy Ivanovdb7a17d2014-08-04 23:39:22 +000065#if defined(__arm__)
66// This seems to be working only for arm.
67// Others platforms optimize LOCAL PROTECTED symbols.
Dmitriy Ivanovd97e9f52014-06-29 12:28:37 -070068TEST(dlfcn, dlsym_local_symbol) {
69 void* handle = dlopen("libtest_local_symbol.so", RTLD_NOW);
70 ASSERT_TRUE(handle != NULL);
71 dlerror();
72 void* sym = dlsym(handle, "private_taxicab_number");
73 ASSERT_TRUE(sym == NULL);
74 ASSERT_STREQ("undefined symbol: private_taxicab_number", dlerror());
75
76 uint32_t (*f)(void);
77 f = reinterpret_cast<uint32_t (*)(void)>(dlsym(handle, "dlsym_local_symbol_get_taxicab_number_using_dlsym"));
78 ASSERT_TRUE(f != NULL);
Elliott Hughesd06ee1d2014-07-01 17:17:46 -070079 ASSERT_EQ(1729U, f());
Dmitriy Ivanovdb7a17d2014-08-04 23:39:22 +000080 dlclose(handle);
Dmitriy Ivanovd97e9f52014-06-29 12:28:37 -070081}
Dmitriy Ivanovce0ba3c2014-07-01 19:09:49 -070082#endif
Dmitriy Ivanovd97e9f52014-06-29 12:28:37 -070083
Dmitriy Ivanovdb7a17d2014-08-04 23:39:22 +000084TEST(dlfcn, dlsym_with_dependencies) {
85 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
86 ASSERT_TRUE(handle != NULL);
87 dlerror();
88 // This symbol is in DT_NEEDED library.
89 void* sym = dlsym(handle, "getRandomNumber");
90 ASSERT_TRUE(sym != NULL);
91 int (*fn)(void);
92 fn = reinterpret_cast<int (*)(void)>(sym);
93 EXPECT_EQ(4, fn());
94 dlclose(handle);
95}
96
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -070097TEST(dlfcn, dlopen_noload) {
98 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
99 ASSERT_TRUE(handle == NULL);
100 handle = dlopen("libtest_simple.so", RTLD_NOW);
101 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
102 ASSERT_TRUE(handle != NULL);
103 ASSERT_TRUE(handle2 != NULL);
104 ASSERT_TRUE(handle == handle2);
105 ASSERT_EQ(0, dlclose(handle));
106 ASSERT_EQ(0, dlclose(handle2));
107}
108
Elliott Hughese66190d2012-12-18 15:57:55 -0800109TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700110 void* self = dlopen("/does/not/exist", RTLD_NOW);
111 ASSERT_TRUE(self == NULL);
Elliott Hughes063525c2014-05-13 11:19:57 -0700112#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700113 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
114#else
115 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
116#endif
117}
118
Elliott Hughesad88a082012-10-24 18:37:21 -0700119static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700120 dlopen("/child/thread", RTLD_NOW);
121 return reinterpret_cast<void*>(strdup(dlerror()));
122}
123
Elliott Hughese66190d2012-12-18 15:57:55 -0800124TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700125 dlopen("/main/thread", RTLD_NOW);
126 const char* main_thread_error = dlerror();
127 ASSERT_SUBSTR("/main/thread", main_thread_error);
128
129 pthread_t t;
130 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
131 void* result;
132 ASSERT_EQ(0, pthread_join(t, &result));
133 char* child_thread_error = static_cast<char*>(result);
134 ASSERT_SUBSTR("/child/thread", child_thread_error);
135 free(child_thread_error);
136
137 ASSERT_SUBSTR("/main/thread", main_thread_error);
138}
139
Elliott Hughese66190d2012-12-18 15:57:55 -0800140TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700141 dlerror(); // Clear any pending errors.
Elliott Hughes8e15b082012-09-26 11:44:01 -0700142 void* self = dlopen(NULL, RTLD_NOW);
143 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700144 ASSERT_TRUE(dlerror() == NULL);
145
146 void* sym;
147
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700148#if defined(__BIONIC__) && !defined(__LP64__)
149 // RTLD_DEFAULT in lp32 bionic is not (void*)0
150 // so it can be distinguished from the NULL handle.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700151 sym = dlsym(NULL, "test");
152 ASSERT_TRUE(sym == NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700153 ASSERT_SUBSTR("dlsym library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700154#endif
155
156 // NULL symbol name.
Elliott Hughes063525c2014-05-13 11:19:57 -0700157#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700158 // glibc marks this parameter non-null and SEGVs if you cheat.
159 sym = dlsym(self, NULL);
160 ASSERT_TRUE(sym == NULL);
161 ASSERT_SUBSTR("", dlerror());
162#endif
163
164 // Symbol that doesn't exist.
165 sym = dlsym(self, "ThisSymbolDoesNotExist");
166 ASSERT_TRUE(sym == NULL);
167 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700168
169 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700170}
171
Elliott Hughese66190d2012-12-18 15:57:55 -0800172TEST(dlfcn, dladdr) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700173 dlerror(); // Clear any pending errors.
174 void* self = dlopen(NULL, RTLD_NOW);
175 ASSERT_TRUE(self != NULL);
176 ASSERT_TRUE(dlerror() == NULL);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700177
178 void* sym = dlsym(self, "DlSymTestFunction");
179 ASSERT_TRUE(sym != NULL);
180
181 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
182 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
183
184 Dl_info info;
185 int rc = dladdr(addr, &info);
186 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
187
188 // Get the name of this executable.
189 char executable_path[PATH_MAX];
190 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
191 ASSERT_NE(rc, -1);
192 executable_path[rc] = '\0';
193 std::string executable_name(basename(executable_path));
194
195 // The filename should be that of this executable.
196 // Note that we don't know whether or not we have the full path, so we want an "ends_with" test.
197 std::string dli_fname(info.dli_fname);
198 dli_fname = basename(&dli_fname[0]);
199 ASSERT_EQ(dli_fname, executable_name);
200
201 // The symbol name should be the symbol we looked up.
202 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
203
204 // The address should be the exact address of the symbol.
205 ASSERT_EQ(info.dli_saddr, sym);
206
207 // Look in /proc/pid/maps to find out what address we were loaded at.
208 // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic.
209 void* base_address = NULL;
210 char path[PATH_MAX];
211 snprintf(path, sizeof(path), "/proc/%d/maps", getpid());
212 char line[BUFSIZ];
213 FILE* fp = fopen(path, "r");
214 ASSERT_TRUE(fp != NULL);
215 while (fgets(line, sizeof(line), fp) != NULL) {
216 uintptr_t start = strtoul(line, 0, 16);
217 line[strlen(line) - 1] = '\0'; // Chomp the '\n'.
218 char* path = strchr(line, '/');
Elliott Hughes156da962012-10-09 17:14:56 -0700219 if (path != NULL && strcmp(executable_path, path) == 0) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700220 base_address = reinterpret_cast<void*>(start);
221 break;
222 }
223 }
224 fclose(fp);
225
226 // The base address should be the address we were loaded at.
227 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700228
229 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700230}
231
Elliott Hughese66190d2012-12-18 15:57:55 -0800232TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700233 Dl_info info;
234
Elliott Hughes3b297c42012-10-11 16:08:51 -0700235 dlerror(); // Clear any pending errors.
236
Elliott Hughes8e15b082012-09-26 11:44:01 -0700237 // No symbol corresponding to NULL.
238 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700239 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700240
241 // No symbol corresponding to a stack address.
242 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700243 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700244}
Elliott Hughes124fae92012-10-31 14:20:03 -0700245
Elliott Hughes124fae92012-10-31 14:20:03 -0700246// Our dynamic linker doesn't support GNU hash tables.
Elliott Hughesa43e9062013-01-07 14:18:22 -0800247#if defined(__BIONIC__)
248// GNU-style ELF hash tables are incompatible with the MIPS ABI.
249// MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code.
250#if !defined(__mips__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800251TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Elliott Hughes124fae92012-10-31 14:20:03 -0700252 dlerror(); // Clear any pending errors.
Elliott Hughes6e33b022012-11-07 18:16:02 -0800253 void* handle = dlopen("no-elf-hash-table-library.so", RTLD_NOW);
Elliott Hughes124fae92012-10-31 14:20:03 -0700254 ASSERT_TRUE(handle == NULL);
Elliott Hughes6e33b022012-11-07 18:16:02 -0800255 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 -0700256}
257#endif
Elliott Hughesa43e9062013-01-07 14:18:22 -0800258#endif
Elliott Hughese66190d2012-12-18 15:57:55 -0800259
260TEST(dlfcn, dlopen_bad_flags) {
261 dlerror(); // Clear any pending errors.
262 void* handle;
263
Elliott Hughes063525c2014-05-13 11:19:57 -0700264#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800265 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
266 handle = dlopen(NULL, 0);
267 ASSERT_TRUE(handle == NULL);
268 ASSERT_SUBSTR("invalid", dlerror());
269#endif
270
271 handle = dlopen(NULL, 0xffffffff);
272 ASSERT_TRUE(handle == NULL);
273 ASSERT_SUBSTR("invalid", dlerror());
274
275 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
276 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY);
277 ASSERT_TRUE(handle != NULL);
278 ASSERT_SUBSTR(NULL, dlerror());
279}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400280
281TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800282 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400283 ASSERT_TRUE(addr == NULL);
284}
285
286TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800287 void* addr = dlsym(RTLD_DEFAULT, "fopen");
288 ASSERT_TRUE(addr != NULL);
289}
290
291TEST(dlfcn, rtld_next_unknown_symbol) {
292 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
293 ASSERT_TRUE(addr == NULL);
294}
295
296TEST(dlfcn, rtld_next_known_symbol) {
297 void* addr = dlsym(RTLD_NEXT, "fopen");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400298 ASSERT_TRUE(addr != NULL);
299}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700300
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700301TEST(dlfcn, dlsym_weak_func) {
302 dlerror();
303 void* handle = dlopen("libtest_dlsym_weak_func.so",RTLD_NOW);
304 ASSERT_TRUE(handle != NULL);
305
306 int (*weak_func)();
307 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
308 ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror();
309 EXPECT_EQ(42, weak_func());
310 dlclose(handle);
311}
312
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700313TEST(dlfcn, dlopen_symlink) {
314 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
315 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
316 ASSERT_TRUE(handle1 != NULL);
317 ASSERT_TRUE(handle2 != NULL);
318 ASSERT_EQ(handle1, handle2);
319}