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