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