blob: a5abda7b138a015297b1ef949fde483d1e6987b2 [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
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -070025#include "private/ScopeGuard.h"
26
Elliott Hughes8e15b082012-09-26 11:44:01 -070027#include <string>
jeffhaoacf5aa72012-09-12 17:25:30 -070028
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070029#include "utils.h"
30
Elliott Hughes3b297c42012-10-11 16:08:51 -070031#define ASSERT_SUBSTR(needle, haystack) \
32 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
33
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070034
Elliott Hughes1728b232014-05-14 10:02:03 -070035static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070036extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070037 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070038}
39
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070040static int g_ctor_function_called = 0;
41
42extern "C" void ctor_function() __attribute__ ((constructor));
43
44extern "C" void ctor_function() {
45 g_ctor_function_called = 17;
46}
47
48TEST(dlfcn, ctor_function_call) {
49 ASSERT_EQ(17, g_ctor_function_called);
50}
51
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070052TEST(dlfcn, dlsym_in_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070053 dlerror(); // Clear any pending errors.
jeffhaoacf5aa72012-09-12 17:25:30 -070054 void* self = dlopen(NULL, RTLD_NOW);
55 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -070056 ASSERT_TRUE(dlerror() == NULL);
jeffhaoacf5aa72012-09-12 17:25:30 -070057
58 void* sym = dlsym(self, "DlSymTestFunction");
59 ASSERT_TRUE(sym != NULL);
60
61 void (*function)() = reinterpret_cast<void(*)()>(sym);
62
Elliott Hughes1728b232014-05-14 10:02:03 -070063 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070064 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070065 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070066
67 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070068}
Elliott Hughes8e15b082012-09-26 11:44:01 -070069
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070070TEST(dlfcn, dlsym_from_sofile) {
71 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL);
72 ASSERT_TRUE(handle != nullptr) << dlerror();
73
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070074 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070075 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
76 ASSERT_TRUE(symbol == nullptr);
77 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
78
79 typedef int* (*fn_t)();
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070080 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
81 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
82 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070083
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070084 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070085 ASSERT_TRUE(ptr != nullptr) << dlerror();
86 ASSERT_EQ(42, *ptr);
87
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070088 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
89 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
90 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
91
92 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
93 ASSERT_TRUE(ptr != nullptr) << dlerror();
94 ASSERT_EQ(44, *ptr);
95
96 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
97 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
98 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
99
100 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
101 ASSERT_TRUE(ptr != nullptr) << dlerror();
102 ASSERT_EQ(43, *ptr);
103
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700104 dlclose(handle);
105}
106
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700107TEST(dlfcn, dlsym_from_sofile_with_preload) {
108 void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL);
109 ASSERT_TRUE(preload != nullptr) << dlerror();
110
111 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
112 ASSERT_TRUE(handle != nullptr) << dlerror();
113
114 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
115 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
116 ASSERT_TRUE(symbol == nullptr);
117 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
118
119 typedef int* (*fn_t)();
120 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
121 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
122 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
123
124 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
125 ASSERT_TRUE(ptr != nullptr) << dlerror();
126 ASSERT_EQ(42, *ptr);
127
128 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
129 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
130 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
131
132 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
133 ASSERT_TRUE(ptr != nullptr) << dlerror();
134 ASSERT_EQ(44, *ptr);
135
136 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
137 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
138 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
139
140 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
141 ASSERT_TRUE(ptr != nullptr) << dlerror();
142 ASSERT_EQ(43, *ptr);
143
144 dlclose(handle);
145 dlclose(preload);
146}
147
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700148TEST(dlfcn, dlsym_with_dependencies) {
149 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
150 ASSERT_TRUE(handle != NULL);
151 dlerror();
152 // This symbol is in DT_NEEDED library.
153 void* sym = dlsym(handle, "getRandomNumber");
154 ASSERT_TRUE(sym != NULL);
155 int (*fn)(void);
156 fn = reinterpret_cast<int (*)(void)>(sym);
157 EXPECT_EQ(4, fn());
158 dlclose(handle);
159}
160
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700161TEST(dlfcn, dlopen_noload) {
162 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
163 ASSERT_TRUE(handle == NULL);
164 handle = dlopen("libtest_simple.so", RTLD_NOW);
165 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
166 ASSERT_TRUE(handle != NULL);
167 ASSERT_TRUE(handle2 != NULL);
168 ASSERT_TRUE(handle == handle2);
169 ASSERT_EQ(0, dlclose(handle));
170 ASSERT_EQ(0, dlclose(handle2));
171}
172
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700173TEST(dlfcn, dlopen_by_soname) {
174 static const char* soname = "libdlext_test_soname.so";
175 static const char* filename = "libdlext_test_different_soname.so";
176 // 1. Make sure there is no library with soname in default search path
177 void* handle = dlopen(soname, RTLD_NOW);
178 ASSERT_TRUE(handle == nullptr);
179
180 // 2. Load a library using filename
181 handle = dlopen(filename, RTLD_NOW);
182 ASSERT_TRUE(handle != nullptr) << dlerror();
183
184 // 3. Find library by soname
185 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
186 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
187 ASSERT_EQ(handle, handle_soname);
188
189 // 4. RTLD_NOLOAD should still work with filename
190 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
191 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
192 ASSERT_EQ(handle, handle_filename);
193
194 dlclose(handle_filename);
195 dlclose(handle_soname);
196 dlclose(handle);
197}
198
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700199// ifuncs are only supported on intel and arm64 for now
200#if defined (__aarch64__) || defined(__i386__) || defined(__x86_64__)
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700201TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700202 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700203
204 // ifunc's choice depends on whether IFUNC_CHOICE has a value
205 // first check the set case
206 setenv("IFUNC_CHOICE", "set", 1);
207 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
208 ASSERT_TRUE(handle != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700209 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
210 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700211 ASSERT_TRUE(foo_ptr != NULL);
212 ASSERT_TRUE(foo_library_ptr != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700213 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
214 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700215 dlclose(handle);
216
217 // then check the unset case
218 unsetenv("IFUNC_CHOICE");
219 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
220 ASSERT_TRUE(handle != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700221 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
222 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700223 ASSERT_TRUE(foo_ptr != NULL);
224 ASSERT_TRUE(foo_library_ptr != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700225 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
226 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0);
227 dlclose(handle);
228}
229
230TEST(dlfcn, ifunc_ctor_call) {
231 typedef const char* (*fn_ptr)();
232
233 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700234 ASSERT_TRUE(handle != nullptr) << dlerror();
235 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
236 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
237 ASSERT_STREQ("false", is_ctor_called());
238
239 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
240 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700241 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700242 dlclose(handle);
243}
244#endif
245
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700246TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
247 // This is the structure of the test library and
248 // its dt_needed libraries
249 // libtest_relo_check_dt_needed_order.so
250 // |
251 // +-> libtest_relo_check_dt_needed_order_1.so
252 // |
253 // +-> libtest_relo_check_dt_needed_order_2.so
254 //
255 // The root library references relo_test_get_answer_lib - which is defined
256 // in both dt_needed libraries, the correct relocation should
257 // use the function defined in libtest_relo_check_dt_needed_order_1.so
258 void* handle = nullptr;
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -0700259 auto guard = make_scope_guard([&]() {
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700260 dlclose(handle);
261 });
262
263 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
264 ASSERT_TRUE(handle != nullptr) << dlerror();
265
266 typedef int (*fn_t) (void);
267 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
268 ASSERT_TRUE(fn != nullptr) << dlerror();
269 ASSERT_EQ(1, fn());
270}
271
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700272TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700273 // Here is how the test library and its dt_needed
274 // libraries are arranged
275 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700276 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700277 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700278 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700279 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700280 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700281 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700282 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700283 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700284 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700285 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700286 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700287 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700288 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700289 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700290 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700291 //
292 // load order should be (1, 2, 3, a, b, d)
293 //
294 // get_answer() is defined in (2, 3, a, b, c)
295 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700296 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700297 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700298 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
299 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700300 typedef int (*fn_t) (void);
301 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700302 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700303 ASSERT_TRUE(fn != NULL) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700304 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700305 ASSERT_TRUE(fn2 != NULL) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700306
307 ASSERT_EQ(42, fn());
308 ASSERT_EQ(43, fn2());
309 dlclose(handle);
310}
311
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700312TEST(dlfcn, dlopen_check_order_reloc_siblings) {
313 // This is how this one works:
314 // we lookup and call get_answer which is defined in '_2.so'
315 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
316 // the correct _impl() is implemented by '_a.so';
317 //
318 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
319 //
320 // Here is the picture:
321 //
322 // libtest_check_order_reloc_siblings.so
323 // |
324 // +-> ..._1.so <- empty
325 // | |
326 // | +-> ..._a.so <- exports correct answer_impl()
327 // | |
328 // | +-> ..._b.so <- every other letter exporting incorrect one.
329 // |
330 // +-> ..._2.so <- empty
331 // | |
332 // | +-> ..._c.so
333 // | |
334 // | +-> ..._d.so
335 // |
336 // +-> ..._3.so <- empty
337 // |
338 // +-> ..._e.so
339 // |
340 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
341 // implements incorrect get_answer_impl()
342
343 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
344 ASSERT_TRUE(handle == nullptr);
345#ifdef __BIONIC__
346 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
347 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
348#endif
349
350 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
351 ASSERT_TRUE(handle != nullptr) << dlerror();
352
353 typedef int (*fn_t) (void);
354 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
355 ASSERT_TRUE(fn != nullptr) << dlerror();
356 ASSERT_EQ(42, fn());
357
358 ASSERT_EQ(0, dlclose(handle));
359}
360
361TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
362 // This test uses the same library as dlopen_check_order_reloc_siblings.
363 // Unlike dlopen_check_order_reloc_siblings it preloads
364 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
365 // dlopen(libtest_check_order_reloc_siblings.so)
366
367 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
368 ASSERT_TRUE(handle == nullptr);
369 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
370 ASSERT_TRUE(handle == nullptr);
371
372 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
373 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
374
375 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
376 ASSERT_TRUE(handle != nullptr) << dlerror();
377
378 ASSERT_EQ(0, dlclose(handle_for_1));
379
380 typedef int (*fn_t) (void);
381 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
382 ASSERT_TRUE(fn != nullptr) << dlerror();
383 ASSERT_EQ(42, fn());
384
385 ASSERT_EQ(0, dlclose(handle));
386}
387
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800388TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
389 // This is how this one works:
390 // we lookup and call grandchild_get_answer which is defined in '_2.so'
391 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
392 // the correct _impl() is implemented by '_c_1.so';
393 //
394 // Here is the picture of subtree:
395 //
396 // libtest_check_order_reloc_siblings.so
397 // |
398 // +-> ..._2.so <- grandchild_get_answer()
399 // |
400 // +-> ..._c.so <- empty
401 // | |
402 // | +-> _c_1.so <- exports correct answer_impl()
403 // | |
404 // | +-> _c_2.so <- exports incorrect answer_impl()
405 // |
406 // +-> ..._d.so <- empty
407
408 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
409 ASSERT_TRUE(handle == nullptr);
410#ifdef __BIONIC__
411 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
412 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
413#endif
414
415 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
416 ASSERT_TRUE(handle != nullptr) << dlerror();
417
418 typedef int (*fn_t) (void);
419 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
420 ASSERT_TRUE(fn != nullptr) << dlerror();
421 ASSERT_EQ(42, fn());
422
423 ASSERT_EQ(0, dlclose(handle));
424}
425
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700426TEST(dlfcn, dlopen_check_order_reloc_nephew) {
427 // This is how this one works:
428 // we lookup and call nephew_get_answer which is defined in '_2.so'
429 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
430 // the correct _impl() is implemented by '_a.so';
431 //
432 // Here is the picture:
433 //
434 // libtest_check_order_reloc_siblings.so
435 // |
436 // +-> ..._1.so <- empty
437 // | |
438 // | +-> ..._a.so <- exports correct answer_impl()
439 // | |
440 // | +-> ..._b.so <- every other letter exporting incorrect one.
441 // |
442 // +-> ..._2.so <- empty
443 // | |
444 // | +-> ..._c.so
445 // | |
446 // | +-> ..._d.so
447 // |
448 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
449 // |
450 // +-> ..._e.so
451 // |
452 // +-> ..._f.so
453
454 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
455 ASSERT_TRUE(handle == nullptr);
456#ifdef __BIONIC__
457 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
458 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
459#endif
460
461 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
462 ASSERT_TRUE(handle != nullptr) << dlerror();
463
464 typedef int (*fn_t) (void);
465 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
466 ASSERT_TRUE(fn != nullptr) << dlerror();
467 ASSERT_EQ(42, fn());
468
469 ASSERT_EQ(0, dlclose(handle));
470}
471
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800472TEST(dlfcn, check_unload_after_reloc) {
473 // This is how this one works:
474 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
475 // |
476 // +-> libtest_two_parents_child
477 //
478 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
479 // |
480 // +-> libtest_two_parents_child
481 //
482 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
483 // as a second step it dlopens parent2 and dlcloses parent1...
484
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700485 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
486 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800487
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700488 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
489 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800490
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700491 typedef int (*fn_t) (void);
492 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
493 ASSERT_TRUE(fn != nullptr) << dlerror();
494 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800495
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700496 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800497
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700498 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
499 ASSERT_TRUE(handle != nullptr);
500 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800501
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700502 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
503 ASSERT_TRUE(fn != nullptr) << dlerror();
504 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800505
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700506 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800507
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700508 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
509 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800510}
511
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700512extern "C" int check_order_reloc_root_get_answer_impl() {
513 return 42;
514}
515
516TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
517 // This is how this one works:
518 // we lookup and call get_answer3 which is defined in 'root.so'
519 // and in turn calls external root_get_answer_impl() defined in _2.so and
520 // above the correct _impl() is one in the executable.
521 //
522 // libtest_check_order_reloc_root.so
523 // |
524 // +-> ..._1.so <- empty
525 // |
526 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
527 //
528
529 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
530 ASSERT_TRUE(handle == nullptr);
531#ifdef __BIONIC__
532 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
533 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
534#endif
535
536 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
537 ASSERT_TRUE(handle != nullptr) << dlerror();
538
539 typedef int (*fn_t) (void);
540 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
541 ASSERT_TRUE(fn != nullptr) << dlerror();
542 ASSERT_EQ(42, fn());
543
544 ASSERT_EQ(0, dlclose(handle));
545}
546
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700547TEST(dlfcn, dlopen_check_rtld_local) {
548 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
549 ASSERT_TRUE(sym == nullptr);
550
551 // implicit RTLD_LOCAL
552 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
553 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
554 ASSERT_TRUE(sym == nullptr);
555 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
556 sym = dlsym(handle, "dlopen_testlib_simple_func");
557 ASSERT_TRUE(sym != nullptr);
558 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
559 dlclose(handle);
560
561 // explicit RTLD_LOCAL
562 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
563 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
564 ASSERT_TRUE(sym == nullptr);
565 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
566 sym = dlsym(handle, "dlopen_testlib_simple_func");
567 ASSERT_TRUE(sym != nullptr);
568 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
569 dlclose(handle);
570}
571
572TEST(dlfcn, dlopen_check_rtld_global) {
573 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
574 ASSERT_TRUE(sym == nullptr);
575
576 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700577 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700578 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
579 ASSERT_TRUE(sym != nullptr) << dlerror();
580 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
581 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700582
583 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
584 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
585 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700586}
587
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700588// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
589// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
590// libtest_with_dependency_loop_a.so
591TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700592 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
593 ASSERT_TRUE(handle != nullptr) << dlerror();
594 void* f = dlsym(handle, "dlopen_test_loopy_function");
595 ASSERT_TRUE(f != nullptr) << dlerror();
596 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
597 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700598
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700599 // dlopen second time to make sure that the library was unloaded correctly
600 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
601 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800602#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700603 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
604 ASSERT_STREQ("dlopen failed: library \"libtest_with_dependency_loop.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700605#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800606
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700607 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
608 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700609}
610
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700611TEST(dlfcn, dlopen_nodelete) {
612 static bool is_unloaded = false;
613
614 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
615 ASSERT_TRUE(handle != nullptr) << dlerror();
616 void (*set_unload_flag_ptr)(bool*);
617 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
618 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
619 set_unload_flag_ptr(&is_unloaded);
620
621 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
622 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
623 ASSERT_EQ(1729U, *taxicab_number);
624 *taxicab_number = 2;
625
626 dlclose(handle);
627 ASSERT_TRUE(!is_unloaded);
628
629 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
630 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
631 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
632
633
634 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
635 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
636 ASSERT_EQ(taxicab_number2, taxicab_number);
637
638 ASSERT_EQ(2U, *taxicab_number2);
639
640 dlclose(handle);
641 ASSERT_TRUE(!is_unloaded);
642}
643
644TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
645 static bool is_unloaded = false;
646
647 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
648 ASSERT_TRUE(handle != nullptr) << dlerror();
649 void (*set_unload_flag_ptr)(bool*);
650 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
651 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
652 set_unload_flag_ptr(&is_unloaded);
653
654 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
655 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
656
657 ASSERT_EQ(1729U, *taxicab_number);
658 *taxicab_number = 2;
659
660 // This RTLD_NODELETE should be ignored
661 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
662 ASSERT_TRUE(handle1 != nullptr) << dlerror();
663 ASSERT_EQ(handle, handle1);
664
665 dlclose(handle1);
666 dlclose(handle);
667
668 ASSERT_TRUE(is_unloaded);
669}
670
671TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
672 static bool is_unloaded = false;
673
674 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
675 ASSERT_TRUE(handle != nullptr) << dlerror();
676 void (*set_unload_flag_ptr)(bool*);
677 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
678 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
679 set_unload_flag_ptr(&is_unloaded);
680
681 dlclose(handle);
682 ASSERT_TRUE(!is_unloaded);
683}
684
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700685TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700686 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
687 ASSERT_TRUE(handle != nullptr) << dlerror();
688 int (*get_answer)();
689 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
690 ASSERT_TRUE(get_answer != nullptr) << dlerror();
691 ASSERT_EQ(42, get_answer());
692 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700693}
694
Elliott Hughese66190d2012-12-18 15:57:55 -0800695TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700696 void* self = dlopen("/does/not/exist", RTLD_NOW);
697 ASSERT_TRUE(self == NULL);
Elliott Hughes063525c2014-05-13 11:19:57 -0700698#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700699 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
700#else
701 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
702#endif
703}
704
Elliott Hughesad88a082012-10-24 18:37:21 -0700705static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700706 dlopen("/child/thread", RTLD_NOW);
707 return reinterpret_cast<void*>(strdup(dlerror()));
708}
709
Elliott Hughese66190d2012-12-18 15:57:55 -0800710TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700711 dlopen("/main/thread", RTLD_NOW);
712 const char* main_thread_error = dlerror();
713 ASSERT_SUBSTR("/main/thread", main_thread_error);
714
715 pthread_t t;
716 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
717 void* result;
718 ASSERT_EQ(0, pthread_join(t, &result));
719 char* child_thread_error = static_cast<char*>(result);
720 ASSERT_SUBSTR("/child/thread", child_thread_error);
721 free(child_thread_error);
722
723 ASSERT_SUBSTR("/main/thread", main_thread_error);
724}
725
Elliott Hughese66190d2012-12-18 15:57:55 -0800726TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700727 dlerror(); // Clear any pending errors.
Elliott Hughes8e15b082012-09-26 11:44:01 -0700728 void* self = dlopen(NULL, RTLD_NOW);
729 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700730 ASSERT_TRUE(dlerror() == NULL);
731
732 void* sym;
733
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700734#if defined(__BIONIC__) && !defined(__LP64__)
735 // RTLD_DEFAULT in lp32 bionic is not (void*)0
736 // so it can be distinguished from the NULL handle.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700737 sym = dlsym(NULL, "test");
738 ASSERT_TRUE(sym == NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700739 ASSERT_SUBSTR("dlsym library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700740#endif
741
742 // NULL symbol name.
Elliott Hughes063525c2014-05-13 11:19:57 -0700743#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700744 // glibc marks this parameter non-null and SEGVs if you cheat.
745 sym = dlsym(self, NULL);
746 ASSERT_TRUE(sym == NULL);
747 ASSERT_SUBSTR("", dlerror());
748#endif
749
750 // Symbol that doesn't exist.
751 sym = dlsym(self, "ThisSymbolDoesNotExist");
752 ASSERT_TRUE(sym == NULL);
753 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700754
755 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700756}
757
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700758TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700759 dlerror(); // Clear any pending errors.
760 void* self = dlopen(NULL, RTLD_NOW);
761 ASSERT_TRUE(self != NULL);
762 ASSERT_TRUE(dlerror() == NULL);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700763
764 void* sym = dlsym(self, "DlSymTestFunction");
765 ASSERT_TRUE(sym != NULL);
766
767 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
768 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
769
770 Dl_info info;
771 int rc = dladdr(addr, &info);
772 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
773
774 // Get the name of this executable.
775 char executable_path[PATH_MAX];
776 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
777 ASSERT_NE(rc, -1);
778 executable_path[rc] = '\0';
Elliott Hughes8e15b082012-09-26 11:44:01 -0700779
780 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700781 char dli_realpath[PATH_MAX];
782 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
783 ASSERT_STREQ(executable_path, dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700784
785 // The symbol name should be the symbol we looked up.
786 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
787
788 // The address should be the exact address of the symbol.
789 ASSERT_EQ(info.dli_saddr, sym);
790
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700791 std::vector<map_record> maps;
792 ASSERT_TRUE(Maps::parse_maps(&maps));
793
794 void* base_address = nullptr;
795 for (const map_record& rec : maps) {
796 if (executable_path == rec.pathname) {
797 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700798 break;
799 }
800 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700801
802 // The base address should be the address we were loaded at.
803 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700804
805 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700806}
807
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700808#if defined(__LP64__)
809#define BIONIC_PATH_TO_LIBC "/system/lib64/libc.so"
810#else
811#define BIONIC_PATH_TO_LIBC "/system/lib/libc.so"
812#endif
813
814TEST(dlfcn, dladdr_libc) {
815#if defined(__BIONIC__)
816 Dl_info info;
817 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
818 ASSERT_TRUE(dladdr(addr, &info) != 0);
819
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700820 // /system/lib is symlink when this test is executed on host.
821 char libc_realpath[PATH_MAX];
822 ASSERT_TRUE(realpath(BIONIC_PATH_TO_LIBC, libc_realpath) == libc_realpath);
823
824 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700825 // TODO: add check for dfi_fbase
826 ASSERT_STREQ("puts", info.dli_sname);
827 ASSERT_EQ(addr, info.dli_saddr);
828#else
829 GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig "
830 "for libc.so, which is symlink itself (not a realpath).\n";
831#endif
832}
833
Elliott Hughese66190d2012-12-18 15:57:55 -0800834TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700835 Dl_info info;
836
Elliott Hughes3b297c42012-10-11 16:08:51 -0700837 dlerror(); // Clear any pending errors.
838
Elliott Hughes8e15b082012-09-26 11:44:01 -0700839 // No symbol corresponding to NULL.
840 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700841 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700842
843 // No symbol corresponding to a stack address.
844 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700845 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700846}
Elliott Hughes124fae92012-10-31 14:20:03 -0700847
Elliott Hughesa43e9062013-01-07 14:18:22 -0800848// GNU-style ELF hash tables are incompatible with the MIPS ABI.
849// 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 -0800850TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800851#if !defined(__mips__)
Elliott Hughes124fae92012-10-31 14:20:03 -0700852 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800853 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
854 ASSERT_TRUE(handle != nullptr) << dlerror();
855 auto guard = make_scope_guard([&]() {
856 dlclose(handle);
857 });
858 void* sym = dlsym(handle, "getRandomNumber");
859 ASSERT_TRUE(sym != nullptr) << dlerror();
860 int (*fn)(void);
861 fn = reinterpret_cast<int (*)(void)>(sym);
862 EXPECT_EQ(4, fn());
863
864 Dl_info dlinfo;
865 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
866
867 ASSERT_TRUE(fn == dlinfo.dli_saddr);
868 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800869 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800870#else
871 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
872#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700873}
Elliott Hughese66190d2012-12-18 15:57:55 -0800874
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800875TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
876 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
877 ASSERT_TRUE(handle != nullptr) << dlerror();
878 auto guard = make_scope_guard([&]() {
879 dlclose(handle);
880 });
881 void* sym = dlsym(handle, "getRandomNumber");
882 ASSERT_TRUE(sym != nullptr) << dlerror();
883 int (*fn)(void);
884 fn = reinterpret_cast<int (*)(void)>(sym);
885 EXPECT_EQ(4, fn());
886
887 Dl_info dlinfo;
888 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
889
890 ASSERT_TRUE(fn == dlinfo.dli_saddr);
891 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
892 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
893}
894
Elliott Hughese66190d2012-12-18 15:57:55 -0800895TEST(dlfcn, dlopen_bad_flags) {
896 dlerror(); // Clear any pending errors.
897 void* handle;
898
Elliott Hughes063525c2014-05-13 11:19:57 -0700899#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800900 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
901 handle = dlopen(NULL, 0);
902 ASSERT_TRUE(handle == NULL);
903 ASSERT_SUBSTR("invalid", dlerror());
904#endif
905
906 handle = dlopen(NULL, 0xffffffff);
907 ASSERT_TRUE(handle == NULL);
908 ASSERT_SUBSTR("invalid", dlerror());
909
910 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
911 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY);
912 ASSERT_TRUE(handle != NULL);
913 ASSERT_SUBSTR(NULL, dlerror());
914}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400915
916TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800917 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400918 ASSERT_TRUE(addr == NULL);
919}
920
921TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800922 void* addr = dlsym(RTLD_DEFAULT, "fopen");
923 ASSERT_TRUE(addr != NULL);
924}
925
926TEST(dlfcn, rtld_next_unknown_symbol) {
927 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
928 ASSERT_TRUE(addr == NULL);
929}
930
931TEST(dlfcn, rtld_next_known_symbol) {
932 void* addr = dlsym(RTLD_NEXT, "fopen");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400933 ASSERT_TRUE(addr != NULL);
934}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700935
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700936TEST(dlfcn, dlsym_weak_func) {
937 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800938 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700939 ASSERT_TRUE(handle != NULL);
940
941 int (*weak_func)();
942 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
943 ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror();
944 EXPECT_EQ(42, weak_func());
945 dlclose(handle);
946}
947
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800948TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700949 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
950 ASSERT_TRUE(handle != nullptr) << dlerror();
951 int (*weak_func)();
952 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
953 ASSERT_TRUE(weak_func != nullptr) << dlerror();
954 EXPECT_EQ(6551, weak_func());
955 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800956}
957
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700958TEST(dlfcn, dlopen_symlink) {
959 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
960 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
961 ASSERT_TRUE(handle1 != NULL);
962 ASSERT_TRUE(handle2 != NULL);
963 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -0700964 dlclose(handle1);
965 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700966}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800967
968// libtest_dlopen_from_ctor_main.so depends on
969// libtest_dlopen_from_ctor.so which has a constructor
970// that calls dlopen(libc...). This is to test the situation
971// described in b/7941716.
972TEST(dlfcn, dlopen_dlopen_from_ctor) {
973#if defined(__BIONIC__)
974 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
975 ASSERT_TRUE(handle != nullptr) << dlerror();
976 dlclose(handle);
977#else
978 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
979#endif
980}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700981
982TEST(dlfcn, symbol_versioning_use_v1) {
983 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
984 ASSERT_TRUE(handle != nullptr) << dlerror();
985 typedef int (*fn_t)();
986 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
987 ASSERT_TRUE(fn != nullptr) << dlerror();
988 ASSERT_EQ(1, fn());
989 dlclose(handle);
990}
991
992TEST(dlfcn, symbol_versioning_use_v2) {
993 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
994 ASSERT_TRUE(handle != nullptr) << dlerror();
995 typedef int (*fn_t)();
996 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
997 ASSERT_TRUE(fn != nullptr) << dlerror();
998 ASSERT_EQ(2, fn());
999 dlclose(handle);
1000}
1001
1002TEST(dlfcn, symbol_versioning_use_other_v2) {
1003 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1004 ASSERT_TRUE(handle != nullptr) << dlerror();
1005 typedef int (*fn_t)();
1006 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1007 ASSERT_TRUE(fn != nullptr) << dlerror();
1008 ASSERT_EQ(20, fn());
1009 dlclose(handle);
1010}
1011
1012TEST(dlfcn, symbol_versioning_use_other_v3) {
1013 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1014 ASSERT_TRUE(handle != nullptr) << dlerror();
1015 typedef int (*fn_t)();
1016 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1017 ASSERT_TRUE(fn != nullptr) << dlerror();
1018 ASSERT_EQ(3, fn());
1019 dlclose(handle);
1020}
1021
1022TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1023 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1024 ASSERT_TRUE(handle != nullptr) << dlerror();
1025 typedef int (*fn_t)();
1026 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1027 ASSERT_TRUE(fn != nullptr) << dlerror();
1028 ASSERT_EQ(3, fn()); // the default version is 3
1029 dlclose(handle);
1030}
1031
1032// This preempts the implementation from libtest_versioned_lib.so
1033extern "C" int version_zero_function() {
1034 return 0;
1035}
1036
1037// This preempts the implementation from libtest_versioned_uselibv*.so
1038extern "C" int version_zero_function2() {
1039 return 0;
1040}