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