blob: a1c58019c0891f70c83a5cb92aa782e68c50dccc [file] [log] [blame]
jeffhaoacf5aa72012-09-12 17:25:30 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <dlfcn.h>
Elliott Hughes8e15b082012-09-26 11:44:01 -070020#include <libgen.h>
21#include <limits.h>
22#include <stdio.h>
23#include <stdint.h>
24
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -070025#include "private/ScopeGuard.h"
26
Elliott Hughes8e15b082012-09-26 11:44:01 -070027#include <string>
jeffhaoacf5aa72012-09-12 17:25:30 -070028
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070029#include "utils.h"
30
Elliott Hughes3b297c42012-10-11 16:08:51 -070031#define ASSERT_SUBSTR(needle, haystack) \
32 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
33
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070034
Elliott Hughes1728b232014-05-14 10:02:03 -070035static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070036extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070037 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070038}
39
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070040static int g_ctor_function_called = 0;
Dimitry Ivanov55437462016-07-20 15:33:07 -070041static int g_ctor_argc = 0;
42static char** g_ctor_argv = reinterpret_cast<char**>(0xDEADBEEF);
43static char** g_ctor_envp = g_ctor_envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070044
Dimitry Ivanov55437462016-07-20 15:33:07 -070045extern "C" void ctor_function(int argc, char** argv, char** envp) __attribute__ ((constructor));
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070046
Dimitry Ivanov55437462016-07-20 15:33:07 -070047extern "C" void ctor_function(int argc, char** argv, char** envp) {
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070048 g_ctor_function_called = 17;
Dimitry Ivanov55437462016-07-20 15:33:07 -070049 g_ctor_argc = argc;
50 g_ctor_argv = argv;
51 g_ctor_envp = envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070052}
53
54TEST(dlfcn, ctor_function_call) {
55 ASSERT_EQ(17, g_ctor_function_called);
Dimitry Ivanov55437462016-07-20 15:33:07 -070056 ASSERT_TRUE(g_ctor_argc = get_argc());
57 ASSERT_TRUE(g_ctor_argv = get_argv());
58 ASSERT_TRUE(g_ctor_envp = get_envp());
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070059}
60
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070061TEST(dlfcn, dlsym_in_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070062 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070063 void* self = dlopen(nullptr, RTLD_NOW);
64 ASSERT_TRUE(self != nullptr);
65 ASSERT_TRUE(dlerror() == nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070066
67 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070068 ASSERT_TRUE(sym != nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070069
70 void (*function)() = reinterpret_cast<void(*)()>(sym);
71
Elliott Hughes1728b232014-05-14 10:02:03 -070072 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070073 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070074 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070075
76 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070077}
Elliott Hughes8e15b082012-09-26 11:44:01 -070078
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070079TEST(dlfcn, dlsym_from_sofile) {
80 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL);
81 ASSERT_TRUE(handle != nullptr) << dlerror();
82
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070083 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070084 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
85 ASSERT_TRUE(symbol == nullptr);
86 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
87
88 typedef int* (*fn_t)();
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070089 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
90 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
91 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070092
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070093 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070094 ASSERT_TRUE(ptr != nullptr) << dlerror();
95 ASSERT_EQ(42, *ptr);
96
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070097 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
98 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
99 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
100
101 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
102 ASSERT_TRUE(ptr != nullptr) << dlerror();
103 ASSERT_EQ(44, *ptr);
104
105 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
106 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
107 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
108
109 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
110 ASSERT_TRUE(ptr != nullptr) << dlerror();
111 ASSERT_EQ(43, *ptr);
112
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700113 dlclose(handle);
114}
115
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700116TEST(dlfcn, dlsym_from_sofile_with_preload) {
117 void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL);
118 ASSERT_TRUE(preload != nullptr) << dlerror();
119
120 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
121 ASSERT_TRUE(handle != nullptr) << dlerror();
122
123 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
124 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
125 ASSERT_TRUE(symbol == nullptr);
126 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
127
128 typedef int* (*fn_t)();
129 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
130 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
131 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
132
133 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
134 ASSERT_TRUE(ptr != nullptr) << dlerror();
135 ASSERT_EQ(42, *ptr);
136
137 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
138 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
139 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
140
141 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
142 ASSERT_TRUE(ptr != nullptr) << dlerror();
143 ASSERT_EQ(44, *ptr);
144
145 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
146 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
147 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
148
149 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
150 ASSERT_TRUE(ptr != nullptr) << dlerror();
151 ASSERT_EQ(43, *ptr);
152
153 dlclose(handle);
154 dlclose(preload);
155}
156
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700157TEST(dlfcn, dlsym_handle_global_sym) {
158 // check that we do not look into global group
159 // when looking up symbol by handle
160 void* handle = dlopen("libtest_empty.so", RTLD_NOW);
161 dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL);
162 void* sym = dlsym(handle, "getRandomNumber");
163 ASSERT_TRUE(sym == nullptr);
164 ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror());
165
166 sym = dlsym(handle, "DlSymTestFunction");
167 ASSERT_TRUE(sym == nullptr);
168 ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror());
169 dlclose(handle);
170}
171
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700172TEST(dlfcn, dlsym_with_dependencies) {
173 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700174 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700175 dlerror();
176 // This symbol is in DT_NEEDED library.
177 void* sym = dlsym(handle, "getRandomNumber");
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700178 ASSERT_TRUE(sym != nullptr) << dlerror();
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700179 int (*fn)(void);
180 fn = reinterpret_cast<int (*)(void)>(sym);
181 EXPECT_EQ(4, fn());
182 dlclose(handle);
183}
184
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700185TEST(dlfcn, dlopen_noload) {
186 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700187 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700188 handle = dlopen("libtest_simple.so", RTLD_NOW);
189 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700190 ASSERT_TRUE(handle != nullptr);
191 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700192 ASSERT_TRUE(handle == handle2);
193 ASSERT_EQ(0, dlclose(handle));
194 ASSERT_EQ(0, dlclose(handle2));
195}
196
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700197TEST(dlfcn, dlopen_by_soname) {
198 static const char* soname = "libdlext_test_soname.so";
199 static const char* filename = "libdlext_test_different_soname.so";
200 // 1. Make sure there is no library with soname in default search path
201 void* handle = dlopen(soname, RTLD_NOW);
202 ASSERT_TRUE(handle == nullptr);
203
204 // 2. Load a library using filename
205 handle = dlopen(filename, RTLD_NOW);
206 ASSERT_TRUE(handle != nullptr) << dlerror();
207
208 // 3. Find library by soname
209 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
210 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
211 ASSERT_EQ(handle, handle_soname);
212
213 // 4. RTLD_NOLOAD should still work with filename
214 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
215 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
216 ASSERT_EQ(handle, handle_filename);
217
218 dlclose(handle_filename);
219 dlclose(handle_soname);
220 dlclose(handle);
221}
222
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700223// mips doesn't support ifuncs
224#if !defined(__mips__)
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700225TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700226 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700227
228 // ifunc's choice depends on whether IFUNC_CHOICE has a value
229 // first check the set case
230 setenv("IFUNC_CHOICE", "set", 1);
231 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700232 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700233 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
234 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700235 ASSERT_TRUE(foo_ptr != nullptr);
236 ASSERT_TRUE(foo_library_ptr != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700237 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
238 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700239 dlclose(handle);
240
241 // then check the unset case
242 unsetenv("IFUNC_CHOICE");
243 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700244 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700245 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
246 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700247 ASSERT_TRUE(foo_ptr != nullptr);
248 ASSERT_TRUE(foo_library_ptr != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700249 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
250 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0);
251 dlclose(handle);
252}
253
254TEST(dlfcn, ifunc_ctor_call) {
255 typedef const char* (*fn_ptr)();
256
257 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700258 ASSERT_TRUE(handle != nullptr) << dlerror();
259 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
260 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
261 ASSERT_STREQ("false", is_ctor_called());
262
263 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
264 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700265 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700266 dlclose(handle);
267}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700268
269TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
270 typedef const char* (*fn_ptr)();
271
272 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
273 ASSERT_TRUE(handle != nullptr) << dlerror();
274 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
275 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
276 ASSERT_STREQ("false", is_ctor_called());
277
278 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
279 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
280 ASSERT_STREQ("true", is_ctor_called());
281 dlclose(handle);
282}
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700283#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700284
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700285TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
286 // This is the structure of the test library and
287 // its dt_needed libraries
288 // libtest_relo_check_dt_needed_order.so
289 // |
290 // +-> libtest_relo_check_dt_needed_order_1.so
291 // |
292 // +-> libtest_relo_check_dt_needed_order_2.so
293 //
294 // The root library references relo_test_get_answer_lib - which is defined
295 // in both dt_needed libraries, the correct relocation should
296 // use the function defined in libtest_relo_check_dt_needed_order_1.so
297 void* handle = nullptr;
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -0700298 auto guard = make_scope_guard([&]() {
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700299 dlclose(handle);
300 });
301
302 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
303 ASSERT_TRUE(handle != nullptr) << dlerror();
304
305 typedef int (*fn_t) (void);
306 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
307 ASSERT_TRUE(fn != nullptr) << dlerror();
308 ASSERT_EQ(1, fn());
309}
310
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700311TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700312 // Here is how the test library and its dt_needed
313 // libraries are arranged
314 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700315 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700316 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700317 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700318 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700319 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700320 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700321 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700322 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700323 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700324 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700325 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700326 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700327 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700328 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700329 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700330 //
331 // load order should be (1, 2, 3, a, b, d)
332 //
333 // get_answer() is defined in (2, 3, a, b, c)
334 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700335 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700336 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700337 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
338 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700339 typedef int (*fn_t) (void);
340 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700341 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700342 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700343 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700344 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700345
346 ASSERT_EQ(42, fn());
347 ASSERT_EQ(43, fn2());
348 dlclose(handle);
349}
350
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700351TEST(dlfcn, dlopen_check_order_reloc_siblings) {
352 // This is how this one works:
353 // we lookup and call get_answer which is defined in '_2.so'
354 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
355 // the correct _impl() is implemented by '_a.so';
356 //
357 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
358 //
359 // Here is the picture:
360 //
361 // libtest_check_order_reloc_siblings.so
362 // |
363 // +-> ..._1.so <- empty
364 // | |
365 // | +-> ..._a.so <- exports correct answer_impl()
366 // | |
367 // | +-> ..._b.so <- every other letter exporting incorrect one.
368 // |
369 // +-> ..._2.so <- empty
370 // | |
371 // | +-> ..._c.so
372 // | |
373 // | +-> ..._d.so
374 // |
375 // +-> ..._3.so <- empty
376 // |
377 // +-> ..._e.so
378 // |
379 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
380 // implements incorrect get_answer_impl()
381
382 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
383 ASSERT_TRUE(handle == nullptr);
384#ifdef __BIONIC__
385 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
386 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
387#endif
388
389 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
390 ASSERT_TRUE(handle != nullptr) << dlerror();
391
392 typedef int (*fn_t) (void);
393 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
394 ASSERT_TRUE(fn != nullptr) << dlerror();
395 ASSERT_EQ(42, fn());
396
397 ASSERT_EQ(0, dlclose(handle));
398}
399
400TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
401 // This test uses the same library as dlopen_check_order_reloc_siblings.
402 // Unlike dlopen_check_order_reloc_siblings it preloads
403 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
404 // dlopen(libtest_check_order_reloc_siblings.so)
405
406 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
407 ASSERT_TRUE(handle == nullptr);
408 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
409 ASSERT_TRUE(handle == nullptr);
410
411 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
412 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
413
414 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
415 ASSERT_TRUE(handle != nullptr) << dlerror();
416
417 ASSERT_EQ(0, dlclose(handle_for_1));
418
419 typedef int (*fn_t) (void);
420 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
421 ASSERT_TRUE(fn != nullptr) << dlerror();
422 ASSERT_EQ(42, fn());
423
424 ASSERT_EQ(0, dlclose(handle));
425}
426
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800427TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
428 // This is how this one works:
429 // we lookup and call grandchild_get_answer which is defined in '_2.so'
430 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
431 // the correct _impl() is implemented by '_c_1.so';
432 //
433 // Here is the picture of subtree:
434 //
435 // libtest_check_order_reloc_siblings.so
436 // |
437 // +-> ..._2.so <- grandchild_get_answer()
438 // |
439 // +-> ..._c.so <- empty
440 // | |
441 // | +-> _c_1.so <- exports correct answer_impl()
442 // | |
443 // | +-> _c_2.so <- exports incorrect answer_impl()
444 // |
445 // +-> ..._d.so <- empty
446
447 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
448 ASSERT_TRUE(handle == nullptr);
449#ifdef __BIONIC__
450 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
451 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
452#endif
453
454 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
455 ASSERT_TRUE(handle != nullptr) << dlerror();
456
457 typedef int (*fn_t) (void);
458 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
459 ASSERT_TRUE(fn != nullptr) << dlerror();
460 ASSERT_EQ(42, fn());
461
462 ASSERT_EQ(0, dlclose(handle));
463}
464
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700465TEST(dlfcn, dlopen_check_order_reloc_nephew) {
466 // This is how this one works:
467 // we lookup and call nephew_get_answer which is defined in '_2.so'
468 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
469 // the correct _impl() is implemented by '_a.so';
470 //
471 // Here is the picture:
472 //
473 // libtest_check_order_reloc_siblings.so
474 // |
475 // +-> ..._1.so <- empty
476 // | |
477 // | +-> ..._a.so <- exports correct answer_impl()
478 // | |
479 // | +-> ..._b.so <- every other letter exporting incorrect one.
480 // |
481 // +-> ..._2.so <- empty
482 // | |
483 // | +-> ..._c.so
484 // | |
485 // | +-> ..._d.so
486 // |
487 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
488 // |
489 // +-> ..._e.so
490 // |
491 // +-> ..._f.so
492
493 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
494 ASSERT_TRUE(handle == nullptr);
495#ifdef __BIONIC__
496 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
497 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
498#endif
499
500 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
501 ASSERT_TRUE(handle != nullptr) << dlerror();
502
503 typedef int (*fn_t) (void);
504 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
505 ASSERT_TRUE(fn != nullptr) << dlerror();
506 ASSERT_EQ(42, fn());
507
508 ASSERT_EQ(0, dlclose(handle));
509}
510
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800511TEST(dlfcn, check_unload_after_reloc) {
512 // This is how this one works:
513 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
514 // |
515 // +-> libtest_two_parents_child
516 //
517 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
518 // |
519 // +-> libtest_two_parents_child
520 //
521 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
522 // as a second step it dlopens parent2 and dlcloses parent1...
523
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700524 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
525 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800526
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700527 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
528 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800529
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700530 typedef int (*fn_t) (void);
531 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
532 ASSERT_TRUE(fn != nullptr) << dlerror();
533 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800534
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700535 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800536
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700537 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
538 ASSERT_TRUE(handle != nullptr);
539 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800540
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700541 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
542 ASSERT_TRUE(fn != nullptr) << dlerror();
543 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800544
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700545 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800546
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700547 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
548 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800549}
550
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700551extern "C" int check_order_reloc_root_get_answer_impl() {
552 return 42;
553}
554
555TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
556 // This is how this one works:
557 // we lookup and call get_answer3 which is defined in 'root.so'
558 // and in turn calls external root_get_answer_impl() defined in _2.so and
559 // above the correct _impl() is one in the executable.
560 //
561 // libtest_check_order_reloc_root.so
562 // |
563 // +-> ..._1.so <- empty
564 // |
565 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
566 //
567
568 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
569 ASSERT_TRUE(handle == nullptr);
570#ifdef __BIONIC__
571 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
572 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
573#endif
574
575 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
576 ASSERT_TRUE(handle != nullptr) << dlerror();
577
578 typedef int (*fn_t) (void);
579 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
580 ASSERT_TRUE(fn != nullptr) << dlerror();
581 ASSERT_EQ(42, fn());
582
583 ASSERT_EQ(0, dlclose(handle));
584}
585
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700586TEST(dlfcn, dlopen_check_rtld_local) {
587 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
588 ASSERT_TRUE(sym == nullptr);
589
590 // implicit RTLD_LOCAL
591 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
592 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
593 ASSERT_TRUE(sym == nullptr);
594 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
595 sym = dlsym(handle, "dlopen_testlib_simple_func");
596 ASSERT_TRUE(sym != nullptr);
597 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
598 dlclose(handle);
599
600 // explicit RTLD_LOCAL
601 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
602 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
603 ASSERT_TRUE(sym == nullptr);
604 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
605 sym = dlsym(handle, "dlopen_testlib_simple_func");
606 ASSERT_TRUE(sym != nullptr);
607 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
608 dlclose(handle);
609}
610
611TEST(dlfcn, dlopen_check_rtld_global) {
612 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
613 ASSERT_TRUE(sym == nullptr);
614
615 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700616 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700617 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
618 ASSERT_TRUE(sym != nullptr) << dlerror();
619 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
620 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700621
622 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
623 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
624 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700625
626 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
627 // shared libraries after symbol was not found in the main executable
628 // and dependent libraries.
629 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
630 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
631 ASSERT_TRUE(sym != nullptr) << dlerror();
632
633 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700634}
635
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700636// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
637// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
638// libtest_with_dependency_loop_a.so
639TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700640 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
641 ASSERT_TRUE(handle != nullptr) << dlerror();
642 void* f = dlsym(handle, "dlopen_test_loopy_function");
643 ASSERT_TRUE(f != nullptr) << dlerror();
644 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
645 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700646
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700647 // dlopen second time to make sure that the library was unloaded correctly
648 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
649 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800650#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700651 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 -0800652#else
653 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
654 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700655#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800656
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700657 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
658 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700659}
660
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700661TEST(dlfcn, dlopen_nodelete) {
662 static bool is_unloaded = false;
663
664 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
665 ASSERT_TRUE(handle != nullptr) << dlerror();
666 void (*set_unload_flag_ptr)(bool*);
667 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
668 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
669 set_unload_flag_ptr(&is_unloaded);
670
671 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
672 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
673 ASSERT_EQ(1729U, *taxicab_number);
674 *taxicab_number = 2;
675
676 dlclose(handle);
677 ASSERT_TRUE(!is_unloaded);
678
679 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
680 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
681 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
682
683
684 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
685 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
686 ASSERT_EQ(taxicab_number2, taxicab_number);
687
688 ASSERT_EQ(2U, *taxicab_number2);
689
690 dlclose(handle);
691 ASSERT_TRUE(!is_unloaded);
692}
693
694TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
695 static bool is_unloaded = false;
696
697 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
698 ASSERT_TRUE(handle != nullptr) << dlerror();
699 void (*set_unload_flag_ptr)(bool*);
700 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
701 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
702 set_unload_flag_ptr(&is_unloaded);
703
704 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
705 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
706
707 ASSERT_EQ(1729U, *taxicab_number);
708 *taxicab_number = 2;
709
710 // This RTLD_NODELETE should be ignored
711 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
712 ASSERT_TRUE(handle1 != nullptr) << dlerror();
713 ASSERT_EQ(handle, handle1);
714
715 dlclose(handle1);
716 dlclose(handle);
717
718 ASSERT_TRUE(is_unloaded);
719}
720
721TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
722 static bool is_unloaded = false;
723
724 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
725 ASSERT_TRUE(handle != nullptr) << dlerror();
726 void (*set_unload_flag_ptr)(bool*);
727 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
728 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
729 set_unload_flag_ptr(&is_unloaded);
730
731 dlclose(handle);
732 ASSERT_TRUE(!is_unloaded);
733}
734
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700735TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700736 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
737 ASSERT_TRUE(handle != nullptr) << dlerror();
738 int (*get_answer)();
739 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
740 ASSERT_TRUE(get_answer != nullptr) << dlerror();
741 ASSERT_EQ(42, get_answer());
742 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700743}
744
Elliott Hughese66190d2012-12-18 15:57:55 -0800745TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700746 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700747 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700748#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700749 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
750#else
751 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
752#endif
753}
754
Elliott Hughesad88a082012-10-24 18:37:21 -0700755static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700756 dlopen("/child/thread", RTLD_NOW);
757 return reinterpret_cast<void*>(strdup(dlerror()));
758}
759
Elliott Hughese66190d2012-12-18 15:57:55 -0800760TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700761 dlopen("/main/thread", RTLD_NOW);
762 const char* main_thread_error = dlerror();
763 ASSERT_SUBSTR("/main/thread", main_thread_error);
764
765 pthread_t t;
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700766 ASSERT_EQ(0, pthread_create(&t, nullptr, ConcurrentDlErrorFn, nullptr));
Elliott Hughes5419b942012-10-16 15:54:46 -0700767 void* result;
768 ASSERT_EQ(0, pthread_join(t, &result));
769 char* child_thread_error = static_cast<char*>(result);
770 ASSERT_SUBSTR("/child/thread", child_thread_error);
771 free(child_thread_error);
772
773 ASSERT_SUBSTR("/main/thread", main_thread_error);
774}
775
Elliott Hughese66190d2012-12-18 15:57:55 -0800776TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700777 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700778 void* self = dlopen(nullptr, RTLD_NOW);
779 ASSERT_TRUE(self != nullptr);
780 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700781
782 void* sym;
783
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700784#if defined(__BIONIC__) && !defined(__LP64__)
785 // RTLD_DEFAULT in lp32 bionic is not (void*)0
786 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700787 sym = dlsym(nullptr, "test");
788 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800789 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700790#endif
791
Elliott Hughes3b297c42012-10-11 16:08:51 -0700792 // Symbol that doesn't exist.
793 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700794 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700795 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700796
797 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700798}
799
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700800TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700801 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700802 void* self = dlopen(nullptr, RTLD_NOW);
803 ASSERT_TRUE(self != nullptr);
804 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700805
806 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700807 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700808
809 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
810 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
811
812 Dl_info info;
813 int rc = dladdr(addr, &info);
814 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
815
816 // Get the name of this executable.
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700817 const std::string& executable_path = get_executable_path();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700818
819 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700820 char dli_realpath[PATH_MAX];
821 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700822 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700823
824 // The symbol name should be the symbol we looked up.
825 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
826
827 // The address should be the exact address of the symbol.
828 ASSERT_EQ(info.dli_saddr, sym);
829
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700830 std::vector<map_record> maps;
831 ASSERT_TRUE(Maps::parse_maps(&maps));
832
833 void* base_address = nullptr;
834 for (const map_record& rec : maps) {
835 if (executable_path == rec.pathname) {
836 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700837 break;
838 }
839 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700840
841 // The base address should be the address we were loaded at.
842 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700843
844 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700845}
846
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700847TEST(dlfcn, dlopen_executable_by_absolute_path) {
848 void* handle1 = dlopen(nullptr, RTLD_NOW);
849 ASSERT_TRUE(handle1 != nullptr) << dlerror();
850
851 void* handle2 = dlopen(get_executable_path().c_str(), RTLD_NOW);
852 ASSERT_TRUE(handle2 != nullptr) << dlerror();
853
854#if defined(__BIONIC__)
855 ASSERT_EQ(handle1, handle2);
856#else
857 GTEST_LOG_(INFO) << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
858 "it loads a separate copy of the main executable "
859 "on dlopen by absolute path.";
860#endif
861}
862
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700863#if defined(__LP64__)
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200864#define PATH_TO_SYSTEM_LIB "/system/lib64/"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700865#else
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200866#define PATH_TO_SYSTEM_LIB "/system/lib/"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700867#endif
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200868#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700869
870TEST(dlfcn, dladdr_libc) {
871#if defined(__BIONIC__)
872 Dl_info info;
873 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
874 ASSERT_TRUE(dladdr(addr, &info) != 0);
875
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700876 // /system/lib is symlink when this test is executed on host.
877 char libc_realpath[PATH_MAX];
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200878 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700879
880 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700881 // TODO: add check for dfi_fbase
882 ASSERT_STREQ("puts", info.dli_sname);
883 ASSERT_EQ(addr, info.dli_saddr);
884#else
885 GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig "
886 "for libc.so, which is symlink itself (not a realpath).\n";
887#endif
888}
889
Elliott Hughese66190d2012-12-18 15:57:55 -0800890TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700891 Dl_info info;
892
Elliott Hughes3b297c42012-10-11 16:08:51 -0700893 dlerror(); // Clear any pending errors.
894
Elliott Hughes8e15b082012-09-26 11:44:01 -0700895 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700896 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
897 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700898
899 // No symbol corresponding to a stack address.
900 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700901 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700902}
Elliott Hughes124fae92012-10-31 14:20:03 -0700903
Elliott Hughesa43e9062013-01-07 14:18:22 -0800904// GNU-style ELF hash tables are incompatible with the MIPS ABI.
905// 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 -0800906TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800907#if !defined(__mips__)
Elliott Hughes124fae92012-10-31 14:20:03 -0700908 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800909 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
910 ASSERT_TRUE(handle != nullptr) << dlerror();
911 auto guard = make_scope_guard([&]() {
912 dlclose(handle);
913 });
914 void* sym = dlsym(handle, "getRandomNumber");
915 ASSERT_TRUE(sym != nullptr) << dlerror();
916 int (*fn)(void);
917 fn = reinterpret_cast<int (*)(void)>(sym);
918 EXPECT_EQ(4, fn());
919
920 Dl_info dlinfo;
921 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
922
923 ASSERT_TRUE(fn == dlinfo.dli_saddr);
924 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800925 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800926#else
927 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
928#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700929}
Elliott Hughese66190d2012-12-18 15:57:55 -0800930
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800931TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
932 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
933 ASSERT_TRUE(handle != nullptr) << dlerror();
934 auto guard = make_scope_guard([&]() {
935 dlclose(handle);
936 });
937 void* sym = dlsym(handle, "getRandomNumber");
938 ASSERT_TRUE(sym != nullptr) << dlerror();
939 int (*fn)(void);
940 fn = reinterpret_cast<int (*)(void)>(sym);
941 EXPECT_EQ(4, fn());
942
943 Dl_info dlinfo;
944 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
945
946 ASSERT_TRUE(fn == dlinfo.dli_saddr);
947 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
948 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
949}
950
Elliott Hughese66190d2012-12-18 15:57:55 -0800951TEST(dlfcn, dlopen_bad_flags) {
952 dlerror(); // Clear any pending errors.
953 void* handle;
954
Elliott Hughes063525c2014-05-13 11:19:57 -0700955#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800956 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700957 handle = dlopen(nullptr, 0);
958 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -0800959 ASSERT_SUBSTR("invalid", dlerror());
960#endif
961
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700962 handle = dlopen(nullptr, 0xffffffff);
963 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -0800964 ASSERT_SUBSTR("invalid", dlerror());
965
966 // 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 -0700967 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
968 ASSERT_TRUE(handle != nullptr);
969 ASSERT_SUBSTR(nullptr, dlerror());
Elliott Hughese66190d2012-12-18 15:57:55 -0800970}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400971
972TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800973 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700974 ASSERT_TRUE(addr == nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400975}
976
977TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800978 void* addr = dlsym(RTLD_DEFAULT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700979 ASSERT_TRUE(addr != nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -0800980}
981
982TEST(dlfcn, rtld_next_unknown_symbol) {
983 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700984 ASSERT_TRUE(addr == nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -0800985}
986
987TEST(dlfcn, rtld_next_known_symbol) {
988 void* addr = dlsym(RTLD_NEXT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700989 ASSERT_TRUE(addr != nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400990}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700991
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700992TEST(dlfcn, dlsym_weak_func) {
993 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800994 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700995 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700996
997 int (*weak_func)();
998 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700999 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001000 EXPECT_EQ(42, weak_func());
1001 dlclose(handle);
1002}
1003
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001004TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001005 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
1006 ASSERT_TRUE(handle != nullptr) << dlerror();
1007 int (*weak_func)();
1008 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
1009 ASSERT_TRUE(weak_func != nullptr) << dlerror();
1010 EXPECT_EQ(6551, weak_func());
1011 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001012}
1013
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001014TEST(dlfcn, dlopen_symlink) {
1015 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
1016 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001017 ASSERT_TRUE(handle1 != nullptr);
1018 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001019 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -07001020 dlclose(handle1);
1021 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001022}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001023
1024// libtest_dlopen_from_ctor_main.so depends on
1025// libtest_dlopen_from_ctor.so which has a constructor
1026// that calls dlopen(libc...). This is to test the situation
1027// described in b/7941716.
1028TEST(dlfcn, dlopen_dlopen_from_ctor) {
1029#if defined(__BIONIC__)
1030 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1031 ASSERT_TRUE(handle != nullptr) << dlerror();
1032 dlclose(handle);
1033#else
1034 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
1035#endif
1036}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001037
1038TEST(dlfcn, symbol_versioning_use_v1) {
1039 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1040 ASSERT_TRUE(handle != nullptr) << dlerror();
1041 typedef int (*fn_t)();
1042 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1043 ASSERT_TRUE(fn != nullptr) << dlerror();
1044 ASSERT_EQ(1, fn());
1045 dlclose(handle);
1046}
1047
1048TEST(dlfcn, symbol_versioning_use_v2) {
1049 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1050 ASSERT_TRUE(handle != nullptr) << dlerror();
1051 typedef int (*fn_t)();
1052 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1053 ASSERT_TRUE(fn != nullptr) << dlerror();
1054 ASSERT_EQ(2, fn());
1055 dlclose(handle);
1056}
1057
1058TEST(dlfcn, symbol_versioning_use_other_v2) {
1059 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1060 ASSERT_TRUE(handle != nullptr) << dlerror();
1061 typedef int (*fn_t)();
1062 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1063 ASSERT_TRUE(fn != nullptr) << dlerror();
1064 ASSERT_EQ(20, fn());
1065 dlclose(handle);
1066}
1067
1068TEST(dlfcn, symbol_versioning_use_other_v3) {
1069 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1070 ASSERT_TRUE(handle != nullptr) << dlerror();
1071 typedef int (*fn_t)();
1072 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1073 ASSERT_TRUE(fn != nullptr) << dlerror();
1074 ASSERT_EQ(3, fn());
1075 dlclose(handle);
1076}
1077
1078TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1079 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1080 ASSERT_TRUE(handle != nullptr) << dlerror();
1081 typedef int (*fn_t)();
1082 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1083 ASSERT_TRUE(fn != nullptr) << dlerror();
1084 ASSERT_EQ(3, fn()); // the default version is 3
1085 dlclose(handle);
1086}
1087
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001088TEST(dlfcn, dlvsym_smoke) {
1089 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1090 ASSERT_TRUE(handle != nullptr) << dlerror();
1091 typedef int (*fn_t)();
1092
1093 {
1094 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1095 ASSERT_TRUE(fn == nullptr);
1096 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1097 }
1098
1099 {
1100 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1101 ASSERT_TRUE(fn != nullptr) << dlerror();
1102 ASSERT_EQ(2, fn());
1103 }
1104
1105 dlclose(handle);
1106}
1107
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001108// This preempts the implementation from libtest_versioned_lib.so
1109extern "C" int version_zero_function() {
1110 return 0;
1111}
1112
1113// This preempts the implementation from libtest_versioned_uselibv*.so
1114extern "C" int version_zero_function2() {
1115 return 0;
1116}
Evgenii Stepanov68650822015-06-10 13:38:39 -07001117
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001118TEST(dlfcn, dt_runpath_smoke) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001119 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1120 ASSERT_TRUE(handle != nullptr) << dlerror();
1121
1122 typedef void *(* dlopen_b_fn)();
1123 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1124 ASSERT_TRUE(fn != nullptr) << dlerror();
1125
1126 void *p = fn();
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001127 ASSERT_TRUE(p != nullptr);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001128
1129 dlclose(handle);
1130}
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001131
1132TEST(dlfcn, dt_runpath_absolute_path) {
1133 void* handle = dlopen(PATH_TO_SYSTEM_LIB "libtest_dt_runpath_d.so", RTLD_NOW);
1134 ASSERT_TRUE(handle != nullptr) << dlerror();
1135
1136 typedef void *(* dlopen_b_fn)();
1137 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1138 ASSERT_TRUE(fn != nullptr) << dlerror();
1139
1140 void *p = fn();
1141 ASSERT_TRUE(p != nullptr);
1142
1143 dlclose(handle);
1144}
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001145
1146// Bionic specific tests
1147#if defined(__BIONIC__)
1148
1149#if defined(__LP64__)
1150#define NATIVE_TESTS_PATH "/nativetest64"
1151#else
1152#define NATIVE_TESTS_PATH "/nativetest"
1153#endif
1154
1155#define PREBUILT_ELF_PATH NATIVE_TESTS_PATH "/prebuilt-elf-files"
1156
1157TEST(dlfcn, dlopen_invalid_rw_load_segment) {
1158 std::string libpath = std::string(getenv("ANDROID_DATA")) + PREBUILT_ELF_PATH + "/libtest_invalid-rw_load_segment.so";
1159 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1160 ASSERT_TRUE(handle == nullptr);
1161 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W + E load segments are not allowed";
1162 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1163}
1164#endif