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