blob: dfb5e54ab202349b10a0905f3e97d820001051a1 [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
Elliott Hughes3b297c42012-10-11 16:08:51 -070029#define ASSERT_SUBSTR(needle, haystack) \
30 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
31
Elliott Hughes1728b232014-05-14 10:02:03 -070032static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070033extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070034 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070035}
36
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070037static int g_ctor_function_called = 0;
38
39extern "C" void ctor_function() __attribute__ ((constructor));
40
41extern "C" void ctor_function() {
42 g_ctor_function_called = 17;
43}
44
45TEST(dlfcn, ctor_function_call) {
46 ASSERT_EQ(17, g_ctor_function_called);
47}
48
Elliott Hughese66190d2012-12-18 15:57:55 -080049TEST(dlfcn, dlsym_in_self) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070050 dlerror(); // Clear any pending errors.
jeffhaoacf5aa72012-09-12 17:25:30 -070051 void* self = dlopen(NULL, RTLD_NOW);
52 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -070053 ASSERT_TRUE(dlerror() == NULL);
jeffhaoacf5aa72012-09-12 17:25:30 -070054
55 void* sym = dlsym(self, "DlSymTestFunction");
56 ASSERT_TRUE(sym != NULL);
57
58 void (*function)() = reinterpret_cast<void(*)()>(sym);
59
Elliott Hughes1728b232014-05-14 10:02:03 -070060 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070061 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070062 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070063
64 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070065}
Elliott Hughes8e15b082012-09-26 11:44:01 -070066
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -070067TEST(dlfcn, dlsym_with_dependencies) {
68 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
69 ASSERT_TRUE(handle != NULL);
70 dlerror();
71 // This symbol is in DT_NEEDED library.
72 void* sym = dlsym(handle, "getRandomNumber");
73 ASSERT_TRUE(sym != NULL);
74 int (*fn)(void);
75 fn = reinterpret_cast<int (*)(void)>(sym);
76 EXPECT_EQ(4, fn());
77 dlclose(handle);
78}
79
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -070080TEST(dlfcn, dlopen_noload) {
81 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
82 ASSERT_TRUE(handle == NULL);
83 handle = dlopen("libtest_simple.so", RTLD_NOW);
84 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
85 ASSERT_TRUE(handle != NULL);
86 ASSERT_TRUE(handle2 != NULL);
87 ASSERT_TRUE(handle == handle2);
88 ASSERT_EQ(0, dlclose(handle));
89 ASSERT_EQ(0, dlclose(handle2));
90}
91
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -070092// ifuncs are only supported on intel and arm64 for now
93#if defined (__aarch64__) || defined(__i386__) || defined(__x86_64__)
Brigid Smithc5a13ef2014-07-23 11:22:25 -070094TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -070095 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -070096
97 // ifunc's choice depends on whether IFUNC_CHOICE has a value
98 // first check the set case
99 setenv("IFUNC_CHOICE", "set", 1);
100 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
101 ASSERT_TRUE(handle != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700102 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
103 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700104 ASSERT_TRUE(foo_ptr != NULL);
105 ASSERT_TRUE(foo_library_ptr != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700106 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
107 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700108 dlclose(handle);
109
110 // then check the unset case
111 unsetenv("IFUNC_CHOICE");
112 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
113 ASSERT_TRUE(handle != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700114 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
115 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700116 ASSERT_TRUE(foo_ptr != NULL);
117 ASSERT_TRUE(foo_library_ptr != NULL);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700118 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
119 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0);
120 dlclose(handle);
121}
122
123TEST(dlfcn, ifunc_ctor_call) {
124 typedef const char* (*fn_ptr)();
125
126 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700127 ASSERT_TRUE(handle != nullptr) << dlerror();
128 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
129 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
130 ASSERT_STREQ("false", is_ctor_called());
131
132 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
133 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700134 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700135 dlclose(handle);
136}
137#endif
138
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700139TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
140 // This is the structure of the test library and
141 // its dt_needed libraries
142 // libtest_relo_check_dt_needed_order.so
143 // |
144 // +-> libtest_relo_check_dt_needed_order_1.so
145 // |
146 // +-> libtest_relo_check_dt_needed_order_2.so
147 //
148 // The root library references relo_test_get_answer_lib - which is defined
149 // in both dt_needed libraries, the correct relocation should
150 // use the function defined in libtest_relo_check_dt_needed_order_1.so
151 void* handle = nullptr;
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -0700152 auto guard = make_scope_guard([&]() {
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700153 dlclose(handle);
154 });
155
156 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
157 ASSERT_TRUE(handle != nullptr) << dlerror();
158
159 typedef int (*fn_t) (void);
160 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
161 ASSERT_TRUE(fn != nullptr) << dlerror();
162 ASSERT_EQ(1, fn());
163}
164
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700165TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700166 // Here is how the test library and its dt_needed
167 // libraries are arranged
168 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700169 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700170 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700171 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700172 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700173 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700174 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700175 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700176 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700177 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700178 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700179 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700180 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700181 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700182 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700183 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700184 //
185 // load order should be (1, 2, 3, a, b, d)
186 //
187 // get_answer() is defined in (2, 3, a, b, c)
188 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700189 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700190 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700191 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
192 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700193 typedef int (*fn_t) (void);
194 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700195 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700196 ASSERT_TRUE(fn != NULL) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700197 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700198 ASSERT_TRUE(fn2 != NULL) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700199
200 ASSERT_EQ(42, fn());
201 ASSERT_EQ(43, fn2());
202 dlclose(handle);
203}
204
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700205TEST(dlfcn, dlopen_check_order_reloc_siblings) {
206 // This is how this one works:
207 // we lookup and call get_answer which is defined in '_2.so'
208 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
209 // the correct _impl() is implemented by '_a.so';
210 //
211 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
212 //
213 // Here is the picture:
214 //
215 // libtest_check_order_reloc_siblings.so
216 // |
217 // +-> ..._1.so <- empty
218 // | |
219 // | +-> ..._a.so <- exports correct answer_impl()
220 // | |
221 // | +-> ..._b.so <- every other letter exporting incorrect one.
222 // |
223 // +-> ..._2.so <- empty
224 // | |
225 // | +-> ..._c.so
226 // | |
227 // | +-> ..._d.so
228 // |
229 // +-> ..._3.so <- empty
230 // |
231 // +-> ..._e.so
232 // |
233 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
234 // implements incorrect get_answer_impl()
235
236 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
237 ASSERT_TRUE(handle == nullptr);
238#ifdef __BIONIC__
239 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
240 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
241#endif
242
243 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
244 ASSERT_TRUE(handle != nullptr) << dlerror();
245
246 typedef int (*fn_t) (void);
247 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
248 ASSERT_TRUE(fn != nullptr) << dlerror();
249 ASSERT_EQ(42, fn());
250
251 ASSERT_EQ(0, dlclose(handle));
252}
253
254TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
255 // This test uses the same library as dlopen_check_order_reloc_siblings.
256 // Unlike dlopen_check_order_reloc_siblings it preloads
257 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
258 // dlopen(libtest_check_order_reloc_siblings.so)
259
260 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
261 ASSERT_TRUE(handle == nullptr);
262 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
263 ASSERT_TRUE(handle == nullptr);
264
265 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
266 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
267
268 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
269 ASSERT_TRUE(handle != nullptr) << dlerror();
270
271 ASSERT_EQ(0, dlclose(handle_for_1));
272
273 typedef int (*fn_t) (void);
274 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
275 ASSERT_TRUE(fn != nullptr) << dlerror();
276 ASSERT_EQ(42, fn());
277
278 ASSERT_EQ(0, dlclose(handle));
279}
280
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800281TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
282 // This is how this one works:
283 // we lookup and call grandchild_get_answer which is defined in '_2.so'
284 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
285 // the correct _impl() is implemented by '_c_1.so';
286 //
287 // Here is the picture of subtree:
288 //
289 // libtest_check_order_reloc_siblings.so
290 // |
291 // +-> ..._2.so <- grandchild_get_answer()
292 // |
293 // +-> ..._c.so <- empty
294 // | |
295 // | +-> _c_1.so <- exports correct answer_impl()
296 // | |
297 // | +-> _c_2.so <- exports incorrect answer_impl()
298 // |
299 // +-> ..._d.so <- empty
300
301 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
302 ASSERT_TRUE(handle == nullptr);
303#ifdef __BIONIC__
304 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
305 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
306#endif
307
308 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
309 ASSERT_TRUE(handle != nullptr) << dlerror();
310
311 typedef int (*fn_t) (void);
312 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
313 ASSERT_TRUE(fn != nullptr) << dlerror();
314 ASSERT_EQ(42, fn());
315
316 ASSERT_EQ(0, dlclose(handle));
317}
318
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700319TEST(dlfcn, dlopen_check_order_reloc_nephew) {
320 // This is how this one works:
321 // we lookup and call nephew_get_answer which is defined in '_2.so'
322 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
323 // the correct _impl() is implemented by '_a.so';
324 //
325 // Here is the picture:
326 //
327 // libtest_check_order_reloc_siblings.so
328 // |
329 // +-> ..._1.so <- empty
330 // | |
331 // | +-> ..._a.so <- exports correct answer_impl()
332 // | |
333 // | +-> ..._b.so <- every other letter exporting incorrect one.
334 // |
335 // +-> ..._2.so <- empty
336 // | |
337 // | +-> ..._c.so
338 // | |
339 // | +-> ..._d.so
340 // |
341 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
342 // |
343 // +-> ..._e.so
344 // |
345 // +-> ..._f.so
346
347 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
348 ASSERT_TRUE(handle == nullptr);
349#ifdef __BIONIC__
350 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
351 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
352#endif
353
354 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
355 ASSERT_TRUE(handle != nullptr) << dlerror();
356
357 typedef int (*fn_t) (void);
358 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
359 ASSERT_TRUE(fn != nullptr) << dlerror();
360 ASSERT_EQ(42, fn());
361
362 ASSERT_EQ(0, dlclose(handle));
363}
364
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800365TEST(dlfcn, check_unload_after_reloc) {
366 // This is how this one works:
367 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
368 // |
369 // +-> libtest_two_parents_child
370 //
371 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
372 // |
373 // +-> libtest_two_parents_child
374 //
375 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
376 // as a second step it dlopens parent2 and dlcloses parent1...
377
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700378 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
379 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800380
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700381 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
382 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800383
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700384 typedef int (*fn_t) (void);
385 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
386 ASSERT_TRUE(fn != nullptr) << dlerror();
387 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800388
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700389 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800390
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700391 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
392 ASSERT_TRUE(handle != nullptr);
393 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800394
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700395 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
396 ASSERT_TRUE(fn != nullptr) << dlerror();
397 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800398
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700399 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800400
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700401 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
402 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800403}
404
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700405extern "C" int check_order_reloc_root_get_answer_impl() {
406 return 42;
407}
408
409TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
410 // This is how this one works:
411 // we lookup and call get_answer3 which is defined in 'root.so'
412 // and in turn calls external root_get_answer_impl() defined in _2.so and
413 // above the correct _impl() is one in the executable.
414 //
415 // libtest_check_order_reloc_root.so
416 // |
417 // +-> ..._1.so <- empty
418 // |
419 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
420 //
421
422 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
423 ASSERT_TRUE(handle == nullptr);
424#ifdef __BIONIC__
425 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
426 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
427#endif
428
429 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
430 ASSERT_TRUE(handle != nullptr) << dlerror();
431
432 typedef int (*fn_t) (void);
433 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
434 ASSERT_TRUE(fn != nullptr) << dlerror();
435 ASSERT_EQ(42, fn());
436
437 ASSERT_EQ(0, dlclose(handle));
438}
439
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700440TEST(dlfcn, dlopen_check_rtld_local) {
441 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
442 ASSERT_TRUE(sym == nullptr);
443
444 // implicit RTLD_LOCAL
445 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
446 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
447 ASSERT_TRUE(sym == nullptr);
448 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
449 sym = dlsym(handle, "dlopen_testlib_simple_func");
450 ASSERT_TRUE(sym != nullptr);
451 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
452 dlclose(handle);
453
454 // explicit RTLD_LOCAL
455 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
456 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
457 ASSERT_TRUE(sym == nullptr);
458 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
459 sym = dlsym(handle, "dlopen_testlib_simple_func");
460 ASSERT_TRUE(sym != nullptr);
461 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
462 dlclose(handle);
463}
464
465TEST(dlfcn, dlopen_check_rtld_global) {
466 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
467 ASSERT_TRUE(sym == nullptr);
468
469 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700470 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700471 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
472 ASSERT_TRUE(sym != nullptr) << dlerror();
473 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
474 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700475
476 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
477 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
478 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700479}
480
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700481// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
482// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
483// libtest_with_dependency_loop_a.so
484TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700485 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
486 ASSERT_TRUE(handle != nullptr) << dlerror();
487 void* f = dlsym(handle, "dlopen_test_loopy_function");
488 ASSERT_TRUE(f != nullptr) << dlerror();
489 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
490 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700491
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700492 // dlopen second time to make sure that the library was unloaded correctly
493 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
494 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800495#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700496 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
497 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 -0700498#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800499
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700500 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
501 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700502}
503
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700504TEST(dlfcn, dlopen_nodelete) {
505 static bool is_unloaded = false;
506
507 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
508 ASSERT_TRUE(handle != nullptr) << dlerror();
509 void (*set_unload_flag_ptr)(bool*);
510 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
511 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
512 set_unload_flag_ptr(&is_unloaded);
513
514 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
515 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
516 ASSERT_EQ(1729U, *taxicab_number);
517 *taxicab_number = 2;
518
519 dlclose(handle);
520 ASSERT_TRUE(!is_unloaded);
521
522 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
523 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
524 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
525
526
527 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
528 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
529 ASSERT_EQ(taxicab_number2, taxicab_number);
530
531 ASSERT_EQ(2U, *taxicab_number2);
532
533 dlclose(handle);
534 ASSERT_TRUE(!is_unloaded);
535}
536
537TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
538 static bool is_unloaded = false;
539
540 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
541 ASSERT_TRUE(handle != nullptr) << dlerror();
542 void (*set_unload_flag_ptr)(bool*);
543 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
544 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
545 set_unload_flag_ptr(&is_unloaded);
546
547 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
548 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
549
550 ASSERT_EQ(1729U, *taxicab_number);
551 *taxicab_number = 2;
552
553 // This RTLD_NODELETE should be ignored
554 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
555 ASSERT_TRUE(handle1 != nullptr) << dlerror();
556 ASSERT_EQ(handle, handle1);
557
558 dlclose(handle1);
559 dlclose(handle);
560
561 ASSERT_TRUE(is_unloaded);
562}
563
564TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
565 static bool is_unloaded = false;
566
567 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
568 ASSERT_TRUE(handle != nullptr) << dlerror();
569 void (*set_unload_flag_ptr)(bool*);
570 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
571 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
572 set_unload_flag_ptr(&is_unloaded);
573
574 dlclose(handle);
575 ASSERT_TRUE(!is_unloaded);
576}
577
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700578TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanov4e446b12014-10-31 17:27:02 -0700579#if !defined(__arm__) && !defined(__aarch64__)
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700580 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
581 ASSERT_TRUE(handle != nullptr) << dlerror();
582 int (*get_answer)();
583 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
584 ASSERT_TRUE(get_answer != nullptr) << dlerror();
585 ASSERT_EQ(42, get_answer());
586 ASSERT_EQ(0, dlclose(handle));
587#else
Dmitriy Ivanov4e446b12014-10-31 17:27:02 -0700588 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 -0700589#endif
590}
591
Elliott Hughese66190d2012-12-18 15:57:55 -0800592TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700593 void* self = dlopen("/does/not/exist", RTLD_NOW);
594 ASSERT_TRUE(self == NULL);
Elliott Hughes063525c2014-05-13 11:19:57 -0700595#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700596 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
597#else
598 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
599#endif
600}
601
Elliott Hughesad88a082012-10-24 18:37:21 -0700602static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700603 dlopen("/child/thread", RTLD_NOW);
604 return reinterpret_cast<void*>(strdup(dlerror()));
605}
606
Elliott Hughese66190d2012-12-18 15:57:55 -0800607TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700608 dlopen("/main/thread", RTLD_NOW);
609 const char* main_thread_error = dlerror();
610 ASSERT_SUBSTR("/main/thread", main_thread_error);
611
612 pthread_t t;
613 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
614 void* result;
615 ASSERT_EQ(0, pthread_join(t, &result));
616 char* child_thread_error = static_cast<char*>(result);
617 ASSERT_SUBSTR("/child/thread", child_thread_error);
618 free(child_thread_error);
619
620 ASSERT_SUBSTR("/main/thread", main_thread_error);
621}
622
Elliott Hughese66190d2012-12-18 15:57:55 -0800623TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700624 dlerror(); // Clear any pending errors.
Elliott Hughes8e15b082012-09-26 11:44:01 -0700625 void* self = dlopen(NULL, RTLD_NOW);
626 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700627 ASSERT_TRUE(dlerror() == NULL);
628
629 void* sym;
630
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700631#if defined(__BIONIC__) && !defined(__LP64__)
632 // RTLD_DEFAULT in lp32 bionic is not (void*)0
633 // so it can be distinguished from the NULL handle.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700634 sym = dlsym(NULL, "test");
635 ASSERT_TRUE(sym == NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700636 ASSERT_SUBSTR("dlsym library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700637#endif
638
639 // NULL symbol name.
Elliott Hughes063525c2014-05-13 11:19:57 -0700640#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700641 // glibc marks this parameter non-null and SEGVs if you cheat.
642 sym = dlsym(self, NULL);
643 ASSERT_TRUE(sym == NULL);
644 ASSERT_SUBSTR("", dlerror());
645#endif
646
647 // Symbol that doesn't exist.
648 sym = dlsym(self, "ThisSymbolDoesNotExist");
649 ASSERT_TRUE(sym == NULL);
650 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700651
652 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700653}
654
Elliott Hughese66190d2012-12-18 15:57:55 -0800655TEST(dlfcn, dladdr) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700656 dlerror(); // Clear any pending errors.
657 void* self = dlopen(NULL, RTLD_NOW);
658 ASSERT_TRUE(self != NULL);
659 ASSERT_TRUE(dlerror() == NULL);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700660
661 void* sym = dlsym(self, "DlSymTestFunction");
662 ASSERT_TRUE(sym != NULL);
663
664 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
665 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
666
667 Dl_info info;
668 int rc = dladdr(addr, &info);
669 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
670
671 // Get the name of this executable.
672 char executable_path[PATH_MAX];
673 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
674 ASSERT_NE(rc, -1);
675 executable_path[rc] = '\0';
676 std::string executable_name(basename(executable_path));
677
678 // The filename should be that of this executable.
679 // Note that we don't know whether or not we have the full path, so we want an "ends_with" test.
680 std::string dli_fname(info.dli_fname);
681 dli_fname = basename(&dli_fname[0]);
682 ASSERT_EQ(dli_fname, executable_name);
683
684 // The symbol name should be the symbol we looked up.
685 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
686
687 // The address should be the exact address of the symbol.
688 ASSERT_EQ(info.dli_saddr, sym);
689
690 // Look in /proc/pid/maps to find out what address we were loaded at.
691 // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic.
692 void* base_address = NULL;
Elliott Hughes8e15b082012-09-26 11:44:01 -0700693 char line[BUFSIZ];
Elliott Hughes57b7a612014-08-25 17:26:50 -0700694 FILE* fp = fopen("/proc/self/maps", "r");
Elliott Hughes8e15b082012-09-26 11:44:01 -0700695 ASSERT_TRUE(fp != NULL);
696 while (fgets(line, sizeof(line), fp) != NULL) {
697 uintptr_t start = strtoul(line, 0, 16);
698 line[strlen(line) - 1] = '\0'; // Chomp the '\n'.
699 char* path = strchr(line, '/');
Elliott Hughes156da962012-10-09 17:14:56 -0700700 if (path != NULL && strcmp(executable_path, path) == 0) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700701 base_address = reinterpret_cast<void*>(start);
702 break;
703 }
704 }
705 fclose(fp);
706
707 // The base address should be the address we were loaded at.
708 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700709
710 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700711}
712
Elliott Hughese66190d2012-12-18 15:57:55 -0800713TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700714 Dl_info info;
715
Elliott Hughes3b297c42012-10-11 16:08:51 -0700716 dlerror(); // Clear any pending errors.
717
Elliott Hughes8e15b082012-09-26 11:44:01 -0700718 // No symbol corresponding to NULL.
719 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700720 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700721
722 // No symbol corresponding to a stack address.
723 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700724 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700725}
Elliott Hughes124fae92012-10-31 14:20:03 -0700726
Elliott Hughesa43e9062013-01-07 14:18:22 -0800727// GNU-style ELF hash tables are incompatible with the MIPS ABI.
728// 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 -0800729TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800730#if !defined(__mips__)
Elliott Hughes124fae92012-10-31 14:20:03 -0700731 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800732 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
733 ASSERT_TRUE(handle != nullptr) << dlerror();
734 auto guard = make_scope_guard([&]() {
735 dlclose(handle);
736 });
737 void* sym = dlsym(handle, "getRandomNumber");
738 ASSERT_TRUE(sym != nullptr) << dlerror();
739 int (*fn)(void);
740 fn = reinterpret_cast<int (*)(void)>(sym);
741 EXPECT_EQ(4, fn());
742
743 Dl_info dlinfo;
744 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
745
746 ASSERT_TRUE(fn == dlinfo.dli_saddr);
747 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800748 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800749#else
750 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
751#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700752}
Elliott Hughese66190d2012-12-18 15:57:55 -0800753
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800754TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
755 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
756 ASSERT_TRUE(handle != nullptr) << dlerror();
757 auto guard = make_scope_guard([&]() {
758 dlclose(handle);
759 });
760 void* sym = dlsym(handle, "getRandomNumber");
761 ASSERT_TRUE(sym != nullptr) << dlerror();
762 int (*fn)(void);
763 fn = reinterpret_cast<int (*)(void)>(sym);
764 EXPECT_EQ(4, fn());
765
766 Dl_info dlinfo;
767 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
768
769 ASSERT_TRUE(fn == dlinfo.dli_saddr);
770 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
771 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
772}
773
Elliott Hughese66190d2012-12-18 15:57:55 -0800774TEST(dlfcn, dlopen_bad_flags) {
775 dlerror(); // Clear any pending errors.
776 void* handle;
777
Elliott Hughes063525c2014-05-13 11:19:57 -0700778#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800779 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
780 handle = dlopen(NULL, 0);
781 ASSERT_TRUE(handle == NULL);
782 ASSERT_SUBSTR("invalid", dlerror());
783#endif
784
785 handle = dlopen(NULL, 0xffffffff);
786 ASSERT_TRUE(handle == NULL);
787 ASSERT_SUBSTR("invalid", dlerror());
788
789 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
790 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY);
791 ASSERT_TRUE(handle != NULL);
792 ASSERT_SUBSTR(NULL, dlerror());
793}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400794
795TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800796 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400797 ASSERT_TRUE(addr == NULL);
798}
799
800TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800801 void* addr = dlsym(RTLD_DEFAULT, "fopen");
802 ASSERT_TRUE(addr != NULL);
803}
804
805TEST(dlfcn, rtld_next_unknown_symbol) {
806 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
807 ASSERT_TRUE(addr == NULL);
808}
809
810TEST(dlfcn, rtld_next_known_symbol) {
811 void* addr = dlsym(RTLD_NEXT, "fopen");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400812 ASSERT_TRUE(addr != NULL);
813}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700814
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700815TEST(dlfcn, dlsym_weak_func) {
816 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800817 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700818 ASSERT_TRUE(handle != NULL);
819
820 int (*weak_func)();
821 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
822 ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror();
823 EXPECT_EQ(42, weak_func());
824 dlclose(handle);
825}
826
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800827TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700828 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
829 ASSERT_TRUE(handle != nullptr) << dlerror();
830 int (*weak_func)();
831 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
832 ASSERT_TRUE(weak_func != nullptr) << dlerror();
833 EXPECT_EQ(6551, weak_func());
834 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800835}
836
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700837TEST(dlfcn, dlopen_symlink) {
838 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
839 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
840 ASSERT_TRUE(handle1 != NULL);
841 ASSERT_TRUE(handle2 != NULL);
842 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -0700843 dlclose(handle1);
844 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700845}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800846
847// libtest_dlopen_from_ctor_main.so depends on
848// libtest_dlopen_from_ctor.so which has a constructor
849// that calls dlopen(libc...). This is to test the situation
850// described in b/7941716.
851TEST(dlfcn, dlopen_dlopen_from_ctor) {
852#if defined(__BIONIC__)
853 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
854 ASSERT_TRUE(handle != nullptr) << dlerror();
855 dlclose(handle);
856#else
857 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
858#endif
859}