blob: b8c33dd53d59aac1c46f65756ffea2529115274e [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 -070065TEST(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 Ivanovb648a8a2014-05-19 15:06:58 -070078TEST(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 Smithc5a13ef2014-07-23 11:22:25 -070090// ifuncs are only supported on intel for now
91#if defined(__i386__) || defined(__x86_64__)
92TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -070093 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -070094
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 Ivanov9598b8c2014-08-21 13:54:03 -0700100 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 Smithc5a13ef2014-07-23 11:22:25 -0700102 ASSERT_TRUE(foo_ptr != NULL);
103 ASSERT_TRUE(foo_library_ptr != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700104 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
105 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700106 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 Ivanov9598b8c2014-08-21 13:54:03 -0700112 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
113 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700114 ASSERT_TRUE(foo_ptr != NULL);
115 ASSERT_TRUE(foo_library_ptr != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700116 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
117 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0);
118 dlclose(handle);
119}
120
121TEST(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 Smithc5a13ef2014-07-23 11:22:25 -0700129 dlclose(handle);
130}
131#endif
132
Dmitriy Ivanova3ad4502014-07-29 14:21:45 -0700133TEST(dlfcn, dlopen_check_order) {
134 // Here is how the test library and its dt_needed
135 // libraries are arranged
136 //
137 // libtest_check_order.so
138 // |
139 // +-> libtest_check_order_1_left.so
140 // | |
141 // | +-> libtest_check_order_a.so
142 // | |
143 // | +-> libtest_check_order_b.so
144 // |
145 // +-> libtest_check_order_2_right.so
146 // | |
147 // | +-> libtest_check_order_d.so
148 // | |
149 // | +-> libtest_check_order_b.so
150 // |
151 // +-> libtest_check_order_3_c.so
152 //
153 // load order should be (1, 2, 3, a, b, d)
154 //
155 // get_answer() is defined in (2, 3, a, b, c)
156 // get_answer2() is defined in (b, d)
157 void* sym = dlsym(RTLD_DEFAULT, "dlopen_test_get_answer");
158 ASSERT_TRUE(sym == nullptr);
159 void* handle = dlopen("libtest_check_order.so", RTLD_NOW);
160 ASSERT_TRUE(handle != nullptr);
161 typedef int (*fn_t) (void);
162 fn_t fn, fn2;
163 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "dlopen_test_get_answer"));
164 ASSERT_TRUE(fn != NULL);
165 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "dlopen_test_get_answer2"));
166 ASSERT_TRUE(fn2 != NULL);
167
168 ASSERT_EQ(42, fn());
169 ASSERT_EQ(43, fn2());
170 dlclose(handle);
171}
172
173// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
174// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
175// libtest_with_dependency_loop_a.so
176TEST(dlfcn, dlopen_check_loop) {
177 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
178 ASSERT_TRUE(handle == NULL);
179 ASSERT_STREQ("dlopen failed: recursive link to \"libtest_with_dependency_loop_a.so\"", dlerror());
180}
181
Elliott Hughese66190d2012-12-18 15:57:55 -0800182TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700183 void* self = dlopen("/does/not/exist", RTLD_NOW);
184 ASSERT_TRUE(self == NULL);
Elliott Hughes063525c2014-05-13 11:19:57 -0700185#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700186 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
187#else
188 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
189#endif
190}
191
Elliott Hughesad88a082012-10-24 18:37:21 -0700192static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700193 dlopen("/child/thread", RTLD_NOW);
194 return reinterpret_cast<void*>(strdup(dlerror()));
195}
196
Elliott Hughese66190d2012-12-18 15:57:55 -0800197TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700198 dlopen("/main/thread", RTLD_NOW);
199 const char* main_thread_error = dlerror();
200 ASSERT_SUBSTR("/main/thread", main_thread_error);
201
202 pthread_t t;
203 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
204 void* result;
205 ASSERT_EQ(0, pthread_join(t, &result));
206 char* child_thread_error = static_cast<char*>(result);
207 ASSERT_SUBSTR("/child/thread", child_thread_error);
208 free(child_thread_error);
209
210 ASSERT_SUBSTR("/main/thread", main_thread_error);
211}
212
Elliott Hughese66190d2012-12-18 15:57:55 -0800213TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700214 dlerror(); // Clear any pending errors.
Elliott Hughes8e15b082012-09-26 11:44:01 -0700215 void* self = dlopen(NULL, RTLD_NOW);
216 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700217 ASSERT_TRUE(dlerror() == NULL);
218
219 void* sym;
220
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700221#if defined(__BIONIC__) && !defined(__LP64__)
222 // RTLD_DEFAULT in lp32 bionic is not (void*)0
223 // so it can be distinguished from the NULL handle.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700224 sym = dlsym(NULL, "test");
225 ASSERT_TRUE(sym == NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700226 ASSERT_SUBSTR("dlsym library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700227#endif
228
229 // NULL symbol name.
Elliott Hughes063525c2014-05-13 11:19:57 -0700230#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700231 // glibc marks this parameter non-null and SEGVs if you cheat.
232 sym = dlsym(self, NULL);
233 ASSERT_TRUE(sym == NULL);
234 ASSERT_SUBSTR("", dlerror());
235#endif
236
237 // Symbol that doesn't exist.
238 sym = dlsym(self, "ThisSymbolDoesNotExist");
239 ASSERT_TRUE(sym == NULL);
240 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700241
242 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700243}
244
Elliott Hughese66190d2012-12-18 15:57:55 -0800245TEST(dlfcn, dladdr) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700246 dlerror(); // Clear any pending errors.
247 void* self = dlopen(NULL, RTLD_NOW);
248 ASSERT_TRUE(self != NULL);
249 ASSERT_TRUE(dlerror() == NULL);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700250
251 void* sym = dlsym(self, "DlSymTestFunction");
252 ASSERT_TRUE(sym != NULL);
253
254 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
255 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
256
257 Dl_info info;
258 int rc = dladdr(addr, &info);
259 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
260
261 // Get the name of this executable.
262 char executable_path[PATH_MAX];
263 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
264 ASSERT_NE(rc, -1);
265 executable_path[rc] = '\0';
266 std::string executable_name(basename(executable_path));
267
268 // The filename should be that of this executable.
269 // Note that we don't know whether or not we have the full path, so we want an "ends_with" test.
270 std::string dli_fname(info.dli_fname);
271 dli_fname = basename(&dli_fname[0]);
272 ASSERT_EQ(dli_fname, executable_name);
273
274 // The symbol name should be the symbol we looked up.
275 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
276
277 // The address should be the exact address of the symbol.
278 ASSERT_EQ(info.dli_saddr, sym);
279
280 // Look in /proc/pid/maps to find out what address we were loaded at.
281 // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic.
282 void* base_address = NULL;
Elliott Hughes8e15b082012-09-26 11:44:01 -0700283 char line[BUFSIZ];
Elliott Hughes57b7a612014-08-25 17:26:50 -0700284 FILE* fp = fopen("/proc/self/maps", "r");
Elliott Hughes8e15b082012-09-26 11:44:01 -0700285 ASSERT_TRUE(fp != NULL);
286 while (fgets(line, sizeof(line), fp) != NULL) {
287 uintptr_t start = strtoul(line, 0, 16);
288 line[strlen(line) - 1] = '\0'; // Chomp the '\n'.
289 char* path = strchr(line, '/');
Elliott Hughes156da962012-10-09 17:14:56 -0700290 if (path != NULL && strcmp(executable_path, path) == 0) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700291 base_address = reinterpret_cast<void*>(start);
292 break;
293 }
294 }
295 fclose(fp);
296
297 // The base address should be the address we were loaded at.
298 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700299
300 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700301}
302
Elliott Hughese66190d2012-12-18 15:57:55 -0800303TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700304 Dl_info info;
305
Elliott Hughes3b297c42012-10-11 16:08:51 -0700306 dlerror(); // Clear any pending errors.
307
Elliott Hughes8e15b082012-09-26 11:44:01 -0700308 // No symbol corresponding to NULL.
309 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700310 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700311
312 // No symbol corresponding to a stack address.
313 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700314 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700315}
Elliott Hughes124fae92012-10-31 14:20:03 -0700316
Elliott Hughes124fae92012-10-31 14:20:03 -0700317// Our dynamic linker doesn't support GNU hash tables.
Elliott Hughesa43e9062013-01-07 14:18:22 -0800318#if defined(__BIONIC__)
319// GNU-style ELF hash tables are incompatible with the MIPS ABI.
320// MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code.
321#if !defined(__mips__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800322TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Elliott Hughes124fae92012-10-31 14:20:03 -0700323 dlerror(); // Clear any pending errors.
Elliott Hughes6e33b022012-11-07 18:16:02 -0800324 void* handle = dlopen("no-elf-hash-table-library.so", RTLD_NOW);
Elliott Hughes124fae92012-10-31 14:20:03 -0700325 ASSERT_TRUE(handle == NULL);
Elliott Hughes6e33b022012-11-07 18:16:02 -0800326 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 -0700327}
328#endif
Elliott Hughesa43e9062013-01-07 14:18:22 -0800329#endif
Elliott Hughese66190d2012-12-18 15:57:55 -0800330
331TEST(dlfcn, dlopen_bad_flags) {
332 dlerror(); // Clear any pending errors.
333 void* handle;
334
Elliott Hughes063525c2014-05-13 11:19:57 -0700335#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800336 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
337 handle = dlopen(NULL, 0);
338 ASSERT_TRUE(handle == NULL);
339 ASSERT_SUBSTR("invalid", dlerror());
340#endif
341
342 handle = dlopen(NULL, 0xffffffff);
343 ASSERT_TRUE(handle == NULL);
344 ASSERT_SUBSTR("invalid", dlerror());
345
346 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
347 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY);
348 ASSERT_TRUE(handle != NULL);
349 ASSERT_SUBSTR(NULL, dlerror());
350}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400351
352TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800353 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400354 ASSERT_TRUE(addr == NULL);
355}
356
357TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800358 void* addr = dlsym(RTLD_DEFAULT, "fopen");
359 ASSERT_TRUE(addr != NULL);
360}
361
362TEST(dlfcn, rtld_next_unknown_symbol) {
363 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
364 ASSERT_TRUE(addr == NULL);
365}
366
367TEST(dlfcn, rtld_next_known_symbol) {
368 void* addr = dlsym(RTLD_NEXT, "fopen");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400369 ASSERT_TRUE(addr != NULL);
370}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700371
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700372TEST(dlfcn, dlsym_weak_func) {
373 dlerror();
374 void* handle = dlopen("libtest_dlsym_weak_func.so",RTLD_NOW);
375 ASSERT_TRUE(handle != NULL);
376
377 int (*weak_func)();
378 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
379 ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror();
380 EXPECT_EQ(42, weak_func());
381 dlclose(handle);
382}
383
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700384TEST(dlfcn, dlopen_symlink) {
385 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
386 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
387 ASSERT_TRUE(handle1 != NULL);
388 ASSERT_TRUE(handle2 != NULL);
389 ASSERT_EQ(handle1, handle2);
390}