blob: 0ec46634c8a619334d6ddf53a34280fa422cfadd [file] [log] [blame]
jeffhaoacf5aa72012-09-12 17:25:30 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <dlfcn.h>
Elliott Hughes8e15b082012-09-26 11:44:01 -070020#include <limits.h>
21#include <stdio.h>
22#include <stdint.h>
Dimitry Ivanov708589f2016-09-19 10:50:28 -070023#include <string.h>
Elliott Hughes8e15b082012-09-26 11:44:01 -070024
25#include <string>
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -080026#include <thread>
jeffhaoacf5aa72012-09-12 17:25:30 -070027
Tom Cherryb8ab6182017-04-05 16:20:29 -070028#include <android-base/scopeguard.h>
29
Dimitry Ivanov927877c2016-09-21 11:17:13 -070030#include "gtest_globals.h"
Dimitry Ivanov708589f2016-09-19 10:50:28 -070031#include "dlfcn_symlink_support.h"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070032#include "utils.h"
33
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -080034#if defined(__BIONIC__) && (defined(__arm__) || defined(__i386__))
35#pragma clang diagnostic push
36#pragma clang diagnostic ignored "-Wunused-parameter"
37
38#include <llvm/ADT/StringRef.h>
39#include <llvm/Object/Binary.h>
40#include <llvm/Object/ELFObjectFile.h>
41#include <llvm/Object/ObjectFile.h>
42
43#pragma clang diagnostic pop
44#endif // defined(__ANDROID__) && (defined(__arm__) || defined(__i386__))
45
Elliott Hughes3b297c42012-10-11 16:08:51 -070046#define ASSERT_SUBSTR(needle, haystack) \
47 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
48
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070049
Elliott Hughes1728b232014-05-14 10:02:03 -070050static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070051extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070052 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070053}
54
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070055static int g_ctor_function_called = 0;
Dimitry Ivanov55437462016-07-20 15:33:07 -070056static int g_ctor_argc = 0;
57static char** g_ctor_argv = reinterpret_cast<char**>(0xDEADBEEF);
58static char** g_ctor_envp = g_ctor_envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070059
Dimitry Ivanov55437462016-07-20 15:33:07 -070060extern "C" void ctor_function(int argc, char** argv, char** envp) __attribute__ ((constructor));
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070061
Dimitry Ivanov55437462016-07-20 15:33:07 -070062extern "C" void ctor_function(int argc, char** argv, char** envp) {
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070063 g_ctor_function_called = 17;
Dimitry Ivanov55437462016-07-20 15:33:07 -070064 g_ctor_argc = argc;
65 g_ctor_argv = argv;
66 g_ctor_envp = envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070067}
68
69TEST(dlfcn, ctor_function_call) {
70 ASSERT_EQ(17, g_ctor_function_called);
Dimitry Ivanov55437462016-07-20 15:33:07 -070071 ASSERT_TRUE(g_ctor_argc = get_argc());
72 ASSERT_TRUE(g_ctor_argv = get_argv());
73 ASSERT_TRUE(g_ctor_envp = get_envp());
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070074}
75
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070076TEST(dlfcn, dlsym_in_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070077 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070078 void* self = dlopen(nullptr, RTLD_NOW);
79 ASSERT_TRUE(self != nullptr);
80 ASSERT_TRUE(dlerror() == nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070081
82 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070083 ASSERT_TRUE(sym != nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070084
85 void (*function)() = reinterpret_cast<void(*)()>(sym);
86
Elliott Hughes1728b232014-05-14 10:02:03 -070087 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070088 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070089 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070090
91 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070092}
Elliott Hughes8e15b082012-09-26 11:44:01 -070093
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070094TEST(dlfcn, dlsym_from_sofile) {
95 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL);
96 ASSERT_TRUE(handle != nullptr) << dlerror();
97
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070098 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070099 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
100 ASSERT_TRUE(symbol == nullptr);
101 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
102
103 typedef int* (*fn_t)();
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700104 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
105 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
106 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700107
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700108 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700109 ASSERT_TRUE(ptr != nullptr) << dlerror();
110 ASSERT_EQ(42, *ptr);
111
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700112 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
113 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
114 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
115
116 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
117 ASSERT_TRUE(ptr != nullptr) << dlerror();
118 ASSERT_EQ(44, *ptr);
119
120 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
121 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
122 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
123
124 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
125 ASSERT_TRUE(ptr != nullptr) << dlerror();
126 ASSERT_EQ(43, *ptr);
127
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700128 dlclose(handle);
129}
130
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700131TEST(dlfcn, dlsym_from_sofile_with_preload) {
132 void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL);
133 ASSERT_TRUE(preload != nullptr) << dlerror();
134
135 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
136 ASSERT_TRUE(handle != nullptr) << dlerror();
137
138 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
139 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
140 ASSERT_TRUE(symbol == nullptr);
141 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
142
143 typedef int* (*fn_t)();
144 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
145 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
146 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
147
148 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
149 ASSERT_TRUE(ptr != nullptr) << dlerror();
150 ASSERT_EQ(42, *ptr);
151
152 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
153 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
154 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
155
156 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
157 ASSERT_TRUE(ptr != nullptr) << dlerror();
158 ASSERT_EQ(44, *ptr);
159
160 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
161 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
162 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
163
164 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
165 ASSERT_TRUE(ptr != nullptr) << dlerror();
166 ASSERT_EQ(43, *ptr);
167
168 dlclose(handle);
169 dlclose(preload);
170}
171
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700172TEST(dlfcn, dlsym_handle_global_sym) {
173 // check that we do not look into global group
174 // when looking up symbol by handle
175 void* handle = dlopen("libtest_empty.so", RTLD_NOW);
176 dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL);
177 void* sym = dlsym(handle, "getRandomNumber");
178 ASSERT_TRUE(sym == nullptr);
179 ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror());
180
181 sym = dlsym(handle, "DlSymTestFunction");
182 ASSERT_TRUE(sym == nullptr);
183 ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror());
184 dlclose(handle);
185}
186
Dimitry Ivanovd5b578a2016-12-14 15:16:56 -0800187TEST(dlfcn, dlsym_handle_empty_symbol) {
188 // check that dlsym of an empty symbol fails (see http://b/33530622)
189 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW);
190 ASSERT_TRUE(handle != nullptr) << dlerror();
191 void* sym = dlsym(handle, "");
192 ASSERT_TRUE(sym == nullptr);
193 ASSERT_SUBSTR("undefined symbol: ", dlerror());
194 dlclose(handle);
195}
196
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700197TEST(dlfcn, dlsym_with_dependencies) {
198 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700199 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700200 dlerror();
201 // This symbol is in DT_NEEDED library.
202 void* sym = dlsym(handle, "getRandomNumber");
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700203 ASSERT_TRUE(sym != nullptr) << dlerror();
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700204 int (*fn)(void);
205 fn = reinterpret_cast<int (*)(void)>(sym);
206 EXPECT_EQ(4, fn());
207 dlclose(handle);
208}
209
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700210TEST(dlfcn, dlopen_noload) {
211 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700212 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700213 handle = dlopen("libtest_simple.so", RTLD_NOW);
214 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700215 ASSERT_TRUE(handle != nullptr);
216 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700217 ASSERT_TRUE(handle == handle2);
218 ASSERT_EQ(0, dlclose(handle));
219 ASSERT_EQ(0, dlclose(handle2));
220}
221
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700222TEST(dlfcn, dlopen_by_soname) {
223 static const char* soname = "libdlext_test_soname.so";
224 static const char* filename = "libdlext_test_different_soname.so";
225 // 1. Make sure there is no library with soname in default search path
226 void* handle = dlopen(soname, RTLD_NOW);
227 ASSERT_TRUE(handle == nullptr);
228
229 // 2. Load a library using filename
230 handle = dlopen(filename, RTLD_NOW);
231 ASSERT_TRUE(handle != nullptr) << dlerror();
232
233 // 3. Find library by soname
234 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
235 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
236 ASSERT_EQ(handle, handle_soname);
237
238 // 4. RTLD_NOLOAD should still work with filename
239 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
240 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
241 ASSERT_EQ(handle, handle_filename);
242
243 dlclose(handle_filename);
244 dlclose(handle_soname);
245 dlclose(handle);
246}
247
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700248// mips doesn't support ifuncs
249#if !defined(__mips__)
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700250TEST(dlfcn, ifunc_variable) {
251 typedef const char* (*fn_ptr)();
252
253 // ifunc's choice depends on whether IFUNC_CHOICE has a value
254 // first check the set case
255 setenv("IFUNC_CHOICE", "set", 1);
256 // preload libtest_ifunc_variable_impl.so
257 void* handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
258 void* handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
259 ASSERT_TRUE(handle != nullptr) << dlerror();
260 const char** foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
261 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
262 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
263 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
264 ASSERT_EQ(strncmp("set", *foo_ptr, 3), 0);
265 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
266 dlclose(handle);
267 dlclose(handle_impl);
268
269 // then check the unset case
270 unsetenv("IFUNC_CHOICE");
271 handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW);
272 handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW);
273 ASSERT_TRUE(handle != nullptr) << dlerror();
274 foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo"));
275 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
276 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
277 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
278 ASSERT_EQ(strncmp("unset", *foo_ptr, 5), 0);
279 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
280 dlclose(handle);
281 dlclose(handle_impl);
282}
283
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700284TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700285 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700286
287 // ifunc's choice depends on whether IFUNC_CHOICE has a value
288 // first check the set case
289 setenv("IFUNC_CHOICE", "set", 1);
290 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700291 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700292 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
293 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700294 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
295 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700296 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
297 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700298 dlclose(handle);
299
300 // then check the unset case
301 unsetenv("IFUNC_CHOICE");
302 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700303 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700304 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
305 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700306 ASSERT_TRUE(foo_ptr != nullptr) << dlerror();
307 ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700308 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
Dimitry Ivanov21975b22017-05-02 16:31:56 -0700309 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700310 dlclose(handle);
311}
312
Dimitry Ivanov01c888c2017-01-23 14:48:21 -0800313// ld.gold for arm produces incorrect binary (see http://b/27930475 for details)
314#if defined(__arm__)
315TEST(dlfcn, KNOWN_FAILURE_ON_BIONIC(ifunc_ctor_call)) {
316#else
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700317TEST(dlfcn, ifunc_ctor_call) {
Dimitry Ivanov01c888c2017-01-23 14:48:21 -0800318#endif
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700319 typedef const char* (*fn_ptr)();
320
321 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700322 ASSERT_TRUE(handle != nullptr) << dlerror();
323 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
324 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
325 ASSERT_STREQ("false", is_ctor_called());
326
327 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
328 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700329 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700330 dlclose(handle);
331}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700332
Dimitry Ivanov01c888c2017-01-23 14:48:21 -0800333// ld.gold for arm produces incorrect binary (see http://b/27930475 for details)
334#if defined(__arm__)
335TEST(dlfcn, KNOWN_FAILURE_ON_BIONIC(ifunc_ctor_call_rtld_lazy)) {
336#else
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700337TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
Dimitry Ivanov01c888c2017-01-23 14:48:21 -0800338#endif
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700339 typedef const char* (*fn_ptr)();
340
341 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
342 ASSERT_TRUE(handle != nullptr) << dlerror();
343 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
344 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
345 ASSERT_STREQ("false", is_ctor_called());
346
347 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
348 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
349 ASSERT_STREQ("true", is_ctor_called());
350 dlclose(handle);
351}
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700352#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700353
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700354TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
355 // This is the structure of the test library and
356 // its dt_needed libraries
357 // libtest_relo_check_dt_needed_order.so
358 // |
359 // +-> libtest_relo_check_dt_needed_order_1.so
360 // |
361 // +-> libtest_relo_check_dt_needed_order_2.so
362 //
363 // The root library references relo_test_get_answer_lib - which is defined
364 // in both dt_needed libraries, the correct relocation should
365 // use the function defined in libtest_relo_check_dt_needed_order_1.so
366 void* handle = nullptr;
Tom Cherryb8ab6182017-04-05 16:20:29 -0700367 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700368
369 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
370 ASSERT_TRUE(handle != nullptr) << dlerror();
371
372 typedef int (*fn_t) (void);
373 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
374 ASSERT_TRUE(fn != nullptr) << dlerror();
375 ASSERT_EQ(1, fn());
376}
377
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700378TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700379 // Here is how the test library and its dt_needed
380 // libraries are arranged
381 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700382 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700383 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700384 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700385 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700386 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700387 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700388 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700389 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700390 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700391 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700392 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700393 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700394 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700395 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700396 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700397 //
398 // load order should be (1, 2, 3, a, b, d)
399 //
400 // get_answer() is defined in (2, 3, a, b, c)
401 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700402 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700403 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700404 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
405 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700406 typedef int (*fn_t) (void);
407 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700408 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700409 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700410 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700411 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700412
413 ASSERT_EQ(42, fn());
414 ASSERT_EQ(43, fn2());
415 dlclose(handle);
416}
417
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700418TEST(dlfcn, dlopen_check_order_reloc_siblings) {
419 // This is how this one works:
420 // we lookup and call get_answer which is defined in '_2.so'
421 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
422 // the correct _impl() is implemented by '_a.so';
423 //
424 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
425 //
426 // Here is the picture:
427 //
428 // libtest_check_order_reloc_siblings.so
429 // |
430 // +-> ..._1.so <- empty
431 // | |
432 // | +-> ..._a.so <- exports correct answer_impl()
433 // | |
434 // | +-> ..._b.so <- every other letter exporting incorrect one.
435 // |
436 // +-> ..._2.so <- empty
437 // | |
438 // | +-> ..._c.so
439 // | |
440 // | +-> ..._d.so
441 // |
442 // +-> ..._3.so <- empty
443 // |
444 // +-> ..._e.so
445 // |
446 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
447 // implements incorrect get_answer_impl()
448
449 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
450 ASSERT_TRUE(handle == nullptr);
451#ifdef __BIONIC__
452 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
453 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
454#endif
455
456 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
457 ASSERT_TRUE(handle != nullptr) << dlerror();
458
459 typedef int (*fn_t) (void);
460 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
461 ASSERT_TRUE(fn != nullptr) << dlerror();
462 ASSERT_EQ(42, fn());
463
464 ASSERT_EQ(0, dlclose(handle));
465}
466
467TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
468 // This test uses the same library as dlopen_check_order_reloc_siblings.
469 // Unlike dlopen_check_order_reloc_siblings it preloads
470 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
471 // dlopen(libtest_check_order_reloc_siblings.so)
472
473 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
474 ASSERT_TRUE(handle == nullptr);
475 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
476 ASSERT_TRUE(handle == nullptr);
477
478 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
479 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
480
481 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
482 ASSERT_TRUE(handle != nullptr) << dlerror();
483
484 ASSERT_EQ(0, dlclose(handle_for_1));
485
486 typedef int (*fn_t) (void);
487 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
488 ASSERT_TRUE(fn != nullptr) << dlerror();
489 ASSERT_EQ(42, fn());
490
491 ASSERT_EQ(0, dlclose(handle));
492}
493
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800494TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
495 // This is how this one works:
496 // we lookup and call grandchild_get_answer which is defined in '_2.so'
497 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
498 // the correct _impl() is implemented by '_c_1.so';
499 //
500 // Here is the picture of subtree:
501 //
502 // libtest_check_order_reloc_siblings.so
503 // |
504 // +-> ..._2.so <- grandchild_get_answer()
505 // |
506 // +-> ..._c.so <- empty
507 // | |
508 // | +-> _c_1.so <- exports correct answer_impl()
509 // | |
510 // | +-> _c_2.so <- exports incorrect answer_impl()
511 // |
512 // +-> ..._d.so <- empty
513
514 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
515 ASSERT_TRUE(handle == nullptr);
516#ifdef __BIONIC__
517 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
518 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
519#endif
520
521 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
522 ASSERT_TRUE(handle != nullptr) << dlerror();
523
524 typedef int (*fn_t) (void);
525 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
526 ASSERT_TRUE(fn != nullptr) << dlerror();
527 ASSERT_EQ(42, fn());
528
529 ASSERT_EQ(0, dlclose(handle));
530}
531
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700532TEST(dlfcn, dlopen_check_order_reloc_nephew) {
533 // This is how this one works:
534 // we lookup and call nephew_get_answer which is defined in '_2.so'
535 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
536 // the correct _impl() is implemented by '_a.so';
537 //
538 // Here is the picture:
539 //
540 // libtest_check_order_reloc_siblings.so
541 // |
542 // +-> ..._1.so <- empty
543 // | |
544 // | +-> ..._a.so <- exports correct answer_impl()
545 // | |
546 // | +-> ..._b.so <- every other letter exporting incorrect one.
547 // |
548 // +-> ..._2.so <- empty
549 // | |
550 // | +-> ..._c.so
551 // | |
552 // | +-> ..._d.so
553 // |
554 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
555 // |
556 // +-> ..._e.so
557 // |
558 // +-> ..._f.so
559
560 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
561 ASSERT_TRUE(handle == nullptr);
562#ifdef __BIONIC__
563 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
564 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
565#endif
566
567 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
568 ASSERT_TRUE(handle != nullptr) << dlerror();
569
570 typedef int (*fn_t) (void);
571 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
572 ASSERT_TRUE(fn != nullptr) << dlerror();
573 ASSERT_EQ(42, fn());
574
575 ASSERT_EQ(0, dlclose(handle));
576}
577
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800578TEST(dlfcn, check_unload_after_reloc) {
579 // This is how this one works:
580 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
581 // |
582 // +-> libtest_two_parents_child
583 //
584 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
585 // |
586 // +-> libtest_two_parents_child
587 //
588 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
589 // as a second step it dlopens parent2 and dlcloses parent1...
590
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700591 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
592 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800593
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700594 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
595 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800596
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700597 typedef int (*fn_t) (void);
598 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
599 ASSERT_TRUE(fn != nullptr) << dlerror();
600 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800601
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700602 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800603
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700604 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
605 ASSERT_TRUE(handle != nullptr);
606 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800607
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700608 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
609 ASSERT_TRUE(fn != nullptr) << dlerror();
610 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800611
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700612 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800613
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700614 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
615 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800616}
617
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700618extern "C" int check_order_reloc_root_get_answer_impl() {
619 return 42;
620}
621
622TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
623 // This is how this one works:
624 // we lookup and call get_answer3 which is defined in 'root.so'
625 // and in turn calls external root_get_answer_impl() defined in _2.so and
626 // above the correct _impl() is one in the executable.
627 //
628 // libtest_check_order_reloc_root.so
629 // |
630 // +-> ..._1.so <- empty
631 // |
632 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
633 //
634
635 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
636 ASSERT_TRUE(handle == nullptr);
637#ifdef __BIONIC__
638 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
639 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
640#endif
641
642 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
643 ASSERT_TRUE(handle != nullptr) << dlerror();
644
645 typedef int (*fn_t) (void);
646 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
647 ASSERT_TRUE(fn != nullptr) << dlerror();
648 ASSERT_EQ(42, fn());
649
650 ASSERT_EQ(0, dlclose(handle));
651}
652
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700653TEST(dlfcn, dlopen_check_rtld_local) {
654 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
655 ASSERT_TRUE(sym == nullptr);
656
657 // implicit RTLD_LOCAL
658 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
659 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
660 ASSERT_TRUE(sym == nullptr);
661 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
662 sym = dlsym(handle, "dlopen_testlib_simple_func");
663 ASSERT_TRUE(sym != nullptr);
664 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
665 dlclose(handle);
666
667 // explicit RTLD_LOCAL
668 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
669 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
670 ASSERT_TRUE(sym == nullptr);
671 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
672 sym = dlsym(handle, "dlopen_testlib_simple_func");
673 ASSERT_TRUE(sym != nullptr);
674 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
675 dlclose(handle);
676}
677
678TEST(dlfcn, dlopen_check_rtld_global) {
679 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
680 ASSERT_TRUE(sym == nullptr);
681
682 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700683 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700684 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
685 ASSERT_TRUE(sym != nullptr) << dlerror();
686 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
687 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700688
689 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
690 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
691 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700692
693 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
694 // shared libraries after symbol was not found in the main executable
695 // and dependent libraries.
696 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
697 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
698 ASSERT_TRUE(sym != nullptr) << dlerror();
699
700 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700701}
702
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700703// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
704// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
705// libtest_with_dependency_loop_a.so
706TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700707 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
708 ASSERT_TRUE(handle != nullptr) << dlerror();
709 void* f = dlsym(handle, "dlopen_test_loopy_function");
710 ASSERT_TRUE(f != nullptr) << dlerror();
711 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
712 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700713
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700714 // dlopen second time to make sure that the library was unloaded correctly
715 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
716 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800717#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700718 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 -0800719#else
720 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
721 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700722#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800723
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700724 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
725 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700726}
727
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700728TEST(dlfcn, dlopen_nodelete) {
729 static bool is_unloaded = false;
730
731 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
732 ASSERT_TRUE(handle != nullptr) << dlerror();
733 void (*set_unload_flag_ptr)(bool*);
734 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
735 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
736 set_unload_flag_ptr(&is_unloaded);
737
738 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
739 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
740 ASSERT_EQ(1729U, *taxicab_number);
741 *taxicab_number = 2;
742
743 dlclose(handle);
744 ASSERT_TRUE(!is_unloaded);
745
746 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
747 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
748 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
749
750
751 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
752 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
753 ASSERT_EQ(taxicab_number2, taxicab_number);
754
755 ASSERT_EQ(2U, *taxicab_number2);
756
757 dlclose(handle);
758 ASSERT_TRUE(!is_unloaded);
759}
760
761TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
762 static bool is_unloaded = false;
763
764 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
765 ASSERT_TRUE(handle != nullptr) << dlerror();
766 void (*set_unload_flag_ptr)(bool*);
767 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
768 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
769 set_unload_flag_ptr(&is_unloaded);
770
771 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
772 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
773
774 ASSERT_EQ(1729U, *taxicab_number);
775 *taxicab_number = 2;
776
777 // This RTLD_NODELETE should be ignored
778 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
779 ASSERT_TRUE(handle1 != nullptr) << dlerror();
780 ASSERT_EQ(handle, handle1);
781
782 dlclose(handle1);
783 dlclose(handle);
784
785 ASSERT_TRUE(is_unloaded);
786}
787
788TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
789 static bool is_unloaded = false;
790
791 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
792 ASSERT_TRUE(handle != nullptr) << dlerror();
793 void (*set_unload_flag_ptr)(bool*);
794 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
795 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
796 set_unload_flag_ptr(&is_unloaded);
797
798 dlclose(handle);
799 ASSERT_TRUE(!is_unloaded);
800}
801
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700802TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700803 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
804 ASSERT_TRUE(handle != nullptr) << dlerror();
805 int (*get_answer)();
806 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
807 ASSERT_TRUE(get_answer != nullptr) << dlerror();
808 ASSERT_EQ(42, get_answer());
809 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700810}
811
Elliott Hughese66190d2012-12-18 15:57:55 -0800812TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700813 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700814 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700815#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700816 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
817#else
818 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
819#endif
820}
821
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800822static void ConcurrentDlErrorFn(std::string& error) {
823 ASSERT_TRUE(dlerror() == nullptr);
824
825 void* handle = dlopen("/child/thread", RTLD_NOW);
826 ASSERT_TRUE(handle == nullptr);
827
828 const char* err = dlerror();
829 ASSERT_TRUE(err != nullptr);
830
831 error = err;
832}
833
834TEST(dlfcn, dlerror_concurrent_buffer) {
835 void* handle = dlopen("/main/thread", RTLD_NOW);
836 ASSERT_TRUE(handle == nullptr);
837 const char* main_thread_error = dlerror();
838 ASSERT_TRUE(main_thread_error != nullptr);
839 ASSERT_SUBSTR("/main/thread", main_thread_error);
840
841 std::string child_thread_error;
842 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
843 t.join();
844 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
845
846 // Check that main thread local buffer was not modified.
847 ASSERT_SUBSTR("/main/thread", main_thread_error);
Elliott Hughes5419b942012-10-16 15:54:46 -0700848}
849
Elliott Hughese66190d2012-12-18 15:57:55 -0800850TEST(dlfcn, dlerror_concurrent) {
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800851 void* handle = dlopen("/main/thread", RTLD_NOW);
852 ASSERT_TRUE(handle == nullptr);
853
854 std::string child_thread_error;
855 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
856 t.join();
857 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
858
Elliott Hughes5419b942012-10-16 15:54:46 -0700859 const char* main_thread_error = dlerror();
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800860 ASSERT_TRUE(main_thread_error != nullptr);
Elliott Hughes5419b942012-10-16 15:54:46 -0700861 ASSERT_SUBSTR("/main/thread", main_thread_error);
862}
863
Elliott Hughese66190d2012-12-18 15:57:55 -0800864TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700865 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700866 void* self = dlopen(nullptr, RTLD_NOW);
867 ASSERT_TRUE(self != nullptr);
868 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700869
870 void* sym;
871
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700872#if defined(__BIONIC__) && !defined(__LP64__)
873 // RTLD_DEFAULT in lp32 bionic is not (void*)0
874 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700875 sym = dlsym(nullptr, "test");
876 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800877 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700878#endif
879
Elliott Hughes3b297c42012-10-11 16:08:51 -0700880 // Symbol that doesn't exist.
881 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700882 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700883 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700884
885 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700886}
887
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700888TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700889 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700890 void* self = dlopen(nullptr, RTLD_NOW);
891 ASSERT_TRUE(self != nullptr);
892 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700893
894 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700895 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700896
897 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
898 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
899
900 Dl_info info;
901 int rc = dladdr(addr, &info);
902 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
903
904 // Get the name of this executable.
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700905 const std::string& executable_path = get_executable_path();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700906
907 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700908 char dli_realpath[PATH_MAX];
909 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700910 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700911
912 // The symbol name should be the symbol we looked up.
913 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
914
915 // The address should be the exact address of the symbol.
916 ASSERT_EQ(info.dli_saddr, sym);
917
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700918 std::vector<map_record> maps;
919 ASSERT_TRUE(Maps::parse_maps(&maps));
920
921 void* base_address = nullptr;
922 for (const map_record& rec : maps) {
923 if (executable_path == rec.pathname) {
924 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700925 break;
926 }
927 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700928
929 // The base address should be the address we were loaded at.
930 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700931
932 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700933}
934
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700935TEST(dlfcn, dlopen_executable_by_absolute_path) {
936 void* handle1 = dlopen(nullptr, RTLD_NOW);
937 ASSERT_TRUE(handle1 != nullptr) << dlerror();
938
939 void* handle2 = dlopen(get_executable_path().c_str(), RTLD_NOW);
940 ASSERT_TRUE(handle2 != nullptr) << dlerror();
941
942#if defined(__BIONIC__)
943 ASSERT_EQ(handle1, handle2);
944#else
945 GTEST_LOG_(INFO) << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
946 "it loads a separate copy of the main executable "
947 "on dlopen by absolute path.";
948#endif
949}
950
Victor Khimenko14b9d712017-01-25 19:59:16 +0100951#if defined (__aarch64__)
952#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm64/"
953#elif defined (__arm__)
954#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm/"
955#elif defined (__i386__)
956#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86/"
957#elif defined (__x86_64__)
958#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86_64/"
959#elif defined (__mips__)
960#if defined(__LP64__)
961#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips64/"
962#else
963#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips/"
964#endif
965#else
966#error "Unknown architecture"
967#endif
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200968#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100969#define ALTERNATE_PATH_TO_LIBC ALTERNATE_PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700970
971TEST(dlfcn, dladdr_libc) {
972#if defined(__BIONIC__)
973 Dl_info info;
974 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
975 ASSERT_TRUE(dladdr(addr, &info) != 0);
976
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700977 char libc_realpath[PATH_MAX];
Victor Khimenko14b9d712017-01-25 19:59:16 +0100978
979 // Check if libc is in canonical path or in alternate path.
980 if (strncmp(ALTERNATE_PATH_TO_SYSTEM_LIB,
981 info.dli_fname,
982 sizeof(ALTERNATE_PATH_TO_SYSTEM_LIB) - 1) == 0) {
983 // Platform with emulated architecture. Symlink on ARC++.
984 ASSERT_TRUE(realpath(ALTERNATE_PATH_TO_LIBC, libc_realpath) == libc_realpath);
985 } else {
986 // /system/lib is symlink when this test is executed on host.
987 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
988 }
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700989
990 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700991 // TODO: add check for dfi_fbase
992 ASSERT_STREQ("puts", info.dli_sname);
993 ASSERT_EQ(addr, info.dli_saddr);
994#else
995 GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig "
996 "for libc.so, which is symlink itself (not a realpath).\n";
997#endif
998}
999
Elliott Hughese66190d2012-12-18 15:57:55 -08001000TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -07001001 Dl_info info;
1002
Elliott Hughes3b297c42012-10-11 16:08:51 -07001003 dlerror(); // Clear any pending errors.
1004
Elliott Hughes8e15b082012-09-26 11:44:01 -07001005 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001006 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
1007 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001008
1009 // No symbol corresponding to a stack address.
1010 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001011 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -07001012}
Elliott Hughes124fae92012-10-31 14:20:03 -07001013
Elliott Hughesa43e9062013-01-07 14:18:22 -08001014// GNU-style ELF hash tables are incompatible with the MIPS ABI.
1015// 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 -08001016TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001017#if !defined(__mips__)
Elliott Hughes124fae92012-10-31 14:20:03 -07001018 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001019 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
1020 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001021 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001022 void* sym = dlsym(handle, "getRandomNumber");
1023 ASSERT_TRUE(sym != nullptr) << dlerror();
1024 int (*fn)(void);
1025 fn = reinterpret_cast<int (*)(void)>(sym);
1026 EXPECT_EQ(4, fn());
1027
1028 Dl_info dlinfo;
1029 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1030
1031 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1032 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001033 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001034#else
1035 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
1036#endif
Elliott Hughes124fae92012-10-31 14:20:03 -07001037}
Elliott Hughese66190d2012-12-18 15:57:55 -08001038
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001039TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
1040 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
1041 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001042 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001043 void* sym = dlsym(handle, "getRandomNumber");
1044 ASSERT_TRUE(sym != nullptr) << dlerror();
1045 int (*fn)(void);
1046 fn = reinterpret_cast<int (*)(void)>(sym);
1047 EXPECT_EQ(4, fn());
1048
1049 Dl_info dlinfo;
1050 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1051
1052 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1053 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
1054 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
1055}
1056
Elliott Hughese66190d2012-12-18 15:57:55 -08001057TEST(dlfcn, dlopen_bad_flags) {
1058 dlerror(); // Clear any pending errors.
1059 void* handle;
1060
Elliott Hughes063525c2014-05-13 11:19:57 -07001061#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -08001062 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001063 handle = dlopen(nullptr, 0);
1064 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001065 ASSERT_SUBSTR("invalid", dlerror());
1066#endif
1067
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001068 handle = dlopen(nullptr, 0xffffffff);
1069 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001070 ASSERT_SUBSTR("invalid", dlerror());
1071
1072 // 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 -07001073 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
1074 ASSERT_TRUE(handle != nullptr);
1075 ASSERT_SUBSTR(nullptr, dlerror());
Elliott Hughese66190d2012-12-18 15:57:55 -08001076}
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001077
1078TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001079 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001080 ASSERT_TRUE(addr == nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001081}
1082
1083TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001084 void* addr = dlsym(RTLD_DEFAULT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001085 ASSERT_TRUE(addr != nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001086}
1087
1088TEST(dlfcn, rtld_next_unknown_symbol) {
1089 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001090 ASSERT_TRUE(addr == nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001091}
1092
1093TEST(dlfcn, rtld_next_known_symbol) {
1094 void* addr = dlsym(RTLD_NEXT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001095 ASSERT_TRUE(addr != nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001096}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001097
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001098// Check that RTLD_NEXT of a libc symbol works in dlopened library
1099TEST(dlfcn, rtld_next_from_library) {
1100 void* library_with_close = dlopen("libtest_check_rtld_next_from_library.so", RTLD_NOW);
1101 ASSERT_TRUE(library_with_close != nullptr) << dlerror();
1102 void* expected_addr = dlsym(RTLD_DEFAULT, "close");
1103 ASSERT_TRUE(expected_addr != nullptr) << dlerror();
1104 typedef void* (*get_libc_close_ptr_fn_t)();
1105 get_libc_close_ptr_fn_t get_libc_close_ptr =
1106 reinterpret_cast<get_libc_close_ptr_fn_t>(dlsym(library_with_close, "get_libc_close_ptr"));
1107 ASSERT_TRUE(get_libc_close_ptr != nullptr) << dlerror();
1108 ASSERT_EQ(expected_addr, get_libc_close_ptr());
1109
1110 dlclose(library_with_close);
1111}
1112
1113
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001114TEST(dlfcn, dlsym_weak_func) {
1115 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001116 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001117 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001118
1119 int (*weak_func)();
1120 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001121 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001122 EXPECT_EQ(42, weak_func());
1123 dlclose(handle);
1124}
1125
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001126TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001127 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
1128 ASSERT_TRUE(handle != nullptr) << dlerror();
1129 int (*weak_func)();
1130 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
1131 ASSERT_TRUE(weak_func != nullptr) << dlerror();
1132 EXPECT_EQ(6551, weak_func());
1133 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001134}
1135
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001136TEST(dlfcn, dlopen_symlink) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001137 DlfcnSymlink symlink("dlopen_symlink");
1138 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001139 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001140 void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001141 ASSERT_TRUE(handle1 != nullptr);
1142 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001143 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -07001144 dlclose(handle1);
1145 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001146}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001147
1148// libtest_dlopen_from_ctor_main.so depends on
1149// libtest_dlopen_from_ctor.so which has a constructor
1150// that calls dlopen(libc...). This is to test the situation
1151// described in b/7941716.
1152TEST(dlfcn, dlopen_dlopen_from_ctor) {
1153#if defined(__BIONIC__)
1154 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1155 ASSERT_TRUE(handle != nullptr) << dlerror();
1156 dlclose(handle);
1157#else
1158 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
1159#endif
1160}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001161
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001162static std::string g_fini_call_order_str;
1163
1164static void register_fini_call(const char* s) {
1165 g_fini_call_order_str += s;
1166}
1167
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001168static void test_init_fini_call_order_for(const char* libname) {
1169 g_fini_call_order_str.clear();
1170 void* handle = dlopen(libname, RTLD_NOW);
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001171 ASSERT_TRUE(handle != nullptr) << dlerror();
1172 typedef int (*get_init_order_number_t)();
1173 get_init_order_number_t get_init_order_number =
1174 reinterpret_cast<get_init_order_number_t>(dlsym(handle, "get_init_order_number"));
1175 ASSERT_EQ(321, get_init_order_number());
1176
1177 typedef void (*set_fini_callback_t)(void (*f)(const char*));
1178 set_fini_callback_t set_fini_callback =
1179 reinterpret_cast<set_fini_callback_t>(dlsym(handle, "set_fini_callback"));
1180 set_fini_callback(register_fini_call);
1181 dlclose(handle);
1182 ASSERT_EQ("(root)(child)(grandchild)", g_fini_call_order_str);
1183}
1184
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001185TEST(dlfcn, init_fini_call_order) {
1186 test_init_fini_call_order_for("libtest_init_fini_order_root.so");
1187 test_init_fini_call_order_for("libtest_init_fini_order_root2.so");
1188}
1189
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001190TEST(dlfcn, symbol_versioning_use_v1) {
1191 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1192 ASSERT_TRUE(handle != nullptr) << dlerror();
1193 typedef int (*fn_t)();
1194 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1195 ASSERT_TRUE(fn != nullptr) << dlerror();
1196 ASSERT_EQ(1, fn());
1197 dlclose(handle);
1198}
1199
1200TEST(dlfcn, symbol_versioning_use_v2) {
1201 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1202 ASSERT_TRUE(handle != nullptr) << dlerror();
1203 typedef int (*fn_t)();
1204 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1205 ASSERT_TRUE(fn != nullptr) << dlerror();
1206 ASSERT_EQ(2, fn());
1207 dlclose(handle);
1208}
1209
1210TEST(dlfcn, symbol_versioning_use_other_v2) {
1211 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1212 ASSERT_TRUE(handle != nullptr) << dlerror();
1213 typedef int (*fn_t)();
1214 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1215 ASSERT_TRUE(fn != nullptr) << dlerror();
1216 ASSERT_EQ(20, fn());
1217 dlclose(handle);
1218}
1219
1220TEST(dlfcn, symbol_versioning_use_other_v3) {
1221 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1222 ASSERT_TRUE(handle != nullptr) << dlerror();
1223 typedef int (*fn_t)();
1224 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1225 ASSERT_TRUE(fn != nullptr) << dlerror();
1226 ASSERT_EQ(3, fn());
1227 dlclose(handle);
1228}
1229
1230TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1231 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1232 ASSERT_TRUE(handle != nullptr) << dlerror();
1233 typedef int (*fn_t)();
1234 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1235 ASSERT_TRUE(fn != nullptr) << dlerror();
1236 ASSERT_EQ(3, fn()); // the default version is 3
1237 dlclose(handle);
1238}
1239
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001240TEST(dlfcn, dlvsym_smoke) {
1241 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1242 ASSERT_TRUE(handle != nullptr) << dlerror();
1243 typedef int (*fn_t)();
1244
1245 {
1246 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1247 ASSERT_TRUE(fn == nullptr);
1248 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1249 }
1250
1251 {
1252 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1253 ASSERT_TRUE(fn != nullptr) << dlerror();
1254 ASSERT_EQ(2, fn());
1255 }
1256
1257 dlclose(handle);
1258}
1259
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001260// This preempts the implementation from libtest_versioned_lib.so
1261extern "C" int version_zero_function() {
1262 return 0;
1263}
1264
1265// This preempts the implementation from libtest_versioned_uselibv*.so
1266extern "C" int version_zero_function2() {
1267 return 0;
1268}
Evgenii Stepanov68650822015-06-10 13:38:39 -07001269
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001270TEST(dlfcn, dt_runpath_smoke) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001271 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1272 ASSERT_TRUE(handle != nullptr) << dlerror();
1273
1274 typedef void *(* dlopen_b_fn)();
1275 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1276 ASSERT_TRUE(fn != nullptr) << dlerror();
1277
1278 void *p = fn();
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001279 ASSERT_TRUE(p != nullptr);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001280
1281 dlclose(handle);
1282}
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001283
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001284// Bionic specific tests
1285#if defined(__BIONIC__)
1286
Dimitry Ivanovfc32dcb2017-03-24 10:58:23 -07001287#if defined(__arm__)
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001288const llvm::ELF::Elf32_Dyn* to_dynamic_table(const char* p) {
1289 return reinterpret_cast<const llvm::ELF::Elf32_Dyn*>(p);
1290}
1291
1292// Duplicate these definitions here because they are android specific
1293// note that we cannot include <elf.h> because #defines conflict with
1294// enum names provided by LLVM.
1295#define DT_ANDROID_REL (llvm::ELF::DT_LOOS + 2)
1296#define DT_ANDROID_RELA (llvm::ELF::DT_LOOS + 4)
1297
1298template<typename ELFT>
1299void validate_compatibility_of_native_library(const std::string& path, ELFT* elf) {
1300 bool has_elf_hash = false;
1301 bool has_android_rel = false;
1302 bool has_rel = false;
1303 // Find dynamic section and check that DT_HASH and there is no DT_ANDROID_REL
1304 for (auto it = elf->section_begin(); it != elf->section_end(); ++it) {
1305 const llvm::object::ELFSectionRef& section_ref = *it;
1306 if (section_ref.getType() == llvm::ELF::SHT_DYNAMIC) {
1307 llvm::StringRef data;
1308 ASSERT_TRUE(!it->getContents(data)) << "unable to get SHT_DYNAMIC section data";
1309 for (auto d = to_dynamic_table(data.data()); d->d_tag != llvm::ELF::DT_NULL; ++d) {
1310 if (d->d_tag == llvm::ELF::DT_HASH) {
1311 has_elf_hash = true;
1312 } else if (d->d_tag == DT_ANDROID_REL || d->d_tag == DT_ANDROID_RELA) {
1313 has_android_rel = true;
1314 } else if (d->d_tag == llvm::ELF::DT_REL || d->d_tag == llvm::ELF::DT_RELA) {
1315 has_rel = true;
1316 }
1317 }
1318
1319 break;
1320 }
1321 }
1322
1323 ASSERT_TRUE(has_elf_hash) << path.c_str() << ": missing elf hash (DT_HASH)";
1324 ASSERT_TRUE(!has_android_rel) << path.c_str() << ": has packed relocations";
1325 ASSERT_TRUE(has_rel) << path.c_str() << ": missing DT_REL/DT_RELA";
1326}
1327
1328void validate_compatibility_of_native_library(const char* soname) {
Victor Khimenko14b9d712017-01-25 19:59:16 +01001329 // On the systems with emulation system libraries would be of different
1330 // architecture. Try to use alternate paths first.
1331 std::string path = std::string(ALTERNATE_PATH_TO_SYSTEM_LIB) + soname;
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001332 auto binary_or_error = llvm::object::createBinary(path);
Victor Khimenko14b9d712017-01-25 19:59:16 +01001333 if (!binary_or_error) {
1334 path = std::string(PATH_TO_SYSTEM_LIB) + soname;
1335 binary_or_error = llvm::object::createBinary(path);
1336 }
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001337 ASSERT_FALSE(!binary_or_error);
1338
1339 llvm::object::Binary* binary = binary_or_error.get().getBinary();
1340
1341 auto obj = llvm::dyn_cast<llvm::object::ObjectFile>(binary);
1342 ASSERT_TRUE(obj != nullptr);
1343
1344 auto elf = llvm::dyn_cast<llvm::object::ELF32LEObjectFile>(obj);
1345
1346 ASSERT_TRUE(elf != nullptr);
1347
1348 validate_compatibility_of_native_library(path, elf);
1349}
1350
Dimitry Ivanovfc32dcb2017-03-24 10:58:23 -07001351// This is a test for app compatibility workaround for arm apps
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001352// affected by http://b/24465209
1353TEST(dlext, compat_elf_hash_and_relocation_tables) {
1354 validate_compatibility_of_native_library("libc.so");
1355 validate_compatibility_of_native_library("liblog.so");
1356 validate_compatibility_of_native_library("libstdc++.so");
1357 validate_compatibility_of_native_library("libdl.so");
1358 validate_compatibility_of_native_library("libm.so");
1359 validate_compatibility_of_native_library("libz.so");
1360 validate_compatibility_of_native_library("libjnigraphics.so");
1361}
1362
Dimitry Ivanovfc32dcb2017-03-24 10:58:23 -07001363#endif // defined(__arm__)
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001364
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001365TEST(dlfcn, dt_runpath_absolute_path) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001366 std::string libpath = get_testlib_root() + "/libtest_dt_runpath_d.so";
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001367 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001368 ASSERT_TRUE(handle != nullptr) << dlerror();
1369
1370 typedef void *(* dlopen_b_fn)();
1371 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1372 ASSERT_TRUE(fn != nullptr) << dlerror();
1373
1374 void *p = fn();
1375 ASSERT_TRUE(p != nullptr);
1376
1377 dlclose(handle);
1378}
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001379
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001380TEST(dlfcn, dlopen_invalid_rw_load_segment) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001381 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001382 "/" + kPrebuiltElfDir +
1383 "/libtest_invalid-rw_load_segment.so";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001384 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1385 ASSERT_TRUE(handle == nullptr);
1386 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W + E load segments are not allowed";
1387 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1388}
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001389
1390TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001391 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001392 "/" + kPrebuiltElfDir +
1393 "/libtest_invalid-unaligned_shdr_offset.so";
1394
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001395 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1396 ASSERT_TRUE(handle == nullptr);
1397 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
1398 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1399}
1400
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001401TEST(dlfcn, dlopen_invalid_zero_shentsize) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001402 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001403 "/" + kPrebuiltElfDir +
1404 "/libtest_invalid-zero_shentsize.so";
1405
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001406 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1407 ASSERT_TRUE(handle == nullptr);
1408 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x";
1409 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1410}
1411
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001412TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001413 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001414 "/" + kPrebuiltElfDir +
1415 "/libtest_invalid-zero_shstrndx.so";
1416
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001417 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1418 ASSERT_TRUE(handle == nullptr);
1419 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx";
1420 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1421}
1422
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001423TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001424 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001425 "/" + kPrebuiltElfDir +
1426 "/libtest_invalid-empty_shdr_table.so";
1427
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001428 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1429 ASSERT_TRUE(handle == nullptr);
1430 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers";
1431 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1432}
1433
Dimitry Ivanov46230442016-08-15 13:40:53 -07001434TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001435 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001436 "/" + kPrebuiltElfDir +
1437 "/libtest_invalid-zero_shdr_table_offset.so";
1438
Dimitry Ivanov46230442016-08-15 13:40:53 -07001439 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1440 ASSERT_TRUE(handle == nullptr);
1441 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/";
1442 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1443}
1444
Dimitry Ivanov55958342016-08-15 14:06:04 -07001445TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001446 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001447 "/" + kPrebuiltElfDir +
1448 "/libtest_invalid-zero_shdr_table_content.so";
1449
Dimitry Ivanov55958342016-08-15 14:06:04 -07001450 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1451 ASSERT_TRUE(handle == nullptr);
1452 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
1453 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1454}
1455
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001456TEST(dlfcn, dlopen_invalid_textrels) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001457 const std::string libpath = get_testlib_root() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001458 "/" + kPrebuiltElfDir +
1459 "/libtest_invalid-textrels.so";
1460
1461 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1462 ASSERT_TRUE(handle == nullptr);
1463 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1464 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1465}
1466
1467TEST(dlfcn, dlopen_invalid_textrels2) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001468 const std::string libpath = get_testlib_root() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001469 "/" + kPrebuiltElfDir +
1470 "/libtest_invalid-textrels2.so";
1471
1472 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1473 ASSERT_TRUE(handle == nullptr);
1474 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1475 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1476}
1477
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001478#endif