blob: 11231634b8072b641ce8165dd8e11439d4bc485d [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.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070054 void* self = dlopen(nullptr, RTLD_NOW);
55 ASSERT_TRUE(self != nullptr);
56 ASSERT_TRUE(dlerror() == nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070057
58 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070059 ASSERT_TRUE(sym != nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070060
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 Ivanovf439b5a2015-05-30 13:04:39 -0700148TEST(dlfcn, dlsym_handle_global_sym) {
149 // check that we do not look into global group
150 // when looking up symbol by handle
151 void* handle = dlopen("libtest_empty.so", RTLD_NOW);
152 dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL);
153 void* sym = dlsym(handle, "getRandomNumber");
154 ASSERT_TRUE(sym == nullptr);
155 ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror());
156
157 sym = dlsym(handle, "DlSymTestFunction");
158 ASSERT_TRUE(sym == nullptr);
159 ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror());
160 dlclose(handle);
161}
162
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700163TEST(dlfcn, dlsym_with_dependencies) {
164 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700165 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700166 dlerror();
167 // This symbol is in DT_NEEDED library.
168 void* sym = dlsym(handle, "getRandomNumber");
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700169 ASSERT_TRUE(sym != nullptr) << dlerror();
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700170 int (*fn)(void);
171 fn = reinterpret_cast<int (*)(void)>(sym);
172 EXPECT_EQ(4, fn());
173 dlclose(handle);
174}
175
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700176TEST(dlfcn, dlopen_noload) {
177 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700178 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700179 handle = dlopen("libtest_simple.so", RTLD_NOW);
180 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700181 ASSERT_TRUE(handle != nullptr);
182 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700183 ASSERT_TRUE(handle == handle2);
184 ASSERT_EQ(0, dlclose(handle));
185 ASSERT_EQ(0, dlclose(handle2));
186}
187
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700188TEST(dlfcn, dlopen_by_soname) {
189 static const char* soname = "libdlext_test_soname.so";
190 static const char* filename = "libdlext_test_different_soname.so";
191 // 1. Make sure there is no library with soname in default search path
192 void* handle = dlopen(soname, RTLD_NOW);
193 ASSERT_TRUE(handle == nullptr);
194
195 // 2. Load a library using filename
196 handle = dlopen(filename, RTLD_NOW);
197 ASSERT_TRUE(handle != nullptr) << dlerror();
198
199 // 3. Find library by soname
200 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
201 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
202 ASSERT_EQ(handle, handle_soname);
203
204 // 4. RTLD_NOLOAD should still work with filename
205 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
206 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
207 ASSERT_EQ(handle, handle_filename);
208
209 dlclose(handle_filename);
210 dlclose(handle_soname);
211 dlclose(handle);
212}
213
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700214TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700215 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700216
217 // ifunc's choice depends on whether IFUNC_CHOICE has a value
218 // first check the set case
219 setenv("IFUNC_CHOICE", "set", 1);
220 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700221 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700222 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
223 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700224 ASSERT_TRUE(foo_ptr != nullptr);
225 ASSERT_TRUE(foo_library_ptr != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700226 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
227 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700228 dlclose(handle);
229
230 // then check the unset case
231 unsetenv("IFUNC_CHOICE");
232 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700233 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700234 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
235 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700236 ASSERT_TRUE(foo_ptr != nullptr);
237 ASSERT_TRUE(foo_library_ptr != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700238 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
239 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0);
240 dlclose(handle);
241}
242
243TEST(dlfcn, ifunc_ctor_call) {
244 typedef const char* (*fn_ptr)();
245
246 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700247 ASSERT_TRUE(handle != nullptr) << dlerror();
248 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
249 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
250 ASSERT_STREQ("false", is_ctor_called());
251
252 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
253 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700254 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700255 dlclose(handle);
256}
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700257
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700258TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
259 // This is the structure of the test library and
260 // its dt_needed libraries
261 // libtest_relo_check_dt_needed_order.so
262 // |
263 // +-> libtest_relo_check_dt_needed_order_1.so
264 // |
265 // +-> libtest_relo_check_dt_needed_order_2.so
266 //
267 // The root library references relo_test_get_answer_lib - which is defined
268 // in both dt_needed libraries, the correct relocation should
269 // use the function defined in libtest_relo_check_dt_needed_order_1.so
270 void* handle = nullptr;
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -0700271 auto guard = make_scope_guard([&]() {
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700272 dlclose(handle);
273 });
274
275 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
276 ASSERT_TRUE(handle != nullptr) << dlerror();
277
278 typedef int (*fn_t) (void);
279 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
280 ASSERT_TRUE(fn != nullptr) << dlerror();
281 ASSERT_EQ(1, fn());
282}
283
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700284TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700285 // Here is how the test library and its dt_needed
286 // libraries are arranged
287 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700288 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700289 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700290 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700291 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700292 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700293 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700294 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700295 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700296 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700297 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700298 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700299 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700300 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700301 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700302 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700303 //
304 // load order should be (1, 2, 3, a, b, d)
305 //
306 // get_answer() is defined in (2, 3, a, b, c)
307 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700308 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700309 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700310 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
311 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700312 typedef int (*fn_t) (void);
313 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700314 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700315 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700316 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700317 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700318
319 ASSERT_EQ(42, fn());
320 ASSERT_EQ(43, fn2());
321 dlclose(handle);
322}
323
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700324TEST(dlfcn, dlopen_check_order_reloc_siblings) {
325 // This is how this one works:
326 // we lookup and call get_answer which is defined in '_2.so'
327 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
328 // the correct _impl() is implemented by '_a.so';
329 //
330 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
331 //
332 // Here is the picture:
333 //
334 // libtest_check_order_reloc_siblings.so
335 // |
336 // +-> ..._1.so <- empty
337 // | |
338 // | +-> ..._a.so <- exports correct answer_impl()
339 // | |
340 // | +-> ..._b.so <- every other letter exporting incorrect one.
341 // |
342 // +-> ..._2.so <- empty
343 // | |
344 // | +-> ..._c.so
345 // | |
346 // | +-> ..._d.so
347 // |
348 // +-> ..._3.so <- empty
349 // |
350 // +-> ..._e.so
351 // |
352 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
353 // implements incorrect get_answer_impl()
354
355 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
356 ASSERT_TRUE(handle == nullptr);
357#ifdef __BIONIC__
358 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
359 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
360#endif
361
362 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
363 ASSERT_TRUE(handle != nullptr) << dlerror();
364
365 typedef int (*fn_t) (void);
366 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
367 ASSERT_TRUE(fn != nullptr) << dlerror();
368 ASSERT_EQ(42, fn());
369
370 ASSERT_EQ(0, dlclose(handle));
371}
372
373TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
374 // This test uses the same library as dlopen_check_order_reloc_siblings.
375 // Unlike dlopen_check_order_reloc_siblings it preloads
376 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
377 // dlopen(libtest_check_order_reloc_siblings.so)
378
379 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
380 ASSERT_TRUE(handle == nullptr);
381 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
382 ASSERT_TRUE(handle == nullptr);
383
384 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
385 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
386
387 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
388 ASSERT_TRUE(handle != nullptr) << dlerror();
389
390 ASSERT_EQ(0, dlclose(handle_for_1));
391
392 typedef int (*fn_t) (void);
393 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
394 ASSERT_TRUE(fn != nullptr) << dlerror();
395 ASSERT_EQ(42, fn());
396
397 ASSERT_EQ(0, dlclose(handle));
398}
399
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800400TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
401 // This is how this one works:
402 // we lookup and call grandchild_get_answer which is defined in '_2.so'
403 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
404 // the correct _impl() is implemented by '_c_1.so';
405 //
406 // Here is the picture of subtree:
407 //
408 // libtest_check_order_reloc_siblings.so
409 // |
410 // +-> ..._2.so <- grandchild_get_answer()
411 // |
412 // +-> ..._c.so <- empty
413 // | |
414 // | +-> _c_1.so <- exports correct answer_impl()
415 // | |
416 // | +-> _c_2.so <- exports incorrect answer_impl()
417 // |
418 // +-> ..._d.so <- empty
419
420 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
421 ASSERT_TRUE(handle == nullptr);
422#ifdef __BIONIC__
423 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
424 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
425#endif
426
427 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
428 ASSERT_TRUE(handle != nullptr) << dlerror();
429
430 typedef int (*fn_t) (void);
431 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
432 ASSERT_TRUE(fn != nullptr) << dlerror();
433 ASSERT_EQ(42, fn());
434
435 ASSERT_EQ(0, dlclose(handle));
436}
437
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700438TEST(dlfcn, dlopen_check_order_reloc_nephew) {
439 // This is how this one works:
440 // we lookup and call nephew_get_answer which is defined in '_2.so'
441 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
442 // the correct _impl() is implemented by '_a.so';
443 //
444 // Here is the picture:
445 //
446 // libtest_check_order_reloc_siblings.so
447 // |
448 // +-> ..._1.so <- empty
449 // | |
450 // | +-> ..._a.so <- exports correct answer_impl()
451 // | |
452 // | +-> ..._b.so <- every other letter exporting incorrect one.
453 // |
454 // +-> ..._2.so <- empty
455 // | |
456 // | +-> ..._c.so
457 // | |
458 // | +-> ..._d.so
459 // |
460 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
461 // |
462 // +-> ..._e.so
463 // |
464 // +-> ..._f.so
465
466 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
467 ASSERT_TRUE(handle == nullptr);
468#ifdef __BIONIC__
469 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
470 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
471#endif
472
473 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
474 ASSERT_TRUE(handle != nullptr) << dlerror();
475
476 typedef int (*fn_t) (void);
477 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
478 ASSERT_TRUE(fn != nullptr) << dlerror();
479 ASSERT_EQ(42, fn());
480
481 ASSERT_EQ(0, dlclose(handle));
482}
483
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800484TEST(dlfcn, check_unload_after_reloc) {
485 // This is how this one works:
486 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
487 // |
488 // +-> libtest_two_parents_child
489 //
490 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
491 // |
492 // +-> libtest_two_parents_child
493 //
494 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
495 // as a second step it dlopens parent2 and dlcloses parent1...
496
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700497 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
498 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800499
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700500 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
501 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800502
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700503 typedef int (*fn_t) (void);
504 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
505 ASSERT_TRUE(fn != nullptr) << dlerror();
506 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800507
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700508 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800509
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700510 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
511 ASSERT_TRUE(handle != nullptr);
512 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800513
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700514 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
515 ASSERT_TRUE(fn != nullptr) << dlerror();
516 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800517
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700518 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800519
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700520 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
521 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800522}
523
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700524extern "C" int check_order_reloc_root_get_answer_impl() {
525 return 42;
526}
527
528TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
529 // This is how this one works:
530 // we lookup and call get_answer3 which is defined in 'root.so'
531 // and in turn calls external root_get_answer_impl() defined in _2.so and
532 // above the correct _impl() is one in the executable.
533 //
534 // libtest_check_order_reloc_root.so
535 // |
536 // +-> ..._1.so <- empty
537 // |
538 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
539 //
540
541 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
542 ASSERT_TRUE(handle == nullptr);
543#ifdef __BIONIC__
544 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
545 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
546#endif
547
548 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
549 ASSERT_TRUE(handle != nullptr) << dlerror();
550
551 typedef int (*fn_t) (void);
552 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
553 ASSERT_TRUE(fn != nullptr) << dlerror();
554 ASSERT_EQ(42, fn());
555
556 ASSERT_EQ(0, dlclose(handle));
557}
558
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700559TEST(dlfcn, dlopen_check_rtld_local) {
560 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
561 ASSERT_TRUE(sym == nullptr);
562
563 // implicit RTLD_LOCAL
564 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
565 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
566 ASSERT_TRUE(sym == nullptr);
567 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
568 sym = dlsym(handle, "dlopen_testlib_simple_func");
569 ASSERT_TRUE(sym != nullptr);
570 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
571 dlclose(handle);
572
573 // explicit RTLD_LOCAL
574 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
575 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
576 ASSERT_TRUE(sym == nullptr);
577 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
578 sym = dlsym(handle, "dlopen_testlib_simple_func");
579 ASSERT_TRUE(sym != nullptr);
580 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
581 dlclose(handle);
582}
583
584TEST(dlfcn, dlopen_check_rtld_global) {
585 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
586 ASSERT_TRUE(sym == nullptr);
587
588 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700589 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700590 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
591 ASSERT_TRUE(sym != nullptr) << dlerror();
592 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
593 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700594
595 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
596 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
597 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700598
599 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
600 // shared libraries after symbol was not found in the main executable
601 // and dependent libraries.
602 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
603 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
604 ASSERT_TRUE(sym != nullptr) << dlerror();
605
606 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700607}
608
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700609// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
610// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
611// libtest_with_dependency_loop_a.so
612TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700613 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
614 ASSERT_TRUE(handle != nullptr) << dlerror();
615 void* f = dlsym(handle, "dlopen_test_loopy_function");
616 ASSERT_TRUE(f != nullptr) << dlerror();
617 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
618 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700619
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700620 // dlopen second time to make sure that the library was unloaded correctly
621 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
622 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800623#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700624 ASSERT_STREQ("dlopen failed: library \"libtest_with_dependency_loop.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -0800625#else
626 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
627 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700628#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800629
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700630 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
631 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700632}
633
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700634TEST(dlfcn, dlopen_nodelete) {
635 static bool is_unloaded = false;
636
637 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
638 ASSERT_TRUE(handle != nullptr) << dlerror();
639 void (*set_unload_flag_ptr)(bool*);
640 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
641 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
642 set_unload_flag_ptr(&is_unloaded);
643
644 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
645 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
646 ASSERT_EQ(1729U, *taxicab_number);
647 *taxicab_number = 2;
648
649 dlclose(handle);
650 ASSERT_TRUE(!is_unloaded);
651
652 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
653 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
654 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
655
656
657 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
658 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
659 ASSERT_EQ(taxicab_number2, taxicab_number);
660
661 ASSERT_EQ(2U, *taxicab_number2);
662
663 dlclose(handle);
664 ASSERT_TRUE(!is_unloaded);
665}
666
667TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
668 static bool is_unloaded = false;
669
670 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
671 ASSERT_TRUE(handle != nullptr) << dlerror();
672 void (*set_unload_flag_ptr)(bool*);
673 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
674 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
675 set_unload_flag_ptr(&is_unloaded);
676
677 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
678 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
679
680 ASSERT_EQ(1729U, *taxicab_number);
681 *taxicab_number = 2;
682
683 // This RTLD_NODELETE should be ignored
684 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
685 ASSERT_TRUE(handle1 != nullptr) << dlerror();
686 ASSERT_EQ(handle, handle1);
687
688 dlclose(handle1);
689 dlclose(handle);
690
691 ASSERT_TRUE(is_unloaded);
692}
693
694TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
695 static bool is_unloaded = false;
696
697 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
698 ASSERT_TRUE(handle != nullptr) << dlerror();
699 void (*set_unload_flag_ptr)(bool*);
700 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
701 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
702 set_unload_flag_ptr(&is_unloaded);
703
704 dlclose(handle);
705 ASSERT_TRUE(!is_unloaded);
706}
707
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700708TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700709 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
710 ASSERT_TRUE(handle != nullptr) << dlerror();
711 int (*get_answer)();
712 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
713 ASSERT_TRUE(get_answer != nullptr) << dlerror();
714 ASSERT_EQ(42, get_answer());
715 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700716}
717
Elliott Hughese66190d2012-12-18 15:57:55 -0800718TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700719 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700720 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700721#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700722 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
723#else
724 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
725#endif
726}
727
Elliott Hughesad88a082012-10-24 18:37:21 -0700728static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700729 dlopen("/child/thread", RTLD_NOW);
730 return reinterpret_cast<void*>(strdup(dlerror()));
731}
732
Elliott Hughese66190d2012-12-18 15:57:55 -0800733TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700734 dlopen("/main/thread", RTLD_NOW);
735 const char* main_thread_error = dlerror();
736 ASSERT_SUBSTR("/main/thread", main_thread_error);
737
738 pthread_t t;
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700739 ASSERT_EQ(0, pthread_create(&t, nullptr, ConcurrentDlErrorFn, nullptr));
Elliott Hughes5419b942012-10-16 15:54:46 -0700740 void* result;
741 ASSERT_EQ(0, pthread_join(t, &result));
742 char* child_thread_error = static_cast<char*>(result);
743 ASSERT_SUBSTR("/child/thread", child_thread_error);
744 free(child_thread_error);
745
746 ASSERT_SUBSTR("/main/thread", main_thread_error);
747}
748
Elliott Hughese66190d2012-12-18 15:57:55 -0800749TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700750 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700751 void* self = dlopen(nullptr, RTLD_NOW);
752 ASSERT_TRUE(self != nullptr);
753 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700754
755 void* sym;
756
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700757#if defined(__BIONIC__) && !defined(__LP64__)
758 // RTLD_DEFAULT in lp32 bionic is not (void*)0
759 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700760 sym = dlsym(nullptr, "test");
761 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800762 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700763#endif
764
Elliott Hughes3b297c42012-10-11 16:08:51 -0700765 // Symbol that doesn't exist.
766 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700767 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700768 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700769
770 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700771}
772
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700773TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700774 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700775 void* self = dlopen(nullptr, RTLD_NOW);
776 ASSERT_TRUE(self != nullptr);
777 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700778
779 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700780 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700781
782 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
783 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
784
785 Dl_info info;
786 int rc = dladdr(addr, &info);
787 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
788
789 // Get the name of this executable.
790 char executable_path[PATH_MAX];
791 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
792 ASSERT_NE(rc, -1);
793 executable_path[rc] = '\0';
Elliott Hughes8e15b082012-09-26 11:44:01 -0700794
795 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700796 char dli_realpath[PATH_MAX];
797 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
798 ASSERT_STREQ(executable_path, dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700799
800 // The symbol name should be the symbol we looked up.
801 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
802
803 // The address should be the exact address of the symbol.
804 ASSERT_EQ(info.dli_saddr, sym);
805
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700806 std::vector<map_record> maps;
807 ASSERT_TRUE(Maps::parse_maps(&maps));
808
809 void* base_address = nullptr;
810 for (const map_record& rec : maps) {
811 if (executable_path == rec.pathname) {
812 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700813 break;
814 }
815 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700816
817 // The base address should be the address we were loaded at.
818 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700819
820 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700821}
822
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700823#if defined(__LP64__)
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200824#define PATH_TO_SYSTEM_LIB "/system/lib64/"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700825#else
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200826#define PATH_TO_SYSTEM_LIB "/system/lib/"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700827#endif
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200828#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700829
830TEST(dlfcn, dladdr_libc) {
831#if defined(__BIONIC__)
832 Dl_info info;
833 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
834 ASSERT_TRUE(dladdr(addr, &info) != 0);
835
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700836 // /system/lib is symlink when this test is executed on host.
837 char libc_realpath[PATH_MAX];
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200838 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700839
840 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700841 // TODO: add check for dfi_fbase
842 ASSERT_STREQ("puts", info.dli_sname);
843 ASSERT_EQ(addr, info.dli_saddr);
844#else
845 GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig "
846 "for libc.so, which is symlink itself (not a realpath).\n";
847#endif
848}
849
Elliott Hughese66190d2012-12-18 15:57:55 -0800850TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700851 Dl_info info;
852
Elliott Hughes3b297c42012-10-11 16:08:51 -0700853 dlerror(); // Clear any pending errors.
854
Elliott Hughes8e15b082012-09-26 11:44:01 -0700855 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700856 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
857 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700858
859 // No symbol corresponding to a stack address.
860 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700861 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700862}
Elliott Hughes124fae92012-10-31 14:20:03 -0700863
Elliott Hughesa43e9062013-01-07 14:18:22 -0800864// GNU-style ELF hash tables are incompatible with the MIPS ABI.
865// 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 -0800866TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800867#if !defined(__mips__)
Elliott Hughes124fae92012-10-31 14:20:03 -0700868 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800869 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
870 ASSERT_TRUE(handle != nullptr) << dlerror();
871 auto guard = make_scope_guard([&]() {
872 dlclose(handle);
873 });
874 void* sym = dlsym(handle, "getRandomNumber");
875 ASSERT_TRUE(sym != nullptr) << dlerror();
876 int (*fn)(void);
877 fn = reinterpret_cast<int (*)(void)>(sym);
878 EXPECT_EQ(4, fn());
879
880 Dl_info dlinfo;
881 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
882
883 ASSERT_TRUE(fn == dlinfo.dli_saddr);
884 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800885 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800886#else
887 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
888#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700889}
Elliott Hughese66190d2012-12-18 15:57:55 -0800890
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800891TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
892 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
893 ASSERT_TRUE(handle != nullptr) << dlerror();
894 auto guard = make_scope_guard([&]() {
895 dlclose(handle);
896 });
897 void* sym = dlsym(handle, "getRandomNumber");
898 ASSERT_TRUE(sym != nullptr) << dlerror();
899 int (*fn)(void);
900 fn = reinterpret_cast<int (*)(void)>(sym);
901 EXPECT_EQ(4, fn());
902
903 Dl_info dlinfo;
904 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
905
906 ASSERT_TRUE(fn == dlinfo.dli_saddr);
907 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
908 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
909}
910
Elliott Hughese66190d2012-12-18 15:57:55 -0800911TEST(dlfcn, dlopen_bad_flags) {
912 dlerror(); // Clear any pending errors.
913 void* handle;
914
Elliott Hughes063525c2014-05-13 11:19:57 -0700915#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800916 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700917 handle = dlopen(nullptr, 0);
918 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -0800919 ASSERT_SUBSTR("invalid", dlerror());
920#endif
921
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700922 handle = dlopen(nullptr, 0xffffffff);
923 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -0800924 ASSERT_SUBSTR("invalid", dlerror());
925
926 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700927 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
928 ASSERT_TRUE(handle != nullptr);
929 ASSERT_SUBSTR(nullptr, dlerror());
Elliott Hughese66190d2012-12-18 15:57:55 -0800930}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400931
932TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800933 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700934 ASSERT_TRUE(addr == nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400935}
936
937TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800938 void* addr = dlsym(RTLD_DEFAULT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700939 ASSERT_TRUE(addr != nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -0800940}
941
942TEST(dlfcn, rtld_next_unknown_symbol) {
943 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700944 ASSERT_TRUE(addr == nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -0800945}
946
947TEST(dlfcn, rtld_next_known_symbol) {
948 void* addr = dlsym(RTLD_NEXT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700949 ASSERT_TRUE(addr != nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400950}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700951
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700952TEST(dlfcn, dlsym_weak_func) {
953 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800954 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700955 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700956
957 int (*weak_func)();
958 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700959 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700960 EXPECT_EQ(42, weak_func());
961 dlclose(handle);
962}
963
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800964TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700965 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
966 ASSERT_TRUE(handle != nullptr) << dlerror();
967 int (*weak_func)();
968 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
969 ASSERT_TRUE(weak_func != nullptr) << dlerror();
970 EXPECT_EQ(6551, weak_func());
971 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800972}
973
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700974TEST(dlfcn, dlopen_symlink) {
975 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
976 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700977 ASSERT_TRUE(handle1 != nullptr);
978 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700979 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -0700980 dlclose(handle1);
981 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700982}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800983
984// libtest_dlopen_from_ctor_main.so depends on
985// libtest_dlopen_from_ctor.so which has a constructor
986// that calls dlopen(libc...). This is to test the situation
987// described in b/7941716.
988TEST(dlfcn, dlopen_dlopen_from_ctor) {
989#if defined(__BIONIC__)
990 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
991 ASSERT_TRUE(handle != nullptr) << dlerror();
992 dlclose(handle);
993#else
994 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
995#endif
996}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700997
998TEST(dlfcn, symbol_versioning_use_v1) {
999 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1000 ASSERT_TRUE(handle != nullptr) << dlerror();
1001 typedef int (*fn_t)();
1002 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1003 ASSERT_TRUE(fn != nullptr) << dlerror();
1004 ASSERT_EQ(1, fn());
1005 dlclose(handle);
1006}
1007
1008TEST(dlfcn, symbol_versioning_use_v2) {
1009 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1010 ASSERT_TRUE(handle != nullptr) << dlerror();
1011 typedef int (*fn_t)();
1012 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1013 ASSERT_TRUE(fn != nullptr) << dlerror();
1014 ASSERT_EQ(2, fn());
1015 dlclose(handle);
1016}
1017
1018TEST(dlfcn, symbol_versioning_use_other_v2) {
1019 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1020 ASSERT_TRUE(handle != nullptr) << dlerror();
1021 typedef int (*fn_t)();
1022 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1023 ASSERT_TRUE(fn != nullptr) << dlerror();
1024 ASSERT_EQ(20, fn());
1025 dlclose(handle);
1026}
1027
1028TEST(dlfcn, symbol_versioning_use_other_v3) {
1029 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1030 ASSERT_TRUE(handle != nullptr) << dlerror();
1031 typedef int (*fn_t)();
1032 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1033 ASSERT_TRUE(fn != nullptr) << dlerror();
1034 ASSERT_EQ(3, fn());
1035 dlclose(handle);
1036}
1037
1038TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1039 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1040 ASSERT_TRUE(handle != nullptr) << dlerror();
1041 typedef int (*fn_t)();
1042 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1043 ASSERT_TRUE(fn != nullptr) << dlerror();
1044 ASSERT_EQ(3, fn()); // the default version is 3
1045 dlclose(handle);
1046}
1047
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001048TEST(dlfcn, dlvsym_smoke) {
1049 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1050 ASSERT_TRUE(handle != nullptr) << dlerror();
1051 typedef int (*fn_t)();
1052
1053 {
1054 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1055 ASSERT_TRUE(fn == nullptr);
1056 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1057 }
1058
1059 {
1060 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1061 ASSERT_TRUE(fn != nullptr) << dlerror();
1062 ASSERT_EQ(2, fn());
1063 }
1064
1065 dlclose(handle);
1066}
1067
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001068// This preempts the implementation from libtest_versioned_lib.so
1069extern "C" int version_zero_function() {
1070 return 0;
1071}
1072
1073// This preempts the implementation from libtest_versioned_uselibv*.so
1074extern "C" int version_zero_function2() {
1075 return 0;
1076}
Evgenii Stepanov68650822015-06-10 13:38:39 -07001077
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001078TEST(dlfcn, dt_runpath_smoke) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001079 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1080 ASSERT_TRUE(handle != nullptr) << dlerror();
1081
1082 typedef void *(* dlopen_b_fn)();
1083 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1084 ASSERT_TRUE(fn != nullptr) << dlerror();
1085
1086 void *p = fn();
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001087 ASSERT_TRUE(p != nullptr);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001088
1089 dlclose(handle);
1090}
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001091
1092TEST(dlfcn, dt_runpath_absolute_path) {
1093 void* handle = dlopen(PATH_TO_SYSTEM_LIB "libtest_dt_runpath_d.so", RTLD_NOW);
1094 ASSERT_TRUE(handle != nullptr) << dlerror();
1095
1096 typedef void *(* dlopen_b_fn)();
1097 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1098 ASSERT_TRUE(fn != nullptr) << dlerror();
1099
1100 void *p = fn();
1101 ASSERT_TRUE(p != nullptr);
1102
1103 dlclose(handle);
1104}