blob: 1613ee8d0b03b1118392ea2812896bfad8a273a5 [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
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -070025#include "private/ScopeGuard.h"
26
Elliott Hughes8e15b082012-09-26 11:44:01 -070027#include <string>
jeffhaoacf5aa72012-09-12 17:25:30 -070028
Elliott Hughes3b297c42012-10-11 16:08:51 -070029#define ASSERT_SUBSTR(needle, haystack) \
30 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
31
Elliott Hughes1728b232014-05-14 10:02:03 -070032static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070033extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070034 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070035}
36
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070037static int g_ctor_function_called = 0;
38
39extern "C" void ctor_function() __attribute__ ((constructor));
40
41extern "C" void ctor_function() {
42 g_ctor_function_called = 17;
43}
44
45TEST(dlfcn, ctor_function_call) {
46 ASSERT_EQ(17, g_ctor_function_called);
47}
48
Elliott Hughese66190d2012-12-18 15:57:55 -080049TEST(dlfcn, dlsym_in_self) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070050 dlerror(); // Clear any pending errors.
jeffhaoacf5aa72012-09-12 17:25:30 -070051 void* self = dlopen(NULL, RTLD_NOW);
52 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -070053 ASSERT_TRUE(dlerror() == NULL);
jeffhaoacf5aa72012-09-12 17:25:30 -070054
55 void* sym = dlsym(self, "DlSymTestFunction");
56 ASSERT_TRUE(sym != NULL);
57
58 void (*function)() = reinterpret_cast<void(*)()>(sym);
59
Elliott Hughes1728b232014-05-14 10:02:03 -070060 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070061 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070062 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070063
64 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070065}
Elliott Hughes8e15b082012-09-26 11:44:01 -070066
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -070067TEST(dlfcn, dlsym_with_dependencies) {
68 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
69 ASSERT_TRUE(handle != NULL);
70 dlerror();
71 // This symbol is in DT_NEEDED library.
72 void* sym = dlsym(handle, "getRandomNumber");
73 ASSERT_TRUE(sym != NULL);
74 int (*fn)(void);
75 fn = reinterpret_cast<int (*)(void)>(sym);
76 EXPECT_EQ(4, fn());
77 dlclose(handle);
78}
79
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -070080TEST(dlfcn, dlopen_noload) {
81 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
82 ASSERT_TRUE(handle == NULL);
83 handle = dlopen("libtest_simple.so", RTLD_NOW);
84 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
85 ASSERT_TRUE(handle != NULL);
86 ASSERT_TRUE(handle2 != NULL);
87 ASSERT_TRUE(handle == handle2);
88 ASSERT_EQ(0, dlclose(handle));
89 ASSERT_EQ(0, dlclose(handle2));
90}
91
Brigid Smithc5a13ef2014-07-23 11:22:25 -070092// ifuncs are only supported on intel for now
93#if defined(__i386__) || defined(__x86_64__)
94TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -070095 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -070096
97 // ifunc's choice depends on whether IFUNC_CHOICE has a value
98 // first check the set case
99 setenv("IFUNC_CHOICE", "set", 1);
100 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
101 ASSERT_TRUE(handle != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700102 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
103 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700104 ASSERT_TRUE(foo_ptr != NULL);
105 ASSERT_TRUE(foo_library_ptr != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700106 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
107 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700108 dlclose(handle);
109
110 // then check the unset case
111 unsetenv("IFUNC_CHOICE");
112 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
113 ASSERT_TRUE(handle != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700114 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
115 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700116 ASSERT_TRUE(foo_ptr != NULL);
117 ASSERT_TRUE(foo_library_ptr != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700118 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
119 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0);
120 dlclose(handle);
121}
122
123TEST(dlfcn, ifunc_ctor_call) {
124 typedef const char* (*fn_ptr)();
125
126 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
127 ASSERT_TRUE(handle != NULL) << dlerror();
128 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called"));
129 ASSERT_TRUE(is_ctor_called != NULL) << dlerror();
130 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700131 dlclose(handle);
132}
133#endif
134
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700135TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
136 // This is the structure of the test library and
137 // its dt_needed libraries
138 // libtest_relo_check_dt_needed_order.so
139 // |
140 // +-> libtest_relo_check_dt_needed_order_1.so
141 // |
142 // +-> libtest_relo_check_dt_needed_order_2.so
143 //
144 // The root library references relo_test_get_answer_lib - which is defined
145 // in both dt_needed libraries, the correct relocation should
146 // use the function defined in libtest_relo_check_dt_needed_order_1.so
147 void* handle = nullptr;
148 auto guard = create_scope_guard([&]() {
149 dlclose(handle);
150 });
151
152 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
153 ASSERT_TRUE(handle != nullptr) << dlerror();
154
155 typedef int (*fn_t) (void);
156 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
157 ASSERT_TRUE(fn != nullptr) << dlerror();
158 ASSERT_EQ(1, fn());
159}
160
Elliott Hughese66190d2012-12-18 15:57:55 -0800161TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700162 void* self = dlopen("/does/not/exist", RTLD_NOW);
163 ASSERT_TRUE(self == NULL);
Elliott Hughes063525c2014-05-13 11:19:57 -0700164#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700165 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
166#else
167 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
168#endif
169}
170
Elliott Hughesad88a082012-10-24 18:37:21 -0700171static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700172 dlopen("/child/thread", RTLD_NOW);
173 return reinterpret_cast<void*>(strdup(dlerror()));
174}
175
Elliott Hughese66190d2012-12-18 15:57:55 -0800176TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700177 dlopen("/main/thread", RTLD_NOW);
178 const char* main_thread_error = dlerror();
179 ASSERT_SUBSTR("/main/thread", main_thread_error);
180
181 pthread_t t;
182 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
183 void* result;
184 ASSERT_EQ(0, pthread_join(t, &result));
185 char* child_thread_error = static_cast<char*>(result);
186 ASSERT_SUBSTR("/child/thread", child_thread_error);
187 free(child_thread_error);
188
189 ASSERT_SUBSTR("/main/thread", main_thread_error);
190}
191
Elliott Hughese66190d2012-12-18 15:57:55 -0800192TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700193 dlerror(); // Clear any pending errors.
Elliott Hughes8e15b082012-09-26 11:44:01 -0700194 void* self = dlopen(NULL, RTLD_NOW);
195 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700196 ASSERT_TRUE(dlerror() == NULL);
197
198 void* sym;
199
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700200#if defined(__BIONIC__) && !defined(__LP64__)
201 // RTLD_DEFAULT in lp32 bionic is not (void*)0
202 // so it can be distinguished from the NULL handle.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700203 sym = dlsym(NULL, "test");
204 ASSERT_TRUE(sym == NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700205 ASSERT_SUBSTR("dlsym library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700206#endif
207
208 // NULL symbol name.
Elliott Hughes063525c2014-05-13 11:19:57 -0700209#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700210 // glibc marks this parameter non-null and SEGVs if you cheat.
211 sym = dlsym(self, NULL);
212 ASSERT_TRUE(sym == NULL);
213 ASSERT_SUBSTR("", dlerror());
214#endif
215
216 // Symbol that doesn't exist.
217 sym = dlsym(self, "ThisSymbolDoesNotExist");
218 ASSERT_TRUE(sym == NULL);
219 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700220
221 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700222}
223
Elliott Hughese66190d2012-12-18 15:57:55 -0800224TEST(dlfcn, dladdr) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700225 dlerror(); // Clear any pending errors.
226 void* self = dlopen(NULL, RTLD_NOW);
227 ASSERT_TRUE(self != NULL);
228 ASSERT_TRUE(dlerror() == NULL);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700229
230 void* sym = dlsym(self, "DlSymTestFunction");
231 ASSERT_TRUE(sym != NULL);
232
233 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
234 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
235
236 Dl_info info;
237 int rc = dladdr(addr, &info);
238 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
239
240 // Get the name of this executable.
241 char executable_path[PATH_MAX];
242 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
243 ASSERT_NE(rc, -1);
244 executable_path[rc] = '\0';
245 std::string executable_name(basename(executable_path));
246
247 // The filename should be that of this executable.
248 // Note that we don't know whether or not we have the full path, so we want an "ends_with" test.
249 std::string dli_fname(info.dli_fname);
250 dli_fname = basename(&dli_fname[0]);
251 ASSERT_EQ(dli_fname, executable_name);
252
253 // The symbol name should be the symbol we looked up.
254 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
255
256 // The address should be the exact address of the symbol.
257 ASSERT_EQ(info.dli_saddr, sym);
258
259 // Look in /proc/pid/maps to find out what address we were loaded at.
260 // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic.
261 void* base_address = NULL;
Elliott Hughes8e15b082012-09-26 11:44:01 -0700262 char line[BUFSIZ];
Elliott Hughes57b7a612014-08-25 17:26:50 -0700263 FILE* fp = fopen("/proc/self/maps", "r");
Elliott Hughes8e15b082012-09-26 11:44:01 -0700264 ASSERT_TRUE(fp != NULL);
265 while (fgets(line, sizeof(line), fp) != NULL) {
266 uintptr_t start = strtoul(line, 0, 16);
267 line[strlen(line) - 1] = '\0'; // Chomp the '\n'.
268 char* path = strchr(line, '/');
Elliott Hughes156da962012-10-09 17:14:56 -0700269 if (path != NULL && strcmp(executable_path, path) == 0) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700270 base_address = reinterpret_cast<void*>(start);
271 break;
272 }
273 }
274 fclose(fp);
275
276 // The base address should be the address we were loaded at.
277 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700278
279 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700280}
281
Elliott Hughese66190d2012-12-18 15:57:55 -0800282TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700283 Dl_info info;
284
Elliott Hughes3b297c42012-10-11 16:08:51 -0700285 dlerror(); // Clear any pending errors.
286
Elliott Hughes8e15b082012-09-26 11:44:01 -0700287 // No symbol corresponding to NULL.
288 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700289 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700290
291 // No symbol corresponding to a stack address.
292 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700293 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700294}
Elliott Hughes124fae92012-10-31 14:20:03 -0700295
Elliott Hughes124fae92012-10-31 14:20:03 -0700296// Our dynamic linker doesn't support GNU hash tables.
Elliott Hughesa43e9062013-01-07 14:18:22 -0800297#if defined(__BIONIC__)
298// GNU-style ELF hash tables are incompatible with the MIPS ABI.
299// MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code.
300#if !defined(__mips__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800301TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Elliott Hughes124fae92012-10-31 14:20:03 -0700302 dlerror(); // Clear any pending errors.
Elliott Hughes6e33b022012-11-07 18:16:02 -0800303 void* handle = dlopen("no-elf-hash-table-library.so", RTLD_NOW);
Elliott Hughes124fae92012-10-31 14:20:03 -0700304 ASSERT_TRUE(handle == NULL);
Elliott Hughes6e33b022012-11-07 18:16:02 -0800305 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 -0700306}
307#endif
Elliott Hughesa43e9062013-01-07 14:18:22 -0800308#endif
Elliott Hughese66190d2012-12-18 15:57:55 -0800309
310TEST(dlfcn, dlopen_bad_flags) {
311 dlerror(); // Clear any pending errors.
312 void* handle;
313
Elliott Hughes063525c2014-05-13 11:19:57 -0700314#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800315 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
316 handle = dlopen(NULL, 0);
317 ASSERT_TRUE(handle == NULL);
318 ASSERT_SUBSTR("invalid", dlerror());
319#endif
320
321 handle = dlopen(NULL, 0xffffffff);
322 ASSERT_TRUE(handle == NULL);
323 ASSERT_SUBSTR("invalid", dlerror());
324
325 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
326 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY);
327 ASSERT_TRUE(handle != NULL);
328 ASSERT_SUBSTR(NULL, dlerror());
329}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400330
331TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800332 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400333 ASSERT_TRUE(addr == NULL);
334}
335
336TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800337 void* addr = dlsym(RTLD_DEFAULT, "fopen");
338 ASSERT_TRUE(addr != NULL);
339}
340
341TEST(dlfcn, rtld_next_unknown_symbol) {
342 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
343 ASSERT_TRUE(addr == NULL);
344}
345
346TEST(dlfcn, rtld_next_known_symbol) {
347 void* addr = dlsym(RTLD_NEXT, "fopen");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400348 ASSERT_TRUE(addr != NULL);
349}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700350
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700351TEST(dlfcn, dlsym_weak_func) {
352 dlerror();
353 void* handle = dlopen("libtest_dlsym_weak_func.so",RTLD_NOW);
354 ASSERT_TRUE(handle != NULL);
355
356 int (*weak_func)();
357 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
358 ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror();
359 EXPECT_EQ(42, weak_func());
360 dlclose(handle);
361}
362
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700363TEST(dlfcn, dlopen_symlink) {
364 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
365 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
366 ASSERT_TRUE(handle1 != NULL);
367 ASSERT_TRUE(handle2 != NULL);
368 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -0700369 dlclose(handle1);
370 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700371}