dlfcn_test.cpp revision ce0ba3c70634d5fe64b9a298d8a305d85bd1e6ac
1/*
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>
20#include <libgen.h>
21#include <limits.h>
22#include <stdio.h>
23#include <stdint.h>
24
25#include <string>
26
27#define ASSERT_SUBSTR(needle, haystack) \
28    ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
29
30static bool g_called = false;
31extern "C" void DlSymTestFunction() {
32  g_called = true;
33}
34
35TEST(dlfcn, dlsym_in_self) {
36  dlerror(); // Clear any pending errors.
37  void* self = dlopen(NULL, RTLD_NOW);
38  ASSERT_TRUE(self != NULL);
39  ASSERT_TRUE(dlerror() == NULL);
40
41  void* sym = dlsym(self, "DlSymTestFunction");
42  ASSERT_TRUE(sym != NULL);
43
44  void (*function)() = reinterpret_cast<void(*)()>(sym);
45
46  g_called = false;
47  function();
48  ASSERT_TRUE(g_called);
49
50  ASSERT_EQ(0, dlclose(self));
51}
52
53#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
57TEST(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);
68  ASSERT_EQ(1729U, f());
69}
70#endif
71
72TEST(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
84TEST(dlfcn, dlopen_failure) {
85  void* self = dlopen("/does/not/exist", RTLD_NOW);
86  ASSERT_TRUE(self == NULL);
87#if defined(__BIONIC__)
88  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
94static void* ConcurrentDlErrorFn(void*) {
95  dlopen("/child/thread", RTLD_NOW);
96  return reinterpret_cast<void*>(strdup(dlerror()));
97}
98
99TEST(dlfcn, dlerror_concurrent) {
100  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
115TEST(dlfcn, dlsym_failures) {
116  dlerror(); // Clear any pending errors.
117  void* self = dlopen(NULL, RTLD_NOW);
118  ASSERT_TRUE(self != NULL);
119  ASSERT_TRUE(dlerror() == NULL);
120
121  void* sym;
122
123#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.
126  sym = dlsym(NULL, "test");
127  ASSERT_TRUE(sym == NULL);
128  ASSERT_SUBSTR("dlsym library handle is null", dlerror());
129#endif
130
131  // NULL symbol name.
132#if defined(__BIONIC__)
133  // 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());
143
144  ASSERT_EQ(0, dlclose(self));
145}
146
147TEST(dlfcn, dladdr) {
148  dlerror(); // Clear any pending errors.
149  void* self = dlopen(NULL, RTLD_NOW);
150  ASSERT_TRUE(self != NULL);
151  ASSERT_TRUE(dlerror() == NULL);
152
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, '/');
194    if (path != NULL && strcmp(executable_path, path) == 0) {
195      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);
203
204  ASSERT_EQ(0, dlclose(self));
205}
206
207TEST(dlfcn, dladdr_invalid) {
208  Dl_info info;
209
210  dlerror(); // Clear any pending errors.
211
212  // No symbol corresponding to NULL.
213  ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
214  ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
215
216  // No symbol corresponding to a stack address.
217  ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
218  ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
219}
220
221// Our dynamic linker doesn't support GNU hash tables.
222#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__)
226TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
227  dlerror(); // Clear any pending errors.
228  void* handle = dlopen("no-elf-hash-table-library.so", RTLD_NOW);
229  ASSERT_TRUE(handle == NULL);
230  ASSERT_STREQ("dlopen failed: empty/missing DT_HASH in \"no-elf-hash-table-library.so\" (built with --hash-style=gnu?)", dlerror());
231}
232#endif
233#endif
234
235TEST(dlfcn, dlopen_bad_flags) {
236  dlerror(); // Clear any pending errors.
237  void* handle;
238
239#if defined(__GLIBC__)
240  // 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}
255
256TEST(dlfcn, rtld_default_unknown_symbol) {
257  void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
258  ASSERT_TRUE(addr == NULL);
259}
260
261TEST(dlfcn, rtld_default_known_symbol) {
262  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");
273  ASSERT_TRUE(addr != NULL);
274}
275
276TEST(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
288TEST(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}
295