blob: 46f6ec04a766e3aeb41bb830cd876e9b3926e281 [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
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -070025#include "private/ScopeGuard.h"
26
Elliott Hughes8e15b082012-09-26 11:44:01 -070027#include <string>
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -080028#include <thread>
jeffhaoacf5aa72012-09-12 17:25:30 -070029
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
Elliott Hughes3b297c42012-10-11 16:08:51 -070034#define ASSERT_SUBSTR(needle, haystack) \
35 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
36
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070037
Elliott Hughes1728b232014-05-14 10:02:03 -070038static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070039extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070040 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070041}
42
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070043static int g_ctor_function_called = 0;
Dimitry Ivanov55437462016-07-20 15:33:07 -070044static int g_ctor_argc = 0;
45static char** g_ctor_argv = reinterpret_cast<char**>(0xDEADBEEF);
46static char** g_ctor_envp = g_ctor_envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070047
Dimitry Ivanov55437462016-07-20 15:33:07 -070048extern "C" void ctor_function(int argc, char** argv, char** envp) __attribute__ ((constructor));
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070049
Dimitry Ivanov55437462016-07-20 15:33:07 -070050extern "C" void ctor_function(int argc, char** argv, char** envp) {
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070051 g_ctor_function_called = 17;
Dimitry Ivanov55437462016-07-20 15:33:07 -070052 g_ctor_argc = argc;
53 g_ctor_argv = argv;
54 g_ctor_envp = envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070055}
56
57TEST(dlfcn, ctor_function_call) {
58 ASSERT_EQ(17, g_ctor_function_called);
Dimitry Ivanov55437462016-07-20 15:33:07 -070059 ASSERT_TRUE(g_ctor_argc = get_argc());
60 ASSERT_TRUE(g_ctor_argv = get_argv());
61 ASSERT_TRUE(g_ctor_envp = get_envp());
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070062}
63
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070064TEST(dlfcn, dlsym_in_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070065 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070066 void* self = dlopen(nullptr, RTLD_NOW);
67 ASSERT_TRUE(self != nullptr);
68 ASSERT_TRUE(dlerror() == nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070069
70 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070071 ASSERT_TRUE(sym != nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070072
73 void (*function)() = reinterpret_cast<void(*)()>(sym);
74
Elliott Hughes1728b232014-05-14 10:02:03 -070075 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070076 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070077 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070078
79 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070080}
Elliott Hughes8e15b082012-09-26 11:44:01 -070081
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070082TEST(dlfcn, dlsym_from_sofile) {
83 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL);
84 ASSERT_TRUE(handle != nullptr) << dlerror();
85
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070086 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070087 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
88 ASSERT_TRUE(symbol == nullptr);
89 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
90
91 typedef int* (*fn_t)();
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070092 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
93 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
94 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070095
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070096 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070097 ASSERT_TRUE(ptr != nullptr) << dlerror();
98 ASSERT_EQ(42, *ptr);
99
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700100 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
101 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
102 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
103
104 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
105 ASSERT_TRUE(ptr != nullptr) << dlerror();
106 ASSERT_EQ(44, *ptr);
107
108 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
109 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
110 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
111
112 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
113 ASSERT_TRUE(ptr != nullptr) << dlerror();
114 ASSERT_EQ(43, *ptr);
115
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700116 dlclose(handle);
117}
118
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700119TEST(dlfcn, dlsym_from_sofile_with_preload) {
120 void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL);
121 ASSERT_TRUE(preload != nullptr) << dlerror();
122
123 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
124 ASSERT_TRUE(handle != nullptr) << dlerror();
125
126 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
127 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
128 ASSERT_TRUE(symbol == nullptr);
129 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
130
131 typedef int* (*fn_t)();
132 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
133 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
134 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
135
136 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
137 ASSERT_TRUE(ptr != nullptr) << dlerror();
138 ASSERT_EQ(42, *ptr);
139
140 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
141 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
142 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
143
144 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
145 ASSERT_TRUE(ptr != nullptr) << dlerror();
146 ASSERT_EQ(44, *ptr);
147
148 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
149 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
150 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
151
152 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
153 ASSERT_TRUE(ptr != nullptr) << dlerror();
154 ASSERT_EQ(43, *ptr);
155
156 dlclose(handle);
157 dlclose(preload);
158}
159
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700160TEST(dlfcn, dlsym_handle_global_sym) {
161 // check that we do not look into global group
162 // when looking up symbol by handle
163 void* handle = dlopen("libtest_empty.so", RTLD_NOW);
164 dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL);
165 void* sym = dlsym(handle, "getRandomNumber");
166 ASSERT_TRUE(sym == nullptr);
167 ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror());
168
169 sym = dlsym(handle, "DlSymTestFunction");
170 ASSERT_TRUE(sym == nullptr);
171 ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror());
172 dlclose(handle);
173}
174
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700175TEST(dlfcn, dlsym_with_dependencies) {
176 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700177 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700178 dlerror();
179 // This symbol is in DT_NEEDED library.
180 void* sym = dlsym(handle, "getRandomNumber");
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700181 ASSERT_TRUE(sym != nullptr) << dlerror();
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700182 int (*fn)(void);
183 fn = reinterpret_cast<int (*)(void)>(sym);
184 EXPECT_EQ(4, fn());
185 dlclose(handle);
186}
187
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700188TEST(dlfcn, dlopen_noload) {
189 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700190 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700191 handle = dlopen("libtest_simple.so", RTLD_NOW);
192 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700193 ASSERT_TRUE(handle != nullptr);
194 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700195 ASSERT_TRUE(handle == handle2);
196 ASSERT_EQ(0, dlclose(handle));
197 ASSERT_EQ(0, dlclose(handle2));
198}
199
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700200TEST(dlfcn, dlopen_by_soname) {
201 static const char* soname = "libdlext_test_soname.so";
202 static const char* filename = "libdlext_test_different_soname.so";
203 // 1. Make sure there is no library with soname in default search path
204 void* handle = dlopen(soname, RTLD_NOW);
205 ASSERT_TRUE(handle == nullptr);
206
207 // 2. Load a library using filename
208 handle = dlopen(filename, RTLD_NOW);
209 ASSERT_TRUE(handle != nullptr) << dlerror();
210
211 // 3. Find library by soname
212 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
213 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
214 ASSERT_EQ(handle, handle_soname);
215
216 // 4. RTLD_NOLOAD should still work with filename
217 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
218 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
219 ASSERT_EQ(handle, handle_filename);
220
221 dlclose(handle_filename);
222 dlclose(handle_soname);
223 dlclose(handle);
224}
225
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700226// mips doesn't support ifuncs
227#if !defined(__mips__)
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700228TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700229 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700230
231 // ifunc's choice depends on whether IFUNC_CHOICE has a value
232 // first check the set case
233 setenv("IFUNC_CHOICE", "set", 1);
234 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700235 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700236 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
237 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700238 ASSERT_TRUE(foo_ptr != nullptr);
239 ASSERT_TRUE(foo_library_ptr != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700240 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
241 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700242 dlclose(handle);
243
244 // then check the unset case
245 unsetenv("IFUNC_CHOICE");
246 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700247 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700248 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
249 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700250 ASSERT_TRUE(foo_ptr != nullptr);
251 ASSERT_TRUE(foo_library_ptr != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700252 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
253 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0);
254 dlclose(handle);
255}
256
257TEST(dlfcn, ifunc_ctor_call) {
258 typedef const char* (*fn_ptr)();
259
260 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700261 ASSERT_TRUE(handle != nullptr) << dlerror();
262 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
263 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
264 ASSERT_STREQ("false", is_ctor_called());
265
266 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
267 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700268 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700269 dlclose(handle);
270}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700271
272TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
273 typedef const char* (*fn_ptr)();
274
275 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
276 ASSERT_TRUE(handle != nullptr) << dlerror();
277 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
278 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
279 ASSERT_STREQ("false", is_ctor_called());
280
281 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
282 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
283 ASSERT_STREQ("true", is_ctor_called());
284 dlclose(handle);
285}
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700286#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700287
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700288TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
289 // This is the structure of the test library and
290 // its dt_needed libraries
291 // libtest_relo_check_dt_needed_order.so
292 // |
293 // +-> libtest_relo_check_dt_needed_order_1.so
294 // |
295 // +-> libtest_relo_check_dt_needed_order_2.so
296 //
297 // The root library references relo_test_get_answer_lib - which is defined
298 // in both dt_needed libraries, the correct relocation should
299 // use the function defined in libtest_relo_check_dt_needed_order_1.so
300 void* handle = nullptr;
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -0700301 auto guard = make_scope_guard([&]() {
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700302 dlclose(handle);
303 });
304
305 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
306 ASSERT_TRUE(handle != nullptr) << dlerror();
307
308 typedef int (*fn_t) (void);
309 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
310 ASSERT_TRUE(fn != nullptr) << dlerror();
311 ASSERT_EQ(1, fn());
312}
313
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700314TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700315 // Here is how the test library and its dt_needed
316 // libraries are arranged
317 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700318 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700319 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700320 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700321 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700322 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700323 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700324 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700325 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700326 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700327 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700328 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700329 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700330 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700331 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700332 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700333 //
334 // load order should be (1, 2, 3, a, b, d)
335 //
336 // get_answer() is defined in (2, 3, a, b, c)
337 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700338 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700339 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700340 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
341 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700342 typedef int (*fn_t) (void);
343 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700344 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700345 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700346 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700347 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700348
349 ASSERT_EQ(42, fn());
350 ASSERT_EQ(43, fn2());
351 dlclose(handle);
352}
353
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700354TEST(dlfcn, dlopen_check_order_reloc_siblings) {
355 // This is how this one works:
356 // we lookup and call get_answer which is defined in '_2.so'
357 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
358 // the correct _impl() is implemented by '_a.so';
359 //
360 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
361 //
362 // Here is the picture:
363 //
364 // libtest_check_order_reloc_siblings.so
365 // |
366 // +-> ..._1.so <- empty
367 // | |
368 // | +-> ..._a.so <- exports correct answer_impl()
369 // | |
370 // | +-> ..._b.so <- every other letter exporting incorrect one.
371 // |
372 // +-> ..._2.so <- empty
373 // | |
374 // | +-> ..._c.so
375 // | |
376 // | +-> ..._d.so
377 // |
378 // +-> ..._3.so <- empty
379 // |
380 // +-> ..._e.so
381 // |
382 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
383 // implements incorrect get_answer_impl()
384
385 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
386 ASSERT_TRUE(handle == nullptr);
387#ifdef __BIONIC__
388 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
389 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
390#endif
391
392 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
393 ASSERT_TRUE(handle != nullptr) << dlerror();
394
395 typedef int (*fn_t) (void);
396 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
397 ASSERT_TRUE(fn != nullptr) << dlerror();
398 ASSERT_EQ(42, fn());
399
400 ASSERT_EQ(0, dlclose(handle));
401}
402
403TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
404 // This test uses the same library as dlopen_check_order_reloc_siblings.
405 // Unlike dlopen_check_order_reloc_siblings it preloads
406 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
407 // dlopen(libtest_check_order_reloc_siblings.so)
408
409 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
410 ASSERT_TRUE(handle == nullptr);
411 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
412 ASSERT_TRUE(handle == nullptr);
413
414 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
415 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
416
417 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
418 ASSERT_TRUE(handle != nullptr) << dlerror();
419
420 ASSERT_EQ(0, dlclose(handle_for_1));
421
422 typedef int (*fn_t) (void);
423 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
424 ASSERT_TRUE(fn != nullptr) << dlerror();
425 ASSERT_EQ(42, fn());
426
427 ASSERT_EQ(0, dlclose(handle));
428}
429
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800430TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
431 // This is how this one works:
432 // we lookup and call grandchild_get_answer which is defined in '_2.so'
433 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
434 // the correct _impl() is implemented by '_c_1.so';
435 //
436 // Here is the picture of subtree:
437 //
438 // libtest_check_order_reloc_siblings.so
439 // |
440 // +-> ..._2.so <- grandchild_get_answer()
441 // |
442 // +-> ..._c.so <- empty
443 // | |
444 // | +-> _c_1.so <- exports correct answer_impl()
445 // | |
446 // | +-> _c_2.so <- exports incorrect answer_impl()
447 // |
448 // +-> ..._d.so <- empty
449
450 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
451 ASSERT_TRUE(handle == nullptr);
452#ifdef __BIONIC__
453 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
454 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
455#endif
456
457 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
458 ASSERT_TRUE(handle != nullptr) << dlerror();
459
460 typedef int (*fn_t) (void);
461 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
462 ASSERT_TRUE(fn != nullptr) << dlerror();
463 ASSERT_EQ(42, fn());
464
465 ASSERT_EQ(0, dlclose(handle));
466}
467
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700468TEST(dlfcn, dlopen_check_order_reloc_nephew) {
469 // This is how this one works:
470 // we lookup and call nephew_get_answer which is defined in '_2.so'
471 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
472 // the correct _impl() is implemented by '_a.so';
473 //
474 // Here is the picture:
475 //
476 // libtest_check_order_reloc_siblings.so
477 // |
478 // +-> ..._1.so <- empty
479 // | |
480 // | +-> ..._a.so <- exports correct answer_impl()
481 // | |
482 // | +-> ..._b.so <- every other letter exporting incorrect one.
483 // |
484 // +-> ..._2.so <- empty
485 // | |
486 // | +-> ..._c.so
487 // | |
488 // | +-> ..._d.so
489 // |
490 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
491 // |
492 // +-> ..._e.so
493 // |
494 // +-> ..._f.so
495
496 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
497 ASSERT_TRUE(handle == nullptr);
498#ifdef __BIONIC__
499 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
500 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
501#endif
502
503 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
504 ASSERT_TRUE(handle != nullptr) << dlerror();
505
506 typedef int (*fn_t) (void);
507 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
508 ASSERT_TRUE(fn != nullptr) << dlerror();
509 ASSERT_EQ(42, fn());
510
511 ASSERT_EQ(0, dlclose(handle));
512}
513
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800514TEST(dlfcn, check_unload_after_reloc) {
515 // This is how this one works:
516 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
517 // |
518 // +-> libtest_two_parents_child
519 //
520 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
521 // |
522 // +-> libtest_two_parents_child
523 //
524 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
525 // as a second step it dlopens parent2 and dlcloses parent1...
526
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700527 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
528 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800529
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700530 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
531 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800532
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700533 typedef int (*fn_t) (void);
534 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
535 ASSERT_TRUE(fn != nullptr) << dlerror();
536 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800537
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700538 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800539
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700540 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
541 ASSERT_TRUE(handle != nullptr);
542 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800543
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700544 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
545 ASSERT_TRUE(fn != nullptr) << dlerror();
546 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800547
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700548 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800549
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700550 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
551 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800552}
553
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700554extern "C" int check_order_reloc_root_get_answer_impl() {
555 return 42;
556}
557
558TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
559 // This is how this one works:
560 // we lookup and call get_answer3 which is defined in 'root.so'
561 // and in turn calls external root_get_answer_impl() defined in _2.so and
562 // above the correct _impl() is one in the executable.
563 //
564 // libtest_check_order_reloc_root.so
565 // |
566 // +-> ..._1.so <- empty
567 // |
568 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
569 //
570
571 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
572 ASSERT_TRUE(handle == nullptr);
573#ifdef __BIONIC__
574 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
575 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
576#endif
577
578 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
579 ASSERT_TRUE(handle != nullptr) << dlerror();
580
581 typedef int (*fn_t) (void);
582 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
583 ASSERT_TRUE(fn != nullptr) << dlerror();
584 ASSERT_EQ(42, fn());
585
586 ASSERT_EQ(0, dlclose(handle));
587}
588
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700589TEST(dlfcn, dlopen_check_rtld_local) {
590 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
591 ASSERT_TRUE(sym == nullptr);
592
593 // implicit RTLD_LOCAL
594 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
595 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
596 ASSERT_TRUE(sym == nullptr);
597 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
598 sym = dlsym(handle, "dlopen_testlib_simple_func");
599 ASSERT_TRUE(sym != nullptr);
600 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
601 dlclose(handle);
602
603 // explicit RTLD_LOCAL
604 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
605 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
606 ASSERT_TRUE(sym == nullptr);
607 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
608 sym = dlsym(handle, "dlopen_testlib_simple_func");
609 ASSERT_TRUE(sym != nullptr);
610 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
611 dlclose(handle);
612}
613
614TEST(dlfcn, dlopen_check_rtld_global) {
615 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
616 ASSERT_TRUE(sym == nullptr);
617
618 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700619 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700620 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
621 ASSERT_TRUE(sym != nullptr) << dlerror();
622 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
623 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700624
625 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
626 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
627 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700628
629 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
630 // shared libraries after symbol was not found in the main executable
631 // and dependent libraries.
632 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
633 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
634 ASSERT_TRUE(sym != nullptr) << dlerror();
635
636 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700637}
638
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700639// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
640// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
641// libtest_with_dependency_loop_a.so
642TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700643 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
644 ASSERT_TRUE(handle != nullptr) << dlerror();
645 void* f = dlsym(handle, "dlopen_test_loopy_function");
646 ASSERT_TRUE(f != nullptr) << dlerror();
647 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
648 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700649
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700650 // dlopen second time to make sure that the library was unloaded correctly
651 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
652 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800653#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700654 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 -0800655#else
656 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
657 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700658#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800659
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700660 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
661 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700662}
663
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700664TEST(dlfcn, dlopen_nodelete) {
665 static bool is_unloaded = false;
666
667 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
668 ASSERT_TRUE(handle != nullptr) << dlerror();
669 void (*set_unload_flag_ptr)(bool*);
670 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
671 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
672 set_unload_flag_ptr(&is_unloaded);
673
674 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
675 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
676 ASSERT_EQ(1729U, *taxicab_number);
677 *taxicab_number = 2;
678
679 dlclose(handle);
680 ASSERT_TRUE(!is_unloaded);
681
682 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
683 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
684 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
685
686
687 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
688 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
689 ASSERT_EQ(taxicab_number2, taxicab_number);
690
691 ASSERT_EQ(2U, *taxicab_number2);
692
693 dlclose(handle);
694 ASSERT_TRUE(!is_unloaded);
695}
696
697TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
698 static bool is_unloaded = false;
699
700 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
701 ASSERT_TRUE(handle != nullptr) << dlerror();
702 void (*set_unload_flag_ptr)(bool*);
703 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
704 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
705 set_unload_flag_ptr(&is_unloaded);
706
707 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
708 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
709
710 ASSERT_EQ(1729U, *taxicab_number);
711 *taxicab_number = 2;
712
713 // This RTLD_NODELETE should be ignored
714 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
715 ASSERT_TRUE(handle1 != nullptr) << dlerror();
716 ASSERT_EQ(handle, handle1);
717
718 dlclose(handle1);
719 dlclose(handle);
720
721 ASSERT_TRUE(is_unloaded);
722}
723
724TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
725 static bool is_unloaded = false;
726
727 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
728 ASSERT_TRUE(handle != nullptr) << dlerror();
729 void (*set_unload_flag_ptr)(bool*);
730 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
731 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
732 set_unload_flag_ptr(&is_unloaded);
733
734 dlclose(handle);
735 ASSERT_TRUE(!is_unloaded);
736}
737
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700738TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700739 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
740 ASSERT_TRUE(handle != nullptr) << dlerror();
741 int (*get_answer)();
742 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
743 ASSERT_TRUE(get_answer != nullptr) << dlerror();
744 ASSERT_EQ(42, get_answer());
745 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700746}
747
Elliott Hughese66190d2012-12-18 15:57:55 -0800748TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700749 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700750 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700751#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700752 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
753#else
754 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
755#endif
756}
757
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800758static void ConcurrentDlErrorFn(std::string& error) {
759 ASSERT_TRUE(dlerror() == nullptr);
760
761 void* handle = dlopen("/child/thread", RTLD_NOW);
762 ASSERT_TRUE(handle == nullptr);
763
764 const char* err = dlerror();
765 ASSERT_TRUE(err != nullptr);
766
767 error = err;
768}
769
770TEST(dlfcn, dlerror_concurrent_buffer) {
771 void* handle = dlopen("/main/thread", RTLD_NOW);
772 ASSERT_TRUE(handle == nullptr);
773 const char* main_thread_error = dlerror();
774 ASSERT_TRUE(main_thread_error != nullptr);
775 ASSERT_SUBSTR("/main/thread", main_thread_error);
776
777 std::string child_thread_error;
778 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
779 t.join();
780 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
781
782 // Check that main thread local buffer was not modified.
783 ASSERT_SUBSTR("/main/thread", main_thread_error);
Elliott Hughes5419b942012-10-16 15:54:46 -0700784}
785
Elliott Hughese66190d2012-12-18 15:57:55 -0800786TEST(dlfcn, dlerror_concurrent) {
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800787 void* handle = dlopen("/main/thread", RTLD_NOW);
788 ASSERT_TRUE(handle == nullptr);
789
790 std::string child_thread_error;
791 std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error));
792 t.join();
793 ASSERT_SUBSTR("/child/thread", child_thread_error.c_str());
794
Elliott Hughes5419b942012-10-16 15:54:46 -0700795 const char* main_thread_error = dlerror();
Dimitry Ivanovc7365eb2016-11-17 12:38:09 -0800796 ASSERT_TRUE(main_thread_error != nullptr);
Elliott Hughes5419b942012-10-16 15:54:46 -0700797 ASSERT_SUBSTR("/main/thread", main_thread_error);
798}
799
Elliott Hughese66190d2012-12-18 15:57:55 -0800800TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700801 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700802 void* self = dlopen(nullptr, RTLD_NOW);
803 ASSERT_TRUE(self != nullptr);
804 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700805
806 void* sym;
807
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700808#if defined(__BIONIC__) && !defined(__LP64__)
809 // RTLD_DEFAULT in lp32 bionic is not (void*)0
810 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700811 sym = dlsym(nullptr, "test");
812 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800813 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700814#endif
815
Elliott Hughes3b297c42012-10-11 16:08:51 -0700816 // Symbol that doesn't exist.
817 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700818 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700819 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700820
821 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700822}
823
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700824TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700825 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700826 void* self = dlopen(nullptr, RTLD_NOW);
827 ASSERT_TRUE(self != nullptr);
828 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700829
830 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700831 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700832
833 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
834 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
835
836 Dl_info info;
837 int rc = dladdr(addr, &info);
838 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
839
840 // Get the name of this executable.
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700841 const std::string& executable_path = get_executable_path();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700842
843 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700844 char dli_realpath[PATH_MAX];
845 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700846 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700847
848 // The symbol name should be the symbol we looked up.
849 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
850
851 // The address should be the exact address of the symbol.
852 ASSERT_EQ(info.dli_saddr, sym);
853
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700854 std::vector<map_record> maps;
855 ASSERT_TRUE(Maps::parse_maps(&maps));
856
857 void* base_address = nullptr;
858 for (const map_record& rec : maps) {
859 if (executable_path == rec.pathname) {
860 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700861 break;
862 }
863 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700864
865 // The base address should be the address we were loaded at.
866 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700867
868 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700869}
870
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700871TEST(dlfcn, dlopen_executable_by_absolute_path) {
872 void* handle1 = dlopen(nullptr, RTLD_NOW);
873 ASSERT_TRUE(handle1 != nullptr) << dlerror();
874
875 void* handle2 = dlopen(get_executable_path().c_str(), RTLD_NOW);
876 ASSERT_TRUE(handle2 != nullptr) << dlerror();
877
878#if defined(__BIONIC__)
879 ASSERT_EQ(handle1, handle2);
880#else
881 GTEST_LOG_(INFO) << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
882 "it loads a separate copy of the main executable "
883 "on dlopen by absolute path.";
884#endif
885}
886
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700887#if defined(__LP64__)
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200888#define PATH_TO_SYSTEM_LIB "/system/lib64/"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700889#else
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200890#define PATH_TO_SYSTEM_LIB "/system/lib/"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700891#endif
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200892#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700893
894TEST(dlfcn, dladdr_libc) {
895#if defined(__BIONIC__)
896 Dl_info info;
897 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
898 ASSERT_TRUE(dladdr(addr, &info) != 0);
899
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700900 // /system/lib is symlink when this test is executed on host.
901 char libc_realpath[PATH_MAX];
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200902 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700903
904 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700905 // TODO: add check for dfi_fbase
906 ASSERT_STREQ("puts", info.dli_sname);
907 ASSERT_EQ(addr, info.dli_saddr);
908#else
909 GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig "
910 "for libc.so, which is symlink itself (not a realpath).\n";
911#endif
912}
913
Elliott Hughese66190d2012-12-18 15:57:55 -0800914TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700915 Dl_info info;
916
Elliott Hughes3b297c42012-10-11 16:08:51 -0700917 dlerror(); // Clear any pending errors.
918
Elliott Hughes8e15b082012-09-26 11:44:01 -0700919 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700920 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
921 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700922
923 // No symbol corresponding to a stack address.
924 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700925 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700926}
Elliott Hughes124fae92012-10-31 14:20:03 -0700927
Elliott Hughesa43e9062013-01-07 14:18:22 -0800928// GNU-style ELF hash tables are incompatible with the MIPS ABI.
929// 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 -0800930TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800931#if !defined(__mips__)
Elliott Hughes124fae92012-10-31 14:20:03 -0700932 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800933 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
934 ASSERT_TRUE(handle != nullptr) << dlerror();
935 auto guard = make_scope_guard([&]() {
936 dlclose(handle);
937 });
938 void* sym = dlsym(handle, "getRandomNumber");
939 ASSERT_TRUE(sym != nullptr) << dlerror();
940 int (*fn)(void);
941 fn = reinterpret_cast<int (*)(void)>(sym);
942 EXPECT_EQ(4, fn());
943
944 Dl_info dlinfo;
945 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
946
947 ASSERT_TRUE(fn == dlinfo.dli_saddr);
948 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800949 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800950#else
951 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
952#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700953}
Elliott Hughese66190d2012-12-18 15:57:55 -0800954
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800955TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
956 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
957 ASSERT_TRUE(handle != nullptr) << dlerror();
958 auto guard = make_scope_guard([&]() {
959 dlclose(handle);
960 });
961 void* sym = dlsym(handle, "getRandomNumber");
962 ASSERT_TRUE(sym != nullptr) << dlerror();
963 int (*fn)(void);
964 fn = reinterpret_cast<int (*)(void)>(sym);
965 EXPECT_EQ(4, fn());
966
967 Dl_info dlinfo;
968 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
969
970 ASSERT_TRUE(fn == dlinfo.dli_saddr);
971 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
972 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
973}
974
Elliott Hughese66190d2012-12-18 15:57:55 -0800975TEST(dlfcn, dlopen_bad_flags) {
976 dlerror(); // Clear any pending errors.
977 void* handle;
978
Elliott Hughes063525c2014-05-13 11:19:57 -0700979#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800980 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700981 handle = dlopen(nullptr, 0);
982 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -0800983 ASSERT_SUBSTR("invalid", dlerror());
984#endif
985
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700986 handle = dlopen(nullptr, 0xffffffff);
987 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -0800988 ASSERT_SUBSTR("invalid", dlerror());
989
990 // 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 -0700991 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
992 ASSERT_TRUE(handle != nullptr);
993 ASSERT_SUBSTR(nullptr, dlerror());
Elliott Hughese66190d2012-12-18 15:57:55 -0800994}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400995
996TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800997 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700998 ASSERT_TRUE(addr == nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400999}
1000
1001TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -08001002 void* addr = dlsym(RTLD_DEFAULT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001003 ASSERT_TRUE(addr != nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001004}
1005
1006TEST(dlfcn, rtld_next_unknown_symbol) {
1007 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001008 ASSERT_TRUE(addr == nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -08001009}
1010
1011TEST(dlfcn, rtld_next_known_symbol) {
1012 void* addr = dlsym(RTLD_NEXT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001013 ASSERT_TRUE(addr != nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001014}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001015
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001016// Check that RTLD_NEXT of a libc symbol works in dlopened library
1017TEST(dlfcn, rtld_next_from_library) {
1018 void* library_with_close = dlopen("libtest_check_rtld_next_from_library.so", RTLD_NOW);
1019 ASSERT_TRUE(library_with_close != nullptr) << dlerror();
1020 void* expected_addr = dlsym(RTLD_DEFAULT, "close");
1021 ASSERT_TRUE(expected_addr != nullptr) << dlerror();
1022 typedef void* (*get_libc_close_ptr_fn_t)();
1023 get_libc_close_ptr_fn_t get_libc_close_ptr =
1024 reinterpret_cast<get_libc_close_ptr_fn_t>(dlsym(library_with_close, "get_libc_close_ptr"));
1025 ASSERT_TRUE(get_libc_close_ptr != nullptr) << dlerror();
1026 ASSERT_EQ(expected_addr, get_libc_close_ptr());
1027
1028 dlclose(library_with_close);
1029}
1030
1031
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001032TEST(dlfcn, dlsym_weak_func) {
1033 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001034 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001035 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001036
1037 int (*weak_func)();
1038 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001039 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001040 EXPECT_EQ(42, weak_func());
1041 dlclose(handle);
1042}
1043
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001044TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001045 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
1046 ASSERT_TRUE(handle != nullptr) << dlerror();
1047 int (*weak_func)();
1048 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
1049 ASSERT_TRUE(weak_func != nullptr) << dlerror();
1050 EXPECT_EQ(6551, weak_func());
1051 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001052}
1053
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001054TEST(dlfcn, dlopen_symlink) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001055 DlfcnSymlink symlink("dlopen_symlink");
1056 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001057 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001058 void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001059 ASSERT_TRUE(handle1 != nullptr);
1060 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001061 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -07001062 dlclose(handle1);
1063 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001064}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001065
1066// libtest_dlopen_from_ctor_main.so depends on
1067// libtest_dlopen_from_ctor.so which has a constructor
1068// that calls dlopen(libc...). This is to test the situation
1069// described in b/7941716.
1070TEST(dlfcn, dlopen_dlopen_from_ctor) {
1071#if defined(__BIONIC__)
1072 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1073 ASSERT_TRUE(handle != nullptr) << dlerror();
1074 dlclose(handle);
1075#else
1076 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
1077#endif
1078}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001079
1080TEST(dlfcn, symbol_versioning_use_v1) {
1081 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1082 ASSERT_TRUE(handle != nullptr) << dlerror();
1083 typedef int (*fn_t)();
1084 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1085 ASSERT_TRUE(fn != nullptr) << dlerror();
1086 ASSERT_EQ(1, fn());
1087 dlclose(handle);
1088}
1089
1090TEST(dlfcn, symbol_versioning_use_v2) {
1091 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1092 ASSERT_TRUE(handle != nullptr) << dlerror();
1093 typedef int (*fn_t)();
1094 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1095 ASSERT_TRUE(fn != nullptr) << dlerror();
1096 ASSERT_EQ(2, fn());
1097 dlclose(handle);
1098}
1099
1100TEST(dlfcn, symbol_versioning_use_other_v2) {
1101 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1102 ASSERT_TRUE(handle != nullptr) << dlerror();
1103 typedef int (*fn_t)();
1104 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1105 ASSERT_TRUE(fn != nullptr) << dlerror();
1106 ASSERT_EQ(20, fn());
1107 dlclose(handle);
1108}
1109
1110TEST(dlfcn, symbol_versioning_use_other_v3) {
1111 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1112 ASSERT_TRUE(handle != nullptr) << dlerror();
1113 typedef int (*fn_t)();
1114 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1115 ASSERT_TRUE(fn != nullptr) << dlerror();
1116 ASSERT_EQ(3, fn());
1117 dlclose(handle);
1118}
1119
1120TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1121 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1122 ASSERT_TRUE(handle != nullptr) << dlerror();
1123 typedef int (*fn_t)();
1124 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1125 ASSERT_TRUE(fn != nullptr) << dlerror();
1126 ASSERT_EQ(3, fn()); // the default version is 3
1127 dlclose(handle);
1128}
1129
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001130TEST(dlfcn, dlvsym_smoke) {
1131 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1132 ASSERT_TRUE(handle != nullptr) << dlerror();
1133 typedef int (*fn_t)();
1134
1135 {
1136 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1137 ASSERT_TRUE(fn == nullptr);
1138 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1139 }
1140
1141 {
1142 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1143 ASSERT_TRUE(fn != nullptr) << dlerror();
1144 ASSERT_EQ(2, fn());
1145 }
1146
1147 dlclose(handle);
1148}
1149
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001150// This preempts the implementation from libtest_versioned_lib.so
1151extern "C" int version_zero_function() {
1152 return 0;
1153}
1154
1155// This preempts the implementation from libtest_versioned_uselibv*.so
1156extern "C" int version_zero_function2() {
1157 return 0;
1158}
Evgenii Stepanov68650822015-06-10 13:38:39 -07001159
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001160TEST(dlfcn, dt_runpath_smoke) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001161 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1162 ASSERT_TRUE(handle != nullptr) << dlerror();
1163
1164 typedef void *(* dlopen_b_fn)();
1165 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1166 ASSERT_TRUE(fn != nullptr) << dlerror();
1167
1168 void *p = fn();
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001169 ASSERT_TRUE(p != nullptr);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001170
1171 dlclose(handle);
1172}
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001173
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001174// Bionic specific tests
1175#if defined(__BIONIC__)
1176
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001177TEST(dlfcn, dt_runpath_absolute_path) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001178 std::string libpath = get_testlib_root() + "/libtest_dt_runpath_d.so";
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001179 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001180 ASSERT_TRUE(handle != nullptr) << dlerror();
1181
1182 typedef void *(* dlopen_b_fn)();
1183 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1184 ASSERT_TRUE(fn != nullptr) << dlerror();
1185
1186 void *p = fn();
1187 ASSERT_TRUE(p != nullptr);
1188
1189 dlclose(handle);
1190}
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001191
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001192TEST(dlfcn, dlopen_invalid_rw_load_segment) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001193 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001194 "/" + kPrebuiltElfDir +
1195 "/libtest_invalid-rw_load_segment.so";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001196 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1197 ASSERT_TRUE(handle == nullptr);
1198 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W + E load segments are not allowed";
1199 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1200}
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001201
1202TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001203 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001204 "/" + kPrebuiltElfDir +
1205 "/libtest_invalid-unaligned_shdr_offset.so";
1206
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001207 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1208 ASSERT_TRUE(handle == nullptr);
1209 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
1210 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1211}
1212
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001213TEST(dlfcn, dlopen_invalid_zero_shentsize) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001214 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001215 "/" + kPrebuiltElfDir +
1216 "/libtest_invalid-zero_shentsize.so";
1217
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001218 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1219 ASSERT_TRUE(handle == nullptr);
1220 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x";
1221 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1222}
1223
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001224TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001225 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001226 "/" + kPrebuiltElfDir +
1227 "/libtest_invalid-zero_shstrndx.so";
1228
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001229 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1230 ASSERT_TRUE(handle == nullptr);
1231 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx";
1232 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1233}
1234
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001235TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001236 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001237 "/" + kPrebuiltElfDir +
1238 "/libtest_invalid-empty_shdr_table.so";
1239
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001240 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1241 ASSERT_TRUE(handle == nullptr);
1242 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers";
1243 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1244}
1245
Dimitry Ivanov46230442016-08-15 13:40:53 -07001246TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001247 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001248 "/" + kPrebuiltElfDir +
1249 "/libtest_invalid-zero_shdr_table_offset.so";
1250
Dimitry Ivanov46230442016-08-15 13:40:53 -07001251 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1252 ASSERT_TRUE(handle == nullptr);
1253 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/";
1254 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1255}
1256
Dimitry Ivanov55958342016-08-15 14:06:04 -07001257TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001258 const std::string libpath = get_testlib_root() +
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001259 "/" + kPrebuiltElfDir +
1260 "/libtest_invalid-zero_shdr_table_content.so";
1261
Dimitry Ivanov55958342016-08-15 14:06:04 -07001262 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1263 ASSERT_TRUE(handle == nullptr);
1264 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
1265 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1266}
1267
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001268TEST(dlfcn, dlopen_invalid_textrels) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001269 const std::string libpath = get_testlib_root() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001270 "/" + kPrebuiltElfDir +
1271 "/libtest_invalid-textrels.so";
1272
1273 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1274 ASSERT_TRUE(handle == nullptr);
1275 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1276 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1277}
1278
1279TEST(dlfcn, dlopen_invalid_textrels2) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001280 const std::string libpath = get_testlib_root() +
Dimitry Ivanov816676e2016-10-19 11:00:28 -07001281 "/" + kPrebuiltElfDir +
1282 "/libtest_invalid-textrels2.so";
1283
1284 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1285 ASSERT_TRUE(handle == nullptr);
1286 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations";
1287 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1288}
1289
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001290#endif