blob: 4ff324e3407c87d136f518ddb5e6054f3a8848a6 [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__)
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700250TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700251 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700252
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 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700257 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700258 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
259 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700260 ASSERT_TRUE(foo_ptr != nullptr);
261 ASSERT_TRUE(foo_library_ptr != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700262 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
263 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700264 dlclose(handle);
265
266 // then check the unset case
267 unsetenv("IFUNC_CHOICE");
268 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700269 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700270 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
271 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700272 ASSERT_TRUE(foo_ptr != nullptr);
273 ASSERT_TRUE(foo_library_ptr != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700274 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
275 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0);
276 dlclose(handle);
277}
278
Dimitry Ivanov01c888c2017-01-23 14:48:21 -0800279// ld.gold for arm produces incorrect binary (see http://b/27930475 for details)
280#if defined(__arm__)
281TEST(dlfcn, KNOWN_FAILURE_ON_BIONIC(ifunc_ctor_call)) {
282#else
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700283TEST(dlfcn, ifunc_ctor_call) {
Dimitry Ivanov01c888c2017-01-23 14:48:21 -0800284#endif
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700285 typedef const char* (*fn_ptr)();
286
287 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700288 ASSERT_TRUE(handle != nullptr) << dlerror();
289 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
290 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
291 ASSERT_STREQ("false", is_ctor_called());
292
293 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
294 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700295 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700296 dlclose(handle);
297}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700298
Dimitry Ivanov01c888c2017-01-23 14:48:21 -0800299// ld.gold for arm produces incorrect binary (see http://b/27930475 for details)
300#if defined(__arm__)
301TEST(dlfcn, KNOWN_FAILURE_ON_BIONIC(ifunc_ctor_call_rtld_lazy)) {
302#else
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700303TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
Dimitry Ivanov01c888c2017-01-23 14:48:21 -0800304#endif
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700305 typedef const char* (*fn_ptr)();
306
307 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
308 ASSERT_TRUE(handle != nullptr) << dlerror();
309 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
310 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
311 ASSERT_STREQ("false", is_ctor_called());
312
313 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
314 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
315 ASSERT_STREQ("true", is_ctor_called());
316 dlclose(handle);
317}
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700318#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700319
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700320TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
321 // This is the structure of the test library and
322 // its dt_needed libraries
323 // libtest_relo_check_dt_needed_order.so
324 // |
325 // +-> libtest_relo_check_dt_needed_order_1.so
326 // |
327 // +-> libtest_relo_check_dt_needed_order_2.so
328 //
329 // The root library references relo_test_get_answer_lib - which is defined
330 // in both dt_needed libraries, the correct relocation should
331 // use the function defined in libtest_relo_check_dt_needed_order_1.so
332 void* handle = nullptr;
Tom Cherryb8ab6182017-04-05 16:20:29 -0700333 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700334
335 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
336 ASSERT_TRUE(handle != nullptr) << dlerror();
337
338 typedef int (*fn_t) (void);
339 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
340 ASSERT_TRUE(fn != nullptr) << dlerror();
341 ASSERT_EQ(1, fn());
342}
343
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700344TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700345 // Here is how the test library and its dt_needed
346 // libraries are arranged
347 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700348 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700349 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700350 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700351 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700352 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700353 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700354 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700355 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700356 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700357 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700358 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700359 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700360 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700361 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700362 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700363 //
364 // load order should be (1, 2, 3, a, b, d)
365 //
366 // get_answer() is defined in (2, 3, a, b, c)
367 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700368 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700369 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700370 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
371 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700372 typedef int (*fn_t) (void);
373 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700374 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700375 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700376 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700377 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700378
379 ASSERT_EQ(42, fn());
380 ASSERT_EQ(43, fn2());
381 dlclose(handle);
382}
383
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700384TEST(dlfcn, dlopen_check_order_reloc_siblings) {
385 // This is how this one works:
386 // we lookup and call get_answer which is defined in '_2.so'
387 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
388 // the correct _impl() is implemented by '_a.so';
389 //
390 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
391 //
392 // Here is the picture:
393 //
394 // libtest_check_order_reloc_siblings.so
395 // |
396 // +-> ..._1.so <- empty
397 // | |
398 // | +-> ..._a.so <- exports correct answer_impl()
399 // | |
400 // | +-> ..._b.so <- every other letter exporting incorrect one.
401 // |
402 // +-> ..._2.so <- empty
403 // | |
404 // | +-> ..._c.so
405 // | |
406 // | +-> ..._d.so
407 // |
408 // +-> ..._3.so <- empty
409 // |
410 // +-> ..._e.so
411 // |
412 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
413 // implements incorrect get_answer_impl()
414
415 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
416 ASSERT_TRUE(handle == nullptr);
417#ifdef __BIONIC__
418 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
419 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
420#endif
421
422 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
423 ASSERT_TRUE(handle != nullptr) << dlerror();
424
425 typedef int (*fn_t) (void);
426 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
427 ASSERT_TRUE(fn != nullptr) << dlerror();
428 ASSERT_EQ(42, fn());
429
430 ASSERT_EQ(0, dlclose(handle));
431}
432
433TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
434 // This test uses the same library as dlopen_check_order_reloc_siblings.
435 // Unlike dlopen_check_order_reloc_siblings it preloads
436 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
437 // dlopen(libtest_check_order_reloc_siblings.so)
438
439 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
440 ASSERT_TRUE(handle == nullptr);
441 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
442 ASSERT_TRUE(handle == nullptr);
443
444 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
445 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
446
447 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
448 ASSERT_TRUE(handle != nullptr) << dlerror();
449
450 ASSERT_EQ(0, dlclose(handle_for_1));
451
452 typedef int (*fn_t) (void);
453 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
454 ASSERT_TRUE(fn != nullptr) << dlerror();
455 ASSERT_EQ(42, fn());
456
457 ASSERT_EQ(0, dlclose(handle));
458}
459
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800460TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
461 // This is how this one works:
462 // we lookup and call grandchild_get_answer which is defined in '_2.so'
463 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
464 // the correct _impl() is implemented by '_c_1.so';
465 //
466 // Here is the picture of subtree:
467 //
468 // libtest_check_order_reloc_siblings.so
469 // |
470 // +-> ..._2.so <- grandchild_get_answer()
471 // |
472 // +-> ..._c.so <- empty
473 // | |
474 // | +-> _c_1.so <- exports correct answer_impl()
475 // | |
476 // | +-> _c_2.so <- exports incorrect answer_impl()
477 // |
478 // +-> ..._d.so <- empty
479
480 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
481 ASSERT_TRUE(handle == nullptr);
482#ifdef __BIONIC__
483 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
484 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
485#endif
486
487 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
488 ASSERT_TRUE(handle != nullptr) << dlerror();
489
490 typedef int (*fn_t) (void);
491 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
492 ASSERT_TRUE(fn != nullptr) << dlerror();
493 ASSERT_EQ(42, fn());
494
495 ASSERT_EQ(0, dlclose(handle));
496}
497
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700498TEST(dlfcn, dlopen_check_order_reloc_nephew) {
499 // This is how this one works:
500 // we lookup and call nephew_get_answer which is defined in '_2.so'
501 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
502 // the correct _impl() is implemented by '_a.so';
503 //
504 // Here is the picture:
505 //
506 // libtest_check_order_reloc_siblings.so
507 // |
508 // +-> ..._1.so <- empty
509 // | |
510 // | +-> ..._a.so <- exports correct answer_impl()
511 // | |
512 // | +-> ..._b.so <- every other letter exporting incorrect one.
513 // |
514 // +-> ..._2.so <- empty
515 // | |
516 // | +-> ..._c.so
517 // | |
518 // | +-> ..._d.so
519 // |
520 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
521 // |
522 // +-> ..._e.so
523 // |
524 // +-> ..._f.so
525
526 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
527 ASSERT_TRUE(handle == nullptr);
528#ifdef __BIONIC__
529 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
530 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
531#endif
532
533 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
534 ASSERT_TRUE(handle != nullptr) << dlerror();
535
536 typedef int (*fn_t) (void);
537 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
538 ASSERT_TRUE(fn != nullptr) << dlerror();
539 ASSERT_EQ(42, fn());
540
541 ASSERT_EQ(0, dlclose(handle));
542}
543
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800544TEST(dlfcn, check_unload_after_reloc) {
545 // This is how this one works:
546 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
547 // |
548 // +-> libtest_two_parents_child
549 //
550 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
551 // |
552 // +-> libtest_two_parents_child
553 //
554 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
555 // as a second step it dlopens parent2 and dlcloses parent1...
556
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700557 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
558 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800559
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700560 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
561 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800562
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700563 typedef int (*fn_t) (void);
564 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
565 ASSERT_TRUE(fn != nullptr) << dlerror();
566 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800567
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700568 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800569
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700570 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
571 ASSERT_TRUE(handle != nullptr);
572 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800573
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700574 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
575 ASSERT_TRUE(fn != nullptr) << dlerror();
576 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800577
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700578 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800579
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700580 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
581 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800582}
583
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700584extern "C" int check_order_reloc_root_get_answer_impl() {
585 return 42;
586}
587
588TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
589 // This is how this one works:
590 // we lookup and call get_answer3 which is defined in 'root.so'
591 // and in turn calls external root_get_answer_impl() defined in _2.so and
592 // above the correct _impl() is one in the executable.
593 //
594 // libtest_check_order_reloc_root.so
595 // |
596 // +-> ..._1.so <- empty
597 // |
598 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
599 //
600
601 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
602 ASSERT_TRUE(handle == nullptr);
603#ifdef __BIONIC__
604 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
605 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
606#endif
607
608 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
609 ASSERT_TRUE(handle != nullptr) << dlerror();
610
611 typedef int (*fn_t) (void);
612 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
613 ASSERT_TRUE(fn != nullptr) << dlerror();
614 ASSERT_EQ(42, fn());
615
616 ASSERT_EQ(0, dlclose(handle));
617}
618
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700619TEST(dlfcn, dlopen_check_rtld_local) {
620 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
621 ASSERT_TRUE(sym == nullptr);
622
623 // implicit RTLD_LOCAL
624 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
625 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
626 ASSERT_TRUE(sym == nullptr);
627 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
628 sym = dlsym(handle, "dlopen_testlib_simple_func");
629 ASSERT_TRUE(sym != nullptr);
630 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
631 dlclose(handle);
632
633 // explicit RTLD_LOCAL
634 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
635 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
636 ASSERT_TRUE(sym == nullptr);
637 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
638 sym = dlsym(handle, "dlopen_testlib_simple_func");
639 ASSERT_TRUE(sym != nullptr);
640 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
641 dlclose(handle);
642}
643
644TEST(dlfcn, dlopen_check_rtld_global) {
645 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
646 ASSERT_TRUE(sym == nullptr);
647
648 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700649 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700650 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
651 ASSERT_TRUE(sym != nullptr) << dlerror();
652 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
653 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700654
655 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
656 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
657 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700658
659 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
660 // shared libraries after symbol was not found in the main executable
661 // and dependent libraries.
662 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
663 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
664 ASSERT_TRUE(sym != nullptr) << dlerror();
665
666 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700667}
668
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700669// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
670// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
671// libtest_with_dependency_loop_a.so
672TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700673 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
674 ASSERT_TRUE(handle != nullptr) << dlerror();
675 void* f = dlsym(handle, "dlopen_test_loopy_function");
676 ASSERT_TRUE(f != nullptr) << dlerror();
677 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
678 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700679
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700680 // dlopen second time to make sure that the library was unloaded correctly
681 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
682 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800683#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700684 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 -0800685#else
686 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
687 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700688#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800689
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700690 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
691 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700692}
693
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700694TEST(dlfcn, dlopen_nodelete) {
695 static bool is_unloaded = false;
696
697 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
698 ASSERT_TRUE(handle != nullptr) << dlerror();
699 void (*set_unload_flag_ptr)(bool*);
700 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
701 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
702 set_unload_flag_ptr(&is_unloaded);
703
704 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
705 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
706 ASSERT_EQ(1729U, *taxicab_number);
707 *taxicab_number = 2;
708
709 dlclose(handle);
710 ASSERT_TRUE(!is_unloaded);
711
712 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
713 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
714 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
715
716
717 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
718 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
719 ASSERT_EQ(taxicab_number2, taxicab_number);
720
721 ASSERT_EQ(2U, *taxicab_number2);
722
723 dlclose(handle);
724 ASSERT_TRUE(!is_unloaded);
725}
726
727TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
728 static bool is_unloaded = false;
729
730 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
731 ASSERT_TRUE(handle != nullptr) << dlerror();
732 void (*set_unload_flag_ptr)(bool*);
733 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
734 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
735 set_unload_flag_ptr(&is_unloaded);
736
737 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
738 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
739
740 ASSERT_EQ(1729U, *taxicab_number);
741 *taxicab_number = 2;
742
743 // This RTLD_NODELETE should be ignored
744 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
745 ASSERT_TRUE(handle1 != nullptr) << dlerror();
746 ASSERT_EQ(handle, handle1);
747
748 dlclose(handle1);
749 dlclose(handle);
750
751 ASSERT_TRUE(is_unloaded);
752}
753
754TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
755 static bool is_unloaded = false;
756
757 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
758 ASSERT_TRUE(handle != nullptr) << dlerror();
759 void (*set_unload_flag_ptr)(bool*);
760 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
761 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
762 set_unload_flag_ptr(&is_unloaded);
763
764 dlclose(handle);
765 ASSERT_TRUE(!is_unloaded);
766}
767
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700768TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700769 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
770 ASSERT_TRUE(handle != nullptr) << dlerror();
771 int (*get_answer)();
772 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
773 ASSERT_TRUE(get_answer != nullptr) << dlerror();
774 ASSERT_EQ(42, get_answer());
775 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700776}
777
Elliott Hughese66190d2012-12-18 15:57:55 -0800778TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700779 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700780 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700781#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700782 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
783#else
784 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
785#endif
786}
787
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800788static void ConcurrentDlErrorFn(std::string& error) {
789 ASSERT_TRUE(dlerror() == nullptr);
790
791 void* handle = dlopen("/child/thread", RTLD_NOW);
792 ASSERT_TRUE(handle == nullptr);
793
794 const char* err = dlerror();
795 ASSERT_TRUE(err != nullptr);
796
797 error = err;
798}
799
800TEST(dlfcn, dlerror_concurrent_buffer) {
801 void* handle = dlopen("/main/thread", RTLD_NOW);
802 ASSERT_TRUE(handle == nullptr);
803 const char* main_thread_error = dlerror();
804 ASSERT_TRUE(main_thread_error != nullptr);
805 ASSERT_SUBSTR("/main/thread", main_thread_error);
806
807 std::string child_thread_error;
808 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
809 t.join();
810 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
811
812 // Check that main thread local buffer was not modified.
813 ASSERT_SUBSTR("/main/thread", main_thread_error);
Elliott Hughes5419b942012-10-16 15:54:46 -0700814}
815
Elliott Hughese66190d2012-12-18 15:57:55 -0800816TEST(dlfcn, dlerror_concurrent) {
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800817 void* handle = dlopen("/main/thread", RTLD_NOW);
818 ASSERT_TRUE(handle == nullptr);
819
820 std::string child_thread_error;
821 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
822 t.join();
823 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
824
Elliott Hughes5419b942012-10-16 15:54:46 -0700825 const char* main_thread_error = dlerror();
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800826 ASSERT_TRUE(main_thread_error != nullptr);
Elliott Hughes5419b942012-10-16 15:54:46 -0700827 ASSERT_SUBSTR("/main/thread", main_thread_error);
828}
829
Elliott Hughese66190d2012-12-18 15:57:55 -0800830TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700831 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700832 void* self = dlopen(nullptr, RTLD_NOW);
833 ASSERT_TRUE(self != nullptr);
834 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700835
836 void* sym;
837
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700838#if defined(__BIONIC__) && !defined(__LP64__)
839 // RTLD_DEFAULT in lp32 bionic is not (void*)0
840 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700841 sym = dlsym(nullptr, "test");
842 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800843 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700844#endif
845
Elliott Hughes3b297c42012-10-11 16:08:51 -0700846 // Symbol that doesn't exist.
847 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700848 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700849 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700850
851 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700852}
853
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700854TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700855 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700856 void* self = dlopen(nullptr, RTLD_NOW);
857 ASSERT_TRUE(self != nullptr);
858 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700859
860 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700861 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700862
863 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
864 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
865
866 Dl_info info;
867 int rc = dladdr(addr, &info);
868 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
869
870 // Get the name of this executable.
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700871 const std::string& executable_path = get_executable_path();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700872
873 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700874 char dli_realpath[PATH_MAX];
875 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700876 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700877
878 // The symbol name should be the symbol we looked up.
879 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
880
881 // The address should be the exact address of the symbol.
882 ASSERT_EQ(info.dli_saddr, sym);
883
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700884 std::vector<map_record> maps;
885 ASSERT_TRUE(Maps::parse_maps(&maps));
886
887 void* base_address = nullptr;
888 for (const map_record& rec : maps) {
889 if (executable_path == rec.pathname) {
890 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700891 break;
892 }
893 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700894
895 // The base address should be the address we were loaded at.
896 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700897
898 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700899}
900
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700901TEST(dlfcn, dlopen_executable_by_absolute_path) {
902 void* handle1 = dlopen(nullptr, RTLD_NOW);
903 ASSERT_TRUE(handle1 != nullptr) << dlerror();
904
905 void* handle2 = dlopen(get_executable_path().c_str(), RTLD_NOW);
906 ASSERT_TRUE(handle2 != nullptr) << dlerror();
907
908#if defined(__BIONIC__)
909 ASSERT_EQ(handle1, handle2);
910#else
911 GTEST_LOG_(INFO) << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
912 "it loads a separate copy of the main executable "
913 "on dlopen by absolute path.";
914#endif
915}
916
Victor Khimenko14b9d712017-01-25 19:59:16 +0100917#if defined (__aarch64__)
918#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm64/"
919#elif defined (__arm__)
920#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm/"
921#elif defined (__i386__)
922#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86/"
923#elif defined (__x86_64__)
924#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86_64/"
925#elif defined (__mips__)
926#if defined(__LP64__)
927#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips64/"
928#else
929#define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips/"
930#endif
931#else
932#error "Unknown architecture"
933#endif
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200934#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Victor Khimenko14b9d712017-01-25 19:59:16 +0100935#define ALTERNATE_PATH_TO_LIBC ALTERNATE_PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700936
937TEST(dlfcn, dladdr_libc) {
938#if defined(__BIONIC__)
939 Dl_info info;
940 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
941 ASSERT_TRUE(dladdr(addr, &info) != 0);
942
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700943 char libc_realpath[PATH_MAX];
Victor Khimenko14b9d712017-01-25 19:59:16 +0100944
945 // Check if libc is in canonical path or in alternate path.
946 if (strncmp(ALTERNATE_PATH_TO_SYSTEM_LIB,
947 info.dli_fname,
948 sizeof(ALTERNATE_PATH_TO_SYSTEM_LIB) - 1) == 0) {
949 // Platform with emulated architecture. Symlink on ARC++.
950 ASSERT_TRUE(realpath(ALTERNATE_PATH_TO_LIBC, libc_realpath) == libc_realpath);
951 } else {
952 // /system/lib is symlink when this test is executed on host.
953 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
954 }
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700955
956 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700957 // TODO: add check for dfi_fbase
958 ASSERT_STREQ("puts", info.dli_sname);
959 ASSERT_EQ(addr, info.dli_saddr);
960#else
961 GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig "
962 "for libc.so, which is symlink itself (not a realpath).\n";
963#endif
964}
965
Elliott Hughese66190d2012-12-18 15:57:55 -0800966TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700967 Dl_info info;
968
Elliott Hughes3b297c42012-10-11 16:08:51 -0700969 dlerror(); // Clear any pending errors.
970
Elliott Hughes8e15b082012-09-26 11:44:01 -0700971 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700972 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
973 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700974
975 // No symbol corresponding to a stack address.
976 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700977 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700978}
Elliott Hughes124fae92012-10-31 14:20:03 -0700979
Elliott Hughesa43e9062013-01-07 14:18:22 -0800980// GNU-style ELF hash tables are incompatible with the MIPS ABI.
981// 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 -0800982TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800983#if !defined(__mips__)
Elliott Hughes124fae92012-10-31 14:20:03 -0700984 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800985 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
986 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -0700987 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800988 void* sym = dlsym(handle, "getRandomNumber");
989 ASSERT_TRUE(sym != nullptr) << dlerror();
990 int (*fn)(void);
991 fn = reinterpret_cast<int (*)(void)>(sym);
992 EXPECT_EQ(4, fn());
993
994 Dl_info dlinfo;
995 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
996
997 ASSERT_TRUE(fn == dlinfo.dli_saddr);
998 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800999 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001000#else
1001 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
1002#endif
Elliott Hughes124fae92012-10-31 14:20:03 -07001003}
Elliott Hughese66190d2012-12-18 15:57:55 -08001004
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001005TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
1006 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
1007 ASSERT_TRUE(handle != nullptr) << dlerror();
Tom Cherryb8ab6182017-04-05 16:20:29 -07001008 auto guard = android::base::make_scope_guard([&]() { dlclose(handle); });
Dmitriy Ivanovb3356772014-11-14 11:19:22 -08001009 void* sym = dlsym(handle, "getRandomNumber");
1010 ASSERT_TRUE(sym != nullptr) << dlerror();
1011 int (*fn)(void);
1012 fn = reinterpret_cast<int (*)(void)>(sym);
1013 EXPECT_EQ(4, fn());
1014
1015 Dl_info dlinfo;
1016 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
1017
1018 ASSERT_TRUE(fn == dlinfo.dli_saddr);
1019 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
1020 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
1021}
1022
Elliott Hughese66190d2012-12-18 15:57:55 -08001023TEST(dlfcn, dlopen_bad_flags) {
1024 dlerror(); // Clear any pending errors.
1025 void* handle;
1026
Elliott Hughes063525c2014-05-13 11:19:57 -07001027#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -08001028 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001029 handle = dlopen(nullptr, 0);
1030 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001031 ASSERT_SUBSTR("invalid", dlerror());
1032#endif
1033
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001034 handle = dlopen(nullptr, 0xffffffff);
1035 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -08001036 ASSERT_SUBSTR("invalid", dlerror());
1037
1038 // 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 -07001039 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
1040 ASSERT_TRUE(handle != nullptr);
1041 ASSERT_SUBSTR(nullptr, dlerror());
Elliott Hughese66190d2012-12-18 15:57:55 -08001042}
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001043
1044TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001045 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001046 ASSERT_TRUE(addr == nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001047}
1048
1049TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001050 void* addr = dlsym(RTLD_DEFAULT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001051 ASSERT_TRUE(addr != nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001052}
1053
1054TEST(dlfcn, rtld_next_unknown_symbol) {
1055 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001056 ASSERT_TRUE(addr == nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001057}
1058
1059TEST(dlfcn, rtld_next_known_symbol) {
1060 void* addr = dlsym(RTLD_NEXT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001061 ASSERT_TRUE(addr != nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001062}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001063
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001064// Check that RTLD_NEXT of a libc symbol works in dlopened library
1065TEST(dlfcn, rtld_next_from_library) {
1066 void* library_with_close = dlopen("libtest_check_rtld_next_from_library.so", RTLD_NOW);
1067 ASSERT_TRUE(library_with_close != nullptr) << dlerror();
1068 void* expected_addr = dlsym(RTLD_DEFAULT, "close");
1069 ASSERT_TRUE(expected_addr != nullptr) << dlerror();
1070 typedef void* (*get_libc_close_ptr_fn_t)();
1071 get_libc_close_ptr_fn_t get_libc_close_ptr =
1072 reinterpret_cast<get_libc_close_ptr_fn_t>(dlsym(library_with_close, "get_libc_close_ptr"));
1073 ASSERT_TRUE(get_libc_close_ptr != nullptr) << dlerror();
1074 ASSERT_EQ(expected_addr, get_libc_close_ptr());
1075
1076 dlclose(library_with_close);
1077}
1078
1079
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001080TEST(dlfcn, dlsym_weak_func) {
1081 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001082 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001083 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001084
1085 int (*weak_func)();
1086 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001087 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001088 EXPECT_EQ(42, weak_func());
1089 dlclose(handle);
1090}
1091
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001092TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001093 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
1094 ASSERT_TRUE(handle != nullptr) << dlerror();
1095 int (*weak_func)();
1096 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
1097 ASSERT_TRUE(weak_func != nullptr) << dlerror();
1098 EXPECT_EQ(6551, weak_func());
1099 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001100}
1101
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001102TEST(dlfcn, dlopen_symlink) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001103 DlfcnSymlink symlink("dlopen_symlink");
1104 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001105 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001106 void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001107 ASSERT_TRUE(handle1 != nullptr);
1108 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001109 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -07001110 dlclose(handle1);
1111 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001112}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001113
1114// libtest_dlopen_from_ctor_main.so depends on
1115// libtest_dlopen_from_ctor.so which has a constructor
1116// that calls dlopen(libc...). This is to test the situation
1117// described in b/7941716.
1118TEST(dlfcn, dlopen_dlopen_from_ctor) {
1119#if defined(__BIONIC__)
1120 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1121 ASSERT_TRUE(handle != nullptr) << dlerror();
1122 dlclose(handle);
1123#else
1124 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
1125#endif
1126}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001127
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001128static std::string g_fini_call_order_str;
1129
1130static void register_fini_call(const char* s) {
1131 g_fini_call_order_str += s;
1132}
1133
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001134static void test_init_fini_call_order_for(const char* libname) {
1135 g_fini_call_order_str.clear();
1136 void* handle = dlopen(libname, RTLD_NOW);
Dimitry Ivanovea8f3962017-02-09 13:31:57 -08001137 ASSERT_TRUE(handle != nullptr) << dlerror();
1138 typedef int (*get_init_order_number_t)();
1139 get_init_order_number_t get_init_order_number =
1140 reinterpret_cast<get_init_order_number_t>(dlsym(handle, "get_init_order_number"));
1141 ASSERT_EQ(321, get_init_order_number());
1142
1143 typedef void (*set_fini_callback_t)(void (*f)(const char*));
1144 set_fini_callback_t set_fini_callback =
1145 reinterpret_cast<set_fini_callback_t>(dlsym(handle, "set_fini_callback"));
1146 set_fini_callback(register_fini_call);
1147 dlclose(handle);
1148 ASSERT_EQ("(root)(child)(grandchild)", g_fini_call_order_str);
1149}
1150
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001151TEST(dlfcn, init_fini_call_order) {
1152 test_init_fini_call_order_for("libtest_init_fini_order_root.so");
1153 test_init_fini_call_order_for("libtest_init_fini_order_root2.so");
1154}
1155
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001156TEST(dlfcn, symbol_versioning_use_v1) {
1157 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1158 ASSERT_TRUE(handle != nullptr) << dlerror();
1159 typedef int (*fn_t)();
1160 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1161 ASSERT_TRUE(fn != nullptr) << dlerror();
1162 ASSERT_EQ(1, fn());
1163 dlclose(handle);
1164}
1165
1166TEST(dlfcn, symbol_versioning_use_v2) {
1167 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1168 ASSERT_TRUE(handle != nullptr) << dlerror();
1169 typedef int (*fn_t)();
1170 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1171 ASSERT_TRUE(fn != nullptr) << dlerror();
1172 ASSERT_EQ(2, fn());
1173 dlclose(handle);
1174}
1175
1176TEST(dlfcn, symbol_versioning_use_other_v2) {
1177 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1178 ASSERT_TRUE(handle != nullptr) << dlerror();
1179 typedef int (*fn_t)();
1180 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1181 ASSERT_TRUE(fn != nullptr) << dlerror();
1182 ASSERT_EQ(20, fn());
1183 dlclose(handle);
1184}
1185
1186TEST(dlfcn, symbol_versioning_use_other_v3) {
1187 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1188 ASSERT_TRUE(handle != nullptr) << dlerror();
1189 typedef int (*fn_t)();
1190 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1191 ASSERT_TRUE(fn != nullptr) << dlerror();
1192 ASSERT_EQ(3, fn());
1193 dlclose(handle);
1194}
1195
1196TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1197 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1198 ASSERT_TRUE(handle != nullptr) << dlerror();
1199 typedef int (*fn_t)();
1200 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1201 ASSERT_TRUE(fn != nullptr) << dlerror();
1202 ASSERT_EQ(3, fn()); // the default version is 3
1203 dlclose(handle);
1204}
1205
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001206TEST(dlfcn, dlvsym_smoke) {
1207 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1208 ASSERT_TRUE(handle != nullptr) << dlerror();
1209 typedef int (*fn_t)();
1210
1211 {
1212 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1213 ASSERT_TRUE(fn == nullptr);
1214 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1215 }
1216
1217 {
1218 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1219 ASSERT_TRUE(fn != nullptr) << dlerror();
1220 ASSERT_EQ(2, fn());
1221 }
1222
1223 dlclose(handle);
1224}
1225
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001226// This preempts the implementation from libtest_versioned_lib.so
1227extern "C" int version_zero_function() {
1228 return 0;
1229}
1230
1231// This preempts the implementation from libtest_versioned_uselibv*.so
1232extern "C" int version_zero_function2() {
1233 return 0;
1234}
Evgenii Stepanov68650822015-06-10 13:38:39 -07001235
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001236TEST(dlfcn, dt_runpath_smoke) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001237 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1238 ASSERT_TRUE(handle != nullptr) << dlerror();
1239
1240 typedef void *(* dlopen_b_fn)();
1241 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1242 ASSERT_TRUE(fn != nullptr) << dlerror();
1243
1244 void *p = fn();
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001245 ASSERT_TRUE(p != nullptr);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001246
1247 dlclose(handle);
1248}
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001249
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001250// Bionic specific tests
1251#if defined(__BIONIC__)
1252
Dimitry Ivanovfc32dcb2017-03-24 10:58:23 -07001253#if defined(__arm__)
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001254const llvm::ELF::Elf32_Dyn* to_dynamic_table(const char* p) {
1255 return reinterpret_cast<const llvm::ELF::Elf32_Dyn*>(p);
1256}
1257
1258// Duplicate these definitions here because they are android specific
1259// note that we cannot include <elf.h> because #defines conflict with
1260// enum names provided by LLVM.
1261#define DT_ANDROID_REL (llvm::ELF::DT_LOOS + 2)
1262#define DT_ANDROID_RELA (llvm::ELF::DT_LOOS + 4)
1263
1264template<typename ELFT>
1265void validate_compatibility_of_native_library(const std::string& path, ELFT* elf) {
1266 bool has_elf_hash = false;
1267 bool has_android_rel = false;
1268 bool has_rel = false;
1269 // Find dynamic section and check that DT_HASH and there is no DT_ANDROID_REL
1270 for (auto it = elf->section_begin(); it != elf->section_end(); ++it) {
1271 const llvm::object::ELFSectionRef& section_ref = *it;
1272 if (section_ref.getType() == llvm::ELF::SHT_DYNAMIC) {
1273 llvm::StringRef data;
1274 ASSERT_TRUE(!it->getContents(data)) << "unable to get SHT_DYNAMIC section data";
1275 for (auto d = to_dynamic_table(data.data()); d->d_tag != llvm::ELF::DT_NULL; ++d) {
1276 if (d->d_tag == llvm::ELF::DT_HASH) {
1277 has_elf_hash = true;
1278 } else if (d->d_tag == DT_ANDROID_REL || d->d_tag == DT_ANDROID_RELA) {
1279 has_android_rel = true;
1280 } else if (d->d_tag == llvm::ELF::DT_REL || d->d_tag == llvm::ELF::DT_RELA) {
1281 has_rel = true;
1282 }
1283 }
1284
1285 break;
1286 }
1287 }
1288
1289 ASSERT_TRUE(has_elf_hash) << path.c_str() << ": missing elf hash (DT_HASH)";
1290 ASSERT_TRUE(!has_android_rel) << path.c_str() << ": has packed relocations";
1291 ASSERT_TRUE(has_rel) << path.c_str() << ": missing DT_REL/DT_RELA";
1292}
1293
1294void validate_compatibility_of_native_library(const char* soname) {
Victor Khimenko14b9d712017-01-25 19:59:16 +01001295 // On the systems with emulation system libraries would be of different
1296 // architecture. Try to use alternate paths first.
1297 std::string path = std::string(ALTERNATE_PATH_TO_SYSTEM_LIB) + soname;
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001298 auto binary_or_error = llvm::object::createBinary(path);
Victor Khimenko14b9d712017-01-25 19:59:16 +01001299 if (!binary_or_error) {
1300 path = std::string(PATH_TO_SYSTEM_LIB) + soname;
1301 binary_or_error = llvm::object::createBinary(path);
1302 }
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001303 ASSERT_FALSE(!binary_or_error);
1304
1305 llvm::object::Binary* binary = binary_or_error.get().getBinary();
1306
1307 auto obj = llvm::dyn_cast<llvm::object::ObjectFile>(binary);
1308 ASSERT_TRUE(obj != nullptr);
1309
1310 auto elf = llvm::dyn_cast<llvm::object::ELF32LEObjectFile>(obj);
1311
1312 ASSERT_TRUE(elf != nullptr);
1313
1314 validate_compatibility_of_native_library(path, elf);
1315}
1316
Dimitry Ivanovfc32dcb2017-03-24 10:58:23 -07001317// This is a test for app compatibility workaround for arm apps
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001318// affected by http://b/24465209
1319TEST(dlext, compat_elf_hash_and_relocation_tables) {
1320 validate_compatibility_of_native_library("libc.so");
1321 validate_compatibility_of_native_library("liblog.so");
1322 validate_compatibility_of_native_library("libstdc++.so");
1323 validate_compatibility_of_native_library("libdl.so");
1324 validate_compatibility_of_native_library("libm.so");
1325 validate_compatibility_of_native_library("libz.so");
1326 validate_compatibility_of_native_library("libjnigraphics.so");
1327}
1328
Dimitry Ivanovfc32dcb2017-03-24 10:58:23 -07001329#endif // defined(__arm__)
Dimitry Ivanovac4bd2f2016-11-21 12:50:38 -08001330
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001331TEST(dlfcn, dt_runpath_absolute_path) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001332 std::string libpath = get_testlib_root() + "/libtest_dt_runpath_d.so";
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001333 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001334 ASSERT_TRUE(handle != nullptr) << dlerror();
1335
1336 typedef void *(* dlopen_b_fn)();
1337 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1338 ASSERT_TRUE(fn != nullptr) << dlerror();
1339
1340 void *p = fn();
1341 ASSERT_TRUE(p != nullptr);
1342
1343 dlclose(handle);
1344}
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001345
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001346TEST(dlfcn, dlopen_invalid_rw_load_segment) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001347 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001348 "/" + kPrebuiltElfDir +
1349 "/libtest_invalid-rw_load_segment.so";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001350 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1351 ASSERT_TRUE(handle == nullptr);
1352 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W + E load segments are not allowed";
1353 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1354}
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001355
1356TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001357 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001358 "/" + kPrebuiltElfDir +
1359 "/libtest_invalid-unaligned_shdr_offset.so";
1360
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001361 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1362 ASSERT_TRUE(handle == nullptr);
1363 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
1364 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1365}
1366
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001367TEST(dlfcn, dlopen_invalid_zero_shentsize) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001368 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001369 "/" + kPrebuiltElfDir +
1370 "/libtest_invalid-zero_shentsize.so";
1371
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001372 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1373 ASSERT_TRUE(handle == nullptr);
1374 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x";
1375 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1376}
1377
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001378TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001379 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001380 "/" + kPrebuiltElfDir +
1381 "/libtest_invalid-zero_shstrndx.so";
1382
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001383 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1384 ASSERT_TRUE(handle == nullptr);
1385 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx";
1386 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1387}
1388
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001389TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001390 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001391 "/" + kPrebuiltElfDir +
1392 "/libtest_invalid-empty_shdr_table.so";
1393
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001394 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1395 ASSERT_TRUE(handle == nullptr);
1396 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers";
1397 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1398}
1399
Dimitry Ivanov46230442016-08-15 13:40:53 -07001400TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001401 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001402 "/" + kPrebuiltElfDir +
1403 "/libtest_invalid-zero_shdr_table_offset.so";
1404
Dimitry Ivanov46230442016-08-15 13:40:53 -07001405 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1406 ASSERT_TRUE(handle == nullptr);
1407 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/";
1408 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1409}
1410
Dimitry Ivanov55958342016-08-15 14:06:04 -07001411TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001412 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001413 "/" + kPrebuiltElfDir +
1414 "/libtest_invalid-zero_shdr_table_content.so";
1415
Dimitry Ivanov55958342016-08-15 14:06:04 -07001416 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1417 ASSERT_TRUE(handle == nullptr);
1418 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
1419 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1420}
1421
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001422TEST(dlfcn, dlopen_invalid_textrels) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001423 const std::string libpath = get_testlib_root() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001424 "/" + kPrebuiltElfDir +
1425 "/libtest_invalid-textrels.so";
1426
1427 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1428 ASSERT_TRUE(handle == nullptr);
1429 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1430 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1431}
1432
1433TEST(dlfcn, dlopen_invalid_textrels2) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001434 const std::string libpath = get_testlib_root() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001435 "/" + kPrebuiltElfDir +
1436 "/libtest_invalid-textrels2.so";
1437
1438 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1439 ASSERT_TRUE(handle == nullptr);
1440 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1441 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1442}
1443
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001444#endif