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