blob: be0523018d5bca7e24a0cfac145fa2c9fe2cc014 [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 Ivanovaa0f2bd2014-07-28 17:32:20 -070065#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 Ivanovaa0f2bd2014-07-28 17:32:20 -070080 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 Ivanovaa0f2bd2014-07-28 17:32:20 -070084TEST(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
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700109// ifuncs are only supported on intel for now
110#if defined(__i386__) || defined(__x86_64__)
111TEST(dlfcn, ifunc) {
112 const char* (*foo_ptr)();
113 const char* (*foo_library_ptr)();
114
115 // ifunc's choice depends on whether IFUNC_CHOICE has a value
116 // first check the set case
117 setenv("IFUNC_CHOICE", "set", 1);
118 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
119 ASSERT_TRUE(handle != NULL);
120 *(void **)(&foo_ptr) = dlsym(handle, "foo");
121 *(void **)(&foo_library_ptr) = dlsym(handle, "foo_library");
122 ASSERT_TRUE(foo_ptr != NULL);
123 ASSERT_TRUE(foo_library_ptr != NULL);
124 ASSERT_EQ(strncmp("set", (*foo_ptr)(), 3), 0);
125 ASSERT_EQ(strncmp("set", (*foo_library_ptr)(), 3), 0);
126 dlclose(handle);
127
128 // then check the unset case
129 unsetenv("IFUNC_CHOICE");
130 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
131 ASSERT_TRUE(handle != NULL);
132 *(void **)(&foo_ptr) = dlsym(handle, "foo");
133 *(void **)(&foo_library_ptr) = dlsym(handle, "foo_library");
134 ASSERT_TRUE(foo_ptr != NULL);
135 ASSERT_TRUE(foo_library_ptr != NULL);
136 ASSERT_EQ(strncmp("unset", (*foo_ptr)(), 5), 0);
137 ASSERT_EQ(strncmp("unset", (*foo_library_ptr)(), 3), 0);
138 dlclose(handle);
139}
140#endif
141
Elliott Hughese66190d2012-12-18 15:57:55 -0800142TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700143 void* self = dlopen("/does/not/exist", RTLD_NOW);
144 ASSERT_TRUE(self == NULL);
Elliott Hughes063525c2014-05-13 11:19:57 -0700145#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700146 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
147#else
148 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
149#endif
150}
151
Elliott Hughesad88a082012-10-24 18:37:21 -0700152static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700153 dlopen("/child/thread", RTLD_NOW);
154 return reinterpret_cast<void*>(strdup(dlerror()));
155}
156
Elliott Hughese66190d2012-12-18 15:57:55 -0800157TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700158 dlopen("/main/thread", RTLD_NOW);
159 const char* main_thread_error = dlerror();
160 ASSERT_SUBSTR("/main/thread", main_thread_error);
161
162 pthread_t t;
163 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
164 void* result;
165 ASSERT_EQ(0, pthread_join(t, &result));
166 char* child_thread_error = static_cast<char*>(result);
167 ASSERT_SUBSTR("/child/thread", child_thread_error);
168 free(child_thread_error);
169
170 ASSERT_SUBSTR("/main/thread", main_thread_error);
171}
172
Elliott Hughese66190d2012-12-18 15:57:55 -0800173TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700174 dlerror(); // Clear any pending errors.
Elliott Hughes8e15b082012-09-26 11:44:01 -0700175 void* self = dlopen(NULL, RTLD_NOW);
176 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700177 ASSERT_TRUE(dlerror() == NULL);
178
179 void* sym;
180
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700181#if defined(__BIONIC__) && !defined(__LP64__)
182 // RTLD_DEFAULT in lp32 bionic is not (void*)0
183 // so it can be distinguished from the NULL handle.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700184 sym = dlsym(NULL, "test");
185 ASSERT_TRUE(sym == NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700186 ASSERT_SUBSTR("dlsym library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700187#endif
188
189 // NULL symbol name.
Elliott Hughes063525c2014-05-13 11:19:57 -0700190#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700191 // glibc marks this parameter non-null and SEGVs if you cheat.
192 sym = dlsym(self, NULL);
193 ASSERT_TRUE(sym == NULL);
194 ASSERT_SUBSTR("", dlerror());
195#endif
196
197 // Symbol that doesn't exist.
198 sym = dlsym(self, "ThisSymbolDoesNotExist");
199 ASSERT_TRUE(sym == NULL);
200 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700201
202 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700203}
204
Elliott Hughese66190d2012-12-18 15:57:55 -0800205TEST(dlfcn, dladdr) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700206 dlerror(); // Clear any pending errors.
207 void* self = dlopen(NULL, RTLD_NOW);
208 ASSERT_TRUE(self != NULL);
209 ASSERT_TRUE(dlerror() == NULL);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700210
211 void* sym = dlsym(self, "DlSymTestFunction");
212 ASSERT_TRUE(sym != NULL);
213
214 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
215 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
216
217 Dl_info info;
218 int rc = dladdr(addr, &info);
219 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
220
221 // Get the name of this executable.
222 char executable_path[PATH_MAX];
223 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
224 ASSERT_NE(rc, -1);
225 executable_path[rc] = '\0';
226 std::string executable_name(basename(executable_path));
227
228 // The filename should be that of this executable.
229 // Note that we don't know whether or not we have the full path, so we want an "ends_with" test.
230 std::string dli_fname(info.dli_fname);
231 dli_fname = basename(&dli_fname[0]);
232 ASSERT_EQ(dli_fname, executable_name);
233
234 // The symbol name should be the symbol we looked up.
235 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
236
237 // The address should be the exact address of the symbol.
238 ASSERT_EQ(info.dli_saddr, sym);
239
240 // Look in /proc/pid/maps to find out what address we were loaded at.
241 // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic.
242 void* base_address = NULL;
243 char path[PATH_MAX];
244 snprintf(path, sizeof(path), "/proc/%d/maps", getpid());
245 char line[BUFSIZ];
246 FILE* fp = fopen(path, "r");
247 ASSERT_TRUE(fp != NULL);
248 while (fgets(line, sizeof(line), fp) != NULL) {
249 uintptr_t start = strtoul(line, 0, 16);
250 line[strlen(line) - 1] = '\0'; // Chomp the '\n'.
251 char* path = strchr(line, '/');
Elliott Hughes156da962012-10-09 17:14:56 -0700252 if (path != NULL && strcmp(executable_path, path) == 0) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700253 base_address = reinterpret_cast<void*>(start);
254 break;
255 }
256 }
257 fclose(fp);
258
259 // The base address should be the address we were loaded at.
260 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700261
262 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700263}
264
Elliott Hughese66190d2012-12-18 15:57:55 -0800265TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700266 Dl_info info;
267
Elliott Hughes3b297c42012-10-11 16:08:51 -0700268 dlerror(); // Clear any pending errors.
269
Elliott Hughes8e15b082012-09-26 11:44:01 -0700270 // No symbol corresponding to NULL.
271 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700272 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700273
274 // No symbol corresponding to a stack address.
275 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700276 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700277}
Elliott Hughes124fae92012-10-31 14:20:03 -0700278
Elliott Hughes124fae92012-10-31 14:20:03 -0700279// Our dynamic linker doesn't support GNU hash tables.
Elliott Hughesa43e9062013-01-07 14:18:22 -0800280#if defined(__BIONIC__)
281// GNU-style ELF hash tables are incompatible with the MIPS ABI.
282// MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code.
283#if !defined(__mips__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800284TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Elliott Hughes124fae92012-10-31 14:20:03 -0700285 dlerror(); // Clear any pending errors.
Elliott Hughes6e33b022012-11-07 18:16:02 -0800286 void* handle = dlopen("no-elf-hash-table-library.so", RTLD_NOW);
Elliott Hughes124fae92012-10-31 14:20:03 -0700287 ASSERT_TRUE(handle == NULL);
Elliott Hughes6e33b022012-11-07 18:16:02 -0800288 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 -0700289}
290#endif
Elliott Hughesa43e9062013-01-07 14:18:22 -0800291#endif
Elliott Hughese66190d2012-12-18 15:57:55 -0800292
293TEST(dlfcn, dlopen_bad_flags) {
294 dlerror(); // Clear any pending errors.
295 void* handle;
296
Elliott Hughes063525c2014-05-13 11:19:57 -0700297#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800298 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
299 handle = dlopen(NULL, 0);
300 ASSERT_TRUE(handle == NULL);
301 ASSERT_SUBSTR("invalid", dlerror());
302#endif
303
304 handle = dlopen(NULL, 0xffffffff);
305 ASSERT_TRUE(handle == NULL);
306 ASSERT_SUBSTR("invalid", dlerror());
307
308 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
309 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY);
310 ASSERT_TRUE(handle != NULL);
311 ASSERT_SUBSTR(NULL, dlerror());
312}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400313
314TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800315 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400316 ASSERT_TRUE(addr == NULL);
317}
318
319TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800320 void* addr = dlsym(RTLD_DEFAULT, "fopen");
321 ASSERT_TRUE(addr != NULL);
322}
323
324TEST(dlfcn, rtld_next_unknown_symbol) {
325 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
326 ASSERT_TRUE(addr == NULL);
327}
328
329TEST(dlfcn, rtld_next_known_symbol) {
330 void* addr = dlsym(RTLD_NEXT, "fopen");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400331 ASSERT_TRUE(addr != NULL);
332}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700333
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700334TEST(dlfcn, dlsym_weak_func) {
335 dlerror();
336 void* handle = dlopen("libtest_dlsym_weak_func.so",RTLD_NOW);
337 ASSERT_TRUE(handle != NULL);
338
339 int (*weak_func)();
340 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
341 ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror();
342 EXPECT_EQ(42, weak_func());
343 dlclose(handle);
344}
345
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700346TEST(dlfcn, dlopen_symlink) {
347 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
348 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
349 ASSERT_TRUE(handle1 != NULL);
350 ASSERT_TRUE(handle2 != NULL);
351 ASSERT_EQ(handle1, handle2);
352}