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