blob: 000d1f7e29a96a139509f2f089b93759ad8635fb [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>
dimitryb9555a92017-10-11 18:04:05 +020024#if __has_include(<sys/auxv.h>)
25#include <sys/auxv.h>
26#endif
dimitry109040c2017-11-03 13:49:07 +010027#include <sys/user.h>
Elliott Hughes8e15b082012-09-26 11:44:01 -070028
29#include <string>
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -080030#include <thread>
jeffhaoacf5aa72012-09-12 17:25:30 -070031
Tom Cherryb8ab6182017-04-05 16:20:29 -070032#include <android-base/scopeguard.h>
33
Dimitry Ivanov927877c2016-09-21 11:17:13 -070034#include "gtest_globals.h"
Dimitry Ivanov708589f2016-09-19 10:50:28 -070035#include "dlfcn_symlink_support.h"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070036#include "utils.h"
37
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -080038#if defined(__BIONIC__) && (defined(__arm__) || defined(__i386__))
39#pragma clang diagnostic push
40#pragma clang diagnostic ignored "-Wunused-parameter"
41
42#include <llvm/ADT/StringRef.h>
43#include <llvm/Object/Binary.h>
44#include <llvm/Object/ELFObjectFile.h>
45#include <llvm/Object/ObjectFile.h>
46
47#pragma clang diagnostic pop
48#endif // defined(__ANDROID__) && (defined(__arm__) || defined(__i386__))
49
Elliott Hughes3b297c42012-10-11 16:08:51 -070050#define ASSERT_SUBSTR(needle, haystack) \
51 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
52
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070053
Elliott Hughes1728b232014-05-14 10:02:03 -070054static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070055extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070056 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070057}
58
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070059static int g_ctor_function_called = 0;
Dimitry Ivanov55437462016-07-20 15:33:07 -070060static int g_ctor_argc = 0;
61static char** g_ctor_argv = reinterpret_cast<char**>(0xDEADBEEF);
62static char** g_ctor_envp = g_ctor_envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070063
Dimitry Ivanov55437462016-07-20 15:33:07 -070064extern "C" void ctor_function(int argc, char** argv, char** envp) __attribute__ ((constructor));
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070065
Dimitry Ivanov55437462016-07-20 15:33:07 -070066extern "C" void ctor_function(int argc, char** argv, char** envp) {
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070067 g_ctor_function_called = 17;
Dimitry Ivanov55437462016-07-20 15:33:07 -070068 g_ctor_argc = argc;
69 g_ctor_argv = argv;
70 g_ctor_envp = envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070071}
72
73TEST(dlfcn, ctor_function_call) {
74 ASSERT_EQ(17, g_ctor_function_called);
Dimitry Ivanov55437462016-07-20 15:33:07 -070075 ASSERT_TRUE(g_ctor_argc = get_argc());
76 ASSERT_TRUE(g_ctor_argv = get_argv());
77 ASSERT_TRUE(g_ctor_envp = get_envp());
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070078}
79
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070080TEST(dlfcn, dlsym_in_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070081 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070082 void* self = dlopen(nullptr, RTLD_NOW);
83 ASSERT_TRUE(self != nullptr);
84 ASSERT_TRUE(dlerror() == nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070085
86 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070087 ASSERT_TRUE(sym != nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070088
89 void (*function)() = reinterpret_cast<void(*)()>(sym);
90
Elliott Hughes1728b232014-05-14 10:02:03 -070091 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070092 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070093 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070094
95 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070096}
Elliott Hughes8e15b082012-09-26 11:44:01 -070097
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070098TEST(dlfcn, dlsym_from_sofile) {
99 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL);
100 ASSERT_TRUE(handle != nullptr) << dlerror();
101
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700102 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700103 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
104 ASSERT_TRUE(symbol == nullptr);
105 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
106
107 typedef int* (*fn_t)();
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700108 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
109 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
110 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700111
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700112 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700113 ASSERT_TRUE(ptr != nullptr) << dlerror();
114 ASSERT_EQ(42, *ptr);
115
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700116 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
117 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
118 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
119
120 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
121 ASSERT_TRUE(ptr != nullptr) << dlerror();
122 ASSERT_EQ(44, *ptr);
123
124 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
125 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
126 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
127
128 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
129 ASSERT_TRUE(ptr != nullptr) << dlerror();
130 ASSERT_EQ(43, *ptr);
131
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700132 dlclose(handle);
133}
134
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700135TEST(dlfcn, dlsym_from_sofile_with_preload) {
136 void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL);
137 ASSERT_TRUE(preload != nullptr) << dlerror();
138
139 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
140 ASSERT_TRUE(handle != nullptr) << dlerror();
141
142 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
143 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
144 ASSERT_TRUE(symbol == nullptr);
145 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
146
147 typedef int* (*fn_t)();
148 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
149 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
150 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
151
152 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
153 ASSERT_TRUE(ptr != nullptr) << dlerror();
154 ASSERT_EQ(42, *ptr);
155
156 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
157 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
158 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
159
160 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
161 ASSERT_TRUE(ptr != nullptr) << dlerror();
162 ASSERT_EQ(44, *ptr);
163
164 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
165 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
166 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
167
168 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
169 ASSERT_TRUE(ptr != nullptr) << dlerror();
170 ASSERT_EQ(43, *ptr);
171
172 dlclose(handle);
173 dlclose(preload);
174}
175
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700176TEST(dlfcn, dlsym_handle_global_sym) {
177 // check that we do not look into global group
178 // when looking up symbol by handle
179 void* handle = dlopen("libtest_empty.so", RTLD_NOW);
180 dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL);
181 void* sym = dlsym(handle, "getRandomNumber");
182 ASSERT_TRUE(sym == nullptr);
183 ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror());
184
185 sym = dlsym(handle, "DlSymTestFunction");
186 ASSERT_TRUE(sym == nullptr);
187 ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror());
188 dlclose(handle);
189}
190
Dimitry Ivanovd5b578a2016-12-14 15:16:56 -0800191TEST(dlfcn, dlsym_handle_empty_symbol) {
192 // check that dlsym of an empty symbol fails (see http://b/33530622)
193 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW);
194 ASSERT_TRUE(handle != nullptr) << dlerror();
195 void* sym = dlsym(handle, "");
196 ASSERT_TRUE(sym == nullptr);
197 ASSERT_SUBSTR("undefined symbol: ", dlerror());
198 dlclose(handle);
199}
200
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700201TEST(dlfcn, dlsym_with_dependencies) {
202 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700203 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700204 dlerror();
205 // This symbol is in DT_NEEDED library.
206 void* sym = dlsym(handle, "getRandomNumber");
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700207 ASSERT_TRUE(sym != nullptr) << dlerror();
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700208 int (*fn)(void);
209 fn = reinterpret_cast<int (*)(void)>(sym);
210 EXPECT_EQ(4, fn());
211 dlclose(handle);
212}
213
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700214TEST(dlfcn, dlopen_noload) {
215 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700216 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700217 handle = dlopen("libtest_simple.so", RTLD_NOW);
218 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700219 ASSERT_TRUE(handle != nullptr);
220 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700221 ASSERT_TRUE(handle == handle2);
222 ASSERT_EQ(0, dlclose(handle));
223 ASSERT_EQ(0, dlclose(handle2));
224}
225
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700226TEST(dlfcn, dlopen_by_soname) {
227 static const char* soname = "libdlext_test_soname.so";
228 static const char* filename = "libdlext_test_different_soname.so";
229 // 1. Make sure there is no library with soname in default search path
230 void* handle = dlopen(soname, RTLD_NOW);
231 ASSERT_TRUE(handle == nullptr);
232
233 // 2. Load a library using filename
234 handle = dlopen(filename, RTLD_NOW);
235 ASSERT_TRUE(handle != nullptr) << dlerror();
236
237 // 3. Find library by soname
238 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
239 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
240 ASSERT_EQ(handle, handle_soname);
241
242 // 4. RTLD_NOLOAD should still work with filename
243 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
244 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
245 ASSERT_EQ(handle, handle_filename);
246
247 dlclose(handle_filename);
248 dlclose(handle_soname);
249 dlclose(handle);
250}
251
dimitryc18de1b2017-09-26 14:31:35 +0200252TEST(dlfcn, dlopen_vdso) {
dimitryb9555a92017-10-11 18:04:05 +0200253#if __has_include(<sys/auxv.h>)
254 if (getauxval(AT_SYSINFO_EHDR) == 0) {
255 GTEST_LOG_(INFO) << "getauxval(AT_SYSINFO_EHDR) == 0, skipping this test.";
256 return;
257 }
258#endif
dimitryc18de1b2017-09-26 14:31:35 +0200259 void* handle = dlopen("linux-vdso.so.1", RTLD_NOW);
260 ASSERT_TRUE(handle != nullptr) << dlerror();
261 dlclose(handle);
262}
263
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700264// mips doesn't support ifuncs
265#if !defined(__mips__)
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700266TEST(dlfcn, ifunc_variable) {
267 typedef const char* (*fn_ptr)();
268
269 // ifunc's choice depends on whether IFUNC_CHOICE has a value
270 // first check the set case
271 setenv("IFUNC_CHOICE", "set", 1);
272 // preload libtest_ifunc_variable_impl.so
273 void* handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
274 void* handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
275 ASSERT_TRUE(handle != nullptr) << dlerror();
276 const char** foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
277 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
278 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
279 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
280 ASSERT_EQ(strncmp("set", *foo_ptr, 3), 0);
281 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
282 dlclose(handle);
283 dlclose(handle_impl);
284
285 // then check the unset case
286 unsetenv("IFUNC_CHOICE");
287 handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
288 handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
289 ASSERT_TRUE(handle != nullptr) << dlerror();
290 foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
291 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
292 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
293 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
294 ASSERT_EQ(strncmp("unset", *foo_ptr, 5), 0);
295 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
296 dlclose(handle);
297 dlclose(handle_impl);
298}
299
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700300TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700301 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700302
303 // ifunc's choice depends on whether IFUNC_CHOICE has a value
304 // first check the set case
305 setenv("IFUNC_CHOICE", "set", 1);
306 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700307 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700308 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
309 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700310 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
311 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700312 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
313 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700314 dlclose(handle);
315
316 // then check the unset case
317 unsetenv("IFUNC_CHOICE");
318 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700319 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700320 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
321 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700322 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
323 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700324 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700325 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700326 dlclose(handle);
327}
328
329TEST(dlfcn, ifunc_ctor_call) {
330 typedef const char* (*fn_ptr)();
331
332 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700333 ASSERT_TRUE(handle != nullptr) << dlerror();
334 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
335 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
336 ASSERT_STREQ("false", is_ctor_called());
337
338 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
339 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700340 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700341 dlclose(handle);
342}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700343
344TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
345 typedef const char* (*fn_ptr)();
346
347 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
348 ASSERT_TRUE(handle != nullptr) << dlerror();
349 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
350 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
351 ASSERT_STREQ("false", is_ctor_called());
352
353 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
354 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
355 ASSERT_STREQ("true", is_ctor_called());
356 dlclose(handle);
357}
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700358#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700359
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700360TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
361 // This is the structure of the test library and
362 // its dt_needed libraries
363 // libtest_relo_check_dt_needed_order.so
364 // |
365 // +-> libtest_relo_check_dt_needed_order_1.so
366 // |
367 // +-> libtest_relo_check_dt_needed_order_2.so
368 //
369 // The root library references relo_test_get_answer_lib - which is defined
370 // in both dt_needed libraries, the correct relocation should
371 // use the function defined in libtest_relo_check_dt_needed_order_1.so
372 void* handle = nullptr;
Tom Cherryb8ab6182017-04-05 16:20:29 -0700373 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700374
375 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
376 ASSERT_TRUE(handle != nullptr) << dlerror();
377
378 typedef int (*fn_t) (void);
379 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
380 ASSERT_TRUE(fn != nullptr) << dlerror();
381 ASSERT_EQ(1, fn());
382}
383
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700384TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700385 // Here is how the test library and its dt_needed
386 // libraries are arranged
387 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700388 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700389 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700390 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700391 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700392 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700393 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700394 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700395 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700396 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700397 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700398 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700399 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700400 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700401 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700402 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700403 //
404 // load order should be (1, 2, 3, a, b, d)
405 //
406 // get_answer() is defined in (2, 3, a, b, c)
407 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700408 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700409 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700410 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
411 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700412 typedef int (*fn_t) (void);
413 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700414 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700415 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700416 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700417 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700418
419 ASSERT_EQ(42, fn());
420 ASSERT_EQ(43, fn2());
421 dlclose(handle);
422}
423
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700424TEST(dlfcn, dlopen_check_order_reloc_siblings) {
425 // This is how this one works:
426 // we lookup and call get_answer which is defined in '_2.so'
427 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
428 // the correct _impl() is implemented by '_a.so';
429 //
430 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
431 //
432 // Here is the picture:
433 //
434 // libtest_check_order_reloc_siblings.so
435 // |
436 // +-> ..._1.so <- empty
437 // | |
438 // | +-> ..._a.so <- exports correct answer_impl()
439 // | |
440 // | +-> ..._b.so <- every other letter exporting incorrect one.
441 // |
442 // +-> ..._2.so <- empty
443 // | |
444 // | +-> ..._c.so
445 // | |
446 // | +-> ..._d.so
447 // |
448 // +-> ..._3.so <- empty
449 // |
450 // +-> ..._e.so
451 // |
452 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
453 // implements incorrect get_answer_impl()
454
455 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
456 ASSERT_TRUE(handle == nullptr);
457#ifdef __BIONIC__
458 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
459 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
460#endif
461
462 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
463 ASSERT_TRUE(handle != nullptr) << dlerror();
464
465 typedef int (*fn_t) (void);
466 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
467 ASSERT_TRUE(fn != nullptr) << dlerror();
468 ASSERT_EQ(42, fn());
469
470 ASSERT_EQ(0, dlclose(handle));
471}
472
473TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
474 // This test uses the same library as dlopen_check_order_reloc_siblings.
475 // Unlike dlopen_check_order_reloc_siblings it preloads
476 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
477 // dlopen(libtest_check_order_reloc_siblings.so)
478
479 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
480 ASSERT_TRUE(handle == nullptr);
481 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
482 ASSERT_TRUE(handle == nullptr);
483
484 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
485 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
486
487 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
488 ASSERT_TRUE(handle != nullptr) << dlerror();
489
490 ASSERT_EQ(0, dlclose(handle_for_1));
491
492 typedef int (*fn_t) (void);
493 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
494 ASSERT_TRUE(fn != nullptr) << dlerror();
495 ASSERT_EQ(42, fn());
496
497 ASSERT_EQ(0, dlclose(handle));
498}
499
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800500TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
501 // This is how this one works:
502 // we lookup and call grandchild_get_answer which is defined in '_2.so'
503 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
504 // the correct _impl() is implemented by '_c_1.so';
505 //
506 // Here is the picture of subtree:
507 //
508 // libtest_check_order_reloc_siblings.so
509 // |
510 // +-> ..._2.so <- grandchild_get_answer()
511 // |
512 // +-> ..._c.so <- empty
513 // | |
514 // | +-> _c_1.so <- exports correct answer_impl()
515 // | |
516 // | +-> _c_2.so <- exports incorrect answer_impl()
517 // |
518 // +-> ..._d.so <- empty
519
520 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
521 ASSERT_TRUE(handle == nullptr);
522#ifdef __BIONIC__
523 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
524 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
525#endif
526
527 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
528 ASSERT_TRUE(handle != nullptr) << dlerror();
529
530 typedef int (*fn_t) (void);
531 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
532 ASSERT_TRUE(fn != nullptr) << dlerror();
533 ASSERT_EQ(42, fn());
534
535 ASSERT_EQ(0, dlclose(handle));
536}
537
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700538TEST(dlfcn, dlopen_check_order_reloc_nephew) {
539 // This is how this one works:
540 // we lookup and call nephew_get_answer which is defined in '_2.so'
541 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
542 // the correct _impl() is implemented by '_a.so';
543 //
544 // Here is the picture:
545 //
546 // libtest_check_order_reloc_siblings.so
547 // |
548 // +-> ..._1.so <- empty
549 // | |
550 // | +-> ..._a.so <- exports correct answer_impl()
551 // | |
552 // | +-> ..._b.so <- every other letter exporting incorrect one.
553 // |
554 // +-> ..._2.so <- empty
555 // | |
556 // | +-> ..._c.so
557 // | |
558 // | +-> ..._d.so
559 // |
560 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
561 // |
562 // +-> ..._e.so
563 // |
564 // +-> ..._f.so
565
566 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
567 ASSERT_TRUE(handle == nullptr);
568#ifdef __BIONIC__
569 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
570 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
571#endif
572
573 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
574 ASSERT_TRUE(handle != nullptr) << dlerror();
575
576 typedef int (*fn_t) (void);
577 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
578 ASSERT_TRUE(fn != nullptr) << dlerror();
579 ASSERT_EQ(42, fn());
580
581 ASSERT_EQ(0, dlclose(handle));
582}
583
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800584TEST(dlfcn, check_unload_after_reloc) {
585 // This is how this one works:
586 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
587 // |
588 // +-> libtest_two_parents_child
589 //
590 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
591 // |
592 // +-> libtest_two_parents_child
593 //
594 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
595 // as a second step it dlopens parent2 and dlcloses parent1...
596
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700597 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
598 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800599
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700600 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
601 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800602
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700603 typedef int (*fn_t) (void);
604 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
605 ASSERT_TRUE(fn != nullptr) << dlerror();
606 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800607
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700608 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800609
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700610 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
611 ASSERT_TRUE(handle != nullptr);
612 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800613
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700614 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
615 ASSERT_TRUE(fn != nullptr) << dlerror();
616 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800617
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700618 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800619
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700620 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
621 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800622}
623
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700624extern "C" int check_order_reloc_root_get_answer_impl() {
625 return 42;
626}
627
628TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
629 // This is how this one works:
630 // we lookup and call get_answer3 which is defined in 'root.so'
631 // and in turn calls external root_get_answer_impl() defined in _2.so and
632 // above the correct _impl() is one in the executable.
633 //
634 // libtest_check_order_reloc_root.so
635 // |
636 // +-> ..._1.so <- empty
637 // |
638 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
639 //
640
641 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
642 ASSERT_TRUE(handle == nullptr);
643#ifdef __BIONIC__
644 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
645 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
646#endif
647
648 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
649 ASSERT_TRUE(handle != nullptr) << dlerror();
650
651 typedef int (*fn_t) (void);
652 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
653 ASSERT_TRUE(fn != nullptr) << dlerror();
654 ASSERT_EQ(42, fn());
655
656 ASSERT_EQ(0, dlclose(handle));
657}
658
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700659TEST(dlfcn, dlopen_check_rtld_local) {
660 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
661 ASSERT_TRUE(sym == nullptr);
662
663 // implicit RTLD_LOCAL
664 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
665 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
666 ASSERT_TRUE(sym == nullptr);
667 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
668 sym = dlsym(handle, "dlopen_testlib_simple_func");
669 ASSERT_TRUE(sym != nullptr);
670 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
671 dlclose(handle);
672
673 // explicit RTLD_LOCAL
674 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
675 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
676 ASSERT_TRUE(sym == nullptr);
677 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
678 sym = dlsym(handle, "dlopen_testlib_simple_func");
679 ASSERT_TRUE(sym != nullptr);
680 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
681 dlclose(handle);
682}
683
684TEST(dlfcn, dlopen_check_rtld_global) {
685 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
686 ASSERT_TRUE(sym == nullptr);
687
688 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700689 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700690 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
691 ASSERT_TRUE(sym != nullptr) << dlerror();
692 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
693 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700694
695 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
696 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
697 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700698
699 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
700 // shared libraries after symbol was not found in the main executable
701 // and dependent libraries.
702 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
703 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
704 ASSERT_TRUE(sym != nullptr) << dlerror();
705
706 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700707}
708
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700709// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
710// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
711// libtest_with_dependency_loop_a.so
712TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700713 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
714 ASSERT_TRUE(handle != nullptr) << dlerror();
715 void* f = dlsym(handle, "dlopen_test_loopy_function");
716 ASSERT_TRUE(f != nullptr) << dlerror();
717 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
718 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700719
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700720 // dlopen second time to make sure that the library was unloaded correctly
721 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
722 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800723#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700724 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 -0800725#else
726 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
727 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700728#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800729
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700730 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
731 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700732}
733
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700734TEST(dlfcn, dlopen_nodelete) {
735 static bool is_unloaded = false;
736
737 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
738 ASSERT_TRUE(handle != nullptr) << dlerror();
739 void (*set_unload_flag_ptr)(bool*);
740 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
741 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
742 set_unload_flag_ptr(&is_unloaded);
743
744 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
745 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
746 ASSERT_EQ(1729U, *taxicab_number);
747 *taxicab_number = 2;
748
749 dlclose(handle);
750 ASSERT_TRUE(!is_unloaded);
751
752 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
753 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
754 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
755
756
757 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
758 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
759 ASSERT_EQ(taxicab_number2, taxicab_number);
760
761 ASSERT_EQ(2U, *taxicab_number2);
762
763 dlclose(handle);
764 ASSERT_TRUE(!is_unloaded);
765}
766
767TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
768 static bool is_unloaded = false;
769
770 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
771 ASSERT_TRUE(handle != nullptr) << dlerror();
772 void (*set_unload_flag_ptr)(bool*);
773 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
774 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
775 set_unload_flag_ptr(&is_unloaded);
776
777 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
778 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
779
780 ASSERT_EQ(1729U, *taxicab_number);
781 *taxicab_number = 2;
782
783 // This RTLD_NODELETE should be ignored
784 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
785 ASSERT_TRUE(handle1 != nullptr) << dlerror();
786 ASSERT_EQ(handle, handle1);
787
788 dlclose(handle1);
789 dlclose(handle);
790
791 ASSERT_TRUE(is_unloaded);
792}
793
794TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
795 static bool is_unloaded = false;
796
797 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
798 ASSERT_TRUE(handle != nullptr) << dlerror();
799 void (*set_unload_flag_ptr)(bool*);
800 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
801 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
802 set_unload_flag_ptr(&is_unloaded);
803
804 dlclose(handle);
805 ASSERT_TRUE(!is_unloaded);
806}
807
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700808TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700809 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
810 ASSERT_TRUE(handle != nullptr) << dlerror();
811 int (*get_answer)();
812 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
813 ASSERT_TRUE(get_answer != nullptr) << dlerror();
814 ASSERT_EQ(42, get_answer());
815 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700816}
817
Elliott Hughese66190d2012-12-18 15:57:55 -0800818TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700819 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700820 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700821#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700822 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
823#else
824 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
825#endif
826}
827
dimitry109040c2017-11-03 13:49:07 +0100828TEST(dlfcn, dlclose_unload) {
829 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
830 ASSERT_TRUE(handle != nullptr) << dlerror();
831 uint32_t* taxicab_number = static_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
832 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
833 EXPECT_EQ(1729U, *taxicab_number);
834 dlclose(handle);
835 // Making sure that the library has been unmapped as part of library unload
836 // process. Note that mprotect somewhat counter-intuitively returns ENOMEM in
837 // this case.
838 uintptr_t page_start = reinterpret_cast<uintptr_t>(taxicab_number) & ~(PAGE_SIZE - 1);
839 ASSERT_TRUE(mprotect(reinterpret_cast<void*>(page_start), PAGE_SIZE, PROT_NONE) != 0);
840 ASSERT_EQ(ENOMEM, errno) << strerror(errno);
841}
842
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800843static void ConcurrentDlErrorFn(std::string& error) {
844 ASSERT_TRUE(dlerror() == nullptr);
845
846 void* handle = dlopen("/child/thread", RTLD_NOW);
847 ASSERT_TRUE(handle == nullptr);
848
849 const char* err = dlerror();
850 ASSERT_TRUE(err != nullptr);
851
852 error = err;
853}
854
855TEST(dlfcn, dlerror_concurrent_buffer) {
856 void* handle = dlopen("/main/thread", RTLD_NOW);
857 ASSERT_TRUE(handle == nullptr);
858 const char* main_thread_error = dlerror();
859 ASSERT_TRUE(main_thread_error != nullptr);
860 ASSERT_SUBSTR("/main/thread", main_thread_error);
861
862 std::string child_thread_error;
863 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
864 t.join();
865 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
866
867 // Check that main thread local buffer was not modified.
868 ASSERT_SUBSTR("/main/thread", main_thread_error);
Elliott Hughes5419b942012-10-16 15:54:46 -0700869}
870
Elliott Hughese66190d2012-12-18 15:57:55 -0800871TEST(dlfcn, dlerror_concurrent) {
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800872 void* handle = dlopen("/main/thread", RTLD_NOW);
873 ASSERT_TRUE(handle == nullptr);
874
875 std::string child_thread_error;
876 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
877 t.join();
878 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
879
Elliott Hughes5419b942012-10-16 15:54:46 -0700880 const char* main_thread_error = dlerror();
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800881 ASSERT_TRUE(main_thread_error != nullptr);
Elliott Hughes5419b942012-10-16 15:54:46 -0700882 ASSERT_SUBSTR("/main/thread", main_thread_error);
883}
884
Elliott Hughese66190d2012-12-18 15:57:55 -0800885TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700886 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700887 void* self = dlopen(nullptr, RTLD_NOW);
888 ASSERT_TRUE(self != nullptr);
889 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700890
891 void* sym;
892
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700893#if defined(__BIONIC__) && !defined(__LP64__)
894 // RTLD_DEFAULT in lp32 bionic is not (void*)0
895 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700896 sym = dlsym(nullptr, "test");
897 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800898 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700899#endif
900
Elliott Hughes3b297c42012-10-11 16:08:51 -0700901 // Symbol that doesn't exist.
902 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700903 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700904 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700905
906 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700907}
908
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700909TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700910 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700911 void* self = dlopen(nullptr, RTLD_NOW);
912 ASSERT_TRUE(self != nullptr);
913 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700914
915 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700916 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700917
918 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
919 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
920
921 Dl_info info;
922 int rc = dladdr(addr, &info);
923 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
924
925 // Get the name of this executable.
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700926 const std::string& executable_path = get_executable_path();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700927
928 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700929 char dli_realpath[PATH_MAX];
930 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700931 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700932
933 // The symbol name should be the symbol we looked up.
934 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
935
936 // The address should be the exact address of the symbol.
937 ASSERT_EQ(info.dli_saddr, sym);
938
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700939 std::vector<map_record> maps;
940 ASSERT_TRUE(Maps::parse_maps(&maps));
941
942 void* base_address = nullptr;
943 for (const map_record& rec : maps) {
944 if (executable_path == rec.pathname) {
945 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700946 break;
947 }
948 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700949
950 // The base address should be the address we were loaded at.
951 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700952
953 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700954}
955
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700956TEST(dlfcn, dlopen_executable_by_absolute_path) {
957 void* handle1 = dlopen(nullptr, RTLD_NOW);
958 ASSERT_TRUE(handle1 != nullptr) << dlerror();
959
960 void* handle2 = dlopen(get_executable_path().c_str(), RTLD_NOW);
961 ASSERT_TRUE(handle2 != nullptr) << dlerror();
962
963#if defined(__BIONIC__)
964 ASSERT_EQ(handle1, handle2);
965#else
966 GTEST_LOG_(INFO) << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
967 "it loads a separate copy of the main executable "
968 "on dlopen by absolute path.";
969#endif
970}
971
Victor Khimenko14b9d712017-01-25 19:59:16 +0100972#if defined (__aarch64__)
973#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm64/"
974#elif defined (__arm__)
975#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm/"
976#elif defined (__i386__)
977#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86/"
978#elif defined (__x86_64__)
979#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86_64/"
980#elif defined (__mips__)
981#if defined(__LP64__)
982#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips64/"
983#else
984#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips/"
985#endif
986#else
987#error "Unknown architecture"
988#endif
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200989#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100990#define ALTERNATE_PATH_TO_LIBC ALTERNATE_PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700991
992TEST(dlfcn, dladdr_libc) {
993#if defined(__BIONIC__)
994 Dl_info info;
995 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
996 ASSERT_TRUE(dladdr(addr, &info) != 0);
997
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700998 char libc_realpath[PATH_MAX];
Victor Khimenko14b9d712017-01-25 19:59:16 +0100999
1000 // Check if libc is in canonical path or in alternate path.
1001 if (strncmp(ALTERNATE_PATH_TO_SYSTEM_LIB,
1002 info.dli_fname,
1003 sizeof(ALTERNATE_PATH_TO_SYSTEM_LIB) - 1) == 0) {
1004 // Platform with emulated architecture. Symlink on ARC++.
1005 ASSERT_TRUE(realpath(ALTERNATE_PATH_TO_LIBC, libc_realpath) == libc_realpath);
1006 } else {
1007 // /system/lib is symlink when this test is executed on host.
1008 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
1009 }
Dmitriy Ivanovef255922015-04-08 11:53:08 -07001010
1011 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001012 // TODO: add check for dfi_fbase
1013 ASSERT_STREQ("puts", info.dli_sname);
1014 ASSERT_EQ(addr, info.dli_saddr);
1015#else
1016 GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig "
1017 "for libc.so, which is symlink itself (not a realpath).\n";
1018#endif
1019}
1020
Elliott Hughese66190d2012-12-18 15:57:55 -08001021TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -07001022 Dl_info info;
1023
Elliott Hughes3b297c42012-10-11 16:08:51 -07001024 dlerror(); // Clear any pending errors.
1025
Elliott Hughes8e15b082012-09-26 11:44:01 -07001026 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001027 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
1028 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001029
1030 // No symbol corresponding to a stack address.
1031 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001032 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001033}
Elliott Hughes124fae92012-10-31 14:20:03 -07001034
Elliott Hughesa43e9062013-01-07 14:18:22 -08001035// GNU-style ELF hash tables are incompatible with the MIPS ABI.
1036// 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 -08001037TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001038#if !defined(__mips__)
Elliott Hughes124fae92012-10-31 14:20:03 -07001039 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001040 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
1041 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001042 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001043 void* sym = dlsym(handle, "getRandomNumber");
1044 ASSERT_TRUE(sym != nullptr) << dlerror();
1045 int (*fn)(void);
1046 fn = reinterpret_cast<int (*)(void)>(sym);
1047 EXPECT_EQ(4, fn());
1048
1049 Dl_info dlinfo;
1050 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1051
1052 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1053 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001054 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001055#else
1056 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
1057#endif
Elliott Hughes124fae92012-10-31 14:20:03 -07001058}
Elliott Hughese66190d2012-12-18 15:57:55 -08001059
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001060TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
1061 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
1062 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001063 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001064 void* sym = dlsym(handle, "getRandomNumber");
1065 ASSERT_TRUE(sym != nullptr) << dlerror();
1066 int (*fn)(void);
1067 fn = reinterpret_cast<int (*)(void)>(sym);
1068 EXPECT_EQ(4, fn());
1069
1070 Dl_info dlinfo;
1071 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1072
1073 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1074 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
1075 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
1076}
1077
Elliott Hughese66190d2012-12-18 15:57:55 -08001078TEST(dlfcn, dlopen_bad_flags) {
1079 dlerror(); // Clear any pending errors.
1080 void* handle;
1081
Elliott Hughes063525c2014-05-13 11:19:57 -07001082#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -08001083 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001084 handle = dlopen(nullptr, 0);
1085 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001086 ASSERT_SUBSTR("invalid", dlerror());
1087#endif
1088
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001089 handle = dlopen(nullptr, 0xffffffff);
1090 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001091 ASSERT_SUBSTR("invalid", dlerror());
1092
1093 // 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 -07001094 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
1095 ASSERT_TRUE(handle != nullptr);
1096 ASSERT_SUBSTR(nullptr, dlerror());
Elliott Hughese66190d2012-12-18 15:57:55 -08001097}
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001098
1099TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001100 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001101 ASSERT_TRUE(addr == nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001102}
1103
1104TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001105 void* addr = dlsym(RTLD_DEFAULT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001106 ASSERT_TRUE(addr != nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001107}
1108
1109TEST(dlfcn, rtld_next_unknown_symbol) {
1110 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001111 ASSERT_TRUE(addr == nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001112}
1113
1114TEST(dlfcn, rtld_next_known_symbol) {
1115 void* addr = dlsym(RTLD_NEXT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001116 ASSERT_TRUE(addr != nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001117}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001118
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001119// Check that RTLD_NEXT of a libc symbol works in dlopened library
1120TEST(dlfcn, rtld_next_from_library) {
Raj Mamadgi527229c2017-11-02 15:53:50 -07001121 void* library_with_fclose = dlopen("libtest_check_rtld_next_from_library.so", RTLD_NOW);
1122 ASSERT_TRUE(library_with_fclose != nullptr) << dlerror();
1123 void* expected_addr = dlsym(RTLD_DEFAULT, "fclose");
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001124 ASSERT_TRUE(expected_addr != nullptr) << dlerror();
Raj Mamadgi527229c2017-11-02 15:53:50 -07001125 typedef void* (*get_libc_fclose_ptr_fn_t)();
1126 get_libc_fclose_ptr_fn_t get_libc_fclose_ptr =
1127 reinterpret_cast<get_libc_fclose_ptr_fn_t>(dlsym(library_with_fclose, "get_libc_fclose_ptr"));
1128 ASSERT_TRUE(get_libc_fclose_ptr != nullptr) << dlerror();
1129 ASSERT_EQ(expected_addr, get_libc_fclose_ptr());
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001130
Raj Mamadgi527229c2017-11-02 15:53:50 -07001131 dlclose(library_with_fclose);
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001132}
1133
1134
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001135TEST(dlfcn, dlsym_weak_func) {
1136 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001137 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001138 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001139
1140 int (*weak_func)();
1141 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001142 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001143 EXPECT_EQ(42, weak_func());
1144 dlclose(handle);
1145}
1146
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001147TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001148 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
1149 ASSERT_TRUE(handle != nullptr) << dlerror();
1150 int (*weak_func)();
1151 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
1152 ASSERT_TRUE(weak_func != nullptr) << dlerror();
1153 EXPECT_EQ(6551, weak_func());
1154 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001155}
1156
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001157TEST(dlfcn, dlopen_symlink) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001158 DlfcnSymlink symlink("dlopen_symlink");
1159 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001160 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001161 void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001162 ASSERT_TRUE(handle1 != nullptr);
1163 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001164 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -07001165 dlclose(handle1);
1166 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001167}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001168
1169// libtest_dlopen_from_ctor_main.so depends on
1170// libtest_dlopen_from_ctor.so which has a constructor
1171// that calls dlopen(libc...). This is to test the situation
1172// described in b/7941716.
1173TEST(dlfcn, dlopen_dlopen_from_ctor) {
1174#if defined(__BIONIC__)
1175 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1176 ASSERT_TRUE(handle != nullptr) << dlerror();
1177 dlclose(handle);
1178#else
1179 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
1180#endif
1181}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001182
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001183static std::string g_fini_call_order_str;
1184
1185static void register_fini_call(const char* s) {
1186 g_fini_call_order_str += s;
1187}
1188
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001189static void test_init_fini_call_order_for(const char* libname) {
1190 g_fini_call_order_str.clear();
1191 void* handle = dlopen(libname, RTLD_NOW);
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001192 ASSERT_TRUE(handle != nullptr) << dlerror();
1193 typedef int (*get_init_order_number_t)();
1194 get_init_order_number_t get_init_order_number =
1195 reinterpret_cast<get_init_order_number_t>(dlsym(handle, "get_init_order_number"));
1196 ASSERT_EQ(321, get_init_order_number());
1197
1198 typedef void (*set_fini_callback_t)(void (*f)(const char*));
1199 set_fini_callback_t set_fini_callback =
1200 reinterpret_cast<set_fini_callback_t>(dlsym(handle, "set_fini_callback"));
1201 set_fini_callback(register_fini_call);
1202 dlclose(handle);
1203 ASSERT_EQ("(root)(child)(grandchild)", g_fini_call_order_str);
1204}
1205
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001206TEST(dlfcn, init_fini_call_order) {
1207 test_init_fini_call_order_for("libtest_init_fini_order_root.so");
1208 test_init_fini_call_order_for("libtest_init_fini_order_root2.so");
1209}
1210
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001211TEST(dlfcn, symbol_versioning_use_v1) {
1212 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1213 ASSERT_TRUE(handle != nullptr) << dlerror();
1214 typedef int (*fn_t)();
1215 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1216 ASSERT_TRUE(fn != nullptr) << dlerror();
1217 ASSERT_EQ(1, fn());
1218 dlclose(handle);
1219}
1220
1221TEST(dlfcn, symbol_versioning_use_v2) {
1222 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1223 ASSERT_TRUE(handle != nullptr) << dlerror();
1224 typedef int (*fn_t)();
1225 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1226 ASSERT_TRUE(fn != nullptr) << dlerror();
1227 ASSERT_EQ(2, fn());
1228 dlclose(handle);
1229}
1230
1231TEST(dlfcn, symbol_versioning_use_other_v2) {
1232 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1233 ASSERT_TRUE(handle != nullptr) << dlerror();
1234 typedef int (*fn_t)();
1235 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1236 ASSERT_TRUE(fn != nullptr) << dlerror();
1237 ASSERT_EQ(20, fn());
1238 dlclose(handle);
1239}
1240
1241TEST(dlfcn, symbol_versioning_use_other_v3) {
1242 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1243 ASSERT_TRUE(handle != nullptr) << dlerror();
1244 typedef int (*fn_t)();
1245 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1246 ASSERT_TRUE(fn != nullptr) << dlerror();
1247 ASSERT_EQ(3, fn());
1248 dlclose(handle);
1249}
1250
1251TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1252 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1253 ASSERT_TRUE(handle != nullptr) << dlerror();
1254 typedef int (*fn_t)();
1255 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1256 ASSERT_TRUE(fn != nullptr) << dlerror();
1257 ASSERT_EQ(3, fn()); // the default version is 3
1258 dlclose(handle);
1259}
1260
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001261TEST(dlfcn, dlvsym_smoke) {
1262 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1263 ASSERT_TRUE(handle != nullptr) << dlerror();
1264 typedef int (*fn_t)();
1265
1266 {
1267 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1268 ASSERT_TRUE(fn == nullptr);
1269 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1270 }
1271
1272 {
1273 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1274 ASSERT_TRUE(fn != nullptr) << dlerror();
1275 ASSERT_EQ(2, fn());
1276 }
1277
1278 dlclose(handle);
1279}
1280
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001281// This preempts the implementation from libtest_versioned_lib.so
1282extern "C" int version_zero_function() {
1283 return 0;
1284}
1285
1286// This preempts the implementation from libtest_versioned_uselibv*.so
1287extern "C" int version_zero_function2() {
1288 return 0;
1289}
Evgenii Stepanov68650822015-06-10 13:38:39 -07001290
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001291TEST(dlfcn, dt_runpath_smoke) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001292 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1293 ASSERT_TRUE(handle != nullptr) << dlerror();
1294
1295 typedef void *(* dlopen_b_fn)();
1296 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1297 ASSERT_TRUE(fn != nullptr) << dlerror();
1298
1299 void *p = fn();
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001300 ASSERT_TRUE(p != nullptr);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001301
1302 dlclose(handle);
1303}
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001304
Dimitry Ivanovdb6ab3d2017-06-27 11:02:51 -07001305TEST(dlfcn, dt_runpath_absolute_path) {
1306 std::string libpath = get_testlib_root() + "/libtest_dt_runpath_d.so";
1307 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1308 ASSERT_TRUE(handle != nullptr) << dlerror();
1309
1310 typedef void *(* dlopen_b_fn)();
1311 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1312 ASSERT_TRUE(fn != nullptr) << dlerror();
1313
1314 void *p = fn();
1315 ASSERT_TRUE(p != nullptr);
1316
1317 dlclose(handle);
1318}
1319
dimitry06016f22018-01-05 11:39:28 +01001320TEST(dlfcn, dlclose_after_thread_local_dtor) {
1321 bool is_dtor_triggered = false;
1322
1323 auto f = [](void* handle, bool* is_dtor_triggered) {
1324 typedef void (*fn_t)(bool*);
1325 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1326 ASSERT_TRUE(fn != nullptr) << dlerror();
1327
1328 fn(is_dtor_triggered);
1329
1330 ASSERT_TRUE(!*is_dtor_triggered);
1331 };
1332
1333 void* handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW | RTLD_NOLOAD);
1334 ASSERT_TRUE(handle == nullptr);
1335
1336 handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW);
1337 ASSERT_TRUE(handle != nullptr) << dlerror();
1338
1339 std::thread t(f, handle, &is_dtor_triggered);
1340 t.join();
1341
1342 ASSERT_TRUE(is_dtor_triggered);
1343 dlclose(handle);
1344
1345 handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW | RTLD_NOLOAD);
1346 ASSERT_TRUE(handle == nullptr);
1347}
1348
1349TEST(dlfcn, dlclose_before_thread_local_dtor) {
1350 bool is_dtor_triggered = false;
1351
1352 auto f = [](bool* is_dtor_triggered) {
1353 void* handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW | RTLD_NOLOAD);
1354 ASSERT_TRUE(handle == nullptr);
1355
1356 handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW);
1357 ASSERT_TRUE(handle != nullptr) << dlerror();
1358
1359 typedef void (*fn_t)(bool*);
1360 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1361 ASSERT_TRUE(fn != nullptr) << dlerror();
1362
1363 fn(is_dtor_triggered);
1364
1365 dlclose(handle);
1366
1367 ASSERT_TRUE(!*is_dtor_triggered);
1368
1369 // Since we have thread_atexit dtors associated with handle - the library should
1370 // still be availabe.
1371 handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW | RTLD_NOLOAD);
1372 ASSERT_TRUE(handle != nullptr) << dlerror();
1373 dlclose(handle);
1374 };
1375
1376 void* handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW);
1377 ASSERT_TRUE(handle != nullptr) << dlerror();
1378 dlclose(handle);
1379
1380 handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW | RTLD_NOLOAD);
1381 ASSERT_TRUE(handle == nullptr);
1382
1383 std::thread t(f, &is_dtor_triggered);
1384 t.join();
1385#if defined(__BIONIC__)
1386 // ld-android.so unloads unreferenced libraries on pthread_exit()
1387 ASSERT_TRUE(is_dtor_triggered);
1388 handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW | RTLD_NOLOAD);
1389 ASSERT_TRUE(handle == nullptr);
1390#else
1391 // GLIBC does not unload libraries with ref_count = 0 on pthread_exit
1392 ASSERT_TRUE(is_dtor_triggered);
1393 handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW | RTLD_NOLOAD);
1394 ASSERT_TRUE(handle != nullptr) << dlerror();
1395#endif
1396}
1397
Elliott Hughes8ad40932017-06-15 15:12:29 -07001398TEST(dlfcn, RTLD_macros) {
1399#if !defined(RTLD_LOCAL)
1400#error no RTLD_LOCAL
1401#elif !defined(RTLD_LAZY)
1402#error no RTLD_LAZY
1403#elif !defined(RTLD_NOW)
1404#error no RTLD_NOW
1405#elif !defined(RTLD_NOLOAD)
1406#error no RTLD_NOLOAD
1407#elif !defined(RTLD_GLOBAL)
1408#error no RTLD_GLOBAL
1409#elif !defined(RTLD_NODELETE)
1410#error no RTLD_NODELETE
1411#endif
1412}
1413
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001414// Bionic specific tests
1415#if defined(__BIONIC__)
1416
Dimitry Ivanovfc32dcb2017-03-24 10:58:23 -07001417#if defined(__arm__)
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001418const llvm::ELF::Elf32_Dyn* to_dynamic_table(const char* p) {
1419 return reinterpret_cast<const llvm::ELF::Elf32_Dyn*>(p);
1420}
1421
1422// Duplicate these definitions here because they are android specific
1423// note that we cannot include <elf.h> because #defines conflict with
1424// enum names provided by LLVM.
1425#define DT_ANDROID_REL (llvm::ELF::DT_LOOS + 2)
1426#define DT_ANDROID_RELA (llvm::ELF::DT_LOOS + 4)
1427
1428template<typename ELFT>
1429void validate_compatibility_of_native_library(const std::string& path, ELFT* elf) {
1430 bool has_elf_hash = false;
1431 bool has_android_rel = false;
1432 bool has_rel = false;
1433 // Find dynamic section and check that DT_HASH and there is no DT_ANDROID_REL
1434 for (auto it = elf->section_begin(); it != elf->section_end(); ++it) {
1435 const llvm::object::ELFSectionRef& section_ref = *it;
1436 if (section_ref.getType() == llvm::ELF::SHT_DYNAMIC) {
1437 llvm::StringRef data;
1438 ASSERT_TRUE(!it->getContents(data)) << "unable to get SHT_DYNAMIC section data";
1439 for (auto d = to_dynamic_table(data.data()); d->d_tag != llvm::ELF::DT_NULL; ++d) {
1440 if (d->d_tag == llvm::ELF::DT_HASH) {
1441 has_elf_hash = true;
1442 } else if (d->d_tag == DT_ANDROID_REL || d->d_tag == DT_ANDROID_RELA) {
1443 has_android_rel = true;
1444 } else if (d->d_tag == llvm::ELF::DT_REL || d->d_tag == llvm::ELF::DT_RELA) {
1445 has_rel = true;
1446 }
1447 }
1448
1449 break;
1450 }
1451 }
1452
1453 ASSERT_TRUE(has_elf_hash) << path.c_str() << ": missing elf hash (DT_HASH)";
1454 ASSERT_TRUE(!has_android_rel) << path.c_str() << ": has packed relocations";
1455 ASSERT_TRUE(has_rel) << path.c_str() << ": missing DT_REL/DT_RELA";
1456}
1457
1458void validate_compatibility_of_native_library(const char* soname) {
Victor Khimenko14b9d712017-01-25 19:59:16 +01001459 // On the systems with emulation system libraries would be of different
1460 // architecture. Try to use alternate paths first.
1461 std::string path = std::string(ALTERNATE_PATH_TO_SYSTEM_LIB) + soname;
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001462 auto binary_or_error = llvm::object::createBinary(path);
Victor Khimenko14b9d712017-01-25 19:59:16 +01001463 if (!binary_or_error) {
1464 path = std::string(PATH_TO_SYSTEM_LIB) + soname;
1465 binary_or_error = llvm::object::createBinary(path);
1466 }
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001467 ASSERT_FALSE(!binary_or_error);
1468
1469 llvm::object::Binary* binary = binary_or_error.get().getBinary();
1470
1471 auto obj = llvm::dyn_cast<llvm::object::ObjectFile>(binary);
1472 ASSERT_TRUE(obj != nullptr);
1473
1474 auto elf = llvm::dyn_cast<llvm::object::ELF32LEObjectFile>(obj);
1475
1476 ASSERT_TRUE(elf != nullptr);
1477
1478 validate_compatibility_of_native_library(path, elf);
1479}
1480
Dimitry Ivanovfc32dcb2017-03-24 10:58:23 -07001481// This is a test for app compatibility workaround for arm apps
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001482// affected by http://b/24465209
1483TEST(dlext, compat_elf_hash_and_relocation_tables) {
1484 validate_compatibility_of_native_library("libc.so");
1485 validate_compatibility_of_native_library("liblog.so");
1486 validate_compatibility_of_native_library("libstdc++.so");
1487 validate_compatibility_of_native_library("libdl.so");
1488 validate_compatibility_of_native_library("libm.so");
1489 validate_compatibility_of_native_library("libz.so");
1490 validate_compatibility_of_native_library("libjnigraphics.so");
1491}
1492
Dimitry Ivanovfc32dcb2017-03-24 10:58:23 -07001493#endif // defined(__arm__)
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001494
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001495TEST(dlfcn, dlopen_invalid_rw_load_segment) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001496 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001497 "/" + kPrebuiltElfDir +
1498 "/libtest_invalid-rw_load_segment.so";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001499 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1500 ASSERT_TRUE(handle == nullptr);
1501 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W + E load segments are not allowed";
1502 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1503}
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001504
1505TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001506 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001507 "/" + kPrebuiltElfDir +
1508 "/libtest_invalid-unaligned_shdr_offset.so";
1509
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001510 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1511 ASSERT_TRUE(handle == nullptr);
1512 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
1513 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1514}
1515
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001516TEST(dlfcn, dlopen_invalid_zero_shentsize) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001517 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001518 "/" + kPrebuiltElfDir +
1519 "/libtest_invalid-zero_shentsize.so";
1520
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001521 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1522 ASSERT_TRUE(handle == nullptr);
1523 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x";
1524 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1525}
1526
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001527TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001528 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001529 "/" + kPrebuiltElfDir +
1530 "/libtest_invalid-zero_shstrndx.so";
1531
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001532 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1533 ASSERT_TRUE(handle == nullptr);
1534 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx";
1535 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1536}
1537
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001538TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001539 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001540 "/" + kPrebuiltElfDir +
1541 "/libtest_invalid-empty_shdr_table.so";
1542
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001543 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1544 ASSERT_TRUE(handle == nullptr);
1545 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers";
1546 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1547}
1548
Dimitry Ivanov46230442016-08-15 13:40:53 -07001549TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001550 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001551 "/" + kPrebuiltElfDir +
1552 "/libtest_invalid-zero_shdr_table_offset.so";
1553
Dimitry Ivanov46230442016-08-15 13:40:53 -07001554 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1555 ASSERT_TRUE(handle == nullptr);
1556 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/";
1557 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1558}
1559
Dimitry Ivanov55958342016-08-15 14:06:04 -07001560TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001561 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001562 "/" + kPrebuiltElfDir +
1563 "/libtest_invalid-zero_shdr_table_content.so";
1564
Dimitry Ivanov55958342016-08-15 14:06:04 -07001565 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1566 ASSERT_TRUE(handle == nullptr);
1567 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
1568 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1569}
1570
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001571TEST(dlfcn, dlopen_invalid_textrels) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001572 const std::string libpath = get_testlib_root() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001573 "/" + kPrebuiltElfDir +
1574 "/libtest_invalid-textrels.so";
1575
1576 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1577 ASSERT_TRUE(handle == nullptr);
1578 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1579 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1580}
1581
1582TEST(dlfcn, dlopen_invalid_textrels2) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001583 const std::string libpath = get_testlib_root() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001584 "/" + kPrebuiltElfDir +
1585 "/libtest_invalid-textrels2.so";
1586
1587 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1588 ASSERT_TRUE(handle == nullptr);
1589 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1590 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1591}
1592
Jiyong Park01162f22017-10-16 15:31:09 +09001593TEST(dlfcn, dlopen_df_1_global) {
1594 void* handle = dlopen("libtest_dlopen_df_1_global.so", RTLD_NOW);
1595 ASSERT_TRUE(handle != nullptr) << dlerror();
1596}
1597
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001598#endif