blob: dd9660b3d488c4f1225afa4635cb3e261950cf80 [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>
jeffhaoacf5aa72012-09-12 17:25:30 -070028
Dimitry Ivanov927877c2016-09-21 11:17:13 -070029#include "gtest_globals.h"
Dimitry Ivanov708589f2016-09-19 10:50:28 -070030#include "dlfcn_symlink_support.h"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070031#include "utils.h"
32
Elliott Hughes3b297c42012-10-11 16:08:51 -070033#define ASSERT_SUBSTR(needle, haystack) \
34 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
35
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070036
Elliott Hughes1728b232014-05-14 10:02:03 -070037static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070038extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070039 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070040}
41
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070042static int g_ctor_function_called = 0;
Dimitry Ivanov55437462016-07-20 15:33:07 -070043static int g_ctor_argc = 0;
44static char** g_ctor_argv = reinterpret_cast<char**>(0xDEADBEEF);
45static char** g_ctor_envp = g_ctor_envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070046
Dimitry Ivanov55437462016-07-20 15:33:07 -070047extern "C" void ctor_function(int argc, char** argv, char** envp) __attribute__ ((constructor));
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070048
Dimitry Ivanov55437462016-07-20 15:33:07 -070049extern "C" void ctor_function(int argc, char** argv, char** envp) {
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070050 g_ctor_function_called = 17;
Dimitry Ivanov55437462016-07-20 15:33:07 -070051 g_ctor_argc = argc;
52 g_ctor_argv = argv;
53 g_ctor_envp = envp;
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070054}
55
56TEST(dlfcn, ctor_function_call) {
57 ASSERT_EQ(17, g_ctor_function_called);
Dimitry Ivanov55437462016-07-20 15:33:07 -070058 ASSERT_TRUE(g_ctor_argc = get_argc());
59 ASSERT_TRUE(g_ctor_argv = get_argv());
60 ASSERT_TRUE(g_ctor_envp = get_envp());
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070061}
62
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070063TEST(dlfcn, dlsym_in_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070064 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070065 void* self = dlopen(nullptr, RTLD_NOW);
66 ASSERT_TRUE(self != nullptr);
67 ASSERT_TRUE(dlerror() == nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070068
69 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -070070 ASSERT_TRUE(sym != nullptr);
jeffhaoacf5aa72012-09-12 17:25:30 -070071
72 void (*function)() = reinterpret_cast<void(*)()>(sym);
73
Elliott Hughes1728b232014-05-14 10:02:03 -070074 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070075 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070076 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070077
78 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070079}
Elliott Hughes8e15b082012-09-26 11:44:01 -070080
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070081TEST(dlfcn, dlsym_from_sofile) {
82 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL);
83 ASSERT_TRUE(handle != nullptr) << dlerror();
84
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070085 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070086 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
87 ASSERT_TRUE(symbol == nullptr);
88 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
89
90 typedef int* (*fn_t)();
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070091 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
92 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
93 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070094
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070095 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -070096 ASSERT_TRUE(ptr != nullptr) << dlerror();
97 ASSERT_EQ(42, *ptr);
98
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -070099 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
100 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
101 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
102
103 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
104 ASSERT_TRUE(ptr != nullptr) << dlerror();
105 ASSERT_EQ(44, *ptr);
106
107 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
108 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
109 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
110
111 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
112 ASSERT_TRUE(ptr != nullptr) << dlerror();
113 ASSERT_EQ(43, *ptr);
114
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700115 dlclose(handle);
116}
117
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700118TEST(dlfcn, dlsym_from_sofile_with_preload) {
119 void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL);
120 ASSERT_TRUE(preload != nullptr) << dlerror();
121
122 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
123 ASSERT_TRUE(handle != nullptr) << dlerror();
124
125 // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT)
126 void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol");
127 ASSERT_TRUE(symbol == nullptr);
128 ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror());
129
130 typedef int* (*fn_t)();
131 fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT =
132 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT"));
133 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror();
134
135 int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT();
136 ASSERT_TRUE(ptr != nullptr) << dlerror();
137 ASSERT_EQ(42, *ptr);
138
139 fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT =
140 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT"));
141 ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror();
142
143 ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT();
144 ASSERT_TRUE(ptr != nullptr) << dlerror();
145 ASSERT_EQ(44, *ptr);
146
147 fn_t lookup_dlsym_symbol_using_RTLD_NEXT =
148 reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT"));
149 ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror();
150
151 ptr = lookup_dlsym_symbol_using_RTLD_NEXT();
152 ASSERT_TRUE(ptr != nullptr) << dlerror();
153 ASSERT_EQ(43, *ptr);
154
155 dlclose(handle);
156 dlclose(preload);
157}
158
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700159TEST(dlfcn, dlsym_handle_global_sym) {
160 // check that we do not look into global group
161 // when looking up symbol by handle
162 void* handle = dlopen("libtest_empty.so", RTLD_NOW);
163 dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL);
164 void* sym = dlsym(handle, "getRandomNumber");
165 ASSERT_TRUE(sym == nullptr);
166 ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror());
167
168 sym = dlsym(handle, "DlSymTestFunction");
169 ASSERT_TRUE(sym == nullptr);
170 ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror());
171 dlclose(handle);
172}
173
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700174TEST(dlfcn, dlsym_with_dependencies) {
175 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700176 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700177 dlerror();
178 // This symbol is in DT_NEEDED library.
179 void* sym = dlsym(handle, "getRandomNumber");
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700180 ASSERT_TRUE(sym != nullptr) << dlerror();
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700181 int (*fn)(void);
182 fn = reinterpret_cast<int (*)(void)>(sym);
183 EXPECT_EQ(4, fn());
184 dlclose(handle);
185}
186
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700187TEST(dlfcn, dlopen_noload) {
188 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700189 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700190 handle = dlopen("libtest_simple.so", RTLD_NOW);
191 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700192 ASSERT_TRUE(handle != nullptr);
193 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700194 ASSERT_TRUE(handle == handle2);
195 ASSERT_EQ(0, dlclose(handle));
196 ASSERT_EQ(0, dlclose(handle2));
197}
198
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700199TEST(dlfcn, dlopen_by_soname) {
200 static const char* soname = "libdlext_test_soname.so";
201 static const char* filename = "libdlext_test_different_soname.so";
202 // 1. Make sure there is no library with soname in default search path
203 void* handle = dlopen(soname, RTLD_NOW);
204 ASSERT_TRUE(handle == nullptr);
205
206 // 2. Load a library using filename
207 handle = dlopen(filename, RTLD_NOW);
208 ASSERT_TRUE(handle != nullptr) << dlerror();
209
210 // 3. Find library by soname
211 void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
212 ASSERT_TRUE(handle_soname != nullptr) << dlerror();
213 ASSERT_EQ(handle, handle_soname);
214
215 // 4. RTLD_NOLOAD should still work with filename
216 void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
217 ASSERT_TRUE(handle_filename != nullptr) << dlerror();
218 ASSERT_EQ(handle, handle_filename);
219
220 dlclose(handle_filename);
221 dlclose(handle_soname);
222 dlclose(handle);
223}
224
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700225// mips doesn't support ifuncs
226#if !defined(__mips__)
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700227TEST(dlfcn, ifunc) {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700228 typedef const char* (*fn_ptr)();
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700229
230 // ifunc's choice depends on whether IFUNC_CHOICE has a value
231 // first check the set case
232 setenv("IFUNC_CHOICE", "set", 1);
233 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700234 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700235 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
236 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700237 ASSERT_TRUE(foo_ptr != nullptr);
238 ASSERT_TRUE(foo_library_ptr != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700239 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
240 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700241 dlclose(handle);
242
243 // then check the unset case
244 unsetenv("IFUNC_CHOICE");
245 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700246 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700247 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
248 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700249 ASSERT_TRUE(foo_ptr != nullptr);
250 ASSERT_TRUE(foo_library_ptr != nullptr);
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700251 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
252 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0);
253 dlclose(handle);
254}
255
256TEST(dlfcn, ifunc_ctor_call) {
257 typedef const char* (*fn_ptr)();
258
259 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700260 ASSERT_TRUE(handle != nullptr) << dlerror();
261 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
262 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
263 ASSERT_STREQ("false", is_ctor_called());
264
265 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
266 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -0700267 ASSERT_STREQ("true", is_ctor_called());
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700268 dlclose(handle);
269}
Dimitry Ivanovba35b2d2016-04-08 11:47:53 -0700270
271TEST(dlfcn, ifunc_ctor_call_rtld_lazy) {
272 typedef const char* (*fn_ptr)();
273
274 void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY);
275 ASSERT_TRUE(handle != nullptr) << dlerror();
276 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
277 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
278 ASSERT_STREQ("false", is_ctor_called());
279
280 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
281 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
282 ASSERT_STREQ("true", is_ctor_called());
283 dlclose(handle);
284}
Dimitry Ivanov0a2ab022016-04-05 17:12:01 -0700285#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700286
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700287TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
288 // This is the structure of the test library and
289 // its dt_needed libraries
290 // libtest_relo_check_dt_needed_order.so
291 // |
292 // +-> libtest_relo_check_dt_needed_order_1.so
293 // |
294 // +-> libtest_relo_check_dt_needed_order_2.so
295 //
296 // The root library references relo_test_get_answer_lib - which is defined
297 // in both dt_needed libraries, the correct relocation should
298 // use the function defined in libtest_relo_check_dt_needed_order_1.so
299 void* handle = nullptr;
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -0700300 auto guard = make_scope_guard([&]() {
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -0700301 dlclose(handle);
302 });
303
304 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
305 ASSERT_TRUE(handle != nullptr) << dlerror();
306
307 typedef int (*fn_t) (void);
308 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
309 ASSERT_TRUE(fn != nullptr) << dlerror();
310 ASSERT_EQ(1, fn());
311}
312
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700313TEST(dlfcn, dlopen_check_order_dlsym) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700314 // Here is how the test library and its dt_needed
315 // libraries are arranged
316 //
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700317 // libtest_check_order_children.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700318 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700319 // +-> ..._1_left.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700320 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700321 // | +-> ..._a.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700322 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700323 // | +-> ...r_b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700324 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700325 // +-> ..._2_right.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700326 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700327 // | +-> ..._d.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700328 // | |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700329 // | +-> ..._b.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700330 // |
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700331 // +-> ..._3_c.so
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700332 //
333 // load order should be (1, 2, 3, a, b, d)
334 //
335 // get_answer() is defined in (2, 3, a, b, c)
336 // get_answer2() is defined in (b, d)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700337 void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700338 ASSERT_TRUE(sym == nullptr);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700339 void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
340 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700341 typedef int (*fn_t) (void);
342 fn_t fn, fn2;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700343 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700344 ASSERT_TRUE(fn != nullptr) << dlerror();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700345 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700346 ASSERT_TRUE(fn2 != nullptr) << dlerror();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700347
348 ASSERT_EQ(42, fn());
349 ASSERT_EQ(43, fn2());
350 dlclose(handle);
351}
352
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700353TEST(dlfcn, dlopen_check_order_reloc_siblings) {
354 // This is how this one works:
355 // we lookup and call get_answer which is defined in '_2.so'
356 // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
357 // the correct _impl() is implemented by '_a.so';
358 //
359 // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
360 //
361 // Here is the picture:
362 //
363 // libtest_check_order_reloc_siblings.so
364 // |
365 // +-> ..._1.so <- empty
366 // | |
367 // | +-> ..._a.so <- exports correct answer_impl()
368 // | |
369 // | +-> ..._b.so <- every other letter exporting incorrect one.
370 // |
371 // +-> ..._2.so <- empty
372 // | |
373 // | +-> ..._c.so
374 // | |
375 // | +-> ..._d.so
376 // |
377 // +-> ..._3.so <- empty
378 // |
379 // +-> ..._e.so
380 // |
381 // +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
382 // implements incorrect get_answer_impl()
383
384 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
385 ASSERT_TRUE(handle == nullptr);
386#ifdef __BIONIC__
387 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
388 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
389#endif
390
391 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
392 ASSERT_TRUE(handle != nullptr) << dlerror();
393
394 typedef int (*fn_t) (void);
395 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
396 ASSERT_TRUE(fn != nullptr) << dlerror();
397 ASSERT_EQ(42, fn());
398
399 ASSERT_EQ(0, dlclose(handle));
400}
401
402TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
403 // This test uses the same library as dlopen_check_order_reloc_siblings.
404 // Unlike dlopen_check_order_reloc_siblings it preloads
405 // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
406 // dlopen(libtest_check_order_reloc_siblings.so)
407
408 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
409 ASSERT_TRUE(handle == nullptr);
410 handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
411 ASSERT_TRUE(handle == nullptr);
412
413 void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
414 ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
415
416 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
417 ASSERT_TRUE(handle != nullptr) << dlerror();
418
419 ASSERT_EQ(0, dlclose(handle_for_1));
420
421 typedef int (*fn_t) (void);
422 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
423 ASSERT_TRUE(fn != nullptr) << dlerror();
424 ASSERT_EQ(42, fn());
425
426 ASSERT_EQ(0, dlclose(handle));
427}
428
Dmitriy Ivanov7699d132014-11-18 17:26:31 -0800429TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
430 // This is how this one works:
431 // we lookup and call grandchild_get_answer which is defined in '_2.so'
432 // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
433 // the correct _impl() is implemented by '_c_1.so';
434 //
435 // Here is the picture of subtree:
436 //
437 // libtest_check_order_reloc_siblings.so
438 // |
439 // +-> ..._2.so <- grandchild_get_answer()
440 // |
441 // +-> ..._c.so <- empty
442 // | |
443 // | +-> _c_1.so <- exports correct answer_impl()
444 // | |
445 // | +-> _c_2.so <- exports incorrect answer_impl()
446 // |
447 // +-> ..._d.so <- empty
448
449 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
450 ASSERT_TRUE(handle == nullptr);
451#ifdef __BIONIC__
452 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
453 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
454#endif
455
456 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
457 ASSERT_TRUE(handle != nullptr) << dlerror();
458
459 typedef int (*fn_t) (void);
460 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
461 ASSERT_TRUE(fn != nullptr) << dlerror();
462 ASSERT_EQ(42, fn());
463
464 ASSERT_EQ(0, dlclose(handle));
465}
466
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700467TEST(dlfcn, dlopen_check_order_reloc_nephew) {
468 // This is how this one works:
469 // we lookup and call nephew_get_answer which is defined in '_2.so'
470 // and in turn calls external get_answer_impl() defined in '_[a-f].so'
471 // the correct _impl() is implemented by '_a.so';
472 //
473 // Here is the picture:
474 //
475 // libtest_check_order_reloc_siblings.so
476 // |
477 // +-> ..._1.so <- empty
478 // | |
479 // | +-> ..._a.so <- exports correct answer_impl()
480 // | |
481 // | +-> ..._b.so <- every other letter exporting incorrect one.
482 // |
483 // +-> ..._2.so <- empty
484 // | |
485 // | +-> ..._c.so
486 // | |
487 // | +-> ..._d.so
488 // |
489 // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
490 // |
491 // +-> ..._e.so
492 // |
493 // +-> ..._f.so
494
495 void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
496 ASSERT_TRUE(handle == nullptr);
497#ifdef __BIONIC__
498 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
499 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
500#endif
501
502 handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
503 ASSERT_TRUE(handle != nullptr) << dlerror();
504
505 typedef int (*fn_t) (void);
506 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
507 ASSERT_TRUE(fn != nullptr) << dlerror();
508 ASSERT_EQ(42, fn());
509
510 ASSERT_EQ(0, dlclose(handle));
511}
512
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800513TEST(dlfcn, check_unload_after_reloc) {
514 // This is how this one works:
515 // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child
516 // |
517 // +-> libtest_two_parents_child
518 //
519 // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child
520 // |
521 // +-> libtest_two_parents_child
522 //
523 // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so
524 // as a second step it dlopens parent2 and dlcloses parent1...
525
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700526 void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL);
527 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800528
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700529 void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL);
530 ASSERT_TRUE(handle2 != nullptr) << dlerror();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800531
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700532 typedef int (*fn_t) (void);
533 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
534 ASSERT_TRUE(fn != nullptr) << dlerror();
535 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800536
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700537 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800538
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700539 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
540 ASSERT_TRUE(handle != nullptr);
541 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800542
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700543 fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer"));
544 ASSERT_TRUE(fn != nullptr) << dlerror();
545 ASSERT_EQ(42, fn());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800546
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700547 ASSERT_EQ(0, dlclose(handle2));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800548
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700549 handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
550 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800551}
552
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700553extern "C" int check_order_reloc_root_get_answer_impl() {
554 return 42;
555}
556
557TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
558 // This is how this one works:
559 // we lookup and call get_answer3 which is defined in 'root.so'
560 // and in turn calls external root_get_answer_impl() defined in _2.so and
561 // above the correct _impl() is one in the executable.
562 //
563 // libtest_check_order_reloc_root.so
564 // |
565 // +-> ..._1.so <- empty
566 // |
567 // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
568 //
569
570 void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
571 ASSERT_TRUE(handle == nullptr);
572#ifdef __BIONIC__
573 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
574 ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
575#endif
576
577 handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
578 ASSERT_TRUE(handle != nullptr) << dlerror();
579
580 typedef int (*fn_t) (void);
581 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
582 ASSERT_TRUE(fn != nullptr) << dlerror();
583 ASSERT_EQ(42, fn());
584
585 ASSERT_EQ(0, dlclose(handle));
586}
587
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700588TEST(dlfcn, dlopen_check_rtld_local) {
589 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
590 ASSERT_TRUE(sym == nullptr);
591
592 // implicit RTLD_LOCAL
593 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
594 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
595 ASSERT_TRUE(sym == nullptr);
596 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
597 sym = dlsym(handle, "dlopen_testlib_simple_func");
598 ASSERT_TRUE(sym != nullptr);
599 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
600 dlclose(handle);
601
602 // explicit RTLD_LOCAL
603 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
604 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
605 ASSERT_TRUE(sym == nullptr);
606 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
607 sym = dlsym(handle, "dlopen_testlib_simple_func");
608 ASSERT_TRUE(sym != nullptr);
609 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
610 dlclose(handle);
611}
612
613TEST(dlfcn, dlopen_check_rtld_global) {
614 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
615 ASSERT_TRUE(sym == nullptr);
616
617 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700618 ASSERT_TRUE(handle != nullptr) << dlerror();
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700619 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
620 ASSERT_TRUE(sym != nullptr) << dlerror();
621 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
622 dlclose(handle);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700623
624 // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
625 void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
626 ASSERT_EQ(sym, sym_after_dlclose);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700627
628 // Check if dlsym() for main program's handle searches RTLD_GLOBAL
629 // shared libraries after symbol was not found in the main executable
630 // and dependent libraries.
631 void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
632 sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
633 ASSERT_TRUE(sym != nullptr) << dlerror();
634
635 dlclose(handle_for_main_executable);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700636}
637
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700638// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
639// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
640// libtest_with_dependency_loop_a.so
641TEST(dlfcn, dlopen_check_loop) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700642 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
643 ASSERT_TRUE(handle != nullptr) << dlerror();
644 void* f = dlsym(handle, "dlopen_test_loopy_function");
645 ASSERT_TRUE(f != nullptr) << dlerror();
646 EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)());
647 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700648
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700649 // dlopen second time to make sure that the library was unloaded correctly
650 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
651 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800652#ifdef __BIONIC__
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700653 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 -0800654#else
655 // TODO: glibc returns nullptr on dlerror() here. Is it bug?
656 ASSERT_TRUE(dlerror() == nullptr);
Dmitriy Ivanoveb27bba2014-09-15 14:13:24 -0700657#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800658
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700659 handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD);
660 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700661}
662
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700663TEST(dlfcn, dlopen_nodelete) {
664 static bool is_unloaded = false;
665
666 void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE);
667 ASSERT_TRUE(handle != nullptr) << dlerror();
668 void (*set_unload_flag_ptr)(bool*);
669 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr"));
670 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
671 set_unload_flag_ptr(&is_unloaded);
672
673 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
674 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
675 ASSERT_EQ(1729U, *taxicab_number);
676 *taxicab_number = 2;
677
678 dlclose(handle);
679 ASSERT_TRUE(!is_unloaded);
680
681 uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
682 ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number);
683 ASSERT_EQ(2U, *taxicab_number_after_dlclose);
684
685
686 handle = dlopen("libtest_nodelete_1.so", RTLD_NOW);
687 uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number"));
688 ASSERT_EQ(taxicab_number2, taxicab_number);
689
690 ASSERT_EQ(2U, *taxicab_number2);
691
692 dlclose(handle);
693 ASSERT_TRUE(!is_unloaded);
694}
695
696TEST(dlfcn, dlopen_nodelete_on_second_dlopen) {
697 static bool is_unloaded = false;
698
699 void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW);
700 ASSERT_TRUE(handle != nullptr) << dlerror();
701 void (*set_unload_flag_ptr)(bool*);
702 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr"));
703 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
704 set_unload_flag_ptr(&is_unloaded);
705
706 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number"));
707 ASSERT_TRUE(taxicab_number != nullptr) << dlerror();
708
709 ASSERT_EQ(1729U, *taxicab_number);
710 *taxicab_number = 2;
711
712 // This RTLD_NODELETE should be ignored
713 void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE);
714 ASSERT_TRUE(handle1 != nullptr) << dlerror();
715 ASSERT_EQ(handle, handle1);
716
717 dlclose(handle1);
718 dlclose(handle);
719
720 ASSERT_TRUE(is_unloaded);
721}
722
723TEST(dlfcn, dlopen_nodelete_dt_flags_1) {
724 static bool is_unloaded = false;
725
726 void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW);
727 ASSERT_TRUE(handle != nullptr) << dlerror();
728 void (*set_unload_flag_ptr)(bool*);
729 set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr"));
730 ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror();
731 set_unload_flag_ptr(&is_unloaded);
732
733 dlclose(handle);
734 ASSERT_TRUE(!is_unloaded);
735}
736
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700737TEST(dlfcn, dlsym_df_1_global) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700738 void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
739 ASSERT_TRUE(handle != nullptr) << dlerror();
740 int (*get_answer)();
741 get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
742 ASSERT_TRUE(get_answer != nullptr) << dlerror();
743 ASSERT_EQ(42, get_answer());
744 ASSERT_EQ(0, dlclose(handle));
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700745}
746
Elliott Hughese66190d2012-12-18 15:57:55 -0800747TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700748 void* self = dlopen("/does/not/exist", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700749 ASSERT_TRUE(self == nullptr);
Elliott Hughes063525c2014-05-13 11:19:57 -0700750#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700751 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
752#else
753 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
754#endif
755}
756
Elliott Hughesad88a082012-10-24 18:37:21 -0700757static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700758 dlopen("/child/thread", RTLD_NOW);
759 return reinterpret_cast<void*>(strdup(dlerror()));
760}
761
Elliott Hughese66190d2012-12-18 15:57:55 -0800762TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700763 dlopen("/main/thread", RTLD_NOW);
764 const char* main_thread_error = dlerror();
765 ASSERT_SUBSTR("/main/thread", main_thread_error);
766
767 pthread_t t;
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700768 ASSERT_EQ(0, pthread_create(&t, nullptr, ConcurrentDlErrorFn, nullptr));
Elliott Hughes5419b942012-10-16 15:54:46 -0700769 void* result;
770 ASSERT_EQ(0, pthread_join(t, &result));
771 char* child_thread_error = static_cast<char*>(result);
772 ASSERT_SUBSTR("/child/thread", child_thread_error);
773 free(child_thread_error);
774
775 ASSERT_SUBSTR("/main/thread", main_thread_error);
776}
777
Elliott Hughese66190d2012-12-18 15:57:55 -0800778TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700779 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700780 void* self = dlopen(nullptr, RTLD_NOW);
781 ASSERT_TRUE(self != nullptr);
782 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700783
784 void* sym;
785
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700786#if defined(__BIONIC__) && !defined(__LP64__)
787 // RTLD_DEFAULT in lp32 bionic is not (void*)0
788 // so it can be distinguished from the NULL handle.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700789 sym = dlsym(nullptr, "test");
790 ASSERT_TRUE(sym == nullptr);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800791 ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700792#endif
793
Elliott Hughes3b297c42012-10-11 16:08:51 -0700794 // Symbol that doesn't exist.
795 sym = dlsym(self, "ThisSymbolDoesNotExist");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700796 ASSERT_TRUE(sym == nullptr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700797 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700798
799 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700800}
801
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700802TEST(dlfcn, dladdr_executable) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700803 dlerror(); // Clear any pending errors.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700804 void* self = dlopen(nullptr, RTLD_NOW);
805 ASSERT_TRUE(self != nullptr);
806 ASSERT_TRUE(dlerror() == nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700807
808 void* sym = dlsym(self, "DlSymTestFunction");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700809 ASSERT_TRUE(sym != nullptr);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700810
811 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
812 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
813
814 Dl_info info;
815 int rc = dladdr(addr, &info);
816 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
817
818 // Get the name of this executable.
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700819 const std::string& executable_path = get_executable_path();
Elliott Hughes8e15b082012-09-26 11:44:01 -0700820
821 // The filename should be that of this executable.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700822 char dli_realpath[PATH_MAX];
823 ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700824 ASSERT_STREQ(executable_path.c_str(), dli_realpath);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700825
826 // The symbol name should be the symbol we looked up.
827 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
828
829 // The address should be the exact address of the symbol.
830 ASSERT_EQ(info.dli_saddr, sym);
831
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700832 std::vector<map_record> maps;
833 ASSERT_TRUE(Maps::parse_maps(&maps));
834
835 void* base_address = nullptr;
836 for (const map_record& rec : maps) {
837 if (executable_path == rec.pathname) {
838 base_address = reinterpret_cast<void*>(rec.addr_start);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700839 break;
840 }
841 }
Elliott Hughes8e15b082012-09-26 11:44:01 -0700842
843 // The base address should be the address we were loaded at.
844 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700845
846 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700847}
848
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700849TEST(dlfcn, dlopen_executable_by_absolute_path) {
850 void* handle1 = dlopen(nullptr, RTLD_NOW);
851 ASSERT_TRUE(handle1 != nullptr) << dlerror();
852
853 void* handle2 = dlopen(get_executable_path().c_str(), RTLD_NOW);
854 ASSERT_TRUE(handle2 != nullptr) << dlerror();
855
856#if defined(__BIONIC__)
857 ASSERT_EQ(handle1, handle2);
858#else
859 GTEST_LOG_(INFO) << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
860 "it loads a separate copy of the main executable "
861 "on dlopen by absolute path.";
862#endif
863}
864
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700865#if defined(__LP64__)
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200866#define PATH_TO_SYSTEM_LIB "/system/lib64/"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700867#else
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200868#define PATH_TO_SYSTEM_LIB "/system/lib/"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700869#endif
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200870#define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so"
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700871
872TEST(dlfcn, dladdr_libc) {
873#if defined(__BIONIC__)
874 Dl_info info;
875 void* addr = reinterpret_cast<void*>(puts); // well-known libc function
876 ASSERT_TRUE(dladdr(addr, &info) != 0);
877
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700878 // /system/lib is symlink when this test is executed on host.
879 char libc_realpath[PATH_MAX];
Lazar Trsic6f2d3102015-10-13 16:43:00 +0200880 ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath);
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700881
882 ASSERT_STREQ(libc_realpath, info.dli_fname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700883 // TODO: add check for dfi_fbase
884 ASSERT_STREQ("puts", info.dli_sname);
885 ASSERT_EQ(addr, info.dli_saddr);
886#else
887 GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig "
888 "for libc.so, which is symlink itself (not a realpath).\n";
889#endif
890}
891
Elliott Hughese66190d2012-12-18 15:57:55 -0800892TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700893 Dl_info info;
894
Elliott Hughes3b297c42012-10-11 16:08:51 -0700895 dlerror(); // Clear any pending errors.
896
Elliott Hughes8e15b082012-09-26 11:44:01 -0700897 // No symbol corresponding to NULL.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700898 ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
899 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700900
901 // No symbol corresponding to a stack address.
902 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700903 ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700904}
Elliott Hughes124fae92012-10-31 14:20:03 -0700905
Elliott Hughesa43e9062013-01-07 14:18:22 -0800906// GNU-style ELF hash tables are incompatible with the MIPS ABI.
907// 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 -0800908TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800909#if !defined(__mips__)
Elliott Hughes124fae92012-10-31 14:20:03 -0700910 dlerror(); // Clear any pending errors.
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800911 void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW);
912 ASSERT_TRUE(handle != nullptr) << dlerror();
913 auto guard = make_scope_guard([&]() {
914 dlclose(handle);
915 });
916 void* sym = dlsym(handle, "getRandomNumber");
917 ASSERT_TRUE(sym != nullptr) << dlerror();
918 int (*fn)(void);
919 fn = reinterpret_cast<int (*)(void)>(sym);
920 EXPECT_EQ(4, fn());
921
922 Dl_info dlinfo;
923 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
924
925 ASSERT_TRUE(fn == dlinfo.dli_saddr);
926 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800927 ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800928#else
929 GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n";
930#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700931}
Elliott Hughese66190d2012-12-18 15:57:55 -0800932
Dmitriy Ivanovb3356772014-11-14 11:19:22 -0800933TEST(dlfcn, dlopen_library_with_only_sysv_hash) {
934 void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW);
935 ASSERT_TRUE(handle != nullptr) << dlerror();
936 auto guard = make_scope_guard([&]() {
937 dlclose(handle);
938 });
939 void* sym = dlsym(handle, "getRandomNumber");
940 ASSERT_TRUE(sym != nullptr) << dlerror();
941 int (*fn)(void);
942 fn = reinterpret_cast<int (*)(void)>(sym);
943 EXPECT_EQ(4, fn());
944
945 Dl_info dlinfo;
946 ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo));
947
948 ASSERT_TRUE(fn == dlinfo.dli_saddr);
949 ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname);
950 ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname);
951}
952
Elliott Hughese66190d2012-12-18 15:57:55 -0800953TEST(dlfcn, dlopen_bad_flags) {
954 dlerror(); // Clear any pending errors.
955 void* handle;
956
Elliott Hughes063525c2014-05-13 11:19:57 -0700957#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800958 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700959 handle = dlopen(nullptr, 0);
960 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -0800961 ASSERT_SUBSTR("invalid", dlerror());
962#endif
963
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700964 handle = dlopen(nullptr, 0xffffffff);
965 ASSERT_TRUE(handle == nullptr);
Elliott Hughese66190d2012-12-18 15:57:55 -0800966 ASSERT_SUBSTR("invalid", dlerror());
967
968 // 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 -0700969 handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY);
970 ASSERT_TRUE(handle != nullptr);
971 ASSERT_SUBSTR(nullptr, dlerror());
Elliott Hughese66190d2012-12-18 15:57:55 -0800972}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400973
974TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800975 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700976 ASSERT_TRUE(addr == nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400977}
978
979TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800980 void* addr = dlsym(RTLD_DEFAULT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700981 ASSERT_TRUE(addr != nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -0800982}
983
984TEST(dlfcn, rtld_next_unknown_symbol) {
985 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700986 ASSERT_TRUE(addr == nullptr);
Elliott Hughes2ed71092013-11-11 15:48:06 -0800987}
988
989TEST(dlfcn, rtld_next_known_symbol) {
990 void* addr = dlsym(RTLD_NEXT, "fopen");
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700991 ASSERT_TRUE(addr != nullptr);
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400992}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700993
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700994TEST(dlfcn, dlsym_weak_func) {
995 dlerror();
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -0800996 void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -0700997 ASSERT_TRUE(handle != nullptr);
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700998
999 int (*weak_func)();
1000 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001001 ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror();
Dmitriy Ivanovce441662014-06-17 15:56:38 -07001002 EXPECT_EQ(42, weak_func());
1003 dlclose(handle);
1004}
1005
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001006TEST(dlfcn, dlopen_undefined_weak_func) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001007 void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW);
1008 ASSERT_TRUE(handle != nullptr) << dlerror();
1009 int (*weak_func)();
1010 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func"));
1011 ASSERT_TRUE(weak_func != nullptr) << dlerror();
1012 EXPECT_EQ(6551, weak_func());
1013 dlclose(handle);
Dmitriy Ivanovbfa88bc2014-12-16 11:40:46 -08001014}
1015
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001016TEST(dlfcn, dlopen_symlink) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001017 DlfcnSymlink symlink("dlopen_symlink");
1018 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001019 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
Dimitry Ivanov708589f2016-09-19 10:50:28 -07001020 void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanovaff18fd2015-06-23 13:58:22 -07001021 ASSERT_TRUE(handle1 != nullptr);
1022 ASSERT_TRUE(handle2 != nullptr);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001023 ASSERT_EQ(handle1, handle2);
Dmitriy Ivanov319356e2014-09-02 17:31:44 -07001024 dlclose(handle1);
1025 dlclose(handle2);
Dmitriy Ivanov7db18092014-05-08 12:27:25 -07001026}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001027
1028// libtest_dlopen_from_ctor_main.so depends on
1029// libtest_dlopen_from_ctor.so which has a constructor
1030// that calls dlopen(libc...). This is to test the situation
1031// described in b/7941716.
1032TEST(dlfcn, dlopen_dlopen_from_ctor) {
1033#if defined(__BIONIC__)
1034 void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW);
1035 ASSERT_TRUE(handle != nullptr) << dlerror();
1036 dlclose(handle);
1037#else
1038 GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n";
1039#endif
1040}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001041
1042TEST(dlfcn, symbol_versioning_use_v1) {
1043 void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW);
1044 ASSERT_TRUE(handle != nullptr) << dlerror();
1045 typedef int (*fn_t)();
1046 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1047 ASSERT_TRUE(fn != nullptr) << dlerror();
1048 ASSERT_EQ(1, fn());
1049 dlclose(handle);
1050}
1051
1052TEST(dlfcn, symbol_versioning_use_v2) {
1053 void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW);
1054 ASSERT_TRUE(handle != nullptr) << dlerror();
1055 typedef int (*fn_t)();
1056 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1057 ASSERT_TRUE(fn != nullptr) << dlerror();
1058 ASSERT_EQ(2, fn());
1059 dlclose(handle);
1060}
1061
1062TEST(dlfcn, symbol_versioning_use_other_v2) {
1063 void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW);
1064 ASSERT_TRUE(handle != nullptr) << dlerror();
1065 typedef int (*fn_t)();
1066 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1067 ASSERT_TRUE(fn != nullptr) << dlerror();
1068 ASSERT_EQ(20, fn());
1069 dlclose(handle);
1070}
1071
1072TEST(dlfcn, symbol_versioning_use_other_v3) {
1073 void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW);
1074 ASSERT_TRUE(handle != nullptr) << dlerror();
1075 typedef int (*fn_t)();
1076 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version"));
1077 ASSERT_TRUE(fn != nullptr) << dlerror();
1078 ASSERT_EQ(3, fn());
1079 dlclose(handle);
1080}
1081
1082TEST(dlfcn, symbol_versioning_default_via_dlsym) {
1083 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1084 ASSERT_TRUE(handle != nullptr) << dlerror();
1085 typedef int (*fn_t)();
1086 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function"));
1087 ASSERT_TRUE(fn != nullptr) << dlerror();
1088 ASSERT_EQ(3, fn()); // the default version is 3
1089 dlclose(handle);
1090}
1091
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001092TEST(dlfcn, dlvsym_smoke) {
1093 void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW);
1094 ASSERT_TRUE(handle != nullptr) << dlerror();
1095 typedef int (*fn_t)();
1096
1097 {
1098 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion"));
1099 ASSERT_TRUE(fn == nullptr);
1100 ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror());
1101 }
1102
1103 {
1104 fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2"));
1105 ASSERT_TRUE(fn != nullptr) << dlerror();
1106 ASSERT_EQ(2, fn());
1107 }
1108
1109 dlclose(handle);
1110}
1111
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001112// This preempts the implementation from libtest_versioned_lib.so
1113extern "C" int version_zero_function() {
1114 return 0;
1115}
1116
1117// This preempts the implementation from libtest_versioned_uselibv*.so
1118extern "C" int version_zero_function2() {
1119 return 0;
1120}
Evgenii Stepanov68650822015-06-10 13:38:39 -07001121
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001122TEST(dlfcn, dt_runpath_smoke) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001123 void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW);
1124 ASSERT_TRUE(handle != nullptr) << dlerror();
1125
1126 typedef void *(* dlopen_b_fn)();
1127 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1128 ASSERT_TRUE(fn != nullptr) << dlerror();
1129
1130 void *p = fn();
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001131 ASSERT_TRUE(p != nullptr);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001132
1133 dlclose(handle);
1134}
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001135
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001136// Bionic specific tests
1137#if defined(__BIONIC__)
1138
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001139TEST(dlfcn, dt_runpath_absolute_path) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001140 std::string libpath = g_testlib_root + "/libtest_dt_runpath_d.so";
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001141 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
Lazar Trsic6f2d3102015-10-13 16:43:00 +02001142 ASSERT_TRUE(handle != nullptr) << dlerror();
1143
1144 typedef void *(* dlopen_b_fn)();
1145 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
1146 ASSERT_TRUE(fn != nullptr) << dlerror();
1147
1148 void *p = fn();
1149 ASSERT_TRUE(p != nullptr);
1150
1151 dlclose(handle);
1152}
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001153
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001154TEST(dlfcn, dlopen_invalid_rw_load_segment) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001155 const std::string libpath = g_testlib_root +
1156 "/" + kPrebuiltElfDir +
1157 "/libtest_invalid-rw_load_segment.so";
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001158 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1159 ASSERT_TRUE(handle == nullptr);
1160 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W + E load segments are not allowed";
1161 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1162}
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001163
1164TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001165 const std::string libpath = g_testlib_root +
1166 "/" + kPrebuiltElfDir +
1167 "/libtest_invalid-unaligned_shdr_offset.so";
1168
Dimitry Ivanov972e3d02016-08-12 14:25:50 -07001169 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1170 ASSERT_TRUE(handle == nullptr);
1171 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
1172 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1173}
1174
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001175TEST(dlfcn, dlopen_invalid_zero_shentsize) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001176 const std::string libpath = g_testlib_root +
1177 "/" + kPrebuiltElfDir +
1178 "/libtest_invalid-zero_shentsize.so";
1179
Dimitry Ivanovcb86c312016-08-12 15:28:42 -07001180 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1181 ASSERT_TRUE(handle == nullptr);
1182 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x";
1183 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1184}
1185
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001186TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001187 const std::string libpath = g_testlib_root +
1188 "/" + kPrebuiltElfDir +
1189 "/libtest_invalid-zero_shstrndx.so";
1190
Dimitry Ivanovc9a95612016-08-15 10:27:47 -07001191 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1192 ASSERT_TRUE(handle == nullptr);
1193 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx";
1194 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1195}
1196
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001197TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001198 const std::string libpath = g_testlib_root +
1199 "/" + kPrebuiltElfDir +
1200 "/libtest_invalid-empty_shdr_table.so";
1201
Dimitry Ivanov8bdf70e2016-08-15 11:30:45 -07001202 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1203 ASSERT_TRUE(handle == nullptr);
1204 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers";
1205 ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
1206}
1207
Dimitry Ivanov46230442016-08-15 13:40:53 -07001208TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001209 const std::string libpath = g_testlib_root +
1210 "/" + kPrebuiltElfDir +
1211 "/libtest_invalid-zero_shdr_table_offset.so";
1212
Dimitry Ivanov46230442016-08-15 13:40:53 -07001213 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1214 ASSERT_TRUE(handle == nullptr);
1215 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/";
1216 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1217}
1218
Dimitry Ivanov55958342016-08-15 14:06:04 -07001219TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001220 const std::string libpath = g_testlib_root +
1221 "/" + kPrebuiltElfDir +
1222 "/libtest_invalid-zero_shdr_table_content.so";
1223
Dimitry Ivanov55958342016-08-15 14:06:04 -07001224 void* handle = dlopen(libpath.c_str(), RTLD_NOW);
1225 ASSERT_TRUE(handle == nullptr);
1226 std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
1227 ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
1228}
1229
Dimitry Ivanov9700bab2016-08-10 18:54:06 -07001230#endif