blob: 1023644c083fb4f13d81bcca7dc84f958d1bd383 [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
74 // check that we cant find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
75 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)();
80 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
81
82 ASSERT_TRUE(fn != nullptr) << dlerror();
83
84 int* ptr = fn();
85 ASSERT_TRUE(ptr != nullptr) << dlerror();
86 ASSERT_EQ(42, *ptr);
87
88 dlclose(handle);
89}
90
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -070091TEST(dlfcn, dlsym_with_dependencies) {
92 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
93 ASSERT_TRUE(handle != NULL);
94 dlerror();
95 // This symbol is in DT_NEEDED library.
96 void* sym = dlsym(handle, "getRandomNumber");
97 ASSERT_TRUE(sym != NULL);
98 int (*fn)(void);
99 fn = reinterpret_cast<int (*)(void)>(sym);
100 EXPECT_EQ(4, fn());
101 dlclose(handle);
102}
103
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700104TEST(dlfcn, dlopen_noload) {
105 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
106 ASSERT_TRUE(handle == NULL);
107 handle = dlopen("libtest_simple.so", RTLD_NOW);
108 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
109 ASSERT_TRUE(handle != NULL);
110 ASSERT_TRUE(handle2 != NULL);
111 ASSERT_TRUE(handle == handle2);
112 ASSERT_EQ(0, dlclose(handle));
113 ASSERT_EQ(0, dlclose(handle2));
114}
115
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700116TEST(dlfcn, dlopen_by_soname) {
117 static const char* soname = "libdlext_test_soname.so";
118 static const char* filename = "libdlext_test_different_soname.so";
119 // 1. Make sure there is no library with soname in default search path
120 void* handle = dlopen(soname, RTLD_NOW);
121 ASSERT_TRUE(handle == nullptr);
122
123 // 2. Load a library using filename
124 handle = dlopen(filename, RTLD_NOW);
125 ASSERT_TRUE(handle != nullptr) << dlerror();
126
127 // 3. Find library by soname
128 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
129 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
130 ASSERT_EQ(handle, handle_soname);
131
132 // 4. RTLD_NOLOAD should still work with filename
133 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
134 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
135 ASSERT_EQ(handle, handle_filename);
136
137 dlclose(handle_filename);
138 dlclose(handle_soname);
139 dlclose(handle);
140}
141
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700142// ifuncs are only supported on intel and arm64 for now
143#if defined (__aarch64__) || defined(__i386__) || defined(__x86_64__)
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700144TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700145 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700146
147 // ifunc's choice depends on whether IFUNC_CHOICE has a value
148 // first check the set case
149 setenv("IFUNC_CHOICE", "set", 1);
150 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
151 ASSERT_TRUE(handle != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700152 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
153 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700154 ASSERT_TRUE(foo_ptr != NULL);
155 ASSERT_TRUE(foo_library_ptr != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700156 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
157 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700158 dlclose(handle);
159
160 // then check the unset case
161 unsetenv("IFUNC_CHOICE");
162 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
163 ASSERT_TRUE(handle != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700164 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
165 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700166 ASSERT_TRUE(foo_ptr != NULL);
167 ASSERT_TRUE(foo_library_ptr != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700168 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
169 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0);
170 dlclose(handle);
171}
172
173TEST(dlfcn, ifunc_ctor_call) {
174 typedef const char* (*fn_ptr)();
175
176 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700177 ASSERT_TRUE(handle != nullptr) << dlerror();
178 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
179 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
180 ASSERT_STREQ("false", is_ctor_called());
181
182 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
183 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700184 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700185 dlclose(handle);
186}
187#endif
188
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700189TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
190 // This is the structure of the test library and
191 // its dt_needed libraries
192 // libtest_relo_check_dt_needed_order.so
193 // |
194 // +-> libtest_relo_check_dt_needed_order_1.so
195 // |
196 // +-> libtest_relo_check_dt_needed_order_2.so
197 //
198 // The root library references relo_test_get_answer_lib - which is defined
199 // in both dt_needed libraries, the correct relocation should
200 // use the function defined in libtest_relo_check_dt_needed_order_1.so
201 void* handle = nullptr;
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -0700202 auto guard = make_scope_guard([&]() {
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700203 dlclose(handle);
204 });
205
206 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
207 ASSERT_TRUE(handle != nullptr) << dlerror();
208
209 typedef int (*fn_t) (void);
210 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
211 ASSERT_TRUE(fn != nullptr) << dlerror();
212 ASSERT_EQ(1, fn());
213}
214
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700215TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700216 // Here is how the test library and its dt_needed
217 // libraries are arranged
218 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700219 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700220 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700221 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700222 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700223 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700224 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700225 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700226 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700227 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700228 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700229 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700230 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700231 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700232 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700233 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700234 //
235 // load order should be (1, 2, 3, a, b, d)
236 //
237 // get_answer() is defined in (2, 3, a, b, c)
238 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700239 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700240 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700241 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
242 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700243 typedef int (*fn_t) (void);
244 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700245 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700246 ASSERT_TRUE(fn != NULL) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700247 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700248 ASSERT_TRUE(fn2 != NULL) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700249
250 ASSERT_EQ(42, fn());
251 ASSERT_EQ(43, fn2());
252 dlclose(handle);
253}
254
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700255TEST(dlfcn, dlopen_check_order_reloc_siblings) {
256 // This is how this one works:
257 // we lookup and call get_answer which is defined in '_2.so'
258 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
259 // the correct _impl() is implemented by '_a.so';
260 //
261 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
262 //
263 // Here is the picture:
264 //
265 // libtest_check_order_reloc_siblings.so
266 // |
267 // +-> ..._1.so <- empty
268 // | |
269 // | +-> ..._a.so <- exports correct answer_impl()
270 // | |
271 // | +-> ..._b.so <- every other letter exporting incorrect one.
272 // |
273 // +-> ..._2.so <- empty
274 // | |
275 // | +-> ..._c.so
276 // | |
277 // | +-> ..._d.so
278 // |
279 // +-> ..._3.so <- empty
280 // |
281 // +-> ..._e.so
282 // |
283 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
284 // implements incorrect get_answer_impl()
285
286 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
287 ASSERT_TRUE(handle == nullptr);
288#ifdef __BIONIC__
289 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
290 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
291#endif
292
293 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
294 ASSERT_TRUE(handle != nullptr) << dlerror();
295
296 typedef int (*fn_t) (void);
297 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
298 ASSERT_TRUE(fn != nullptr) << dlerror();
299 ASSERT_EQ(42, fn());
300
301 ASSERT_EQ(0, dlclose(handle));
302}
303
304TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
305 // This test uses the same library as dlopen_check_order_reloc_siblings.
306 // Unlike dlopen_check_order_reloc_siblings it preloads
307 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
308 // dlopen(libtest_check_order_reloc_siblings.so)
309
310 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
311 ASSERT_TRUE(handle == nullptr);
312 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
313 ASSERT_TRUE(handle == nullptr);
314
315 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
316 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
317
318 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
319 ASSERT_TRUE(handle != nullptr) << dlerror();
320
321 ASSERT_EQ(0, dlclose(handle_for_1));
322
323 typedef int (*fn_t) (void);
324 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
325 ASSERT_TRUE(fn != nullptr) << dlerror();
326 ASSERT_EQ(42, fn());
327
328 ASSERT_EQ(0, dlclose(handle));
329}
330
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800331TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
332 // This is how this one works:
333 // we lookup and call grandchild_get_answer which is defined in '_2.so'
334 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
335 // the correct _impl() is implemented by '_c_1.so';
336 //
337 // Here is the picture of subtree:
338 //
339 // libtest_check_order_reloc_siblings.so
340 // |
341 // +-> ..._2.so <- grandchild_get_answer()
342 // |
343 // +-> ..._c.so <- empty
344 // | |
345 // | +-> _c_1.so <- exports correct answer_impl()
346 // | |
347 // | +-> _c_2.so <- exports incorrect answer_impl()
348 // |
349 // +-> ..._d.so <- empty
350
351 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
352 ASSERT_TRUE(handle == nullptr);
353#ifdef __BIONIC__
354 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
355 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
356#endif
357
358 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
359 ASSERT_TRUE(handle != nullptr) << dlerror();
360
361 typedef int (*fn_t) (void);
362 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
363 ASSERT_TRUE(fn != nullptr) << dlerror();
364 ASSERT_EQ(42, fn());
365
366 ASSERT_EQ(0, dlclose(handle));
367}
368
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700369TEST(dlfcn, dlopen_check_order_reloc_nephew) {
370 // This is how this one works:
371 // we lookup and call nephew_get_answer which is defined in '_2.so'
372 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
373 // the correct _impl() is implemented by '_a.so';
374 //
375 // Here is the picture:
376 //
377 // libtest_check_order_reloc_siblings.so
378 // |
379 // +-> ..._1.so <- empty
380 // | |
381 // | +-> ..._a.so <- exports correct answer_impl()
382 // | |
383 // | +-> ..._b.so <- every other letter exporting incorrect one.
384 // |
385 // +-> ..._2.so <- empty
386 // | |
387 // | +-> ..._c.so
388 // | |
389 // | +-> ..._d.so
390 // |
391 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
392 // |
393 // +-> ..._e.so
394 // |
395 // +-> ..._f.so
396
397 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
398 ASSERT_TRUE(handle == nullptr);
399#ifdef __BIONIC__
400 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
401 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
402#endif
403
404 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
405 ASSERT_TRUE(handle != nullptr) << dlerror();
406
407 typedef int (*fn_t) (void);
408 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
409 ASSERT_TRUE(fn != nullptr) << dlerror();
410 ASSERT_EQ(42, fn());
411
412 ASSERT_EQ(0, dlclose(handle));
413}
414
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800415TEST(dlfcn, check_unload_after_reloc) {
416 // This is how this one works:
417 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
418 // |
419 // +-> libtest_two_parents_child
420 //
421 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
422 // |
423 // +-> libtest_two_parents_child
424 //
425 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
426 // as a second step it dlopens parent2 and dlcloses parent1...
427
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700428 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
429 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800430
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700431 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
432 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800433
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700434 typedef int (*fn_t) (void);
435 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
436 ASSERT_TRUE(fn != nullptr) << dlerror();
437 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800438
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700439 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800440
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700441 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
442 ASSERT_TRUE(handle != nullptr);
443 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800444
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700445 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
446 ASSERT_TRUE(fn != nullptr) << dlerror();
447 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800448
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700449 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800450
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700451 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
452 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800453}
454
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700455extern "C" int check_order_reloc_root_get_answer_impl() {
456 return 42;
457}
458
459TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
460 // This is how this one works:
461 // we lookup and call get_answer3 which is defined in 'root.so'
462 // and in turn calls external root_get_answer_impl() defined in _2.so and
463 // above the correct _impl() is one in the executable.
464 //
465 // libtest_check_order_reloc_root.so
466 // |
467 // +-> ..._1.so <- empty
468 // |
469 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
470 //
471
472 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
473 ASSERT_TRUE(handle == nullptr);
474#ifdef __BIONIC__
475 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
476 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
477#endif
478
479 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
480 ASSERT_TRUE(handle != nullptr) << dlerror();
481
482 typedef int (*fn_t) (void);
483 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
484 ASSERT_TRUE(fn != nullptr) << dlerror();
485 ASSERT_EQ(42, fn());
486
487 ASSERT_EQ(0, dlclose(handle));
488}
489
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700490TEST(dlfcn, dlopen_check_rtld_local) {
491 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
492 ASSERT_TRUE(sym == nullptr);
493
494 // implicit RTLD_LOCAL
495 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
496 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
497 ASSERT_TRUE(sym == nullptr);
498 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
499 sym = dlsym(handle, "dlopen_testlib_simple_func");
500 ASSERT_TRUE(sym != nullptr);
501 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
502 dlclose(handle);
503
504 // explicit RTLD_LOCAL
505 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
506 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
507 ASSERT_TRUE(sym == nullptr);
508 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
509 sym = dlsym(handle, "dlopen_testlib_simple_func");
510 ASSERT_TRUE(sym != nullptr);
511 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
512 dlclose(handle);
513}
514
515TEST(dlfcn, dlopen_check_rtld_global) {
516 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
517 ASSERT_TRUE(sym == nullptr);
518
519 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700520 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700521 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
522 ASSERT_TRUE(sym != nullptr) << dlerror();
523 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
524 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700525
526 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
527 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
528 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700529}
530
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700531// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
532// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
533// libtest_with_dependency_loop_a.so
534TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700535 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
536 ASSERT_TRUE(handle != nullptr) << dlerror();
537 void* f = dlsym(handle, "dlopen_test_loopy_function");
538 ASSERT_TRUE(f != nullptr) << dlerror();
539 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
540 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700541
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700542 // dlopen second time to make sure that the library was unloaded correctly
543 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
544 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800545#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700546 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
547 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 -0700548#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800549
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700550 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
551 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700552}
553
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700554TEST(dlfcn, dlopen_nodelete) {
555 static bool is_unloaded = false;
556
557 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
558 ASSERT_TRUE(handle != nullptr) << dlerror();
559 void (*set_unload_flag_ptr)(bool*);
560 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
561 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
562 set_unload_flag_ptr(&is_unloaded);
563
564 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
565 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
566 ASSERT_EQ(1729U, *taxicab_number);
567 *taxicab_number = 2;
568
569 dlclose(handle);
570 ASSERT_TRUE(!is_unloaded);
571
572 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
573 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
574 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
575
576
577 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
578 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
579 ASSERT_EQ(taxicab_number2, taxicab_number);
580
581 ASSERT_EQ(2U, *taxicab_number2);
582
583 dlclose(handle);
584 ASSERT_TRUE(!is_unloaded);
585}
586
587TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
588 static bool is_unloaded = false;
589
590 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
591 ASSERT_TRUE(handle != nullptr) << dlerror();
592 void (*set_unload_flag_ptr)(bool*);
593 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
594 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
595 set_unload_flag_ptr(&is_unloaded);
596
597 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
598 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
599
600 ASSERT_EQ(1729U, *taxicab_number);
601 *taxicab_number = 2;
602
603 // This RTLD_NODELETE should be ignored
604 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
605 ASSERT_TRUE(handle1 != nullptr) << dlerror();
606 ASSERT_EQ(handle, handle1);
607
608 dlclose(handle1);
609 dlclose(handle);
610
611 ASSERT_TRUE(is_unloaded);
612}
613
614TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
615 static bool is_unloaded = false;
616
617 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
618 ASSERT_TRUE(handle != nullptr) << dlerror();
619 void (*set_unload_flag_ptr)(bool*);
620 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
621 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
622 set_unload_flag_ptr(&is_unloaded);
623
624 dlclose(handle);
625 ASSERT_TRUE(!is_unloaded);
626}
627
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700628TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanov4e446b12014-10-31 17:27:02 -0700629#if !defined(__arm__) && !defined(__aarch64__)
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700630 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
631 ASSERT_TRUE(handle != nullptr) << dlerror();
632 int (*get_answer)();
633 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
634 ASSERT_TRUE(get_answer != nullptr) << dlerror();
635 ASSERT_EQ(42, get_answer());
636 ASSERT_EQ(0, dlclose(handle));
637#else
Dmitriy Ivanov4e446b12014-10-31 17:27:02 -0700638 GTEST_LOG_(INFO) << "This test does nothing on arm/arm64 (to be reenabled once b/18137520 or b/18130452 are fixed).\n";
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700639#endif
640}
641
Elliott Hughese66190d2012-12-18 15:57:55 -0800642TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700643 void* self = dlopen("/does/not/exist", RTLD_NOW);
644 ASSERT_TRUE(self == NULL);
Elliott Hughes063525c2014-05-13 11:19:57 -0700645#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700646 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
647#else
648 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
649#endif
650}
651
Elliott Hughesad88a082012-10-24 18:37:21 -0700652static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700653 dlopen("/child/thread", RTLD_NOW);
654 return reinterpret_cast<void*>(strdup(dlerror()));
655}
656
Elliott Hughese66190d2012-12-18 15:57:55 -0800657TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700658 dlopen("/main/thread", RTLD_NOW);
659 const char* main_thread_error = dlerror();
660 ASSERT_SUBSTR("/main/thread", main_thread_error);
661
662 pthread_t t;
663 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
664 void* result;
665 ASSERT_EQ(0, pthread_join(t, &result));
666 char* child_thread_error = static_cast<char*>(result);
667 ASSERT_SUBSTR("/child/thread", child_thread_error);
668 free(child_thread_error);
669
670 ASSERT_SUBSTR("/main/thread", main_thread_error);
671}
672
Elliott Hughese66190d2012-12-18 15:57:55 -0800673TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700674 dlerror(); // Clear any pending errors.
Elliott Hughes8e15b082012-09-26 11:44:01 -0700675 void* self = dlopen(NULL, RTLD_NOW);
676 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700677 ASSERT_TRUE(dlerror() == NULL);
678
679 void* sym;
680
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700681#if defined(__BIONIC__) && !defined(__LP64__)
682 // RTLD_DEFAULT in lp32 bionic is not (void*)0
683 // so it can be distinguished from the NULL handle.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700684 sym = dlsym(NULL, "test");
685 ASSERT_TRUE(sym == NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700686 ASSERT_SUBSTR("dlsym library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700687#endif
688
689 // NULL symbol name.
Elliott Hughes063525c2014-05-13 11:19:57 -0700690#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700691 // glibc marks this parameter non-null and SEGVs if you cheat.
692 sym = dlsym(self, NULL);
693 ASSERT_TRUE(sym == NULL);
694 ASSERT_SUBSTR("", dlerror());
695#endif
696
697 // Symbol that doesn't exist.
698 sym = dlsym(self, "ThisSymbolDoesNotExist");
699 ASSERT_TRUE(sym == NULL);
700 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700701
702 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700703}
704
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700705TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700706 dlerror(); // Clear any pending errors.
707 void* self = dlopen(NULL, RTLD_NOW);
708 ASSERT_TRUE(self != NULL);
709 ASSERT_TRUE(dlerror() == NULL);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700710
711 void* sym = dlsym(self, "DlSymTestFunction");
712 ASSERT_TRUE(sym != NULL);
713
714 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
715 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
716
717 Dl_info info;
718 int rc = dladdr(addr, &info);
719 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
720
721 // Get the name of this executable.
722 char executable_path[PATH_MAX];
723 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
724 ASSERT_NE(rc, -1);
725 executable_path[rc] = '\0';
Elliott Hughes8e15b082012-09-26 11:44:01 -0700726
727 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700728 char dli_realpath[PATH_MAX];
729 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
730 ASSERT_STREQ(executable_path, dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700731
732 // The symbol name should be the symbol we looked up.
733 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
734
735 // The address should be the exact address of the symbol.
736 ASSERT_EQ(info.dli_saddr, sym);
737
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700738 std::vector<map_record> maps;
739 ASSERT_TRUE(Maps::parse_maps(&maps));
740
741 void* base_address = nullptr;
742 for (const map_record& rec : maps) {
743 if (executable_path == rec.pathname) {
744 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700745 break;
746 }
747 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700748
749 // The base address should be the address we were loaded at.
750 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700751
752 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700753}
754
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700755#if defined(__LP64__)
756#define BIONIC_PATH_TO_LIBC "/system/lib64/libc.so"
757#else
758#define BIONIC_PATH_TO_LIBC "/system/lib/libc.so"
759#endif
760
761TEST(dlfcn, dladdr_libc) {
762#if defined(__BIONIC__)
763 Dl_info info;
764 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
765 ASSERT_TRUE(dladdr(addr, &info) != 0);
766
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700767 // /system/lib is symlink when this test is executed on host.
768 char libc_realpath[PATH_MAX];
769 ASSERT_TRUE(realpath(BIONIC_PATH_TO_LIBC, libc_realpath) == libc_realpath);
770
771 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700772 // TODO: add check for dfi_fbase
773 ASSERT_STREQ("puts", info.dli_sname);
774 ASSERT_EQ(addr, info.dli_saddr);
775#else
776 GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig "
777 "for libc.so, which is symlink itself (not a realpath).\n";
778#endif
779}
780
Elliott Hughese66190d2012-12-18 15:57:55 -0800781TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700782 Dl_info info;
783
Elliott Hughes3b297c42012-10-11 16:08:51 -0700784 dlerror(); // Clear any pending errors.
785
Elliott Hughes8e15b082012-09-26 11:44:01 -0700786 // No symbol corresponding to NULL.
787 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700788 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700789
790 // No symbol corresponding to a stack address.
791 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700792 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700793}
Elliott Hughes124fae92012-10-31 14:20:03 -0700794
Elliott Hughesa43e9062013-01-07 14:18:22 -0800795// GNU-style ELF hash tables are incompatible with the MIPS ABI.
796// 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 -0800797TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800798#if !defined(__mips__)
Elliott Hughes124fae92012-10-31 14:20:03 -0700799 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800800 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
801 ASSERT_TRUE(handle != nullptr) << dlerror();
802 auto guard = make_scope_guard([&]() {
803 dlclose(handle);
804 });
805 void* sym = dlsym(handle, "getRandomNumber");
806 ASSERT_TRUE(sym != nullptr) << dlerror();
807 int (*fn)(void);
808 fn = reinterpret_cast<int (*)(void)>(sym);
809 EXPECT_EQ(4, fn());
810
811 Dl_info dlinfo;
812 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
813
814 ASSERT_TRUE(fn == dlinfo.dli_saddr);
815 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800816 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800817#else
818 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
819#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700820}
Elliott Hughese66190d2012-12-18 15:57:55 -0800821
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800822TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
823 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
824 ASSERT_TRUE(handle != nullptr) << dlerror();
825 auto guard = make_scope_guard([&]() {
826 dlclose(handle);
827 });
828 void* sym = dlsym(handle, "getRandomNumber");
829 ASSERT_TRUE(sym != nullptr) << dlerror();
830 int (*fn)(void);
831 fn = reinterpret_cast<int (*)(void)>(sym);
832 EXPECT_EQ(4, fn());
833
834 Dl_info dlinfo;
835 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
836
837 ASSERT_TRUE(fn == dlinfo.dli_saddr);
838 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
839 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
840}
841
Elliott Hughese66190d2012-12-18 15:57:55 -0800842TEST(dlfcn, dlopen_bad_flags) {
843 dlerror(); // Clear any pending errors.
844 void* handle;
845
Elliott Hughes063525c2014-05-13 11:19:57 -0700846#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800847 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
848 handle = dlopen(NULL, 0);
849 ASSERT_TRUE(handle == NULL);
850 ASSERT_SUBSTR("invalid", dlerror());
851#endif
852
853 handle = dlopen(NULL, 0xffffffff);
854 ASSERT_TRUE(handle == NULL);
855 ASSERT_SUBSTR("invalid", dlerror());
856
857 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
858 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY);
859 ASSERT_TRUE(handle != NULL);
860 ASSERT_SUBSTR(NULL, dlerror());
861}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400862
863TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800864 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400865 ASSERT_TRUE(addr == NULL);
866}
867
868TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800869 void* addr = dlsym(RTLD_DEFAULT, "fopen");
870 ASSERT_TRUE(addr != NULL);
871}
872
873TEST(dlfcn, rtld_next_unknown_symbol) {
874 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
875 ASSERT_TRUE(addr == NULL);
876}
877
878TEST(dlfcn, rtld_next_known_symbol) {
879 void* addr = dlsym(RTLD_NEXT, "fopen");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400880 ASSERT_TRUE(addr != NULL);
881}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700882
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700883TEST(dlfcn, dlsym_weak_func) {
884 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800885 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700886 ASSERT_TRUE(handle != NULL);
887
888 int (*weak_func)();
889 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
890 ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror();
891 EXPECT_EQ(42, weak_func());
892 dlclose(handle);
893}
894
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800895TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700896 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
897 ASSERT_TRUE(handle != nullptr) << dlerror();
898 int (*weak_func)();
899 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
900 ASSERT_TRUE(weak_func != nullptr) << dlerror();
901 EXPECT_EQ(6551, weak_func());
902 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800903}
904
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700905TEST(dlfcn, dlopen_symlink) {
906 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
907 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
908 ASSERT_TRUE(handle1 != NULL);
909 ASSERT_TRUE(handle2 != NULL);
910 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -0700911 dlclose(handle1);
912 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700913}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800914
915// libtest_dlopen_from_ctor_main.so depends on
916// libtest_dlopen_from_ctor.so which has a constructor
917// that calls dlopen(libc...). This is to test the situation
918// described in b/7941716.
919TEST(dlfcn, dlopen_dlopen_from_ctor) {
920#if defined(__BIONIC__)
921 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
922 ASSERT_TRUE(handle != nullptr) << dlerror();
923 dlclose(handle);
924#else
925 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
926#endif
927}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700928
929TEST(dlfcn, symbol_versioning_use_v1) {
930 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
931 ASSERT_TRUE(handle != nullptr) << dlerror();
932 typedef int (*fn_t)();
933 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
934 ASSERT_TRUE(fn != nullptr) << dlerror();
935 ASSERT_EQ(1, fn());
936 dlclose(handle);
937}
938
939TEST(dlfcn, symbol_versioning_use_v2) {
940 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
941 ASSERT_TRUE(handle != nullptr) << dlerror();
942 typedef int (*fn_t)();
943 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
944 ASSERT_TRUE(fn != nullptr) << dlerror();
945 ASSERT_EQ(2, fn());
946 dlclose(handle);
947}
948
949TEST(dlfcn, symbol_versioning_use_other_v2) {
950 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
951 ASSERT_TRUE(handle != nullptr) << dlerror();
952 typedef int (*fn_t)();
953 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
954 ASSERT_TRUE(fn != nullptr) << dlerror();
955 ASSERT_EQ(20, fn());
956 dlclose(handle);
957}
958
959TEST(dlfcn, symbol_versioning_use_other_v3) {
960 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
961 ASSERT_TRUE(handle != nullptr) << dlerror();
962 typedef int (*fn_t)();
963 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
964 ASSERT_TRUE(fn != nullptr) << dlerror();
965 ASSERT_EQ(3, fn());
966 dlclose(handle);
967}
968
969TEST(dlfcn, symbol_versioning_default_via_dlsym) {
970 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
971 ASSERT_TRUE(handle != nullptr) << dlerror();
972 typedef int (*fn_t)();
973 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
974 ASSERT_TRUE(fn != nullptr) << dlerror();
975 ASSERT_EQ(3, fn()); // the default version is 3
976 dlclose(handle);
977}
978
979// This preempts the implementation from libtest_versioned_lib.so
980extern "C" int version_zero_function() {
981 return 0;
982}
983
984// This preempts the implementation from libtest_versioned_uselibv*.so
985extern "C" int version_zero_function2() {
986 return 0;
987}