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 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 32 | static bool g_called = false; |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 33 | extern "C" void DlSymTestFunction() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 34 | g_called = true; |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Dmitriy Ivanov | f8846a4 | 2014-07-08 21:21:34 -0700 | [diff] [blame] | 37 | static int g_ctor_function_called = 0; |
| 38 | |
| 39 | extern "C" void ctor_function() __attribute__ ((constructor)); |
| 40 | |
| 41 | extern "C" void ctor_function() { |
| 42 | g_ctor_function_called = 17; |
| 43 | } |
| 44 | |
| 45 | TEST(dlfcn, ctor_function_call) { |
| 46 | ASSERT_EQ(17, g_ctor_function_called); |
| 47 | } |
| 48 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 49 | TEST(dlfcn, dlsym_in_self) { |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 50 | dlerror(); // Clear any pending errors. |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 51 | void* self = dlopen(NULL, RTLD_NOW); |
| 52 | ASSERT_TRUE(self != NULL); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 53 | ASSERT_TRUE(dlerror() == NULL); |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 54 | |
| 55 | void* sym = dlsym(self, "DlSymTestFunction"); |
| 56 | ASSERT_TRUE(sym != NULL); |
| 57 | |
| 58 | void (*function)() = reinterpret_cast<void(*)()>(sym); |
| 59 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 60 | g_called = false; |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 61 | function(); |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 62 | ASSERT_TRUE(g_called); |
Elliott Hughes | 1a69616 | 2012-11-01 13:49:32 -0700 | [diff] [blame] | 63 | |
| 64 | ASSERT_EQ(0, dlclose(self)); |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 65 | } |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 66 | |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 67 | TEST(dlfcn, dlsym_with_dependencies) { |
| 68 | void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW); |
| 69 | ASSERT_TRUE(handle != NULL); |
| 70 | dlerror(); |
| 71 | // This symbol is in DT_NEEDED library. |
| 72 | void* sym = dlsym(handle, "getRandomNumber"); |
| 73 | ASSERT_TRUE(sym != NULL); |
| 74 | int (*fn)(void); |
| 75 | fn = reinterpret_cast<int (*)(void)>(sym); |
| 76 | EXPECT_EQ(4, fn()); |
| 77 | dlclose(handle); |
| 78 | } |
| 79 | |
Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 80 | TEST(dlfcn, dlopen_noload) { |
| 81 | void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD); |
| 82 | ASSERT_TRUE(handle == NULL); |
| 83 | handle = dlopen("libtest_simple.so", RTLD_NOW); |
| 84 | void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD); |
| 85 | ASSERT_TRUE(handle != NULL); |
| 86 | ASSERT_TRUE(handle2 != NULL); |
| 87 | ASSERT_TRUE(handle == handle2); |
| 88 | ASSERT_EQ(0, dlclose(handle)); |
| 89 | ASSERT_EQ(0, dlclose(handle2)); |
| 90 | } |
| 91 | |
Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 92 | // ifuncs are only supported on intel and arm64 for now |
| 93 | #if defined (__aarch64__) || defined(__i386__) || defined(__x86_64__) |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 94 | TEST(dlfcn, ifunc) { |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 95 | typedef const char* (*fn_ptr)(); |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 96 | |
| 97 | // ifunc's choice depends on whether IFUNC_CHOICE has a value |
| 98 | // first check the set case |
| 99 | setenv("IFUNC_CHOICE", "set", 1); |
| 100 | void* handle = dlopen("libtest_ifunc.so", RTLD_NOW); |
| 101 | ASSERT_TRUE(handle != NULL); |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 102 | fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo")); |
| 103 | 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] | 104 | ASSERT_TRUE(foo_ptr != NULL); |
| 105 | ASSERT_TRUE(foo_library_ptr != NULL); |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 106 | ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0); |
| 107 | ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0); |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 108 | dlclose(handle); |
| 109 | |
| 110 | // then check the unset case |
| 111 | unsetenv("IFUNC_CHOICE"); |
| 112 | handle = dlopen("libtest_ifunc.so", RTLD_NOW); |
| 113 | ASSERT_TRUE(handle != NULL); |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 114 | foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo")); |
| 115 | foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library")); |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 116 | ASSERT_TRUE(foo_ptr != NULL); |
| 117 | ASSERT_TRUE(foo_library_ptr != NULL); |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 118 | ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0); |
| 119 | ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0); |
| 120 | dlclose(handle); |
| 121 | } |
| 122 | |
| 123 | TEST(dlfcn, ifunc_ctor_call) { |
| 124 | typedef const char* (*fn_ptr)(); |
| 125 | |
| 126 | void* handle = dlopen("libtest_ifunc.so", RTLD_NOW); |
Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 127 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 128 | fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative")); |
| 129 | ASSERT_TRUE(is_ctor_called != nullptr) << dlerror(); |
| 130 | ASSERT_STREQ("false", is_ctor_called()); |
| 131 | |
| 132 | is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot")); |
| 133 | ASSERT_TRUE(is_ctor_called != nullptr) << dlerror(); |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 134 | ASSERT_STREQ("true", is_ctor_called()); |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 135 | dlclose(handle); |
| 136 | } |
| 137 | #endif |
| 138 | |
Dmitriy Ivanov | b2a30ee | 2014-09-04 18:23:00 -0700 | [diff] [blame] | 139 | TEST(dlfcn, dlopen_check_relocation_dt_needed_order) { |
| 140 | // This is the structure of the test library and |
| 141 | // its dt_needed libraries |
| 142 | // libtest_relo_check_dt_needed_order.so |
| 143 | // | |
| 144 | // +-> libtest_relo_check_dt_needed_order_1.so |
| 145 | // | |
| 146 | // +-> libtest_relo_check_dt_needed_order_2.so |
| 147 | // |
| 148 | // The root library references relo_test_get_answer_lib - which is defined |
| 149 | // in both dt_needed libraries, the correct relocation should |
| 150 | // use the function defined in libtest_relo_check_dt_needed_order_1.so |
| 151 | void* handle = nullptr; |
Dmitriy Ivanov | d9ff722 | 2014-09-08 16:22:22 -0700 | [diff] [blame] | 152 | auto guard = make_scope_guard([&]() { |
Dmitriy Ivanov | b2a30ee | 2014-09-04 18:23:00 -0700 | [diff] [blame] | 153 | dlclose(handle); |
| 154 | }); |
| 155 | |
| 156 | handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW); |
| 157 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 158 | |
| 159 | typedef int (*fn_t) (void); |
| 160 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer")); |
| 161 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 162 | ASSERT_EQ(1, fn()); |
| 163 | } |
| 164 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame^] | 165 | TEST(dlfcn, dlopen_check_order_dlsym) { |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 166 | // Here is how the test library and its dt_needed |
| 167 | // libraries are arranged |
| 168 | // |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame^] | 169 | // libtest_check_order_children.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 170 | // | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame^] | 171 | // +-> ..._1_left.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 172 | // | | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame^] | 173 | // | +-> ..._a.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 174 | // | | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame^] | 175 | // | +-> ...r_b.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 176 | // | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame^] | 177 | // +-> ..._2_right.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 178 | // | | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame^] | 179 | // | +-> ..._d.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 180 | // | | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame^] | 181 | // | +-> ..._b.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 182 | // | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame^] | 183 | // +-> ..._3_c.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 184 | // |
| 185 | // load order should be (1, 2, 3, a, b, d) |
| 186 | // |
| 187 | // get_answer() is defined in (2, 3, a, b, c) |
| 188 | // get_answer2() is defined in (b, d) |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame^] | 189 | void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 190 | ASSERT_TRUE(sym == nullptr); |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame^] | 191 | void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL); |
| 192 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 193 | typedef int (*fn_t) (void); |
| 194 | fn_t fn, fn2; |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame^] | 195 | 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] | 196 | ASSERT_TRUE(fn != NULL) << dlerror(); |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame^] | 197 | 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] | 198 | ASSERT_TRUE(fn2 != NULL) << dlerror(); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 199 | |
| 200 | ASSERT_EQ(42, fn()); |
| 201 | ASSERT_EQ(43, fn2()); |
| 202 | dlclose(handle); |
| 203 | } |
| 204 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame^] | 205 | TEST(dlfcn, dlopen_check_order_reloc_siblings) { |
| 206 | // This is how this one works: |
| 207 | // we lookup and call get_answer which is defined in '_2.so' |
| 208 | // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so' |
| 209 | // the correct _impl() is implemented by '_a.so'; |
| 210 | // |
| 211 | // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?) |
| 212 | // |
| 213 | // Here is the picture: |
| 214 | // |
| 215 | // libtest_check_order_reloc_siblings.so |
| 216 | // | |
| 217 | // +-> ..._1.so <- empty |
| 218 | // | | |
| 219 | // | +-> ..._a.so <- exports correct answer_impl() |
| 220 | // | | |
| 221 | // | +-> ..._b.so <- every other letter exporting incorrect one. |
| 222 | // | |
| 223 | // +-> ..._2.so <- empty |
| 224 | // | | |
| 225 | // | +-> ..._c.so |
| 226 | // | | |
| 227 | // | +-> ..._d.so |
| 228 | // | |
| 229 | // +-> ..._3.so <- empty |
| 230 | // | |
| 231 | // +-> ..._e.so |
| 232 | // | |
| 233 | // +-> ..._f.so <- exports get_answer() that calls get_anser_impl(); |
| 234 | // implements incorrect get_answer_impl() |
| 235 | |
| 236 | void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD); |
| 237 | ASSERT_TRUE(handle == nullptr); |
| 238 | #ifdef __BIONIC__ |
| 239 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? |
| 240 | ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
| 241 | #endif |
| 242 | |
| 243 | handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL); |
| 244 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 245 | |
| 246 | typedef int (*fn_t) (void); |
| 247 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer")); |
| 248 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 249 | ASSERT_EQ(42, fn()); |
| 250 | |
| 251 | ASSERT_EQ(0, dlclose(handle)); |
| 252 | } |
| 253 | |
| 254 | TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) { |
| 255 | // This test uses the same library as dlopen_check_order_reloc_siblings. |
| 256 | // Unlike dlopen_check_order_reloc_siblings it preloads |
| 257 | // libtest_check_order_reloc_siblings_1.so (first dependency) prior to |
| 258 | // dlopen(libtest_check_order_reloc_siblings.so) |
| 259 | |
| 260 | void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD); |
| 261 | ASSERT_TRUE(handle == nullptr); |
| 262 | handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD); |
| 263 | ASSERT_TRUE(handle == nullptr); |
| 264 | |
| 265 | void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL); |
| 266 | ASSERT_TRUE(handle_for_1 != nullptr) << dlerror(); |
| 267 | |
| 268 | handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL); |
| 269 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 270 | |
| 271 | ASSERT_EQ(0, dlclose(handle_for_1)); |
| 272 | |
| 273 | typedef int (*fn_t) (void); |
| 274 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer")); |
| 275 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 276 | ASSERT_EQ(42, fn()); |
| 277 | |
| 278 | ASSERT_EQ(0, dlclose(handle)); |
| 279 | } |
| 280 | |
| 281 | TEST(dlfcn, dlopen_check_order_reloc_nephew) { |
| 282 | // This is how this one works: |
| 283 | // we lookup and call nephew_get_answer which is defined in '_2.so' |
| 284 | // and in turn calls external get_answer_impl() defined in '_[a-f].so' |
| 285 | // the correct _impl() is implemented by '_a.so'; |
| 286 | // |
| 287 | // Here is the picture: |
| 288 | // |
| 289 | // libtest_check_order_reloc_siblings.so |
| 290 | // | |
| 291 | // +-> ..._1.so <- empty |
| 292 | // | | |
| 293 | // | +-> ..._a.so <- exports correct answer_impl() |
| 294 | // | | |
| 295 | // | +-> ..._b.so <- every other letter exporting incorrect one. |
| 296 | // | |
| 297 | // +-> ..._2.so <- empty |
| 298 | // | | |
| 299 | // | +-> ..._c.so |
| 300 | // | | |
| 301 | // | +-> ..._d.so |
| 302 | // | |
| 303 | // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl(); |
| 304 | // | |
| 305 | // +-> ..._e.so |
| 306 | // | |
| 307 | // +-> ..._f.so |
| 308 | |
| 309 | void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD); |
| 310 | ASSERT_TRUE(handle == nullptr); |
| 311 | #ifdef __BIONIC__ |
| 312 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? |
| 313 | ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
| 314 | #endif |
| 315 | |
| 316 | handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL); |
| 317 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 318 | |
| 319 | typedef int (*fn_t) (void); |
| 320 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer")); |
| 321 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 322 | ASSERT_EQ(42, fn()); |
| 323 | |
| 324 | ASSERT_EQ(0, dlclose(handle)); |
| 325 | } |
| 326 | |
| 327 | extern "C" int check_order_reloc_root_get_answer_impl() { |
| 328 | return 42; |
| 329 | } |
| 330 | |
| 331 | TEST(dlfcn, dlopen_check_order_reloc_main_executable) { |
| 332 | // This is how this one works: |
| 333 | // we lookup and call get_answer3 which is defined in 'root.so' |
| 334 | // and in turn calls external root_get_answer_impl() defined in _2.so and |
| 335 | // above the correct _impl() is one in the executable. |
| 336 | // |
| 337 | // libtest_check_order_reloc_root.so |
| 338 | // | |
| 339 | // +-> ..._1.so <- empty |
| 340 | // | |
| 341 | // +-> ..._2.so <- gives incorrect answer for answer_main_impl() |
| 342 | // |
| 343 | |
| 344 | void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD); |
| 345 | ASSERT_TRUE(handle == nullptr); |
| 346 | #ifdef __BIONIC__ |
| 347 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? |
| 348 | ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
| 349 | #endif |
| 350 | |
| 351 | handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL); |
| 352 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 353 | |
| 354 | typedef int (*fn_t) (void); |
| 355 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer")); |
| 356 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 357 | ASSERT_EQ(42, fn()); |
| 358 | |
| 359 | ASSERT_EQ(0, dlclose(handle)); |
| 360 | } |
| 361 | |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 362 | TEST(dlfcn, dlopen_check_rtld_local) { |
| 363 | void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 364 | ASSERT_TRUE(sym == nullptr); |
| 365 | |
| 366 | // implicit RTLD_LOCAL |
| 367 | void* handle = dlopen("libtest_simple.so", RTLD_NOW); |
| 368 | sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 369 | ASSERT_TRUE(sym == nullptr); |
| 370 | ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror()); |
| 371 | sym = dlsym(handle, "dlopen_testlib_simple_func"); |
| 372 | ASSERT_TRUE(sym != nullptr); |
| 373 | ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)()); |
| 374 | dlclose(handle); |
| 375 | |
| 376 | // explicit RTLD_LOCAL |
| 377 | handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL); |
| 378 | sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 379 | ASSERT_TRUE(sym == nullptr); |
| 380 | ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror()); |
| 381 | sym = dlsym(handle, "dlopen_testlib_simple_func"); |
| 382 | ASSERT_TRUE(sym != nullptr); |
| 383 | ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)()); |
| 384 | dlclose(handle); |
| 385 | } |
| 386 | |
| 387 | TEST(dlfcn, dlopen_check_rtld_global) { |
| 388 | void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 389 | ASSERT_TRUE(sym == nullptr); |
| 390 | |
| 391 | void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL); |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 392 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 393 | sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 394 | ASSERT_TRUE(sym != nullptr) << dlerror(); |
| 395 | ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)()); |
| 396 | dlclose(handle); |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 397 | |
| 398 | // RTLD_GLOBAL implies RTLD_NODELETE, let's check that |
| 399 | void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 400 | ASSERT_EQ(sym, sym_after_dlclose); |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 403 | // libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so -> |
| 404 | // libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so -> |
| 405 | // libtest_with_dependency_loop_a.so |
| 406 | TEST(dlfcn, dlopen_check_loop) { |
| 407 | void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW); |
Dmitriy Ivanov | eb27bba | 2014-09-15 14:13:24 -0700 | [diff] [blame] | 408 | #if defined(__BIONIC__) |
Dmitriy Ivanov | a6ac54a | 2014-09-09 10:21:42 -0700 | [diff] [blame] | 409 | ASSERT_TRUE(handle == nullptr); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 410 | ASSERT_STREQ("dlopen failed: recursive link to \"libtest_with_dependency_loop_a.so\"", dlerror()); |
Dmitriy Ivanov | a6ac54a | 2014-09-09 10:21:42 -0700 | [diff] [blame] | 411 | // This symbol should never be exposed |
| 412 | void* f = dlsym(RTLD_DEFAULT, "dlopen_test_invalid_function"); |
| 413 | ASSERT_TRUE(f == nullptr); |
| 414 | ASSERT_SUBSTR("undefined symbol: dlopen_test_invalid_function", dlerror()); |
| 415 | |
| 416 | // dlopen second time to make sure that the library wasn't loaded even though dlopen returned null. |
| 417 | // This may happen if during cleanup the root library or one of the depended libs were not removed |
| 418 | // from soinfo list. |
| 419 | handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD); |
| 420 | ASSERT_TRUE(handle == nullptr); |
| 421 | 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] | 422 | #else // glibc allows recursive links |
| 423 | ASSERT_TRUE(handle != nullptr); |
| 424 | dlclose(handle); |
| 425 | #endif |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 428 | TEST(dlfcn, dlopen_nodelete) { |
| 429 | static bool is_unloaded = false; |
| 430 | |
| 431 | void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE); |
| 432 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 433 | void (*set_unload_flag_ptr)(bool*); |
| 434 | set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr")); |
| 435 | ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror(); |
| 436 | set_unload_flag_ptr(&is_unloaded); |
| 437 | |
| 438 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number")); |
| 439 | ASSERT_TRUE(taxicab_number != nullptr) << dlerror(); |
| 440 | ASSERT_EQ(1729U, *taxicab_number); |
| 441 | *taxicab_number = 2; |
| 442 | |
| 443 | dlclose(handle); |
| 444 | ASSERT_TRUE(!is_unloaded); |
| 445 | |
| 446 | uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number")); |
| 447 | ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number); |
| 448 | ASSERT_EQ(2U, *taxicab_number_after_dlclose); |
| 449 | |
| 450 | |
| 451 | handle = dlopen("libtest_nodelete_1.so", RTLD_NOW); |
| 452 | uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number")); |
| 453 | ASSERT_EQ(taxicab_number2, taxicab_number); |
| 454 | |
| 455 | ASSERT_EQ(2U, *taxicab_number2); |
| 456 | |
| 457 | dlclose(handle); |
| 458 | ASSERT_TRUE(!is_unloaded); |
| 459 | } |
| 460 | |
| 461 | TEST(dlfcn, dlopen_nodelete_on_second_dlopen) { |
| 462 | static bool is_unloaded = false; |
| 463 | |
| 464 | void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW); |
| 465 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 466 | void (*set_unload_flag_ptr)(bool*); |
| 467 | set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr")); |
| 468 | ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror(); |
| 469 | set_unload_flag_ptr(&is_unloaded); |
| 470 | |
| 471 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number")); |
| 472 | ASSERT_TRUE(taxicab_number != nullptr) << dlerror(); |
| 473 | |
| 474 | ASSERT_EQ(1729U, *taxicab_number); |
| 475 | *taxicab_number = 2; |
| 476 | |
| 477 | // This RTLD_NODELETE should be ignored |
| 478 | void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE); |
| 479 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 480 | ASSERT_EQ(handle, handle1); |
| 481 | |
| 482 | dlclose(handle1); |
| 483 | dlclose(handle); |
| 484 | |
| 485 | ASSERT_TRUE(is_unloaded); |
| 486 | } |
| 487 | |
| 488 | TEST(dlfcn, dlopen_nodelete_dt_flags_1) { |
| 489 | static bool is_unloaded = false; |
| 490 | |
| 491 | void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW); |
| 492 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 493 | void (*set_unload_flag_ptr)(bool*); |
| 494 | set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr")); |
| 495 | ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror(); |
| 496 | set_unload_flag_ptr(&is_unloaded); |
| 497 | |
| 498 | dlclose(handle); |
| 499 | ASSERT_TRUE(!is_unloaded); |
| 500 | } |
| 501 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 502 | TEST(dlfcn, dlopen_failure) { |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 503 | void* self = dlopen("/does/not/exist", RTLD_NOW); |
| 504 | ASSERT_TRUE(self == NULL); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 505 | #if defined(__BIONIC__) |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 506 | ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror()); |
| 507 | #else |
| 508 | ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror()); |
| 509 | #endif |
| 510 | } |
| 511 | |
Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 512 | static void* ConcurrentDlErrorFn(void*) { |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 513 | dlopen("/child/thread", RTLD_NOW); |
| 514 | return reinterpret_cast<void*>(strdup(dlerror())); |
| 515 | } |
| 516 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 517 | TEST(dlfcn, dlerror_concurrent) { |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 518 | dlopen("/main/thread", RTLD_NOW); |
| 519 | const char* main_thread_error = dlerror(); |
| 520 | ASSERT_SUBSTR("/main/thread", main_thread_error); |
| 521 | |
| 522 | pthread_t t; |
| 523 | ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL)); |
| 524 | void* result; |
| 525 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 526 | char* child_thread_error = static_cast<char*>(result); |
| 527 | ASSERT_SUBSTR("/child/thread", child_thread_error); |
| 528 | free(child_thread_error); |
| 529 | |
| 530 | ASSERT_SUBSTR("/main/thread", main_thread_error); |
| 531 | } |
| 532 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 533 | TEST(dlfcn, dlsym_failures) { |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 534 | dlerror(); // Clear any pending errors. |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 535 | void* self = dlopen(NULL, RTLD_NOW); |
| 536 | ASSERT_TRUE(self != NULL); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 537 | ASSERT_TRUE(dlerror() == NULL); |
| 538 | |
| 539 | void* sym; |
| 540 | |
Dmitriy Ivanov | 44adf93 | 2014-05-22 09:49:24 -0700 | [diff] [blame] | 541 | #if defined(__BIONIC__) && !defined(__LP64__) |
| 542 | // RTLD_DEFAULT in lp32 bionic is not (void*)0 |
| 543 | // so it can be distinguished from the NULL handle. |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 544 | sym = dlsym(NULL, "test"); |
| 545 | ASSERT_TRUE(sym == NULL); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 546 | ASSERT_SUBSTR("dlsym library handle is null", dlerror()); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 547 | #endif |
| 548 | |
| 549 | // NULL symbol name. |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 550 | #if defined(__BIONIC__) |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 551 | // glibc marks this parameter non-null and SEGVs if you cheat. |
| 552 | sym = dlsym(self, NULL); |
| 553 | ASSERT_TRUE(sym == NULL); |
| 554 | ASSERT_SUBSTR("", dlerror()); |
| 555 | #endif |
| 556 | |
| 557 | // Symbol that doesn't exist. |
| 558 | sym = dlsym(self, "ThisSymbolDoesNotExist"); |
| 559 | ASSERT_TRUE(sym == NULL); |
| 560 | ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror()); |
Elliott Hughes | 1a69616 | 2012-11-01 13:49:32 -0700 | [diff] [blame] | 561 | |
| 562 | ASSERT_EQ(0, dlclose(self)); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 565 | TEST(dlfcn, dladdr) { |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 566 | dlerror(); // Clear any pending errors. |
| 567 | void* self = dlopen(NULL, RTLD_NOW); |
| 568 | ASSERT_TRUE(self != NULL); |
| 569 | ASSERT_TRUE(dlerror() == NULL); |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 570 | |
| 571 | void* sym = dlsym(self, "DlSymTestFunction"); |
| 572 | ASSERT_TRUE(sym != NULL); |
| 573 | |
| 574 | // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address. |
| 575 | void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2); |
| 576 | |
| 577 | Dl_info info; |
| 578 | int rc = dladdr(addr, &info); |
| 579 | ASSERT_NE(rc, 0); // Zero on error, non-zero on success. |
| 580 | |
| 581 | // Get the name of this executable. |
| 582 | char executable_path[PATH_MAX]; |
| 583 | rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path)); |
| 584 | ASSERT_NE(rc, -1); |
| 585 | executable_path[rc] = '\0'; |
| 586 | std::string executable_name(basename(executable_path)); |
| 587 | |
| 588 | // The filename should be that of this executable. |
| 589 | // Note that we don't know whether or not we have the full path, so we want an "ends_with" test. |
| 590 | std::string dli_fname(info.dli_fname); |
| 591 | dli_fname = basename(&dli_fname[0]); |
| 592 | ASSERT_EQ(dli_fname, executable_name); |
| 593 | |
| 594 | // The symbol name should be the symbol we looked up. |
| 595 | ASSERT_STREQ(info.dli_sname, "DlSymTestFunction"); |
| 596 | |
| 597 | // The address should be the exact address of the symbol. |
| 598 | ASSERT_EQ(info.dli_saddr, sym); |
| 599 | |
| 600 | // Look in /proc/pid/maps to find out what address we were loaded at. |
| 601 | // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic. |
| 602 | void* base_address = NULL; |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 603 | char line[BUFSIZ]; |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 604 | FILE* fp = fopen("/proc/self/maps", "r"); |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 605 | ASSERT_TRUE(fp != NULL); |
| 606 | while (fgets(line, sizeof(line), fp) != NULL) { |
| 607 | uintptr_t start = strtoul(line, 0, 16); |
| 608 | line[strlen(line) - 1] = '\0'; // Chomp the '\n'. |
| 609 | char* path = strchr(line, '/'); |
Elliott Hughes | 156da96 | 2012-10-09 17:14:56 -0700 | [diff] [blame] | 610 | if (path != NULL && strcmp(executable_path, path) == 0) { |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 611 | base_address = reinterpret_cast<void*>(start); |
| 612 | break; |
| 613 | } |
| 614 | } |
| 615 | fclose(fp); |
| 616 | |
| 617 | // The base address should be the address we were loaded at. |
| 618 | ASSERT_EQ(info.dli_fbase, base_address); |
Elliott Hughes | 1a69616 | 2012-11-01 13:49:32 -0700 | [diff] [blame] | 619 | |
| 620 | ASSERT_EQ(0, dlclose(self)); |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 623 | TEST(dlfcn, dladdr_invalid) { |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 624 | Dl_info info; |
| 625 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 626 | dlerror(); // Clear any pending errors. |
| 627 | |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 628 | // No symbol corresponding to NULL. |
| 629 | 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] | 630 | ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3). |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 631 | |
| 632 | // No symbol corresponding to a stack address. |
| 633 | 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] | 634 | ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3). |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 635 | } |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 636 | |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 637 | // Our dynamic linker doesn't support GNU hash tables. |
Elliott Hughes | a43e906 | 2013-01-07 14:18:22 -0800 | [diff] [blame] | 638 | #if defined(__BIONIC__) |
| 639 | // GNU-style ELF hash tables are incompatible with the MIPS ABI. |
| 640 | // MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code. |
| 641 | #if !defined(__mips__) |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 642 | TEST(dlfcn, dlopen_library_with_only_gnu_hash) { |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 643 | dlerror(); // Clear any pending errors. |
Elliott Hughes | 6e33b02 | 2012-11-07 18:16:02 -0800 | [diff] [blame] | 644 | void* handle = dlopen("no-elf-hash-table-library.so", RTLD_NOW); |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 645 | ASSERT_TRUE(handle == NULL); |
Elliott Hughes | 6e33b02 | 2012-11-07 18:16:02 -0800 | [diff] [blame] | 646 | ASSERT_STREQ("dlopen failed: empty/missing DT_HASH in \"no-elf-hash-table-library.so\" (built with --hash-style=gnu?)", dlerror()); |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 647 | } |
| 648 | #endif |
Elliott Hughes | a43e906 | 2013-01-07 14:18:22 -0800 | [diff] [blame] | 649 | #endif |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 650 | |
| 651 | TEST(dlfcn, dlopen_bad_flags) { |
| 652 | dlerror(); // Clear any pending errors. |
| 653 | void* handle; |
| 654 | |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 655 | #if defined(__GLIBC__) |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 656 | // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags. |
| 657 | handle = dlopen(NULL, 0); |
| 658 | ASSERT_TRUE(handle == NULL); |
| 659 | ASSERT_SUBSTR("invalid", dlerror()); |
| 660 | #endif |
| 661 | |
| 662 | handle = dlopen(NULL, 0xffffffff); |
| 663 | ASSERT_TRUE(handle == NULL); |
| 664 | ASSERT_SUBSTR("invalid", dlerror()); |
| 665 | |
| 666 | // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we. |
| 667 | handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY); |
| 668 | ASSERT_TRUE(handle != NULL); |
| 669 | ASSERT_SUBSTR(NULL, dlerror()); |
| 670 | } |
Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 671 | |
| 672 | TEST(dlfcn, rtld_default_unknown_symbol) { |
Elliott Hughes | 2ed7109 | 2013-11-11 15:48:06 -0800 | [diff] [blame] | 673 | void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME"); |
Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 674 | ASSERT_TRUE(addr == NULL); |
| 675 | } |
| 676 | |
| 677 | TEST(dlfcn, rtld_default_known_symbol) { |
Elliott Hughes | 2ed7109 | 2013-11-11 15:48:06 -0800 | [diff] [blame] | 678 | void* addr = dlsym(RTLD_DEFAULT, "fopen"); |
| 679 | ASSERT_TRUE(addr != NULL); |
| 680 | } |
| 681 | |
| 682 | TEST(dlfcn, rtld_next_unknown_symbol) { |
| 683 | void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME"); |
| 684 | ASSERT_TRUE(addr == NULL); |
| 685 | } |
| 686 | |
| 687 | TEST(dlfcn, rtld_next_known_symbol) { |
| 688 | void* addr = dlsym(RTLD_NEXT, "fopen"); |
Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 689 | ASSERT_TRUE(addr != NULL); |
| 690 | } |
Dmitriy Ivanov | 7db1809 | 2014-05-08 12:27:25 -0700 | [diff] [blame] | 691 | |
Dmitriy Ivanov | ce44166 | 2014-06-17 15:56:38 -0700 | [diff] [blame] | 692 | TEST(dlfcn, dlsym_weak_func) { |
| 693 | dlerror(); |
| 694 | void* handle = dlopen("libtest_dlsym_weak_func.so",RTLD_NOW); |
| 695 | ASSERT_TRUE(handle != NULL); |
| 696 | |
| 697 | int (*weak_func)(); |
| 698 | weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func")); |
| 699 | ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror(); |
| 700 | EXPECT_EQ(42, weak_func()); |
| 701 | dlclose(handle); |
| 702 | } |
| 703 | |
Dmitriy Ivanov | 7db1809 | 2014-05-08 12:27:25 -0700 | [diff] [blame] | 704 | TEST(dlfcn, dlopen_symlink) { |
| 705 | void* handle1 = dlopen("libdlext_test.so", RTLD_NOW); |
| 706 | void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW); |
| 707 | ASSERT_TRUE(handle1 != NULL); |
| 708 | ASSERT_TRUE(handle2 != NULL); |
| 709 | ASSERT_EQ(handle1, handle2); |
Dmitriy Ivanov | 319356e | 2014-09-02 17:31:44 -0700 | [diff] [blame] | 710 | dlclose(handle1); |
| 711 | dlclose(handle2); |
Dmitriy Ivanov | 7db1809 | 2014-05-08 12:27:25 -0700 | [diff] [blame] | 712 | } |