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