blob: 311e462c964c6e6ec1cac39de402b03ab48d4167 [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
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -070032#include <android-base/file.h>
Tom Cherryb8ab6182017-04-05 16:20:29 -070033#include <android-base/scopeguard.h>
34
Dimitry Ivanov927877c2016-09-21 11:17:13 -070035#include "gtest_globals.h"
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -070036#include "gtest_utils.h"
Dimitry Ivanov708589f2016-09-19 10:50:28 -070037#include "dlfcn_symlink_support.h"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070038#include "utils.h"
39
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -080040#if defined(__BIONIC__) && (defined(__arm__) || defined(__i386__))
41#pragma clang diagnostic push
42#pragma clang diagnostic ignored "-Wunused-parameter"
43
44#include <llvm/ADT/StringRef.h>
45#include <llvm/Object/Binary.h>
46#include <llvm/Object/ELFObjectFile.h>
47#include <llvm/Object/ObjectFile.h>
48
49#pragma clang diagnostic pop
50#endif // defined(__ANDROID__) && (defined(__arm__) || defined(__i386__))
51
Elliott Hughes3b297c42012-10-11 16:08:51 -070052#define ASSERT_SUBSTR(needle, haystack) \
53 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
54
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070055
Elliott Hughes1728b232014-05-14 10:02:03 -070056static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070057extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070058 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070059}
60
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070061static int g_ctor_function_called = 0;
Dimitry Ivanov55437462016-07-20 15:33:07 -070062static int g_ctor_argc = 0;
63static char** g_ctor_argv = reinterpret_cast<char**>(0xDEADBEEF);
64static char** g_ctor_envp = g_ctor_envp;
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) __attribute__ ((constructor));
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070067
Dimitry Ivanov55437462016-07-20 15:33:07 -070068extern "C" void ctor_function(int argc, char** argv, char** envp) {
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070069 g_ctor_function_called = 17;
Dimitry Ivanov55437462016-07-20 15:33:07 -070070 g_ctor_argc = argc;
71 g_ctor_argv = argv;
72 g_ctor_envp = envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070073}
74
75TEST(dlfcn, ctor_function_call) {
76 ASSERT_EQ(17, g_ctor_function_called);
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -070077 ASSERT_TRUE(g_ctor_argc = GetArgc());
78 ASSERT_TRUE(g_ctor_argv = GetArgv());
79 ASSERT_TRUE(g_ctor_envp = GetEnvp());
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070080}
81
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070082TEST(dlfcn, dlsym_in_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070083 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070084 void* self = dlopen(nullptr, RTLD_NOW);
85 ASSERT_TRUE(self != nullptr);
86 ASSERT_TRUE(dlerror() == nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070087
88 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070089 ASSERT_TRUE(sym != nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070090
91 void (*function)() = reinterpret_cast<void(*)()>(sym);
92
Elliott Hughes1728b232014-05-14 10:02:03 -070093 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070094 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070095 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070096
97 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070098}
Elliott Hughes8e15b082012-09-26 11:44:01 -070099
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700100TEST(dlfcn, dlsym_from_sofile) {
101 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL);
102 ASSERT_TRUE(handle != nullptr) << dlerror();
103
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700104 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700105 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
106 ASSERT_TRUE(symbol == nullptr);
107 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
108
109 typedef int* (*fn_t)();
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700110 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
111 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
112 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700113
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700114 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700115 ASSERT_TRUE(ptr != nullptr) << dlerror();
116 ASSERT_EQ(42, *ptr);
117
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700118 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
119 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
120 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
121
122 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
123 ASSERT_TRUE(ptr != nullptr) << dlerror();
124 ASSERT_EQ(44, *ptr);
125
126 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
127 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
128 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
129
130 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
131 ASSERT_TRUE(ptr != nullptr) << dlerror();
132 ASSERT_EQ(43, *ptr);
133
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700134 dlclose(handle);
135}
136
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700137TEST(dlfcn, dlsym_from_sofile_with_preload) {
138 void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL);
139 ASSERT_TRUE(preload != nullptr) << dlerror();
140
141 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
142 ASSERT_TRUE(handle != nullptr) << dlerror();
143
144 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
145 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
146 ASSERT_TRUE(symbol == nullptr);
147 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
148
149 typedef int* (*fn_t)();
150 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
151 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
152 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
153
154 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
155 ASSERT_TRUE(ptr != nullptr) << dlerror();
156 ASSERT_EQ(42, *ptr);
157
158 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
159 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
160 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
161
162 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
163 ASSERT_TRUE(ptr != nullptr) << dlerror();
164 ASSERT_EQ(44, *ptr);
165
166 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
167 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
168 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
169
170 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
171 ASSERT_TRUE(ptr != nullptr) << dlerror();
172 ASSERT_EQ(43, *ptr);
173
174 dlclose(handle);
175 dlclose(preload);
176}
177
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700178TEST(dlfcn, dlsym_handle_global_sym) {
179 // check that we do not look into global group
180 // when looking up symbol by handle
181 void* handle = dlopen("libtest_empty.so", RTLD_NOW);
182 dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL);
183 void* sym = dlsym(handle, "getRandomNumber");
184 ASSERT_TRUE(sym == nullptr);
185 ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror());
186
187 sym = dlsym(handle, "DlSymTestFunction");
188 ASSERT_TRUE(sym == nullptr);
189 ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror());
190 dlclose(handle);
191}
192
Dimitry Ivanovd5b578a2016-12-14 15:16:56 -0800193TEST(dlfcn, dlsym_handle_empty_symbol) {
194 // check that dlsym of an empty symbol fails (see http://b/33530622)
195 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW);
196 ASSERT_TRUE(handle != nullptr) << dlerror();
197 void* sym = dlsym(handle, "");
198 ASSERT_TRUE(sym == nullptr);
199 ASSERT_SUBSTR("undefined symbol: ", dlerror());
200 dlclose(handle);
201}
202
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700203TEST(dlfcn, dlsym_with_dependencies) {
204 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700205 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700206 dlerror();
207 // This symbol is in DT_NEEDED library.
208 void* sym = dlsym(handle, "getRandomNumber");
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700209 ASSERT_TRUE(sym != nullptr) << dlerror();
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700210 int (*fn)(void);
211 fn = reinterpret_cast<int (*)(void)>(sym);
212 EXPECT_EQ(4, fn());
213 dlclose(handle);
214}
215
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700216TEST(dlfcn, dlopen_noload) {
217 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700218 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700219 handle = dlopen("libtest_simple.so", RTLD_NOW);
220 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700221 ASSERT_TRUE(handle != nullptr);
222 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700223 ASSERT_TRUE(handle == handle2);
224 ASSERT_EQ(0, dlclose(handle));
225 ASSERT_EQ(0, dlclose(handle2));
226}
227
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700228TEST(dlfcn, dlopen_by_soname) {
229 static const char* soname = "libdlext_test_soname.so";
230 static const char* filename = "libdlext_test_different_soname.so";
231 // 1. Make sure there is no library with soname in default search path
232 void* handle = dlopen(soname, RTLD_NOW);
233 ASSERT_TRUE(handle == nullptr);
234
235 // 2. Load a library using filename
236 handle = dlopen(filename, RTLD_NOW);
237 ASSERT_TRUE(handle != nullptr) << dlerror();
238
239 // 3. Find library by soname
240 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
241 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
242 ASSERT_EQ(handle, handle_soname);
243
244 // 4. RTLD_NOLOAD should still work with filename
245 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
246 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
247 ASSERT_EQ(handle, handle_filename);
248
249 dlclose(handle_filename);
250 dlclose(handle_soname);
251 dlclose(handle);
252}
253
dimitryc18de1b2017-09-26 14:31:35 +0200254TEST(dlfcn, dlopen_vdso) {
dimitryb9555a92017-10-11 18:04:05 +0200255#if __has_include(<sys/auxv.h>)
256 if (getauxval(AT_SYSINFO_EHDR) == 0) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800257 GTEST_SKIP() << "getauxval(AT_SYSINFO_EHDR) == 0, skipping this test";
dimitryb9555a92017-10-11 18:04:05 +0200258 }
259#endif
Elliott Hughesda1bb112018-02-16 10:05:08 -0800260
261 const char* vdso_name = "linux-vdso.so.1";
262#if defined(__i386__)
263 vdso_name = "linux-gate.so.1";
264#endif
265 void* handle = dlopen(vdso_name, RTLD_NOW);
dimitryc18de1b2017-09-26 14:31:35 +0200266 ASSERT_TRUE(handle != nullptr) << dlerror();
267 dlclose(handle);
268}
269
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700270// mips doesn't support ifuncs
271#if !defined(__mips__)
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700272TEST(dlfcn, ifunc_variable) {
273 typedef const char* (*fn_ptr)();
274
275 // ifunc's choice depends on whether IFUNC_CHOICE has a value
276 // first check the set case
277 setenv("IFUNC_CHOICE", "set", 1);
278 // preload libtest_ifunc_variable_impl.so
279 void* handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
280 void* handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
281 ASSERT_TRUE(handle != nullptr) << dlerror();
282 const char** foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
283 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
284 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
285 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
286 ASSERT_EQ(strncmp("set", *foo_ptr, 3), 0);
287 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
288 dlclose(handle);
289 dlclose(handle_impl);
290
291 // then check the unset case
292 unsetenv("IFUNC_CHOICE");
293 handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
294 handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
295 ASSERT_TRUE(handle != nullptr) << dlerror();
296 foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
297 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
298 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
299 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
300 ASSERT_EQ(strncmp("unset", *foo_ptr, 5), 0);
301 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
302 dlclose(handle);
303 dlclose(handle_impl);
304}
305
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700306TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700307 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700308
309 // ifunc's choice depends on whether IFUNC_CHOICE has a value
310 // first check the set case
311 setenv("IFUNC_CHOICE", "set", 1);
312 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700313 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700314 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
315 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700316 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
317 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700318 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
319 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700320 dlclose(handle);
321
322 // then check the unset case
323 unsetenv("IFUNC_CHOICE");
324 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700325 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700326 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
327 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700328 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
329 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700330 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700331 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700332 dlclose(handle);
333}
334
335TEST(dlfcn, ifunc_ctor_call) {
336 typedef const char* (*fn_ptr)();
337
338 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700339 ASSERT_TRUE(handle != nullptr) << dlerror();
340 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
341 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
342 ASSERT_STREQ("false", is_ctor_called());
343
344 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
345 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700346 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700347 dlclose(handle);
348}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700349
350TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
351 typedef const char* (*fn_ptr)();
352
353 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
354 ASSERT_TRUE(handle != nullptr) << dlerror();
355 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
356 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
357 ASSERT_STREQ("false", is_ctor_called());
358
359 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
360 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
361 ASSERT_STREQ("true", is_ctor_called());
362 dlclose(handle);
363}
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700364#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700365
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700366TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
367 // This is the structure of the test library and
368 // its dt_needed libraries
369 // libtest_relo_check_dt_needed_order.so
370 // |
371 // +-> libtest_relo_check_dt_needed_order_1.so
372 // |
373 // +-> libtest_relo_check_dt_needed_order_2.so
374 //
375 // The root library references relo_test_get_answer_lib - which is defined
376 // in both dt_needed libraries, the correct relocation should
377 // use the function defined in libtest_relo_check_dt_needed_order_1.so
378 void* handle = nullptr;
Tom Cherryb8ab6182017-04-05 16:20:29 -0700379 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700380
381 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
382 ASSERT_TRUE(handle != nullptr) << dlerror();
383
384 typedef int (*fn_t) (void);
385 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
386 ASSERT_TRUE(fn != nullptr) << dlerror();
387 ASSERT_EQ(1, fn());
388}
389
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700390TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700391 // Here is how the test library and its dt_needed
392 // libraries are arranged
393 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700394 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700395 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700396 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700397 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700398 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700399 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700400 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700401 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700402 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700403 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700404 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700405 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700406 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700407 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700408 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700409 //
410 // load order should be (1, 2, 3, a, b, d)
411 //
412 // get_answer() is defined in (2, 3, a, b, c)
413 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700414 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700415 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700416 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
417 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700418 typedef int (*fn_t) (void);
419 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700420 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700421 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700422 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700423 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700424
425 ASSERT_EQ(42, fn());
426 ASSERT_EQ(43, fn2());
427 dlclose(handle);
428}
429
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700430TEST(dlfcn, dlopen_check_order_reloc_siblings) {
431 // This is how this one works:
432 // we lookup and call get_answer which is defined in '_2.so'
433 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
434 // the correct _impl() is implemented by '_a.so';
435 //
436 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
437 //
438 // Here is the picture:
439 //
440 // libtest_check_order_reloc_siblings.so
441 // |
442 // +-> ..._1.so <- empty
443 // | |
444 // | +-> ..._a.so <- exports correct answer_impl()
445 // | |
446 // | +-> ..._b.so <- every other letter exporting incorrect one.
447 // |
448 // +-> ..._2.so <- empty
449 // | |
450 // | +-> ..._c.so
451 // | |
452 // | +-> ..._d.so
453 // |
454 // +-> ..._3.so <- empty
455 // |
456 // +-> ..._e.so
457 // |
458 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
459 // implements incorrect get_answer_impl()
460
461 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
462 ASSERT_TRUE(handle == nullptr);
463#ifdef __BIONIC__
464 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
465 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
466#endif
467
468 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
469 ASSERT_TRUE(handle != nullptr) << dlerror();
470
471 typedef int (*fn_t) (void);
472 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
473 ASSERT_TRUE(fn != nullptr) << dlerror();
474 ASSERT_EQ(42, fn());
475
476 ASSERT_EQ(0, dlclose(handle));
477}
478
479TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
480 // This test uses the same library as dlopen_check_order_reloc_siblings.
481 // Unlike dlopen_check_order_reloc_siblings it preloads
482 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
483 // dlopen(libtest_check_order_reloc_siblings.so)
484
485 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
486 ASSERT_TRUE(handle == nullptr);
487 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
488 ASSERT_TRUE(handle == nullptr);
489
490 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
491 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
492
493 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
494 ASSERT_TRUE(handle != nullptr) << dlerror();
495
496 ASSERT_EQ(0, dlclose(handle_for_1));
497
498 typedef int (*fn_t) (void);
499 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
500 ASSERT_TRUE(fn != nullptr) << dlerror();
501 ASSERT_EQ(42, fn());
502
503 ASSERT_EQ(0, dlclose(handle));
504}
505
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800506TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
507 // This is how this one works:
508 // we lookup and call grandchild_get_answer which is defined in '_2.so'
509 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
510 // the correct _impl() is implemented by '_c_1.so';
511 //
512 // Here is the picture of subtree:
513 //
514 // libtest_check_order_reloc_siblings.so
515 // |
516 // +-> ..._2.so <- grandchild_get_answer()
517 // |
518 // +-> ..._c.so <- empty
519 // | |
520 // | +-> _c_1.so <- exports correct answer_impl()
521 // | |
522 // | +-> _c_2.so <- exports incorrect answer_impl()
523 // |
524 // +-> ..._d.so <- empty
525
526 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
527 ASSERT_TRUE(handle == nullptr);
528#ifdef __BIONIC__
529 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
530 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
531#endif
532
533 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
534 ASSERT_TRUE(handle != nullptr) << dlerror();
535
536 typedef int (*fn_t) (void);
537 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
538 ASSERT_TRUE(fn != nullptr) << dlerror();
539 ASSERT_EQ(42, fn());
540
541 ASSERT_EQ(0, dlclose(handle));
542}
543
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700544TEST(dlfcn, dlopen_check_order_reloc_nephew) {
545 // This is how this one works:
546 // we lookup and call nephew_get_answer which is defined in '_2.so'
547 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
548 // the correct _impl() is implemented by '_a.so';
549 //
550 // Here is the picture:
551 //
552 // libtest_check_order_reloc_siblings.so
553 // |
554 // +-> ..._1.so <- empty
555 // | |
556 // | +-> ..._a.so <- exports correct answer_impl()
557 // | |
558 // | +-> ..._b.so <- every other letter exporting incorrect one.
559 // |
560 // +-> ..._2.so <- empty
561 // | |
562 // | +-> ..._c.so
563 // | |
564 // | +-> ..._d.so
565 // |
566 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
567 // |
568 // +-> ..._e.so
569 // |
570 // +-> ..._f.so
571
572 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
573 ASSERT_TRUE(handle == nullptr);
574#ifdef __BIONIC__
575 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
576 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
577#endif
578
579 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
580 ASSERT_TRUE(handle != nullptr) << dlerror();
581
582 typedef int (*fn_t) (void);
583 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
584 ASSERT_TRUE(fn != nullptr) << dlerror();
585 ASSERT_EQ(42, fn());
586
587 ASSERT_EQ(0, dlclose(handle));
588}
589
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800590TEST(dlfcn, check_unload_after_reloc) {
591 // This is how this one works:
592 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
593 // |
594 // +-> libtest_two_parents_child
595 //
596 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
597 // |
598 // +-> libtest_two_parents_child
599 //
600 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
601 // as a second step it dlopens parent2 and dlcloses parent1...
602
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700603 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
604 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800605
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700606 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
607 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800608
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700609 typedef int (*fn_t) (void);
610 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
611 ASSERT_TRUE(fn != nullptr) << dlerror();
612 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800613
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700614 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800615
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700616 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
617 ASSERT_TRUE(handle != nullptr);
618 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800619
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700620 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
621 ASSERT_TRUE(fn != nullptr) << dlerror();
622 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800623
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700624 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800625
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700626 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
627 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800628}
629
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700630extern "C" int check_order_reloc_root_get_answer_impl() {
631 return 42;
632}
633
634TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
635 // This is how this one works:
636 // we lookup and call get_answer3 which is defined in 'root.so'
637 // and in turn calls external root_get_answer_impl() defined in _2.so and
638 // above the correct _impl() is one in the executable.
639 //
640 // libtest_check_order_reloc_root.so
641 // |
642 // +-> ..._1.so <- empty
643 // |
644 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
645 //
646
647 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
648 ASSERT_TRUE(handle == nullptr);
649#ifdef __BIONIC__
650 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
651 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
652#endif
653
654 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
655 ASSERT_TRUE(handle != nullptr) << dlerror();
656
657 typedef int (*fn_t) (void);
658 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
659 ASSERT_TRUE(fn != nullptr) << dlerror();
660 ASSERT_EQ(42, fn());
661
662 ASSERT_EQ(0, dlclose(handle));
663}
664
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700665TEST(dlfcn, dlopen_check_rtld_local) {
666 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
667 ASSERT_TRUE(sym == nullptr);
668
669 // implicit RTLD_LOCAL
670 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
671 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
672 ASSERT_TRUE(sym == nullptr);
673 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
674 sym = dlsym(handle, "dlopen_testlib_simple_func");
675 ASSERT_TRUE(sym != nullptr);
676 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
677 dlclose(handle);
678
679 // explicit RTLD_LOCAL
680 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
681 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
682 ASSERT_TRUE(sym == nullptr);
683 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
684 sym = dlsym(handle, "dlopen_testlib_simple_func");
685 ASSERT_TRUE(sym != nullptr);
686 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
687 dlclose(handle);
688}
689
690TEST(dlfcn, dlopen_check_rtld_global) {
691 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
692 ASSERT_TRUE(sym == nullptr);
693
694 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700695 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700696 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
697 ASSERT_TRUE(sym != nullptr) << dlerror();
698 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
699 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700700
701 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
702 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
703 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700704
705 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
706 // shared libraries after symbol was not found in the main executable
707 // and dependent libraries.
708 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
709 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
710 ASSERT_TRUE(sym != nullptr) << dlerror();
711
712 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700713}
714
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700715// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
716// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
717// libtest_with_dependency_loop_a.so
718TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700719 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
720 ASSERT_TRUE(handle != nullptr) << dlerror();
721 void* f = dlsym(handle, "dlopen_test_loopy_function");
722 ASSERT_TRUE(f != nullptr) << dlerror();
723 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
724 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700725
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700726 // dlopen second time to make sure that the library was unloaded correctly
727 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
728 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800729#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700730 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 -0800731#else
732 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
733 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700734#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800735
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700736 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
737 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700738}
739
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700740TEST(dlfcn, dlopen_nodelete) {
741 static bool is_unloaded = false;
742
743 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
744 ASSERT_TRUE(handle != nullptr) << dlerror();
745 void (*set_unload_flag_ptr)(bool*);
746 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
747 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
748 set_unload_flag_ptr(&is_unloaded);
749
750 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
751 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
752 ASSERT_EQ(1729U, *taxicab_number);
753 *taxicab_number = 2;
754
755 dlclose(handle);
756 ASSERT_TRUE(!is_unloaded);
757
758 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
759 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
760 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
761
762
763 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
764 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
765 ASSERT_EQ(taxicab_number2, taxicab_number);
766
767 ASSERT_EQ(2U, *taxicab_number2);
768
769 dlclose(handle);
770 ASSERT_TRUE(!is_unloaded);
771}
772
773TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
774 static bool is_unloaded = false;
775
776 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
777 ASSERT_TRUE(handle != nullptr) << dlerror();
778 void (*set_unload_flag_ptr)(bool*);
779 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
780 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
781 set_unload_flag_ptr(&is_unloaded);
782
783 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
784 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
785
786 ASSERT_EQ(1729U, *taxicab_number);
787 *taxicab_number = 2;
788
789 // This RTLD_NODELETE should be ignored
790 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
791 ASSERT_TRUE(handle1 != nullptr) << dlerror();
792 ASSERT_EQ(handle, handle1);
793
794 dlclose(handle1);
795 dlclose(handle);
796
797 ASSERT_TRUE(is_unloaded);
798}
799
800TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
801 static bool is_unloaded = false;
802
803 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
804 ASSERT_TRUE(handle != nullptr) << dlerror();
805 void (*set_unload_flag_ptr)(bool*);
806 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
807 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
808 set_unload_flag_ptr(&is_unloaded);
809
810 dlclose(handle);
811 ASSERT_TRUE(!is_unloaded);
812}
813
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700814TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700815 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
816 ASSERT_TRUE(handle != nullptr) << dlerror();
817 int (*get_answer)();
818 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
819 ASSERT_TRUE(get_answer != nullptr) << dlerror();
820 ASSERT_EQ(42, get_answer());
821 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700822}
823
Elliott Hughese66190d2012-12-18 15:57:55 -0800824TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700825 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700826 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700827#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700828 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
829#else
830 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
831#endif
832}
833
dimitry109040c2017-11-03 13:49:07 +0100834TEST(dlfcn, dlclose_unload) {
835 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
836 ASSERT_TRUE(handle != nullptr) << dlerror();
837 uint32_t* taxicab_number = static_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
838 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
839 EXPECT_EQ(1729U, *taxicab_number);
840 dlclose(handle);
841 // Making sure that the library has been unmapped as part of library unload
842 // process. Note that mprotect somewhat counter-intuitively returns ENOMEM in
843 // this case.
844 uintptr_t page_start = reinterpret_cast<uintptr_t>(taxicab_number) & ~(PAGE_SIZE - 1);
845 ASSERT_TRUE(mprotect(reinterpret_cast<void*>(page_start), PAGE_SIZE, PROT_NONE) != 0);
846 ASSERT_EQ(ENOMEM, errno) << strerror(errno);
847}
848
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800849static void ConcurrentDlErrorFn(std::string& error) {
850 ASSERT_TRUE(dlerror() == nullptr);
851
852 void* handle = dlopen("/child/thread", RTLD_NOW);
853 ASSERT_TRUE(handle == nullptr);
854
855 const char* err = dlerror();
856 ASSERT_TRUE(err != nullptr);
857
858 error = err;
859}
860
861TEST(dlfcn, dlerror_concurrent_buffer) {
862 void* handle = dlopen("/main/thread", RTLD_NOW);
863 ASSERT_TRUE(handle == nullptr);
864 const char* main_thread_error = dlerror();
865 ASSERT_TRUE(main_thread_error != nullptr);
866 ASSERT_SUBSTR("/main/thread", main_thread_error);
867
868 std::string child_thread_error;
869 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
870 t.join();
871 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
872
873 // Check that main thread local buffer was not modified.
874 ASSERT_SUBSTR("/main/thread", main_thread_error);
Elliott Hughes5419b942012-10-16 15:54:46 -0700875}
876
Elliott Hughese66190d2012-12-18 15:57:55 -0800877TEST(dlfcn, dlerror_concurrent) {
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800878 void* handle = dlopen("/main/thread", RTLD_NOW);
879 ASSERT_TRUE(handle == nullptr);
880
881 std::string child_thread_error;
882 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
883 t.join();
884 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
885
Elliott Hughes5419b942012-10-16 15:54:46 -0700886 const char* main_thread_error = dlerror();
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800887 ASSERT_TRUE(main_thread_error != nullptr);
Elliott Hughes5419b942012-10-16 15:54:46 -0700888 ASSERT_SUBSTR("/main/thread", main_thread_error);
889}
890
Elliott Hughese66190d2012-12-18 15:57:55 -0800891TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700892 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700893 void* self = dlopen(nullptr, RTLD_NOW);
894 ASSERT_TRUE(self != nullptr);
895 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700896
897 void* sym;
898
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700899#if defined(__BIONIC__) && !defined(__LP64__)
900 // RTLD_DEFAULT in lp32 bionic is not (void*)0
901 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700902 sym = dlsym(nullptr, "test");
903 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800904 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700905#endif
906
Elliott Hughes3b297c42012-10-11 16:08:51 -0700907 // Symbol that doesn't exist.
908 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700909 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700910 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700911
912 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700913}
914
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700915TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700916 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700917 void* self = dlopen(nullptr, RTLD_NOW);
918 ASSERT_TRUE(self != nullptr);
919 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700920
921 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700922 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700923
924 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
925 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
926
927 Dl_info info;
928 int rc = dladdr(addr, &info);
929 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
930
931 // Get the name of this executable.
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700932 const std::string executable_path = android::base::GetExecutablePath();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700933
934 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700935 char dli_realpath[PATH_MAX];
936 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700937 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700938
939 // The symbol name should be the symbol we looked up.
940 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
941
942 // The address should be the exact address of the symbol.
943 ASSERT_EQ(info.dli_saddr, sym);
944
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700945 std::vector<map_record> maps;
946 ASSERT_TRUE(Maps::parse_maps(&maps));
947
948 void* base_address = nullptr;
949 for (const map_record& rec : maps) {
950 if (executable_path == rec.pathname) {
951 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700952 break;
953 }
954 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700955
956 // The base address should be the address we were loaded at.
957 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700958
959 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700960}
961
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700962TEST(dlfcn, dlopen_executable_by_absolute_path) {
963 void* handle1 = dlopen(nullptr, RTLD_NOW);
964 ASSERT_TRUE(handle1 != nullptr) << dlerror();
965
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700966 void* handle2 = dlopen(android::base::GetExecutablePath().c_str(), RTLD_NOW);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700967 ASSERT_TRUE(handle2 != nullptr) << dlerror();
968
969#if defined(__BIONIC__)
970 ASSERT_EQ(handle1, handle2);
971#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800972 GTEST_SKIP() << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
973 "it loads a separate copy of the main executable "
974 "on dlopen by absolute path";
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700975#endif
976}
977
Victor Khimenko14b9d712017-01-25 19:59:16 +0100978#if defined (__aarch64__)
979#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm64/"
980#elif defined (__arm__)
981#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm/"
982#elif defined (__i386__)
983#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86/"
984#elif defined (__x86_64__)
985#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86_64/"
986#elif defined (__mips__)
987#if defined(__LP64__)
988#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips64/"
989#else
990#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips/"
991#endif
992#else
993#error "Unknown architecture"
994#endif
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200995#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100996#define ALTERNATE_PATH_TO_LIBC ALTERNATE_PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700997
998TEST(dlfcn, dladdr_libc) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800999#if defined(__GLIBC__)
1000 GTEST_SKIP() << "glibc returns libc.so's ldconfig path, which is a symlink (not a realpath)";
1001#endif
1002
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001003 Dl_info info;
1004 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
1005 ASSERT_TRUE(dladdr(addr, &info) != 0);
1006
Dmitriy Ivanovef255922015-04-08 11:53:08 -07001007 char libc_realpath[PATH_MAX];
Victor Khimenko14b9d712017-01-25 19:59:16 +01001008
1009 // Check if libc is in canonical path or in alternate path.
1010 if (strncmp(ALTERNATE_PATH_TO_SYSTEM_LIB,
1011 info.dli_fname,
1012 sizeof(ALTERNATE_PATH_TO_SYSTEM_LIB) - 1) == 0) {
1013 // Platform with emulated architecture. Symlink on ARC++.
1014 ASSERT_TRUE(realpath(ALTERNATE_PATH_TO_LIBC, libc_realpath) == libc_realpath);
1015 } else {
1016 // /system/lib is symlink when this test is executed on host.
1017 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
1018 }
Dmitriy Ivanovef255922015-04-08 11:53:08 -07001019
1020 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001021 // TODO: add check for dfi_fbase
1022 ASSERT_STREQ("puts", info.dli_sname);
1023 ASSERT_EQ(addr, info.dli_saddr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001024}
1025
Elliott Hughese66190d2012-12-18 15:57:55 -08001026TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -07001027 Dl_info info;
1028
Elliott Hughes3b297c42012-10-11 16:08:51 -07001029 dlerror(); // Clear any pending errors.
1030
Elliott Hughes8e15b082012-09-26 11:44:01 -07001031 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001032 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
1033 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001034
1035 // No symbol corresponding to a stack address.
1036 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001037 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001038}
Elliott Hughes124fae92012-10-31 14:20:03 -07001039
Elliott Hughesa43e9062013-01-07 14:18:22 -08001040// GNU-style ELF hash tables are incompatible with the MIPS ABI.
1041// 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 -08001042TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001043#if !defined(__mips__)
Elliott Hughes124fae92012-10-31 14:20:03 -07001044 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001045 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
1046 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001047 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001048 void* sym = dlsym(handle, "getRandomNumber");
1049 ASSERT_TRUE(sym != nullptr) << dlerror();
1050 int (*fn)(void);
1051 fn = reinterpret_cast<int (*)(void)>(sym);
1052 EXPECT_EQ(4, fn());
1053
1054 Dl_info dlinfo;
1055 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1056
1057 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1058 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001059 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001060#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001061 GTEST_SKIP() << "mips toolchain does not support '--hash-style=gnu'";
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001062#endif
Elliott Hughes124fae92012-10-31 14:20:03 -07001063}
Elliott Hughese66190d2012-12-18 15:57:55 -08001064
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001065TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
1066 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
1067 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001068 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001069 void* sym = dlsym(handle, "getRandomNumber");
1070 ASSERT_TRUE(sym != nullptr) << dlerror();
1071 int (*fn)(void);
1072 fn = reinterpret_cast<int (*)(void)>(sym);
1073 EXPECT_EQ(4, fn());
1074
1075 Dl_info dlinfo;
1076 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1077
1078 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1079 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
1080 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
1081}
1082
Elliott Hughese66190d2012-12-18 15:57:55 -08001083TEST(dlfcn, dlopen_bad_flags) {
1084 dlerror(); // Clear any pending errors.
1085 void* handle;
1086
Elliott Hughes063525c2014-05-13 11:19:57 -07001087#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -08001088 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001089 handle = dlopen(nullptr, 0);
1090 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001091 ASSERT_SUBSTR("invalid", dlerror());
1092#endif
1093
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001094 handle = dlopen(nullptr, 0xffffffff);
1095 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001096 ASSERT_SUBSTR("invalid", dlerror());
1097
1098 // 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 -07001099 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
1100 ASSERT_TRUE(handle != nullptr);
1101 ASSERT_SUBSTR(nullptr, dlerror());
Elliott Hughese66190d2012-12-18 15:57:55 -08001102}
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001103
1104TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001105 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001106 ASSERT_TRUE(addr == nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001107}
1108
1109TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001110 void* addr = dlsym(RTLD_DEFAULT, "fopen");
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_unknown_symbol) {
1115 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001116 ASSERT_TRUE(addr == nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001117}
1118
1119TEST(dlfcn, rtld_next_known_symbol) {
1120 void* addr = dlsym(RTLD_NEXT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001121 ASSERT_TRUE(addr != nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001122}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001123
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001124// Check that RTLD_NEXT of a libc symbol works in dlopened library
1125TEST(dlfcn, rtld_next_from_library) {
dimitry153168c2018-02-20 16:51:41 +01001126 void* library_with_fclose = dlopen("libtest_check_rtld_next_from_library.so", RTLD_NOW | RTLD_GLOBAL);
Raj Mamadgi527229c2017-11-02 15:53:50 -07001127 ASSERT_TRUE(library_with_fclose != nullptr) << dlerror();
1128 void* expected_addr = dlsym(RTLD_DEFAULT, "fclose");
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001129 ASSERT_TRUE(expected_addr != nullptr) << dlerror();
Raj Mamadgi527229c2017-11-02 15:53:50 -07001130 typedef void* (*get_libc_fclose_ptr_fn_t)();
1131 get_libc_fclose_ptr_fn_t get_libc_fclose_ptr =
1132 reinterpret_cast<get_libc_fclose_ptr_fn_t>(dlsym(library_with_fclose, "get_libc_fclose_ptr"));
1133 ASSERT_TRUE(get_libc_fclose_ptr != nullptr) << dlerror();
1134 ASSERT_EQ(expected_addr, get_libc_fclose_ptr());
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001135
Raj Mamadgi527229c2017-11-02 15:53:50 -07001136 dlclose(library_with_fclose);
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001137}
1138
1139
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001140TEST(dlfcn, dlsym_weak_func) {
1141 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001142 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001143 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001144
1145 int (*weak_func)();
1146 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001147 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001148 EXPECT_EQ(42, weak_func());
1149 dlclose(handle);
1150}
1151
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001152TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001153 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
1154 ASSERT_TRUE(handle != nullptr) << dlerror();
1155 int (*weak_func)();
1156 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
1157 ASSERT_TRUE(weak_func != nullptr) << dlerror();
1158 EXPECT_EQ(6551, weak_func());
1159 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001160}
1161
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001162TEST(dlfcn, dlopen_symlink) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001163 DlfcnSymlink symlink("dlopen_symlink");
1164 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001165 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001166 void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001167 ASSERT_TRUE(handle1 != nullptr);
1168 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001169 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -07001170 dlclose(handle1);
1171 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001172}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001173
1174// libtest_dlopen_from_ctor_main.so depends on
1175// libtest_dlopen_from_ctor.so which has a constructor
1176// that calls dlopen(libc...). This is to test the situation
1177// described in b/7941716.
1178TEST(dlfcn, dlopen_dlopen_from_ctor) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001179#if defined(__GLIBC__)
1180 GTEST_SKIP() << "glibc segfaults if you try to call dlopen from a constructor";
1181#endif
1182
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001183 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1184 ASSERT_TRUE(handle != nullptr) << dlerror();
1185 dlclose(handle);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001186}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001187
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001188static std::string g_fini_call_order_str;
1189
1190static void register_fini_call(const char* s) {
1191 g_fini_call_order_str += s;
1192}
1193
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001194static void test_init_fini_call_order_for(const char* libname) {
1195 g_fini_call_order_str.clear();
1196 void* handle = dlopen(libname, RTLD_NOW);
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001197 ASSERT_TRUE(handle != nullptr) << dlerror();
1198 typedef int (*get_init_order_number_t)();
1199 get_init_order_number_t get_init_order_number =
1200 reinterpret_cast<get_init_order_number_t>(dlsym(handle, "get_init_order_number"));
1201 ASSERT_EQ(321, get_init_order_number());
1202
1203 typedef void (*set_fini_callback_t)(void (*f)(const char*));
1204 set_fini_callback_t set_fini_callback =
1205 reinterpret_cast<set_fini_callback_t>(dlsym(handle, "set_fini_callback"));
1206 set_fini_callback(register_fini_call);
1207 dlclose(handle);
1208 ASSERT_EQ("(root)(child)(grandchild)", g_fini_call_order_str);
1209}
1210
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001211TEST(dlfcn, init_fini_call_order) {
1212 test_init_fini_call_order_for("libtest_init_fini_order_root.so");
1213 test_init_fini_call_order_for("libtest_init_fini_order_root2.so");
1214}
1215
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001216TEST(dlfcn, symbol_versioning_use_v1) {
1217 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1218 ASSERT_TRUE(handle != nullptr) << dlerror();
1219 typedef int (*fn_t)();
1220 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1221 ASSERT_TRUE(fn != nullptr) << dlerror();
1222 ASSERT_EQ(1, fn());
1223 dlclose(handle);
1224}
1225
1226TEST(dlfcn, symbol_versioning_use_v2) {
1227 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1228 ASSERT_TRUE(handle != nullptr) << dlerror();
1229 typedef int (*fn_t)();
1230 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1231 ASSERT_TRUE(fn != nullptr) << dlerror();
1232 ASSERT_EQ(2, fn());
1233 dlclose(handle);
1234}
1235
1236TEST(dlfcn, symbol_versioning_use_other_v2) {
1237 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1238 ASSERT_TRUE(handle != nullptr) << dlerror();
1239 typedef int (*fn_t)();
1240 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1241 ASSERT_TRUE(fn != nullptr) << dlerror();
1242 ASSERT_EQ(20, fn());
1243 dlclose(handle);
1244}
1245
1246TEST(dlfcn, symbol_versioning_use_other_v3) {
1247 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1248 ASSERT_TRUE(handle != nullptr) << dlerror();
1249 typedef int (*fn_t)();
1250 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1251 ASSERT_TRUE(fn != nullptr) << dlerror();
1252 ASSERT_EQ(3, fn());
1253 dlclose(handle);
1254}
1255
1256TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1257 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1258 ASSERT_TRUE(handle != nullptr) << dlerror();
1259 typedef int (*fn_t)();
1260 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1261 ASSERT_TRUE(fn != nullptr) << dlerror();
1262 ASSERT_EQ(3, fn()); // the default version is 3
1263 dlclose(handle);
1264}
1265
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001266TEST(dlfcn, dlvsym_smoke) {
1267 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1268 ASSERT_TRUE(handle != nullptr) << dlerror();
1269 typedef int (*fn_t)();
1270
1271 {
1272 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1273 ASSERT_TRUE(fn == nullptr);
1274 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1275 }
1276
1277 {
1278 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1279 ASSERT_TRUE(fn != nullptr) << dlerror();
1280 ASSERT_EQ(2, fn());
1281 }
1282
1283 dlclose(handle);
1284}
1285
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001286// This preempts the implementation from libtest_versioned_lib.so
1287extern "C" int version_zero_function() {
1288 return 0;
1289}
1290
1291// This preempts the implementation from libtest_versioned_uselibv*.so
1292extern "C" int version_zero_function2() {
1293 return 0;
1294}
Evgenii Stepanov68650822015-06-10 13:38:39 -07001295
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001296TEST(dlfcn, dt_runpath_smoke) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001297 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1298 ASSERT_TRUE(handle != nullptr) << dlerror();
1299
1300 typedef void *(* dlopen_b_fn)();
1301 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1302 ASSERT_TRUE(fn != nullptr) << dlerror();
1303
1304 void *p = fn();
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001305 ASSERT_TRUE(p != nullptr);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001306
1307 dlclose(handle);
1308}
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001309
Dimitry Ivanovdb6ab3d2017-06-27 11:02:51 -07001310TEST(dlfcn, dt_runpath_absolute_path) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001311 std::string libpath = GetTestlibRoot() + "/libtest_dt_runpath_d.so";
Dimitry Ivanovdb6ab3d2017-06-27 11:02:51 -07001312 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1313 ASSERT_TRUE(handle != nullptr) << dlerror();
1314
1315 typedef void *(* dlopen_b_fn)();
1316 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1317 ASSERT_TRUE(fn != nullptr) << dlerror();
1318
1319 void *p = fn();
1320 ASSERT_TRUE(p != nullptr);
1321
1322 dlclose(handle);
1323}
1324
dimitry55547db2018-05-25 14:17:37 +02001325static void test_dlclose_after_thread_local_dtor(const char* library_name) {
dimitry06016f22018-01-05 11:39:28 +01001326 bool is_dtor_triggered = false;
1327
1328 auto f = [](void* handle, bool* is_dtor_triggered) {
1329 typedef void (*fn_t)(bool*);
1330 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1331 ASSERT_TRUE(fn != nullptr) << dlerror();
1332
1333 fn(is_dtor_triggered);
1334
1335 ASSERT_TRUE(!*is_dtor_triggered);
1336 };
1337
dimitry55547db2018-05-25 14:17:37 +02001338 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001339 ASSERT_TRUE(handle == nullptr);
1340
dimitry55547db2018-05-25 14:17:37 +02001341 handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001342 ASSERT_TRUE(handle != nullptr) << dlerror();
1343
1344 std::thread t(f, handle, &is_dtor_triggered);
1345 t.join();
1346
1347 ASSERT_TRUE(is_dtor_triggered);
1348 dlclose(handle);
1349
dimitry55547db2018-05-25 14:17:37 +02001350 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001351 ASSERT_TRUE(handle == nullptr);
1352}
1353
dimitry55547db2018-05-25 14:17:37 +02001354TEST(dlfcn, dlclose_after_thread_local_dtor) {
1355 test_dlclose_after_thread_local_dtor("libtest_thread_local_dtor.so");
1356}
1357
1358TEST(dlfcn, dlclose_after_thread_local_dtor_indirect) {
1359 test_dlclose_after_thread_local_dtor("libtest_indirect_thread_local_dtor.so");
1360}
1361
1362static void test_dlclose_before_thread_local_dtor(const char* library_name) {
dimitry06016f22018-01-05 11:39:28 +01001363 bool is_dtor_triggered = false;
1364
dimitry55547db2018-05-25 14:17:37 +02001365 auto f = [library_name](bool* is_dtor_triggered) {
1366 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001367 ASSERT_TRUE(handle == nullptr);
1368
dimitry55547db2018-05-25 14:17:37 +02001369 handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001370 ASSERT_TRUE(handle != nullptr) << dlerror();
1371
1372 typedef void (*fn_t)(bool*);
1373 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1374 ASSERT_TRUE(fn != nullptr) << dlerror();
1375
1376 fn(is_dtor_triggered);
1377
1378 dlclose(handle);
1379
1380 ASSERT_TRUE(!*is_dtor_triggered);
1381
1382 // Since we have thread_atexit dtors associated with handle - the library should
1383 // still be availabe.
dimitry55547db2018-05-25 14:17:37 +02001384 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001385 ASSERT_TRUE(handle != nullptr) << dlerror();
1386 dlclose(handle);
1387 };
1388
dimitry55547db2018-05-25 14:17:37 +02001389 void* handle = dlopen(library_name, RTLD_NOW);
dimitry06016f22018-01-05 11:39:28 +01001390 ASSERT_TRUE(handle != nullptr) << dlerror();
1391 dlclose(handle);
1392
dimitry55547db2018-05-25 14:17:37 +02001393 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001394 ASSERT_TRUE(handle == nullptr);
1395
1396 std::thread t(f, &is_dtor_triggered);
1397 t.join();
1398#if defined(__BIONIC__)
1399 // ld-android.so unloads unreferenced libraries on pthread_exit()
1400 ASSERT_TRUE(is_dtor_triggered);
dimitry55547db2018-05-25 14:17:37 +02001401 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001402 ASSERT_TRUE(handle == nullptr);
1403#else
1404 // GLIBC does not unload libraries with ref_count = 0 on pthread_exit
1405 ASSERT_TRUE(is_dtor_triggered);
dimitry55547db2018-05-25 14:17:37 +02001406 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1407 ASSERT_TRUE(handle != nullptr) << dlerror();
1408#endif
1409}
1410
1411TEST(dlfcn, dlclose_before_thread_local_dtor) {
1412 test_dlclose_before_thread_local_dtor("libtest_thread_local_dtor.so");
1413}
1414
1415TEST(dlfcn, dlclose_before_thread_local_dtor_indirect) {
1416 test_dlclose_before_thread_local_dtor("libtest_indirect_thread_local_dtor.so");
1417}
1418
1419TEST(dlfcn, dlclose_before_thread_local_dtor_multiple_dsos) {
1420 const constexpr char* library_name = "libtest_indirect_thread_local_dtor.so";
1421
1422 bool is_dtor1_triggered = false;
1423 bool is_dtor2_triggered = false;
1424
1425 std::mutex mtx;
1426 std::condition_variable cv;
1427 void* library_handle = nullptr;
1428 bool thread1_dlopen_complete = false;
1429 bool thread2_thread_local_dtor_initialized = false;
1430 bool thread1_complete = false;
1431
1432 auto f1 = [&]() {
1433 void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1434 ASSERT_TRUE(handle == nullptr);
1435
1436 handle = dlopen(library_name, RTLD_NOW);
1437 ASSERT_TRUE(handle != nullptr) << dlerror();
1438 std::unique_lock<std::mutex> lock(mtx);
1439 thread1_dlopen_complete = true;
1440 library_handle = handle;
1441 lock.unlock();
1442 cv.notify_one();
1443
1444 typedef void (*fn_t)(bool*);
1445 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1446 ASSERT_TRUE(fn != nullptr) << dlerror();
1447
1448 fn(&is_dtor1_triggered);
1449
1450 lock.lock();
1451 cv.wait(lock, [&] { return thread2_thread_local_dtor_initialized; });
1452 lock.unlock();
1453
1454 dlclose(handle);
1455
1456 ASSERT_TRUE(!is_dtor1_triggered);
1457
1458 // Since we have thread_atexit dtors associated with handle - the library should
1459 // still be availabe.
1460 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1461 ASSERT_TRUE(handle != nullptr) << dlerror();
1462 dlclose(handle);
1463 };
1464
1465 auto f2 = [&]() {
1466 std::unique_lock<std::mutex> lock(mtx);
1467 cv.wait(lock, [&] { return thread1_dlopen_complete; });
1468 void* handle = library_handle;
1469 lock.unlock();
1470
1471 typedef void (*fn_t)(bool*);
1472 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable2"));
1473 ASSERT_TRUE(fn != nullptr) << dlerror();
1474
1475 fn(&is_dtor2_triggered);
1476
1477 lock.lock();
1478 thread2_thread_local_dtor_initialized = true;
1479 lock.unlock();
1480 cv.notify_one();
1481
1482 lock.lock();
1483 cv.wait(lock, [&] { return thread1_complete; });
1484 lock.unlock();
1485
1486 ASSERT_TRUE(!is_dtor2_triggered);
1487 };
1488
1489 void* handle = dlopen(library_name, RTLD_NOW);
1490 ASSERT_TRUE(handle != nullptr) << dlerror();
1491 dlclose(handle);
1492
1493 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1494 ASSERT_TRUE(handle == nullptr);
1495
1496 std::thread t1(f1);
1497 std::thread t2(f2);
1498 t1.join();
1499 ASSERT_TRUE(is_dtor1_triggered);
1500 ASSERT_TRUE(!is_dtor2_triggered);
1501
1502 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1503 ASSERT_TRUE(handle != nullptr) << dlerror();
1504 dlclose(handle);
1505
1506 std::unique_lock<std::mutex> lock(mtx);
1507 thread1_complete = true;
1508 lock.unlock();
1509 cv.notify_one();
1510
1511 t2.join();
1512 ASSERT_TRUE(is_dtor2_triggered);
1513
1514#if defined(__BIONIC__)
1515 // ld-android.so unloads unreferenced libraries on pthread_exit()
1516 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
1517 ASSERT_TRUE(handle == nullptr);
1518#else
1519 // GLIBC does not unload libraries with ref_count = 0 on pthread_exit
1520 handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD);
dimitry06016f22018-01-05 11:39:28 +01001521 ASSERT_TRUE(handle != nullptr) << dlerror();
1522#endif
1523}
1524
Elliott Hughes8ad40932017-06-15 15:12:29 -07001525TEST(dlfcn, RTLD_macros) {
1526#if !defined(RTLD_LOCAL)
1527#error no RTLD_LOCAL
1528#elif !defined(RTLD_LAZY)
1529#error no RTLD_LAZY
1530#elif !defined(RTLD_NOW)
1531#error no RTLD_NOW
1532#elif !defined(RTLD_NOLOAD)
1533#error no RTLD_NOLOAD
1534#elif !defined(RTLD_GLOBAL)
1535#error no RTLD_GLOBAL
1536#elif !defined(RTLD_NODELETE)
1537#error no RTLD_NODELETE
1538#endif
1539}
1540
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001541// Bionic specific tests
1542#if defined(__BIONIC__)
1543
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001544#if defined(__arm__)
1545const llvm::ELF::Elf32_Dyn* to_dynamic_table(const char* p) {
1546 return reinterpret_cast<const llvm::ELF::Elf32_Dyn*>(p);
1547}
1548
1549// Duplicate these definitions here because they are android specific
Ryan Prichard68140282018-05-23 14:20:29 -07001550// - note that we cannot include <elf.h> because #defines conflict with
1551// enum names provided by LLVM.
1552// - we also don't use llvm::ELF::DT_LOOS because its value is 0x60000000
1553// rather than the 0x6000000d we expect
1554#define DT_LOOS 0x6000000d
1555#define DT_ANDROID_REL (DT_LOOS + 2)
1556#define DT_ANDROID_RELA (DT_LOOS + 4)
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001557
1558template<typename ELFT>
Ryan Prichard470b6662018-03-27 22:10:55 -07001559void validate_compatibility_of_native_library(const std::string& soname,
1560 const std::string& path, ELFT* elf) {
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001561 bool has_elf_hash = false;
1562 bool has_android_rel = false;
1563 bool has_rel = false;
1564 // Find dynamic section and check that DT_HASH and there is no DT_ANDROID_REL
1565 for (auto it = elf->section_begin(); it != elf->section_end(); ++it) {
1566 const llvm::object::ELFSectionRef& section_ref = *it;
1567 if (section_ref.getType() == llvm::ELF::SHT_DYNAMIC) {
1568 llvm::StringRef data;
1569 ASSERT_TRUE(!it->getContents(data)) << "unable to get SHT_DYNAMIC section data";
1570 for (auto d = to_dynamic_table(data.data()); d->d_tag != llvm::ELF::DT_NULL; ++d) {
1571 if (d->d_tag == llvm::ELF::DT_HASH) {
1572 has_elf_hash = true;
1573 } else if (d->d_tag == DT_ANDROID_REL || d->d_tag == DT_ANDROID_RELA) {
1574 has_android_rel = true;
1575 } else if (d->d_tag == llvm::ELF::DT_REL || d->d_tag == llvm::ELF::DT_RELA) {
1576 has_rel = true;
1577 }
1578 }
1579
1580 break;
1581 }
1582 }
1583
1584 ASSERT_TRUE(has_elf_hash) << path.c_str() << ": missing elf hash (DT_HASH)";
1585 ASSERT_TRUE(!has_android_rel) << path.c_str() << ": has packed relocations";
Ryan Prichard470b6662018-03-27 22:10:55 -07001586 // libdl.so is simple enough that it might not have any relocations, so
1587 // exempt it from the DT_REL/DT_RELA check.
1588 if (soname != "libdl.so") {
1589 ASSERT_TRUE(has_rel) << path.c_str() << ": missing DT_REL/DT_RELA";
1590 }
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001591}
1592
Ryan Prichard470b6662018-03-27 22:10:55 -07001593void validate_compatibility_of_native_library(const std::string& soname) {
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001594 // On the systems with emulation system libraries would be of different
1595 // architecture. Try to use alternate paths first.
1596 std::string path = std::string(ALTERNATE_PATH_TO_SYSTEM_LIB) + soname;
1597 auto binary_or_error = llvm::object::createBinary(path);
1598 if (!binary_or_error) {
1599 path = std::string(PATH_TO_SYSTEM_LIB) + soname;
1600 binary_or_error = llvm::object::createBinary(path);
1601 }
1602 ASSERT_FALSE(!binary_or_error);
1603
1604 llvm::object::Binary* binary = binary_or_error.get().getBinary();
1605
1606 auto obj = llvm::dyn_cast<llvm::object::ObjectFile>(binary);
1607 ASSERT_TRUE(obj != nullptr);
1608
1609 auto elf = llvm::dyn_cast<llvm::object::ELF32LEObjectFile>(obj);
1610
1611 ASSERT_TRUE(elf != nullptr);
1612
Ryan Prichard470b6662018-03-27 22:10:55 -07001613 validate_compatibility_of_native_library(soname, path, elf);
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001614}
1615
1616// This is a test for app compatibility workaround for arm apps
1617// affected by http://b/24465209
1618TEST(dlext, compat_elf_hash_and_relocation_tables) {
1619 validate_compatibility_of_native_library("libc.so");
1620 validate_compatibility_of_native_library("liblog.so");
1621 validate_compatibility_of_native_library("libstdc++.so");
1622 validate_compatibility_of_native_library("libdl.so");
1623 validate_compatibility_of_native_library("libm.so");
1624 validate_compatibility_of_native_library("libz.so");
1625 validate_compatibility_of_native_library("libjnigraphics.so");
1626}
1627
1628#endif // defined(__arm__)
1629
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001630TEST(dlfcn, dlopen_invalid_rw_load_segment) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001631 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001632 "/" + kPrebuiltElfDir +
1633 "/libtest_invalid-rw_load_segment.so";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001634 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1635 ASSERT_TRUE(handle == nullptr);
Elliott Hughes9076b0c2018-02-28 11:29:45 -08001636 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W+E load segments are not allowed";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001637 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1638}
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001639
1640TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001641 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001642 "/" + kPrebuiltElfDir +
1643 "/libtest_invalid-unaligned_shdr_offset.so";
1644
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001645 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1646 ASSERT_TRUE(handle == nullptr);
1647 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
1648 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1649}
1650
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001651TEST(dlfcn, dlopen_invalid_zero_shentsize) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001652 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001653 "/" + kPrebuiltElfDir +
1654 "/libtest_invalid-zero_shentsize.so";
1655
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001656 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1657 ASSERT_TRUE(handle == nullptr);
1658 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x";
1659 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1660}
1661
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001662TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001663 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001664 "/" + kPrebuiltElfDir +
1665 "/libtest_invalid-zero_shstrndx.so";
1666
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001667 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1668 ASSERT_TRUE(handle == nullptr);
1669 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx";
1670 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1671}
1672
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001673TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001674 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001675 "/" + kPrebuiltElfDir +
1676 "/libtest_invalid-empty_shdr_table.so";
1677
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001678 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1679 ASSERT_TRUE(handle == nullptr);
1680 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers";
1681 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1682}
1683
Dimitry Ivanov46230442016-08-15 13:40:53 -07001684TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001685 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001686 "/" + kPrebuiltElfDir +
1687 "/libtest_invalid-zero_shdr_table_offset.so";
1688
Dimitry Ivanov46230442016-08-15 13:40:53 -07001689 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1690 ASSERT_TRUE(handle == nullptr);
1691 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/";
1692 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1693}
1694
Dimitry Ivanov55958342016-08-15 14:06:04 -07001695TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001696 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001697 "/" + kPrebuiltElfDir +
1698 "/libtest_invalid-zero_shdr_table_content.so";
1699
Dimitry Ivanov55958342016-08-15 14:06:04 -07001700 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1701 ASSERT_TRUE(handle == nullptr);
1702 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
1703 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1704}
1705
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001706TEST(dlfcn, dlopen_invalid_textrels) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001707 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001708 "/" + kPrebuiltElfDir +
1709 "/libtest_invalid-textrels.so";
1710
1711 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1712 ASSERT_TRUE(handle == nullptr);
1713 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1714 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1715}
1716
1717TEST(dlfcn, dlopen_invalid_textrels2) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001718 const std::string libpath = GetTestlibRoot() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001719 "/" + kPrebuiltElfDir +
1720 "/libtest_invalid-textrels2.so";
1721
1722 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1723 ASSERT_TRUE(handle == nullptr);
1724 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1725 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1726}
1727
Jiyong Park01162f22017-10-16 15:31:09 +09001728TEST(dlfcn, dlopen_df_1_global) {
1729 void* handle = dlopen("libtest_dlopen_df_1_global.so", RTLD_NOW);
1730 ASSERT_TRUE(handle != nullptr) << dlerror();
1731}
1732
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001733#endif