blob: 457fcd5c08e178f11b6468132ca99c9d7dffa5d0 [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
Elliott Hughese66190d2012-12-18 15:57:55 -080090TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070091 void* self = dlopen("/does/not/exist", RTLD_NOW);
92 ASSERT_TRUE(self == NULL);
Elliott Hughes063525c2014-05-13 11:19:57 -070093#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -070094 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
95#else
96 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
97#endif
98}
99
Elliott Hughesad88a082012-10-24 18:37:21 -0700100static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700101 dlopen("/child/thread", RTLD_NOW);
102 return reinterpret_cast<void*>(strdup(dlerror()));
103}
104
Elliott Hughese66190d2012-12-18 15:57:55 -0800105TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700106 dlopen("/main/thread", RTLD_NOW);
107 const char* main_thread_error = dlerror();
108 ASSERT_SUBSTR("/main/thread", main_thread_error);
109
110 pthread_t t;
111 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
112 void* result;
113 ASSERT_EQ(0, pthread_join(t, &result));
114 char* child_thread_error = static_cast<char*>(result);
115 ASSERT_SUBSTR("/child/thread", child_thread_error);
116 free(child_thread_error);
117
118 ASSERT_SUBSTR("/main/thread", main_thread_error);
119}
120
Elliott Hughese66190d2012-12-18 15:57:55 -0800121TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700122 dlerror(); // Clear any pending errors.
Elliott Hughes8e15b082012-09-26 11:44:01 -0700123 void* self = dlopen(NULL, RTLD_NOW);
124 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700125 ASSERT_TRUE(dlerror() == NULL);
126
127 void* sym;
128
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700129#if defined(__BIONIC__) && !defined(__LP64__)
130 // RTLD_DEFAULT in lp32 bionic is not (void*)0
131 // so it can be distinguished from the NULL handle.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700132 sym = dlsym(NULL, "test");
133 ASSERT_TRUE(sym == NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700134 ASSERT_SUBSTR("dlsym library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700135#endif
136
137 // NULL symbol name.
Elliott Hughes063525c2014-05-13 11:19:57 -0700138#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700139 // glibc marks this parameter non-null and SEGVs if you cheat.
140 sym = dlsym(self, NULL);
141 ASSERT_TRUE(sym == NULL);
142 ASSERT_SUBSTR("", dlerror());
143#endif
144
145 // Symbol that doesn't exist.
146 sym = dlsym(self, "ThisSymbolDoesNotExist");
147 ASSERT_TRUE(sym == NULL);
148 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700149
150 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700151}
152
Elliott Hughese66190d2012-12-18 15:57:55 -0800153TEST(dlfcn, dladdr) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700154 dlerror(); // Clear any pending errors.
155 void* self = dlopen(NULL, RTLD_NOW);
156 ASSERT_TRUE(self != NULL);
157 ASSERT_TRUE(dlerror() == NULL);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700158
159 void* sym = dlsym(self, "DlSymTestFunction");
160 ASSERT_TRUE(sym != NULL);
161
162 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
163 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
164
165 Dl_info info;
166 int rc = dladdr(addr, &info);
167 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
168
169 // Get the name of this executable.
170 char executable_path[PATH_MAX];
171 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
172 ASSERT_NE(rc, -1);
173 executable_path[rc] = '\0';
174 std::string executable_name(basename(executable_path));
175
176 // The filename should be that of this executable.
177 // Note that we don't know whether or not we have the full path, so we want an "ends_with" test.
178 std::string dli_fname(info.dli_fname);
179 dli_fname = basename(&dli_fname[0]);
180 ASSERT_EQ(dli_fname, executable_name);
181
182 // The symbol name should be the symbol we looked up.
183 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
184
185 // The address should be the exact address of the symbol.
186 ASSERT_EQ(info.dli_saddr, sym);
187
188 // Look in /proc/pid/maps to find out what address we were loaded at.
189 // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic.
190 void* base_address = NULL;
Elliott Hughes8e15b082012-09-26 11:44:01 -0700191 char line[BUFSIZ];
Elliott Hughes64218232014-08-25 17:26:50 -0700192 FILE* fp = fopen("/proc/self/maps", "r");
Elliott Hughes8e15b082012-09-26 11:44:01 -0700193 ASSERT_TRUE(fp != NULL);
194 while (fgets(line, sizeof(line), fp) != NULL) {
195 uintptr_t start = strtoul(line, 0, 16);
196 line[strlen(line) - 1] = '\0'; // Chomp the '\n'.
197 char* path = strchr(line, '/');
Elliott Hughes156da962012-10-09 17:14:56 -0700198 if (path != NULL && strcmp(executable_path, path) == 0) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700199 base_address = reinterpret_cast<void*>(start);
200 break;
201 }
202 }
203 fclose(fp);
204
205 // The base address should be the address we were loaded at.
206 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700207
208 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700209}
210
Elliott Hughese66190d2012-12-18 15:57:55 -0800211TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700212 Dl_info info;
213
Elliott Hughes3b297c42012-10-11 16:08:51 -0700214 dlerror(); // Clear any pending errors.
215
Elliott Hughes8e15b082012-09-26 11:44:01 -0700216 // No symbol corresponding to NULL.
217 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700218 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700219
220 // No symbol corresponding to a stack address.
221 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700222 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700223}
Elliott Hughes124fae92012-10-31 14:20:03 -0700224
Elliott Hughes124fae92012-10-31 14:20:03 -0700225// Our dynamic linker doesn't support GNU hash tables.
Elliott Hughesa43e9062013-01-07 14:18:22 -0800226#if defined(__BIONIC__)
227// GNU-style ELF hash tables are incompatible with the MIPS ABI.
228// MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code.
229#if !defined(__mips__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800230TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Elliott Hughes124fae92012-10-31 14:20:03 -0700231 dlerror(); // Clear any pending errors.
Elliott Hughes6e33b022012-11-07 18:16:02 -0800232 void* handle = dlopen("no-elf-hash-table-library.so", RTLD_NOW);
Elliott Hughes124fae92012-10-31 14:20:03 -0700233 ASSERT_TRUE(handle == NULL);
Elliott Hughes6e33b022012-11-07 18:16:02 -0800234 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 -0700235}
236#endif
Elliott Hughesa43e9062013-01-07 14:18:22 -0800237#endif
Elliott Hughese66190d2012-12-18 15:57:55 -0800238
239TEST(dlfcn, dlopen_bad_flags) {
240 dlerror(); // Clear any pending errors.
241 void* handle;
242
Elliott Hughes063525c2014-05-13 11:19:57 -0700243#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800244 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
245 handle = dlopen(NULL, 0);
246 ASSERT_TRUE(handle == NULL);
247 ASSERT_SUBSTR("invalid", dlerror());
248#endif
249
250 handle = dlopen(NULL, 0xffffffff);
251 ASSERT_TRUE(handle == NULL);
252 ASSERT_SUBSTR("invalid", dlerror());
253
254 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
255 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY);
256 ASSERT_TRUE(handle != NULL);
257 ASSERT_SUBSTR(NULL, dlerror());
258}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400259
260TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800261 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400262 ASSERT_TRUE(addr == NULL);
263}
264
265TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800266 void* addr = dlsym(RTLD_DEFAULT, "fopen");
267 ASSERT_TRUE(addr != NULL);
268}
269
270TEST(dlfcn, rtld_next_unknown_symbol) {
271 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
272 ASSERT_TRUE(addr == NULL);
273}
274
275TEST(dlfcn, rtld_next_known_symbol) {
276 void* addr = dlsym(RTLD_NEXT, "fopen");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400277 ASSERT_TRUE(addr != NULL);
278}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700279
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700280TEST(dlfcn, dlsym_weak_func) {
281 dlerror();
282 void* handle = dlopen("libtest_dlsym_weak_func.so",RTLD_NOW);
283 ASSERT_TRUE(handle != NULL);
284
285 int (*weak_func)();
286 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
287 ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror();
288 EXPECT_EQ(42, weak_func());
289 dlclose(handle);
290}
291
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700292TEST(dlfcn, dlopen_symlink) {
293 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
294 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
295 ASSERT_TRUE(handle1 != NULL);
296 ASSERT_TRUE(handle2 != NULL);
297 ASSERT_EQ(handle1, handle2);
298}