blob: b38a353e28ac139f0ad963b2833d2983707c0418 [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 <limits.h>
21#include <stdio.h>
22#include <stdint.h>
Dimitry Ivanov708589f2016-09-19 10:50:28 -070023#include <string.h>
Elliott Hughes8e15b082012-09-26 11:44:01 -070024
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -070025#include "private/ScopeGuard.h"
26
Elliott Hughes8e15b082012-09-26 11:44:01 -070027#include <string>
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -080028#include <thread>
jeffhaoacf5aa72012-09-12 17:25:30 -070029
Dimitry Ivanov927877c2016-09-21 11:17:13 -070030#include "gtest_globals.h"
Dimitry Ivanov708589f2016-09-19 10:50:28 -070031#include "dlfcn_symlink_support.h"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070032#include "utils.h"
33
Elliott Hughes3b297c42012-10-11 16:08:51 -070034#define ASSERT_SUBSTR(needle, haystack) \
35 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
36
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070037
Elliott Hughes1728b232014-05-14 10:02:03 -070038static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070039extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070040 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070041}
42
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070043static int g_ctor_function_called = 0;
Dimitry Ivanov55437462016-07-20 15:33:07 -070044static int g_ctor_argc = 0;
45static char** g_ctor_argv = reinterpret_cast<char**>(0xDEADBEEF);
46static char** g_ctor_envp = g_ctor_envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070047
Dimitry Ivanov55437462016-07-20 15:33:07 -070048extern "C" void ctor_function(int argc, char** argv, char** envp) __attribute__ ((constructor));
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070049
Dimitry Ivanov55437462016-07-20 15:33:07 -070050extern "C" void ctor_function(int argc, char** argv, char** envp) {
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070051 g_ctor_function_called = 17;
Dimitry Ivanov55437462016-07-20 15:33:07 -070052 g_ctor_argc = argc;
53 g_ctor_argv = argv;
54 g_ctor_envp = envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070055}
56
57TEST(dlfcn, ctor_function_call) {
58 ASSERT_EQ(17, g_ctor_function_called);
Dimitry Ivanov55437462016-07-20 15:33:07 -070059 ASSERT_TRUE(g_ctor_argc = get_argc());
60 ASSERT_TRUE(g_ctor_argv = get_argv());
61 ASSERT_TRUE(g_ctor_envp = get_envp());
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070062}
63
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070064TEST(dlfcn, dlsym_in_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070065 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070066 void* self = dlopen(nullptr, RTLD_NOW);
67 ASSERT_TRUE(self != nullptr);
68 ASSERT_TRUE(dlerror() == nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070069
70 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070071 ASSERT_TRUE(sym != nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070072
73 void (*function)() = reinterpret_cast<void(*)()>(sym);
74
Elliott Hughes1728b232014-05-14 10:02:03 -070075 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070076 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070077 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070078
79 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070080}
Elliott Hughes8e15b082012-09-26 11:44:01 -070081
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070082TEST(dlfcn, dlsym_from_sofile) {
83 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL);
84 ASSERT_TRUE(handle != nullptr) << dlerror();
85
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070086 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070087 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
88 ASSERT_TRUE(symbol == nullptr);
89 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
90
91 typedef int* (*fn_t)();
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070092 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
93 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
94 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070095
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070096 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070097 ASSERT_TRUE(ptr != nullptr) << dlerror();
98 ASSERT_EQ(42, *ptr);
99
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700100 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
101 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
102 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
103
104 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
105 ASSERT_TRUE(ptr != nullptr) << dlerror();
106 ASSERT_EQ(44, *ptr);
107
108 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
109 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
110 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
111
112 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
113 ASSERT_TRUE(ptr != nullptr) << dlerror();
114 ASSERT_EQ(43, *ptr);
115
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700116 dlclose(handle);
117}
118
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700119TEST(dlfcn, dlsym_from_sofile_with_preload) {
120 void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL);
121 ASSERT_TRUE(preload != nullptr) << dlerror();
122
123 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
124 ASSERT_TRUE(handle != nullptr) << dlerror();
125
126 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
127 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
128 ASSERT_TRUE(symbol == nullptr);
129 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
130
131 typedef int* (*fn_t)();
132 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
133 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
134 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
135
136 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
137 ASSERT_TRUE(ptr != nullptr) << dlerror();
138 ASSERT_EQ(42, *ptr);
139
140 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
141 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
142 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
143
144 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
145 ASSERT_TRUE(ptr != nullptr) << dlerror();
146 ASSERT_EQ(44, *ptr);
147
148 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
149 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
150 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
151
152 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
153 ASSERT_TRUE(ptr != nullptr) << dlerror();
154 ASSERT_EQ(43, *ptr);
155
156 dlclose(handle);
157 dlclose(preload);
158}
159
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700160TEST(dlfcn, dlsym_handle_global_sym) {
161 // check that we do not look into global group
162 // when looking up symbol by handle
163 void* handle = dlopen("libtest_empty.so", RTLD_NOW);
164 dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL);
165 void* sym = dlsym(handle, "getRandomNumber");
166 ASSERT_TRUE(sym == nullptr);
167 ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror());
168
169 sym = dlsym(handle, "DlSymTestFunction");
170 ASSERT_TRUE(sym == nullptr);
171 ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror());
172 dlclose(handle);
173}
174
Dimitry Ivanovd5b578a2016-12-14 15:16:56 -0800175TEST(dlfcn, dlsym_handle_empty_symbol) {
176 // check that dlsym of an empty symbol fails (see http://b/33530622)
177 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW);
178 ASSERT_TRUE(handle != nullptr) << dlerror();
179 void* sym = dlsym(handle, "");
180 ASSERT_TRUE(sym == nullptr);
181 ASSERT_SUBSTR("undefined symbol: ", dlerror());
182 dlclose(handle);
183}
184
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700185TEST(dlfcn, dlsym_with_dependencies) {
186 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700187 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700188 dlerror();
189 // This symbol is in DT_NEEDED library.
190 void* sym = dlsym(handle, "getRandomNumber");
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700191 ASSERT_TRUE(sym != nullptr) << dlerror();
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700192 int (*fn)(void);
193 fn = reinterpret_cast<int (*)(void)>(sym);
194 EXPECT_EQ(4, fn());
195 dlclose(handle);
196}
197
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700198TEST(dlfcn, dlopen_noload) {
199 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700200 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700201 handle = dlopen("libtest_simple.so", RTLD_NOW);
202 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700203 ASSERT_TRUE(handle != nullptr);
204 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700205 ASSERT_TRUE(handle == handle2);
206 ASSERT_EQ(0, dlclose(handle));
207 ASSERT_EQ(0, dlclose(handle2));
208}
209
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700210TEST(dlfcn, dlopen_by_soname) {
211 static const char* soname = "libdlext_test_soname.so";
212 static const char* filename = "libdlext_test_different_soname.so";
213 // 1. Make sure there is no library with soname in default search path
214 void* handle = dlopen(soname, RTLD_NOW);
215 ASSERT_TRUE(handle == nullptr);
216
217 // 2. Load a library using filename
218 handle = dlopen(filename, RTLD_NOW);
219 ASSERT_TRUE(handle != nullptr) << dlerror();
220
221 // 3. Find library by soname
222 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
223 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
224 ASSERT_EQ(handle, handle_soname);
225
226 // 4. RTLD_NOLOAD should still work with filename
227 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
228 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
229 ASSERT_EQ(handle, handle_filename);
230
231 dlclose(handle_filename);
232 dlclose(handle_soname);
233 dlclose(handle);
234}
235
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700236// mips doesn't support ifuncs
237#if !defined(__mips__)
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700238TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700239 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700240
241 // ifunc's choice depends on whether IFUNC_CHOICE has a value
242 // first check the set case
243 setenv("IFUNC_CHOICE", "set", 1);
244 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700245 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700246 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
247 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700248 ASSERT_TRUE(foo_ptr != nullptr);
249 ASSERT_TRUE(foo_library_ptr != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700250 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
251 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700252 dlclose(handle);
253
254 // then check the unset case
255 unsetenv("IFUNC_CHOICE");
256 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700257 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700258 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
259 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700260 ASSERT_TRUE(foo_ptr != nullptr);
261 ASSERT_TRUE(foo_library_ptr != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700262 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
263 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0);
264 dlclose(handle);
265}
266
267TEST(dlfcn, ifunc_ctor_call) {
268 typedef const char* (*fn_ptr)();
269
270 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700271 ASSERT_TRUE(handle != nullptr) << dlerror();
272 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
273 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
274 ASSERT_STREQ("false", is_ctor_called());
275
276 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
277 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700278 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700279 dlclose(handle);
280}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700281
282TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
283 typedef const char* (*fn_ptr)();
284
285 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
286 ASSERT_TRUE(handle != nullptr) << dlerror();
287 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
288 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
289 ASSERT_STREQ("false", is_ctor_called());
290
291 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
292 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
293 ASSERT_STREQ("true", is_ctor_called());
294 dlclose(handle);
295}
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700296#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700297
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700298TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
299 // This is the structure of the test library and
300 // its dt_needed libraries
301 // libtest_relo_check_dt_needed_order.so
302 // |
303 // +-> libtest_relo_check_dt_needed_order_1.so
304 // |
305 // +-> libtest_relo_check_dt_needed_order_2.so
306 //
307 // The root library references relo_test_get_answer_lib - which is defined
308 // in both dt_needed libraries, the correct relocation should
309 // use the function defined in libtest_relo_check_dt_needed_order_1.so
310 void* handle = nullptr;
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -0700311 auto guard = make_scope_guard([&]() {
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700312 dlclose(handle);
313 });
314
315 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
316 ASSERT_TRUE(handle != nullptr) << dlerror();
317
318 typedef int (*fn_t) (void);
319 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
320 ASSERT_TRUE(fn != nullptr) << dlerror();
321 ASSERT_EQ(1, fn());
322}
323
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700324TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700325 // Here is how the test library and its dt_needed
326 // libraries are arranged
327 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700328 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700329 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700330 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700331 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700332 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700333 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700334 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700335 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700336 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700337 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700338 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700339 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700340 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700341 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700342 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700343 //
344 // load order should be (1, 2, 3, a, b, d)
345 //
346 // get_answer() is defined in (2, 3, a, b, c)
347 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700348 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700349 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700350 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
351 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700352 typedef int (*fn_t) (void);
353 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700354 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700355 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700356 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700357 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700358
359 ASSERT_EQ(42, fn());
360 ASSERT_EQ(43, fn2());
361 dlclose(handle);
362}
363
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700364TEST(dlfcn, dlopen_check_order_reloc_siblings) {
365 // This is how this one works:
366 // we lookup and call get_answer which is defined in '_2.so'
367 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
368 // the correct _impl() is implemented by '_a.so';
369 //
370 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
371 //
372 // Here is the picture:
373 //
374 // libtest_check_order_reloc_siblings.so
375 // |
376 // +-> ..._1.so <- empty
377 // | |
378 // | +-> ..._a.so <- exports correct answer_impl()
379 // | |
380 // | +-> ..._b.so <- every other letter exporting incorrect one.
381 // |
382 // +-> ..._2.so <- empty
383 // | |
384 // | +-> ..._c.so
385 // | |
386 // | +-> ..._d.so
387 // |
388 // +-> ..._3.so <- empty
389 // |
390 // +-> ..._e.so
391 // |
392 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
393 // implements incorrect get_answer_impl()
394
395 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
396 ASSERT_TRUE(handle == nullptr);
397#ifdef __BIONIC__
398 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
399 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
400#endif
401
402 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
403 ASSERT_TRUE(handle != nullptr) << dlerror();
404
405 typedef int (*fn_t) (void);
406 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
407 ASSERT_TRUE(fn != nullptr) << dlerror();
408 ASSERT_EQ(42, fn());
409
410 ASSERT_EQ(0, dlclose(handle));
411}
412
413TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
414 // This test uses the same library as dlopen_check_order_reloc_siblings.
415 // Unlike dlopen_check_order_reloc_siblings it preloads
416 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
417 // dlopen(libtest_check_order_reloc_siblings.so)
418
419 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
420 ASSERT_TRUE(handle == nullptr);
421 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
422 ASSERT_TRUE(handle == nullptr);
423
424 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
425 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
426
427 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
428 ASSERT_TRUE(handle != nullptr) << dlerror();
429
430 ASSERT_EQ(0, dlclose(handle_for_1));
431
432 typedef int (*fn_t) (void);
433 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
434 ASSERT_TRUE(fn != nullptr) << dlerror();
435 ASSERT_EQ(42, fn());
436
437 ASSERT_EQ(0, dlclose(handle));
438}
439
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800440TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
441 // This is how this one works:
442 // we lookup and call grandchild_get_answer which is defined in '_2.so'
443 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
444 // the correct _impl() is implemented by '_c_1.so';
445 //
446 // Here is the picture of subtree:
447 //
448 // libtest_check_order_reloc_siblings.so
449 // |
450 // +-> ..._2.so <- grandchild_get_answer()
451 // |
452 // +-> ..._c.so <- empty
453 // | |
454 // | +-> _c_1.so <- exports correct answer_impl()
455 // | |
456 // | +-> _c_2.so <- exports incorrect answer_impl()
457 // |
458 // +-> ..._d.so <- empty
459
460 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
461 ASSERT_TRUE(handle == nullptr);
462#ifdef __BIONIC__
463 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
464 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
465#endif
466
467 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
468 ASSERT_TRUE(handle != nullptr) << dlerror();
469
470 typedef int (*fn_t) (void);
471 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
472 ASSERT_TRUE(fn != nullptr) << dlerror();
473 ASSERT_EQ(42, fn());
474
475 ASSERT_EQ(0, dlclose(handle));
476}
477
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700478TEST(dlfcn, dlopen_check_order_reloc_nephew) {
479 // This is how this one works:
480 // we lookup and call nephew_get_answer which is defined in '_2.so'
481 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
482 // the correct _impl() is implemented by '_a.so';
483 //
484 // Here is the picture:
485 //
486 // libtest_check_order_reloc_siblings.so
487 // |
488 // +-> ..._1.so <- empty
489 // | |
490 // | +-> ..._a.so <- exports correct answer_impl()
491 // | |
492 // | +-> ..._b.so <- every other letter exporting incorrect one.
493 // |
494 // +-> ..._2.so <- empty
495 // | |
496 // | +-> ..._c.so
497 // | |
498 // | +-> ..._d.so
499 // |
500 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
501 // |
502 // +-> ..._e.so
503 // |
504 // +-> ..._f.so
505
506 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
507 ASSERT_TRUE(handle == nullptr);
508#ifdef __BIONIC__
509 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
510 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
511#endif
512
513 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
514 ASSERT_TRUE(handle != nullptr) << dlerror();
515
516 typedef int (*fn_t) (void);
517 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
518 ASSERT_TRUE(fn != nullptr) << dlerror();
519 ASSERT_EQ(42, fn());
520
521 ASSERT_EQ(0, dlclose(handle));
522}
523
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800524TEST(dlfcn, check_unload_after_reloc) {
525 // This is how this one works:
526 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
527 // |
528 // +-> libtest_two_parents_child
529 //
530 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
531 // |
532 // +-> libtest_two_parents_child
533 //
534 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
535 // as a second step it dlopens parent2 and dlcloses parent1...
536
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700537 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
538 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800539
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700540 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
541 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800542
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700543 typedef int (*fn_t) (void);
544 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
545 ASSERT_TRUE(fn != nullptr) << dlerror();
546 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800547
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700548 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800549
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700550 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
551 ASSERT_TRUE(handle != nullptr);
552 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800553
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700554 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
555 ASSERT_TRUE(fn != nullptr) << dlerror();
556 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800557
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700558 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800559
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700560 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
561 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800562}
563
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700564extern "C" int check_order_reloc_root_get_answer_impl() {
565 return 42;
566}
567
568TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
569 // This is how this one works:
570 // we lookup and call get_answer3 which is defined in 'root.so'
571 // and in turn calls external root_get_answer_impl() defined in _2.so and
572 // above the correct _impl() is one in the executable.
573 //
574 // libtest_check_order_reloc_root.so
575 // |
576 // +-> ..._1.so <- empty
577 // |
578 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
579 //
580
581 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
582 ASSERT_TRUE(handle == nullptr);
583#ifdef __BIONIC__
584 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
585 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
586#endif
587
588 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
589 ASSERT_TRUE(handle != nullptr) << dlerror();
590
591 typedef int (*fn_t) (void);
592 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
593 ASSERT_TRUE(fn != nullptr) << dlerror();
594 ASSERT_EQ(42, fn());
595
596 ASSERT_EQ(0, dlclose(handle));
597}
598
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700599TEST(dlfcn, dlopen_check_rtld_local) {
600 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
601 ASSERT_TRUE(sym == nullptr);
602
603 // implicit RTLD_LOCAL
604 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
605 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
606 ASSERT_TRUE(sym == nullptr);
607 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
608 sym = dlsym(handle, "dlopen_testlib_simple_func");
609 ASSERT_TRUE(sym != nullptr);
610 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
611 dlclose(handle);
612
613 // explicit RTLD_LOCAL
614 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
615 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
616 ASSERT_TRUE(sym == nullptr);
617 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
618 sym = dlsym(handle, "dlopen_testlib_simple_func");
619 ASSERT_TRUE(sym != nullptr);
620 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
621 dlclose(handle);
622}
623
624TEST(dlfcn, dlopen_check_rtld_global) {
625 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
626 ASSERT_TRUE(sym == nullptr);
627
628 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700629 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700630 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
631 ASSERT_TRUE(sym != nullptr) << dlerror();
632 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
633 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700634
635 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
636 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
637 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700638
639 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
640 // shared libraries after symbol was not found in the main executable
641 // and dependent libraries.
642 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
643 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
644 ASSERT_TRUE(sym != nullptr) << dlerror();
645
646 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700647}
648
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700649// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
650// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
651// libtest_with_dependency_loop_a.so
652TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700653 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
654 ASSERT_TRUE(handle != nullptr) << dlerror();
655 void* f = dlsym(handle, "dlopen_test_loopy_function");
656 ASSERT_TRUE(f != nullptr) << dlerror();
657 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
658 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700659
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700660 // dlopen second time to make sure that the library was unloaded correctly
661 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
662 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800663#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700664 ASSERT_STREQ("dlopen failed: library \"libtest_with_dependency_loop.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -0800665#else
666 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
667 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700668#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800669
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700670 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
671 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700672}
673
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700674TEST(dlfcn, dlopen_nodelete) {
675 static bool is_unloaded = false;
676
677 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
678 ASSERT_TRUE(handle != nullptr) << dlerror();
679 void (*set_unload_flag_ptr)(bool*);
680 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
681 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
682 set_unload_flag_ptr(&is_unloaded);
683
684 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
685 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
686 ASSERT_EQ(1729U, *taxicab_number);
687 *taxicab_number = 2;
688
689 dlclose(handle);
690 ASSERT_TRUE(!is_unloaded);
691
692 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
693 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
694 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
695
696
697 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
698 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
699 ASSERT_EQ(taxicab_number2, taxicab_number);
700
701 ASSERT_EQ(2U, *taxicab_number2);
702
703 dlclose(handle);
704 ASSERT_TRUE(!is_unloaded);
705}
706
707TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
708 static bool is_unloaded = false;
709
710 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
711 ASSERT_TRUE(handle != nullptr) << dlerror();
712 void (*set_unload_flag_ptr)(bool*);
713 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
714 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
715 set_unload_flag_ptr(&is_unloaded);
716
717 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
718 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
719
720 ASSERT_EQ(1729U, *taxicab_number);
721 *taxicab_number = 2;
722
723 // This RTLD_NODELETE should be ignored
724 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
725 ASSERT_TRUE(handle1 != nullptr) << dlerror();
726 ASSERT_EQ(handle, handle1);
727
728 dlclose(handle1);
729 dlclose(handle);
730
731 ASSERT_TRUE(is_unloaded);
732}
733
734TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
735 static bool is_unloaded = false;
736
737 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
738 ASSERT_TRUE(handle != nullptr) << dlerror();
739 void (*set_unload_flag_ptr)(bool*);
740 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
741 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
742 set_unload_flag_ptr(&is_unloaded);
743
744 dlclose(handle);
745 ASSERT_TRUE(!is_unloaded);
746}
747
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700748TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700749 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
750 ASSERT_TRUE(handle != nullptr) << dlerror();
751 int (*get_answer)();
752 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
753 ASSERT_TRUE(get_answer != nullptr) << dlerror();
754 ASSERT_EQ(42, get_answer());
755 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700756}
757
Elliott Hughese66190d2012-12-18 15:57:55 -0800758TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700759 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700760 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700761#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700762 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
763#else
764 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
765#endif
766}
767
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800768static void ConcurrentDlErrorFn(std::string& error) {
769 ASSERT_TRUE(dlerror() == nullptr);
770
771 void* handle = dlopen("/child/thread", RTLD_NOW);
772 ASSERT_TRUE(handle == nullptr);
773
774 const char* err = dlerror();
775 ASSERT_TRUE(err != nullptr);
776
777 error = err;
778}
779
780TEST(dlfcn, dlerror_concurrent_buffer) {
781 void* handle = dlopen("/main/thread", RTLD_NOW);
782 ASSERT_TRUE(handle == nullptr);
783 const char* main_thread_error = dlerror();
784 ASSERT_TRUE(main_thread_error != nullptr);
785 ASSERT_SUBSTR("/main/thread", main_thread_error);
786
787 std::string child_thread_error;
788 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
789 t.join();
790 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
791
792 // Check that main thread local buffer was not modified.
793 ASSERT_SUBSTR("/main/thread", main_thread_error);
Elliott Hughes5419b942012-10-16 15:54:46 -0700794}
795
Elliott Hughese66190d2012-12-18 15:57:55 -0800796TEST(dlfcn, dlerror_concurrent) {
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800797 void* handle = dlopen("/main/thread", RTLD_NOW);
798 ASSERT_TRUE(handle == nullptr);
799
800 std::string child_thread_error;
801 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
802 t.join();
803 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
804
Elliott Hughes5419b942012-10-16 15:54:46 -0700805 const char* main_thread_error = dlerror();
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800806 ASSERT_TRUE(main_thread_error != nullptr);
Elliott Hughes5419b942012-10-16 15:54:46 -0700807 ASSERT_SUBSTR("/main/thread", main_thread_error);
808}
809
Elliott Hughese66190d2012-12-18 15:57:55 -0800810TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700811 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700812 void* self = dlopen(nullptr, RTLD_NOW);
813 ASSERT_TRUE(self != nullptr);
814 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700815
816 void* sym;
817
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700818#if defined(__BIONIC__) && !defined(__LP64__)
819 // RTLD_DEFAULT in lp32 bionic is not (void*)0
820 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700821 sym = dlsym(nullptr, "test");
822 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800823 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700824#endif
825
Elliott Hughes3b297c42012-10-11 16:08:51 -0700826 // Symbol that doesn't exist.
827 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700828 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700829 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700830
831 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700832}
833
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700834TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700835 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700836 void* self = dlopen(nullptr, RTLD_NOW);
837 ASSERT_TRUE(self != nullptr);
838 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700839
840 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700841 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700842
843 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
844 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
845
846 Dl_info info;
847 int rc = dladdr(addr, &info);
848 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
849
850 // Get the name of this executable.
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700851 const std::string& executable_path = get_executable_path();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700852
853 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700854 char dli_realpath[PATH_MAX];
855 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700856 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700857
858 // The symbol name should be the symbol we looked up.
859 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
860
861 // The address should be the exact address of the symbol.
862 ASSERT_EQ(info.dli_saddr, sym);
863
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700864 std::vector<map_record> maps;
865 ASSERT_TRUE(Maps::parse_maps(&maps));
866
867 void* base_address = nullptr;
868 for (const map_record& rec : maps) {
869 if (executable_path == rec.pathname) {
870 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700871 break;
872 }
873 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700874
875 // The base address should be the address we were loaded at.
876 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700877
878 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700879}
880
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700881TEST(dlfcn, dlopen_executable_by_absolute_path) {
882 void* handle1 = dlopen(nullptr, RTLD_NOW);
883 ASSERT_TRUE(handle1 != nullptr) << dlerror();
884
885 void* handle2 = dlopen(get_executable_path().c_str(), RTLD_NOW);
886 ASSERT_TRUE(handle2 != nullptr) << dlerror();
887
888#if defined(__BIONIC__)
889 ASSERT_EQ(handle1, handle2);
890#else
891 GTEST_LOG_(INFO) << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
892 "it loads a separate copy of the main executable "
893 "on dlopen by absolute path.";
894#endif
895}
896
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700897#if defined(__LP64__)
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200898#define PATH_TO_SYSTEM_LIB "/system/lib64/"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700899#else
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200900#define PATH_TO_SYSTEM_LIB "/system/lib/"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700901#endif
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200902#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700903
904TEST(dlfcn, dladdr_libc) {
905#if defined(__BIONIC__)
906 Dl_info info;
907 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
908 ASSERT_TRUE(dladdr(addr, &info) != 0);
909
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700910 // /system/lib is symlink when this test is executed on host.
911 char libc_realpath[PATH_MAX];
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200912 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700913
914 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700915 // TODO: add check for dfi_fbase
916 ASSERT_STREQ("puts", info.dli_sname);
917 ASSERT_EQ(addr, info.dli_saddr);
918#else
919 GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig "
920 "for libc.so, which is symlink itself (not a realpath).\n";
921#endif
922}
923
Elliott Hughese66190d2012-12-18 15:57:55 -0800924TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700925 Dl_info info;
926
Elliott Hughes3b297c42012-10-11 16:08:51 -0700927 dlerror(); // Clear any pending errors.
928
Elliott Hughes8e15b082012-09-26 11:44:01 -0700929 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700930 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
931 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700932
933 // No symbol corresponding to a stack address.
934 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700935 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700936}
Elliott Hughes124fae92012-10-31 14:20:03 -0700937
Elliott Hughesa43e9062013-01-07 14:18:22 -0800938// GNU-style ELF hash tables are incompatible with the MIPS ABI.
939// MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code.
Elliott Hughese66190d2012-12-18 15:57:55 -0800940TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800941#if !defined(__mips__)
Elliott Hughes124fae92012-10-31 14:20:03 -0700942 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800943 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
944 ASSERT_TRUE(handle != nullptr) << dlerror();
945 auto guard = make_scope_guard([&]() {
946 dlclose(handle);
947 });
948 void* sym = dlsym(handle, "getRandomNumber");
949 ASSERT_TRUE(sym != nullptr) << dlerror();
950 int (*fn)(void);
951 fn = reinterpret_cast<int (*)(void)>(sym);
952 EXPECT_EQ(4, fn());
953
954 Dl_info dlinfo;
955 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
956
957 ASSERT_TRUE(fn == dlinfo.dli_saddr);
958 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800959 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800960#else
961 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
962#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700963}
Elliott Hughese66190d2012-12-18 15:57:55 -0800964
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800965TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
966 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
967 ASSERT_TRUE(handle != nullptr) << dlerror();
968 auto guard = make_scope_guard([&]() {
969 dlclose(handle);
970 });
971 void* sym = dlsym(handle, "getRandomNumber");
972 ASSERT_TRUE(sym != nullptr) << dlerror();
973 int (*fn)(void);
974 fn = reinterpret_cast<int (*)(void)>(sym);
975 EXPECT_EQ(4, fn());
976
977 Dl_info dlinfo;
978 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
979
980 ASSERT_TRUE(fn == dlinfo.dli_saddr);
981 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
982 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
983}
984
Elliott Hughese66190d2012-12-18 15:57:55 -0800985TEST(dlfcn, dlopen_bad_flags) {
986 dlerror(); // Clear any pending errors.
987 void* handle;
988
Elliott Hughes063525c2014-05-13 11:19:57 -0700989#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800990 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700991 handle = dlopen(nullptr, 0);
992 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -0800993 ASSERT_SUBSTR("invalid", dlerror());
994#endif
995
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700996 handle = dlopen(nullptr, 0xffffffff);
997 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -0800998 ASSERT_SUBSTR("invalid", dlerror());
999
1000 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001001 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
1002 ASSERT_TRUE(handle != nullptr);
1003 ASSERT_SUBSTR(nullptr, dlerror());
Elliott Hughese66190d2012-12-18 15:57:55 -08001004}
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001005
1006TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001007 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001008 ASSERT_TRUE(addr == nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001009}
1010
1011TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001012 void* addr = dlsym(RTLD_DEFAULT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001013 ASSERT_TRUE(addr != nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001014}
1015
1016TEST(dlfcn, rtld_next_unknown_symbol) {
1017 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001018 ASSERT_TRUE(addr == nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001019}
1020
1021TEST(dlfcn, rtld_next_known_symbol) {
1022 void* addr = dlsym(RTLD_NEXT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001023 ASSERT_TRUE(addr != nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001024}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001025
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001026// Check that RTLD_NEXT of a libc symbol works in dlopened library
1027TEST(dlfcn, rtld_next_from_library) {
1028 void* library_with_close = dlopen("libtest_check_rtld_next_from_library.so", RTLD_NOW);
1029 ASSERT_TRUE(library_with_close != nullptr) << dlerror();
1030 void* expected_addr = dlsym(RTLD_DEFAULT, "close");
1031 ASSERT_TRUE(expected_addr != nullptr) << dlerror();
1032 typedef void* (*get_libc_close_ptr_fn_t)();
1033 get_libc_close_ptr_fn_t get_libc_close_ptr =
1034 reinterpret_cast<get_libc_close_ptr_fn_t>(dlsym(library_with_close, "get_libc_close_ptr"));
1035 ASSERT_TRUE(get_libc_close_ptr != nullptr) << dlerror();
1036 ASSERT_EQ(expected_addr, get_libc_close_ptr());
1037
1038 dlclose(library_with_close);
1039}
1040
1041
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001042TEST(dlfcn, dlsym_weak_func) {
1043 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001044 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001045 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001046
1047 int (*weak_func)();
1048 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001049 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001050 EXPECT_EQ(42, weak_func());
1051 dlclose(handle);
1052}
1053
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001054TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001055 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
1056 ASSERT_TRUE(handle != nullptr) << dlerror();
1057 int (*weak_func)();
1058 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
1059 ASSERT_TRUE(weak_func != nullptr) << dlerror();
1060 EXPECT_EQ(6551, weak_func());
1061 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001062}
1063
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001064TEST(dlfcn, dlopen_symlink) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001065 DlfcnSymlink symlink("dlopen_symlink");
1066 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001067 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001068 void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001069 ASSERT_TRUE(handle1 != nullptr);
1070 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001071 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -07001072 dlclose(handle1);
1073 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001074}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001075
1076// libtest_dlopen_from_ctor_main.so depends on
1077// libtest_dlopen_from_ctor.so which has a constructor
1078// that calls dlopen(libc...). This is to test the situation
1079// described in b/7941716.
1080TEST(dlfcn, dlopen_dlopen_from_ctor) {
1081#if defined(__BIONIC__)
1082 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1083 ASSERT_TRUE(handle != nullptr) << dlerror();
1084 dlclose(handle);
1085#else
1086 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
1087#endif
1088}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001089
1090TEST(dlfcn, symbol_versioning_use_v1) {
1091 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1092 ASSERT_TRUE(handle != nullptr) << dlerror();
1093 typedef int (*fn_t)();
1094 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1095 ASSERT_TRUE(fn != nullptr) << dlerror();
1096 ASSERT_EQ(1, fn());
1097 dlclose(handle);
1098}
1099
1100TEST(dlfcn, symbol_versioning_use_v2) {
1101 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1102 ASSERT_TRUE(handle != nullptr) << dlerror();
1103 typedef int (*fn_t)();
1104 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1105 ASSERT_TRUE(fn != nullptr) << dlerror();
1106 ASSERT_EQ(2, fn());
1107 dlclose(handle);
1108}
1109
1110TEST(dlfcn, symbol_versioning_use_other_v2) {
1111 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1112 ASSERT_TRUE(handle != nullptr) << dlerror();
1113 typedef int (*fn_t)();
1114 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1115 ASSERT_TRUE(fn != nullptr) << dlerror();
1116 ASSERT_EQ(20, fn());
1117 dlclose(handle);
1118}
1119
1120TEST(dlfcn, symbol_versioning_use_other_v3) {
1121 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1122 ASSERT_TRUE(handle != nullptr) << dlerror();
1123 typedef int (*fn_t)();
1124 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1125 ASSERT_TRUE(fn != nullptr) << dlerror();
1126 ASSERT_EQ(3, fn());
1127 dlclose(handle);
1128}
1129
1130TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1131 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1132 ASSERT_TRUE(handle != nullptr) << dlerror();
1133 typedef int (*fn_t)();
1134 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1135 ASSERT_TRUE(fn != nullptr) << dlerror();
1136 ASSERT_EQ(3, fn()); // the default version is 3
1137 dlclose(handle);
1138}
1139
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001140TEST(dlfcn, dlvsym_smoke) {
1141 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1142 ASSERT_TRUE(handle != nullptr) << dlerror();
1143 typedef int (*fn_t)();
1144
1145 {
1146 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1147 ASSERT_TRUE(fn == nullptr);
1148 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1149 }
1150
1151 {
1152 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1153 ASSERT_TRUE(fn != nullptr) << dlerror();
1154 ASSERT_EQ(2, fn());
1155 }
1156
1157 dlclose(handle);
1158}
1159
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001160// This preempts the implementation from libtest_versioned_lib.so
1161extern "C" int version_zero_function() {
1162 return 0;
1163}
1164
1165// This preempts the implementation from libtest_versioned_uselibv*.so
1166extern "C" int version_zero_function2() {
1167 return 0;
1168}
Evgenii Stepanov68650822015-06-10 13:38:39 -07001169
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001170TEST(dlfcn, dt_runpath_smoke) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001171 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1172 ASSERT_TRUE(handle != nullptr) << dlerror();
1173
1174 typedef void *(* dlopen_b_fn)();
1175 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1176 ASSERT_TRUE(fn != nullptr) << dlerror();
1177
1178 void *p = fn();
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001179 ASSERT_TRUE(p != nullptr);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001180
1181 dlclose(handle);
1182}
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001183
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001184// Bionic specific tests
1185#if defined(__BIONIC__)
1186
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001187TEST(dlfcn, dt_runpath_absolute_path) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001188 std::string libpath = get_testlib_root() + "/libtest_dt_runpath_d.so";
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001189 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001190 ASSERT_TRUE(handle != nullptr) << dlerror();
1191
1192 typedef void *(* dlopen_b_fn)();
1193 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1194 ASSERT_TRUE(fn != nullptr) << dlerror();
1195
1196 void *p = fn();
1197 ASSERT_TRUE(p != nullptr);
1198
1199 dlclose(handle);
1200}
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001201
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001202TEST(dlfcn, dlopen_invalid_rw_load_segment) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001203 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001204 "/" + kPrebuiltElfDir +
1205 "/libtest_invalid-rw_load_segment.so";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001206 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1207 ASSERT_TRUE(handle == nullptr);
1208 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W + E load segments are not allowed";
1209 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1210}
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001211
1212TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001213 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001214 "/" + kPrebuiltElfDir +
1215 "/libtest_invalid-unaligned_shdr_offset.so";
1216
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001217 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1218 ASSERT_TRUE(handle == nullptr);
1219 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
1220 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1221}
1222
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001223TEST(dlfcn, dlopen_invalid_zero_shentsize) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001224 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001225 "/" + kPrebuiltElfDir +
1226 "/libtest_invalid-zero_shentsize.so";
1227
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001228 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1229 ASSERT_TRUE(handle == nullptr);
1230 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x";
1231 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1232}
1233
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001234TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001235 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001236 "/" + kPrebuiltElfDir +
1237 "/libtest_invalid-zero_shstrndx.so";
1238
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001239 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1240 ASSERT_TRUE(handle == nullptr);
1241 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx";
1242 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1243}
1244
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001245TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001246 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001247 "/" + kPrebuiltElfDir +
1248 "/libtest_invalid-empty_shdr_table.so";
1249
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001250 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1251 ASSERT_TRUE(handle == nullptr);
1252 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers";
1253 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1254}
1255
Dimitry Ivanov46230442016-08-15 13:40:53 -07001256TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001257 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001258 "/" + kPrebuiltElfDir +
1259 "/libtest_invalid-zero_shdr_table_offset.so";
1260
Dimitry Ivanov46230442016-08-15 13:40:53 -07001261 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1262 ASSERT_TRUE(handle == nullptr);
1263 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/";
1264 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1265}
1266
Dimitry Ivanov55958342016-08-15 14:06:04 -07001267TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001268 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001269 "/" + kPrebuiltElfDir +
1270 "/libtest_invalid-zero_shdr_table_content.so";
1271
Dimitry Ivanov55958342016-08-15 14:06:04 -07001272 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1273 ASSERT_TRUE(handle == nullptr);
1274 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
1275 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1276}
1277
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001278TEST(dlfcn, dlopen_invalid_textrels) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001279 const std::string libpath = get_testlib_root() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001280 "/" + kPrebuiltElfDir +
1281 "/libtest_invalid-textrels.so";
1282
1283 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1284 ASSERT_TRUE(handle == nullptr);
1285 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1286 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1287}
1288
1289TEST(dlfcn, dlopen_invalid_textrels2) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001290 const std::string libpath = get_testlib_root() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001291 "/" + kPrebuiltElfDir +
1292 "/libtest_invalid-textrels2.so";
1293
1294 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1295 ASSERT_TRUE(handle == nullptr);
1296 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1297 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1298}
1299
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001300#endif