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