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