blob: d38d8c5736aaa421db236fa53cb8b359cb2c5691 [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
jeffhaoacf5aa72012-09-12 17:25:30 -070030static bool gCalled = false;
31extern "C" void DlSymTestFunction() {
32 gCalled = true;
33}
34
35TEST(dlopen, 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
46 gCalled = false;
47 function();
48 ASSERT_TRUE(gCalled);
49}
Elliott Hughes8e15b082012-09-26 11:44:01 -070050
Elliott Hughes3b297c42012-10-11 16:08:51 -070051TEST(dlopen, dlopen_failure) {
52 void* self = dlopen("/does/not/exist", RTLD_NOW);
53 ASSERT_TRUE(self == NULL);
54#if __BIONIC__
55 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
56#else
57 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
58#endif
59}
60
Elliott Hughesad88a082012-10-24 18:37:21 -070061static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -070062 dlopen("/child/thread", RTLD_NOW);
63 return reinterpret_cast<void*>(strdup(dlerror()));
64}
65
66TEST(dlopen, dlerror_concurrent) {
67 dlopen("/main/thread", RTLD_NOW);
68 const char* main_thread_error = dlerror();
69 ASSERT_SUBSTR("/main/thread", main_thread_error);
70
71 pthread_t t;
72 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
73 void* result;
74 ASSERT_EQ(0, pthread_join(t, &result));
75 char* child_thread_error = static_cast<char*>(result);
76 ASSERT_SUBSTR("/child/thread", child_thread_error);
77 free(child_thread_error);
78
79 ASSERT_SUBSTR("/main/thread", main_thread_error);
80}
81
Elliott Hughes3b297c42012-10-11 16:08:51 -070082TEST(dlopen, dlsym_failures) {
83 dlerror(); // Clear any pending errors.
Elliott Hughes8e15b082012-09-26 11:44:01 -070084 void* self = dlopen(NULL, RTLD_NOW);
85 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -070086 ASSERT_TRUE(dlerror() == NULL);
87
88 void* sym;
89
90 // NULL handle.
91 sym = dlsym(NULL, "test");
92 ASSERT_TRUE(sym == NULL);
93#if __BIONIC__
94 ASSERT_SUBSTR("dlsym library handle is null", dlerror());
95#else
96 ASSERT_SUBSTR("undefined symbol: test", dlerror()); // glibc isn't specific about the failure.
97#endif
98
99 // NULL symbol name.
100#if __BIONIC__
101 // glibc marks this parameter non-null and SEGVs if you cheat.
102 sym = dlsym(self, NULL);
103 ASSERT_TRUE(sym == NULL);
104 ASSERT_SUBSTR("", dlerror());
105#endif
106
107 // Symbol that doesn't exist.
108 sym = dlsym(self, "ThisSymbolDoesNotExist");
109 ASSERT_TRUE(sym == NULL);
110 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
111}
112
113TEST(dlopen, dladdr) {
114 dlerror(); // Clear any pending errors.
115 void* self = dlopen(NULL, RTLD_NOW);
116 ASSERT_TRUE(self != NULL);
117 ASSERT_TRUE(dlerror() == NULL);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700118
119 void* sym = dlsym(self, "DlSymTestFunction");
120 ASSERT_TRUE(sym != NULL);
121
122 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
123 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
124
125 Dl_info info;
126 int rc = dladdr(addr, &info);
127 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
128
129 // Get the name of this executable.
130 char executable_path[PATH_MAX];
131 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
132 ASSERT_NE(rc, -1);
133 executable_path[rc] = '\0';
134 std::string executable_name(basename(executable_path));
135
136 // The filename should be that of this executable.
137 // Note that we don't know whether or not we have the full path, so we want an "ends_with" test.
138 std::string dli_fname(info.dli_fname);
139 dli_fname = basename(&dli_fname[0]);
140 ASSERT_EQ(dli_fname, executable_name);
141
142 // The symbol name should be the symbol we looked up.
143 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
144
145 // The address should be the exact address of the symbol.
146 ASSERT_EQ(info.dli_saddr, sym);
147
148 // Look in /proc/pid/maps to find out what address we were loaded at.
149 // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic.
150 void* base_address = NULL;
151 char path[PATH_MAX];
152 snprintf(path, sizeof(path), "/proc/%d/maps", getpid());
153 char line[BUFSIZ];
154 FILE* fp = fopen(path, "r");
155 ASSERT_TRUE(fp != NULL);
156 while (fgets(line, sizeof(line), fp) != NULL) {
157 uintptr_t start = strtoul(line, 0, 16);
158 line[strlen(line) - 1] = '\0'; // Chomp the '\n'.
159 char* path = strchr(line, '/');
Elliott Hughes156da962012-10-09 17:14:56 -0700160 if (path != NULL && strcmp(executable_path, path) == 0) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700161 base_address = reinterpret_cast<void*>(start);
162 break;
163 }
164 }
165 fclose(fp);
166
167 // The base address should be the address we were loaded at.
168 ASSERT_EQ(info.dli_fbase, base_address);
169}
170
171TEST(dlopen, dladdr_invalid) {
172 Dl_info info;
173
Elliott Hughes3b297c42012-10-11 16:08:51 -0700174 dlerror(); // Clear any pending errors.
175
Elliott Hughes8e15b082012-09-26 11:44:01 -0700176 // No symbol corresponding to NULL.
177 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700178 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700179
180 // No symbol corresponding to a stack address.
181 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700182 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700183}