jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 20 | #include <libgen.h> |
| 21 | #include <limits.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdint.h> |
| 24 | |
Dmitriy Ivanov | b2a30ee | 2014-09-04 18:23:00 -0700 | [diff] [blame] | 25 | #include "private/ScopeGuard.h" |
| 26 | |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 27 | #include <string> |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 28 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 29 | #define ASSERT_SUBSTR(needle, haystack) \ |
| 30 | ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack) |
| 31 | |
Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 32 | #if defined(__LP64__) |
| 33 | #define LIBPATH_PREFIX "/nativetest64/libdlext_test_fd/" |
| 34 | #else |
| 35 | #define LIBPATH_PREFIX "/nativetest/libdlext_test_fd/" |
| 36 | #endif |
| 37 | |
| 38 | #define LIBZIPPATH LIBPATH_PREFIX "libdlext_test_fd_zipaligned.zip" |
| 39 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 40 | static bool g_called = false; |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 41 | extern "C" void DlSymTestFunction() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 42 | g_called = true; |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Dmitriy Ivanov | f8846a4 | 2014-07-08 21:21:34 -0700 | [diff] [blame] | 45 | static int g_ctor_function_called = 0; |
| 46 | |
| 47 | extern "C" void ctor_function() __attribute__ ((constructor)); |
| 48 | |
| 49 | extern "C" void ctor_function() { |
| 50 | g_ctor_function_called = 17; |
| 51 | } |
| 52 | |
| 53 | TEST(dlfcn, ctor_function_call) { |
| 54 | ASSERT_EQ(17, g_ctor_function_called); |
| 55 | } |
| 56 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 57 | TEST(dlfcn, dlsym_in_self) { |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 58 | dlerror(); // Clear any pending errors. |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 59 | void* self = dlopen(NULL, RTLD_NOW); |
| 60 | ASSERT_TRUE(self != NULL); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 61 | ASSERT_TRUE(dlerror() == NULL); |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 62 | |
| 63 | void* sym = dlsym(self, "DlSymTestFunction"); |
| 64 | ASSERT_TRUE(sym != NULL); |
| 65 | |
| 66 | void (*function)() = reinterpret_cast<void(*)()>(sym); |
| 67 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 68 | g_called = false; |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 69 | function(); |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 70 | ASSERT_TRUE(g_called); |
Elliott Hughes | 1a69616 | 2012-11-01 13:49:32 -0700 | [diff] [blame] | 71 | |
| 72 | ASSERT_EQ(0, dlclose(self)); |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 73 | } |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 74 | |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 75 | TEST(dlfcn, dlsym_with_dependencies) { |
| 76 | void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW); |
| 77 | ASSERT_TRUE(handle != NULL); |
| 78 | dlerror(); |
| 79 | // This symbol is in DT_NEEDED library. |
| 80 | void* sym = dlsym(handle, "getRandomNumber"); |
| 81 | ASSERT_TRUE(sym != NULL); |
| 82 | int (*fn)(void); |
| 83 | fn = reinterpret_cast<int (*)(void)>(sym); |
| 84 | EXPECT_EQ(4, fn()); |
| 85 | dlclose(handle); |
| 86 | } |
| 87 | |
Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 88 | TEST(dlfcn, dlopen_noload) { |
| 89 | void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD); |
| 90 | ASSERT_TRUE(handle == NULL); |
| 91 | handle = dlopen("libtest_simple.so", RTLD_NOW); |
| 92 | void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD); |
| 93 | ASSERT_TRUE(handle != NULL); |
| 94 | ASSERT_TRUE(handle2 != NULL); |
| 95 | ASSERT_TRUE(handle == handle2); |
| 96 | ASSERT_EQ(0, dlclose(handle)); |
| 97 | ASSERT_EQ(0, dlclose(handle2)); |
| 98 | } |
| 99 | |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame^] | 100 | TEST(dlfcn, dlopen_by_soname) { |
| 101 | static const char* soname = "libdlext_test_soname.so"; |
| 102 | static const char* filename = "libdlext_test_different_soname.so"; |
| 103 | // 1. Make sure there is no library with soname in default search path |
| 104 | void* handle = dlopen(soname, RTLD_NOW); |
| 105 | ASSERT_TRUE(handle == nullptr); |
| 106 | |
| 107 | // 2. Load a library using filename |
| 108 | handle = dlopen(filename, RTLD_NOW); |
| 109 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 110 | |
| 111 | // 3. Find library by soname |
| 112 | void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD); |
| 113 | ASSERT_TRUE(handle_soname != nullptr) << dlerror(); |
| 114 | ASSERT_EQ(handle, handle_soname); |
| 115 | |
| 116 | // 4. RTLD_NOLOAD should still work with filename |
| 117 | void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD); |
| 118 | ASSERT_TRUE(handle_filename != nullptr) << dlerror(); |
| 119 | ASSERT_EQ(handle, handle_filename); |
| 120 | |
| 121 | dlclose(handle_filename); |
| 122 | dlclose(handle_soname); |
| 123 | dlclose(handle); |
| 124 | } |
| 125 | |
Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 126 | // ifuncs are only supported on intel and arm64 for now |
| 127 | #if defined (__aarch64__) || defined(__i386__) || defined(__x86_64__) |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 128 | TEST(dlfcn, ifunc) { |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 129 | typedef const char* (*fn_ptr)(); |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 130 | |
| 131 | // ifunc's choice depends on whether IFUNC_CHOICE has a value |
| 132 | // first check the set case |
| 133 | setenv("IFUNC_CHOICE", "set", 1); |
| 134 | void* handle = dlopen("libtest_ifunc.so", RTLD_NOW); |
| 135 | ASSERT_TRUE(handle != NULL); |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 136 | fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo")); |
| 137 | fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library")); |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 138 | ASSERT_TRUE(foo_ptr != NULL); |
| 139 | ASSERT_TRUE(foo_library_ptr != NULL); |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 140 | ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0); |
| 141 | ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0); |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 142 | dlclose(handle); |
| 143 | |
| 144 | // then check the unset case |
| 145 | unsetenv("IFUNC_CHOICE"); |
| 146 | handle = dlopen("libtest_ifunc.so", RTLD_NOW); |
| 147 | ASSERT_TRUE(handle != NULL); |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 148 | foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo")); |
| 149 | foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library")); |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 150 | ASSERT_TRUE(foo_ptr != NULL); |
| 151 | ASSERT_TRUE(foo_library_ptr != NULL); |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 152 | ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0); |
| 153 | ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0); |
| 154 | dlclose(handle); |
| 155 | } |
| 156 | |
| 157 | TEST(dlfcn, ifunc_ctor_call) { |
| 158 | typedef const char* (*fn_ptr)(); |
| 159 | |
| 160 | void* handle = dlopen("libtest_ifunc.so", RTLD_NOW); |
Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 161 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 162 | fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative")); |
| 163 | ASSERT_TRUE(is_ctor_called != nullptr) << dlerror(); |
| 164 | ASSERT_STREQ("false", is_ctor_called()); |
| 165 | |
| 166 | is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot")); |
| 167 | ASSERT_TRUE(is_ctor_called != nullptr) << dlerror(); |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 168 | ASSERT_STREQ("true", is_ctor_called()); |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 169 | dlclose(handle); |
| 170 | } |
| 171 | #endif |
| 172 | |
Dmitriy Ivanov | b2a30ee | 2014-09-04 18:23:00 -0700 | [diff] [blame] | 173 | TEST(dlfcn, dlopen_check_relocation_dt_needed_order) { |
| 174 | // This is the structure of the test library and |
| 175 | // its dt_needed libraries |
| 176 | // libtest_relo_check_dt_needed_order.so |
| 177 | // | |
| 178 | // +-> libtest_relo_check_dt_needed_order_1.so |
| 179 | // | |
| 180 | // +-> libtest_relo_check_dt_needed_order_2.so |
| 181 | // |
| 182 | // The root library references relo_test_get_answer_lib - which is defined |
| 183 | // in both dt_needed libraries, the correct relocation should |
| 184 | // use the function defined in libtest_relo_check_dt_needed_order_1.so |
| 185 | void* handle = nullptr; |
Dmitriy Ivanov | d9ff722 | 2014-09-08 16:22:22 -0700 | [diff] [blame] | 186 | auto guard = make_scope_guard([&]() { |
Dmitriy Ivanov | b2a30ee | 2014-09-04 18:23:00 -0700 | [diff] [blame] | 187 | dlclose(handle); |
| 188 | }); |
| 189 | |
| 190 | handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW); |
| 191 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 192 | |
| 193 | typedef int (*fn_t) (void); |
| 194 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer")); |
| 195 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 196 | ASSERT_EQ(1, fn()); |
| 197 | } |
| 198 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 199 | TEST(dlfcn, dlopen_check_order_dlsym) { |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 200 | // Here is how the test library and its dt_needed |
| 201 | // libraries are arranged |
| 202 | // |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 203 | // libtest_check_order_children.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 204 | // | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 205 | // +-> ..._1_left.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 206 | // | | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 207 | // | +-> ..._a.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 208 | // | | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 209 | // | +-> ...r_b.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 210 | // | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 211 | // +-> ..._2_right.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 212 | // | | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 213 | // | +-> ..._d.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 214 | // | | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 215 | // | +-> ..._b.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 216 | // | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 217 | // +-> ..._3_c.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 218 | // |
| 219 | // load order should be (1, 2, 3, a, b, d) |
| 220 | // |
| 221 | // get_answer() is defined in (2, 3, a, b, c) |
| 222 | // get_answer2() is defined in (b, d) |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 223 | void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 224 | ASSERT_TRUE(sym == nullptr); |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 225 | void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL); |
| 226 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 227 | typedef int (*fn_t) (void); |
| 228 | fn_t fn, fn2; |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 229 | fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer")); |
Dmitriy Ivanov | eb27bba | 2014-09-15 14:13:24 -0700 | [diff] [blame] | 230 | ASSERT_TRUE(fn != NULL) << dlerror(); |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 231 | fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2")); |
Dmitriy Ivanov | eb27bba | 2014-09-15 14:13:24 -0700 | [diff] [blame] | 232 | ASSERT_TRUE(fn2 != NULL) << dlerror(); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 233 | |
| 234 | ASSERT_EQ(42, fn()); |
| 235 | ASSERT_EQ(43, fn2()); |
| 236 | dlclose(handle); |
| 237 | } |
| 238 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 239 | TEST(dlfcn, dlopen_check_order_reloc_siblings) { |
| 240 | // This is how this one works: |
| 241 | // we lookup and call get_answer which is defined in '_2.so' |
| 242 | // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so' |
| 243 | // the correct _impl() is implemented by '_a.so'; |
| 244 | // |
| 245 | // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?) |
| 246 | // |
| 247 | // Here is the picture: |
| 248 | // |
| 249 | // libtest_check_order_reloc_siblings.so |
| 250 | // | |
| 251 | // +-> ..._1.so <- empty |
| 252 | // | | |
| 253 | // | +-> ..._a.so <- exports correct answer_impl() |
| 254 | // | | |
| 255 | // | +-> ..._b.so <- every other letter exporting incorrect one. |
| 256 | // | |
| 257 | // +-> ..._2.so <- empty |
| 258 | // | | |
| 259 | // | +-> ..._c.so |
| 260 | // | | |
| 261 | // | +-> ..._d.so |
| 262 | // | |
| 263 | // +-> ..._3.so <- empty |
| 264 | // | |
| 265 | // +-> ..._e.so |
| 266 | // | |
| 267 | // +-> ..._f.so <- exports get_answer() that calls get_anser_impl(); |
| 268 | // implements incorrect get_answer_impl() |
| 269 | |
| 270 | void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD); |
| 271 | ASSERT_TRUE(handle == nullptr); |
| 272 | #ifdef __BIONIC__ |
| 273 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? |
| 274 | ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
| 275 | #endif |
| 276 | |
| 277 | handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL); |
| 278 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 279 | |
| 280 | typedef int (*fn_t) (void); |
| 281 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer")); |
| 282 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 283 | ASSERT_EQ(42, fn()); |
| 284 | |
| 285 | ASSERT_EQ(0, dlclose(handle)); |
| 286 | } |
| 287 | |
| 288 | TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) { |
| 289 | // This test uses the same library as dlopen_check_order_reloc_siblings. |
| 290 | // Unlike dlopen_check_order_reloc_siblings it preloads |
| 291 | // libtest_check_order_reloc_siblings_1.so (first dependency) prior to |
| 292 | // dlopen(libtest_check_order_reloc_siblings.so) |
| 293 | |
| 294 | void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD); |
| 295 | ASSERT_TRUE(handle == nullptr); |
| 296 | handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD); |
| 297 | ASSERT_TRUE(handle == nullptr); |
| 298 | |
| 299 | void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL); |
| 300 | ASSERT_TRUE(handle_for_1 != nullptr) << dlerror(); |
| 301 | |
| 302 | handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL); |
| 303 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 304 | |
| 305 | ASSERT_EQ(0, dlclose(handle_for_1)); |
| 306 | |
| 307 | typedef int (*fn_t) (void); |
| 308 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer")); |
| 309 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 310 | ASSERT_EQ(42, fn()); |
| 311 | |
| 312 | ASSERT_EQ(0, dlclose(handle)); |
| 313 | } |
| 314 | |
Dmitriy Ivanov | 7699d13 | 2014-11-18 17:26:31 -0800 | [diff] [blame] | 315 | TEST(dlfcn, dlopen_check_order_reloc_grandchild) { |
| 316 | // This is how this one works: |
| 317 | // we lookup and call grandchild_get_answer which is defined in '_2.so' |
| 318 | // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so' |
| 319 | // the correct _impl() is implemented by '_c_1.so'; |
| 320 | // |
| 321 | // Here is the picture of subtree: |
| 322 | // |
| 323 | // libtest_check_order_reloc_siblings.so |
| 324 | // | |
| 325 | // +-> ..._2.so <- grandchild_get_answer() |
| 326 | // | |
| 327 | // +-> ..._c.so <- empty |
| 328 | // | | |
| 329 | // | +-> _c_1.so <- exports correct answer_impl() |
| 330 | // | | |
| 331 | // | +-> _c_2.so <- exports incorrect answer_impl() |
| 332 | // | |
| 333 | // +-> ..._d.so <- empty |
| 334 | |
| 335 | void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD); |
| 336 | ASSERT_TRUE(handle == nullptr); |
| 337 | #ifdef __BIONIC__ |
| 338 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? |
| 339 | ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
| 340 | #endif |
| 341 | |
| 342 | handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL); |
| 343 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 344 | |
| 345 | typedef int (*fn_t) (void); |
| 346 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer")); |
| 347 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 348 | ASSERT_EQ(42, fn()); |
| 349 | |
| 350 | ASSERT_EQ(0, dlclose(handle)); |
| 351 | } |
| 352 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 353 | TEST(dlfcn, dlopen_check_order_reloc_nephew) { |
| 354 | // This is how this one works: |
| 355 | // we lookup and call nephew_get_answer which is defined in '_2.so' |
| 356 | // and in turn calls external get_answer_impl() defined in '_[a-f].so' |
| 357 | // the correct _impl() is implemented by '_a.so'; |
| 358 | // |
| 359 | // Here is the picture: |
| 360 | // |
| 361 | // libtest_check_order_reloc_siblings.so |
| 362 | // | |
| 363 | // +-> ..._1.so <- empty |
| 364 | // | | |
| 365 | // | +-> ..._a.so <- exports correct answer_impl() |
| 366 | // | | |
| 367 | // | +-> ..._b.so <- every other letter exporting incorrect one. |
| 368 | // | |
| 369 | // +-> ..._2.so <- empty |
| 370 | // | | |
| 371 | // | +-> ..._c.so |
| 372 | // | | |
| 373 | // | +-> ..._d.so |
| 374 | // | |
| 375 | // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl(); |
| 376 | // | |
| 377 | // +-> ..._e.so |
| 378 | // | |
| 379 | // +-> ..._f.so |
| 380 | |
| 381 | void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD); |
| 382 | ASSERT_TRUE(handle == nullptr); |
| 383 | #ifdef __BIONIC__ |
| 384 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? |
| 385 | ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
| 386 | #endif |
| 387 | |
| 388 | handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL); |
| 389 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 390 | |
| 391 | typedef int (*fn_t) (void); |
| 392 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer")); |
| 393 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 394 | ASSERT_EQ(42, fn()); |
| 395 | |
| 396 | ASSERT_EQ(0, dlclose(handle)); |
| 397 | } |
| 398 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 399 | TEST(dlfcn, check_unload_after_reloc) { |
| 400 | // This is how this one works: |
| 401 | // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child |
| 402 | // | |
| 403 | // +-> libtest_two_parents_child |
| 404 | // |
| 405 | // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child |
| 406 | // | |
| 407 | // +-> libtest_two_parents_child |
| 408 | // |
| 409 | // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so |
| 410 | // as a second step it dlopens parent2 and dlcloses parent1... |
| 411 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 412 | void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL); |
| 413 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 414 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 415 | void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL); |
| 416 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 417 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 418 | typedef int (*fn_t) (void); |
| 419 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer")); |
| 420 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 421 | ASSERT_EQ(42, fn()); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 422 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 423 | ASSERT_EQ(0, dlclose(handle)); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 424 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 425 | handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD); |
| 426 | ASSERT_TRUE(handle != nullptr); |
| 427 | ASSERT_EQ(0, dlclose(handle)); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 428 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 429 | fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer")); |
| 430 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 431 | ASSERT_EQ(42, fn()); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 432 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 433 | ASSERT_EQ(0, dlclose(handle2)); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 434 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 435 | handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD); |
| 436 | ASSERT_TRUE(handle == nullptr); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 439 | extern "C" int check_order_reloc_root_get_answer_impl() { |
| 440 | return 42; |
| 441 | } |
| 442 | |
| 443 | TEST(dlfcn, dlopen_check_order_reloc_main_executable) { |
| 444 | // This is how this one works: |
| 445 | // we lookup and call get_answer3 which is defined in 'root.so' |
| 446 | // and in turn calls external root_get_answer_impl() defined in _2.so and |
| 447 | // above the correct _impl() is one in the executable. |
| 448 | // |
| 449 | // libtest_check_order_reloc_root.so |
| 450 | // | |
| 451 | // +-> ..._1.so <- empty |
| 452 | // | |
| 453 | // +-> ..._2.so <- gives incorrect answer for answer_main_impl() |
| 454 | // |
| 455 | |
| 456 | void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD); |
| 457 | ASSERT_TRUE(handle == nullptr); |
| 458 | #ifdef __BIONIC__ |
| 459 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? |
| 460 | ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
| 461 | #endif |
| 462 | |
| 463 | handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL); |
| 464 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 465 | |
| 466 | typedef int (*fn_t) (void); |
| 467 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer")); |
| 468 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 469 | ASSERT_EQ(42, fn()); |
| 470 | |
| 471 | ASSERT_EQ(0, dlclose(handle)); |
| 472 | } |
| 473 | |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 474 | TEST(dlfcn, dlopen_check_rtld_local) { |
| 475 | void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 476 | ASSERT_TRUE(sym == nullptr); |
| 477 | |
| 478 | // implicit RTLD_LOCAL |
| 479 | void* handle = dlopen("libtest_simple.so", RTLD_NOW); |
| 480 | sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 481 | ASSERT_TRUE(sym == nullptr); |
| 482 | ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror()); |
| 483 | sym = dlsym(handle, "dlopen_testlib_simple_func"); |
| 484 | ASSERT_TRUE(sym != nullptr); |
| 485 | ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)()); |
| 486 | dlclose(handle); |
| 487 | |
| 488 | // explicit RTLD_LOCAL |
| 489 | handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL); |
| 490 | sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 491 | ASSERT_TRUE(sym == nullptr); |
| 492 | ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror()); |
| 493 | sym = dlsym(handle, "dlopen_testlib_simple_func"); |
| 494 | ASSERT_TRUE(sym != nullptr); |
| 495 | ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)()); |
| 496 | dlclose(handle); |
| 497 | } |
| 498 | |
| 499 | TEST(dlfcn, dlopen_check_rtld_global) { |
| 500 | void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 501 | ASSERT_TRUE(sym == nullptr); |
| 502 | |
| 503 | void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL); |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 504 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 505 | sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 506 | ASSERT_TRUE(sym != nullptr) << dlerror(); |
| 507 | ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)()); |
| 508 | dlclose(handle); |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 509 | |
| 510 | // RTLD_GLOBAL implies RTLD_NODELETE, let's check that |
| 511 | void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 512 | ASSERT_EQ(sym, sym_after_dlclose); |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 515 | // libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so -> |
| 516 | // libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so -> |
| 517 | // libtest_with_dependency_loop_a.so |
| 518 | TEST(dlfcn, dlopen_check_loop) { |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 519 | void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW); |
| 520 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 521 | void* f = dlsym(handle, "dlopen_test_loopy_function"); |
| 522 | ASSERT_TRUE(f != nullptr) << dlerror(); |
| 523 | EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)()); |
| 524 | ASSERT_EQ(0, dlclose(handle)); |
Dmitriy Ivanov | a6ac54a | 2014-09-09 10:21:42 -0700 | [diff] [blame] | 525 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 526 | // dlopen second time to make sure that the library was unloaded correctly |
| 527 | handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD); |
| 528 | ASSERT_TRUE(handle == nullptr); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 529 | #ifdef __BIONIC__ |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 530 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? |
| 531 | ASSERT_STREQ("dlopen failed: library \"libtest_with_dependency_loop.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
Dmitriy Ivanov | eb27bba | 2014-09-15 14:13:24 -0700 | [diff] [blame] | 532 | #endif |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 533 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 534 | handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD); |
| 535 | ASSERT_TRUE(handle == nullptr); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 538 | TEST(dlfcn, dlopen_nodelete) { |
| 539 | static bool is_unloaded = false; |
| 540 | |
| 541 | void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE); |
| 542 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 543 | void (*set_unload_flag_ptr)(bool*); |
| 544 | set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr")); |
| 545 | ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror(); |
| 546 | set_unload_flag_ptr(&is_unloaded); |
| 547 | |
| 548 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number")); |
| 549 | ASSERT_TRUE(taxicab_number != nullptr) << dlerror(); |
| 550 | ASSERT_EQ(1729U, *taxicab_number); |
| 551 | *taxicab_number = 2; |
| 552 | |
| 553 | dlclose(handle); |
| 554 | ASSERT_TRUE(!is_unloaded); |
| 555 | |
| 556 | uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number")); |
| 557 | ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number); |
| 558 | ASSERT_EQ(2U, *taxicab_number_after_dlclose); |
| 559 | |
| 560 | |
| 561 | handle = dlopen("libtest_nodelete_1.so", RTLD_NOW); |
| 562 | uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number")); |
| 563 | ASSERT_EQ(taxicab_number2, taxicab_number); |
| 564 | |
| 565 | ASSERT_EQ(2U, *taxicab_number2); |
| 566 | |
| 567 | dlclose(handle); |
| 568 | ASSERT_TRUE(!is_unloaded); |
| 569 | } |
| 570 | |
| 571 | TEST(dlfcn, dlopen_nodelete_on_second_dlopen) { |
| 572 | static bool is_unloaded = false; |
| 573 | |
| 574 | void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW); |
| 575 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 576 | void (*set_unload_flag_ptr)(bool*); |
| 577 | set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr")); |
| 578 | ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror(); |
| 579 | set_unload_flag_ptr(&is_unloaded); |
| 580 | |
| 581 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number")); |
| 582 | ASSERT_TRUE(taxicab_number != nullptr) << dlerror(); |
| 583 | |
| 584 | ASSERT_EQ(1729U, *taxicab_number); |
| 585 | *taxicab_number = 2; |
| 586 | |
| 587 | // This RTLD_NODELETE should be ignored |
| 588 | void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE); |
| 589 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 590 | ASSERT_EQ(handle, handle1); |
| 591 | |
| 592 | dlclose(handle1); |
| 593 | dlclose(handle); |
| 594 | |
| 595 | ASSERT_TRUE(is_unloaded); |
| 596 | } |
| 597 | |
| 598 | TEST(dlfcn, dlopen_nodelete_dt_flags_1) { |
| 599 | static bool is_unloaded = false; |
| 600 | |
| 601 | void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW); |
| 602 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 603 | void (*set_unload_flag_ptr)(bool*); |
| 604 | set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr")); |
| 605 | ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror(); |
| 606 | set_unload_flag_ptr(&is_unloaded); |
| 607 | |
| 608 | dlclose(handle); |
| 609 | ASSERT_TRUE(!is_unloaded); |
| 610 | } |
| 611 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 612 | TEST(dlfcn, dlsym_df_1_global) { |
Dmitriy Ivanov | 4e446b1 | 2014-10-31 17:27:02 -0700 | [diff] [blame] | 613 | #if !defined(__arm__) && !defined(__aarch64__) |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 614 | void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW); |
| 615 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 616 | int (*get_answer)(); |
| 617 | get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer")); |
| 618 | ASSERT_TRUE(get_answer != nullptr) << dlerror(); |
| 619 | ASSERT_EQ(42, get_answer()); |
| 620 | ASSERT_EQ(0, dlclose(handle)); |
| 621 | #else |
Dmitriy Ivanov | 4e446b1 | 2014-10-31 17:27:02 -0700 | [diff] [blame] | 622 | GTEST_LOG_(INFO) << "This test does nothing on arm/arm64 (to be reenabled once b/18137520 or b/18130452 are fixed).\n"; |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 623 | #endif |
| 624 | } |
| 625 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 626 | TEST(dlfcn, dlopen_failure) { |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 627 | void* self = dlopen("/does/not/exist", RTLD_NOW); |
| 628 | ASSERT_TRUE(self == NULL); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 629 | #if defined(__BIONIC__) |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 630 | ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror()); |
| 631 | #else |
| 632 | ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror()); |
| 633 | #endif |
| 634 | } |
| 635 | |
Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 636 | static void* ConcurrentDlErrorFn(void*) { |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 637 | dlopen("/child/thread", RTLD_NOW); |
| 638 | return reinterpret_cast<void*>(strdup(dlerror())); |
| 639 | } |
| 640 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 641 | TEST(dlfcn, dlerror_concurrent) { |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 642 | dlopen("/main/thread", RTLD_NOW); |
| 643 | const char* main_thread_error = dlerror(); |
| 644 | ASSERT_SUBSTR("/main/thread", main_thread_error); |
| 645 | |
| 646 | pthread_t t; |
| 647 | ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL)); |
| 648 | void* result; |
| 649 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 650 | char* child_thread_error = static_cast<char*>(result); |
| 651 | ASSERT_SUBSTR("/child/thread", child_thread_error); |
| 652 | free(child_thread_error); |
| 653 | |
| 654 | ASSERT_SUBSTR("/main/thread", main_thread_error); |
| 655 | } |
| 656 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 657 | TEST(dlfcn, dlsym_failures) { |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 658 | dlerror(); // Clear any pending errors. |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 659 | void* self = dlopen(NULL, RTLD_NOW); |
| 660 | ASSERT_TRUE(self != NULL); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 661 | ASSERT_TRUE(dlerror() == NULL); |
| 662 | |
| 663 | void* sym; |
| 664 | |
Dmitriy Ivanov | 44adf93 | 2014-05-22 09:49:24 -0700 | [diff] [blame] | 665 | #if defined(__BIONIC__) && !defined(__LP64__) |
| 666 | // RTLD_DEFAULT in lp32 bionic is not (void*)0 |
| 667 | // so it can be distinguished from the NULL handle. |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 668 | sym = dlsym(NULL, "test"); |
| 669 | ASSERT_TRUE(sym == NULL); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 670 | ASSERT_SUBSTR("dlsym library handle is null", dlerror()); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 671 | #endif |
| 672 | |
| 673 | // NULL symbol name. |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 674 | #if defined(__BIONIC__) |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 675 | // glibc marks this parameter non-null and SEGVs if you cheat. |
| 676 | sym = dlsym(self, NULL); |
| 677 | ASSERT_TRUE(sym == NULL); |
| 678 | ASSERT_SUBSTR("", dlerror()); |
| 679 | #endif |
| 680 | |
| 681 | // Symbol that doesn't exist. |
| 682 | sym = dlsym(self, "ThisSymbolDoesNotExist"); |
| 683 | ASSERT_TRUE(sym == NULL); |
| 684 | ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror()); |
Elliott Hughes | 1a69616 | 2012-11-01 13:49:32 -0700 | [diff] [blame] | 685 | |
| 686 | ASSERT_EQ(0, dlclose(self)); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 689 | TEST(dlfcn, dladdr) { |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 690 | dlerror(); // Clear any pending errors. |
| 691 | void* self = dlopen(NULL, RTLD_NOW); |
| 692 | ASSERT_TRUE(self != NULL); |
| 693 | ASSERT_TRUE(dlerror() == NULL); |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 694 | |
| 695 | void* sym = dlsym(self, "DlSymTestFunction"); |
| 696 | ASSERT_TRUE(sym != NULL); |
| 697 | |
| 698 | // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address. |
| 699 | void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2); |
| 700 | |
| 701 | Dl_info info; |
| 702 | int rc = dladdr(addr, &info); |
| 703 | ASSERT_NE(rc, 0); // Zero on error, non-zero on success. |
| 704 | |
| 705 | // Get the name of this executable. |
| 706 | char executable_path[PATH_MAX]; |
| 707 | rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path)); |
| 708 | ASSERT_NE(rc, -1); |
| 709 | executable_path[rc] = '\0'; |
| 710 | std::string executable_name(basename(executable_path)); |
| 711 | |
| 712 | // The filename should be that of this executable. |
| 713 | // Note that we don't know whether or not we have the full path, so we want an "ends_with" test. |
| 714 | std::string dli_fname(info.dli_fname); |
| 715 | dli_fname = basename(&dli_fname[0]); |
| 716 | ASSERT_EQ(dli_fname, executable_name); |
| 717 | |
| 718 | // The symbol name should be the symbol we looked up. |
| 719 | ASSERT_STREQ(info.dli_sname, "DlSymTestFunction"); |
| 720 | |
| 721 | // The address should be the exact address of the symbol. |
| 722 | ASSERT_EQ(info.dli_saddr, sym); |
| 723 | |
| 724 | // Look in /proc/pid/maps to find out what address we were loaded at. |
| 725 | // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic. |
| 726 | void* base_address = NULL; |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 727 | char line[BUFSIZ]; |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 728 | FILE* fp = fopen("/proc/self/maps", "r"); |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 729 | ASSERT_TRUE(fp != NULL); |
| 730 | while (fgets(line, sizeof(line), fp) != NULL) { |
| 731 | uintptr_t start = strtoul(line, 0, 16); |
| 732 | line[strlen(line) - 1] = '\0'; // Chomp the '\n'. |
| 733 | char* path = strchr(line, '/'); |
Elliott Hughes | 156da96 | 2012-10-09 17:14:56 -0700 | [diff] [blame] | 734 | if (path != NULL && strcmp(executable_path, path) == 0) { |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 735 | base_address = reinterpret_cast<void*>(start); |
| 736 | break; |
| 737 | } |
| 738 | } |
| 739 | fclose(fp); |
| 740 | |
| 741 | // The base address should be the address we were loaded at. |
| 742 | ASSERT_EQ(info.dli_fbase, base_address); |
Elliott Hughes | 1a69616 | 2012-11-01 13:49:32 -0700 | [diff] [blame] | 743 | |
| 744 | ASSERT_EQ(0, dlclose(self)); |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 747 | TEST(dlfcn, dladdr_invalid) { |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 748 | Dl_info info; |
| 749 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 750 | dlerror(); // Clear any pending errors. |
| 751 | |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 752 | // No symbol corresponding to NULL. |
| 753 | ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success. |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 754 | ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3). |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 755 | |
| 756 | // No symbol corresponding to a stack address. |
| 757 | ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success. |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 758 | ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3). |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 759 | } |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 760 | |
Elliott Hughes | a43e906 | 2013-01-07 14:18:22 -0800 | [diff] [blame] | 761 | // GNU-style ELF hash tables are incompatible with the MIPS ABI. |
| 762 | // MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code. |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 763 | TEST(dlfcn, dlopen_library_with_only_gnu_hash) { |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 764 | #if !defined(__mips__) |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 765 | dlerror(); // Clear any pending errors. |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 766 | void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW); |
| 767 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 768 | auto guard = make_scope_guard([&]() { |
| 769 | dlclose(handle); |
| 770 | }); |
| 771 | void* sym = dlsym(handle, "getRandomNumber"); |
| 772 | ASSERT_TRUE(sym != nullptr) << dlerror(); |
| 773 | int (*fn)(void); |
| 774 | fn = reinterpret_cast<int (*)(void)>(sym); |
| 775 | EXPECT_EQ(4, fn()); |
| 776 | |
| 777 | Dl_info dlinfo; |
| 778 | ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo)); |
| 779 | |
| 780 | ASSERT_TRUE(fn == dlinfo.dli_saddr); |
| 781 | ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname); |
Dmitriy Ivanov | b335677 | 2014-11-14 11:19:22 -0800 | [diff] [blame] | 782 | ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname); |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 783 | #else |
| 784 | GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n"; |
| 785 | #endif |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 786 | } |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 787 | |
Dmitriy Ivanov | b335677 | 2014-11-14 11:19:22 -0800 | [diff] [blame] | 788 | TEST(dlfcn, dlopen_library_with_only_sysv_hash) { |
| 789 | void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW); |
| 790 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 791 | auto guard = make_scope_guard([&]() { |
| 792 | dlclose(handle); |
| 793 | }); |
| 794 | void* sym = dlsym(handle, "getRandomNumber"); |
| 795 | ASSERT_TRUE(sym != nullptr) << dlerror(); |
| 796 | int (*fn)(void); |
| 797 | fn = reinterpret_cast<int (*)(void)>(sym); |
| 798 | EXPECT_EQ(4, fn()); |
| 799 | |
| 800 | Dl_info dlinfo; |
| 801 | ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo)); |
| 802 | |
| 803 | ASSERT_TRUE(fn == dlinfo.dli_saddr); |
| 804 | ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname); |
| 805 | ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname); |
| 806 | } |
| 807 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 808 | TEST(dlfcn, dlopen_bad_flags) { |
| 809 | dlerror(); // Clear any pending errors. |
| 810 | void* handle; |
| 811 | |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 812 | #if defined(__GLIBC__) |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 813 | // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags. |
| 814 | handle = dlopen(NULL, 0); |
| 815 | ASSERT_TRUE(handle == NULL); |
| 816 | ASSERT_SUBSTR("invalid", dlerror()); |
| 817 | #endif |
| 818 | |
| 819 | handle = dlopen(NULL, 0xffffffff); |
| 820 | ASSERT_TRUE(handle == NULL); |
| 821 | ASSERT_SUBSTR("invalid", dlerror()); |
| 822 | |
| 823 | // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we. |
| 824 | handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY); |
| 825 | ASSERT_TRUE(handle != NULL); |
| 826 | ASSERT_SUBSTR(NULL, dlerror()); |
| 827 | } |
Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 828 | |
| 829 | TEST(dlfcn, rtld_default_unknown_symbol) { |
Elliott Hughes | 2ed7109 | 2013-11-11 15:48:06 -0800 | [diff] [blame] | 830 | void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME"); |
Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 831 | ASSERT_TRUE(addr == NULL); |
| 832 | } |
| 833 | |
| 834 | TEST(dlfcn, rtld_default_known_symbol) { |
Elliott Hughes | 2ed7109 | 2013-11-11 15:48:06 -0800 | [diff] [blame] | 835 | void* addr = dlsym(RTLD_DEFAULT, "fopen"); |
| 836 | ASSERT_TRUE(addr != NULL); |
| 837 | } |
| 838 | |
| 839 | TEST(dlfcn, rtld_next_unknown_symbol) { |
| 840 | void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME"); |
| 841 | ASSERT_TRUE(addr == NULL); |
| 842 | } |
| 843 | |
| 844 | TEST(dlfcn, rtld_next_known_symbol) { |
| 845 | void* addr = dlsym(RTLD_NEXT, "fopen"); |
Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 846 | ASSERT_TRUE(addr != NULL); |
| 847 | } |
Dmitriy Ivanov | 7db1809 | 2014-05-08 12:27:25 -0700 | [diff] [blame] | 848 | |
Dmitriy Ivanov | ce44166 | 2014-06-17 15:56:38 -0700 | [diff] [blame] | 849 | TEST(dlfcn, dlsym_weak_func) { |
| 850 | dlerror(); |
Dmitriy Ivanov | bfa88bc | 2014-12-16 11:40:46 -0800 | [diff] [blame] | 851 | void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW); |
Dmitriy Ivanov | ce44166 | 2014-06-17 15:56:38 -0700 | [diff] [blame] | 852 | ASSERT_TRUE(handle != NULL); |
| 853 | |
| 854 | int (*weak_func)(); |
| 855 | weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func")); |
| 856 | ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror(); |
| 857 | EXPECT_EQ(42, weak_func()); |
| 858 | dlclose(handle); |
| 859 | } |
| 860 | |
Dmitriy Ivanov | bfa88bc | 2014-12-16 11:40:46 -0800 | [diff] [blame] | 861 | TEST(dlfcn, dlopen_undefined_weak_func) { |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 862 | void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW); |
| 863 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 864 | int (*weak_func)(); |
| 865 | weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func")); |
| 866 | ASSERT_TRUE(weak_func != nullptr) << dlerror(); |
| 867 | EXPECT_EQ(6551, weak_func()); |
| 868 | dlclose(handle); |
Dmitriy Ivanov | bfa88bc | 2014-12-16 11:40:46 -0800 | [diff] [blame] | 869 | } |
| 870 | |
Dmitriy Ivanov | 7db1809 | 2014-05-08 12:27:25 -0700 | [diff] [blame] | 871 | TEST(dlfcn, dlopen_symlink) { |
| 872 | void* handle1 = dlopen("libdlext_test.so", RTLD_NOW); |
| 873 | void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW); |
| 874 | ASSERT_TRUE(handle1 != NULL); |
| 875 | ASSERT_TRUE(handle2 != NULL); |
| 876 | ASSERT_EQ(handle1, handle2); |
Dmitriy Ivanov | 319356e | 2014-09-02 17:31:44 -0700 | [diff] [blame] | 877 | dlclose(handle1); |
| 878 | dlclose(handle2); |
Dmitriy Ivanov | 7db1809 | 2014-05-08 12:27:25 -0700 | [diff] [blame] | 879 | } |
Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 880 | |
Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 881 | TEST(dlfcn, dlopen_from_zip_absolute_path) { |
| 882 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH; |
| 883 | |
| 884 | void* handle = dlopen((lib_path + "!libdir/libdlext_test_fd.so").c_str(), RTLD_NOW); |
| 885 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 886 | |
| 887 | int (*fn)(void); |
| 888 | fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber")); |
| 889 | ASSERT_TRUE(fn != nullptr); |
| 890 | EXPECT_EQ(4, fn()); |
| 891 | |
| 892 | dlclose(handle); |
| 893 | } |
| 894 | |
| 895 | TEST(dlfcn, dlopen_from_zip_ld_library_path) { |
| 896 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH + "!libdir"; |
| 897 | |
| 898 | typedef void (*fn_t)(const char*); |
| 899 | fn_t android_update_LD_LIBRARY_PATH = |
| 900 | reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH")); |
| 901 | |
| 902 | ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror(); |
| 903 | |
| 904 | void* handle = dlopen("libdlext_test_fd.so", RTLD_NOW); |
| 905 | ASSERT_TRUE(handle == nullptr); |
| 906 | |
| 907 | android_update_LD_LIBRARY_PATH(lib_path.c_str()); |
| 908 | |
| 909 | handle = dlopen("libdlext_test_fd.so", RTLD_NOW); |
| 910 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 911 | |
| 912 | int (*fn)(void); |
| 913 | fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber")); |
| 914 | ASSERT_TRUE(fn != nullptr); |
| 915 | EXPECT_EQ(4, fn()); |
| 916 | |
| 917 | dlclose(handle); |
| 918 | } |
| 919 | |
| 920 | |
Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 921 | // libtest_dlopen_from_ctor_main.so depends on |
| 922 | // libtest_dlopen_from_ctor.so which has a constructor |
| 923 | // that calls dlopen(libc...). This is to test the situation |
| 924 | // described in b/7941716. |
| 925 | TEST(dlfcn, dlopen_dlopen_from_ctor) { |
| 926 | #if defined(__BIONIC__) |
| 927 | void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW); |
| 928 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 929 | dlclose(handle); |
| 930 | #else |
| 931 | GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n"; |
| 932 | #endif |
| 933 | } |