blob: a79263da5878686e16c709103f4f621fc9eb4877 [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
Elliott Hughese66190d2012-12-18 15:57:55 -080035TEST(dlfcn, dlsym_in_self) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070036 dlerror(); // Clear any pending errors.
jeffhaoacf5aa72012-09-12 17:25:30 -070037 void* self = dlopen(NULL, RTLD_NOW);
38 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -070039 ASSERT_TRUE(dlerror() == NULL);
jeffhaoacf5aa72012-09-12 17:25:30 -070040
41 void* sym = dlsym(self, "DlSymTestFunction");
42 ASSERT_TRUE(sym != NULL);
43
44 void (*function)() = reinterpret_cast<void(*)()>(sym);
45
Elliott Hughes1728b232014-05-14 10:02:03 -070046 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070047 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070048 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070049
50 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070051}
Elliott Hughes8e15b082012-09-26 11:44:01 -070052
Dmitriy Ivanovce0ba3c2014-07-01 19:09:49 -070053#if !defined(__LP64__)
54// Current compiler/static linker used for aarch64
55// platform optimizes LOCAL PROTECTED symbol
56// in libtest_local_symbol.so out of existence
Dmitriy Ivanovd97e9f52014-06-29 12:28:37 -070057TEST(dlfcn, dlsym_local_symbol) {
58 void* handle = dlopen("libtest_local_symbol.so", RTLD_NOW);
59 ASSERT_TRUE(handle != NULL);
60 dlerror();
61 void* sym = dlsym(handle, "private_taxicab_number");
62 ASSERT_TRUE(sym == NULL);
63 ASSERT_STREQ("undefined symbol: private_taxicab_number", dlerror());
64
65 uint32_t (*f)(void);
66 f = reinterpret_cast<uint32_t (*)(void)>(dlsym(handle, "dlsym_local_symbol_get_taxicab_number_using_dlsym"));
67 ASSERT_TRUE(f != NULL);
Elliott Hughesd06ee1d2014-07-01 17:17:46 -070068 ASSERT_EQ(1729U, f());
Dmitriy Ivanovd97e9f52014-06-29 12:28:37 -070069}
Dmitriy Ivanovce0ba3c2014-07-01 19:09:49 -070070#endif
Dmitriy Ivanovd97e9f52014-06-29 12:28:37 -070071
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -070072TEST(dlfcn, dlopen_noload) {
73 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
74 ASSERT_TRUE(handle == NULL);
75 handle = dlopen("libtest_simple.so", RTLD_NOW);
76 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
77 ASSERT_TRUE(handle != NULL);
78 ASSERT_TRUE(handle2 != NULL);
79 ASSERT_TRUE(handle == handle2);
80 ASSERT_EQ(0, dlclose(handle));
81 ASSERT_EQ(0, dlclose(handle2));
82}
83
Elliott Hughese66190d2012-12-18 15:57:55 -080084TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070085 void* self = dlopen("/does/not/exist", RTLD_NOW);
86 ASSERT_TRUE(self == NULL);
Elliott Hughes063525c2014-05-13 11:19:57 -070087#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -070088 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
89#else
90 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
91#endif
92}
93
Elliott Hughesad88a082012-10-24 18:37:21 -070094static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -070095 dlopen("/child/thread", RTLD_NOW);
96 return reinterpret_cast<void*>(strdup(dlerror()));
97}
98
Elliott Hughese66190d2012-12-18 15:57:55 -080099TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700100 dlopen("/main/thread", RTLD_NOW);
101 const char* main_thread_error = dlerror();
102 ASSERT_SUBSTR("/main/thread", main_thread_error);
103
104 pthread_t t;
105 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
106 void* result;
107 ASSERT_EQ(0, pthread_join(t, &result));
108 char* child_thread_error = static_cast<char*>(result);
109 ASSERT_SUBSTR("/child/thread", child_thread_error);
110 free(child_thread_error);
111
112 ASSERT_SUBSTR("/main/thread", main_thread_error);
113}
114
Elliott Hughese66190d2012-12-18 15:57:55 -0800115TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700116 dlerror(); // Clear any pending errors.
Elliott Hughes8e15b082012-09-26 11:44:01 -0700117 void* self = dlopen(NULL, RTLD_NOW);
118 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700119 ASSERT_TRUE(dlerror() == NULL);
120
121 void* sym;
122
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700123#if defined(__BIONIC__) && !defined(__LP64__)
124 // RTLD_DEFAULT in lp32 bionic is not (void*)0
125 // so it can be distinguished from the NULL handle.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700126 sym = dlsym(NULL, "test");
127 ASSERT_TRUE(sym == NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700128 ASSERT_SUBSTR("dlsym library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700129#endif
130
131 // NULL symbol name.
Elliott Hughes063525c2014-05-13 11:19:57 -0700132#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700133 // glibc marks this parameter non-null and SEGVs if you cheat.
134 sym = dlsym(self, NULL);
135 ASSERT_TRUE(sym == NULL);
136 ASSERT_SUBSTR("", dlerror());
137#endif
138
139 // Symbol that doesn't exist.
140 sym = dlsym(self, "ThisSymbolDoesNotExist");
141 ASSERT_TRUE(sym == NULL);
142 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700143
144 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700145}
146
Elliott Hughese66190d2012-12-18 15:57:55 -0800147TEST(dlfcn, dladdr) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700148 dlerror(); // Clear any pending errors.
149 void* self = dlopen(NULL, RTLD_NOW);
150 ASSERT_TRUE(self != NULL);
151 ASSERT_TRUE(dlerror() == NULL);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700152
153 void* sym = dlsym(self, "DlSymTestFunction");
154 ASSERT_TRUE(sym != NULL);
155
156 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
157 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
158
159 Dl_info info;
160 int rc = dladdr(addr, &info);
161 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
162
163 // Get the name of this executable.
164 char executable_path[PATH_MAX];
165 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
166 ASSERT_NE(rc, -1);
167 executable_path[rc] = '\0';
168 std::string executable_name(basename(executable_path));
169
170 // The filename should be that of this executable.
171 // Note that we don't know whether or not we have the full path, so we want an "ends_with" test.
172 std::string dli_fname(info.dli_fname);
173 dli_fname = basename(&dli_fname[0]);
174 ASSERT_EQ(dli_fname, executable_name);
175
176 // The symbol name should be the symbol we looked up.
177 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
178
179 // The address should be the exact address of the symbol.
180 ASSERT_EQ(info.dli_saddr, sym);
181
182 // Look in /proc/pid/maps to find out what address we were loaded at.
183 // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic.
184 void* base_address = NULL;
185 char path[PATH_MAX];
186 snprintf(path, sizeof(path), "/proc/%d/maps", getpid());
187 char line[BUFSIZ];
188 FILE* fp = fopen(path, "r");
189 ASSERT_TRUE(fp != NULL);
190 while (fgets(line, sizeof(line), fp) != NULL) {
191 uintptr_t start = strtoul(line, 0, 16);
192 line[strlen(line) - 1] = '\0'; // Chomp the '\n'.
193 char* path = strchr(line, '/');
Elliott Hughes156da962012-10-09 17:14:56 -0700194 if (path != NULL && strcmp(executable_path, path) == 0) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700195 base_address = reinterpret_cast<void*>(start);
196 break;
197 }
198 }
199 fclose(fp);
200
201 // The base address should be the address we were loaded at.
202 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700203
204 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700205}
206
Elliott Hughese66190d2012-12-18 15:57:55 -0800207TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700208 Dl_info info;
209
Elliott Hughes3b297c42012-10-11 16:08:51 -0700210 dlerror(); // Clear any pending errors.
211
Elliott Hughes8e15b082012-09-26 11:44:01 -0700212 // No symbol corresponding to NULL.
213 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700214 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700215
216 // No symbol corresponding to a stack address.
217 ASSERT_EQ(dladdr(&info, &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}
Elliott Hughes124fae92012-10-31 14:20:03 -0700220
Elliott Hughes124fae92012-10-31 14:20:03 -0700221// Our dynamic linker doesn't support GNU hash tables.
Elliott Hughesa43e9062013-01-07 14:18:22 -0800222#if defined(__BIONIC__)
223// GNU-style ELF hash tables are incompatible with the MIPS ABI.
224// MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code.
225#if !defined(__mips__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800226TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Elliott Hughes124fae92012-10-31 14:20:03 -0700227 dlerror(); // Clear any pending errors.
Elliott Hughes6e33b022012-11-07 18:16:02 -0800228 void* handle = dlopen("no-elf-hash-table-library.so", RTLD_NOW);
Elliott Hughes124fae92012-10-31 14:20:03 -0700229 ASSERT_TRUE(handle == NULL);
Elliott Hughes6e33b022012-11-07 18:16:02 -0800230 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 -0700231}
232#endif
Elliott Hughesa43e9062013-01-07 14:18:22 -0800233#endif
Elliott Hughese66190d2012-12-18 15:57:55 -0800234
235TEST(dlfcn, dlopen_bad_flags) {
236 dlerror(); // Clear any pending errors.
237 void* handle;
238
Elliott Hughes063525c2014-05-13 11:19:57 -0700239#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800240 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
241 handle = dlopen(NULL, 0);
242 ASSERT_TRUE(handle == NULL);
243 ASSERT_SUBSTR("invalid", dlerror());
244#endif
245
246 handle = dlopen(NULL, 0xffffffff);
247 ASSERT_TRUE(handle == NULL);
248 ASSERT_SUBSTR("invalid", dlerror());
249
250 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
251 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY);
252 ASSERT_TRUE(handle != NULL);
253 ASSERT_SUBSTR(NULL, dlerror());
254}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400255
256TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800257 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400258 ASSERT_TRUE(addr == NULL);
259}
260
261TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800262 void* addr = dlsym(RTLD_DEFAULT, "fopen");
263 ASSERT_TRUE(addr != NULL);
264}
265
266TEST(dlfcn, rtld_next_unknown_symbol) {
267 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
268 ASSERT_TRUE(addr == NULL);
269}
270
271TEST(dlfcn, rtld_next_known_symbol) {
272 void* addr = dlsym(RTLD_NEXT, "fopen");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400273 ASSERT_TRUE(addr != NULL);
274}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700275
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700276TEST(dlfcn, dlsym_weak_func) {
277 dlerror();
278 void* handle = dlopen("libtest_dlsym_weak_func.so",RTLD_NOW);
279 ASSERT_TRUE(handle != NULL);
280
281 int (*weak_func)();
282 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
283 ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror();
284 EXPECT_EQ(42, weak_func());
285 dlclose(handle);
286}
287
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700288TEST(dlfcn, dlopen_symlink) {
289 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
290 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
291 ASSERT_TRUE(handle1 != NULL);
292 ASSERT_TRUE(handle2 != NULL);
293 ASSERT_EQ(handle1, handle2);
294}