blob: 23b133bbea96195aa246b92407bd9afc49f6d22e [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 <limits.h>
21#include <stdio.h>
22#include <stdint.h>
Dimitry Ivanov708589f2016-09-19 10:50:28 -070023#include <string.h>
Elliott Hughes8e15b082012-09-26 11:44:01 -070024
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
Dimitry Ivanov708589f2016-09-19 10:50:28 -070029#include "dlfcn_symlink_support.h"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070030#include "utils.h"
31
Elliott Hughes3b297c42012-10-11 16:08:51 -070032#define ASSERT_SUBSTR(needle, haystack) \
33 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
34
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070035
Elliott Hughes1728b232014-05-14 10:02:03 -070036static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070037extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070038 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070039}
40
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070041static int g_ctor_function_called = 0;
Dimitry Ivanov55437462016-07-20 15:33:07 -070042static int g_ctor_argc = 0;
43static char** g_ctor_argv = reinterpret_cast<char**>(0xDEADBEEF);
44static char** g_ctor_envp = g_ctor_envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070045
Dimitry Ivanov55437462016-07-20 15:33:07 -070046extern "C" void ctor_function(int argc, char** argv, char** envp) __attribute__ ((constructor));
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070047
Dimitry Ivanov55437462016-07-20 15:33:07 -070048extern "C" void ctor_function(int argc, char** argv, char** envp) {
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070049 g_ctor_function_called = 17;
Dimitry Ivanov55437462016-07-20 15:33:07 -070050 g_ctor_argc = argc;
51 g_ctor_argv = argv;
52 g_ctor_envp = envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070053}
54
55TEST(dlfcn, ctor_function_call) {
56 ASSERT_EQ(17, g_ctor_function_called);
Dimitry Ivanov55437462016-07-20 15:33:07 -070057 ASSERT_TRUE(g_ctor_argc = get_argc());
58 ASSERT_TRUE(g_ctor_argv = get_argv());
59 ASSERT_TRUE(g_ctor_envp = get_envp());
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070060}
61
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070062TEST(dlfcn, dlsym_in_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070063 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070064 void* self = dlopen(nullptr, RTLD_NOW);
65 ASSERT_TRUE(self != nullptr);
66 ASSERT_TRUE(dlerror() == nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070067
68 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070069 ASSERT_TRUE(sym != nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070070
71 void (*function)() = reinterpret_cast<void(*)()>(sym);
72
Elliott Hughes1728b232014-05-14 10:02:03 -070073 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070074 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070075 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070076
77 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070078}
Elliott Hughes8e15b082012-09-26 11:44:01 -070079
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070080TEST(dlfcn, dlsym_from_sofile) {
81 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL);
82 ASSERT_TRUE(handle != nullptr) << dlerror();
83
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070084 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070085 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
86 ASSERT_TRUE(symbol == nullptr);
87 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
88
89 typedef int* (*fn_t)();
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070090 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
91 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
92 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070093
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070094 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070095 ASSERT_TRUE(ptr != nullptr) << dlerror();
96 ASSERT_EQ(42, *ptr);
97
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070098 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
99 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
100 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
101
102 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
103 ASSERT_TRUE(ptr != nullptr) << dlerror();
104 ASSERT_EQ(44, *ptr);
105
106 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
107 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
108 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
109
110 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
111 ASSERT_TRUE(ptr != nullptr) << dlerror();
112 ASSERT_EQ(43, *ptr);
113
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700114 dlclose(handle);
115}
116
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700117TEST(dlfcn, dlsym_from_sofile_with_preload) {
118 void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL);
119 ASSERT_TRUE(preload != nullptr) << dlerror();
120
121 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
122 ASSERT_TRUE(handle != nullptr) << dlerror();
123
124 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
125 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
126 ASSERT_TRUE(symbol == nullptr);
127 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
128
129 typedef int* (*fn_t)();
130 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
131 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
132 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
133
134 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
135 ASSERT_TRUE(ptr != nullptr) << dlerror();
136 ASSERT_EQ(42, *ptr);
137
138 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
139 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
140 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
141
142 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
143 ASSERT_TRUE(ptr != nullptr) << dlerror();
144 ASSERT_EQ(44, *ptr);
145
146 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
147 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
148 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
149
150 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
151 ASSERT_TRUE(ptr != nullptr) << dlerror();
152 ASSERT_EQ(43, *ptr);
153
154 dlclose(handle);
155 dlclose(preload);
156}
157
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700158TEST(dlfcn, dlsym_handle_global_sym) {
159 // check that we do not look into global group
160 // when looking up symbol by handle
161 void* handle = dlopen("libtest_empty.so", RTLD_NOW);
162 dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL);
163 void* sym = dlsym(handle, "getRandomNumber");
164 ASSERT_TRUE(sym == nullptr);
165 ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror());
166
167 sym = dlsym(handle, "DlSymTestFunction");
168 ASSERT_TRUE(sym == nullptr);
169 ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror());
170 dlclose(handle);
171}
172
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700173TEST(dlfcn, dlsym_with_dependencies) {
174 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700175 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700176 dlerror();
177 // This symbol is in DT_NEEDED library.
178 void* sym = dlsym(handle, "getRandomNumber");
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700179 ASSERT_TRUE(sym != nullptr) << dlerror();
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700180 int (*fn)(void);
181 fn = reinterpret_cast<int (*)(void)>(sym);
182 EXPECT_EQ(4, fn());
183 dlclose(handle);
184}
185
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700186TEST(dlfcn, dlopen_noload) {
187 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700188 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700189 handle = dlopen("libtest_simple.so", RTLD_NOW);
190 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700191 ASSERT_TRUE(handle != nullptr);
192 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700193 ASSERT_TRUE(handle == handle2);
194 ASSERT_EQ(0, dlclose(handle));
195 ASSERT_EQ(0, dlclose(handle2));
196}
197
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700198TEST(dlfcn, dlopen_by_soname) {
199 static const char* soname = "libdlext_test_soname.so";
200 static const char* filename = "libdlext_test_different_soname.so";
201 // 1. Make sure there is no library with soname in default search path
202 void* handle = dlopen(soname, RTLD_NOW);
203 ASSERT_TRUE(handle == nullptr);
204
205 // 2. Load a library using filename
206 handle = dlopen(filename, RTLD_NOW);
207 ASSERT_TRUE(handle != nullptr) << dlerror();
208
209 // 3. Find library by soname
210 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
211 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
212 ASSERT_EQ(handle, handle_soname);
213
214 // 4. RTLD_NOLOAD should still work with filename
215 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
216 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
217 ASSERT_EQ(handle, handle_filename);
218
219 dlclose(handle_filename);
220 dlclose(handle_soname);
221 dlclose(handle);
222}
223
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700224// mips doesn't support ifuncs
225#if !defined(__mips__)
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700226TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700227 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700228
229 // ifunc's choice depends on whether IFUNC_CHOICE has a value
230 // first check the set case
231 setenv("IFUNC_CHOICE", "set", 1);
232 void* 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 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
235 fn_ptr 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("set", foo_ptr(), 3), 0);
239 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700240 dlclose(handle);
241
242 // then check the unset case
243 unsetenv("IFUNC_CHOICE");
244 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700245 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700246 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
247 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700248 ASSERT_TRUE(foo_ptr != nullptr);
249 ASSERT_TRUE(foo_library_ptr != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700250 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
251 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0);
252 dlclose(handle);
253}
254
255TEST(dlfcn, ifunc_ctor_call) {
256 typedef const char* (*fn_ptr)();
257
258 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700259 ASSERT_TRUE(handle != nullptr) << dlerror();
260 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
261 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
262 ASSERT_STREQ("false", is_ctor_called());
263
264 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
265 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700266 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700267 dlclose(handle);
268}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700269
270TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
271 typedef const char* (*fn_ptr)();
272
273 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
274 ASSERT_TRUE(handle != nullptr) << dlerror();
275 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
276 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
277 ASSERT_STREQ("false", is_ctor_called());
278
279 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
280 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
281 ASSERT_STREQ("true", is_ctor_called());
282 dlclose(handle);
283}
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700284#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700285
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700286TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
287 // This is the structure of the test library and
288 // its dt_needed libraries
289 // libtest_relo_check_dt_needed_order.so
290 // |
291 // +-> libtest_relo_check_dt_needed_order_1.so
292 // |
293 // +-> libtest_relo_check_dt_needed_order_2.so
294 //
295 // The root library references relo_test_get_answer_lib - which is defined
296 // in both dt_needed libraries, the correct relocation should
297 // use the function defined in libtest_relo_check_dt_needed_order_1.so
298 void* handle = nullptr;
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -0700299 auto guard = make_scope_guard([&]() {
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700300 dlclose(handle);
301 });
302
303 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
304 ASSERT_TRUE(handle != nullptr) << dlerror();
305
306 typedef int (*fn_t) (void);
307 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
308 ASSERT_TRUE(fn != nullptr) << dlerror();
309 ASSERT_EQ(1, fn());
310}
311
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700312TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700313 // Here is how the test library and its dt_needed
314 // libraries are arranged
315 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700316 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700317 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700318 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700319 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700320 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700321 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700322 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700323 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700324 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700325 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700326 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700327 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700328 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700329 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700330 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700331 //
332 // load order should be (1, 2, 3, a, b, d)
333 //
334 // get_answer() is defined in (2, 3, a, b, c)
335 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700336 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700337 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700338 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
339 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700340 typedef int (*fn_t) (void);
341 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700342 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700343 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700344 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700345 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700346
347 ASSERT_EQ(42, fn());
348 ASSERT_EQ(43, fn2());
349 dlclose(handle);
350}
351
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700352TEST(dlfcn, dlopen_check_order_reloc_siblings) {
353 // This is how this one works:
354 // we lookup and call get_answer which is defined in '_2.so'
355 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
356 // the correct _impl() is implemented by '_a.so';
357 //
358 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
359 //
360 // Here is the picture:
361 //
362 // libtest_check_order_reloc_siblings.so
363 // |
364 // +-> ..._1.so <- empty
365 // | |
366 // | +-> ..._a.so <- exports correct answer_impl()
367 // | |
368 // | +-> ..._b.so <- every other letter exporting incorrect one.
369 // |
370 // +-> ..._2.so <- empty
371 // | |
372 // | +-> ..._c.so
373 // | |
374 // | +-> ..._d.so
375 // |
376 // +-> ..._3.so <- empty
377 // |
378 // +-> ..._e.so
379 // |
380 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
381 // implements incorrect get_answer_impl()
382
383 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
384 ASSERT_TRUE(handle == nullptr);
385#ifdef __BIONIC__
386 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
387 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
388#endif
389
390 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
391 ASSERT_TRUE(handle != nullptr) << dlerror();
392
393 typedef int (*fn_t) (void);
394 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
395 ASSERT_TRUE(fn != nullptr) << dlerror();
396 ASSERT_EQ(42, fn());
397
398 ASSERT_EQ(0, dlclose(handle));
399}
400
401TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
402 // This test uses the same library as dlopen_check_order_reloc_siblings.
403 // Unlike dlopen_check_order_reloc_siblings it preloads
404 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
405 // dlopen(libtest_check_order_reloc_siblings.so)
406
407 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
408 ASSERT_TRUE(handle == nullptr);
409 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
410 ASSERT_TRUE(handle == nullptr);
411
412 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
413 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
414
415 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
416 ASSERT_TRUE(handle != nullptr) << dlerror();
417
418 ASSERT_EQ(0, dlclose(handle_for_1));
419
420 typedef int (*fn_t) (void);
421 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
422 ASSERT_TRUE(fn != nullptr) << dlerror();
423 ASSERT_EQ(42, fn());
424
425 ASSERT_EQ(0, dlclose(handle));
426}
427
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800428TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
429 // This is how this one works:
430 // we lookup and call grandchild_get_answer which is defined in '_2.so'
431 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
432 // the correct _impl() is implemented by '_c_1.so';
433 //
434 // Here is the picture of subtree:
435 //
436 // libtest_check_order_reloc_siblings.so
437 // |
438 // +-> ..._2.so <- grandchild_get_answer()
439 // |
440 // +-> ..._c.so <- empty
441 // | |
442 // | +-> _c_1.so <- exports correct answer_impl()
443 // | |
444 // | +-> _c_2.so <- exports incorrect answer_impl()
445 // |
446 // +-> ..._d.so <- empty
447
448 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
449 ASSERT_TRUE(handle == nullptr);
450#ifdef __BIONIC__
451 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
452 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
453#endif
454
455 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
456 ASSERT_TRUE(handle != nullptr) << dlerror();
457
458 typedef int (*fn_t) (void);
459 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
460 ASSERT_TRUE(fn != nullptr) << dlerror();
461 ASSERT_EQ(42, fn());
462
463 ASSERT_EQ(0, dlclose(handle));
464}
465
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700466TEST(dlfcn, dlopen_check_order_reloc_nephew) {
467 // This is how this one works:
468 // we lookup and call nephew_get_answer which is defined in '_2.so'
469 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
470 // the correct _impl() is implemented by '_a.so';
471 //
472 // Here is the picture:
473 //
474 // libtest_check_order_reloc_siblings.so
475 // |
476 // +-> ..._1.so <- empty
477 // | |
478 // | +-> ..._a.so <- exports correct answer_impl()
479 // | |
480 // | +-> ..._b.so <- every other letter exporting incorrect one.
481 // |
482 // +-> ..._2.so <- empty
483 // | |
484 // | +-> ..._c.so
485 // | |
486 // | +-> ..._d.so
487 // |
488 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
489 // |
490 // +-> ..._e.so
491 // |
492 // +-> ..._f.so
493
494 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
495 ASSERT_TRUE(handle == nullptr);
496#ifdef __BIONIC__
497 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
498 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
499#endif
500
501 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
502 ASSERT_TRUE(handle != nullptr) << dlerror();
503
504 typedef int (*fn_t) (void);
505 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
506 ASSERT_TRUE(fn != nullptr) << dlerror();
507 ASSERT_EQ(42, fn());
508
509 ASSERT_EQ(0, dlclose(handle));
510}
511
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800512TEST(dlfcn, check_unload_after_reloc) {
513 // This is how this one works:
514 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
515 // |
516 // +-> libtest_two_parents_child
517 //
518 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
519 // |
520 // +-> libtest_two_parents_child
521 //
522 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
523 // as a second step it dlopens parent2 and dlcloses parent1...
524
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700525 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
526 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800527
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700528 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
529 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800530
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700531 typedef int (*fn_t) (void);
532 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
533 ASSERT_TRUE(fn != nullptr) << dlerror();
534 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800535
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700536 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800537
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700538 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
539 ASSERT_TRUE(handle != nullptr);
540 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800541
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700542 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
543 ASSERT_TRUE(fn != nullptr) << dlerror();
544 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800545
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700546 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800547
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700548 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
549 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800550}
551
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700552extern "C" int check_order_reloc_root_get_answer_impl() {
553 return 42;
554}
555
556TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
557 // This is how this one works:
558 // we lookup and call get_answer3 which is defined in 'root.so'
559 // and in turn calls external root_get_answer_impl() defined in _2.so and
560 // above the correct _impl() is one in the executable.
561 //
562 // libtest_check_order_reloc_root.so
563 // |
564 // +-> ..._1.so <- empty
565 // |
566 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
567 //
568
569 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
570 ASSERT_TRUE(handle == nullptr);
571#ifdef __BIONIC__
572 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
573 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
574#endif
575
576 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
577 ASSERT_TRUE(handle != nullptr) << dlerror();
578
579 typedef int (*fn_t) (void);
580 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
581 ASSERT_TRUE(fn != nullptr) << dlerror();
582 ASSERT_EQ(42, fn());
583
584 ASSERT_EQ(0, dlclose(handle));
585}
586
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700587TEST(dlfcn, dlopen_check_rtld_local) {
588 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
589 ASSERT_TRUE(sym == nullptr);
590
591 // implicit RTLD_LOCAL
592 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
593 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
594 ASSERT_TRUE(sym == nullptr);
595 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
596 sym = dlsym(handle, "dlopen_testlib_simple_func");
597 ASSERT_TRUE(sym != nullptr);
598 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
599 dlclose(handle);
600
601 // explicit RTLD_LOCAL
602 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
603 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
604 ASSERT_TRUE(sym == nullptr);
605 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
606 sym = dlsym(handle, "dlopen_testlib_simple_func");
607 ASSERT_TRUE(sym != nullptr);
608 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
609 dlclose(handle);
610}
611
612TEST(dlfcn, dlopen_check_rtld_global) {
613 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
614 ASSERT_TRUE(sym == nullptr);
615
616 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700617 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700618 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
619 ASSERT_TRUE(sym != nullptr) << dlerror();
620 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
621 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700622
623 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
624 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
625 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700626
627 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
628 // shared libraries after symbol was not found in the main executable
629 // and dependent libraries.
630 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
631 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
632 ASSERT_TRUE(sym != nullptr) << dlerror();
633
634 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700635}
636
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700637// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
638// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
639// libtest_with_dependency_loop_a.so
640TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700641 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
642 ASSERT_TRUE(handle != nullptr) << dlerror();
643 void* f = dlsym(handle, "dlopen_test_loopy_function");
644 ASSERT_TRUE(f != nullptr) << dlerror();
645 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
646 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700647
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700648 // dlopen second time to make sure that the library was unloaded correctly
649 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
650 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800651#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700652 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 -0800653#else
654 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
655 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700656#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800657
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700658 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
659 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700660}
661
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700662TEST(dlfcn, dlopen_nodelete) {
663 static bool is_unloaded = false;
664
665 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
666 ASSERT_TRUE(handle != nullptr) << dlerror();
667 void (*set_unload_flag_ptr)(bool*);
668 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
669 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
670 set_unload_flag_ptr(&is_unloaded);
671
672 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
673 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
674 ASSERT_EQ(1729U, *taxicab_number);
675 *taxicab_number = 2;
676
677 dlclose(handle);
678 ASSERT_TRUE(!is_unloaded);
679
680 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
681 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
682 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
683
684
685 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
686 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
687 ASSERT_EQ(taxicab_number2, taxicab_number);
688
689 ASSERT_EQ(2U, *taxicab_number2);
690
691 dlclose(handle);
692 ASSERT_TRUE(!is_unloaded);
693}
694
695TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
696 static bool is_unloaded = false;
697
698 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
699 ASSERT_TRUE(handle != nullptr) << dlerror();
700 void (*set_unload_flag_ptr)(bool*);
701 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
702 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
703 set_unload_flag_ptr(&is_unloaded);
704
705 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
706 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
707
708 ASSERT_EQ(1729U, *taxicab_number);
709 *taxicab_number = 2;
710
711 // This RTLD_NODELETE should be ignored
712 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
713 ASSERT_TRUE(handle1 != nullptr) << dlerror();
714 ASSERT_EQ(handle, handle1);
715
716 dlclose(handle1);
717 dlclose(handle);
718
719 ASSERT_TRUE(is_unloaded);
720}
721
722TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
723 static bool is_unloaded = false;
724
725 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
726 ASSERT_TRUE(handle != nullptr) << dlerror();
727 void (*set_unload_flag_ptr)(bool*);
728 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
729 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
730 set_unload_flag_ptr(&is_unloaded);
731
732 dlclose(handle);
733 ASSERT_TRUE(!is_unloaded);
734}
735
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700736TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700737 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
738 ASSERT_TRUE(handle != nullptr) << dlerror();
739 int (*get_answer)();
740 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
741 ASSERT_TRUE(get_answer != nullptr) << dlerror();
742 ASSERT_EQ(42, get_answer());
743 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700744}
745
Elliott Hughese66190d2012-12-18 15:57:55 -0800746TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700747 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700748 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700749#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700750 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
751#else
752 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
753#endif
754}
755
Elliott Hughesad88a082012-10-24 18:37:21 -0700756static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700757 dlopen("/child/thread", RTLD_NOW);
758 return reinterpret_cast<void*>(strdup(dlerror()));
759}
760
Elliott Hughese66190d2012-12-18 15:57:55 -0800761TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700762 dlopen("/main/thread", RTLD_NOW);
763 const char* main_thread_error = dlerror();
764 ASSERT_SUBSTR("/main/thread", main_thread_error);
765
766 pthread_t t;
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700767 ASSERT_EQ(0, pthread_create(&t, nullptr, ConcurrentDlErrorFn, nullptr));
Elliott Hughes5419b942012-10-16 15:54:46 -0700768 void* result;
769 ASSERT_EQ(0, pthread_join(t, &result));
770 char* child_thread_error = static_cast<char*>(result);
771 ASSERT_SUBSTR("/child/thread", child_thread_error);
772 free(child_thread_error);
773
774 ASSERT_SUBSTR("/main/thread", main_thread_error);
775}
776
Elliott Hughese66190d2012-12-18 15:57:55 -0800777TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700778 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700779 void* self = dlopen(nullptr, RTLD_NOW);
780 ASSERT_TRUE(self != nullptr);
781 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700782
783 void* sym;
784
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700785#if defined(__BIONIC__) && !defined(__LP64__)
786 // RTLD_DEFAULT in lp32 bionic is not (void*)0
787 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700788 sym = dlsym(nullptr, "test");
789 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800790 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700791#endif
792
Elliott Hughes3b297c42012-10-11 16:08:51 -0700793 // Symbol that doesn't exist.
794 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700795 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700796 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700797
798 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700799}
800
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700801TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700802 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700803 void* self = dlopen(nullptr, RTLD_NOW);
804 ASSERT_TRUE(self != nullptr);
805 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700806
807 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700808 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700809
810 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
811 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
812
813 Dl_info info;
814 int rc = dladdr(addr, &info);
815 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
816
817 // Get the name of this executable.
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700818 const std::string& executable_path = get_executable_path();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700819
820 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700821 char dli_realpath[PATH_MAX];
822 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700823 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700824
825 // The symbol name should be the symbol we looked up.
826 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
827
828 // The address should be the exact address of the symbol.
829 ASSERT_EQ(info.dli_saddr, sym);
830
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700831 std::vector<map_record> maps;
832 ASSERT_TRUE(Maps::parse_maps(&maps));
833
834 void* base_address = nullptr;
835 for (const map_record& rec : maps) {
836 if (executable_path == rec.pathname) {
837 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700838 break;
839 }
840 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700841
842 // The base address should be the address we were loaded at.
843 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700844
845 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700846}
847
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700848TEST(dlfcn, dlopen_executable_by_absolute_path) {
849 void* handle1 = dlopen(nullptr, RTLD_NOW);
850 ASSERT_TRUE(handle1 != nullptr) << dlerror();
851
852 void* handle2 = dlopen(get_executable_path().c_str(), RTLD_NOW);
853 ASSERT_TRUE(handle2 != nullptr) << dlerror();
854
855#if defined(__BIONIC__)
856 ASSERT_EQ(handle1, handle2);
857#else
858 GTEST_LOG_(INFO) << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
859 "it loads a separate copy of the main executable "
860 "on dlopen by absolute path.";
861#endif
862}
863
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700864#if defined(__LP64__)
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200865#define PATH_TO_SYSTEM_LIB "/system/lib64/"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700866#else
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200867#define PATH_TO_SYSTEM_LIB "/system/lib/"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700868#endif
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200869#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700870
871TEST(dlfcn, dladdr_libc) {
872#if defined(__BIONIC__)
873 Dl_info info;
874 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
875 ASSERT_TRUE(dladdr(addr, &info) != 0);
876
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700877 // /system/lib is symlink when this test is executed on host.
878 char libc_realpath[PATH_MAX];
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200879 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700880
881 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700882 // TODO: add check for dfi_fbase
883 ASSERT_STREQ("puts", info.dli_sname);
884 ASSERT_EQ(addr, info.dli_saddr);
885#else
886 GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig "
887 "for libc.so, which is symlink itself (not a realpath).\n";
888#endif
889}
890
Elliott Hughese66190d2012-12-18 15:57:55 -0800891TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700892 Dl_info info;
893
Elliott Hughes3b297c42012-10-11 16:08:51 -0700894 dlerror(); // Clear any pending errors.
895
Elliott Hughes8e15b082012-09-26 11:44:01 -0700896 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700897 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
898 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700899
900 // No symbol corresponding to a stack address.
901 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700902 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700903}
Elliott Hughes124fae92012-10-31 14:20:03 -0700904
Elliott Hughesa43e9062013-01-07 14:18:22 -0800905// GNU-style ELF hash tables are incompatible with the MIPS ABI.
906// 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 -0800907TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800908#if !defined(__mips__)
Elliott Hughes124fae92012-10-31 14:20:03 -0700909 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800910 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
911 ASSERT_TRUE(handle != nullptr) << dlerror();
912 auto guard = make_scope_guard([&]() {
913 dlclose(handle);
914 });
915 void* sym = dlsym(handle, "getRandomNumber");
916 ASSERT_TRUE(sym != nullptr) << dlerror();
917 int (*fn)(void);
918 fn = reinterpret_cast<int (*)(void)>(sym);
919 EXPECT_EQ(4, fn());
920
921 Dl_info dlinfo;
922 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
923
924 ASSERT_TRUE(fn == dlinfo.dli_saddr);
925 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800926 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800927#else
928 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
929#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700930}
Elliott Hughese66190d2012-12-18 15:57:55 -0800931
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800932TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
933 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
934 ASSERT_TRUE(handle != nullptr) << dlerror();
935 auto guard = make_scope_guard([&]() {
936 dlclose(handle);
937 });
938 void* sym = dlsym(handle, "getRandomNumber");
939 ASSERT_TRUE(sym != nullptr) << dlerror();
940 int (*fn)(void);
941 fn = reinterpret_cast<int (*)(void)>(sym);
942 EXPECT_EQ(4, fn());
943
944 Dl_info dlinfo;
945 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
946
947 ASSERT_TRUE(fn == dlinfo.dli_saddr);
948 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
949 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
950}
951
Elliott Hughese66190d2012-12-18 15:57:55 -0800952TEST(dlfcn, dlopen_bad_flags) {
953 dlerror(); // Clear any pending errors.
954 void* handle;
955
Elliott Hughes063525c2014-05-13 11:19:57 -0700956#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800957 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700958 handle = dlopen(nullptr, 0);
959 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -0800960 ASSERT_SUBSTR("invalid", dlerror());
961#endif
962
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700963 handle = dlopen(nullptr, 0xffffffff);
964 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -0800965 ASSERT_SUBSTR("invalid", dlerror());
966
967 // 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 -0700968 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
969 ASSERT_TRUE(handle != nullptr);
970 ASSERT_SUBSTR(nullptr, dlerror());
Elliott Hughese66190d2012-12-18 15:57:55 -0800971}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400972
973TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800974 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700975 ASSERT_TRUE(addr == nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400976}
977
978TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800979 void* addr = dlsym(RTLD_DEFAULT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700980 ASSERT_TRUE(addr != nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -0800981}
982
983TEST(dlfcn, rtld_next_unknown_symbol) {
984 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700985 ASSERT_TRUE(addr == nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -0800986}
987
988TEST(dlfcn, rtld_next_known_symbol) {
989 void* addr = dlsym(RTLD_NEXT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700990 ASSERT_TRUE(addr != nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400991}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700992
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700993TEST(dlfcn, dlsym_weak_func) {
994 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800995 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700996 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700997
998 int (*weak_func)();
999 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001000 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001001 EXPECT_EQ(42, weak_func());
1002 dlclose(handle);
1003}
1004
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001005TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001006 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
1007 ASSERT_TRUE(handle != nullptr) << dlerror();
1008 int (*weak_func)();
1009 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
1010 ASSERT_TRUE(weak_func != nullptr) << dlerror();
1011 EXPECT_EQ(6551, weak_func());
1012 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001013}
1014
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001015TEST(dlfcn, dlopen_symlink) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001016 DlfcnSymlink symlink("dlopen_symlink");
1017 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001018 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001019 void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001020 ASSERT_TRUE(handle1 != nullptr);
1021 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001022 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -07001023 dlclose(handle1);
1024 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001025}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001026
1027// libtest_dlopen_from_ctor_main.so depends on
1028// libtest_dlopen_from_ctor.so which has a constructor
1029// that calls dlopen(libc...). This is to test the situation
1030// described in b/7941716.
1031TEST(dlfcn, dlopen_dlopen_from_ctor) {
1032#if defined(__BIONIC__)
1033 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1034 ASSERT_TRUE(handle != nullptr) << dlerror();
1035 dlclose(handle);
1036#else
1037 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
1038#endif
1039}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001040
1041TEST(dlfcn, symbol_versioning_use_v1) {
1042 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1043 ASSERT_TRUE(handle != nullptr) << dlerror();
1044 typedef int (*fn_t)();
1045 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1046 ASSERT_TRUE(fn != nullptr) << dlerror();
1047 ASSERT_EQ(1, fn());
1048 dlclose(handle);
1049}
1050
1051TEST(dlfcn, symbol_versioning_use_v2) {
1052 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1053 ASSERT_TRUE(handle != nullptr) << dlerror();
1054 typedef int (*fn_t)();
1055 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1056 ASSERT_TRUE(fn != nullptr) << dlerror();
1057 ASSERT_EQ(2, fn());
1058 dlclose(handle);
1059}
1060
1061TEST(dlfcn, symbol_versioning_use_other_v2) {
1062 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1063 ASSERT_TRUE(handle != nullptr) << dlerror();
1064 typedef int (*fn_t)();
1065 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1066 ASSERT_TRUE(fn != nullptr) << dlerror();
1067 ASSERT_EQ(20, fn());
1068 dlclose(handle);
1069}
1070
1071TEST(dlfcn, symbol_versioning_use_other_v3) {
1072 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1073 ASSERT_TRUE(handle != nullptr) << dlerror();
1074 typedef int (*fn_t)();
1075 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1076 ASSERT_TRUE(fn != nullptr) << dlerror();
1077 ASSERT_EQ(3, fn());
1078 dlclose(handle);
1079}
1080
1081TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1082 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1083 ASSERT_TRUE(handle != nullptr) << dlerror();
1084 typedef int (*fn_t)();
1085 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1086 ASSERT_TRUE(fn != nullptr) << dlerror();
1087 ASSERT_EQ(3, fn()); // the default version is 3
1088 dlclose(handle);
1089}
1090
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001091TEST(dlfcn, dlvsym_smoke) {
1092 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1093 ASSERT_TRUE(handle != nullptr) << dlerror();
1094 typedef int (*fn_t)();
1095
1096 {
1097 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1098 ASSERT_TRUE(fn == nullptr);
1099 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1100 }
1101
1102 {
1103 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1104 ASSERT_TRUE(fn != nullptr) << dlerror();
1105 ASSERT_EQ(2, fn());
1106 }
1107
1108 dlclose(handle);
1109}
1110
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001111// This preempts the implementation from libtest_versioned_lib.so
1112extern "C" int version_zero_function() {
1113 return 0;
1114}
1115
1116// This preempts the implementation from libtest_versioned_uselibv*.so
1117extern "C" int version_zero_function2() {
1118 return 0;
1119}
Evgenii Stepanov68650822015-06-10 13:38:39 -07001120
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001121TEST(dlfcn, dt_runpath_smoke) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001122 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1123 ASSERT_TRUE(handle != nullptr) << dlerror();
1124
1125 typedef void *(* dlopen_b_fn)();
1126 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1127 ASSERT_TRUE(fn != nullptr) << dlerror();
1128
1129 void *p = fn();
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001130 ASSERT_TRUE(p != nullptr);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001131
1132 dlclose(handle);
1133}
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001134
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001135// Bionic specific tests
1136#if defined(__BIONIC__)
1137
1138#if defined(__LP64__)
1139#define NATIVE_TESTS_PATH "/nativetest64/bionic-loader-test-libs"
1140#else
1141#define NATIVE_TESTS_PATH "/nativetest/bionic-loader-test-libs"
1142#endif
1143
1144#define PREBUILT_ELF_PATH NATIVE_TESTS_PATH "/prebuilt-elf-files"
1145
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001146TEST(dlfcn, dt_runpath_absolute_path) {
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001147 std::string libpath = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH + "/libtest_dt_runpath_d.so";
1148 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001149 ASSERT_TRUE(handle != nullptr) << dlerror();
1150
1151 typedef void *(* dlopen_b_fn)();
1152 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1153 ASSERT_TRUE(fn != nullptr) << dlerror();
1154
1155 void *p = fn();
1156 ASSERT_TRUE(p != nullptr);
1157
1158 dlclose(handle);
1159}
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001160
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001161TEST(dlfcn, dlopen_invalid_rw_load_segment) {
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001162 std::string data_path;
1163 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
1164 const std::string libpath = data_path + PREBUILT_ELF_PATH + "/libtest_invalid-rw_load_segment.so";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001165 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1166 ASSERT_TRUE(handle == nullptr);
1167 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W + E load segments are not allowed";
1168 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1169}
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001170
1171TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001172 std::string data_path;
1173 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
1174 const std::string libpath = data_path + PREBUILT_ELF_PATH + "/libtest_invalid-unaligned_shdr_offset.so";
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001175 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1176 ASSERT_TRUE(handle == nullptr);
1177 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
1178 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1179}
1180
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001181TEST(dlfcn, dlopen_invalid_zero_shentsize) {
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001182 std::string data_path;
1183 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
1184 const std::string libpath = data_path + PREBUILT_ELF_PATH + "/libtest_invalid-zero_shentsize.so";
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001185 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1186 ASSERT_TRUE(handle == nullptr);
1187 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x";
1188 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1189}
1190
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001191TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001192 std::string data_path;
1193 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
1194 const std::string libpath = data_path + PREBUILT_ELF_PATH + "/libtest_invalid-zero_shstrndx.so";
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001195 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1196 ASSERT_TRUE(handle == nullptr);
1197 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx";
1198 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1199}
1200
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001201TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001202 std::string data_path;
1203 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
1204 std::string libpath = data_path + PREBUILT_ELF_PATH + "/libtest_invalid-empty_shdr_table.so";
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001205 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1206 ASSERT_TRUE(handle == nullptr);
1207 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers";
1208 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1209}
1210
Dimitry Ivanov46230442016-08-15 13:40:53 -07001211TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001212 std::string data_path;
1213 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
1214 const std::string libpath = data_path + PREBUILT_ELF_PATH + "/libtest_invalid-zero_shdr_table_offset.so";
Dimitry Ivanov46230442016-08-15 13:40:53 -07001215 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1216 ASSERT_TRUE(handle == nullptr);
1217 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/";
1218 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1219}
1220
Dimitry Ivanov55958342016-08-15 14:06:04 -07001221TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001222 std::string data_path;
1223 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
1224 const std::string libpath = data_path + PREBUILT_ELF_PATH + "/libtest_invalid-zero_shdr_table_content.so";
Dimitry Ivanov55958342016-08-15 14:06:04 -07001225 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1226 ASSERT_TRUE(handle == nullptr);
1227 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
1228 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1229}
1230
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001231#endif