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 <limits.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdint.h> |
Dimitry Ivanov | 708589f | 2016-09-19 10:50:28 -0700 | [diff] [blame] | 23 | #include <string.h> |
dimitry | b9555a9 | 2017-10-11 18:04:05 +0200 | [diff] [blame] | 24 | #if __has_include(<sys/auxv.h>) |
| 25 | #include <sys/auxv.h> |
| 26 | #endif |
dimitry | 109040c | 2017-11-03 13:49:07 +0100 | [diff] [blame] | 27 | #include <sys/user.h> |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 28 | |
| 29 | #include <string> |
Dimitry Ivanov | c7365eb | 2016-11-17 12:38:09 -0800 | [diff] [blame] | 30 | #include <thread> |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 31 | |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 32 | #include <android-base/file.h> |
Tom Cherry | b8ab618 | 2017-04-05 16:20:29 -0700 | [diff] [blame] | 33 | #include <android-base/scopeguard.h> |
| 34 | |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 35 | #include "gtest_globals.h" |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 36 | #include "gtest_utils.h" |
Dimitry Ivanov | 708589f | 2016-09-19 10:50:28 -0700 | [diff] [blame] | 37 | #include "dlfcn_symlink_support.h" |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 38 | #include "utils.h" |
| 39 | |
Dimitry Ivanov | ac4bd2f | 2016-11-21 12:50:38 -0800 | [diff] [blame] | 40 | #if defined(__BIONIC__) && (defined(__arm__) || defined(__i386__)) |
| 41 | #pragma clang diagnostic push |
| 42 | #pragma clang diagnostic ignored "-Wunused-parameter" |
| 43 | |
| 44 | #include <llvm/ADT/StringRef.h> |
| 45 | #include <llvm/Object/Binary.h> |
| 46 | #include <llvm/Object/ELFObjectFile.h> |
| 47 | #include <llvm/Object/ObjectFile.h> |
| 48 | |
| 49 | #pragma clang diagnostic pop |
| 50 | #endif // defined(__ANDROID__) && (defined(__arm__) || defined(__i386__)) |
| 51 | |
Peter Collingbourne | b39cb3c | 2019-03-01 13:12:49 -0800 | [diff] [blame] | 52 | // Declared manually because the macro definitions in <elf.h> conflict with LLVM headers. |
| 53 | #ifdef __arm__ |
| 54 | typedef uintptr_t _Unwind_Ptr; |
| 55 | extern "C" _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr, int*); |
| 56 | #endif |
| 57 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 58 | #define ASSERT_SUBSTR(needle, haystack) \ |
| 59 | ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack) |
| 60 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 61 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 62 | static bool g_called = false; |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 63 | extern "C" void DlSymTestFunction() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 64 | g_called = true; |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Dmitriy Ivanov | f8846a4 | 2014-07-08 21:21:34 -0700 | [diff] [blame] | 67 | static int g_ctor_function_called = 0; |
Dimitry Ivanov | 5543746 | 2016-07-20 15:33:07 -0700 | [diff] [blame] | 68 | static int g_ctor_argc = 0; |
| 69 | static char** g_ctor_argv = reinterpret_cast<char**>(0xDEADBEEF); |
| 70 | static char** g_ctor_envp = g_ctor_envp; |
Dmitriy Ivanov | f8846a4 | 2014-07-08 21:21:34 -0700 | [diff] [blame] | 71 | |
Dimitry Ivanov | 5543746 | 2016-07-20 15:33:07 -0700 | [diff] [blame] | 72 | extern "C" void ctor_function(int argc, char** argv, char** envp) __attribute__ ((constructor)); |
Dmitriy Ivanov | f8846a4 | 2014-07-08 21:21:34 -0700 | [diff] [blame] | 73 | |
Dimitry Ivanov | 5543746 | 2016-07-20 15:33:07 -0700 | [diff] [blame] | 74 | extern "C" void ctor_function(int argc, char** argv, char** envp) { |
Dmitriy Ivanov | f8846a4 | 2014-07-08 21:21:34 -0700 | [diff] [blame] | 75 | g_ctor_function_called = 17; |
Dimitry Ivanov | 5543746 | 2016-07-20 15:33:07 -0700 | [diff] [blame] | 76 | g_ctor_argc = argc; |
| 77 | g_ctor_argv = argv; |
| 78 | g_ctor_envp = envp; |
Dmitriy Ivanov | f8846a4 | 2014-07-08 21:21:34 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | TEST(dlfcn, ctor_function_call) { |
| 82 | ASSERT_EQ(17, g_ctor_function_called); |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 83 | ASSERT_TRUE(g_ctor_argc = GetArgc()); |
| 84 | ASSERT_TRUE(g_ctor_argv = GetArgv()); |
| 85 | ASSERT_TRUE(g_ctor_envp = GetEnvp()); |
Dmitriy Ivanov | f8846a4 | 2014-07-08 21:21:34 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 88 | TEST(dlfcn, dlsym_in_executable) { |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 89 | dlerror(); // Clear any pending errors. |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 90 | void* self = dlopen(nullptr, RTLD_NOW); |
| 91 | ASSERT_TRUE(self != nullptr); |
| 92 | ASSERT_TRUE(dlerror() == nullptr); |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 93 | |
| 94 | void* sym = dlsym(self, "DlSymTestFunction"); |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 95 | ASSERT_TRUE(sym != nullptr); |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 96 | |
| 97 | void (*function)() = reinterpret_cast<void(*)()>(sym); |
| 98 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 99 | g_called = false; |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 100 | function(); |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 101 | ASSERT_TRUE(g_called); |
Elliott Hughes | 1a69616 | 2012-11-01 13:49:32 -0700 | [diff] [blame] | 102 | |
| 103 | ASSERT_EQ(0, dlclose(self)); |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 104 | } |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 105 | |
Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 106 | TEST(dlfcn, dlsym_from_sofile) { |
| 107 | void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL); |
| 108 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 109 | |
Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 110 | // 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] | 111 | void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol"); |
| 112 | ASSERT_TRUE(symbol == nullptr); |
| 113 | ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror()); |
| 114 | |
| 115 | typedef int* (*fn_t)(); |
Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 116 | fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT = |
| 117 | reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT")); |
| 118 | ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror(); |
Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 119 | |
Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 120 | int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT(); |
Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 121 | ASSERT_TRUE(ptr != nullptr) << dlerror(); |
| 122 | ASSERT_EQ(42, *ptr); |
| 123 | |
Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 124 | fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT = |
| 125 | reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT")); |
| 126 | ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror(); |
| 127 | |
| 128 | ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT(); |
| 129 | ASSERT_TRUE(ptr != nullptr) << dlerror(); |
| 130 | ASSERT_EQ(44, *ptr); |
| 131 | |
| 132 | fn_t lookup_dlsym_symbol_using_RTLD_NEXT = |
| 133 | reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT")); |
| 134 | ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror(); |
| 135 | |
| 136 | ptr = lookup_dlsym_symbol_using_RTLD_NEXT(); |
| 137 | ASSERT_TRUE(ptr != nullptr) << dlerror(); |
| 138 | ASSERT_EQ(43, *ptr); |
| 139 | |
Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 140 | dlclose(handle); |
| 141 | } |
| 142 | |
Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 143 | TEST(dlfcn, dlsym_from_sofile_with_preload) { |
| 144 | void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL); |
| 145 | ASSERT_TRUE(preload != nullptr) << dlerror(); |
| 146 | |
| 147 | void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL); |
| 148 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 149 | |
| 150 | // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT) |
| 151 | void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol"); |
| 152 | ASSERT_TRUE(symbol == nullptr); |
| 153 | ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror()); |
| 154 | |
| 155 | typedef int* (*fn_t)(); |
| 156 | fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT = |
| 157 | reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT")); |
| 158 | ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror(); |
| 159 | |
| 160 | int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT(); |
| 161 | ASSERT_TRUE(ptr != nullptr) << dlerror(); |
| 162 | ASSERT_EQ(42, *ptr); |
| 163 | |
| 164 | fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT = |
| 165 | reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT")); |
| 166 | ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror(); |
| 167 | |
| 168 | ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT(); |
| 169 | ASSERT_TRUE(ptr != nullptr) << dlerror(); |
| 170 | ASSERT_EQ(44, *ptr); |
| 171 | |
| 172 | fn_t lookup_dlsym_symbol_using_RTLD_NEXT = |
| 173 | reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT")); |
| 174 | ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror(); |
| 175 | |
| 176 | ptr = lookup_dlsym_symbol_using_RTLD_NEXT(); |
| 177 | ASSERT_TRUE(ptr != nullptr) << dlerror(); |
| 178 | ASSERT_EQ(43, *ptr); |
| 179 | |
| 180 | dlclose(handle); |
| 181 | dlclose(preload); |
| 182 | } |
| 183 | |
Dmitriy Ivanov | f439b5a | 2015-05-30 13:04:39 -0700 | [diff] [blame] | 184 | TEST(dlfcn, dlsym_handle_global_sym) { |
| 185 | // check that we do not look into global group |
| 186 | // when looking up symbol by handle |
| 187 | void* handle = dlopen("libtest_empty.so", RTLD_NOW); |
| 188 | dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL); |
| 189 | void* sym = dlsym(handle, "getRandomNumber"); |
| 190 | ASSERT_TRUE(sym == nullptr); |
| 191 | ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror()); |
| 192 | |
| 193 | sym = dlsym(handle, "DlSymTestFunction"); |
| 194 | ASSERT_TRUE(sym == nullptr); |
| 195 | ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror()); |
| 196 | dlclose(handle); |
| 197 | } |
| 198 | |
Dimitry Ivanov | d5b578a | 2016-12-14 15:16:56 -0800 | [diff] [blame] | 199 | TEST(dlfcn, dlsym_handle_empty_symbol) { |
| 200 | // check that dlsym of an empty symbol fails (see http://b/33530622) |
| 201 | void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW); |
| 202 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 203 | void* sym = dlsym(handle, ""); |
| 204 | ASSERT_TRUE(sym == nullptr); |
| 205 | ASSERT_SUBSTR("undefined symbol: ", dlerror()); |
| 206 | dlclose(handle); |
| 207 | } |
| 208 | |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 209 | TEST(dlfcn, dlsym_with_dependencies) { |
| 210 | void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW); |
Dmitriy Ivanov | f439b5a | 2015-05-30 13:04:39 -0700 | [diff] [blame] | 211 | ASSERT_TRUE(handle != nullptr); |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 212 | dlerror(); |
| 213 | // This symbol is in DT_NEEDED library. |
| 214 | void* sym = dlsym(handle, "getRandomNumber"); |
Dmitriy Ivanov | f439b5a | 2015-05-30 13:04:39 -0700 | [diff] [blame] | 215 | ASSERT_TRUE(sym != nullptr) << dlerror(); |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 216 | int (*fn)(void); |
| 217 | fn = reinterpret_cast<int (*)(void)>(sym); |
| 218 | EXPECT_EQ(4, fn()); |
| 219 | dlclose(handle); |
| 220 | } |
| 221 | |
Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 222 | TEST(dlfcn, dlopen_noload) { |
| 223 | void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD); |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 224 | ASSERT_TRUE(handle == nullptr); |
Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 225 | handle = dlopen("libtest_simple.so", RTLD_NOW); |
| 226 | void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD); |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 227 | ASSERT_TRUE(handle != nullptr); |
| 228 | ASSERT_TRUE(handle2 != nullptr); |
Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 229 | ASSERT_TRUE(handle == handle2); |
| 230 | ASSERT_EQ(0, dlclose(handle)); |
| 231 | ASSERT_EQ(0, dlclose(handle2)); |
| 232 | } |
| 233 | |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 234 | TEST(dlfcn, dlopen_by_soname) { |
| 235 | static const char* soname = "libdlext_test_soname.so"; |
| 236 | static const char* filename = "libdlext_test_different_soname.so"; |
| 237 | // 1. Make sure there is no library with soname in default search path |
| 238 | void* handle = dlopen(soname, RTLD_NOW); |
| 239 | ASSERT_TRUE(handle == nullptr); |
| 240 | |
| 241 | // 2. Load a library using filename |
| 242 | handle = dlopen(filename, RTLD_NOW); |
| 243 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 244 | |
| 245 | // 3. Find library by soname |
| 246 | void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD); |
| 247 | ASSERT_TRUE(handle_soname != nullptr) << dlerror(); |
| 248 | ASSERT_EQ(handle, handle_soname); |
| 249 | |
| 250 | // 4. RTLD_NOLOAD should still work with filename |
| 251 | void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD); |
| 252 | ASSERT_TRUE(handle_filename != nullptr) << dlerror(); |
| 253 | ASSERT_EQ(handle, handle_filename); |
| 254 | |
| 255 | dlclose(handle_filename); |
| 256 | dlclose(handle_soname); |
| 257 | dlclose(handle); |
| 258 | } |
| 259 | |
dimitry | c18de1b | 2017-09-26 14:31:35 +0200 | [diff] [blame] | 260 | TEST(dlfcn, dlopen_vdso) { |
dimitry | b9555a9 | 2017-10-11 18:04:05 +0200 | [diff] [blame] | 261 | #if __has_include(<sys/auxv.h>) |
| 262 | if (getauxval(AT_SYSINFO_EHDR) == 0) { |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 263 | GTEST_SKIP() << "getauxval(AT_SYSINFO_EHDR) == 0, skipping this test"; |
dimitry | b9555a9 | 2017-10-11 18:04:05 +0200 | [diff] [blame] | 264 | } |
| 265 | #endif |
Elliott Hughes | da1bb11 | 2018-02-16 10:05:08 -0800 | [diff] [blame] | 266 | |
| 267 | const char* vdso_name = "linux-vdso.so.1"; |
| 268 | #if defined(__i386__) |
| 269 | vdso_name = "linux-gate.so.1"; |
| 270 | #endif |
| 271 | void* handle = dlopen(vdso_name, RTLD_NOW); |
dimitry | c18de1b | 2017-09-26 14:31:35 +0200 | [diff] [blame] | 272 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 273 | dlclose(handle); |
| 274 | } |
| 275 | |
Dimitry Ivanov | 0a2ab02 | 2016-04-05 17:12:01 -0700 | [diff] [blame] | 276 | // mips doesn't support ifuncs |
| 277 | #if !defined(__mips__) |
Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 278 | TEST(dlfcn, ifunc_variable) { |
| 279 | typedef const char* (*fn_ptr)(); |
| 280 | |
| 281 | // ifunc's choice depends on whether IFUNC_CHOICE has a value |
| 282 | // first check the set case |
| 283 | setenv("IFUNC_CHOICE", "set", 1); |
| 284 | // preload libtest_ifunc_variable_impl.so |
| 285 | void* handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW); |
| 286 | void* handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW); |
| 287 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 288 | const char** foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo")); |
| 289 | fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library")); |
| 290 | ASSERT_TRUE(foo_ptr != nullptr) << dlerror(); |
| 291 | ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror(); |
| 292 | ASSERT_EQ(strncmp("set", *foo_ptr, 3), 0); |
| 293 | ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0); |
| 294 | dlclose(handle); |
| 295 | dlclose(handle_impl); |
| 296 | |
| 297 | // then check the unset case |
| 298 | unsetenv("IFUNC_CHOICE"); |
| 299 | handle_impl = dlopen("libtest_ifunc_variable_impl.so", RTLD_NOW); |
| 300 | handle = dlopen("libtest_ifunc_variable.so", RTLD_NOW); |
| 301 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 302 | foo_ptr = reinterpret_cast<const char**>(dlsym(handle, "foo")); |
| 303 | foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library")); |
| 304 | ASSERT_TRUE(foo_ptr != nullptr) << dlerror(); |
| 305 | ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror(); |
| 306 | ASSERT_EQ(strncmp("unset", *foo_ptr, 5), 0); |
| 307 | ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0); |
| 308 | dlclose(handle); |
| 309 | dlclose(handle_impl); |
| 310 | } |
| 311 | |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 312 | TEST(dlfcn, ifunc) { |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 313 | typedef const char* (*fn_ptr)(); |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 314 | |
| 315 | // ifunc's choice depends on whether IFUNC_CHOICE has a value |
| 316 | // first check the set case |
| 317 | setenv("IFUNC_CHOICE", "set", 1); |
| 318 | void* handle = dlopen("libtest_ifunc.so", RTLD_NOW); |
Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 319 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 320 | fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo")); |
| 321 | fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library")); |
Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 322 | ASSERT_TRUE(foo_ptr != nullptr) << dlerror(); |
| 323 | ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror(); |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 324 | ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0); |
| 325 | ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0); |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 326 | dlclose(handle); |
| 327 | |
| 328 | // then check the unset case |
| 329 | unsetenv("IFUNC_CHOICE"); |
| 330 | handle = dlopen("libtest_ifunc.so", RTLD_NOW); |
Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 331 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 332 | foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo")); |
| 333 | foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library")); |
Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 334 | ASSERT_TRUE(foo_ptr != nullptr) << dlerror(); |
| 335 | ASSERT_TRUE(foo_library_ptr != nullptr) << dlerror(); |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 336 | ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0); |
Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 337 | ASSERT_EQ(strncmp("unset", foo_library_ptr(), 5), 0); |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 338 | dlclose(handle); |
| 339 | } |
| 340 | |
| 341 | TEST(dlfcn, ifunc_ctor_call) { |
| 342 | typedef const char* (*fn_ptr)(); |
| 343 | |
| 344 | void* handle = dlopen("libtest_ifunc.so", RTLD_NOW); |
Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 345 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 346 | fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative")); |
| 347 | ASSERT_TRUE(is_ctor_called != nullptr) << dlerror(); |
| 348 | ASSERT_STREQ("false", is_ctor_called()); |
| 349 | |
| 350 | is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot")); |
| 351 | ASSERT_TRUE(is_ctor_called != nullptr) << dlerror(); |
Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 352 | ASSERT_STREQ("true", is_ctor_called()); |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 353 | dlclose(handle); |
| 354 | } |
Dimitry Ivanov | ba35b2d | 2016-04-08 11:47:53 -0700 | [diff] [blame] | 355 | |
| 356 | TEST(dlfcn, ifunc_ctor_call_rtld_lazy) { |
| 357 | typedef const char* (*fn_ptr)(); |
| 358 | |
| 359 | void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY); |
| 360 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 361 | fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative")); |
| 362 | ASSERT_TRUE(is_ctor_called != nullptr) << dlerror(); |
| 363 | ASSERT_STREQ("false", is_ctor_called()); |
| 364 | |
| 365 | is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot")); |
| 366 | ASSERT_TRUE(is_ctor_called != nullptr) << dlerror(); |
| 367 | ASSERT_STREQ("true", is_ctor_called()); |
| 368 | dlclose(handle); |
| 369 | } |
Dimitry Ivanov | 0a2ab02 | 2016-04-05 17:12:01 -0700 | [diff] [blame] | 370 | #endif |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 371 | |
Dmitriy Ivanov | b2a30ee | 2014-09-04 18:23:00 -0700 | [diff] [blame] | 372 | TEST(dlfcn, dlopen_check_relocation_dt_needed_order) { |
| 373 | // This is the structure of the test library and |
| 374 | // its dt_needed libraries |
| 375 | // libtest_relo_check_dt_needed_order.so |
| 376 | // | |
| 377 | // +-> libtest_relo_check_dt_needed_order_1.so |
| 378 | // | |
| 379 | // +-> libtest_relo_check_dt_needed_order_2.so |
| 380 | // |
| 381 | // The root library references relo_test_get_answer_lib - which is defined |
| 382 | // in both dt_needed libraries, the correct relocation should |
| 383 | // use the function defined in libtest_relo_check_dt_needed_order_1.so |
| 384 | void* handle = nullptr; |
Tom Cherry | b8ab618 | 2017-04-05 16:20:29 -0700 | [diff] [blame] | 385 | auto guard = android::base::make_scope_guard([&]() { dlclose(handle); }); |
Dmitriy Ivanov | b2a30ee | 2014-09-04 18:23:00 -0700 | [diff] [blame] | 386 | |
| 387 | handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW); |
| 388 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 389 | |
| 390 | typedef int (*fn_t) (void); |
| 391 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer")); |
| 392 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 393 | ASSERT_EQ(1, fn()); |
| 394 | } |
| 395 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 396 | TEST(dlfcn, dlopen_check_order_dlsym) { |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 397 | // Here is how the test library and its dt_needed |
| 398 | // libraries are arranged |
| 399 | // |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 400 | // libtest_check_order_children.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 401 | // | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 402 | // +-> ..._1_left.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 403 | // | | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 404 | // | +-> ..._a.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 405 | // | | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 406 | // | +-> ...r_b.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 407 | // | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 408 | // +-> ..._2_right.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 409 | // | | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 410 | // | +-> ..._d.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 411 | // | | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 412 | // | +-> ..._b.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 413 | // | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 414 | // +-> ..._3_c.so |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 415 | // |
| 416 | // load order should be (1, 2, 3, a, b, d) |
| 417 | // |
| 418 | // get_answer() is defined in (2, 3, a, b, c) |
| 419 | // get_answer2() is defined in (b, d) |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 420 | void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 421 | ASSERT_TRUE(sym == nullptr); |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 422 | void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL); |
| 423 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 424 | typedef int (*fn_t) (void); |
| 425 | fn_t fn, fn2; |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 426 | 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] | 427 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 428 | 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] | 429 | ASSERT_TRUE(fn2 != nullptr) << dlerror(); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 430 | |
| 431 | ASSERT_EQ(42, fn()); |
| 432 | ASSERT_EQ(43, fn2()); |
| 433 | dlclose(handle); |
| 434 | } |
| 435 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 436 | TEST(dlfcn, dlopen_check_order_reloc_siblings) { |
| 437 | // This is how this one works: |
| 438 | // we lookup and call get_answer which is defined in '_2.so' |
| 439 | // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so' |
| 440 | // the correct _impl() is implemented by '_a.so'; |
| 441 | // |
| 442 | // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?) |
| 443 | // |
| 444 | // Here is the picture: |
| 445 | // |
| 446 | // libtest_check_order_reloc_siblings.so |
| 447 | // | |
| 448 | // +-> ..._1.so <- empty |
| 449 | // | | |
| 450 | // | +-> ..._a.so <- exports correct answer_impl() |
| 451 | // | | |
| 452 | // | +-> ..._b.so <- every other letter exporting incorrect one. |
| 453 | // | |
| 454 | // +-> ..._2.so <- empty |
| 455 | // | | |
| 456 | // | +-> ..._c.so |
| 457 | // | | |
| 458 | // | +-> ..._d.so |
| 459 | // | |
| 460 | // +-> ..._3.so <- empty |
| 461 | // | |
| 462 | // +-> ..._e.so |
| 463 | // | |
| 464 | // +-> ..._f.so <- exports get_answer() that calls get_anser_impl(); |
| 465 | // implements incorrect get_answer_impl() |
| 466 | |
| 467 | void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD); |
| 468 | ASSERT_TRUE(handle == nullptr); |
| 469 | #ifdef __BIONIC__ |
| 470 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? |
| 471 | ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
| 472 | #endif |
| 473 | |
| 474 | handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL); |
| 475 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 476 | |
| 477 | typedef int (*fn_t) (void); |
| 478 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer")); |
| 479 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 480 | ASSERT_EQ(42, fn()); |
| 481 | |
| 482 | ASSERT_EQ(0, dlclose(handle)); |
| 483 | } |
| 484 | |
| 485 | TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) { |
| 486 | // This test uses the same library as dlopen_check_order_reloc_siblings. |
| 487 | // Unlike dlopen_check_order_reloc_siblings it preloads |
| 488 | // libtest_check_order_reloc_siblings_1.so (first dependency) prior to |
| 489 | // dlopen(libtest_check_order_reloc_siblings.so) |
| 490 | |
| 491 | void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD); |
| 492 | ASSERT_TRUE(handle == nullptr); |
| 493 | handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD); |
| 494 | ASSERT_TRUE(handle == nullptr); |
| 495 | |
| 496 | void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL); |
| 497 | ASSERT_TRUE(handle_for_1 != nullptr) << dlerror(); |
| 498 | |
| 499 | handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL); |
| 500 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 501 | |
| 502 | ASSERT_EQ(0, dlclose(handle_for_1)); |
| 503 | |
| 504 | typedef int (*fn_t) (void); |
| 505 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer")); |
| 506 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 507 | ASSERT_EQ(42, fn()); |
| 508 | |
| 509 | ASSERT_EQ(0, dlclose(handle)); |
| 510 | } |
| 511 | |
Dmitriy Ivanov | 7699d13 | 2014-11-18 17:26:31 -0800 | [diff] [blame] | 512 | TEST(dlfcn, dlopen_check_order_reloc_grandchild) { |
| 513 | // This is how this one works: |
| 514 | // we lookup and call grandchild_get_answer which is defined in '_2.so' |
| 515 | // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so' |
| 516 | // the correct _impl() is implemented by '_c_1.so'; |
| 517 | // |
| 518 | // Here is the picture of subtree: |
| 519 | // |
| 520 | // libtest_check_order_reloc_siblings.so |
| 521 | // | |
| 522 | // +-> ..._2.so <- grandchild_get_answer() |
| 523 | // | |
| 524 | // +-> ..._c.so <- empty |
| 525 | // | | |
| 526 | // | +-> _c_1.so <- exports correct answer_impl() |
| 527 | // | | |
| 528 | // | +-> _c_2.so <- exports incorrect answer_impl() |
| 529 | // | |
| 530 | // +-> ..._d.so <- empty |
| 531 | |
| 532 | void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD); |
| 533 | ASSERT_TRUE(handle == nullptr); |
| 534 | #ifdef __BIONIC__ |
| 535 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? |
| 536 | ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
| 537 | #endif |
| 538 | |
| 539 | handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL); |
| 540 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 541 | |
| 542 | typedef int (*fn_t) (void); |
| 543 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer")); |
| 544 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 545 | ASSERT_EQ(42, fn()); |
| 546 | |
| 547 | ASSERT_EQ(0, dlclose(handle)); |
| 548 | } |
| 549 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 550 | TEST(dlfcn, dlopen_check_order_reloc_nephew) { |
| 551 | // This is how this one works: |
| 552 | // we lookup and call nephew_get_answer which is defined in '_2.so' |
| 553 | // and in turn calls external get_answer_impl() defined in '_[a-f].so' |
| 554 | // the correct _impl() is implemented by '_a.so'; |
| 555 | // |
| 556 | // Here is the picture: |
| 557 | // |
| 558 | // libtest_check_order_reloc_siblings.so |
| 559 | // | |
| 560 | // +-> ..._1.so <- empty |
| 561 | // | | |
| 562 | // | +-> ..._a.so <- exports correct answer_impl() |
| 563 | // | | |
| 564 | // | +-> ..._b.so <- every other letter exporting incorrect one. |
| 565 | // | |
| 566 | // +-> ..._2.so <- empty |
| 567 | // | | |
| 568 | // | +-> ..._c.so |
| 569 | // | | |
| 570 | // | +-> ..._d.so |
| 571 | // | |
| 572 | // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl(); |
| 573 | // | |
| 574 | // +-> ..._e.so |
| 575 | // | |
| 576 | // +-> ..._f.so |
| 577 | |
| 578 | void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD); |
| 579 | ASSERT_TRUE(handle == nullptr); |
| 580 | #ifdef __BIONIC__ |
| 581 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? |
| 582 | ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
| 583 | #endif |
| 584 | |
| 585 | handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL); |
| 586 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 587 | |
| 588 | typedef int (*fn_t) (void); |
| 589 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer")); |
| 590 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 591 | ASSERT_EQ(42, fn()); |
| 592 | |
| 593 | ASSERT_EQ(0, dlclose(handle)); |
| 594 | } |
| 595 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 596 | TEST(dlfcn, check_unload_after_reloc) { |
| 597 | // This is how this one works: |
| 598 | // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child |
| 599 | // | |
| 600 | // +-> libtest_two_parents_child |
| 601 | // |
| 602 | // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child |
| 603 | // | |
| 604 | // +-> libtest_two_parents_child |
| 605 | // |
| 606 | // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so |
| 607 | // as a second step it dlopens parent2 and dlcloses parent1... |
| 608 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 609 | void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL); |
| 610 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 611 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 612 | void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL); |
| 613 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 614 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 615 | typedef int (*fn_t) (void); |
| 616 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer")); |
| 617 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 618 | ASSERT_EQ(42, fn()); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 619 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 620 | ASSERT_EQ(0, dlclose(handle)); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 621 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 622 | handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD); |
| 623 | ASSERT_TRUE(handle != nullptr); |
| 624 | ASSERT_EQ(0, dlclose(handle)); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 625 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 626 | fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer")); |
| 627 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 628 | ASSERT_EQ(42, fn()); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 629 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 630 | ASSERT_EQ(0, dlclose(handle2)); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 631 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 632 | handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD); |
| 633 | ASSERT_TRUE(handle == nullptr); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 636 | extern "C" int check_order_reloc_root_get_answer_impl() { |
| 637 | return 42; |
| 638 | } |
| 639 | |
| 640 | TEST(dlfcn, dlopen_check_order_reloc_main_executable) { |
| 641 | // This is how this one works: |
| 642 | // we lookup and call get_answer3 which is defined in 'root.so' |
| 643 | // and in turn calls external root_get_answer_impl() defined in _2.so and |
| 644 | // above the correct _impl() is one in the executable. |
| 645 | // |
| 646 | // libtest_check_order_reloc_root.so |
| 647 | // | |
| 648 | // +-> ..._1.so <- empty |
| 649 | // | |
| 650 | // +-> ..._2.so <- gives incorrect answer for answer_main_impl() |
| 651 | // |
| 652 | |
| 653 | void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD); |
| 654 | ASSERT_TRUE(handle == nullptr); |
| 655 | #ifdef __BIONIC__ |
| 656 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? |
| 657 | ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
| 658 | #endif |
| 659 | |
| 660 | handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL); |
| 661 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 662 | |
| 663 | typedef int (*fn_t) (void); |
| 664 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer")); |
| 665 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 666 | ASSERT_EQ(42, fn()); |
| 667 | |
| 668 | ASSERT_EQ(0, dlclose(handle)); |
| 669 | } |
| 670 | |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 671 | TEST(dlfcn, dlopen_check_rtld_local) { |
| 672 | void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 673 | ASSERT_TRUE(sym == nullptr); |
| 674 | |
| 675 | // implicit RTLD_LOCAL |
| 676 | void* handle = dlopen("libtest_simple.so", RTLD_NOW); |
| 677 | sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 678 | ASSERT_TRUE(sym == nullptr); |
| 679 | ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror()); |
| 680 | sym = dlsym(handle, "dlopen_testlib_simple_func"); |
| 681 | ASSERT_TRUE(sym != nullptr); |
| 682 | ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)()); |
| 683 | dlclose(handle); |
| 684 | |
| 685 | // explicit RTLD_LOCAL |
| 686 | handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL); |
| 687 | sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 688 | ASSERT_TRUE(sym == nullptr); |
| 689 | ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror()); |
| 690 | sym = dlsym(handle, "dlopen_testlib_simple_func"); |
| 691 | ASSERT_TRUE(sym != nullptr); |
| 692 | ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)()); |
| 693 | dlclose(handle); |
| 694 | } |
| 695 | |
| 696 | TEST(dlfcn, dlopen_check_rtld_global) { |
| 697 | void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 698 | ASSERT_TRUE(sym == nullptr); |
| 699 | |
| 700 | void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL); |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 701 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 702 | sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 703 | ASSERT_TRUE(sym != nullptr) << dlerror(); |
| 704 | ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)()); |
| 705 | dlclose(handle); |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 706 | |
| 707 | // RTLD_GLOBAL implies RTLD_NODELETE, let's check that |
| 708 | void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); |
| 709 | ASSERT_EQ(sym, sym_after_dlclose); |
Dmitriy Ivanov | f439b5a | 2015-05-30 13:04:39 -0700 | [diff] [blame] | 710 | |
| 711 | // Check if dlsym() for main program's handle searches RTLD_GLOBAL |
| 712 | // shared libraries after symbol was not found in the main executable |
| 713 | // and dependent libraries. |
| 714 | void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW); |
| 715 | sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func"); |
| 716 | ASSERT_TRUE(sym != nullptr) << dlerror(); |
| 717 | |
| 718 | dlclose(handle_for_main_executable); |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 721 | // libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so -> |
| 722 | // libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so -> |
| 723 | // libtest_with_dependency_loop_a.so |
| 724 | TEST(dlfcn, dlopen_check_loop) { |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 725 | void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW); |
| 726 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 727 | void* f = dlsym(handle, "dlopen_test_loopy_function"); |
| 728 | ASSERT_TRUE(f != nullptr) << dlerror(); |
| 729 | EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)()); |
| 730 | ASSERT_EQ(0, dlclose(handle)); |
Dmitriy Ivanov | a6ac54a | 2014-09-09 10:21:42 -0700 | [diff] [blame] | 731 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 732 | // dlopen second time to make sure that the library was unloaded correctly |
| 733 | handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD); |
| 734 | ASSERT_TRUE(handle == nullptr); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 735 | #ifdef __BIONIC__ |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 736 | ASSERT_STREQ("dlopen failed: library \"libtest_with_dependency_loop.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 737 | #else |
| 738 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? |
| 739 | ASSERT_TRUE(dlerror() == nullptr); |
Dmitriy Ivanov | eb27bba | 2014-09-15 14:13:24 -0700 | [diff] [blame] | 740 | #endif |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 741 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 742 | handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD); |
| 743 | ASSERT_TRUE(handle == nullptr); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 746 | TEST(dlfcn, dlopen_nodelete) { |
| 747 | static bool is_unloaded = false; |
| 748 | |
| 749 | void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE); |
| 750 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 751 | void (*set_unload_flag_ptr)(bool*); |
| 752 | set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr")); |
| 753 | ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror(); |
| 754 | set_unload_flag_ptr(&is_unloaded); |
| 755 | |
| 756 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number")); |
| 757 | ASSERT_TRUE(taxicab_number != nullptr) << dlerror(); |
| 758 | ASSERT_EQ(1729U, *taxicab_number); |
| 759 | *taxicab_number = 2; |
| 760 | |
| 761 | dlclose(handle); |
| 762 | ASSERT_TRUE(!is_unloaded); |
| 763 | |
| 764 | uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number")); |
| 765 | ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number); |
| 766 | ASSERT_EQ(2U, *taxicab_number_after_dlclose); |
| 767 | |
| 768 | |
| 769 | handle = dlopen("libtest_nodelete_1.so", RTLD_NOW); |
| 770 | uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number")); |
| 771 | ASSERT_EQ(taxicab_number2, taxicab_number); |
| 772 | |
| 773 | ASSERT_EQ(2U, *taxicab_number2); |
| 774 | |
| 775 | dlclose(handle); |
| 776 | ASSERT_TRUE(!is_unloaded); |
| 777 | } |
| 778 | |
| 779 | TEST(dlfcn, dlopen_nodelete_on_second_dlopen) { |
| 780 | static bool is_unloaded = false; |
| 781 | |
| 782 | void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW); |
| 783 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 784 | void (*set_unload_flag_ptr)(bool*); |
| 785 | set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr")); |
| 786 | ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror(); |
| 787 | set_unload_flag_ptr(&is_unloaded); |
| 788 | |
| 789 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number")); |
| 790 | ASSERT_TRUE(taxicab_number != nullptr) << dlerror(); |
| 791 | |
| 792 | ASSERT_EQ(1729U, *taxicab_number); |
| 793 | *taxicab_number = 2; |
| 794 | |
| 795 | // This RTLD_NODELETE should be ignored |
| 796 | void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE); |
| 797 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 798 | ASSERT_EQ(handle, handle1); |
| 799 | |
| 800 | dlclose(handle1); |
| 801 | dlclose(handle); |
| 802 | |
| 803 | ASSERT_TRUE(is_unloaded); |
| 804 | } |
| 805 | |
| 806 | TEST(dlfcn, dlopen_nodelete_dt_flags_1) { |
| 807 | static bool is_unloaded = false; |
| 808 | |
| 809 | void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW); |
| 810 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 811 | void (*set_unload_flag_ptr)(bool*); |
| 812 | set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr")); |
| 813 | ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror(); |
| 814 | set_unload_flag_ptr(&is_unloaded); |
| 815 | |
| 816 | dlclose(handle); |
| 817 | ASSERT_TRUE(!is_unloaded); |
| 818 | } |
| 819 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 820 | TEST(dlfcn, dlsym_df_1_global) { |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 821 | void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW); |
| 822 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 823 | int (*get_answer)(); |
| 824 | get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer")); |
| 825 | ASSERT_TRUE(get_answer != nullptr) << dlerror(); |
| 826 | ASSERT_EQ(42, get_answer()); |
| 827 | ASSERT_EQ(0, dlclose(handle)); |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 830 | TEST(dlfcn, dlopen_failure) { |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 831 | void* self = dlopen("/does/not/exist", RTLD_NOW); |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 832 | ASSERT_TRUE(self == nullptr); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 833 | #if defined(__BIONIC__) |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 834 | ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror()); |
| 835 | #else |
| 836 | ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror()); |
| 837 | #endif |
| 838 | } |
| 839 | |
dimitry | 109040c | 2017-11-03 13:49:07 +0100 | [diff] [blame] | 840 | TEST(dlfcn, dlclose_unload) { |
| 841 | void* handle = dlopen("libtest_simple.so", RTLD_NOW); |
| 842 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 843 | uint32_t* taxicab_number = static_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number")); |
| 844 | ASSERT_TRUE(taxicab_number != nullptr) << dlerror(); |
| 845 | EXPECT_EQ(1729U, *taxicab_number); |
| 846 | dlclose(handle); |
| 847 | // Making sure that the library has been unmapped as part of library unload |
| 848 | // process. Note that mprotect somewhat counter-intuitively returns ENOMEM in |
| 849 | // this case. |
| 850 | uintptr_t page_start = reinterpret_cast<uintptr_t>(taxicab_number) & ~(PAGE_SIZE - 1); |
| 851 | ASSERT_TRUE(mprotect(reinterpret_cast<void*>(page_start), PAGE_SIZE, PROT_NONE) != 0); |
| 852 | ASSERT_EQ(ENOMEM, errno) << strerror(errno); |
| 853 | } |
| 854 | |
Dimitry Ivanov | c7365eb | 2016-11-17 12:38:09 -0800 | [diff] [blame] | 855 | static void ConcurrentDlErrorFn(std::string& error) { |
| 856 | ASSERT_TRUE(dlerror() == nullptr); |
| 857 | |
| 858 | void* handle = dlopen("/child/thread", RTLD_NOW); |
| 859 | ASSERT_TRUE(handle == nullptr); |
| 860 | |
| 861 | const char* err = dlerror(); |
| 862 | ASSERT_TRUE(err != nullptr); |
| 863 | |
| 864 | error = err; |
| 865 | } |
| 866 | |
| 867 | TEST(dlfcn, dlerror_concurrent_buffer) { |
| 868 | void* handle = dlopen("/main/thread", RTLD_NOW); |
| 869 | ASSERT_TRUE(handle == nullptr); |
| 870 | const char* main_thread_error = dlerror(); |
| 871 | ASSERT_TRUE(main_thread_error != nullptr); |
| 872 | ASSERT_SUBSTR("/main/thread", main_thread_error); |
| 873 | |
| 874 | std::string child_thread_error; |
| 875 | std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error)); |
| 876 | t.join(); |
| 877 | ASSERT_SUBSTR("/child/thread", child_thread_error.c_str()); |
| 878 | |
| 879 | // Check that main thread local buffer was not modified. |
| 880 | ASSERT_SUBSTR("/main/thread", main_thread_error); |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 883 | TEST(dlfcn, dlerror_concurrent) { |
Dimitry Ivanov | c7365eb | 2016-11-17 12:38:09 -0800 | [diff] [blame] | 884 | void* handle = dlopen("/main/thread", RTLD_NOW); |
| 885 | ASSERT_TRUE(handle == nullptr); |
| 886 | |
| 887 | std::string child_thread_error; |
| 888 | std::thread t(ConcurrentDlErrorFn, std::ref(child_thread_error)); |
| 889 | t.join(); |
| 890 | ASSERT_SUBSTR("/child/thread", child_thread_error.c_str()); |
| 891 | |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 892 | const char* main_thread_error = dlerror(); |
Dimitry Ivanov | c7365eb | 2016-11-17 12:38:09 -0800 | [diff] [blame] | 893 | ASSERT_TRUE(main_thread_error != nullptr); |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 894 | ASSERT_SUBSTR("/main/thread", main_thread_error); |
| 895 | } |
| 896 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 897 | TEST(dlfcn, dlsym_failures) { |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 898 | dlerror(); // Clear any pending errors. |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 899 | void* self = dlopen(nullptr, RTLD_NOW); |
| 900 | ASSERT_TRUE(self != nullptr); |
| 901 | ASSERT_TRUE(dlerror() == nullptr); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 902 | |
| 903 | void* sym; |
| 904 | |
Dmitriy Ivanov | 44adf93 | 2014-05-22 09:49:24 -0700 | [diff] [blame] | 905 | #if defined(__BIONIC__) && !defined(__LP64__) |
| 906 | // RTLD_DEFAULT in lp32 bionic is not (void*)0 |
| 907 | // so it can be distinguished from the NULL handle. |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 908 | sym = dlsym(nullptr, "test"); |
| 909 | ASSERT_TRUE(sym == nullptr); |
Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 910 | ASSERT_STREQ("dlsym failed: library handle is null", dlerror()); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 911 | #endif |
| 912 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 913 | // Symbol that doesn't exist. |
| 914 | sym = dlsym(self, "ThisSymbolDoesNotExist"); |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 915 | ASSERT_TRUE(sym == nullptr); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 916 | ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror()); |
Elliott Hughes | 1a69616 | 2012-11-01 13:49:32 -0700 | [diff] [blame] | 917 | |
| 918 | ASSERT_EQ(0, dlclose(self)); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 921 | TEST(dlfcn, dladdr_executable) { |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 922 | dlerror(); // Clear any pending errors. |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 923 | void* self = dlopen(nullptr, RTLD_NOW); |
| 924 | ASSERT_TRUE(self != nullptr); |
| 925 | ASSERT_TRUE(dlerror() == nullptr); |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 926 | |
| 927 | void* sym = dlsym(self, "DlSymTestFunction"); |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 928 | ASSERT_TRUE(sym != nullptr); |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 929 | |
| 930 | // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address. |
| 931 | void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2); |
| 932 | |
| 933 | Dl_info info; |
| 934 | int rc = dladdr(addr, &info); |
| 935 | ASSERT_NE(rc, 0); // Zero on error, non-zero on success. |
| 936 | |
| 937 | // Get the name of this executable. |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 938 | const std::string executable_path = android::base::GetExecutablePath(); |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 939 | |
| 940 | // The filename should be that of this executable. |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 941 | char dli_realpath[PATH_MAX]; |
| 942 | ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr); |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame] | 943 | ASSERT_STREQ(executable_path.c_str(), dli_realpath); |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 944 | |
| 945 | // The symbol name should be the symbol we looked up. |
| 946 | ASSERT_STREQ(info.dli_sname, "DlSymTestFunction"); |
| 947 | |
| 948 | // The address should be the exact address of the symbol. |
| 949 | ASSERT_EQ(info.dli_saddr, sym); |
| 950 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 951 | std::vector<map_record> maps; |
| 952 | ASSERT_TRUE(Maps::parse_maps(&maps)); |
| 953 | |
| 954 | void* base_address = nullptr; |
| 955 | for (const map_record& rec : maps) { |
| 956 | if (executable_path == rec.pathname) { |
| 957 | base_address = reinterpret_cast<void*>(rec.addr_start); |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 958 | break; |
| 959 | } |
| 960 | } |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 961 | |
| 962 | // The base address should be the address we were loaded at. |
| 963 | ASSERT_EQ(info.dli_fbase, base_address); |
Elliott Hughes | 1a69616 | 2012-11-01 13:49:32 -0700 | [diff] [blame] | 964 | |
| 965 | ASSERT_EQ(0, dlclose(self)); |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 966 | } |
| 967 | |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame] | 968 | TEST(dlfcn, dlopen_executable_by_absolute_path) { |
| 969 | void* handle1 = dlopen(nullptr, RTLD_NOW); |
| 970 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 971 | |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 972 | void* handle2 = dlopen(android::base::GetExecutablePath().c_str(), RTLD_NOW); |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame] | 973 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
| 974 | |
| 975 | #if defined(__BIONIC__) |
| 976 | ASSERT_EQ(handle1, handle2); |
| 977 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 978 | GTEST_SKIP() << "Skipping ASSERT_EQ(handle1, handle2) for glibc: " |
| 979 | "it loads a separate copy of the main executable " |
| 980 | "on dlopen by absolute path"; |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame] | 981 | #endif |
| 982 | } |
| 983 | |
Victor Khimenko | 14b9d71 | 2017-01-25 19:59:16 +0100 | [diff] [blame] | 984 | #if defined (__aarch64__) |
Dmytro Chystiakov | e712cd1 | 2019-03-29 13:49:12 -0700 | [diff] [blame] | 985 | #define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib64/arm64/" |
Victor Khimenko | 14b9d71 | 2017-01-25 19:59:16 +0100 | [diff] [blame] | 986 | #elif defined (__arm__) |
| 987 | #define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/arm/" |
| 988 | #elif defined (__i386__) |
| 989 | #define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/x86/" |
| 990 | #elif defined (__x86_64__) |
Dmytro Chystiakov | e712cd1 | 2019-03-29 13:49:12 -0700 | [diff] [blame] | 991 | #define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib64/x86_64/" |
Victor Khimenko | 14b9d71 | 2017-01-25 19:59:16 +0100 | [diff] [blame] | 992 | #elif defined (__mips__) |
| 993 | #if defined(__LP64__) |
Dmytro Chystiakov | e712cd1 | 2019-03-29 13:49:12 -0700 | [diff] [blame] | 994 | #define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib64/mips64/" |
Victor Khimenko | 14b9d71 | 2017-01-25 19:59:16 +0100 | [diff] [blame] | 995 | #else |
| 996 | #define ALTERNATE_PATH_TO_SYSTEM_LIB "/system/lib/mips/" |
| 997 | #endif |
| 998 | #else |
| 999 | #error "Unknown architecture" |
| 1000 | #endif |
Lazar Trsic | 6f2d310 | 2015-10-13 16:43:00 +0200 | [diff] [blame] | 1001 | #define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so" |
Peter Collingbourne | ea11be0 | 2019-04-30 16:05:13 -0700 | [diff] [blame^] | 1002 | #define PATH_TO_BOOTSTRAP_LIBC PATH_TO_SYSTEM_LIB "bootstrap/libc.so" |
Victor Khimenko | 14b9d71 | 2017-01-25 19:59:16 +0100 | [diff] [blame] | 1003 | #define ALTERNATE_PATH_TO_LIBC ALTERNATE_PATH_TO_SYSTEM_LIB "libc.so" |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1004 | |
| 1005 | TEST(dlfcn, dladdr_libc) { |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1006 | #if defined(__GLIBC__) |
| 1007 | GTEST_SKIP() << "glibc returns libc.so's ldconfig path, which is a symlink (not a realpath)"; |
| 1008 | #endif |
| 1009 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1010 | Dl_info info; |
| 1011 | void* addr = reinterpret_cast<void*>(puts); // well-known libc function |
| 1012 | ASSERT_TRUE(dladdr(addr, &info) != 0); |
| 1013 | |
Dmitriy Ivanov | ef25592 | 2015-04-08 11:53:08 -0700 | [diff] [blame] | 1014 | char libc_realpath[PATH_MAX]; |
Victor Khimenko | 14b9d71 | 2017-01-25 19:59:16 +0100 | [diff] [blame] | 1015 | |
| 1016 | // Check if libc is in canonical path or in alternate path. |
| 1017 | if (strncmp(ALTERNATE_PATH_TO_SYSTEM_LIB, |
| 1018 | info.dli_fname, |
| 1019 | sizeof(ALTERNATE_PATH_TO_SYSTEM_LIB) - 1) == 0) { |
| 1020 | // Platform with emulated architecture. Symlink on ARC++. |
| 1021 | ASSERT_TRUE(realpath(ALTERNATE_PATH_TO_LIBC, libc_realpath) == libc_realpath); |
Peter Collingbourne | ea11be0 | 2019-04-30 16:05:13 -0700 | [diff] [blame^] | 1022 | } else if (strncmp(PATH_TO_BOOTSTRAP_LIBC, info.dli_fname, |
| 1023 | sizeof(PATH_TO_BOOTSTRAP_LIBC) - 1) == 0) { |
| 1024 | ASSERT_TRUE(realpath(PATH_TO_BOOTSTRAP_LIBC, libc_realpath) == libc_realpath); |
Victor Khimenko | 14b9d71 | 2017-01-25 19:59:16 +0100 | [diff] [blame] | 1025 | } else { |
| 1026 | // /system/lib is symlink when this test is executed on host. |
| 1027 | ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath); |
| 1028 | } |
Dmitriy Ivanov | ef25592 | 2015-04-08 11:53:08 -0700 | [diff] [blame] | 1029 | |
| 1030 | ASSERT_STREQ(libc_realpath, info.dli_fname); |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1031 | // TODO: add check for dfi_fbase |
| 1032 | ASSERT_STREQ("puts", info.dli_sname); |
| 1033 | ASSERT_EQ(addr, info.dli_saddr); |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 1036 | TEST(dlfcn, dladdr_invalid) { |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 1037 | Dl_info info; |
| 1038 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 1039 | dlerror(); // Clear any pending errors. |
| 1040 | |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 1041 | // No symbol corresponding to NULL. |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 1042 | ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success. |
| 1043 | ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3). |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 1044 | |
| 1045 | // No symbol corresponding to a stack address. |
| 1046 | 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] | 1047 | ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3). |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 1048 | } |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1049 | |
Elliott Hughes | a43e906 | 2013-01-07 14:18:22 -0800 | [diff] [blame] | 1050 | // GNU-style ELF hash tables are incompatible with the MIPS ABI. |
| 1051 | // 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] | 1052 | TEST(dlfcn, dlopen_library_with_only_gnu_hash) { |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1053 | #if !defined(__mips__) |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1054 | dlerror(); // Clear any pending errors. |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1055 | void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW); |
| 1056 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
Tom Cherry | b8ab618 | 2017-04-05 16:20:29 -0700 | [diff] [blame] | 1057 | auto guard = android::base::make_scope_guard([&]() { dlclose(handle); }); |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1058 | void* sym = dlsym(handle, "getRandomNumber"); |
| 1059 | ASSERT_TRUE(sym != nullptr) << dlerror(); |
| 1060 | int (*fn)(void); |
| 1061 | fn = reinterpret_cast<int (*)(void)>(sym); |
| 1062 | EXPECT_EQ(4, fn()); |
| 1063 | |
| 1064 | Dl_info dlinfo; |
| 1065 | ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo)); |
| 1066 | |
| 1067 | ASSERT_TRUE(fn == dlinfo.dli_saddr); |
| 1068 | ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname); |
Dmitriy Ivanov | b335677 | 2014-11-14 11:19:22 -0800 | [diff] [blame] | 1069 | ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname); |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1070 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1071 | GTEST_SKIP() << "mips toolchain does not support '--hash-style=gnu'"; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1072 | #endif |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1073 | } |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 1074 | |
Dmitriy Ivanov | b335677 | 2014-11-14 11:19:22 -0800 | [diff] [blame] | 1075 | TEST(dlfcn, dlopen_library_with_only_sysv_hash) { |
| 1076 | void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW); |
| 1077 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
Tom Cherry | b8ab618 | 2017-04-05 16:20:29 -0700 | [diff] [blame] | 1078 | auto guard = android::base::make_scope_guard([&]() { dlclose(handle); }); |
Dmitriy Ivanov | b335677 | 2014-11-14 11:19:22 -0800 | [diff] [blame] | 1079 | void* sym = dlsym(handle, "getRandomNumber"); |
| 1080 | ASSERT_TRUE(sym != nullptr) << dlerror(); |
| 1081 | int (*fn)(void); |
| 1082 | fn = reinterpret_cast<int (*)(void)>(sym); |
| 1083 | EXPECT_EQ(4, fn()); |
| 1084 | |
| 1085 | Dl_info dlinfo; |
| 1086 | ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo)); |
| 1087 | |
| 1088 | ASSERT_TRUE(fn == dlinfo.dli_saddr); |
| 1089 | ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname); |
| 1090 | ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname); |
| 1091 | } |
| 1092 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 1093 | TEST(dlfcn, dlopen_bad_flags) { |
| 1094 | dlerror(); // Clear any pending errors. |
| 1095 | void* handle; |
| 1096 | |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 1097 | #if defined(__GLIBC__) |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 1098 | // 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] | 1099 | handle = dlopen(nullptr, 0); |
| 1100 | ASSERT_TRUE(handle == nullptr); |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 1101 | ASSERT_SUBSTR("invalid", dlerror()); |
| 1102 | #endif |
| 1103 | |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 1104 | handle = dlopen(nullptr, 0xffffffff); |
| 1105 | ASSERT_TRUE(handle == nullptr); |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 1106 | ASSERT_SUBSTR("invalid", dlerror()); |
| 1107 | |
| 1108 | // 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] | 1109 | handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY); |
| 1110 | ASSERT_TRUE(handle != nullptr); |
| 1111 | ASSERT_SUBSTR(nullptr, dlerror()); |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 1112 | } |
Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 1113 | |
| 1114 | TEST(dlfcn, rtld_default_unknown_symbol) { |
Elliott Hughes | 2ed7109 | 2013-11-11 15:48:06 -0800 | [diff] [blame] | 1115 | void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME"); |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 1116 | ASSERT_TRUE(addr == nullptr); |
Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | TEST(dlfcn, rtld_default_known_symbol) { |
Elliott Hughes | 2ed7109 | 2013-11-11 15:48:06 -0800 | [diff] [blame] | 1120 | void* addr = dlsym(RTLD_DEFAULT, "fopen"); |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 1121 | ASSERT_TRUE(addr != nullptr); |
Elliott Hughes | 2ed7109 | 2013-11-11 15:48:06 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | TEST(dlfcn, rtld_next_unknown_symbol) { |
| 1125 | void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME"); |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 1126 | ASSERT_TRUE(addr == nullptr); |
Elliott Hughes | 2ed7109 | 2013-11-11 15:48:06 -0800 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | TEST(dlfcn, rtld_next_known_symbol) { |
| 1130 | void* addr = dlsym(RTLD_NEXT, "fopen"); |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 1131 | ASSERT_TRUE(addr != nullptr); |
Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 1132 | } |
Dmitriy Ivanov | 7db1809 | 2014-05-08 12:27:25 -0700 | [diff] [blame] | 1133 | |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1134 | // Check that RTLD_NEXT of a libc symbol works in dlopened library |
| 1135 | TEST(dlfcn, rtld_next_from_library) { |
dimitry | 153168c | 2018-02-20 16:51:41 +0100 | [diff] [blame] | 1136 | void* library_with_fclose = dlopen("libtest_check_rtld_next_from_library.so", RTLD_NOW | RTLD_GLOBAL); |
Raj Mamadgi | 527229c | 2017-11-02 15:53:50 -0700 | [diff] [blame] | 1137 | ASSERT_TRUE(library_with_fclose != nullptr) << dlerror(); |
| 1138 | void* expected_addr = dlsym(RTLD_DEFAULT, "fclose"); |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1139 | ASSERT_TRUE(expected_addr != nullptr) << dlerror(); |
Raj Mamadgi | 527229c | 2017-11-02 15:53:50 -0700 | [diff] [blame] | 1140 | typedef void* (*get_libc_fclose_ptr_fn_t)(); |
| 1141 | get_libc_fclose_ptr_fn_t get_libc_fclose_ptr = |
| 1142 | reinterpret_cast<get_libc_fclose_ptr_fn_t>(dlsym(library_with_fclose, "get_libc_fclose_ptr")); |
| 1143 | ASSERT_TRUE(get_libc_fclose_ptr != nullptr) << dlerror(); |
| 1144 | ASSERT_EQ(expected_addr, get_libc_fclose_ptr()); |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1145 | |
Raj Mamadgi | 527229c | 2017-11-02 15:53:50 -0700 | [diff] [blame] | 1146 | dlclose(library_with_fclose); |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | |
Dmitriy Ivanov | ce44166 | 2014-06-17 15:56:38 -0700 | [diff] [blame] | 1150 | TEST(dlfcn, dlsym_weak_func) { |
| 1151 | dlerror(); |
Dmitriy Ivanov | bfa88bc | 2014-12-16 11:40:46 -0800 | [diff] [blame] | 1152 | void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW); |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 1153 | ASSERT_TRUE(handle != nullptr); |
Dmitriy Ivanov | ce44166 | 2014-06-17 15:56:38 -0700 | [diff] [blame] | 1154 | |
| 1155 | int (*weak_func)(); |
| 1156 | weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func")); |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 1157 | ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror(); |
Dmitriy Ivanov | ce44166 | 2014-06-17 15:56:38 -0700 | [diff] [blame] | 1158 | EXPECT_EQ(42, weak_func()); |
| 1159 | dlclose(handle); |
| 1160 | } |
| 1161 | |
Dmitriy Ivanov | bfa88bc | 2014-12-16 11:40:46 -0800 | [diff] [blame] | 1162 | TEST(dlfcn, dlopen_undefined_weak_func) { |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 1163 | void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW); |
| 1164 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1165 | int (*weak_func)(); |
| 1166 | weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func")); |
| 1167 | ASSERT_TRUE(weak_func != nullptr) << dlerror(); |
| 1168 | EXPECT_EQ(6551, weak_func()); |
| 1169 | dlclose(handle); |
Dmitriy Ivanov | bfa88bc | 2014-12-16 11:40:46 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
Dmitriy Ivanov | 7db1809 | 2014-05-08 12:27:25 -0700 | [diff] [blame] | 1172 | TEST(dlfcn, dlopen_symlink) { |
Dimitry Ivanov | 708589f | 2016-09-19 10:50:28 -0700 | [diff] [blame] | 1173 | DlfcnSymlink symlink("dlopen_symlink"); |
| 1174 | const std::string symlink_name = basename(symlink.get_symlink_path().c_str()); |
Dmitriy Ivanov | 7db1809 | 2014-05-08 12:27:25 -0700 | [diff] [blame] | 1175 | void* handle1 = dlopen("libdlext_test.so", RTLD_NOW); |
Dimitry Ivanov | 708589f | 2016-09-19 10:50:28 -0700 | [diff] [blame] | 1176 | void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW); |
Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 1177 | ASSERT_TRUE(handle1 != nullptr); |
| 1178 | ASSERT_TRUE(handle2 != nullptr); |
Dmitriy Ivanov | 7db1809 | 2014-05-08 12:27:25 -0700 | [diff] [blame] | 1179 | ASSERT_EQ(handle1, handle2); |
Dmitriy Ivanov | 319356e | 2014-09-02 17:31:44 -0700 | [diff] [blame] | 1180 | dlclose(handle1); |
| 1181 | dlclose(handle2); |
Dmitriy Ivanov | 7db1809 | 2014-05-08 12:27:25 -0700 | [diff] [blame] | 1182 | } |
Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 1183 | |
| 1184 | // libtest_dlopen_from_ctor_main.so depends on |
| 1185 | // libtest_dlopen_from_ctor.so which has a constructor |
| 1186 | // that calls dlopen(libc...). This is to test the situation |
| 1187 | // described in b/7941716. |
| 1188 | TEST(dlfcn, dlopen_dlopen_from_ctor) { |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1189 | #if defined(__GLIBC__) |
| 1190 | GTEST_SKIP() << "glibc segfaults if you try to call dlopen from a constructor"; |
| 1191 | #endif |
| 1192 | |
Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 1193 | void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW); |
| 1194 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1195 | dlclose(handle); |
Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 1196 | } |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 1197 | |
Dimitry Ivanov | ea8f396 | 2017-02-09 13:31:57 -0800 | [diff] [blame] | 1198 | static std::string g_fini_call_order_str; |
| 1199 | |
| 1200 | static void register_fini_call(const char* s) { |
| 1201 | g_fini_call_order_str += s; |
| 1202 | } |
| 1203 | |
Dimitry Ivanov | ec90e24 | 2017-02-10 11:04:20 -0800 | [diff] [blame] | 1204 | static void test_init_fini_call_order_for(const char* libname) { |
| 1205 | g_fini_call_order_str.clear(); |
| 1206 | void* handle = dlopen(libname, RTLD_NOW); |
Dimitry Ivanov | ea8f396 | 2017-02-09 13:31:57 -0800 | [diff] [blame] | 1207 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1208 | typedef int (*get_init_order_number_t)(); |
| 1209 | get_init_order_number_t get_init_order_number = |
| 1210 | reinterpret_cast<get_init_order_number_t>(dlsym(handle, "get_init_order_number")); |
| 1211 | ASSERT_EQ(321, get_init_order_number()); |
| 1212 | |
| 1213 | typedef void (*set_fini_callback_t)(void (*f)(const char*)); |
| 1214 | set_fini_callback_t set_fini_callback = |
| 1215 | reinterpret_cast<set_fini_callback_t>(dlsym(handle, "set_fini_callback")); |
| 1216 | set_fini_callback(register_fini_call); |
| 1217 | dlclose(handle); |
| 1218 | ASSERT_EQ("(root)(child)(grandchild)", g_fini_call_order_str); |
| 1219 | } |
| 1220 | |
Dimitry Ivanov | ec90e24 | 2017-02-10 11:04:20 -0800 | [diff] [blame] | 1221 | TEST(dlfcn, init_fini_call_order) { |
| 1222 | test_init_fini_call_order_for("libtest_init_fini_order_root.so"); |
| 1223 | test_init_fini_call_order_for("libtest_init_fini_order_root2.so"); |
| 1224 | } |
| 1225 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 1226 | TEST(dlfcn, symbol_versioning_use_v1) { |
| 1227 | void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW); |
| 1228 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1229 | typedef int (*fn_t)(); |
| 1230 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version")); |
| 1231 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 1232 | ASSERT_EQ(1, fn()); |
| 1233 | dlclose(handle); |
| 1234 | } |
| 1235 | |
| 1236 | TEST(dlfcn, symbol_versioning_use_v2) { |
| 1237 | void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW); |
| 1238 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1239 | typedef int (*fn_t)(); |
| 1240 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version")); |
| 1241 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 1242 | ASSERT_EQ(2, fn()); |
| 1243 | dlclose(handle); |
| 1244 | } |
| 1245 | |
| 1246 | TEST(dlfcn, symbol_versioning_use_other_v2) { |
| 1247 | void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW); |
| 1248 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1249 | typedef int (*fn_t)(); |
| 1250 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version")); |
| 1251 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 1252 | ASSERT_EQ(20, fn()); |
| 1253 | dlclose(handle); |
| 1254 | } |
| 1255 | |
| 1256 | TEST(dlfcn, symbol_versioning_use_other_v3) { |
| 1257 | void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW); |
| 1258 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1259 | typedef int (*fn_t)(); |
| 1260 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version")); |
| 1261 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 1262 | ASSERT_EQ(3, fn()); |
| 1263 | dlclose(handle); |
| 1264 | } |
| 1265 | |
| 1266 | TEST(dlfcn, symbol_versioning_default_via_dlsym) { |
| 1267 | void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW); |
| 1268 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1269 | typedef int (*fn_t)(); |
| 1270 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function")); |
| 1271 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 1272 | ASSERT_EQ(3, fn()); // the default version is 3 |
| 1273 | dlclose(handle); |
| 1274 | } |
| 1275 | |
Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 1276 | TEST(dlfcn, dlvsym_smoke) { |
| 1277 | void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW); |
| 1278 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1279 | typedef int (*fn_t)(); |
| 1280 | |
| 1281 | { |
| 1282 | fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion")); |
| 1283 | ASSERT_TRUE(fn == nullptr); |
| 1284 | ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror()); |
| 1285 | } |
| 1286 | |
| 1287 | { |
| 1288 | fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2")); |
| 1289 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 1290 | ASSERT_EQ(2, fn()); |
| 1291 | } |
| 1292 | |
| 1293 | dlclose(handle); |
| 1294 | } |
| 1295 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 1296 | // This preempts the implementation from libtest_versioned_lib.so |
| 1297 | extern "C" int version_zero_function() { |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | // This preempts the implementation from libtest_versioned_uselibv*.so |
| 1302 | extern "C" int version_zero_function2() { |
| 1303 | return 0; |
| 1304 | } |
Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 1305 | |
Lazar Trsic | 6f2d310 | 2015-10-13 16:43:00 +0200 | [diff] [blame] | 1306 | TEST(dlfcn, dt_runpath_smoke) { |
Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 1307 | void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW); |
| 1308 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1309 | |
| 1310 | typedef void *(* dlopen_b_fn)(); |
| 1311 | dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b"); |
| 1312 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 1313 | |
| 1314 | void *p = fn(); |
Evgenii Stepanov | 0cdef7e | 2015-07-06 17:56:31 -0700 | [diff] [blame] | 1315 | ASSERT_TRUE(p != nullptr); |
Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 1316 | |
| 1317 | dlclose(handle); |
| 1318 | } |
Lazar Trsic | 6f2d310 | 2015-10-13 16:43:00 +0200 | [diff] [blame] | 1319 | |
Dimitry Ivanov | db6ab3d | 2017-06-27 11:02:51 -0700 | [diff] [blame] | 1320 | TEST(dlfcn, dt_runpath_absolute_path) { |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 1321 | std::string libpath = GetTestlibRoot() + "/libtest_dt_runpath_d.so"; |
Dimitry Ivanov | db6ab3d | 2017-06-27 11:02:51 -0700 | [diff] [blame] | 1322 | void* handle = dlopen(libpath.c_str(), RTLD_NOW); |
| 1323 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1324 | |
| 1325 | typedef void *(* dlopen_b_fn)(); |
| 1326 | dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b"); |
| 1327 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 1328 | |
| 1329 | void *p = fn(); |
| 1330 | ASSERT_TRUE(p != nullptr); |
| 1331 | |
| 1332 | dlclose(handle); |
| 1333 | } |
| 1334 | |
dimitry | 55547db | 2018-05-25 14:17:37 +0200 | [diff] [blame] | 1335 | static void test_dlclose_after_thread_local_dtor(const char* library_name) { |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 1336 | bool is_dtor_triggered = false; |
| 1337 | |
| 1338 | auto f = [](void* handle, bool* is_dtor_triggered) { |
| 1339 | typedef void (*fn_t)(bool*); |
| 1340 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable")); |
| 1341 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 1342 | |
| 1343 | fn(is_dtor_triggered); |
| 1344 | |
| 1345 | ASSERT_TRUE(!*is_dtor_triggered); |
| 1346 | }; |
| 1347 | |
dimitry | 55547db | 2018-05-25 14:17:37 +0200 | [diff] [blame] | 1348 | void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD); |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 1349 | ASSERT_TRUE(handle == nullptr); |
| 1350 | |
dimitry | 55547db | 2018-05-25 14:17:37 +0200 | [diff] [blame] | 1351 | handle = dlopen(library_name, RTLD_NOW); |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 1352 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1353 | |
| 1354 | std::thread t(f, handle, &is_dtor_triggered); |
| 1355 | t.join(); |
| 1356 | |
| 1357 | ASSERT_TRUE(is_dtor_triggered); |
| 1358 | dlclose(handle); |
| 1359 | |
dimitry | 55547db | 2018-05-25 14:17:37 +0200 | [diff] [blame] | 1360 | handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD); |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 1361 | ASSERT_TRUE(handle == nullptr); |
| 1362 | } |
| 1363 | |
dimitry | 55547db | 2018-05-25 14:17:37 +0200 | [diff] [blame] | 1364 | TEST(dlfcn, dlclose_after_thread_local_dtor) { |
| 1365 | test_dlclose_after_thread_local_dtor("libtest_thread_local_dtor.so"); |
| 1366 | } |
| 1367 | |
| 1368 | TEST(dlfcn, dlclose_after_thread_local_dtor_indirect) { |
| 1369 | test_dlclose_after_thread_local_dtor("libtest_indirect_thread_local_dtor.so"); |
| 1370 | } |
| 1371 | |
| 1372 | static void test_dlclose_before_thread_local_dtor(const char* library_name) { |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 1373 | bool is_dtor_triggered = false; |
| 1374 | |
dimitry | 55547db | 2018-05-25 14:17:37 +0200 | [diff] [blame] | 1375 | auto f = [library_name](bool* is_dtor_triggered) { |
| 1376 | void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD); |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 1377 | ASSERT_TRUE(handle == nullptr); |
| 1378 | |
dimitry | 55547db | 2018-05-25 14:17:37 +0200 | [diff] [blame] | 1379 | handle = dlopen(library_name, RTLD_NOW); |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 1380 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1381 | |
| 1382 | typedef void (*fn_t)(bool*); |
| 1383 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable")); |
| 1384 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 1385 | |
| 1386 | fn(is_dtor_triggered); |
| 1387 | |
| 1388 | dlclose(handle); |
| 1389 | |
| 1390 | ASSERT_TRUE(!*is_dtor_triggered); |
| 1391 | |
| 1392 | // Since we have thread_atexit dtors associated with handle - the library should |
| 1393 | // still be availabe. |
dimitry | 55547db | 2018-05-25 14:17:37 +0200 | [diff] [blame] | 1394 | handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD); |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 1395 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1396 | dlclose(handle); |
| 1397 | }; |
| 1398 | |
dimitry | 55547db | 2018-05-25 14:17:37 +0200 | [diff] [blame] | 1399 | void* handle = dlopen(library_name, RTLD_NOW); |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 1400 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1401 | dlclose(handle); |
| 1402 | |
dimitry | 55547db | 2018-05-25 14:17:37 +0200 | [diff] [blame] | 1403 | handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD); |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 1404 | ASSERT_TRUE(handle == nullptr); |
| 1405 | |
| 1406 | std::thread t(f, &is_dtor_triggered); |
| 1407 | t.join(); |
| 1408 | #if defined(__BIONIC__) |
| 1409 | // ld-android.so unloads unreferenced libraries on pthread_exit() |
| 1410 | ASSERT_TRUE(is_dtor_triggered); |
dimitry | 55547db | 2018-05-25 14:17:37 +0200 | [diff] [blame] | 1411 | handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD); |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 1412 | ASSERT_TRUE(handle == nullptr); |
| 1413 | #else |
| 1414 | // GLIBC does not unload libraries with ref_count = 0 on pthread_exit |
| 1415 | ASSERT_TRUE(is_dtor_triggered); |
dimitry | 55547db | 2018-05-25 14:17:37 +0200 | [diff] [blame] | 1416 | handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD); |
| 1417 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1418 | #endif |
| 1419 | } |
| 1420 | |
| 1421 | TEST(dlfcn, dlclose_before_thread_local_dtor) { |
| 1422 | test_dlclose_before_thread_local_dtor("libtest_thread_local_dtor.so"); |
| 1423 | } |
| 1424 | |
| 1425 | TEST(dlfcn, dlclose_before_thread_local_dtor_indirect) { |
| 1426 | test_dlclose_before_thread_local_dtor("libtest_indirect_thread_local_dtor.so"); |
| 1427 | } |
| 1428 | |
| 1429 | TEST(dlfcn, dlclose_before_thread_local_dtor_multiple_dsos) { |
| 1430 | const constexpr char* library_name = "libtest_indirect_thread_local_dtor.so"; |
| 1431 | |
| 1432 | bool is_dtor1_triggered = false; |
| 1433 | bool is_dtor2_triggered = false; |
| 1434 | |
| 1435 | std::mutex mtx; |
| 1436 | std::condition_variable cv; |
| 1437 | void* library_handle = nullptr; |
| 1438 | bool thread1_dlopen_complete = false; |
| 1439 | bool thread2_thread_local_dtor_initialized = false; |
| 1440 | bool thread1_complete = false; |
| 1441 | |
| 1442 | auto f1 = [&]() { |
| 1443 | void* handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD); |
| 1444 | ASSERT_TRUE(handle == nullptr); |
| 1445 | |
| 1446 | handle = dlopen(library_name, RTLD_NOW); |
| 1447 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1448 | std::unique_lock<std::mutex> lock(mtx); |
| 1449 | thread1_dlopen_complete = true; |
| 1450 | library_handle = handle; |
| 1451 | lock.unlock(); |
| 1452 | cv.notify_one(); |
| 1453 | |
| 1454 | typedef void (*fn_t)(bool*); |
| 1455 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable")); |
| 1456 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 1457 | |
| 1458 | fn(&is_dtor1_triggered); |
| 1459 | |
| 1460 | lock.lock(); |
| 1461 | cv.wait(lock, [&] { return thread2_thread_local_dtor_initialized; }); |
| 1462 | lock.unlock(); |
| 1463 | |
| 1464 | dlclose(handle); |
| 1465 | |
| 1466 | ASSERT_TRUE(!is_dtor1_triggered); |
| 1467 | |
| 1468 | // Since we have thread_atexit dtors associated with handle - the library should |
| 1469 | // still be availabe. |
| 1470 | handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD); |
| 1471 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1472 | dlclose(handle); |
| 1473 | }; |
| 1474 | |
| 1475 | auto f2 = [&]() { |
| 1476 | std::unique_lock<std::mutex> lock(mtx); |
| 1477 | cv.wait(lock, [&] { return thread1_dlopen_complete; }); |
| 1478 | void* handle = library_handle; |
| 1479 | lock.unlock(); |
| 1480 | |
| 1481 | typedef void (*fn_t)(bool*); |
| 1482 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "init_thread_local_variable2")); |
| 1483 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 1484 | |
| 1485 | fn(&is_dtor2_triggered); |
| 1486 | |
| 1487 | lock.lock(); |
| 1488 | thread2_thread_local_dtor_initialized = true; |
| 1489 | lock.unlock(); |
| 1490 | cv.notify_one(); |
| 1491 | |
| 1492 | lock.lock(); |
| 1493 | cv.wait(lock, [&] { return thread1_complete; }); |
| 1494 | lock.unlock(); |
| 1495 | |
| 1496 | ASSERT_TRUE(!is_dtor2_triggered); |
| 1497 | }; |
| 1498 | |
| 1499 | void* handle = dlopen(library_name, RTLD_NOW); |
| 1500 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1501 | dlclose(handle); |
| 1502 | |
| 1503 | handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD); |
| 1504 | ASSERT_TRUE(handle == nullptr); |
| 1505 | |
| 1506 | std::thread t1(f1); |
| 1507 | std::thread t2(f2); |
| 1508 | t1.join(); |
| 1509 | ASSERT_TRUE(is_dtor1_triggered); |
| 1510 | ASSERT_TRUE(!is_dtor2_triggered); |
| 1511 | |
| 1512 | handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD); |
| 1513 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1514 | dlclose(handle); |
| 1515 | |
| 1516 | std::unique_lock<std::mutex> lock(mtx); |
| 1517 | thread1_complete = true; |
| 1518 | lock.unlock(); |
| 1519 | cv.notify_one(); |
| 1520 | |
| 1521 | t2.join(); |
| 1522 | ASSERT_TRUE(is_dtor2_triggered); |
| 1523 | |
| 1524 | #if defined(__BIONIC__) |
| 1525 | // ld-android.so unloads unreferenced libraries on pthread_exit() |
| 1526 | handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD); |
| 1527 | ASSERT_TRUE(handle == nullptr); |
| 1528 | #else |
| 1529 | // GLIBC does not unload libraries with ref_count = 0 on pthread_exit |
| 1530 | handle = dlopen(library_name, RTLD_NOW | RTLD_NOLOAD); |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 1531 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1532 | #endif |
| 1533 | } |
| 1534 | |
Elliott Hughes | 8ad4093 | 2017-06-15 15:12:29 -0700 | [diff] [blame] | 1535 | TEST(dlfcn, RTLD_macros) { |
| 1536 | #if !defined(RTLD_LOCAL) |
| 1537 | #error no RTLD_LOCAL |
| 1538 | #elif !defined(RTLD_LAZY) |
| 1539 | #error no RTLD_LAZY |
| 1540 | #elif !defined(RTLD_NOW) |
| 1541 | #error no RTLD_NOW |
| 1542 | #elif !defined(RTLD_NOLOAD) |
| 1543 | #error no RTLD_NOLOAD |
| 1544 | #elif !defined(RTLD_GLOBAL) |
| 1545 | #error no RTLD_GLOBAL |
| 1546 | #elif !defined(RTLD_NODELETE) |
| 1547 | #error no RTLD_NODELETE |
| 1548 | #endif |
| 1549 | } |
| 1550 | |
Dimitry Ivanov | a36e59b | 2016-09-01 11:37:39 -0700 | [diff] [blame] | 1551 | // Bionic specific tests |
| 1552 | #if defined(__BIONIC__) |
| 1553 | |
Ian Pedowitz | b6310c2 | 2018-01-18 16:26:19 -0800 | [diff] [blame] | 1554 | #if defined(__arm__) |
| 1555 | const llvm::ELF::Elf32_Dyn* to_dynamic_table(const char* p) { |
| 1556 | return reinterpret_cast<const llvm::ELF::Elf32_Dyn*>(p); |
| 1557 | } |
| 1558 | |
| 1559 | // Duplicate these definitions here because they are android specific |
Ryan Prichard | 6814028 | 2018-05-23 14:20:29 -0700 | [diff] [blame] | 1560 | // - note that we cannot include <elf.h> because #defines conflict with |
| 1561 | // enum names provided by LLVM. |
| 1562 | // - we also don't use llvm::ELF::DT_LOOS because its value is 0x60000000 |
| 1563 | // rather than the 0x6000000d we expect |
| 1564 | #define DT_LOOS 0x6000000d |
| 1565 | #define DT_ANDROID_REL (DT_LOOS + 2) |
| 1566 | #define DT_ANDROID_RELA (DT_LOOS + 4) |
Ian Pedowitz | b6310c2 | 2018-01-18 16:26:19 -0800 | [diff] [blame] | 1567 | |
| 1568 | template<typename ELFT> |
Ryan Prichard | 470b666 | 2018-03-27 22:10:55 -0700 | [diff] [blame] | 1569 | void validate_compatibility_of_native_library(const std::string& soname, |
| 1570 | const std::string& path, ELFT* elf) { |
Ian Pedowitz | b6310c2 | 2018-01-18 16:26:19 -0800 | [diff] [blame] | 1571 | bool has_elf_hash = false; |
| 1572 | bool has_android_rel = false; |
| 1573 | bool has_rel = false; |
| 1574 | // Find dynamic section and check that DT_HASH and there is no DT_ANDROID_REL |
| 1575 | for (auto it = elf->section_begin(); it != elf->section_end(); ++it) { |
| 1576 | const llvm::object::ELFSectionRef& section_ref = *it; |
| 1577 | if (section_ref.getType() == llvm::ELF::SHT_DYNAMIC) { |
| 1578 | llvm::StringRef data; |
| 1579 | ASSERT_TRUE(!it->getContents(data)) << "unable to get SHT_DYNAMIC section data"; |
| 1580 | for (auto d = to_dynamic_table(data.data()); d->d_tag != llvm::ELF::DT_NULL; ++d) { |
| 1581 | if (d->d_tag == llvm::ELF::DT_HASH) { |
| 1582 | has_elf_hash = true; |
| 1583 | } else if (d->d_tag == DT_ANDROID_REL || d->d_tag == DT_ANDROID_RELA) { |
| 1584 | has_android_rel = true; |
| 1585 | } else if (d->d_tag == llvm::ELF::DT_REL || d->d_tag == llvm::ELF::DT_RELA) { |
| 1586 | has_rel = true; |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | break; |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | ASSERT_TRUE(has_elf_hash) << path.c_str() << ": missing elf hash (DT_HASH)"; |
| 1595 | ASSERT_TRUE(!has_android_rel) << path.c_str() << ": has packed relocations"; |
Ryan Prichard | 470b666 | 2018-03-27 22:10:55 -0700 | [diff] [blame] | 1596 | // libdl.so is simple enough that it might not have any relocations, so |
| 1597 | // exempt it from the DT_REL/DT_RELA check. |
| 1598 | if (soname != "libdl.so") { |
| 1599 | ASSERT_TRUE(has_rel) << path.c_str() << ": missing DT_REL/DT_RELA"; |
| 1600 | } |
Ian Pedowitz | b6310c2 | 2018-01-18 16:26:19 -0800 | [diff] [blame] | 1601 | } |
| 1602 | |
Ryan Prichard | 470b666 | 2018-03-27 22:10:55 -0700 | [diff] [blame] | 1603 | void validate_compatibility_of_native_library(const std::string& soname) { |
Ian Pedowitz | b6310c2 | 2018-01-18 16:26:19 -0800 | [diff] [blame] | 1604 | // On the systems with emulation system libraries would be of different |
| 1605 | // architecture. Try to use alternate paths first. |
| 1606 | std::string path = std::string(ALTERNATE_PATH_TO_SYSTEM_LIB) + soname; |
| 1607 | auto binary_or_error = llvm::object::createBinary(path); |
| 1608 | if (!binary_or_error) { |
| 1609 | path = std::string(PATH_TO_SYSTEM_LIB) + soname; |
| 1610 | binary_or_error = llvm::object::createBinary(path); |
| 1611 | } |
| 1612 | ASSERT_FALSE(!binary_or_error); |
| 1613 | |
| 1614 | llvm::object::Binary* binary = binary_or_error.get().getBinary(); |
| 1615 | |
| 1616 | auto obj = llvm::dyn_cast<llvm::object::ObjectFile>(binary); |
| 1617 | ASSERT_TRUE(obj != nullptr); |
| 1618 | |
| 1619 | auto elf = llvm::dyn_cast<llvm::object::ELF32LEObjectFile>(obj); |
| 1620 | |
| 1621 | ASSERT_TRUE(elf != nullptr); |
| 1622 | |
Ryan Prichard | 470b666 | 2018-03-27 22:10:55 -0700 | [diff] [blame] | 1623 | validate_compatibility_of_native_library(soname, path, elf); |
Ian Pedowitz | b6310c2 | 2018-01-18 16:26:19 -0800 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | // This is a test for app compatibility workaround for arm apps |
| 1627 | // affected by http://b/24465209 |
| 1628 | TEST(dlext, compat_elf_hash_and_relocation_tables) { |
| 1629 | validate_compatibility_of_native_library("libc.so"); |
| 1630 | validate_compatibility_of_native_library("liblog.so"); |
| 1631 | validate_compatibility_of_native_library("libstdc++.so"); |
| 1632 | validate_compatibility_of_native_library("libdl.so"); |
| 1633 | validate_compatibility_of_native_library("libm.so"); |
| 1634 | validate_compatibility_of_native_library("libz.so"); |
| 1635 | validate_compatibility_of_native_library("libjnigraphics.so"); |
| 1636 | } |
| 1637 | |
| 1638 | #endif // defined(__arm__) |
| 1639 | |
Dimitry Ivanov | 9700bab | 2016-08-10 18:54:06 -0700 | [diff] [blame] | 1640 | TEST(dlfcn, dlopen_invalid_rw_load_segment) { |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 1641 | const std::string libpath = GetTestlibRoot() + |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 1642 | "/" + kPrebuiltElfDir + |
| 1643 | "/libtest_invalid-rw_load_segment.so"; |
Dimitry Ivanov | 9700bab | 2016-08-10 18:54:06 -0700 | [diff] [blame] | 1644 | void* handle = dlopen(libpath.c_str(), RTLD_NOW); |
| 1645 | ASSERT_TRUE(handle == nullptr); |
Elliott Hughes | 9076b0c | 2018-02-28 11:29:45 -0800 | [diff] [blame] | 1646 | std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W+E load segments are not allowed"; |
Dimitry Ivanov | 9700bab | 2016-08-10 18:54:06 -0700 | [diff] [blame] | 1647 | ASSERT_STREQ(expected_dlerror.c_str(), dlerror()); |
| 1648 | } |
Dimitry Ivanov | 972e3d0 | 2016-08-12 14:25:50 -0700 | [diff] [blame] | 1649 | |
| 1650 | TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) { |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 1651 | const std::string libpath = GetTestlibRoot() + |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 1652 | "/" + kPrebuiltElfDir + |
| 1653 | "/libtest_invalid-unaligned_shdr_offset.so"; |
| 1654 | |
Dimitry Ivanov | 972e3d0 | 2016-08-12 14:25:50 -0700 | [diff] [blame] | 1655 | void* handle = dlopen(libpath.c_str(), RTLD_NOW); |
| 1656 | ASSERT_TRUE(handle == nullptr); |
| 1657 | std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: "; |
| 1658 | ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror()); |
| 1659 | } |
| 1660 | |
Dimitry Ivanov | cb86c31 | 2016-08-12 15:28:42 -0700 | [diff] [blame] | 1661 | TEST(dlfcn, dlopen_invalid_zero_shentsize) { |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 1662 | const std::string libpath = GetTestlibRoot() + |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 1663 | "/" + kPrebuiltElfDir + |
| 1664 | "/libtest_invalid-zero_shentsize.so"; |
| 1665 | |
Dimitry Ivanov | cb86c31 | 2016-08-12 15:28:42 -0700 | [diff] [blame] | 1666 | void* handle = dlopen(libpath.c_str(), RTLD_NOW); |
| 1667 | ASSERT_TRUE(handle == nullptr); |
| 1668 | std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x"; |
| 1669 | ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror()); |
| 1670 | } |
| 1671 | |
Dimitry Ivanov | c9a9561 | 2016-08-15 10:27:47 -0700 | [diff] [blame] | 1672 | TEST(dlfcn, dlopen_invalid_zero_shstrndx) { |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 1673 | const std::string libpath = GetTestlibRoot() + |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 1674 | "/" + kPrebuiltElfDir + |
| 1675 | "/libtest_invalid-zero_shstrndx.so"; |
| 1676 | |
Dimitry Ivanov | c9a9561 | 2016-08-15 10:27:47 -0700 | [diff] [blame] | 1677 | void* handle = dlopen(libpath.c_str(), RTLD_NOW); |
| 1678 | ASSERT_TRUE(handle == nullptr); |
| 1679 | std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx"; |
| 1680 | ASSERT_STREQ(expected_dlerror.c_str(), dlerror()); |
| 1681 | } |
| 1682 | |
Dimitry Ivanov | 8bdf70e | 2016-08-15 11:30:45 -0700 | [diff] [blame] | 1683 | TEST(dlfcn, dlopen_invalid_empty_shdr_table) { |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 1684 | const std::string libpath = GetTestlibRoot() + |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 1685 | "/" + kPrebuiltElfDir + |
| 1686 | "/libtest_invalid-empty_shdr_table.so"; |
| 1687 | |
Dimitry Ivanov | 8bdf70e | 2016-08-15 11:30:45 -0700 | [diff] [blame] | 1688 | void* handle = dlopen(libpath.c_str(), RTLD_NOW); |
| 1689 | ASSERT_TRUE(handle == nullptr); |
| 1690 | std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers"; |
| 1691 | ASSERT_STREQ(expected_dlerror.c_str(), dlerror()); |
| 1692 | } |
| 1693 | |
Dimitry Ivanov | 4623044 | 2016-08-15 13:40:53 -0700 | [diff] [blame] | 1694 | TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) { |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 1695 | const std::string libpath = GetTestlibRoot() + |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 1696 | "/" + kPrebuiltElfDir + |
| 1697 | "/libtest_invalid-zero_shdr_table_offset.so"; |
| 1698 | |
Dimitry Ivanov | 4623044 | 2016-08-15 13:40:53 -0700 | [diff] [blame] | 1699 | void* handle = dlopen(libpath.c_str(), RTLD_NOW); |
| 1700 | ASSERT_TRUE(handle == nullptr); |
| 1701 | std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/"; |
| 1702 | ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror()); |
| 1703 | } |
| 1704 | |
Dimitry Ivanov | 5595834 | 2016-08-15 14:06:04 -0700 | [diff] [blame] | 1705 | TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) { |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 1706 | const std::string libpath = GetTestlibRoot() + |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 1707 | "/" + kPrebuiltElfDir + |
| 1708 | "/libtest_invalid-zero_shdr_table_content.so"; |
| 1709 | |
Dimitry Ivanov | 5595834 | 2016-08-15 14:06:04 -0700 | [diff] [blame] | 1710 | void* handle = dlopen(libpath.c_str(), RTLD_NOW); |
| 1711 | ASSERT_TRUE(handle == nullptr); |
| 1712 | std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found"; |
| 1713 | ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror()); |
| 1714 | } |
| 1715 | |
Dimitry Ivanov | 816676e | 2016-10-19 11:00:28 -0700 | [diff] [blame] | 1716 | TEST(dlfcn, dlopen_invalid_textrels) { |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 1717 | const std::string libpath = GetTestlibRoot() + |
Dimitry Ivanov | 816676e | 2016-10-19 11:00:28 -0700 | [diff] [blame] | 1718 | "/" + kPrebuiltElfDir + |
| 1719 | "/libtest_invalid-textrels.so"; |
| 1720 | |
| 1721 | void* handle = dlopen(libpath.c_str(), RTLD_NOW); |
| 1722 | ASSERT_TRUE(handle == nullptr); |
| 1723 | std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations"; |
| 1724 | ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror()); |
| 1725 | } |
| 1726 | |
| 1727 | TEST(dlfcn, dlopen_invalid_textrels2) { |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 1728 | const std::string libpath = GetTestlibRoot() + |
Dimitry Ivanov | 816676e | 2016-10-19 11:00:28 -0700 | [diff] [blame] | 1729 | "/" + kPrebuiltElfDir + |
| 1730 | "/libtest_invalid-textrels2.so"; |
| 1731 | |
| 1732 | void* handle = dlopen(libpath.c_str(), RTLD_NOW); |
| 1733 | ASSERT_TRUE(handle == nullptr); |
| 1734 | std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has text relocations"; |
| 1735 | ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror()); |
| 1736 | } |
| 1737 | |
Jiyong Park | 01162f2 | 2017-10-16 15:31:09 +0900 | [diff] [blame] | 1738 | TEST(dlfcn, dlopen_df_1_global) { |
| 1739 | void* handle = dlopen("libtest_dlopen_df_1_global.so", RTLD_NOW); |
| 1740 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1741 | } |
| 1742 | |
Peter Collingbourne | b39cb3c | 2019-03-01 13:12:49 -0800 | [diff] [blame] | 1743 | TEST(dlfcn, segment_gap) { |
| 1744 | void* handle = dlopen("libsegment_gap_outer.so", RTLD_NOW); |
| 1745 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1746 | |
| 1747 | auto get_inner = reinterpret_cast<void* (*)()>(dlsym(handle, "get_inner")); |
| 1748 | void* inner = get_inner(); |
| 1749 | (void)inner; |
| 1750 | |
| 1751 | #if __arm__ |
| 1752 | int count; |
| 1753 | _Unwind_Ptr outer_exidx = dl_unwind_find_exidx(reinterpret_cast<_Unwind_Ptr>(get_inner), &count); |
| 1754 | _Unwind_Ptr inner_exidx = dl_unwind_find_exidx(reinterpret_cast<_Unwind_Ptr>(inner), &count); |
| 1755 | EXPECT_NE(0u, outer_exidx); |
| 1756 | EXPECT_NE(0u, inner_exidx); |
| 1757 | EXPECT_NE(inner_exidx, outer_exidx); |
| 1758 | #endif |
| 1759 | |
| 1760 | Dl_info info; |
| 1761 | int rc = dladdr(inner, &info); |
| 1762 | ASSERT_NE(rc, 0); |
| 1763 | |
| 1764 | EXPECT_NE(nullptr, strstr(info.dli_fname, "libsegment_gap_inner.so")); |
| 1765 | } |
| 1766 | |
Dimitry Ivanov | 9700bab | 2016-08-10 18:54:06 -0700 | [diff] [blame] | 1767 | #endif |