1 /*
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>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <string.h>
24
25 #include <string>
26 #include <thread>
27
28 #include <android-base/scopeguard.h>
29
30 #include "gtest_globals.h"
31 #include "dlfcn_symlink_support.h"
32 #include "utils.h"
33
34 #if defined(__BIONIC__) && (defined(__arm__) || defined(__i386__))
35 #pragma clang diagnostic push
36 #pragma clang diagnostic ignored "-Wunused-parameter"
37
38 #include <llvm/ADT/StringRef.h>
39 #include <llvm/Object/Binary.h>
40 #include <llvm/Object/ELFObjectFile.h>
41 #include <llvm/Object/ObjectFile.h>
42
43 #pragma clang diagnostic pop
44 #endif // defined(__ANDROID__) && (defined(__arm__) || defined(__i386__))
45
46 #define ASSERT_SUBSTR(needle, haystack) \
47 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
48
49
50 static bool g_called = false;
DlSymTestFunction()51 extern "C" void DlSymTestFunction() {
52 g_called = true;
53 }
54
55 static int g_ctor_function_called = 0;
56 static int g_ctor_argc = 0;
57 static char** g_ctor_argv = reinterpret_cast<char**>(0xDEADBEEF);
58 static char** g_ctor_envp = g_ctor_envp;
59
60 extern "C" void ctor_function(int argc, char** argv, char** envp) __attribute__ ((constructor));
61
ctor_function(int argc,char ** argv,char ** envp)62 extern "C" void ctor_function(int argc, char** argv, char** envp) {
63 g_ctor_function_called = 17;
64 g_ctor_argc = argc;
65 g_ctor_argv = argv;
66 g_ctor_envp = envp;
67 }
68
TEST(dlfcn,ctor_function_call)69 TEST(dlfcn, ctor_function_call) {
70 ASSERT_EQ(17, g_ctor_function_called);
71 ASSERT_TRUE(g_ctor_argc = get_argc());
72 ASSERT_TRUE(g_ctor_argv = get_argv());
73 ASSERT_TRUE(g_ctor_envp = get_envp());
74 }
75
TEST(dlfcn,dlsym_in_executable)76 TEST(dlfcn, dlsym_in_executable) {
77 dlerror(); // Clear any pending errors.
78 void* self = dlopen(nullptr, RTLD_NOW);
79 ASSERT_TRUE(self != nullptr);
80 ASSERT_TRUE(dlerror() == nullptr);
81
82 void* sym = dlsym(self, "DlSymTestFunction");
83 ASSERT_TRUE(sym != nullptr);
84
85 void (*function)() = reinterpret_cast<void(*)()>(sym);
86
87 g_called = false;
88 function();
89 ASSERT_TRUE(g_called);
90
91 ASSERT_EQ(0, dlclose(self));
92 }
93
TEST(dlfcn,dlsym_from_sofile)94 TEST(dlfcn, dlsym_from_sofile) {
95 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL);
96 ASSERT_TRUE(handle != nullptr) << dlerror();
97
98 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
99 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
100 ASSERT_TRUE(symbol == nullptr);
101 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
102
103 typedef int* (*fn_t)();
104 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
105 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
106 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
107
108 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
109 ASSERT_TRUE(ptr != nullptr) << dlerror();
110 ASSERT_EQ(42, *ptr);
111
112 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
113 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
114 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
115
116 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
117 ASSERT_TRUE(ptr != nullptr) << dlerror();
118 ASSERT_EQ(44, *ptr);
119
120 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
121 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
122 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
123
124 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
125 ASSERT_TRUE(ptr != nullptr) << dlerror();
126 ASSERT_EQ(43, *ptr);
127
128 dlclose(handle);
129 }
130
TEST(dlfcn,dlsym_from_sofile_with_preload)131 TEST(dlfcn, dlsym_from_sofile_with_preload) {
132 void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL);
133 ASSERT_TRUE(preload != nullptr) << dlerror();
134
135 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
136 ASSERT_TRUE(handle != nullptr) << dlerror();
137
138 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
139 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
140 ASSERT_TRUE(symbol == nullptr);
141 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
142
143 typedef int* (*fn_t)();
144 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
145 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
146 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
147
148 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
149 ASSERT_TRUE(ptr != nullptr) << dlerror();
150 ASSERT_EQ(42, *ptr);
151
152 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
153 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
154 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
155
156 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
157 ASSERT_TRUE(ptr != nullptr) << dlerror();
158 ASSERT_EQ(44, *ptr);
159
160 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
161 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
162 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
163
164 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
165 ASSERT_TRUE(ptr != nullptr) << dlerror();
166 ASSERT_EQ(43, *ptr);
167
168 dlclose(handle);
169 dlclose(preload);
170 }
171
TEST(dlfcn,dlsym_handle_global_sym)172 TEST(dlfcn, dlsym_handle_global_sym) {
173 // check that we do not look into global group
174 // when looking up symbol by handle
175 void* handle = dlopen("libtest_empty.so", RTLD_NOW);
176 dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL);
177 void* sym = dlsym(handle, "getRandomNumber");
178 ASSERT_TRUE(sym == nullptr);
179 ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror());
180
181 sym = dlsym(handle, "DlSymTestFunction");
182 ASSERT_TRUE(sym == nullptr);
183 ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror());
184 dlclose(handle);
185 }
186
TEST(dlfcn,dlsym_handle_empty_symbol)187 TEST(dlfcn, dlsym_handle_empty_symbol) {
188 // check that dlsym of an empty symbol fails (see http://b/33530622)
189 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW);
190 ASSERT_TRUE(handle != nullptr) << dlerror();
191 void* sym = dlsym(handle, "");
192 ASSERT_TRUE(sym == nullptr);
193 ASSERT_SUBSTR("undefined symbol: ", dlerror());
194 dlclose(handle);
195 }
196
TEST(dlfcn,dlsym_with_dependencies)197 TEST(dlfcn, dlsym_with_dependencies) {
198 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
199 ASSERT_TRUE(handle != nullptr);
200 dlerror();
201 // This symbol is in DT_NEEDED library.
202 void* sym = dlsym(handle, "getRandomNumber");
203 ASSERT_TRUE(sym != nullptr) << dlerror();
204 int (*fn)(void);
205 fn = reinterpret_cast<int (*)(void)>(sym);
206 EXPECT_EQ(4, fn());
207 dlclose(handle);
208 }
209
TEST(dlfcn,dlopen_noload)210 TEST(dlfcn, dlopen_noload) {
211 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
212 ASSERT_TRUE(handle == nullptr);
213 handle = dlopen("libtest_simple.so", RTLD_NOW);
214 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
215 ASSERT_TRUE(handle != nullptr);
216 ASSERT_TRUE(handle2 != nullptr);
217 ASSERT_TRUE(handle == handle2);
218 ASSERT_EQ(0, dlclose(handle));
219 ASSERT_EQ(0, dlclose(handle2));
220 }
221
TEST(dlfcn,dlopen_by_soname)222 TEST(dlfcn, dlopen_by_soname) {
223 static const char* soname = "libdlext_test_soname.so";
224 static const char* filename = "libdlext_test_different_soname.so";
225 // 1. Make sure there is no library with soname in default search path
226 void* handle = dlopen(soname, RTLD_NOW);
227 ASSERT_TRUE(handle == nullptr);
228
229 // 2. Load a library using filename
230 handle = dlopen(filename, RTLD_NOW);
231 ASSERT_TRUE(handle != nullptr) << dlerror();
232
233 // 3. Find library by soname
234 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
235 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
236 ASSERT_EQ(handle, handle_soname);
237
238 // 4. RTLD_NOLOAD should still work with filename
239 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
240 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
241 ASSERT_EQ(handle, handle_filename);
242
243 dlclose(handle_filename);
244 dlclose(handle_soname);
245 dlclose(handle);
246 }
247
248 // mips doesn't support ifuncs
249 #if !defined(__mips__)
TEST(dlfcn,ifunc_variable)250 TEST(dlfcn, ifunc_variable) {
251 typedef const char* (*fn_ptr)();
252
253 // ifunc's choice depends on whether IFUNC_CHOICE has a value
254 // first check the set case
255 setenv("IFUNC_CHOICE", "set", 1);
256 // preload libtest_ifunc_variable_impl.so
257 void* handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
258 void* handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
259 ASSERT_TRUE(handle != nullptr) << dlerror();
260 const char** foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
261 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
262 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
263 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
264 ASSERT_EQ(strncmp("set", *foo_ptr, 3), 0);
265 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
266 dlclose(handle);
267 dlclose(handle_impl);
268
269 // then check the unset case
270 unsetenv("IFUNC_CHOICE");
271 handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
272 handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
273 ASSERT_TRUE(handle != nullptr) << dlerror();
274 foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
275 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
276 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
277 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
278 ASSERT_EQ(strncmp("unset", *foo_ptr, 5), 0);
279 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
280 dlclose(handle);
281 dlclose(handle_impl);
282 }
283
TEST(dlfcn,ifunc)284 TEST(dlfcn, ifunc) {
285 typedef const char* (*fn_ptr)();
286
287 // ifunc's choice depends on whether IFUNC_CHOICE has a value
288 // first check the set case
289 setenv("IFUNC_CHOICE", "set", 1);
290 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
291 ASSERT_TRUE(handle != nullptr) << dlerror();
292 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
293 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
294 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
295 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
296 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
297 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
298 dlclose(handle);
299
300 // then check the unset case
301 unsetenv("IFUNC_CHOICE");
302 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
303 ASSERT_TRUE(handle != nullptr) << dlerror();
304 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
305 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
306 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
307 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
308 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
309 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
310 dlclose(handle);
311 }
312
TEST(dlfcn,ifunc_ctor_call)313 TEST(dlfcn, ifunc_ctor_call) {
314 typedef const char* (*fn_ptr)();
315
316 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
317 ASSERT_TRUE(handle != nullptr) << dlerror();
318 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
319 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
320 ASSERT_STREQ("false", is_ctor_called());
321
322 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
323 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
324 ASSERT_STREQ("true", is_ctor_called());
325 dlclose(handle);
326 }
327
TEST(dlfcn,ifunc_ctor_call_rtld_lazy)328 TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
329 typedef const char* (*fn_ptr)();
330
331 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
332 ASSERT_TRUE(handle != nullptr) << dlerror();
333 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
334 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
335 ASSERT_STREQ("false", is_ctor_called());
336
337 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
338 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
339 ASSERT_STREQ("true", is_ctor_called());
340 dlclose(handle);
341 }
342 #endif
343
TEST(dlfcn,dlopen_check_relocation_dt_needed_order)344 TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
345 // This is the structure of the test library and
346 // its dt_needed libraries
347 // libtest_relo_check_dt_needed_order.so
348 // |
349 // +-> libtest_relo_check_dt_needed_order_1.so
350 // |
351 // +-> libtest_relo_check_dt_needed_order_2.so
352 //
353 // The root library references relo_test_get_answer_lib - which is defined
354 // in both dt_needed libraries, the correct relocation should
355 // use the function defined in libtest_relo_check_dt_needed_order_1.so
356 void* handle = nullptr;
357 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
358
359 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
360 ASSERT_TRUE(handle != nullptr) << dlerror();
361
362 typedef int (*fn_t) (void);
363 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
364 ASSERT_TRUE(fn != nullptr) << dlerror();
365 ASSERT_EQ(1, fn());
366 }
367
TEST(dlfcn,dlopen_check_order_dlsym)368 TEST(dlfcn, dlopen_check_order_dlsym) {
369 // Here is how the test library and its dt_needed
370 // libraries are arranged
371 //
372 // libtest_check_order_children.so
373 // |
374 // +-> ..._1_left.so
375 // | |
376 // | +-> ..._a.so
377 // | |
378 // | +-> ...r_b.so
379 // |
380 // +-> ..._2_right.so
381 // | |
382 // | +-> ..._d.so
383 // | |
384 // | +-> ..._b.so
385 // |
386 // +-> ..._3_c.so
387 //
388 // load order should be (1, 2, 3, a, b, d)
389 //
390 // get_answer() is defined in (2, 3, a, b, c)
391 // get_answer2() is defined in (b, d)
392 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
393 ASSERT_TRUE(sym == nullptr);
394 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
395 ASSERT_TRUE(handle != nullptr) << dlerror();
396 typedef int (*fn_t) (void);
397 fn_t fn, fn2;
398 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
399 ASSERT_TRUE(fn != nullptr) << dlerror();
400 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
401 ASSERT_TRUE(fn2 != nullptr) << dlerror();
402
403 ASSERT_EQ(42, fn());
404 ASSERT_EQ(43, fn2());
405 dlclose(handle);
406 }
407
TEST(dlfcn,dlopen_check_order_reloc_siblings)408 TEST(dlfcn, dlopen_check_order_reloc_siblings) {
409 // This is how this one works:
410 // we lookup and call get_answer which is defined in '_2.so'
411 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
412 // the correct _impl() is implemented by '_a.so';
413 //
414 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
415 //
416 // Here is the picture:
417 //
418 // libtest_check_order_reloc_siblings.so
419 // |
420 // +-> ..._1.so <- empty
421 // | |
422 // | +-> ..._a.so <- exports correct answer_impl()
423 // | |
424 // | +-> ..._b.so <- every other letter exporting incorrect one.
425 // |
426 // +-> ..._2.so <- empty
427 // | |
428 // | +-> ..._c.so
429 // | |
430 // | +-> ..._d.so
431 // |
432 // +-> ..._3.so <- empty
433 // |
434 // +-> ..._e.so
435 // |
436 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
437 // implements incorrect get_answer_impl()
438
439 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
440 ASSERT_TRUE(handle == nullptr);
441 #ifdef __BIONIC__
442 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
443 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
444 #endif
445
446 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
447 ASSERT_TRUE(handle != nullptr) << dlerror();
448
449 typedef int (*fn_t) (void);
450 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
451 ASSERT_TRUE(fn != nullptr) << dlerror();
452 ASSERT_EQ(42, fn());
453
454 ASSERT_EQ(0, dlclose(handle));
455 }
456
TEST(dlfcn,dlopen_check_order_reloc_siblings_with_preload)457 TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
458 // This test uses the same library as dlopen_check_order_reloc_siblings.
459 // Unlike dlopen_check_order_reloc_siblings it preloads
460 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
461 // dlopen(libtest_check_order_reloc_siblings.so)
462
463 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
464 ASSERT_TRUE(handle == nullptr);
465 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
466 ASSERT_TRUE(handle == nullptr);
467
468 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
469 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
470
471 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
472 ASSERT_TRUE(handle != nullptr) << dlerror();
473
474 ASSERT_EQ(0, dlclose(handle_for_1));
475
476 typedef int (*fn_t) (void);
477 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
478 ASSERT_TRUE(fn != nullptr) << dlerror();
479 ASSERT_EQ(42, fn());
480
481 ASSERT_EQ(0, dlclose(handle));
482 }
483
TEST(dlfcn,dlopen_check_order_reloc_grandchild)484 TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
485 // This is how this one works:
486 // we lookup and call grandchild_get_answer which is defined in '_2.so'
487 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
488 // the correct _impl() is implemented by '_c_1.so';
489 //
490 // Here is the picture of subtree:
491 //
492 // libtest_check_order_reloc_siblings.so
493 // |
494 // +-> ..._2.so <- grandchild_get_answer()
495 // |
496 // +-> ..._c.so <- empty
497 // | |
498 // | +-> _c_1.so <- exports correct answer_impl()
499 // | |
500 // | +-> _c_2.so <- exports incorrect answer_impl()
501 // |
502 // +-> ..._d.so <- empty
503
504 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
505 ASSERT_TRUE(handle == nullptr);
506 #ifdef __BIONIC__
507 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
508 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
509 #endif
510
511 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
512 ASSERT_TRUE(handle != nullptr) << dlerror();
513
514 typedef int (*fn_t) (void);
515 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
516 ASSERT_TRUE(fn != nullptr) << dlerror();
517 ASSERT_EQ(42, fn());
518
519 ASSERT_EQ(0, dlclose(handle));
520 }
521
TEST(dlfcn,dlopen_check_order_reloc_nephew)522 TEST(dlfcn, dlopen_check_order_reloc_nephew) {
523 // This is how this one works:
524 // we lookup and call nephew_get_answer which is defined in '_2.so'
525 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
526 // the correct _impl() is implemented by '_a.so';
527 //
528 // Here is the picture:
529 //
530 // libtest_check_order_reloc_siblings.so
531 // |
532 // +-> ..._1.so <- empty
533 // | |
534 // | +-> ..._a.so <- exports correct answer_impl()
535 // | |
536 // | +-> ..._b.so <- every other letter exporting incorrect one.
537 // |
538 // +-> ..._2.so <- empty
539 // | |
540 // | +-> ..._c.so
541 // | |
542 // | +-> ..._d.so
543 // |
544 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
545 // |
546 // +-> ..._e.so
547 // |
548 // +-> ..._f.so
549
550 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
551 ASSERT_TRUE(handle == nullptr);
552 #ifdef __BIONIC__
553 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
554 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
555 #endif
556
557 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
558 ASSERT_TRUE(handle != nullptr) << dlerror();
559
560 typedef int (*fn_t) (void);
561 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
562 ASSERT_TRUE(fn != nullptr) << dlerror();
563 ASSERT_EQ(42, fn());
564
565 ASSERT_EQ(0, dlclose(handle));
566 }
567
TEST(dlfcn,check_unload_after_reloc)568 TEST(dlfcn, check_unload_after_reloc) {
569 // This is how this one works:
570 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
571 // |
572 // +-> libtest_two_parents_child
573 //
574 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
575 // |
576 // +-> libtest_two_parents_child
577 //
578 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
579 // as a second step it dlopens parent2 and dlcloses parent1...
580
581 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
582 ASSERT_TRUE(handle != nullptr) << dlerror();
583
584 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
585 ASSERT_TRUE(handle2 != nullptr) << dlerror();
586
587 typedef int (*fn_t) (void);
588 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
589 ASSERT_TRUE(fn != nullptr) << dlerror();
590 ASSERT_EQ(42, fn());
591
592 ASSERT_EQ(0, dlclose(handle));
593
594 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
595 ASSERT_TRUE(handle != nullptr);
596 ASSERT_EQ(0, dlclose(handle));
597
598 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
599 ASSERT_TRUE(fn != nullptr) << dlerror();
600 ASSERT_EQ(42, fn());
601
602 ASSERT_EQ(0, dlclose(handle2));
603
604 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
605 ASSERT_TRUE(handle == nullptr);
606 }
607
check_order_reloc_root_get_answer_impl()608 extern "C" int check_order_reloc_root_get_answer_impl() {
609 return 42;
610 }
611
TEST(dlfcn,dlopen_check_order_reloc_main_executable)612 TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
613 // This is how this one works:
614 // we lookup and call get_answer3 which is defined in 'root.so'
615 // and in turn calls external root_get_answer_impl() defined in _2.so and
616 // above the correct _impl() is one in the executable.
617 //
618 // libtest_check_order_reloc_root.so
619 // |
620 // +-> ..._1.so <- empty
621 // |
622 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
623 //
624
625 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
626 ASSERT_TRUE(handle == nullptr);
627 #ifdef __BIONIC__
628 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
629 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
630 #endif
631
632 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
633 ASSERT_TRUE(handle != nullptr) << dlerror();
634
635 typedef int (*fn_t) (void);
636 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
637 ASSERT_TRUE(fn != nullptr) << dlerror();
638 ASSERT_EQ(42, fn());
639
640 ASSERT_EQ(0, dlclose(handle));
641 }
642
TEST(dlfcn,dlopen_check_rtld_local)643 TEST(dlfcn, dlopen_check_rtld_local) {
644 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
645 ASSERT_TRUE(sym == nullptr);
646
647 // implicit RTLD_LOCAL
648 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
649 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
650 ASSERT_TRUE(sym == nullptr);
651 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
652 sym = dlsym(handle, "dlopen_testlib_simple_func");
653 ASSERT_TRUE(sym != nullptr);
654 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
655 dlclose(handle);
656
657 // explicit RTLD_LOCAL
658 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
659 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
660 ASSERT_TRUE(sym == nullptr);
661 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
662 sym = dlsym(handle, "dlopen_testlib_simple_func");
663 ASSERT_TRUE(sym != nullptr);
664 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
665 dlclose(handle);
666 }
667
TEST(dlfcn,dlopen_check_rtld_global)668 TEST(dlfcn, dlopen_check_rtld_global) {
669 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
670 ASSERT_TRUE(sym == nullptr);
671
672 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
673 ASSERT_TRUE(handle != nullptr) << dlerror();
674 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
675 ASSERT_TRUE(sym != nullptr) << dlerror();
676 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
677 dlclose(handle);
678
679 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
680 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
681 ASSERT_EQ(sym, sym_after_dlclose);
682
683 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
684 // shared libraries after symbol was not found in the main executable
685 // and dependent libraries.
686 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
687 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
688 ASSERT_TRUE(sym != nullptr) << dlerror();
689
690 dlclose(handle_for_main_executable);
691 }
692
693 // libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
694 // libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
695 // libtest_with_dependency_loop_a.so
TEST(dlfcn,dlopen_check_loop)696 TEST(dlfcn, dlopen_check_loop) {
697 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
698 ASSERT_TRUE(handle != nullptr) << dlerror();
699 void* f = dlsym(handle, "dlopen_test_loopy_function");
700 ASSERT_TRUE(f != nullptr) << dlerror();
701 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
702 ASSERT_EQ(0, dlclose(handle));
703
704 // dlopen second time to make sure that the library was unloaded correctly
705 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
706 ASSERT_TRUE(handle == nullptr);
707 #ifdef __BIONIC__
708 ASSERT_STREQ("dlopen failed: library \"libtest_with_dependency_loop.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
709 #else
710 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
711 ASSERT_TRUE(dlerror() == nullptr);
712 #endif
713
714 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
715 ASSERT_TRUE(handle == nullptr);
716 }
717
TEST(dlfcn,dlopen_nodelete)718 TEST(dlfcn, dlopen_nodelete) {
719 static bool is_unloaded = false;
720
721 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
722 ASSERT_TRUE(handle != nullptr) << dlerror();
723 void (*set_unload_flag_ptr)(bool*);
724 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
725 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
726 set_unload_flag_ptr(&is_unloaded);
727
728 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
729 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
730 ASSERT_EQ(1729U, *taxicab_number);
731 *taxicab_number = 2;
732
733 dlclose(handle);
734 ASSERT_TRUE(!is_unloaded);
735
736 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
737 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
738 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
739
740
741 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
742 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
743 ASSERT_EQ(taxicab_number2, taxicab_number);
744
745 ASSERT_EQ(2U, *taxicab_number2);
746
747 dlclose(handle);
748 ASSERT_TRUE(!is_unloaded);
749 }
750
TEST(dlfcn,dlopen_nodelete_on_second_dlopen)751 TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
752 static bool is_unloaded = false;
753
754 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
755 ASSERT_TRUE(handle != nullptr) << dlerror();
756 void (*set_unload_flag_ptr)(bool*);
757 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
758 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
759 set_unload_flag_ptr(&is_unloaded);
760
761 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
762 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
763
764 ASSERT_EQ(1729U, *taxicab_number);
765 *taxicab_number = 2;
766
767 // This RTLD_NODELETE should be ignored
768 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
769 ASSERT_TRUE(handle1 != nullptr) << dlerror();
770 ASSERT_EQ(handle, handle1);
771
772 dlclose(handle1);
773 dlclose(handle);
774
775 ASSERT_TRUE(is_unloaded);
776 }
777
TEST(dlfcn,dlopen_nodelete_dt_flags_1)778 TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
779 static bool is_unloaded = false;
780
781 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
782 ASSERT_TRUE(handle != nullptr) << dlerror();
783 void (*set_unload_flag_ptr)(bool*);
784 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
785 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
786 set_unload_flag_ptr(&is_unloaded);
787
788 dlclose(handle);
789 ASSERT_TRUE(!is_unloaded);
790 }
791
TEST(dlfcn,dlsym_df_1_global)792 TEST(dlfcn, dlsym_df_1_global) {
793 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
794 ASSERT_TRUE(handle != nullptr) << dlerror();
795 int (*get_answer)();
796 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
797 ASSERT_TRUE(get_answer != nullptr) << dlerror();
798 ASSERT_EQ(42, get_answer());
799 ASSERT_EQ(0, dlclose(handle));
800 }
801
TEST(dlfcn,dlopen_failure)802 TEST(dlfcn, dlopen_failure) {
803 void* self = dlopen("/does/not/exist", RTLD_NOW);
804 ASSERT_TRUE(self == nullptr);
805 #if defined(__BIONIC__)
806 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
807 #else
808 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
809 #endif
810 }
811
ConcurrentDlErrorFn(std::string & error)812 static void ConcurrentDlErrorFn(std::string& error) {
813 ASSERT_TRUE(dlerror() == nullptr);
814
815 void* handle = dlopen("/child/thread", RTLD_NOW);
816 ASSERT_TRUE(handle == nullptr);
817
818 const char* err = dlerror();
819 ASSERT_TRUE(err != nullptr);
820
821 error = err;
822 }
823
TEST(dlfcn,dlerror_concurrent_buffer)824 TEST(dlfcn, dlerror_concurrent_buffer) {
825 void* handle = dlopen("/main/thread", RTLD_NOW);
826 ASSERT_TRUE(handle == nullptr);
827 const char* main_thread_error = dlerror();
828 ASSERT_TRUE(main_thread_error != nullptr);
829 ASSERT_SUBSTR("/main/thread", main_thread_error);
830
831 std::string child_thread_error;
832 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
833 t.join();
834 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
835
836 // Check that main thread local buffer was not modified.
837 ASSERT_SUBSTR("/main/thread", main_thread_error);
838 }
839
TEST(dlfcn,dlerror_concurrent)840 TEST(dlfcn, dlerror_concurrent) {
841 void* handle = dlopen("/main/thread", RTLD_NOW);
842 ASSERT_TRUE(handle == nullptr);
843
844 std::string child_thread_error;
845 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
846 t.join();
847 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
848
849 const char* main_thread_error = dlerror();
850 ASSERT_TRUE(main_thread_error != nullptr);
851 ASSERT_SUBSTR("/main/thread", main_thread_error);
852 }
853
TEST(dlfcn,dlsym_failures)854 TEST(dlfcn, dlsym_failures) {
855 dlerror(); // Clear any pending errors.
856 void* self = dlopen(nullptr, RTLD_NOW);
857 ASSERT_TRUE(self != nullptr);
858 ASSERT_TRUE(dlerror() == nullptr);
859
860 void* sym;
861
862 #if defined(__BIONIC__) && !defined(__LP64__)
863 // RTLD_DEFAULT in lp32 bionic is not (void*)0
864 // so it can be distinguished from the NULL handle.
865 sym = dlsym(nullptr, "test");
866 ASSERT_TRUE(sym == nullptr);
867 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
868 #endif
869
870 // Symbol that doesn't exist.
871 sym = dlsym(self, "ThisSymbolDoesNotExist");
872 ASSERT_TRUE(sym == nullptr);
873 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
874
875 ASSERT_EQ(0, dlclose(self));
876 }
877
TEST(dlfcn,dladdr_executable)878 TEST(dlfcn, dladdr_executable) {
879 dlerror(); // Clear any pending errors.
880 void* self = dlopen(nullptr, RTLD_NOW);
881 ASSERT_TRUE(self != nullptr);
882 ASSERT_TRUE(dlerror() == nullptr);
883
884 void* sym = dlsym(self, "DlSymTestFunction");
885 ASSERT_TRUE(sym != nullptr);
886
887 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
888 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
889
890 Dl_info info;
891 int rc = dladdr(addr, &info);
892 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
893
894 // Get the name of this executable.
895 const std::string& executable_path = get_executable_path();
896
897 // The filename should be that of this executable.
898 char dli_realpath[PATH_MAX];
899 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
900 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
901
902 // The symbol name should be the symbol we looked up.
903 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
904
905 // The address should be the exact address of the symbol.
906 ASSERT_EQ(info.dli_saddr, sym);
907
908 std::vector<map_record> maps;
909 ASSERT_TRUE(Maps::parse_maps(&maps));
910
911 void* base_address = nullptr;
912 for (const map_record& rec : maps) {
913 if (executable_path == rec.pathname) {
914 base_address = reinterpret_cast<void*>(rec.addr_start);
915 break;
916 }
917 }
918
919 // The base address should be the address we were loaded at.
920 ASSERT_EQ(info.dli_fbase, base_address);
921
922 ASSERT_EQ(0, dlclose(self));
923 }
924
TEST(dlfcn,dlopen_executable_by_absolute_path)925 TEST(dlfcn, dlopen_executable_by_absolute_path) {
926 void* handle1 = dlopen(nullptr, RTLD_NOW);
927 ASSERT_TRUE(handle1 != nullptr) << dlerror();
928
929 void* handle2 = dlopen(get_executable_path().c_str(), RTLD_NOW);
930 ASSERT_TRUE(handle2 != nullptr) << dlerror();
931
932 #if defined(__BIONIC__)
933 ASSERT_EQ(handle1, handle2);
934 #else
935 GTEST_LOG_(INFO) << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
936 "it loads a separate copy of the main executable "
937 "on dlopen by absolute path.";
938 #endif
939 }
940
941 #if defined (__aarch64__)
942 #define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm64/"
943 #elif defined (__arm__)
944 #define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm/"
945 #elif defined (__i386__)
946 #define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86/"
947 #elif defined (__x86_64__)
948 #define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86_64/"
949 #elif defined (__mips__)
950 #if defined(__LP64__)
951 #define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips64/"
952 #else
953 #define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips/"
954 #endif
955 #else
956 #error "Unknown architecture"
957 #endif
958 #define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
959 #define ALTERNATE_PATH_TO_LIBC ALTERNATE_PATH_TO_SYSTEM_LIB "libc.so"
960
TEST(dlfcn,dladdr_libc)961 TEST(dlfcn, dladdr_libc) {
962 #if defined(__BIONIC__)
963 Dl_info info;
964 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
965 ASSERT_TRUE(dladdr(addr, &info) != 0);
966
967 char libc_realpath[PATH_MAX];
968
969 // Check if libc is in canonical path or in alternate path.
970 if (strncmp(ALTERNATE_PATH_TO_SYSTEM_LIB,
971 info.dli_fname,
972 sizeof(ALTERNATE_PATH_TO_SYSTEM_LIB) - 1) == 0) {
973 // Platform with emulated architecture. Symlink on ARC++.
974 ASSERT_TRUE(realpath(ALTERNATE_PATH_TO_LIBC, libc_realpath) == libc_realpath);
975 } else {
976 // /system/lib is symlink when this test is executed on host.
977 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
978 }
979
980 ASSERT_STREQ(libc_realpath, info.dli_fname);
981 // TODO: add check for dfi_fbase
982 ASSERT_STREQ("puts", info.dli_sname);
983 ASSERT_EQ(addr, info.dli_saddr);
984 #else
985 GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig "
986 "for libc.so, which is symlink itself (not a realpath).\n";
987 #endif
988 }
989
TEST(dlfcn,dladdr_invalid)990 TEST(dlfcn, dladdr_invalid) {
991 Dl_info info;
992
993 dlerror(); // Clear any pending errors.
994
995 // No symbol corresponding to NULL.
996 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
997 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
998
999 // No symbol corresponding to a stack address.
1000 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
1001 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
1002 }
1003
1004 // GNU-style ELF hash tables are incompatible with the MIPS ABI.
1005 // MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code.
TEST(dlfcn,dlopen_library_with_only_gnu_hash)1006 TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
1007 #if !defined(__mips__)
1008 dlerror(); // Clear any pending errors.
1009 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
1010 ASSERT_TRUE(handle != nullptr) << dlerror();
1011 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
1012 void* sym = dlsym(handle, "getRandomNumber");
1013 ASSERT_TRUE(sym != nullptr) << dlerror();
1014 int (*fn)(void);
1015 fn = reinterpret_cast<int (*)(void)>(sym);
1016 EXPECT_EQ(4, fn());
1017
1018 Dl_info dlinfo;
1019 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1020
1021 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1022 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
1023 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
1024 #else
1025 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
1026 #endif
1027 }
1028
TEST(dlfcn,dlopen_library_with_only_sysv_hash)1029 TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
1030 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
1031 ASSERT_TRUE(handle != nullptr) << dlerror();
1032 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
1033 void* sym = dlsym(handle, "getRandomNumber");
1034 ASSERT_TRUE(sym != nullptr) << dlerror();
1035 int (*fn)(void);
1036 fn = reinterpret_cast<int (*)(void)>(sym);
1037 EXPECT_EQ(4, fn());
1038
1039 Dl_info dlinfo;
1040 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1041
1042 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1043 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
1044 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
1045 }
1046
TEST(dlfcn,dlopen_bad_flags)1047 TEST(dlfcn, dlopen_bad_flags) {
1048 dlerror(); // Clear any pending errors.
1049 void* handle;
1050
1051 #if defined(__GLIBC__)
1052 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
1053 handle = dlopen(nullptr, 0);
1054 ASSERT_TRUE(handle == nullptr);
1055 ASSERT_SUBSTR("invalid", dlerror());
1056 #endif
1057
1058 handle = dlopen(nullptr, 0xffffffff);
1059 ASSERT_TRUE(handle == nullptr);
1060 ASSERT_SUBSTR("invalid", dlerror());
1061
1062 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
1063 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
1064 ASSERT_TRUE(handle != nullptr);
1065 ASSERT_SUBSTR(nullptr, dlerror());
1066 }
1067
TEST(dlfcn,rtld_default_unknown_symbol)1068 TEST(dlfcn, rtld_default_unknown_symbol) {
1069 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
1070 ASSERT_TRUE(addr == nullptr);
1071 }
1072
TEST(dlfcn,rtld_default_known_symbol)1073 TEST(dlfcn, rtld_default_known_symbol) {
1074 void* addr = dlsym(RTLD_DEFAULT, "fopen");
1075 ASSERT_TRUE(addr != nullptr);
1076 }
1077
TEST(dlfcn,rtld_next_unknown_symbol)1078 TEST(dlfcn, rtld_next_unknown_symbol) {
1079 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
1080 ASSERT_TRUE(addr == nullptr);
1081 }
1082
TEST(dlfcn,rtld_next_known_symbol)1083 TEST(dlfcn, rtld_next_known_symbol) {
1084 void* addr = dlsym(RTLD_NEXT, "fopen");
1085 ASSERT_TRUE(addr != nullptr);
1086 }
1087
1088 // Check that RTLD_NEXT of a libc symbol works in dlopened library
TEST(dlfcn,rtld_next_from_library)1089 TEST(dlfcn, rtld_next_from_library) {
1090 void* library_with_close = dlopen("libtest_check_rtld_next_from_library.so", RTLD_NOW);
1091 ASSERT_TRUE(library_with_close != nullptr) << dlerror();
1092 void* expected_addr = dlsym(RTLD_DEFAULT, "close");
1093 ASSERT_TRUE(expected_addr != nullptr) << dlerror();
1094 typedef void* (*get_libc_close_ptr_fn_t)();
1095 get_libc_close_ptr_fn_t get_libc_close_ptr =
1096 reinterpret_cast<get_libc_close_ptr_fn_t>(dlsym(library_with_close, "get_libc_close_ptr"));
1097 ASSERT_TRUE(get_libc_close_ptr != nullptr) << dlerror();
1098 ASSERT_EQ(expected_addr, get_libc_close_ptr());
1099
1100 dlclose(library_with_close);
1101 }
1102
1103
TEST(dlfcn,dlsym_weak_func)1104 TEST(dlfcn, dlsym_weak_func) {
1105 dlerror();
1106 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
1107 ASSERT_TRUE(handle != nullptr);
1108
1109 int (*weak_func)();
1110 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
1111 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
1112 EXPECT_EQ(42, weak_func());
1113 dlclose(handle);
1114 }
1115
TEST(dlfcn,dlopen_undefined_weak_func)1116 TEST(dlfcn, dlopen_undefined_weak_func) {
1117 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
1118 ASSERT_TRUE(handle != nullptr) << dlerror();
1119 int (*weak_func)();
1120 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
1121 ASSERT_TRUE(weak_func != nullptr) << dlerror();
1122 EXPECT_EQ(6551, weak_func());
1123 dlclose(handle);
1124 }
1125
TEST(dlfcn,dlopen_symlink)1126 TEST(dlfcn, dlopen_symlink) {
1127 DlfcnSymlink symlink("dlopen_symlink");
1128 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
1129 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
1130 void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW);
1131 ASSERT_TRUE(handle1 != nullptr);
1132 ASSERT_TRUE(handle2 != nullptr);
1133 ASSERT_EQ(handle1, handle2);
1134 dlclose(handle1);
1135 dlclose(handle2);
1136 }
1137
1138 // libtest_dlopen_from_ctor_main.so depends on
1139 // libtest_dlopen_from_ctor.so which has a constructor
1140 // that calls dlopen(libc...). This is to test the situation
1141 // described in b/7941716.
TEST(dlfcn,dlopen_dlopen_from_ctor)1142 TEST(dlfcn, dlopen_dlopen_from_ctor) {
1143 #if defined(__BIONIC__)
1144 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1145 ASSERT_TRUE(handle != nullptr) << dlerror();
1146 dlclose(handle);
1147 #else
1148 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
1149 #endif
1150 }
1151
1152 static std::string g_fini_call_order_str;
1153
register_fini_call(const char * s)1154 static void register_fini_call(const char* s) {
1155 g_fini_call_order_str += s;
1156 }
1157
test_init_fini_call_order_for(const char * libname)1158 static void test_init_fini_call_order_for(const char* libname) {
1159 g_fini_call_order_str.clear();
1160 void* handle = dlopen(libname, RTLD_NOW);
1161 ASSERT_TRUE(handle != nullptr) << dlerror();
1162 typedef int (*get_init_order_number_t)();
1163 get_init_order_number_t get_init_order_number =
1164 reinterpret_cast<get_init_order_number_t>(dlsym(handle, "get_init_order_number"));
1165 ASSERT_EQ(321, get_init_order_number());
1166
1167 typedef void (*set_fini_callback_t)(void (*f)(const char*));
1168 set_fini_callback_t set_fini_callback =
1169 reinterpret_cast<set_fini_callback_t>(dlsym(handle, "set_fini_callback"));
1170 set_fini_callback(register_fini_call);
1171 dlclose(handle);
1172 ASSERT_EQ("(root)(child)(grandchild)", g_fini_call_order_str);
1173 }
1174
TEST(dlfcn,init_fini_call_order)1175 TEST(dlfcn, init_fini_call_order) {
1176 test_init_fini_call_order_for("libtest_init_fini_order_root.so");
1177 test_init_fini_call_order_for("libtest_init_fini_order_root2.so");
1178 }
1179
TEST(dlfcn,symbol_versioning_use_v1)1180 TEST(dlfcn, symbol_versioning_use_v1) {
1181 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1182 ASSERT_TRUE(handle != nullptr) << dlerror();
1183 typedef int (*fn_t)();
1184 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1185 ASSERT_TRUE(fn != nullptr) << dlerror();
1186 ASSERT_EQ(1, fn());
1187 dlclose(handle);
1188 }
1189
TEST(dlfcn,symbol_versioning_use_v2)1190 TEST(dlfcn, symbol_versioning_use_v2) {
1191 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1192 ASSERT_TRUE(handle != nullptr) << dlerror();
1193 typedef int (*fn_t)();
1194 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1195 ASSERT_TRUE(fn != nullptr) << dlerror();
1196 ASSERT_EQ(2, fn());
1197 dlclose(handle);
1198 }
1199
TEST(dlfcn,symbol_versioning_use_other_v2)1200 TEST(dlfcn, symbol_versioning_use_other_v2) {
1201 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1202 ASSERT_TRUE(handle != nullptr) << dlerror();
1203 typedef int (*fn_t)();
1204 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1205 ASSERT_TRUE(fn != nullptr) << dlerror();
1206 ASSERT_EQ(20, fn());
1207 dlclose(handle);
1208 }
1209
TEST(dlfcn,symbol_versioning_use_other_v3)1210 TEST(dlfcn, symbol_versioning_use_other_v3) {
1211 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1212 ASSERT_TRUE(handle != nullptr) << dlerror();
1213 typedef int (*fn_t)();
1214 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1215 ASSERT_TRUE(fn != nullptr) << dlerror();
1216 ASSERT_EQ(3, fn());
1217 dlclose(handle);
1218 }
1219
TEST(dlfcn,symbol_versioning_default_via_dlsym)1220 TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1221 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1222 ASSERT_TRUE(handle != nullptr) << dlerror();
1223 typedef int (*fn_t)();
1224 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1225 ASSERT_TRUE(fn != nullptr) << dlerror();
1226 ASSERT_EQ(3, fn()); // the default version is 3
1227 dlclose(handle);
1228 }
1229
TEST(dlfcn,dlvsym_smoke)1230 TEST(dlfcn, dlvsym_smoke) {
1231 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1232 ASSERT_TRUE(handle != nullptr) << dlerror();
1233 typedef int (*fn_t)();
1234
1235 {
1236 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1237 ASSERT_TRUE(fn == nullptr);
1238 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1239 }
1240
1241 {
1242 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1243 ASSERT_TRUE(fn != nullptr) << dlerror();
1244 ASSERT_EQ(2, fn());
1245 }
1246
1247 dlclose(handle);
1248 }
1249
1250 // This preempts the implementation from libtest_versioned_lib.so
version_zero_function()1251 extern "C" int version_zero_function() {
1252 return 0;
1253 }
1254
1255 // This preempts the implementation from libtest_versioned_uselibv*.so
version_zero_function2()1256 extern "C" int version_zero_function2() {
1257 return 0;
1258 }
1259
TEST(dlfcn,dt_runpath_smoke)1260 TEST(dlfcn, dt_runpath_smoke) {
1261 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1262 ASSERT_TRUE(handle != nullptr) << dlerror();
1263
1264 typedef void *(* dlopen_b_fn)();
1265 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1266 ASSERT_TRUE(fn != nullptr) << dlerror();
1267
1268 void *p = fn();
1269 ASSERT_TRUE(p != nullptr);
1270
1271 dlclose(handle);
1272 }
1273
TEST(dlfcn,dt_runpath_absolute_path)1274 TEST(dlfcn, dt_runpath_absolute_path) {
1275 std::string libpath = get_testlib_root() + "/libtest_dt_runpath_d.so";
1276 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1277 ASSERT_TRUE(handle != nullptr) << dlerror();
1278
1279 typedef void *(* dlopen_b_fn)();
1280 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1281 ASSERT_TRUE(fn != nullptr) << dlerror();
1282
1283 void *p = fn();
1284 ASSERT_TRUE(p != nullptr);
1285
1286 dlclose(handle);
1287 }
1288
TEST(dlfcn,RTLD_macros)1289 TEST(dlfcn, RTLD_macros) {
1290 #if !defined(RTLD_LOCAL)
1291 #error no RTLD_LOCAL
1292 #elif !defined(RTLD_LAZY)
1293 #error no RTLD_LAZY
1294 #elif !defined(RTLD_NOW)
1295 #error no RTLD_NOW
1296 #elif !defined(RTLD_NOLOAD)
1297 #error no RTLD_NOLOAD
1298 #elif !defined(RTLD_GLOBAL)
1299 #error no RTLD_GLOBAL
1300 #elif !defined(RTLD_NODELETE)
1301 #error no RTLD_NODELETE
1302 #endif
1303 }
1304
1305 // Bionic specific tests
1306 #if defined(__BIONIC__)
1307
1308 #if defined(__arm__)
to_dynamic_table(const char * p)1309 const llvm::ELF::Elf32_Dyn* to_dynamic_table(const char* p) {
1310 return reinterpret_cast<const llvm::ELF::Elf32_Dyn*>(p);
1311 }
1312
1313 // Duplicate these definitions here because they are android specific
1314 // note that we cannot include <elf.h> because #defines conflict with
1315 // enum names provided by LLVM.
1316 #define DT_ANDROID_REL (llvm::ELF::DT_LOOS + 2)
1317 #define DT_ANDROID_RELA (llvm::ELF::DT_LOOS + 4)
1318
1319 template<typename ELFT>
validate_compatibility_of_native_library(const std::string & path,ELFT * elf)1320 void validate_compatibility_of_native_library(const std::string& path, ELFT* elf) {
1321 bool has_elf_hash = false;
1322 bool has_android_rel = false;
1323 bool has_rel = false;
1324 // Find dynamic section and check that DT_HASH and there is no DT_ANDROID_REL
1325 for (auto it = elf->section_begin(); it != elf->section_end(); ++it) {
1326 const llvm::object::ELFSectionRef& section_ref = *it;
1327 if (section_ref.getType() == llvm::ELF::SHT_DYNAMIC) {
1328 llvm::StringRef data;
1329 ASSERT_TRUE(!it->getContents(data)) << "unable to get SHT_DYNAMIC section data";
1330 for (auto d = to_dynamic_table(data.data()); d->d_tag != llvm::ELF::DT_NULL; ++d) {
1331 if (d->d_tag == llvm::ELF::DT_HASH) {
1332 has_elf_hash = true;
1333 } else if (d->d_tag == DT_ANDROID_REL || d->d_tag == DT_ANDROID_RELA) {
1334 has_android_rel = true;
1335 } else if (d->d_tag == llvm::ELF::DT_REL || d->d_tag == llvm::ELF::DT_RELA) {
1336 has_rel = true;
1337 }
1338 }
1339
1340 break;
1341 }
1342 }
1343
1344 ASSERT_TRUE(has_elf_hash) << path.c_str() << ": missing elf hash (DT_HASH)";
1345 ASSERT_TRUE(!has_android_rel) << path.c_str() << ": has packed relocations";
1346 ASSERT_TRUE(has_rel) << path.c_str() << ": missing DT_REL/DT_RELA";
1347 }
1348
validate_compatibility_of_native_library(const char * soname)1349 void validate_compatibility_of_native_library(const char* soname) {
1350 // On the systems with emulation system libraries would be of different
1351 // architecture. Try to use alternate paths first.
1352 std::string path = std::string(ALTERNATE_PATH_TO_SYSTEM_LIB) + soname;
1353 auto binary_or_error = llvm::object::createBinary(path);
1354 if (!binary_or_error) {
1355 path = std::string(PATH_TO_SYSTEM_LIB) + soname;
1356 binary_or_error = llvm::object::createBinary(path);
1357 }
1358 ASSERT_FALSE(!binary_or_error);
1359
1360 llvm::object::Binary* binary = binary_or_error.get().getBinary();
1361
1362 auto obj = llvm::dyn_cast<llvm::object::ObjectFile>(binary);
1363 ASSERT_TRUE(obj != nullptr);
1364
1365 auto elf = llvm::dyn_cast<llvm::object::ELF32LEObjectFile>(obj);
1366
1367 ASSERT_TRUE(elf != nullptr);
1368
1369 validate_compatibility_of_native_library(path, elf);
1370 }
1371
1372 // This is a test for app compatibility workaround for arm apps
1373 // affected by http://b/24465209
TEST(dlext,compat_elf_hash_and_relocation_tables)1374 TEST(dlext, compat_elf_hash_and_relocation_tables) {
1375 validate_compatibility_of_native_library("libc.so");
1376 validate_compatibility_of_native_library("liblog.so");
1377 validate_compatibility_of_native_library("libstdc++.so");
1378 validate_compatibility_of_native_library("libdl.so");
1379 validate_compatibility_of_native_library("libm.so");
1380 validate_compatibility_of_native_library("libz.so");
1381 validate_compatibility_of_native_library("libjnigraphics.so");
1382 }
1383
1384 #endif // defined(__arm__)
1385
TEST(dlfcn,dlopen_invalid_rw_load_segment)1386 TEST(dlfcn, dlopen_invalid_rw_load_segment) {
1387 const std::string libpath = get_testlib_root() +
1388 "/" + kPrebuiltElfDir +
1389 "/libtest_invalid-rw_load_segment.so";
1390 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1391 ASSERT_TRUE(handle == nullptr);
1392 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W + E load segments are not allowed";
1393 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1394 }
1395
TEST(dlfcn,dlopen_invalid_unaligned_shdr_offset)1396 TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
1397 const std::string libpath = get_testlib_root() +
1398 "/" + kPrebuiltElfDir +
1399 "/libtest_invalid-unaligned_shdr_offset.so";
1400
1401 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1402 ASSERT_TRUE(handle == nullptr);
1403 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
1404 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1405 }
1406
TEST(dlfcn,dlopen_invalid_zero_shentsize)1407 TEST(dlfcn, dlopen_invalid_zero_shentsize) {
1408 const std::string libpath = get_testlib_root() +
1409 "/" + kPrebuiltElfDir +
1410 "/libtest_invalid-zero_shentsize.so";
1411
1412 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1413 ASSERT_TRUE(handle == nullptr);
1414 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x";
1415 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1416 }
1417
TEST(dlfcn,dlopen_invalid_zero_shstrndx)1418 TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
1419 const std::string libpath = get_testlib_root() +
1420 "/" + kPrebuiltElfDir +
1421 "/libtest_invalid-zero_shstrndx.so";
1422
1423 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1424 ASSERT_TRUE(handle == nullptr);
1425 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx";
1426 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1427 }
1428
TEST(dlfcn,dlopen_invalid_empty_shdr_table)1429 TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
1430 const std::string libpath = get_testlib_root() +
1431 "/" + kPrebuiltElfDir +
1432 "/libtest_invalid-empty_shdr_table.so";
1433
1434 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1435 ASSERT_TRUE(handle == nullptr);
1436 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers";
1437 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1438 }
1439
TEST(dlfcn,dlopen_invalid_zero_shdr_table_offset)1440 TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
1441 const std::string libpath = get_testlib_root() +
1442 "/" + kPrebuiltElfDir +
1443 "/libtest_invalid-zero_shdr_table_offset.so";
1444
1445 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1446 ASSERT_TRUE(handle == nullptr);
1447 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/";
1448 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1449 }
1450
TEST(dlfcn,dlopen_invalid_zero_shdr_table_content)1451 TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
1452 const std::string libpath = get_testlib_root() +
1453 "/" + kPrebuiltElfDir +
1454 "/libtest_invalid-zero_shdr_table_content.so";
1455
1456 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1457 ASSERT_TRUE(handle == nullptr);
1458 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
1459 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1460 }
1461
TEST(dlfcn,dlopen_invalid_textrels)1462 TEST(dlfcn, dlopen_invalid_textrels) {
1463 const std::string libpath = get_testlib_root() +
1464 "/" + kPrebuiltElfDir +
1465 "/libtest_invalid-textrels.so";
1466
1467 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1468 ASSERT_TRUE(handle == nullptr);
1469 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1470 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1471 }
1472
TEST(dlfcn,dlopen_invalid_textrels2)1473 TEST(dlfcn, dlopen_invalid_textrels2) {
1474 const std::string libpath = get_testlib_root() +
1475 "/" + kPrebuiltElfDir +
1476 "/libtest_invalid-textrels2.so";
1477
1478 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1479 ASSERT_TRUE(handle == nullptr);
1480 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1481 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1482 }
1483
1484 #endif
1485