| 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 <libgen.h> | 
|  | 21 | #include <limits.h> | 
|  | 22 | #include <stdio.h> | 
|  | 23 | #include <stdint.h> | 
|  | 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> | 
| jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 28 |  | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 29 | #include "utils.h" | 
|  | 30 |  | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 31 | #define ASSERT_SUBSTR(needle, haystack) \ | 
|  | 32 | ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack) | 
|  | 33 |  | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 34 |  | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 35 | static bool g_called = false; | 
| jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 36 | extern "C" void DlSymTestFunction() { | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 37 | g_called = true; | 
| jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 38 | } | 
|  | 39 |  | 
| Dmitriy Ivanov | f8846a4 | 2014-07-08 21:21:34 -0700 | [diff] [blame] | 40 | static int g_ctor_function_called = 0; | 
|  | 41 |  | 
|  | 42 | extern "C" void ctor_function() __attribute__ ((constructor)); | 
|  | 43 |  | 
|  | 44 | extern "C" void ctor_function() { | 
|  | 45 | g_ctor_function_called = 17; | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | TEST(dlfcn, ctor_function_call) { | 
|  | 49 | ASSERT_EQ(17, g_ctor_function_called); | 
|  | 50 | } | 
|  | 51 |  | 
| Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 52 | TEST(dlfcn, dlsym_in_executable) { | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 53 | dlerror(); // Clear any pending errors. | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 54 | void* self = dlopen(nullptr, RTLD_NOW); | 
|  | 55 | ASSERT_TRUE(self != nullptr); | 
|  | 56 | ASSERT_TRUE(dlerror() == nullptr); | 
| jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 57 |  | 
|  | 58 | void* sym = dlsym(self, "DlSymTestFunction"); | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 59 | ASSERT_TRUE(sym != nullptr); | 
| jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 60 |  | 
|  | 61 | void (*function)() = reinterpret_cast<void(*)()>(sym); | 
|  | 62 |  | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 63 | g_called = false; | 
| jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 64 | function(); | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 65 | ASSERT_TRUE(g_called); | 
| Elliott Hughes | 1a69616 | 2012-11-01 13:49:32 -0700 | [diff] [blame] | 66 |  | 
|  | 67 | ASSERT_EQ(0, dlclose(self)); | 
| jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 68 | } | 
| Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 69 |  | 
| Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 70 | TEST(dlfcn, dlsym_from_sofile) { | 
|  | 71 | void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_LAZY | RTLD_LOCAL); | 
|  | 72 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 73 |  | 
| Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 74 | // 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] | 75 | void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol"); | 
|  | 76 | ASSERT_TRUE(symbol == nullptr); | 
|  | 77 | ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror()); | 
|  | 78 |  | 
|  | 79 | typedef int* (*fn_t)(); | 
| Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 80 | fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT = | 
|  | 81 | reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT")); | 
|  | 82 | ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror(); | 
| Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 83 |  | 
| Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 84 | int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT(); | 
| Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 85 | ASSERT_TRUE(ptr != nullptr) << dlerror(); | 
|  | 86 | ASSERT_EQ(42, *ptr); | 
|  | 87 |  | 
| Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 88 | fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT = | 
|  | 89 | reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT")); | 
|  | 90 | ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror(); | 
|  | 91 |  | 
|  | 92 | ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT(); | 
|  | 93 | ASSERT_TRUE(ptr != nullptr) << dlerror(); | 
|  | 94 | ASSERT_EQ(44, *ptr); | 
|  | 95 |  | 
|  | 96 | fn_t lookup_dlsym_symbol_using_RTLD_NEXT = | 
|  | 97 | reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT")); | 
|  | 98 | ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror(); | 
|  | 99 |  | 
|  | 100 | ptr = lookup_dlsym_symbol_using_RTLD_NEXT(); | 
|  | 101 | ASSERT_TRUE(ptr != nullptr) << dlerror(); | 
|  | 102 | ASSERT_EQ(43, *ptr); | 
|  | 103 |  | 
| Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 104 | dlclose(handle); | 
|  | 105 | } | 
|  | 106 |  | 
| Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 107 | TEST(dlfcn, dlsym_from_sofile_with_preload) { | 
|  | 108 | void* preload = dlopen("libtest_dlsym_from_this_grandchild.so", RTLD_NOW | RTLD_LOCAL); | 
|  | 109 | ASSERT_TRUE(preload != nullptr) << dlerror(); | 
|  | 110 |  | 
|  | 111 | void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL); | 
|  | 112 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 113 |  | 
|  | 114 | // check that we can't find '_test_dlsym_symbol' via dlsym(RTLD_DEFAULT) | 
|  | 115 | void* symbol = dlsym(RTLD_DEFAULT, "test_dlsym_symbol"); | 
|  | 116 | ASSERT_TRUE(symbol == nullptr); | 
|  | 117 | ASSERT_SUBSTR("undefined symbol: test_dlsym_symbol", dlerror()); | 
|  | 118 |  | 
|  | 119 | typedef int* (*fn_t)(); | 
|  | 120 | fn_t lookup_dlsym_symbol_using_RTLD_DEFAULT = | 
|  | 121 | reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_DEFAULT")); | 
|  | 122 | ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_DEFAULT != nullptr) << dlerror(); | 
|  | 123 |  | 
|  | 124 | int* ptr = lookup_dlsym_symbol_using_RTLD_DEFAULT(); | 
|  | 125 | ASSERT_TRUE(ptr != nullptr) << dlerror(); | 
|  | 126 | ASSERT_EQ(42, *ptr); | 
|  | 127 |  | 
|  | 128 | fn_t lookup_dlsym_symbol2_using_RTLD_DEFAULT = | 
|  | 129 | reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol2_using_RTLD_DEFAULT")); | 
|  | 130 | ASSERT_TRUE(lookup_dlsym_symbol2_using_RTLD_DEFAULT != nullptr) << dlerror(); | 
|  | 131 |  | 
|  | 132 | ptr = lookup_dlsym_symbol2_using_RTLD_DEFAULT(); | 
|  | 133 | ASSERT_TRUE(ptr != nullptr) << dlerror(); | 
|  | 134 | ASSERT_EQ(44, *ptr); | 
|  | 135 |  | 
|  | 136 | fn_t lookup_dlsym_symbol_using_RTLD_NEXT = | 
|  | 137 | reinterpret_cast<fn_t>(dlsym(handle, "lookup_dlsym_symbol_using_RTLD_NEXT")); | 
|  | 138 | ASSERT_TRUE(lookup_dlsym_symbol_using_RTLD_NEXT != nullptr) << dlerror(); | 
|  | 139 |  | 
|  | 140 | ptr = lookup_dlsym_symbol_using_RTLD_NEXT(); | 
|  | 141 | ASSERT_TRUE(ptr != nullptr) << dlerror(); | 
|  | 142 | ASSERT_EQ(43, *ptr); | 
|  | 143 |  | 
|  | 144 | dlclose(handle); | 
|  | 145 | dlclose(preload); | 
|  | 146 | } | 
|  | 147 |  | 
| Dmitriy Ivanov | f439b5a | 2015-05-30 13:04:39 -0700 | [diff] [blame] | 148 | TEST(dlfcn, dlsym_handle_global_sym) { | 
|  | 149 | // check that we do not look into global group | 
|  | 150 | // when looking up symbol by handle | 
|  | 151 | void* handle = dlopen("libtest_empty.so", RTLD_NOW); | 
|  | 152 | dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL); | 
|  | 153 | void* sym = dlsym(handle, "getRandomNumber"); | 
|  | 154 | ASSERT_TRUE(sym == nullptr); | 
|  | 155 | ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror()); | 
|  | 156 |  | 
|  | 157 | sym = dlsym(handle, "DlSymTestFunction"); | 
|  | 158 | ASSERT_TRUE(sym == nullptr); | 
|  | 159 | ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror()); | 
|  | 160 | dlclose(handle); | 
|  | 161 | } | 
|  | 162 |  | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 163 | TEST(dlfcn, dlsym_with_dependencies) { | 
|  | 164 | void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW); | 
| Dmitriy Ivanov | f439b5a | 2015-05-30 13:04:39 -0700 | [diff] [blame] | 165 | ASSERT_TRUE(handle != nullptr); | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 166 | dlerror(); | 
|  | 167 | // This symbol is in DT_NEEDED library. | 
|  | 168 | void* sym = dlsym(handle, "getRandomNumber"); | 
| Dmitriy Ivanov | f439b5a | 2015-05-30 13:04:39 -0700 | [diff] [blame] | 169 | ASSERT_TRUE(sym != nullptr) << dlerror(); | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 170 | int (*fn)(void); | 
|  | 171 | fn = reinterpret_cast<int (*)(void)>(sym); | 
|  | 172 | EXPECT_EQ(4, fn()); | 
|  | 173 | dlclose(handle); | 
|  | 174 | } | 
|  | 175 |  | 
| Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 176 | TEST(dlfcn, dlopen_noload) { | 
|  | 177 | void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD); | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 178 | ASSERT_TRUE(handle == nullptr); | 
| Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 179 | handle = dlopen("libtest_simple.so", RTLD_NOW); | 
|  | 180 | void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD); | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 181 | ASSERT_TRUE(handle != nullptr); | 
|  | 182 | ASSERT_TRUE(handle2 != nullptr); | 
| Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 183 | ASSERT_TRUE(handle == handle2); | 
|  | 184 | ASSERT_EQ(0, dlclose(handle)); | 
|  | 185 | ASSERT_EQ(0, dlclose(handle2)); | 
|  | 186 | } | 
|  | 187 |  | 
| Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 188 | TEST(dlfcn, dlopen_by_soname) { | 
|  | 189 | static const char* soname = "libdlext_test_soname.so"; | 
|  | 190 | static const char* filename = "libdlext_test_different_soname.so"; | 
|  | 191 | // 1. Make sure there is no library with soname in default search path | 
|  | 192 | void* handle = dlopen(soname, RTLD_NOW); | 
|  | 193 | ASSERT_TRUE(handle == nullptr); | 
|  | 194 |  | 
|  | 195 | // 2. Load a library using filename | 
|  | 196 | handle = dlopen(filename, RTLD_NOW); | 
|  | 197 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 198 |  | 
|  | 199 | // 3. Find library by soname | 
|  | 200 | void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD); | 
|  | 201 | ASSERT_TRUE(handle_soname != nullptr) << dlerror(); | 
|  | 202 | ASSERT_EQ(handle, handle_soname); | 
|  | 203 |  | 
|  | 204 | // 4. RTLD_NOLOAD should still work with filename | 
|  | 205 | void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD); | 
|  | 206 | ASSERT_TRUE(handle_filename != nullptr) << dlerror(); | 
|  | 207 | ASSERT_EQ(handle, handle_filename); | 
|  | 208 |  | 
|  | 209 | dlclose(handle_filename); | 
|  | 210 | dlclose(handle_soname); | 
|  | 211 | dlclose(handle); | 
|  | 212 | } | 
|  | 213 |  | 
| Dimitry Ivanov | 0a2ab02 | 2016-04-05 17:12:01 -0700 | [diff] [blame] | 214 | // mips doesn't support ifuncs | 
|  | 215 | #if !defined(__mips__) | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 216 | TEST(dlfcn, ifunc) { | 
| Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 217 | typedef const char* (*fn_ptr)(); | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 218 |  | 
|  | 219 | // ifunc's choice depends on whether IFUNC_CHOICE has a value | 
|  | 220 | // first check the set case | 
|  | 221 | setenv("IFUNC_CHOICE", "set", 1); | 
|  | 222 | void* handle = dlopen("libtest_ifunc.so", RTLD_NOW); | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 223 | ASSERT_TRUE(handle != nullptr); | 
| Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 224 | fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo")); | 
|  | 225 | 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] | 226 | ASSERT_TRUE(foo_ptr != nullptr); | 
|  | 227 | ASSERT_TRUE(foo_library_ptr != nullptr); | 
| Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 228 | ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0); | 
|  | 229 | ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0); | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 230 | dlclose(handle); | 
|  | 231 |  | 
|  | 232 | // then check the unset case | 
|  | 233 | unsetenv("IFUNC_CHOICE"); | 
|  | 234 | handle = dlopen("libtest_ifunc.so", RTLD_NOW); | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 235 | ASSERT_TRUE(handle != nullptr); | 
| Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 236 | foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo")); | 
|  | 237 | foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library")); | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 238 | ASSERT_TRUE(foo_ptr != nullptr); | 
|  | 239 | ASSERT_TRUE(foo_library_ptr != nullptr); | 
| Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 240 | ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0); | 
|  | 241 | ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0); | 
|  | 242 | dlclose(handle); | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | TEST(dlfcn, ifunc_ctor_call) { | 
|  | 246 | typedef const char* (*fn_ptr)(); | 
|  | 247 |  | 
|  | 248 | void* handle = dlopen("libtest_ifunc.so", RTLD_NOW); | 
| Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 249 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 250 | fn_ptr is_ctor_called =  reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative")); | 
|  | 251 | ASSERT_TRUE(is_ctor_called != nullptr) << dlerror(); | 
|  | 252 | ASSERT_STREQ("false", is_ctor_called()); | 
|  | 253 |  | 
|  | 254 | is_ctor_called =  reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot")); | 
|  | 255 | ASSERT_TRUE(is_ctor_called != nullptr) << dlerror(); | 
| Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 256 | ASSERT_STREQ("true", is_ctor_called()); | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 257 | dlclose(handle); | 
|  | 258 | } | 
| Dimitry Ivanov | ba35b2d | 2016-04-08 11:47:53 -0700 | [diff] [blame] | 259 |  | 
|  | 260 | TEST(dlfcn, ifunc_ctor_call_rtld_lazy) { | 
|  | 261 | typedef const char* (*fn_ptr)(); | 
|  | 262 |  | 
|  | 263 | void* handle = dlopen("libtest_ifunc.so", RTLD_LAZY); | 
|  | 264 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 265 | fn_ptr is_ctor_called =  reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative")); | 
|  | 266 | ASSERT_TRUE(is_ctor_called != nullptr) << dlerror(); | 
|  | 267 | ASSERT_STREQ("false", is_ctor_called()); | 
|  | 268 |  | 
|  | 269 | is_ctor_called =  reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot")); | 
|  | 270 | ASSERT_TRUE(is_ctor_called != nullptr) << dlerror(); | 
|  | 271 | ASSERT_STREQ("true", is_ctor_called()); | 
|  | 272 | dlclose(handle); | 
|  | 273 | } | 
| Dimitry Ivanov | 0a2ab02 | 2016-04-05 17:12:01 -0700 | [diff] [blame] | 274 | #endif | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 275 |  | 
| Dmitriy Ivanov | b2a30ee | 2014-09-04 18:23:00 -0700 | [diff] [blame] | 276 | TEST(dlfcn, dlopen_check_relocation_dt_needed_order) { | 
|  | 277 | // This is the structure of the test library and | 
|  | 278 | // its dt_needed libraries | 
|  | 279 | // libtest_relo_check_dt_needed_order.so | 
|  | 280 | // | | 
|  | 281 | // +-> libtest_relo_check_dt_needed_order_1.so | 
|  | 282 | // | | 
|  | 283 | // +-> libtest_relo_check_dt_needed_order_2.so | 
|  | 284 | // | 
|  | 285 | // The root library references relo_test_get_answer_lib - which is defined | 
|  | 286 | // in both dt_needed libraries, the correct relocation should | 
|  | 287 | // use the function defined in libtest_relo_check_dt_needed_order_1.so | 
|  | 288 | void* handle = nullptr; | 
| Dmitriy Ivanov | d9ff722 | 2014-09-08 16:22:22 -0700 | [diff] [blame] | 289 | auto guard = make_scope_guard([&]() { | 
| Dmitriy Ivanov | b2a30ee | 2014-09-04 18:23:00 -0700 | [diff] [blame] | 290 | dlclose(handle); | 
|  | 291 | }); | 
|  | 292 |  | 
|  | 293 | handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW); | 
|  | 294 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 295 |  | 
|  | 296 | typedef int (*fn_t) (void); | 
|  | 297 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer")); | 
|  | 298 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 299 | ASSERT_EQ(1, fn()); | 
|  | 300 | } | 
|  | 301 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 302 | TEST(dlfcn, dlopen_check_order_dlsym) { | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 303 | // Here is how the test library and its dt_needed | 
|  | 304 | // libraries are arranged | 
|  | 305 | // | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 306 | //  libtest_check_order_children.so | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 307 | //  | | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 308 | //  +-> ..._1_left.so | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 309 | //  |   | | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 310 | //  |   +-> ..._a.so | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 311 | //  |   | | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 312 | //  |   +-> ...r_b.so | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 313 | //  | | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 314 | //  +-> ..._2_right.so | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 315 | //  |   | | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 316 | //  |   +-> ..._d.so | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 317 | //  |       | | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 318 | //  |       +-> ..._b.so | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 319 | //  | | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 320 | //  +-> ..._3_c.so | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 321 | // | 
|  | 322 | //  load order should be (1, 2, 3, a, b, d) | 
|  | 323 | // | 
|  | 324 | // get_answer() is defined in (2, 3, a, b, c) | 
|  | 325 | // get_answer2() is defined in (b, d) | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 326 | void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"); | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 327 | ASSERT_TRUE(sym == nullptr); | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 328 | void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL); | 
|  | 329 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 330 | typedef int (*fn_t) (void); | 
|  | 331 | fn_t fn, fn2; | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 332 | 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] | 333 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 334 | 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] | 335 | ASSERT_TRUE(fn2 != nullptr) << dlerror(); | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 336 |  | 
|  | 337 | ASSERT_EQ(42, fn()); | 
|  | 338 | ASSERT_EQ(43, fn2()); | 
|  | 339 | dlclose(handle); | 
|  | 340 | } | 
|  | 341 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 342 | TEST(dlfcn, dlopen_check_order_reloc_siblings) { | 
|  | 343 | // This is how this one works: | 
|  | 344 | // we lookup and call get_answer which is defined in '_2.so' | 
|  | 345 | // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so' | 
|  | 346 | // the correct _impl() is implemented by '_a.so'; | 
|  | 347 | // | 
|  | 348 | // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?) | 
|  | 349 | // | 
|  | 350 | // Here is the picture: | 
|  | 351 | // | 
|  | 352 | // libtest_check_order_reloc_siblings.so | 
|  | 353 | // | | 
|  | 354 | // +-> ..._1.so <- empty | 
|  | 355 | // |   | | 
|  | 356 | // |   +-> ..._a.so <- exports correct answer_impl() | 
|  | 357 | // |   | | 
|  | 358 | // |   +-> ..._b.so <- every other letter exporting incorrect one. | 
|  | 359 | // | | 
|  | 360 | // +-> ..._2.so <- empty | 
|  | 361 | // |   | | 
|  | 362 | // |   +-> ..._c.so | 
|  | 363 | // |   | | 
|  | 364 | // |   +-> ..._d.so | 
|  | 365 | // | | 
|  | 366 | // +-> ..._3.so <- empty | 
|  | 367 | //     | | 
|  | 368 | //     +-> ..._e.so | 
|  | 369 | //     | | 
|  | 370 | //     +-> ..._f.so <- exports get_answer() that calls get_anser_impl(); | 
|  | 371 | //                     implements incorrect get_answer_impl() | 
|  | 372 |  | 
|  | 373 | void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD); | 
|  | 374 | ASSERT_TRUE(handle == nullptr); | 
|  | 375 | #ifdef __BIONIC__ | 
|  | 376 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? | 
|  | 377 | ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); | 
|  | 378 | #endif | 
|  | 379 |  | 
|  | 380 | handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL); | 
|  | 381 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 382 |  | 
|  | 383 | typedef int (*fn_t) (void); | 
|  | 384 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer")); | 
|  | 385 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 386 | ASSERT_EQ(42, fn()); | 
|  | 387 |  | 
|  | 388 | ASSERT_EQ(0, dlclose(handle)); | 
|  | 389 | } | 
|  | 390 |  | 
|  | 391 | TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) { | 
|  | 392 | // This test uses the same library as dlopen_check_order_reloc_siblings. | 
|  | 393 | // Unlike dlopen_check_order_reloc_siblings it preloads | 
|  | 394 | // libtest_check_order_reloc_siblings_1.so (first dependency) prior to | 
|  | 395 | // dlopen(libtest_check_order_reloc_siblings.so) | 
|  | 396 |  | 
|  | 397 | void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD); | 
|  | 398 | ASSERT_TRUE(handle == nullptr); | 
|  | 399 | handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD); | 
|  | 400 | ASSERT_TRUE(handle == nullptr); | 
|  | 401 |  | 
|  | 402 | void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL); | 
|  | 403 | ASSERT_TRUE(handle_for_1 != nullptr) << dlerror(); | 
|  | 404 |  | 
|  | 405 | handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL); | 
|  | 406 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 407 |  | 
|  | 408 | ASSERT_EQ(0, dlclose(handle_for_1)); | 
|  | 409 |  | 
|  | 410 | typedef int (*fn_t) (void); | 
|  | 411 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer")); | 
|  | 412 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 413 | ASSERT_EQ(42, fn()); | 
|  | 414 |  | 
|  | 415 | ASSERT_EQ(0, dlclose(handle)); | 
|  | 416 | } | 
|  | 417 |  | 
| Dmitriy Ivanov | 7699d13 | 2014-11-18 17:26:31 -0800 | [diff] [blame] | 418 | TEST(dlfcn, dlopen_check_order_reloc_grandchild) { | 
|  | 419 | // This is how this one works: | 
|  | 420 | // we lookup and call grandchild_get_answer which is defined in '_2.so' | 
|  | 421 | // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so' | 
|  | 422 | // the correct _impl() is implemented by '_c_1.so'; | 
|  | 423 | // | 
|  | 424 | // Here is the picture of subtree: | 
|  | 425 | // | 
|  | 426 | // libtest_check_order_reloc_siblings.so | 
|  | 427 | // | | 
|  | 428 | // +-> ..._2.so <- grandchild_get_answer() | 
|  | 429 | //     | | 
|  | 430 | //     +-> ..._c.so <- empty | 
|  | 431 | //     |   | | 
|  | 432 | //     |   +-> _c_1.so <- exports correct answer_impl() | 
|  | 433 | //     |   | | 
|  | 434 | //     |   +-> _c_2.so <- exports incorrect answer_impl() | 
|  | 435 | //     | | 
|  | 436 | //     +-> ..._d.so <- empty | 
|  | 437 |  | 
|  | 438 | void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD); | 
|  | 439 | ASSERT_TRUE(handle == nullptr); | 
|  | 440 | #ifdef __BIONIC__ | 
|  | 441 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? | 
|  | 442 | ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); | 
|  | 443 | #endif | 
|  | 444 |  | 
|  | 445 | handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL); | 
|  | 446 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 447 |  | 
|  | 448 | typedef int (*fn_t) (void); | 
|  | 449 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer")); | 
|  | 450 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 451 | ASSERT_EQ(42, fn()); | 
|  | 452 |  | 
|  | 453 | ASSERT_EQ(0, dlclose(handle)); | 
|  | 454 | } | 
|  | 455 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 456 | TEST(dlfcn, dlopen_check_order_reloc_nephew) { | 
|  | 457 | // This is how this one works: | 
|  | 458 | // we lookup and call nephew_get_answer which is defined in '_2.so' | 
|  | 459 | // and in turn calls external get_answer_impl() defined in '_[a-f].so' | 
|  | 460 | // the correct _impl() is implemented by '_a.so'; | 
|  | 461 | // | 
|  | 462 | // Here is the picture: | 
|  | 463 | // | 
|  | 464 | // libtest_check_order_reloc_siblings.so | 
|  | 465 | // | | 
|  | 466 | // +-> ..._1.so <- empty | 
|  | 467 | // |   | | 
|  | 468 | // |   +-> ..._a.so <- exports correct answer_impl() | 
|  | 469 | // |   | | 
|  | 470 | // |   +-> ..._b.so <- every other letter exporting incorrect one. | 
|  | 471 | // | | 
|  | 472 | // +-> ..._2.so <- empty | 
|  | 473 | // |   | | 
|  | 474 | // |   +-> ..._c.so | 
|  | 475 | // |   | | 
|  | 476 | // |   +-> ..._d.so | 
|  | 477 | // | | 
|  | 478 | // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl(); | 
|  | 479 | //     | | 
|  | 480 | //     +-> ..._e.so | 
|  | 481 | //     | | 
|  | 482 | //     +-> ..._f.so | 
|  | 483 |  | 
|  | 484 | void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD); | 
|  | 485 | ASSERT_TRUE(handle == nullptr); | 
|  | 486 | #ifdef __BIONIC__ | 
|  | 487 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? | 
|  | 488 | ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); | 
|  | 489 | #endif | 
|  | 490 |  | 
|  | 491 | handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL); | 
|  | 492 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 493 |  | 
|  | 494 | typedef int (*fn_t) (void); | 
|  | 495 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer")); | 
|  | 496 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 497 | ASSERT_EQ(42, fn()); | 
|  | 498 |  | 
|  | 499 | ASSERT_EQ(0, dlclose(handle)); | 
|  | 500 | } | 
|  | 501 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 502 | TEST(dlfcn, check_unload_after_reloc) { | 
|  | 503 | // This is how this one works: | 
|  | 504 | // libtest_two_parents_parent1 <- answer_impl() used by libtest_two_parents_child | 
|  | 505 | // | | 
|  | 506 | // +-> libtest_two_parents_child | 
|  | 507 | // | 
|  | 508 | // libtest_two_parents_parent2 <- answer_impl() not used by libtest_two_parents_child | 
|  | 509 | // | | 
|  | 510 | // +-> libtest_two_parents_child | 
|  | 511 | // | 
|  | 512 | // Test dlopens parent1 which loads and relocates libtest_two_parents_child.so | 
|  | 513 | // as a second step it dlopens parent2 and dlcloses parent1... | 
|  | 514 |  | 
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 515 | void* handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL); | 
|  | 516 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 517 |  | 
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 518 | void* handle2 = dlopen("libtest_two_parents_parent2.so", RTLD_NOW | RTLD_LOCAL); | 
|  | 519 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 520 |  | 
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 521 | typedef int (*fn_t) (void); | 
|  | 522 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer")); | 
|  | 523 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 524 | ASSERT_EQ(42, fn()); | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 525 |  | 
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 526 | ASSERT_EQ(0, dlclose(handle)); | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 527 |  | 
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 528 | handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD); | 
|  | 529 | ASSERT_TRUE(handle != nullptr); | 
|  | 530 | ASSERT_EQ(0, dlclose(handle)); | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 531 |  | 
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 532 | fn = reinterpret_cast<fn_t>(dlsym(handle2, "check_order_reloc_get_answer")); | 
|  | 533 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 534 | ASSERT_EQ(42, fn()); | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 535 |  | 
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 536 | ASSERT_EQ(0, dlclose(handle2)); | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 537 |  | 
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 538 | handle = dlopen("libtest_two_parents_parent1.so", RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD); | 
|  | 539 | ASSERT_TRUE(handle == nullptr); | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 540 | } | 
|  | 541 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 542 | extern "C" int check_order_reloc_root_get_answer_impl() { | 
|  | 543 | return 42; | 
|  | 544 | } | 
|  | 545 |  | 
|  | 546 | TEST(dlfcn, dlopen_check_order_reloc_main_executable) { | 
|  | 547 | // This is how this one works: | 
|  | 548 | // we lookup and call get_answer3 which is defined in 'root.so' | 
|  | 549 | // and in turn calls external root_get_answer_impl() defined in _2.so and | 
|  | 550 | // above the correct _impl() is one in the executable. | 
|  | 551 | // | 
|  | 552 | // libtest_check_order_reloc_root.so | 
|  | 553 | // | | 
|  | 554 | // +-> ..._1.so <- empty | 
|  | 555 | // | | 
|  | 556 | // +-> ..._2.so <- gives incorrect answer for answer_main_impl() | 
|  | 557 | // | 
|  | 558 |  | 
|  | 559 | void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD); | 
|  | 560 | ASSERT_TRUE(handle == nullptr); | 
|  | 561 | #ifdef __BIONIC__ | 
|  | 562 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? | 
|  | 563 | ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); | 
|  | 564 | #endif | 
|  | 565 |  | 
|  | 566 | handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL); | 
|  | 567 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 568 |  | 
|  | 569 | typedef int (*fn_t) (void); | 
|  | 570 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer")); | 
|  | 571 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 572 | ASSERT_EQ(42, fn()); | 
|  | 573 |  | 
|  | 574 | ASSERT_EQ(0, dlclose(handle)); | 
|  | 575 | } | 
|  | 576 |  | 
| Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 577 | TEST(dlfcn, dlopen_check_rtld_local) { | 
|  | 578 | void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); | 
|  | 579 | ASSERT_TRUE(sym == nullptr); | 
|  | 580 |  | 
|  | 581 | // implicit RTLD_LOCAL | 
|  | 582 | void* handle = dlopen("libtest_simple.so", RTLD_NOW); | 
|  | 583 | sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); | 
|  | 584 | ASSERT_TRUE(sym == nullptr); | 
|  | 585 | ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror()); | 
|  | 586 | sym = dlsym(handle, "dlopen_testlib_simple_func"); | 
|  | 587 | ASSERT_TRUE(sym != nullptr); | 
|  | 588 | ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)()); | 
|  | 589 | dlclose(handle); | 
|  | 590 |  | 
|  | 591 | // explicit RTLD_LOCAL | 
|  | 592 | handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL); | 
|  | 593 | sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); | 
|  | 594 | ASSERT_TRUE(sym == nullptr); | 
|  | 595 | ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror()); | 
|  | 596 | sym = dlsym(handle, "dlopen_testlib_simple_func"); | 
|  | 597 | ASSERT_TRUE(sym != nullptr); | 
|  | 598 | ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)()); | 
|  | 599 | dlclose(handle); | 
|  | 600 | } | 
|  | 601 |  | 
|  | 602 | TEST(dlfcn, dlopen_check_rtld_global) { | 
|  | 603 | void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); | 
|  | 604 | ASSERT_TRUE(sym == nullptr); | 
|  | 605 |  | 
|  | 606 | void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL); | 
| Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 607 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
| Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 608 | sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); | 
|  | 609 | ASSERT_TRUE(sym != nullptr) << dlerror(); | 
|  | 610 | ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)()); | 
|  | 611 | dlclose(handle); | 
| Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 612 |  | 
|  | 613 | // RTLD_GLOBAL implies RTLD_NODELETE, let's check that | 
|  | 614 | void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func"); | 
|  | 615 | ASSERT_EQ(sym, sym_after_dlclose); | 
| Dmitriy Ivanov | f439b5a | 2015-05-30 13:04:39 -0700 | [diff] [blame] | 616 |  | 
|  | 617 | // Check if dlsym() for main program's handle searches RTLD_GLOBAL | 
|  | 618 | // shared libraries after symbol was not found in the main executable | 
|  | 619 | // and dependent libraries. | 
|  | 620 | void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW); | 
|  | 621 | sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func"); | 
|  | 622 | ASSERT_TRUE(sym != nullptr) << dlerror(); | 
|  | 623 |  | 
|  | 624 | dlclose(handle_for_main_executable); | 
| Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 625 | } | 
|  | 626 |  | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 627 | // libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so -> | 
|  | 628 | // libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so -> | 
|  | 629 | // libtest_with_dependency_loop_a.so | 
|  | 630 | TEST(dlfcn, dlopen_check_loop) { | 
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 631 | void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW); | 
|  | 632 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 633 | void* f = dlsym(handle, "dlopen_test_loopy_function"); | 
|  | 634 | ASSERT_TRUE(f != nullptr) << dlerror(); | 
|  | 635 | EXPECT_TRUE(reinterpret_cast<bool (*)(void)>(f)()); | 
|  | 636 | ASSERT_EQ(0, dlclose(handle)); | 
| Dmitriy Ivanov | a6ac54a | 2014-09-09 10:21:42 -0700 | [diff] [blame] | 637 |  | 
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 638 | // dlopen second time to make sure that the library was unloaded correctly | 
|  | 639 | handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD); | 
|  | 640 | ASSERT_TRUE(handle == nullptr); | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 641 | #ifdef __BIONIC__ | 
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 642 | 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] | 643 | #else | 
|  | 644 | // TODO: glibc returns nullptr on dlerror() here. Is it bug? | 
|  | 645 | ASSERT_TRUE(dlerror() == nullptr); | 
| Dmitriy Ivanov | eb27bba | 2014-09-15 14:13:24 -0700 | [diff] [blame] | 646 | #endif | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 647 |  | 
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 648 | handle = dlopen("libtest_with_dependency_a.so", RTLD_NOW | RTLD_NOLOAD); | 
|  | 649 | ASSERT_TRUE(handle == nullptr); | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 650 | } | 
|  | 651 |  | 
| Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 652 | TEST(dlfcn, dlopen_nodelete) { | 
|  | 653 | static bool is_unloaded = false; | 
|  | 654 |  | 
|  | 655 | void* handle = dlopen("libtest_nodelete_1.so", RTLD_NOW | RTLD_NODELETE); | 
|  | 656 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 657 | void (*set_unload_flag_ptr)(bool*); | 
|  | 658 | set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_1_set_unload_flag_ptr")); | 
|  | 659 | ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror(); | 
|  | 660 | set_unload_flag_ptr(&is_unloaded); | 
|  | 661 |  | 
|  | 662 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number")); | 
|  | 663 | ASSERT_TRUE(taxicab_number != nullptr) << dlerror(); | 
|  | 664 | ASSERT_EQ(1729U, *taxicab_number); | 
|  | 665 | *taxicab_number = 2; | 
|  | 666 |  | 
|  | 667 | dlclose(handle); | 
|  | 668 | ASSERT_TRUE(!is_unloaded); | 
|  | 669 |  | 
|  | 670 | uint32_t* taxicab_number_after_dlclose = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number")); | 
|  | 671 | ASSERT_EQ(taxicab_number_after_dlclose, taxicab_number); | 
|  | 672 | ASSERT_EQ(2U, *taxicab_number_after_dlclose); | 
|  | 673 |  | 
|  | 674 |  | 
|  | 675 | handle = dlopen("libtest_nodelete_1.so", RTLD_NOW); | 
|  | 676 | uint32_t* taxicab_number2 = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_1_taxicab_number")); | 
|  | 677 | ASSERT_EQ(taxicab_number2, taxicab_number); | 
|  | 678 |  | 
|  | 679 | ASSERT_EQ(2U, *taxicab_number2); | 
|  | 680 |  | 
|  | 681 | dlclose(handle); | 
|  | 682 | ASSERT_TRUE(!is_unloaded); | 
|  | 683 | } | 
|  | 684 |  | 
|  | 685 | TEST(dlfcn, dlopen_nodelete_on_second_dlopen) { | 
|  | 686 | static bool is_unloaded = false; | 
|  | 687 |  | 
|  | 688 | void* handle = dlopen("libtest_nodelete_2.so", RTLD_NOW); | 
|  | 689 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 690 | void (*set_unload_flag_ptr)(bool*); | 
|  | 691 | set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_2_set_unload_flag_ptr")); | 
|  | 692 | ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror(); | 
|  | 693 | set_unload_flag_ptr(&is_unloaded); | 
|  | 694 |  | 
|  | 695 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_nodelete_2_taxicab_number")); | 
|  | 696 | ASSERT_TRUE(taxicab_number != nullptr) << dlerror(); | 
|  | 697 |  | 
|  | 698 | ASSERT_EQ(1729U, *taxicab_number); | 
|  | 699 | *taxicab_number = 2; | 
|  | 700 |  | 
|  | 701 | // This RTLD_NODELETE should be ignored | 
|  | 702 | void* handle1 = dlopen("libtest_nodelete_2.so", RTLD_NOW | RTLD_NODELETE); | 
|  | 703 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); | 
|  | 704 | ASSERT_EQ(handle, handle1); | 
|  | 705 |  | 
|  | 706 | dlclose(handle1); | 
|  | 707 | dlclose(handle); | 
|  | 708 |  | 
|  | 709 | ASSERT_TRUE(is_unloaded); | 
|  | 710 | } | 
|  | 711 |  | 
|  | 712 | TEST(dlfcn, dlopen_nodelete_dt_flags_1) { | 
|  | 713 | static bool is_unloaded = false; | 
|  | 714 |  | 
|  | 715 | void* handle = dlopen("libtest_nodelete_dt_flags_1.so", RTLD_NOW); | 
|  | 716 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 717 | void (*set_unload_flag_ptr)(bool*); | 
|  | 718 | set_unload_flag_ptr = reinterpret_cast<void (*)(bool*)>(dlsym(handle, "dlopen_nodelete_dt_flags_1_set_unload_flag_ptr")); | 
|  | 719 | ASSERT_TRUE(set_unload_flag_ptr != nullptr) << dlerror(); | 
|  | 720 | set_unload_flag_ptr(&is_unloaded); | 
|  | 721 |  | 
|  | 722 | dlclose(handle); | 
|  | 723 | ASSERT_TRUE(!is_unloaded); | 
|  | 724 | } | 
|  | 725 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 726 | TEST(dlfcn, dlsym_df_1_global) { | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 727 | void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW); | 
|  | 728 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 729 | int (*get_answer)(); | 
|  | 730 | get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer")); | 
|  | 731 | ASSERT_TRUE(get_answer != nullptr) << dlerror(); | 
|  | 732 | ASSERT_EQ(42, get_answer()); | 
|  | 733 | ASSERT_EQ(0, dlclose(handle)); | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 734 | } | 
|  | 735 |  | 
| Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 736 | TEST(dlfcn, dlopen_failure) { | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 737 | void* self = dlopen("/does/not/exist", RTLD_NOW); | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 738 | ASSERT_TRUE(self == nullptr); | 
| Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 739 | #if defined(__BIONIC__) | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 740 | ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror()); | 
|  | 741 | #else | 
|  | 742 | ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror()); | 
|  | 743 | #endif | 
|  | 744 | } | 
|  | 745 |  | 
| Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 746 | static void* ConcurrentDlErrorFn(void*) { | 
| Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 747 | dlopen("/child/thread", RTLD_NOW); | 
|  | 748 | return reinterpret_cast<void*>(strdup(dlerror())); | 
|  | 749 | } | 
|  | 750 |  | 
| Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 751 | TEST(dlfcn, dlerror_concurrent) { | 
| Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 752 | dlopen("/main/thread", RTLD_NOW); | 
|  | 753 | const char* main_thread_error = dlerror(); | 
|  | 754 | ASSERT_SUBSTR("/main/thread", main_thread_error); | 
|  | 755 |  | 
|  | 756 | pthread_t t; | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 757 | ASSERT_EQ(0, pthread_create(&t, nullptr, ConcurrentDlErrorFn, nullptr)); | 
| Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 758 | void* result; | 
|  | 759 | ASSERT_EQ(0, pthread_join(t, &result)); | 
|  | 760 | char* child_thread_error = static_cast<char*>(result); | 
|  | 761 | ASSERT_SUBSTR("/child/thread", child_thread_error); | 
|  | 762 | free(child_thread_error); | 
|  | 763 |  | 
|  | 764 | ASSERT_SUBSTR("/main/thread", main_thread_error); | 
|  | 765 | } | 
|  | 766 |  | 
| Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 767 | TEST(dlfcn, dlsym_failures) { | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 768 | dlerror(); // Clear any pending errors. | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 769 | void* self = dlopen(nullptr, RTLD_NOW); | 
|  | 770 | ASSERT_TRUE(self != nullptr); | 
|  | 771 | ASSERT_TRUE(dlerror() == nullptr); | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 772 |  | 
|  | 773 | void* sym; | 
|  | 774 |  | 
| Dmitriy Ivanov | 44adf93 | 2014-05-22 09:49:24 -0700 | [diff] [blame] | 775 | #if defined(__BIONIC__) && !defined(__LP64__) | 
|  | 776 | // RTLD_DEFAULT in lp32 bionic is not (void*)0 | 
|  | 777 | // so it can be distinguished from the NULL handle. | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 778 | sym = dlsym(nullptr, "test"); | 
|  | 779 | ASSERT_TRUE(sym == nullptr); | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 780 | ASSERT_STREQ("dlsym failed: library handle is null", dlerror()); | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 781 | #endif | 
|  | 782 |  | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 783 | // Symbol that doesn't exist. | 
|  | 784 | sym = dlsym(self, "ThisSymbolDoesNotExist"); | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 785 | ASSERT_TRUE(sym == nullptr); | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 786 | ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror()); | 
| Elliott Hughes | 1a69616 | 2012-11-01 13:49:32 -0700 | [diff] [blame] | 787 |  | 
|  | 788 | ASSERT_EQ(0, dlclose(self)); | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 789 | } | 
|  | 790 |  | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 791 | TEST(dlfcn, dladdr_executable) { | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 792 | dlerror(); // Clear any pending errors. | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 793 | void* self = dlopen(nullptr, RTLD_NOW); | 
|  | 794 | ASSERT_TRUE(self != nullptr); | 
|  | 795 | ASSERT_TRUE(dlerror() == nullptr); | 
| Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 796 |  | 
|  | 797 | void* sym = dlsym(self, "DlSymTestFunction"); | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 798 | ASSERT_TRUE(sym != nullptr); | 
| Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 799 |  | 
|  | 800 | // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address. | 
|  | 801 | void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2); | 
|  | 802 |  | 
|  | 803 | Dl_info info; | 
|  | 804 | int rc = dladdr(addr, &info); | 
|  | 805 | ASSERT_NE(rc, 0); // Zero on error, non-zero on success. | 
|  | 806 |  | 
|  | 807 | // Get the name of this executable. | 
|  | 808 | char executable_path[PATH_MAX]; | 
|  | 809 | rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path)); | 
|  | 810 | ASSERT_NE(rc, -1); | 
|  | 811 | executable_path[rc] = '\0'; | 
| Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 812 |  | 
|  | 813 | // The filename should be that of this executable. | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 814 | char dli_realpath[PATH_MAX]; | 
|  | 815 | ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr); | 
|  | 816 | ASSERT_STREQ(executable_path, dli_realpath); | 
| Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 817 |  | 
|  | 818 | // The symbol name should be the symbol we looked up. | 
|  | 819 | ASSERT_STREQ(info.dli_sname, "DlSymTestFunction"); | 
|  | 820 |  | 
|  | 821 | // The address should be the exact address of the symbol. | 
|  | 822 | ASSERT_EQ(info.dli_saddr, sym); | 
|  | 823 |  | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 824 | std::vector<map_record> maps; | 
|  | 825 | ASSERT_TRUE(Maps::parse_maps(&maps)); | 
|  | 826 |  | 
|  | 827 | void* base_address = nullptr; | 
|  | 828 | for (const map_record& rec : maps) { | 
|  | 829 | if (executable_path == rec.pathname) { | 
|  | 830 | base_address = reinterpret_cast<void*>(rec.addr_start); | 
| Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 831 | break; | 
|  | 832 | } | 
|  | 833 | } | 
| Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 834 |  | 
|  | 835 | // The base address should be the address we were loaded at. | 
|  | 836 | ASSERT_EQ(info.dli_fbase, base_address); | 
| Elliott Hughes | 1a69616 | 2012-11-01 13:49:32 -0700 | [diff] [blame] | 837 |  | 
|  | 838 | ASSERT_EQ(0, dlclose(self)); | 
| Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 839 | } | 
|  | 840 |  | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 841 | #if defined(__LP64__) | 
| Lazar Trsic | 6f2d310 | 2015-10-13 16:43:00 +0200 | [diff] [blame] | 842 | #define PATH_TO_SYSTEM_LIB "/system/lib64/" | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 843 | #else | 
| Lazar Trsic | 6f2d310 | 2015-10-13 16:43:00 +0200 | [diff] [blame] | 844 | #define PATH_TO_SYSTEM_LIB "/system/lib/" | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 845 | #endif | 
| Lazar Trsic | 6f2d310 | 2015-10-13 16:43:00 +0200 | [diff] [blame] | 846 | #define PATH_TO_LIBC PATH_TO_SYSTEM_LIB "libc.so" | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 847 |  | 
|  | 848 | TEST(dlfcn, dladdr_libc) { | 
|  | 849 | #if defined(__BIONIC__) | 
|  | 850 | Dl_info info; | 
|  | 851 | void* addr = reinterpret_cast<void*>(puts); // well-known libc function | 
|  | 852 | ASSERT_TRUE(dladdr(addr, &info) != 0); | 
|  | 853 |  | 
| Dmitriy Ivanov | ef25592 | 2015-04-08 11:53:08 -0700 | [diff] [blame] | 854 | // /system/lib is symlink when this test is executed on host. | 
|  | 855 | char libc_realpath[PATH_MAX]; | 
| Lazar Trsic | 6f2d310 | 2015-10-13 16:43:00 +0200 | [diff] [blame] | 856 | ASSERT_TRUE(realpath(PATH_TO_LIBC, libc_realpath) == libc_realpath); | 
| Dmitriy Ivanov | ef25592 | 2015-04-08 11:53:08 -0700 | [diff] [blame] | 857 |  | 
|  | 858 | ASSERT_STREQ(libc_realpath, info.dli_fname); | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 859 | // TODO: add check for dfi_fbase | 
|  | 860 | ASSERT_STREQ("puts", info.dli_sname); | 
|  | 861 | ASSERT_EQ(addr, info.dli_saddr); | 
|  | 862 | #else | 
|  | 863 | GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig " | 
|  | 864 | "for libc.so, which is symlink itself (not a realpath).\n"; | 
|  | 865 | #endif | 
|  | 866 | } | 
|  | 867 |  | 
| Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 868 | TEST(dlfcn, dladdr_invalid) { | 
| Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 869 | Dl_info info; | 
|  | 870 |  | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 871 | dlerror(); // Clear any pending errors. | 
|  | 872 |  | 
| Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 873 | // No symbol corresponding to NULL. | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 874 | ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success. | 
|  | 875 | ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3). | 
| Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 876 |  | 
|  | 877 | // No symbol corresponding to a stack address. | 
|  | 878 | 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] | 879 | ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3). | 
| Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 880 | } | 
| Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 881 |  | 
| Elliott Hughes | a43e906 | 2013-01-07 14:18:22 -0800 | [diff] [blame] | 882 | // GNU-style ELF hash tables are incompatible with the MIPS ABI. | 
|  | 883 | // 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] | 884 | TEST(dlfcn, dlopen_library_with_only_gnu_hash) { | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 885 | #if !defined(__mips__) | 
| Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 886 | dlerror(); // Clear any pending errors. | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 887 | void* handle = dlopen("libgnu-hash-table-library.so", RTLD_NOW); | 
|  | 888 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 889 | auto guard = make_scope_guard([&]() { | 
|  | 890 | dlclose(handle); | 
|  | 891 | }); | 
|  | 892 | void* sym = dlsym(handle, "getRandomNumber"); | 
|  | 893 | ASSERT_TRUE(sym != nullptr) << dlerror(); | 
|  | 894 | int (*fn)(void); | 
|  | 895 | fn = reinterpret_cast<int (*)(void)>(sym); | 
|  | 896 | EXPECT_EQ(4, fn()); | 
|  | 897 |  | 
|  | 898 | Dl_info dlinfo; | 
|  | 899 | ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo)); | 
|  | 900 |  | 
|  | 901 | ASSERT_TRUE(fn == dlinfo.dli_saddr); | 
|  | 902 | ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname); | 
| Dmitriy Ivanov | b335677 | 2014-11-14 11:19:22 -0800 | [diff] [blame] | 903 | ASSERT_SUBSTR("libgnu-hash-table-library.so", dlinfo.dli_fname); | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 904 | #else | 
|  | 905 | GTEST_LOG_(INFO) << "This test does nothing for mips/mips64; mips toolchain does not support '--hash-style=gnu'\n"; | 
|  | 906 | #endif | 
| Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 907 | } | 
| Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 908 |  | 
| Dmitriy Ivanov | b335677 | 2014-11-14 11:19:22 -0800 | [diff] [blame] | 909 | TEST(dlfcn, dlopen_library_with_only_sysv_hash) { | 
|  | 910 | void* handle = dlopen("libsysv-hash-table-library.so", RTLD_NOW); | 
|  | 911 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 912 | auto guard = make_scope_guard([&]() { | 
|  | 913 | dlclose(handle); | 
|  | 914 | }); | 
|  | 915 | void* sym = dlsym(handle, "getRandomNumber"); | 
|  | 916 | ASSERT_TRUE(sym != nullptr) << dlerror(); | 
|  | 917 | int (*fn)(void); | 
|  | 918 | fn = reinterpret_cast<int (*)(void)>(sym); | 
|  | 919 | EXPECT_EQ(4, fn()); | 
|  | 920 |  | 
|  | 921 | Dl_info dlinfo; | 
|  | 922 | ASSERT_TRUE(0 != dladdr(reinterpret_cast<void*>(fn), &dlinfo)); | 
|  | 923 |  | 
|  | 924 | ASSERT_TRUE(fn == dlinfo.dli_saddr); | 
|  | 925 | ASSERT_STREQ("getRandomNumber", dlinfo.dli_sname); | 
|  | 926 | ASSERT_SUBSTR("libsysv-hash-table-library.so", dlinfo.dli_fname); | 
|  | 927 | } | 
|  | 928 |  | 
| Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 929 | TEST(dlfcn, dlopen_bad_flags) { | 
|  | 930 | dlerror(); // Clear any pending errors. | 
|  | 931 | void* handle; | 
|  | 932 |  | 
| Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 933 | #if defined(__GLIBC__) | 
| Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 934 | // 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] | 935 | handle = dlopen(nullptr, 0); | 
|  | 936 | ASSERT_TRUE(handle == nullptr); | 
| Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 937 | ASSERT_SUBSTR("invalid", dlerror()); | 
|  | 938 | #endif | 
|  | 939 |  | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 940 | handle = dlopen(nullptr, 0xffffffff); | 
|  | 941 | ASSERT_TRUE(handle == nullptr); | 
| Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 942 | ASSERT_SUBSTR("invalid", dlerror()); | 
|  | 943 |  | 
|  | 944 | // 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] | 945 | handle = dlopen(nullptr, RTLD_NOW|RTLD_LAZY); | 
|  | 946 | ASSERT_TRUE(handle != nullptr); | 
|  | 947 | ASSERT_SUBSTR(nullptr, dlerror()); | 
| Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 948 | } | 
| Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 949 |  | 
|  | 950 | TEST(dlfcn, rtld_default_unknown_symbol) { | 
| Elliott Hughes | 2ed7109 | 2013-11-11 15:48:06 -0800 | [diff] [blame] | 951 | void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME"); | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 952 | ASSERT_TRUE(addr == nullptr); | 
| Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 953 | } | 
|  | 954 |  | 
|  | 955 | TEST(dlfcn, rtld_default_known_symbol) { | 
| Elliott Hughes | 2ed7109 | 2013-11-11 15:48:06 -0800 | [diff] [blame] | 956 | void* addr = dlsym(RTLD_DEFAULT, "fopen"); | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 957 | ASSERT_TRUE(addr != nullptr); | 
| Elliott Hughes | 2ed7109 | 2013-11-11 15:48:06 -0800 | [diff] [blame] | 958 | } | 
|  | 959 |  | 
|  | 960 | TEST(dlfcn, rtld_next_unknown_symbol) { | 
|  | 961 | void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME"); | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 962 | ASSERT_TRUE(addr == nullptr); | 
| Elliott Hughes | 2ed7109 | 2013-11-11 15:48:06 -0800 | [diff] [blame] | 963 | } | 
|  | 964 |  | 
|  | 965 | TEST(dlfcn, rtld_next_known_symbol) { | 
|  | 966 | void* addr = dlsym(RTLD_NEXT, "fopen"); | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 967 | ASSERT_TRUE(addr != nullptr); | 
| Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 968 | } | 
| Dmitriy Ivanov | 7db1809 | 2014-05-08 12:27:25 -0700 | [diff] [blame] | 969 |  | 
| Dmitriy Ivanov | ce44166 | 2014-06-17 15:56:38 -0700 | [diff] [blame] | 970 | TEST(dlfcn, dlsym_weak_func) { | 
|  | 971 | dlerror(); | 
| Dmitriy Ivanov | bfa88bc | 2014-12-16 11:40:46 -0800 | [diff] [blame] | 972 | void* handle = dlopen("libtest_dlsym_weak_func.so", RTLD_NOW); | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 973 | ASSERT_TRUE(handle != nullptr); | 
| Dmitriy Ivanov | ce44166 | 2014-06-17 15:56:38 -0700 | [diff] [blame] | 974 |  | 
|  | 975 | int (*weak_func)(); | 
|  | 976 | weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func")); | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 977 | ASSERT_TRUE(weak_func != nullptr) << "dlerror: " << dlerror(); | 
| Dmitriy Ivanov | ce44166 | 2014-06-17 15:56:38 -0700 | [diff] [blame] | 978 | EXPECT_EQ(42, weak_func()); | 
|  | 979 | dlclose(handle); | 
|  | 980 | } | 
|  | 981 |  | 
| Dmitriy Ivanov | bfa88bc | 2014-12-16 11:40:46 -0800 | [diff] [blame] | 982 | TEST(dlfcn, dlopen_undefined_weak_func) { | 
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 983 | void* handle = dlopen("libtest_dlopen_weak_undefined_func.so", RTLD_NOW); | 
|  | 984 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 985 | int (*weak_func)(); | 
|  | 986 | weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "use_weak_undefined_func")); | 
|  | 987 | ASSERT_TRUE(weak_func != nullptr) << dlerror(); | 
|  | 988 | EXPECT_EQ(6551, weak_func()); | 
|  | 989 | dlclose(handle); | 
| Dmitriy Ivanov | bfa88bc | 2014-12-16 11:40:46 -0800 | [diff] [blame] | 990 | } | 
|  | 991 |  | 
| Dmitriy Ivanov | 7db1809 | 2014-05-08 12:27:25 -0700 | [diff] [blame] | 992 | TEST(dlfcn, dlopen_symlink) { | 
|  | 993 | void* handle1 = dlopen("libdlext_test.so", RTLD_NOW); | 
|  | 994 | void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW); | 
| Dmitriy Ivanov | aff18fd | 2015-06-23 13:58:22 -0700 | [diff] [blame] | 995 | ASSERT_TRUE(handle1 != nullptr); | 
|  | 996 | ASSERT_TRUE(handle2 != nullptr); | 
| Dmitriy Ivanov | 7db1809 | 2014-05-08 12:27:25 -0700 | [diff] [blame] | 997 | ASSERT_EQ(handle1, handle2); | 
| Dmitriy Ivanov | 319356e | 2014-09-02 17:31:44 -0700 | [diff] [blame] | 998 | dlclose(handle1); | 
|  | 999 | dlclose(handle2); | 
| Dmitriy Ivanov | 7db1809 | 2014-05-08 12:27:25 -0700 | [diff] [blame] | 1000 | } | 
| Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 1001 |  | 
|  | 1002 | // libtest_dlopen_from_ctor_main.so depends on | 
|  | 1003 | // libtest_dlopen_from_ctor.so which has a constructor | 
|  | 1004 | // that calls dlopen(libc...). This is to test the situation | 
|  | 1005 | // described in b/7941716. | 
|  | 1006 | TEST(dlfcn, dlopen_dlopen_from_ctor) { | 
|  | 1007 | #if defined(__BIONIC__) | 
|  | 1008 | void* handle = dlopen("libtest_dlopen_from_ctor_main.so", RTLD_NOW); | 
|  | 1009 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 1010 | dlclose(handle); | 
|  | 1011 | #else | 
|  | 1012 | GTEST_LOG_(INFO) << "This test is disabled for glibc (glibc segfaults if you try to call dlopen from a constructor).\n"; | 
|  | 1013 | #endif | 
|  | 1014 | } | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 1015 |  | 
|  | 1016 | TEST(dlfcn, symbol_versioning_use_v1) { | 
|  | 1017 | void* handle = dlopen("libtest_versioned_uselibv1.so", RTLD_NOW); | 
|  | 1018 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 1019 | typedef int (*fn_t)(); | 
|  | 1020 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version")); | 
|  | 1021 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 1022 | ASSERT_EQ(1, fn()); | 
|  | 1023 | dlclose(handle); | 
|  | 1024 | } | 
|  | 1025 |  | 
|  | 1026 | TEST(dlfcn, symbol_versioning_use_v2) { | 
|  | 1027 | void* handle = dlopen("libtest_versioned_uselibv2.so", RTLD_NOW); | 
|  | 1028 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 1029 | typedef int (*fn_t)(); | 
|  | 1030 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version")); | 
|  | 1031 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 1032 | ASSERT_EQ(2, fn()); | 
|  | 1033 | dlclose(handle); | 
|  | 1034 | } | 
|  | 1035 |  | 
|  | 1036 | TEST(dlfcn, symbol_versioning_use_other_v2) { | 
|  | 1037 | void* handle = dlopen("libtest_versioned_uselibv2_other.so", RTLD_NOW); | 
|  | 1038 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 1039 | typedef int (*fn_t)(); | 
|  | 1040 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version")); | 
|  | 1041 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 1042 | ASSERT_EQ(20, fn()); | 
|  | 1043 | dlclose(handle); | 
|  | 1044 | } | 
|  | 1045 |  | 
|  | 1046 | TEST(dlfcn, symbol_versioning_use_other_v3) { | 
|  | 1047 | void* handle = dlopen("libtest_versioned_uselibv3_other.so", RTLD_NOW); | 
|  | 1048 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 1049 | typedef int (*fn_t)(); | 
|  | 1050 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "get_function_version")); | 
|  | 1051 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 1052 | ASSERT_EQ(3, fn()); | 
|  | 1053 | dlclose(handle); | 
|  | 1054 | } | 
|  | 1055 |  | 
|  | 1056 | TEST(dlfcn, symbol_versioning_default_via_dlsym) { | 
|  | 1057 | void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW); | 
|  | 1058 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 1059 | typedef int (*fn_t)(); | 
|  | 1060 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "versioned_function")); | 
|  | 1061 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 1062 | ASSERT_EQ(3, fn()); // the default version is 3 | 
|  | 1063 | dlclose(handle); | 
|  | 1064 | } | 
|  | 1065 |  | 
| Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 1066 | TEST(dlfcn, dlvsym_smoke) { | 
|  | 1067 | void* handle = dlopen("libtest_versioned_lib.so", RTLD_NOW); | 
|  | 1068 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 1069 | typedef int (*fn_t)(); | 
|  | 1070 |  | 
|  | 1071 | { | 
|  | 1072 | fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "nonversion")); | 
|  | 1073 | ASSERT_TRUE(fn == nullptr); | 
|  | 1074 | ASSERT_SUBSTR("undefined symbol: versioned_function, version nonversion", dlerror()); | 
|  | 1075 | } | 
|  | 1076 |  | 
|  | 1077 | { | 
|  | 1078 | fn_t fn = reinterpret_cast<fn_t>(dlvsym(handle, "versioned_function", "TESTLIB_V2")); | 
|  | 1079 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 1080 | ASSERT_EQ(2, fn()); | 
|  | 1081 | } | 
|  | 1082 |  | 
|  | 1083 | dlclose(handle); | 
|  | 1084 | } | 
|  | 1085 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 1086 | // This preempts the implementation from libtest_versioned_lib.so | 
|  | 1087 | extern "C" int version_zero_function() { | 
|  | 1088 | return 0; | 
|  | 1089 | } | 
|  | 1090 |  | 
|  | 1091 | // This preempts the implementation from libtest_versioned_uselibv*.so | 
|  | 1092 | extern "C" int version_zero_function2() { | 
|  | 1093 | return 0; | 
|  | 1094 | } | 
| Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 1095 |  | 
| Lazar Trsic | 6f2d310 | 2015-10-13 16:43:00 +0200 | [diff] [blame] | 1096 | TEST(dlfcn, dt_runpath_smoke) { | 
| Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 1097 | void* handle = dlopen("libtest_dt_runpath_d.so", RTLD_NOW); | 
|  | 1098 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 1099 |  | 
|  | 1100 | typedef void *(* dlopen_b_fn)(); | 
|  | 1101 | dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b"); | 
|  | 1102 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 1103 |  | 
|  | 1104 | void *p = fn(); | 
| Evgenii Stepanov | 0cdef7e | 2015-07-06 17:56:31 -0700 | [diff] [blame] | 1105 | ASSERT_TRUE(p != nullptr); | 
| Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 1106 |  | 
|  | 1107 | dlclose(handle); | 
|  | 1108 | } | 
| Lazar Trsic | 6f2d310 | 2015-10-13 16:43:00 +0200 | [diff] [blame] | 1109 |  | 
|  | 1110 | TEST(dlfcn, dt_runpath_absolute_path) { | 
|  | 1111 | void* handle = dlopen(PATH_TO_SYSTEM_LIB "libtest_dt_runpath_d.so", RTLD_NOW); | 
|  | 1112 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 1113 |  | 
|  | 1114 | typedef void *(* dlopen_b_fn)(); | 
|  | 1115 | dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b"); | 
|  | 1116 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 1117 |  | 
|  | 1118 | void *p = fn(); | 
|  | 1119 | ASSERT_TRUE(p != nullptr); | 
|  | 1120 |  | 
|  | 1121 | dlclose(handle); | 
|  | 1122 | } |