blob: 46f5097a7c75ada5e5c6061168ca4408eea418ed [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
Elliott Hughesda1bb112018-02-16 10:05:08 -0800259
260 const char* vdso_name = "linux-vdso.so.1";
261#if defined(__i386__)
262 vdso_name = "linux-gate.so.1";
263#endif
264 void* handle = dlopen(vdso_name, RTLD_NOW);
dimitryc18de1b2017-09-26 14:31:35 +0200265 ASSERT_TRUE(handle != nullptr) << dlerror();
266 dlclose(handle);
267}
268
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700269// mips doesn't support ifuncs
270#if !defined(__mips__)
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700271TEST(dlfcn, ifunc_variable) {
272 typedef const char* (*fn_ptr)();
273
274 // ifunc's choice depends on whether IFUNC_CHOICE has a value
275 // first check the set case
276 setenv("IFUNC_CHOICE", "set", 1);
277 // preload libtest_ifunc_variable_impl.so
278 void* handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
279 void* handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
280 ASSERT_TRUE(handle != nullptr) << dlerror();
281 const char** foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
282 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
283 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
284 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
285 ASSERT_EQ(strncmp("set", *foo_ptr, 3), 0);
286 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
287 dlclose(handle);
288 dlclose(handle_impl);
289
290 // then check the unset case
291 unsetenv("IFUNC_CHOICE");
292 handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
293 handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
294 ASSERT_TRUE(handle != nullptr) << dlerror();
295 foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
296 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
297 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
298 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
299 ASSERT_EQ(strncmp("unset", *foo_ptr, 5), 0);
300 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
301 dlclose(handle);
302 dlclose(handle_impl);
303}
304
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700305TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700306 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700307
308 // ifunc's choice depends on whether IFUNC_CHOICE has a value
309 // first check the set case
310 setenv("IFUNC_CHOICE", "set", 1);
311 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700312 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700313 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
314 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700315 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
316 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700317 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
318 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700319 dlclose(handle);
320
321 // then check the unset case
322 unsetenv("IFUNC_CHOICE");
323 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700324 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700325 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
326 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700327 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
328 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700329 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700330 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700331 dlclose(handle);
332}
333
334TEST(dlfcn, ifunc_ctor_call) {
335 typedef const char* (*fn_ptr)();
336
337 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700338 ASSERT_TRUE(handle != nullptr) << dlerror();
339 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
340 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
341 ASSERT_STREQ("false", is_ctor_called());
342
343 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
344 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700345 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700346 dlclose(handle);
347}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700348
349TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
350 typedef const char* (*fn_ptr)();
351
352 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
353 ASSERT_TRUE(handle != nullptr) << dlerror();
354 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
355 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
356 ASSERT_STREQ("false", is_ctor_called());
357
358 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
359 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
360 ASSERT_STREQ("true", is_ctor_called());
361 dlclose(handle);
362}
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700363#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700364
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700365TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
366 // This is the structure of the test library and
367 // its dt_needed libraries
368 // libtest_relo_check_dt_needed_order.so
369 // |
370 // +-> libtest_relo_check_dt_needed_order_1.so
371 // |
372 // +-> libtest_relo_check_dt_needed_order_2.so
373 //
374 // The root library references relo_test_get_answer_lib - which is defined
375 // in both dt_needed libraries, the correct relocation should
376 // use the function defined in libtest_relo_check_dt_needed_order_1.so
377 void* handle = nullptr;
Tom Cherryb8ab6182017-04-05 16:20:29 -0700378 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700379
380 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
381 ASSERT_TRUE(handle != nullptr) << dlerror();
382
383 typedef int (*fn_t) (void);
384 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
385 ASSERT_TRUE(fn != nullptr) << dlerror();
386 ASSERT_EQ(1, fn());
387}
388
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700389TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700390 // Here is how the test library and its dt_needed
391 // libraries are arranged
392 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700393 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700394 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700395 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700396 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700397 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700398 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700399 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700400 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700401 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700402 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700403 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700404 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700405 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700406 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700407 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700408 //
409 // load order should be (1, 2, 3, a, b, d)
410 //
411 // get_answer() is defined in (2, 3, a, b, c)
412 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700413 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700414 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700415 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
416 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700417 typedef int (*fn_t) (void);
418 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700419 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700420 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700421 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700422 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700423
424 ASSERT_EQ(42, fn());
425 ASSERT_EQ(43, fn2());
426 dlclose(handle);
427}
428
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700429TEST(dlfcn, dlopen_check_order_reloc_siblings) {
430 // This is how this one works:
431 // we lookup and call get_answer which is defined in '_2.so'
432 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
433 // the correct _impl() is implemented by '_a.so';
434 //
435 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
436 //
437 // Here is the picture:
438 //
439 // libtest_check_order_reloc_siblings.so
440 // |
441 // +-> ..._1.so <- empty
442 // | |
443 // | +-> ..._a.so <- exports correct answer_impl()
444 // | |
445 // | +-> ..._b.so <- every other letter exporting incorrect one.
446 // |
447 // +-> ..._2.so <- empty
448 // | |
449 // | +-> ..._c.so
450 // | |
451 // | +-> ..._d.so
452 // |
453 // +-> ..._3.so <- empty
454 // |
455 // +-> ..._e.so
456 // |
457 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
458 // implements incorrect get_answer_impl()
459
460 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
461 ASSERT_TRUE(handle == nullptr);
462#ifdef __BIONIC__
463 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
464 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
465#endif
466
467 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
468 ASSERT_TRUE(handle != nullptr) << dlerror();
469
470 typedef int (*fn_t) (void);
471 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
472 ASSERT_TRUE(fn != nullptr) << dlerror();
473 ASSERT_EQ(42, fn());
474
475 ASSERT_EQ(0, dlclose(handle));
476}
477
478TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
479 // This test uses the same library as dlopen_check_order_reloc_siblings.
480 // Unlike dlopen_check_order_reloc_siblings it preloads
481 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
482 // dlopen(libtest_check_order_reloc_siblings.so)
483
484 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
485 ASSERT_TRUE(handle == nullptr);
486 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
487 ASSERT_TRUE(handle == nullptr);
488
489 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
490 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
491
492 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
493 ASSERT_TRUE(handle != nullptr) << dlerror();
494
495 ASSERT_EQ(0, dlclose(handle_for_1));
496
497 typedef int (*fn_t) (void);
498 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
499 ASSERT_TRUE(fn != nullptr) << dlerror();
500 ASSERT_EQ(42, fn());
501
502 ASSERT_EQ(0, dlclose(handle));
503}
504
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800505TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
506 // This is how this one works:
507 // we lookup and call grandchild_get_answer which is defined in '_2.so'
508 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
509 // the correct _impl() is implemented by '_c_1.so';
510 //
511 // Here is the picture of subtree:
512 //
513 // libtest_check_order_reloc_siblings.so
514 // |
515 // +-> ..._2.so <- grandchild_get_answer()
516 // |
517 // +-> ..._c.so <- empty
518 // | |
519 // | +-> _c_1.so <- exports correct answer_impl()
520 // | |
521 // | +-> _c_2.so <- exports incorrect answer_impl()
522 // |
523 // +-> ..._d.so <- empty
524
525 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
526 ASSERT_TRUE(handle == nullptr);
527#ifdef __BIONIC__
528 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
529 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
530#endif
531
532 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
533 ASSERT_TRUE(handle != nullptr) << dlerror();
534
535 typedef int (*fn_t) (void);
536 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
537 ASSERT_TRUE(fn != nullptr) << dlerror();
538 ASSERT_EQ(42, fn());
539
540 ASSERT_EQ(0, dlclose(handle));
541}
542
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700543TEST(dlfcn, dlopen_check_order_reloc_nephew) {
544 // This is how this one works:
545 // we lookup and call nephew_get_answer which is defined in '_2.so'
546 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
547 // the correct _impl() is implemented by '_a.so';
548 //
549 // Here is the picture:
550 //
551 // libtest_check_order_reloc_siblings.so
552 // |
553 // +-> ..._1.so <- empty
554 // | |
555 // | +-> ..._a.so <- exports correct answer_impl()
556 // | |
557 // | +-> ..._b.so <- every other letter exporting incorrect one.
558 // |
559 // +-> ..._2.so <- empty
560 // | |
561 // | +-> ..._c.so
562 // | |
563 // | +-> ..._d.so
564 // |
565 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
566 // |
567 // +-> ..._e.so
568 // |
569 // +-> ..._f.so
570
571 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
572 ASSERT_TRUE(handle == nullptr);
573#ifdef __BIONIC__
574 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
575 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
576#endif
577
578 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
579 ASSERT_TRUE(handle != nullptr) << dlerror();
580
581 typedef int (*fn_t) (void);
582 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
583 ASSERT_TRUE(fn != nullptr) << dlerror();
584 ASSERT_EQ(42, fn());
585
586 ASSERT_EQ(0, dlclose(handle));
587}
588
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800589TEST(dlfcn, check_unload_after_reloc) {
590 // This is how this one works:
591 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
592 // |
593 // +-> libtest_two_parents_child
594 //
595 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
596 // |
597 // +-> libtest_two_parents_child
598 //
599 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
600 // as a second step it dlopens parent2 and dlcloses parent1...
601
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700602 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
603 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800604
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700605 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
606 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800607
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700608 typedef int (*fn_t) (void);
609 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
610 ASSERT_TRUE(fn != nullptr) << dlerror();
611 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800612
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700613 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800614
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700615 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
616 ASSERT_TRUE(handle != nullptr);
617 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800618
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700619 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
620 ASSERT_TRUE(fn != nullptr) << dlerror();
621 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800622
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700623 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800624
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700625 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
626 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800627}
628
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700629extern "C" int check_order_reloc_root_get_answer_impl() {
630 return 42;
631}
632
633TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
634 // This is how this one works:
635 // we lookup and call get_answer3 which is defined in 'root.so'
636 // and in turn calls external root_get_answer_impl() defined in _2.so and
637 // above the correct _impl() is one in the executable.
638 //
639 // libtest_check_order_reloc_root.so
640 // |
641 // +-> ..._1.so <- empty
642 // |
643 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
644 //
645
646 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
647 ASSERT_TRUE(handle == nullptr);
648#ifdef __BIONIC__
649 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
650 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
651#endif
652
653 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
654 ASSERT_TRUE(handle != nullptr) << dlerror();
655
656 typedef int (*fn_t) (void);
657 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
658 ASSERT_TRUE(fn != nullptr) << dlerror();
659 ASSERT_EQ(42, fn());
660
661 ASSERT_EQ(0, dlclose(handle));
662}
663
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700664TEST(dlfcn, dlopen_check_rtld_local) {
665 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
666 ASSERT_TRUE(sym == nullptr);
667
668 // implicit RTLD_LOCAL
669 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
670 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
671 ASSERT_TRUE(sym == nullptr);
672 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
673 sym = dlsym(handle, "dlopen_testlib_simple_func");
674 ASSERT_TRUE(sym != nullptr);
675 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
676 dlclose(handle);
677
678 // explicit RTLD_LOCAL
679 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
680 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
681 ASSERT_TRUE(sym == nullptr);
682 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
683 sym = dlsym(handle, "dlopen_testlib_simple_func");
684 ASSERT_TRUE(sym != nullptr);
685 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
686 dlclose(handle);
687}
688
689TEST(dlfcn, dlopen_check_rtld_global) {
690 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
691 ASSERT_TRUE(sym == nullptr);
692
693 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700694 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700695 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
696 ASSERT_TRUE(sym != nullptr) << dlerror();
697 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
698 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700699
700 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
701 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
702 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700703
704 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
705 // shared libraries after symbol was not found in the main executable
706 // and dependent libraries.
707 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
708 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
709 ASSERT_TRUE(sym != nullptr) << dlerror();
710
711 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700712}
713
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700714// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
715// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
716// libtest_with_dependency_loop_a.so
717TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700718 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
719 ASSERT_TRUE(handle != nullptr) << dlerror();
720 void* f = dlsym(handle, "dlopen_test_loopy_function");
721 ASSERT_TRUE(f != nullptr) << dlerror();
722 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
723 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700724
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700725 // dlopen second time to make sure that the library was unloaded correctly
726 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
727 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800728#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700729 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 -0800730#else
731 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
732 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700733#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800734
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700735 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
736 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700737}
738
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700739TEST(dlfcn, dlopen_nodelete) {
740 static bool is_unloaded = false;
741
742 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
743 ASSERT_TRUE(handle != nullptr) << dlerror();
744 void (*set_unload_flag_ptr)(bool*);
745 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
746 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
747 set_unload_flag_ptr(&is_unloaded);
748
749 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
750 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
751 ASSERT_EQ(1729U, *taxicab_number);
752 *taxicab_number = 2;
753
754 dlclose(handle);
755 ASSERT_TRUE(!is_unloaded);
756
757 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
758 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
759 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
760
761
762 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
763 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
764 ASSERT_EQ(taxicab_number2, taxicab_number);
765
766 ASSERT_EQ(2U, *taxicab_number2);
767
768 dlclose(handle);
769 ASSERT_TRUE(!is_unloaded);
770}
771
772TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
773 static bool is_unloaded = false;
774
775 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
776 ASSERT_TRUE(handle != nullptr) << dlerror();
777 void (*set_unload_flag_ptr)(bool*);
778 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
779 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
780 set_unload_flag_ptr(&is_unloaded);
781
782 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
783 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
784
785 ASSERT_EQ(1729U, *taxicab_number);
786 *taxicab_number = 2;
787
788 // This RTLD_NODELETE should be ignored
789 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
790 ASSERT_TRUE(handle1 != nullptr) << dlerror();
791 ASSERT_EQ(handle, handle1);
792
793 dlclose(handle1);
794 dlclose(handle);
795
796 ASSERT_TRUE(is_unloaded);
797}
798
799TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
800 static bool is_unloaded = false;
801
802 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
803 ASSERT_TRUE(handle != nullptr) << dlerror();
804 void (*set_unload_flag_ptr)(bool*);
805 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
806 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
807 set_unload_flag_ptr(&is_unloaded);
808
809 dlclose(handle);
810 ASSERT_TRUE(!is_unloaded);
811}
812
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700813TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700814 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
815 ASSERT_TRUE(handle != nullptr) << dlerror();
816 int (*get_answer)();
817 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
818 ASSERT_TRUE(get_answer != nullptr) << dlerror();
819 ASSERT_EQ(42, get_answer());
820 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700821}
822
Elliott Hughese66190d2012-12-18 15:57:55 -0800823TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700824 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700825 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700826#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700827 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
828#else
829 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
830#endif
831}
832
dimitry109040c2017-11-03 13:49:07 +0100833TEST(dlfcn, dlclose_unload) {
834 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
835 ASSERT_TRUE(handle != nullptr) << dlerror();
836 uint32_t* taxicab_number = static_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
837 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
838 EXPECT_EQ(1729U, *taxicab_number);
839 dlclose(handle);
840 // Making sure that the library has been unmapped as part of library unload
841 // process. Note that mprotect somewhat counter-intuitively returns ENOMEM in
842 // this case.
843 uintptr_t page_start = reinterpret_cast<uintptr_t>(taxicab_number) & ~(PAGE_SIZE - 1);
844 ASSERT_TRUE(mprotect(reinterpret_cast<void*>(page_start), PAGE_SIZE, PROT_NONE) != 0);
845 ASSERT_EQ(ENOMEM, errno) << strerror(errno);
846}
847
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800848static void ConcurrentDlErrorFn(std::string& error) {
849 ASSERT_TRUE(dlerror() == nullptr);
850
851 void* handle = dlopen("/child/thread", RTLD_NOW);
852 ASSERT_TRUE(handle == nullptr);
853
854 const char* err = dlerror();
855 ASSERT_TRUE(err != nullptr);
856
857 error = err;
858}
859
860TEST(dlfcn, dlerror_concurrent_buffer) {
861 void* handle = dlopen("/main/thread", RTLD_NOW);
862 ASSERT_TRUE(handle == nullptr);
863 const char* main_thread_error = dlerror();
864 ASSERT_TRUE(main_thread_error != nullptr);
865 ASSERT_SUBSTR("/main/thread", main_thread_error);
866
867 std::string child_thread_error;
868 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
869 t.join();
870 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
871
872 // Check that main thread local buffer was not modified.
873 ASSERT_SUBSTR("/main/thread", main_thread_error);
Elliott Hughes5419b942012-10-16 15:54:46 -0700874}
875
Elliott Hughese66190d2012-12-18 15:57:55 -0800876TEST(dlfcn, dlerror_concurrent) {
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800877 void* handle = dlopen("/main/thread", RTLD_NOW);
878 ASSERT_TRUE(handle == nullptr);
879
880 std::string child_thread_error;
881 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
882 t.join();
883 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
884
Elliott Hughes5419b942012-10-16 15:54:46 -0700885 const char* main_thread_error = dlerror();
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800886 ASSERT_TRUE(main_thread_error != nullptr);
Elliott Hughes5419b942012-10-16 15:54:46 -0700887 ASSERT_SUBSTR("/main/thread", main_thread_error);
888}
889
Elliott Hughese66190d2012-12-18 15:57:55 -0800890TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700891 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700892 void* self = dlopen(nullptr, RTLD_NOW);
893 ASSERT_TRUE(self != nullptr);
894 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700895
896 void* sym;
897
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700898#if defined(__BIONIC__) && !defined(__LP64__)
899 // RTLD_DEFAULT in lp32 bionic is not (void*)0
900 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700901 sym = dlsym(nullptr, "test");
902 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800903 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700904#endif
905
Elliott Hughes3b297c42012-10-11 16:08:51 -0700906 // Symbol that doesn't exist.
907 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700908 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700909 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700910
911 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700912}
913
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700914TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700915 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700916 void* self = dlopen(nullptr, RTLD_NOW);
917 ASSERT_TRUE(self != nullptr);
918 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700919
920 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700921 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700922
923 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
924 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
925
926 Dl_info info;
927 int rc = dladdr(addr, &info);
928 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
929
930 // Get the name of this executable.
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700931 const std::string& executable_path = get_executable_path();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700932
933 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700934 char dli_realpath[PATH_MAX];
935 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700936 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700937
938 // The symbol name should be the symbol we looked up.
939 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
940
941 // The address should be the exact address of the symbol.
942 ASSERT_EQ(info.dli_saddr, sym);
943
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700944 std::vector<map_record> maps;
945 ASSERT_TRUE(Maps::parse_maps(&maps));
946
947 void* base_address = nullptr;
948 for (const map_record& rec : maps) {
949 if (executable_path == rec.pathname) {
950 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700951 break;
952 }
953 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700954
955 // The base address should be the address we were loaded at.
956 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700957
958 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700959}
960
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700961TEST(dlfcn, dlopen_executable_by_absolute_path) {
962 void* handle1 = dlopen(nullptr, RTLD_NOW);
963 ASSERT_TRUE(handle1 != nullptr) << dlerror();
964
965 void* handle2 = dlopen(get_executable_path().c_str(), RTLD_NOW);
966 ASSERT_TRUE(handle2 != nullptr) << dlerror();
967
968#if defined(__BIONIC__)
969 ASSERT_EQ(handle1, handle2);
970#else
971 GTEST_LOG_(INFO) << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
972 "it loads a separate copy of the main executable "
973 "on dlopen by absolute path.";
974#endif
975}
976
Victor Khimenko14b9d712017-01-25 19:59:16 +0100977#if defined (__aarch64__)
978#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm64/"
979#elif defined (__arm__)
980#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm/"
981#elif defined (__i386__)
982#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86/"
983#elif defined (__x86_64__)
984#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86_64/"
985#elif defined (__mips__)
986#if defined(__LP64__)
987#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips64/"
988#else
989#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips/"
990#endif
991#else
992#error "Unknown architecture"
993#endif
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200994#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100995#define ALTERNATE_PATH_TO_LIBC ALTERNATE_PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700996
997TEST(dlfcn, dladdr_libc) {
998#if defined(__BIONIC__)
999 Dl_info info;
1000 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
1001 ASSERT_TRUE(dladdr(addr, &info) != 0);
1002
Dmitriy Ivanovef255922015-04-08 11:53:08 -07001003 char libc_realpath[PATH_MAX];
Victor Khimenko14b9d712017-01-25 19:59:16 +01001004
1005 // Check if libc is in canonical path or in alternate path.
1006 if (strncmp(ALTERNATE_PATH_TO_SYSTEM_LIB,
1007 info.dli_fname,
1008 sizeof(ALTERNATE_PATH_TO_SYSTEM_LIB) - 1) == 0) {
1009 // Platform with emulated architecture. Symlink on ARC++.
1010 ASSERT_TRUE(realpath(ALTERNATE_PATH_TO_LIBC, libc_realpath) == libc_realpath);
1011 } else {
1012 // /system/lib is symlink when this test is executed on host.
1013 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
1014 }
Dmitriy Ivanovef255922015-04-08 11:53:08 -07001015
1016 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001017 // TODO: add check for dfi_fbase
1018 ASSERT_STREQ("puts", info.dli_sname);
1019 ASSERT_EQ(addr, info.dli_saddr);
1020#else
1021 GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig "
1022 "for libc.so, which is symlink itself (not a realpath).\n";
1023#endif
1024}
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
1061 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
1062#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) {
1179#if defined(__BIONIC__)
1180 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1181 ASSERT_TRUE(handle != nullptr) << dlerror();
1182 dlclose(handle);
1183#else
1184 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
1185#endif
1186}
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) {
1311 std::string libpath = get_testlib_root() + "/libtest_dt_runpath_d.so";
1312 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
dimitry06016f22018-01-05 11:39:28 +01001325TEST(dlfcn, dlclose_after_thread_local_dtor) {
1326 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
1338 void* handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW | RTLD_NOLOAD);
1339 ASSERT_TRUE(handle == nullptr);
1340
1341 handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW);
1342 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
1350 handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW | RTLD_NOLOAD);
1351 ASSERT_TRUE(handle == nullptr);
1352}
1353
1354TEST(dlfcn, dlclose_before_thread_local_dtor) {
1355 bool is_dtor_triggered = false;
1356
1357 auto f = [](bool* is_dtor_triggered) {
1358 void* handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW | RTLD_NOLOAD);
1359 ASSERT_TRUE(handle == nullptr);
1360
1361 handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW);
1362 ASSERT_TRUE(handle != nullptr) << dlerror();
1363
1364 typedef void (*fn_t)(bool*);
1365 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable"));
1366 ASSERT_TRUE(fn != nullptr) << dlerror();
1367
1368 fn(is_dtor_triggered);
1369
1370 dlclose(handle);
1371
1372 ASSERT_TRUE(!*is_dtor_triggered);
1373
1374 // Since we have thread_atexit dtors associated with handle - the library should
1375 // still be availabe.
1376 handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW | RTLD_NOLOAD);
1377 ASSERT_TRUE(handle != nullptr) << dlerror();
1378 dlclose(handle);
1379 };
1380
1381 void* handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW);
1382 ASSERT_TRUE(handle != nullptr) << dlerror();
1383 dlclose(handle);
1384
1385 handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW | RTLD_NOLOAD);
1386 ASSERT_TRUE(handle == nullptr);
1387
1388 std::thread t(f, &is_dtor_triggered);
1389 t.join();
1390#if defined(__BIONIC__)
1391 // ld-android.so unloads unreferenced libraries 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);
1395#else
1396 // GLIBC does not unload libraries with ref_count = 0 on pthread_exit
1397 ASSERT_TRUE(is_dtor_triggered);
1398 handle = dlopen("libtest_thread_local_dtor.so", RTLD_NOW | RTLD_NOLOAD);
1399 ASSERT_TRUE(handle != nullptr) << dlerror();
1400#endif
1401}
1402
Elliott Hughes8ad40932017-06-15 15:12:29 -07001403TEST(dlfcn, RTLD_macros) {
1404#if !defined(RTLD_LOCAL)
1405#error no RTLD_LOCAL
1406#elif !defined(RTLD_LAZY)
1407#error no RTLD_LAZY
1408#elif !defined(RTLD_NOW)
1409#error no RTLD_NOW
1410#elif !defined(RTLD_NOLOAD)
1411#error no RTLD_NOLOAD
1412#elif !defined(RTLD_GLOBAL)
1413#error no RTLD_GLOBAL
1414#elif !defined(RTLD_NODELETE)
1415#error no RTLD_NODELETE
1416#endif
1417}
1418
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001419// Bionic specific tests
1420#if defined(__BIONIC__)
1421
Ian Pedowitzb6310c22018-01-18 16:26:19 -08001422#if defined(__arm__)
1423const llvm::ELF::Elf32_Dyn* to_dynamic_table(const char* p) {
1424 return reinterpret_cast<const llvm::ELF::Elf32_Dyn*>(p);
1425}
1426
1427// Duplicate these definitions here because they are android specific
1428// note that we cannot include <elf.h> because #defines conflict with
1429// enum names provided by LLVM.
1430#define DT_ANDROID_REL (llvm::ELF::DT_LOOS + 2)
1431#define DT_ANDROID_RELA (llvm::ELF::DT_LOOS + 4)
1432
1433template<typename ELFT>
1434void validate_compatibility_of_native_library(const std::string& path, ELFT* elf) {
1435 bool has_elf_hash = false;
1436 bool has_android_rel = false;
1437 bool has_rel = false;
1438 // Find dynamic section and check that DT_HASH and there is no DT_ANDROID_REL
1439 for (auto it = elf->section_begin(); it != elf->section_end(); ++it) {
1440 const llvm::object::ELFSectionRef& section_ref = *it;
1441 if (section_ref.getType() == llvm::ELF::SHT_DYNAMIC) {
1442 llvm::StringRef data;
1443 ASSERT_TRUE(!it->getContents(data)) << "unable to get SHT_DYNAMIC section data";
1444 for (auto d = to_dynamic_table(data.data()); d->d_tag != llvm::ELF::DT_NULL; ++d) {
1445 if (d->d_tag == llvm::ELF::DT_HASH) {
1446 has_elf_hash = true;
1447 } else if (d->d_tag == DT_ANDROID_REL || d->d_tag == DT_ANDROID_RELA) {
1448 has_android_rel = true;
1449 } else if (d->d_tag == llvm::ELF::DT_REL || d->d_tag == llvm::ELF::DT_RELA) {
1450 has_rel = true;
1451 }
1452 }
1453
1454 break;
1455 }
1456 }
1457
1458 ASSERT_TRUE(has_elf_hash) << path.c_str() << ": missing elf hash (DT_HASH)";
1459 ASSERT_TRUE(!has_android_rel) << path.c_str() << ": has packed relocations";
1460 ASSERT_TRUE(has_rel) << path.c_str() << ": missing DT_REL/DT_RELA";
1461}
1462
1463void validate_compatibility_of_native_library(const char* soname) {
1464 // On the systems with emulation system libraries would be of different
1465 // architecture. Try to use alternate paths first.
1466 std::string path = std::string(ALTERNATE_PATH_TO_SYSTEM_LIB) + soname;
1467 auto binary_or_error = llvm::object::createBinary(path);
1468 if (!binary_or_error) {
1469 path = std::string(PATH_TO_SYSTEM_LIB) + soname;
1470 binary_or_error = llvm::object::createBinary(path);
1471 }
1472 ASSERT_FALSE(!binary_or_error);
1473
1474 llvm::object::Binary* binary = binary_or_error.get().getBinary();
1475
1476 auto obj = llvm::dyn_cast<llvm::object::ObjectFile>(binary);
1477 ASSERT_TRUE(obj != nullptr);
1478
1479 auto elf = llvm::dyn_cast<llvm::object::ELF32LEObjectFile>(obj);
1480
1481 ASSERT_TRUE(elf != nullptr);
1482
1483 validate_compatibility_of_native_library(path, elf);
1484}
1485
1486// This is a test for app compatibility workaround for arm apps
1487// affected by http://b/24465209
1488TEST(dlext, compat_elf_hash_and_relocation_tables) {
1489 validate_compatibility_of_native_library("libc.so");
1490 validate_compatibility_of_native_library("liblog.so");
1491 validate_compatibility_of_native_library("libstdc++.so");
1492 validate_compatibility_of_native_library("libdl.so");
1493 validate_compatibility_of_native_library("libm.so");
1494 validate_compatibility_of_native_library("libz.so");
1495 validate_compatibility_of_native_library("libjnigraphics.so");
1496}
1497
1498#endif // defined(__arm__)
1499
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001500TEST(dlfcn, dlopen_invalid_rw_load_segment) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001501 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001502 "/" + kPrebuiltElfDir +
1503 "/libtest_invalid-rw_load_segment.so";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001504 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1505 ASSERT_TRUE(handle == nullptr);
Elliott Hughes9076b0c2018-02-28 11:29:45 -08001506 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W+E load segments are not allowed";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001507 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1508}
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001509
1510TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001511 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001512 "/" + kPrebuiltElfDir +
1513 "/libtest_invalid-unaligned_shdr_offset.so";
1514
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001515 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1516 ASSERT_TRUE(handle == nullptr);
1517 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
1518 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1519}
1520
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001521TEST(dlfcn, dlopen_invalid_zero_shentsize) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001522 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001523 "/" + kPrebuiltElfDir +
1524 "/libtest_invalid-zero_shentsize.so";
1525
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001526 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1527 ASSERT_TRUE(handle == nullptr);
1528 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x";
1529 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1530}
1531
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001532TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001533 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001534 "/" + kPrebuiltElfDir +
1535 "/libtest_invalid-zero_shstrndx.so";
1536
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001537 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1538 ASSERT_TRUE(handle == nullptr);
1539 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx";
1540 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1541}
1542
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001543TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001544 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001545 "/" + kPrebuiltElfDir +
1546 "/libtest_invalid-empty_shdr_table.so";
1547
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001548 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1549 ASSERT_TRUE(handle == nullptr);
1550 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers";
1551 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1552}
1553
Dimitry Ivanov46230442016-08-15 13:40:53 -07001554TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001555 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001556 "/" + kPrebuiltElfDir +
1557 "/libtest_invalid-zero_shdr_table_offset.so";
1558
Dimitry Ivanov46230442016-08-15 13:40:53 -07001559 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1560 ASSERT_TRUE(handle == nullptr);
1561 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/";
1562 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1563}
1564
Dimitry Ivanov55958342016-08-15 14:06:04 -07001565TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001566 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001567 "/" + kPrebuiltElfDir +
1568 "/libtest_invalid-zero_shdr_table_content.so";
1569
Dimitry Ivanov55958342016-08-15 14:06:04 -07001570 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1571 ASSERT_TRUE(handle == nullptr);
1572 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
1573 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1574}
1575
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001576TEST(dlfcn, dlopen_invalid_textrels) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001577 const std::string libpath = get_testlib_root() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001578 "/" + kPrebuiltElfDir +
1579 "/libtest_invalid-textrels.so";
1580
1581 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1582 ASSERT_TRUE(handle == nullptr);
1583 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1584 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1585}
1586
1587TEST(dlfcn, dlopen_invalid_textrels2) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001588 const std::string libpath = get_testlib_root() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001589 "/" + kPrebuiltElfDir +
1590 "/libtest_invalid-textrels2.so";
1591
1592 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1593 ASSERT_TRUE(handle == nullptr);
1594 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1595 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1596}
1597
Jiyong Park01162f22017-10-16 15:31:09 +09001598TEST(dlfcn, dlopen_df_1_global) {
1599 void* handle = dlopen("libtest_dlopen_df_1_global.so", RTLD_NOW);
1600 ASSERT_TRUE(handle != nullptr) << dlerror();
1601}
1602
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001603#endif