blob: 260cbd601516dd66d5c817ee534e18bffbd5d753 [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 +000065TEST(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 Smith31b88da2014-07-23 11:22:25 -070090// ifuncs are only supported on intel for now
91#if defined(__i386__) || defined(__x86_64__)
92TEST(dlfcn, ifunc) {
93 const char* (*foo_ptr)();
94 const char* (*foo_library_ptr)();
95
96 // ifunc's choice depends on whether IFUNC_CHOICE has a value
97 // first check the set case
98 setenv("IFUNC_CHOICE", "set", 1);
99 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
100 ASSERT_TRUE(handle != NULL);
101 *(void **)(&foo_ptr) = dlsym(handle, "foo");
102 *(void **)(&foo_library_ptr) = dlsym(handle, "foo_library");
103 ASSERT_TRUE(foo_ptr != NULL);
104 ASSERT_TRUE(foo_library_ptr != NULL);
105 ASSERT_EQ(strncmp("set", (*foo_ptr)(), 3), 0);
106 ASSERT_EQ(strncmp("set", (*foo_library_ptr)(), 3), 0);
107 dlclose(handle);
108
109 // then check the unset case
110 unsetenv("IFUNC_CHOICE");
111 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
112 ASSERT_TRUE(handle != NULL);
113 *(void **)(&foo_ptr) = dlsym(handle, "foo");
114 *(void **)(&foo_library_ptr) = dlsym(handle, "foo_library");
115 ASSERT_TRUE(foo_ptr != NULL);
116 ASSERT_TRUE(foo_library_ptr != NULL);
117 ASSERT_EQ(strncmp("unset", (*foo_ptr)(), 5), 0);
118 ASSERT_EQ(strncmp("unset", (*foo_library_ptr)(), 3), 0);
119 dlclose(handle);
120}
121#endif
122
Elliott Hughese66190d2012-12-18 15:57:55 -0800123TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700124 void* self = dlopen("/does/not/exist", RTLD_NOW);
125 ASSERT_TRUE(self == NULL);
Elliott Hughes063525c2014-05-13 11:19:57 -0700126#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700127 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
128#else
129 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
130#endif
131}
132
Elliott Hughesad88a082012-10-24 18:37:21 -0700133static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700134 dlopen("/child/thread", RTLD_NOW);
135 return reinterpret_cast<void*>(strdup(dlerror()));
136}
137
Elliott Hughese66190d2012-12-18 15:57:55 -0800138TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700139 dlopen("/main/thread", RTLD_NOW);
140 const char* main_thread_error = dlerror();
141 ASSERT_SUBSTR("/main/thread", main_thread_error);
142
143 pthread_t t;
144 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
145 void* result;
146 ASSERT_EQ(0, pthread_join(t, &result));
147 char* child_thread_error = static_cast<char*>(result);
148 ASSERT_SUBSTR("/child/thread", child_thread_error);
149 free(child_thread_error);
150
151 ASSERT_SUBSTR("/main/thread", main_thread_error);
152}
153
Elliott Hughese66190d2012-12-18 15:57:55 -0800154TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700155 dlerror(); // Clear any pending errors.
Elliott Hughes8e15b082012-09-26 11:44:01 -0700156 void* self = dlopen(NULL, RTLD_NOW);
157 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700158 ASSERT_TRUE(dlerror() == NULL);
159
160 void* sym;
161
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700162#if defined(__BIONIC__) && !defined(__LP64__)
163 // RTLD_DEFAULT in lp32 bionic is not (void*)0
164 // so it can be distinguished from the NULL handle.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700165 sym = dlsym(NULL, "test");
166 ASSERT_TRUE(sym == NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700167 ASSERT_SUBSTR("dlsym library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700168#endif
169
170 // NULL symbol name.
Elliott Hughes063525c2014-05-13 11:19:57 -0700171#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700172 // glibc marks this parameter non-null and SEGVs if you cheat.
173 sym = dlsym(self, NULL);
174 ASSERT_TRUE(sym == NULL);
175 ASSERT_SUBSTR("", dlerror());
176#endif
177
178 // Symbol that doesn't exist.
179 sym = dlsym(self, "ThisSymbolDoesNotExist");
180 ASSERT_TRUE(sym == NULL);
181 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700182
183 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700184}
185
Elliott Hughese66190d2012-12-18 15:57:55 -0800186TEST(dlfcn, dladdr) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700187 dlerror(); // Clear any pending errors.
188 void* self = dlopen(NULL, RTLD_NOW);
189 ASSERT_TRUE(self != NULL);
190 ASSERT_TRUE(dlerror() == NULL);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700191
192 void* sym = dlsym(self, "DlSymTestFunction");
193 ASSERT_TRUE(sym != NULL);
194
195 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
196 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
197
198 Dl_info info;
199 int rc = dladdr(addr, &info);
200 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
201
202 // Get the name of this executable.
203 char executable_path[PATH_MAX];
204 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
205 ASSERT_NE(rc, -1);
206 executable_path[rc] = '\0';
207 std::string executable_name(basename(executable_path));
208
209 // The filename should be that of this executable.
210 // Note that we don't know whether or not we have the full path, so we want an "ends_with" test.
211 std::string dli_fname(info.dli_fname);
212 dli_fname = basename(&dli_fname[0]);
213 ASSERT_EQ(dli_fname, executable_name);
214
215 // The symbol name should be the symbol we looked up.
216 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
217
218 // The address should be the exact address of the symbol.
219 ASSERT_EQ(info.dli_saddr, sym);
220
221 // Look in /proc/pid/maps to find out what address we were loaded at.
222 // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic.
223 void* base_address = NULL;
Elliott Hughes8e15b082012-09-26 11:44:01 -0700224 char line[BUFSIZ];
Elliott Hughes64218232014-08-25 17:26:50 -0700225 FILE* fp = fopen("/proc/self/maps", "r");
Elliott Hughes8e15b082012-09-26 11:44:01 -0700226 ASSERT_TRUE(fp != NULL);
227 while (fgets(line, sizeof(line), fp) != NULL) {
228 uintptr_t start = strtoul(line, 0, 16);
229 line[strlen(line) - 1] = '\0'; // Chomp the '\n'.
230 char* path = strchr(line, '/');
Elliott Hughes156da962012-10-09 17:14:56 -0700231 if (path != NULL && strcmp(executable_path, path) == 0) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700232 base_address = reinterpret_cast<void*>(start);
233 break;
234 }
235 }
236 fclose(fp);
237
238 // The base address should be the address we were loaded at.
239 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700240
241 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700242}
243
Elliott Hughese66190d2012-12-18 15:57:55 -0800244TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700245 Dl_info info;
246
Elliott Hughes3b297c42012-10-11 16:08:51 -0700247 dlerror(); // Clear any pending errors.
248
Elliott Hughes8e15b082012-09-26 11:44:01 -0700249 // No symbol corresponding to NULL.
250 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700251 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700252
253 // No symbol corresponding to a stack address.
254 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700255 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700256}
Elliott Hughes124fae92012-10-31 14:20:03 -0700257
Elliott Hughes124fae92012-10-31 14:20:03 -0700258// Our dynamic linker doesn't support GNU hash tables.
Elliott Hughesa43e9062013-01-07 14:18:22 -0800259#if defined(__BIONIC__)
260// GNU-style ELF hash tables are incompatible with the MIPS ABI.
261// MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code.
262#if !defined(__mips__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800263TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Elliott Hughes124fae92012-10-31 14:20:03 -0700264 dlerror(); // Clear any pending errors.
Elliott Hughes6e33b022012-11-07 18:16:02 -0800265 void* handle = dlopen("no-elf-hash-table-library.so", RTLD_NOW);
Elliott Hughes124fae92012-10-31 14:20:03 -0700266 ASSERT_TRUE(handle == NULL);
Elliott Hughes6e33b022012-11-07 18:16:02 -0800267 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 -0700268}
269#endif
Elliott Hughesa43e9062013-01-07 14:18:22 -0800270#endif
Elliott Hughese66190d2012-12-18 15:57:55 -0800271
272TEST(dlfcn, dlopen_bad_flags) {
273 dlerror(); // Clear any pending errors.
274 void* handle;
275
Elliott Hughes063525c2014-05-13 11:19:57 -0700276#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800277 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
278 handle = dlopen(NULL, 0);
279 ASSERT_TRUE(handle == NULL);
280 ASSERT_SUBSTR("invalid", dlerror());
281#endif
282
283 handle = dlopen(NULL, 0xffffffff);
284 ASSERT_TRUE(handle == NULL);
285 ASSERT_SUBSTR("invalid", dlerror());
286
287 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
288 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY);
289 ASSERT_TRUE(handle != NULL);
290 ASSERT_SUBSTR(NULL, dlerror());
291}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400292
293TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800294 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400295 ASSERT_TRUE(addr == NULL);
296}
297
298TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800299 void* addr = dlsym(RTLD_DEFAULT, "fopen");
300 ASSERT_TRUE(addr != NULL);
301}
302
303TEST(dlfcn, rtld_next_unknown_symbol) {
304 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
305 ASSERT_TRUE(addr == NULL);
306}
307
308TEST(dlfcn, rtld_next_known_symbol) {
309 void* addr = dlsym(RTLD_NEXT, "fopen");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400310 ASSERT_TRUE(addr != NULL);
311}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700312
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700313TEST(dlfcn, dlsym_weak_func) {
314 dlerror();
315 void* handle = dlopen("libtest_dlsym_weak_func.so",RTLD_NOW);
316 ASSERT_TRUE(handle != NULL);
317
318 int (*weak_func)();
319 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
320 ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror();
321 EXPECT_EQ(42, weak_func());
322 dlclose(handle);
323}
324
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700325TEST(dlfcn, dlopen_symlink) {
326 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
327 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
328 ASSERT_TRUE(handle1 != NULL);
329 ASSERT_TRUE(handle2 != NULL);
330 ASSERT_EQ(handle1, handle2);
331}