Dmitriy Ivanov | 0416d88 | 2014-11-04 09:38:18 -0800 | [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 | |
Jiyong Park | 4945d8f | 2017-08-30 11:30:53 +0900 | [diff] [blame] | 19 | #if defined(__BIONIC__) |
| 20 | #include <android-base/properties.h> |
| 21 | #endif |
| 22 | |
Dmitriy Ivanov | 0416d88 | 2014-11-04 09:38:18 -0800 | [diff] [blame] | 23 | #include <dlfcn.h> |
| 24 | #include <libgen.h> |
| 25 | #include <limits.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdint.h> |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame] | 28 | #include <sys/stat.h> |
Dmitriy Ivanov | 0416d88 | 2014-11-04 09:38:18 -0800 | [diff] [blame] | 29 | |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 30 | #include <fstream> |
Elliott Hughes | ec580d3 | 2021-04-12 15:55:29 -0700 | [diff] [blame] | 31 | #include <iostream> |
| 32 | #include <regex> |
| 33 | #include <string> |
Dmitriy Ivanov | 0416d88 | 2014-11-04 09:38:18 -0800 | [diff] [blame] | 34 | |
Elliott Hughes | eb04ed5 | 2017-03-29 13:48:02 -0700 | [diff] [blame] | 35 | #include "gtest_globals.h" |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 36 | #include <android-base/file.h> |
Dimitry Ivanov | 2a6955e | 2017-02-23 11:53:43 -0800 | [diff] [blame] | 37 | #include "utils.h" |
| 38 | |
Dmitriy Ivanov | 0416d88 | 2014-11-04 09:38:18 -0800 | [diff] [blame] | 39 | extern "C" int main_global_default_serial() { |
| 40 | return 3370318; |
| 41 | } |
| 42 | |
| 43 | extern "C" int main_global_protected_serial() { |
| 44 | return 2716057; |
| 45 | } |
| 46 | |
| 47 | // The following functions are defined in DT_NEEDED |
| 48 | // libdl_preempt_test.so library. |
| 49 | |
| 50 | // This one calls main_global_default_serial |
| 51 | extern "C" int main_global_default_get_serial(); |
| 52 | |
| 53 | // This one calls main_global_protected_serial |
| 54 | extern "C" int main_global_protected_get_serial(); |
| 55 | |
| 56 | // This one calls lib_global_default_serial |
| 57 | extern "C" int lib_global_default_get_serial(); |
| 58 | |
| 59 | // This one calls lib_global_protected_serial |
| 60 | extern "C" int lib_global_protected_get_serial(); |
| 61 | |
| 62 | // This test verifies that the global default function |
| 63 | // main_global_default_serial() is preempted by |
| 64 | // the function defined above. |
| 65 | TEST(dl, main_preempts_global_default) { |
| 66 | ASSERT_EQ(3370318, main_global_default_get_serial()); |
| 67 | } |
| 68 | |
| 69 | // This one makes sure that the global protected |
| 70 | // symbols do not get preempted |
| 71 | TEST(dl, main_does_not_preempt_global_protected) { |
| 72 | ASSERT_EQ(3370318, main_global_protected_get_serial()); |
| 73 | } |
| 74 | |
| 75 | // check same things for lib |
| 76 | TEST(dl, lib_preempts_global_default) { |
| 77 | ASSERT_EQ(3370318, lib_global_default_get_serial()); |
| 78 | } |
| 79 | |
| 80 | TEST(dl, lib_does_not_preempt_global_protected) { |
| 81 | ASSERT_EQ(3370318, lib_global_protected_get_serial()); |
| 82 | } |
| 83 | |
Dimitry Ivanov | 2a6955e | 2017-02-23 11:53:43 -0800 | [diff] [blame] | 84 | #if defined(__BIONIC__) |
| 85 | #if defined(__LP64__) |
| 86 | static constexpr const char* kPathToLinker = "/system/bin/linker64"; |
| 87 | #else |
| 88 | static constexpr const char* kPathToLinker = "/system/bin/linker"; |
| 89 | #endif |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame] | 90 | |
| 91 | #if defined (__aarch64__) |
| 92 | static constexpr const char* kAlternatePathToLinker = "/system/bin/arm64/linker64"; |
| 93 | #elif defined (__arm__) |
| 94 | static constexpr const char* kAlternatePathToLinker = "/system/bin/arm/linker"; |
| 95 | #elif defined (__x86_64__) |
| 96 | static constexpr const char* kAlternatePathToLinker = "/system/bin/x86_64/linker64"; |
| 97 | #elif defined (__i386__) |
| 98 | static constexpr const char* kAlternatePathToLinker = "/system/bin/x86/linker"; |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame] | 99 | #else |
| 100 | #error "Unknown architecture" |
| 101 | #endif |
| 102 | |
| 103 | const char* PathToLinker() { |
| 104 | // On the systems with emulated architecture linker would be of different |
| 105 | // architecture. Try to use alternate paths first. |
| 106 | struct stat buffer; |
| 107 | if (stat(kAlternatePathToLinker, &buffer) == 0) { |
| 108 | return kAlternatePathToLinker; |
| 109 | } |
| 110 | return kPathToLinker; |
| 111 | } |
| 112 | #endif // defined(__BIONIC__) |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 113 | |
| 114 | TEST(dl, exec_linker) { |
| 115 | #if defined(__BIONIC__) |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame] | 116 | const char* path_to_linker = PathToLinker(); |
| 117 | std::string usage_prefix = std::string("Usage: ") + path_to_linker; |
Dimitry Ivanov | 2a6955e | 2017-02-23 11:53:43 -0800 | [diff] [blame] | 118 | ExecTestHelper eth; |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame] | 119 | eth.SetArgs({ path_to_linker, nullptr }); |
| 120 | eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, nullptr); |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 121 | ASSERT_EQ(0u, eth.GetOutput().find(usage_prefix)) << "Test output:\n" << eth.GetOutput(); |
| 122 | #endif |
| 123 | } |
| 124 | |
| 125 | TEST(dl, exec_linker_load_file) { |
| 126 | #if defined(__BIONIC__) |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame] | 127 | const char* path_to_linker = PathToLinker(); |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 128 | std::string helper = GetTestlibRoot() + |
| 129 | "/exec_linker_helper/exec_linker_helper"; |
| 130 | std::string expected_output = |
| 131 | "ctor: argc=1 argv[0]=" + helper + "\n" + |
| 132 | "main: argc=1 argv[0]=" + helper + "\n" + |
Elliott Hughes | 75064c1 | 2020-01-22 20:46:12 -0800 | [diff] [blame] | 133 | "__progname=exec_linker_helper\n" + |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 134 | "helper_func called\n"; |
| 135 | ExecTestHelper eth; |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame] | 136 | eth.SetArgs({ path_to_linker, helper.c_str(), nullptr }); |
| 137 | eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, expected_output.c_str()); |
Dimitry Ivanov | 2a6955e | 2017-02-23 11:53:43 -0800 | [diff] [blame] | 138 | #endif |
| 139 | } |
| 140 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 141 | TEST(dl, exec_linker_load_from_zip) { |
| 142 | #if defined(__BIONIC__) |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame] | 143 | const char* path_to_linker = PathToLinker(); |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 144 | std::string helper = GetTestlibRoot() + |
| 145 | "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip!/libdir/exec_linker_helper"; |
| 146 | std::string expected_output = |
| 147 | "ctor: argc=1 argv[0]=" + helper + "\n" + |
| 148 | "main: argc=1 argv[0]=" + helper + "\n" + |
Elliott Hughes | 75064c1 | 2020-01-22 20:46:12 -0800 | [diff] [blame] | 149 | "__progname=exec_linker_helper\n" + |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 150 | "helper_func called\n"; |
| 151 | ExecTestHelper eth; |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame] | 152 | eth.SetArgs({ path_to_linker, helper.c_str(), nullptr }); |
| 153 | eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, expected_output.c_str()); |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 154 | #endif |
| 155 | } |
| 156 | |
| 157 | TEST(dl, exec_linker_load_self) { |
| 158 | #if defined(__BIONIC__) |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame] | 159 | const char* path_to_linker = PathToLinker(); |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 160 | std::string error_message = "error: linker cannot load itself\n"; |
| 161 | ExecTestHelper eth; |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame] | 162 | eth.SetArgs({ path_to_linker, path_to_linker, nullptr }); |
| 163 | eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str()); |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 164 | #endif |
| 165 | } |
| 166 | |
Elliott Hughes | eb04ed5 | 2017-03-29 13:48:02 -0700 | [diff] [blame] | 167 | TEST(dl, preinit_system_calls) { |
| 168 | #if defined(__BIONIC__) |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 169 | SKIP_WITH_HWASAN << "hwasan not initialized in preinit_array, b/124007027"; |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 170 | std::string helper = GetTestlibRoot() + |
Elliott Hughes | eb04ed5 | 2017-03-29 13:48:02 -0700 | [diff] [blame] | 171 | "/preinit_syscall_test_helper/preinit_syscall_test_helper"; |
| 172 | chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607 |
| 173 | ExecTestHelper eth; |
| 174 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 175 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr); |
| 176 | #endif |
| 177 | } |
| 178 | |
Ryan Prichard | 5a66490 | 2018-11-22 02:14:14 -0800 | [diff] [blame] | 179 | TEST(dl, preinit_getauxval) { |
Elliott Hughes | eb04ed5 | 2017-03-29 13:48:02 -0700 | [diff] [blame] | 180 | #if defined(__BIONIC__) |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 181 | SKIP_WITH_HWASAN << "hwasan not initialized in preinit_array, b/124007027"; |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 182 | std::string helper = GetTestlibRoot() + |
Elliott Hughes | eb04ed5 | 2017-03-29 13:48:02 -0700 | [diff] [blame] | 183 | "/preinit_getauxval_test_helper/preinit_getauxval_test_helper"; |
| 184 | chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607 |
| 185 | ExecTestHelper eth; |
| 186 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 187 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr); |
Christopher Ferris | 01db9bd | 2018-11-07 14:39:43 -0800 | [diff] [blame] | 188 | #else |
| 189 | // Force a failure when not compiled for bionic so the test is considered a pass. |
| 190 | ASSERT_TRUE(false); |
Elliott Hughes | eb04ed5 | 2017-03-29 13:48:02 -0700 | [diff] [blame] | 191 | #endif |
| 192 | } |
| 193 | |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 194 | |
| 195 | TEST(dl, exec_without_ld_preload) { |
| 196 | #if defined(__BIONIC__) |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 197 | std::string helper = GetTestlibRoot() + |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 198 | "/ld_preload_test_helper/ld_preload_test_helper"; |
| 199 | chmod(helper.c_str(), 0755); |
| 200 | ExecTestHelper eth; |
| 201 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 202 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "12345"); |
| 203 | #endif |
| 204 | } |
| 205 | |
| 206 | TEST(dl, exec_with_ld_preload) { |
| 207 | #if defined(__BIONIC__) |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 208 | std::string helper = GetTestlibRoot() + |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 209 | "/ld_preload_test_helper/ld_preload_test_helper"; |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 210 | std::string env = std::string("LD_PRELOAD=") + GetTestlibRoot() + "/ld_preload_test_helper_lib2.so"; |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 211 | chmod(helper.c_str(), 0755); |
| 212 | ExecTestHelper eth; |
| 213 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 214 | eth.SetEnv({ env.c_str(), nullptr }); |
| 215 | // ld_preload_test_helper calls get_value_from_lib() and returns the value. |
| 216 | // The symbol is defined by two libs: ld_preload_test_helper_lib.so and |
| 217 | // ld_preloaded_lib.so. The former is DT_NEEDED and the latter is LD_PRELOADED |
| 218 | // via this execution. The main executable is linked to the LD_PRELOADED lib |
| 219 | // and the value given from the lib is returned. |
| 220 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321"); |
| 221 | #endif |
| 222 | } |
| 223 | |
| 224 | |
| 225 | // ld_config_test_helper must fail because it is depending on a lib which is not |
| 226 | // in the search path |
| 227 | // |
| 228 | // Call sequence is... |
| 229 | // _helper -- (get_value_from_lib()) --> |
| 230 | // _lib1.so -- (get_value_from_another_lib()) --> |
| 231 | // _lib2.so (returns 12345) |
| 232 | // The two libs are in ns2/ subdir. |
| 233 | TEST(dl, exec_without_ld_config_file) { |
| 234 | #if defined(__BIONIC__) |
Josh Gao | 1626957 | 2019-10-29 13:41:00 -0700 | [diff] [blame] | 235 | std::string error_message = |
| 236 | "CANNOT LINK EXECUTABLE \"" + GetTestlibRoot() + |
| 237 | "/ld_config_test_helper/ld_config_test_helper\": library \"ld_config_test_helper_lib1.so\" " |
| 238 | "not found: needed by main executable\n"; |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 239 | std::string helper = GetTestlibRoot() + |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 240 | "/ld_config_test_helper/ld_config_test_helper"; |
| 241 | chmod(helper.c_str(), 0755); |
| 242 | ExecTestHelper eth; |
| 243 | eth.SetArgs({ helper.c_str(), nullptr }); |
dimitry | 04f7a79 | 2017-09-29 11:52:17 +0200 | [diff] [blame] | 244 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str()); |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 245 | #endif |
| 246 | } |
| 247 | |
| 248 | #if defined(__BIONIC__) |
dimitry | 1280cf5 | 2018-05-09 14:37:47 +0200 | [diff] [blame] | 249 | extern "C" void android_get_LD_LIBRARY_PATH(char*, size_t); |
Ryan Prichard | 0044cd1 | 2018-04-03 20:03:12 -0700 | [diff] [blame] | 250 | static void create_ld_config_file(const char* config_file) { |
dimitry | 1280cf5 | 2018-05-09 14:37:47 +0200 | [diff] [blame] | 251 | char default_search_paths[PATH_MAX]; |
| 252 | android_get_LD_LIBRARY_PATH(default_search_paths, sizeof(default_search_paths)); |
| 253 | |
Ryan Prichard | 0044cd1 | 2018-04-03 20:03:12 -0700 | [diff] [blame] | 254 | std::ofstream fout(config_file, std::ios::out); |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 255 | fout << "dir.test = " << GetTestlibRoot() << "/ld_config_test_helper/" << std::endl |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 256 | << "[test]" << std::endl |
| 257 | << "additional.namespaces = ns2" << std::endl |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 258 | << "namespace.default.search.paths = " << GetTestlibRoot() << std::endl |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 259 | << "namespace.default.links = ns2" << std::endl |
| 260 | << "namespace.default.link.ns2.shared_libs = libc.so:libm.so:libdl.so:ld_config_test_helper_lib1.so" << std::endl |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 261 | << "namespace.ns2.search.paths = " << default_search_paths << ":" << GetTestlibRoot() << "/ns2" << std::endl; |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 262 | fout.close(); |
| 263 | } |
| 264 | #endif |
| 265 | |
Jiyong Park | 41704cd | 2017-09-19 09:48:07 +0900 | [diff] [blame] | 266 | #if defined(__BIONIC__) |
Ryan Prichard | 546723b | 2021-06-04 17:27:39 -0700 | [diff] [blame] | 267 | // This test can't rely on ro.debuggable, because it might have been forced on |
| 268 | // in a user build ("Force Debuggable"). In that configuration, ro.debuggable is |
| 269 | // true, but Bionic's LD_CONFIG_FILE testing support is still disabled. |
| 270 | static bool is_user_build() { |
| 271 | return android::base::GetProperty("ro.build.type", "user") == std::string("user"); |
Jiyong Park | 41704cd | 2017-09-19 09:48:07 +0900 | [diff] [blame] | 272 | } |
| 273 | #endif |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 274 | |
Ryan Prichard | 058eb8f | 2020-12-17 22:59:04 -0800 | [diff] [blame] | 275 | // lib1.so and lib2.so are now searchable by having another namespace 'ns2' |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 276 | // whose search paths include the 'ns2/' subdir. |
Ryan Prichard | 058eb8f | 2020-12-17 22:59:04 -0800 | [diff] [blame] | 277 | // |
| 278 | // lib1.so is linked with DF_1_GLOBAL, so both it and the executable are added |
| 279 | // to every namespace. |
| 280 | // |
| 281 | // namespace configuration ('*' indicates primary ns) |
| 282 | // - default: exe[*], lib1.so |
| 283 | // - ns2: exe, lib1.so[*], lib2.so[*] |
| 284 | // |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 285 | TEST(dl, exec_with_ld_config_file) { |
| 286 | #if defined(__BIONIC__) |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 287 | SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config"; |
Ryan Prichard | 546723b | 2021-06-04 17:27:39 -0700 | [diff] [blame] | 288 | if (is_user_build()) { |
Ryan Prichard | 55f9a24 | 2019-12-10 14:45:20 -0800 | [diff] [blame] | 289 | GTEST_SKIP() << "LD_CONFIG_FILE is not supported on user build"; |
Jiyong Park | 41704cd | 2017-09-19 09:48:07 +0900 | [diff] [blame] | 290 | } |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 291 | std::string helper = GetTestlibRoot() + |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 292 | "/ld_config_test_helper/ld_config_test_helper"; |
Ryan Prichard | 0044cd1 | 2018-04-03 20:03:12 -0700 | [diff] [blame] | 293 | TemporaryFile config_file; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 294 | create_ld_config_file(config_file.path); |
| 295 | std::string env = std::string("LD_CONFIG_FILE=") + config_file.path; |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 296 | chmod(helper.c_str(), 0755); |
| 297 | ExecTestHelper eth; |
| 298 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 299 | eth.SetEnv({ env.c_str(), nullptr }); |
Ryan Prichard | 058eb8f | 2020-12-17 22:59:04 -0800 | [diff] [blame] | 300 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, |
| 301 | "foo lib1\n" |
| 302 | "lib1_call_funcs\n" |
| 303 | "foo lib1\n" |
| 304 | "bar lib2\n"); |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 305 | #endif |
| 306 | } |
| 307 | |
Ryan Prichard | 058eb8f | 2020-12-17 22:59:04 -0800 | [diff] [blame] | 308 | // lib3.so has same foo and bar symbols as lib2.so. lib3.so is LD_PRELOADed. |
| 309 | // This test ensures that LD_PRELOADed libs are available to all namespaces. |
| 310 | // |
| 311 | // namespace configuration ('*' indicates primary ns) |
| 312 | // - default: exe[*], lib3.so[*], lib1.so |
| 313 | // - ns2: exe, lib3.so, lib1.so[*], lib2.so[*] |
| 314 | // |
| 315 | // Ensure that, in both namespaces, a call to foo calls the lib3.so symbol, |
| 316 | // which then calls the lib1.so symbol using RTLD_NEXT. Ensure that RTLD_NEXT |
| 317 | // finds nothing when called from lib1.so. |
| 318 | // |
| 319 | // For the bar symbol, lib3.so's primary namespace is the default namespace, but |
| 320 | // lib2.so is not in the default namespace, so using RTLD_NEXT from lib3.so |
| 321 | // doesn't find the symbol in lib2.so. |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 322 | TEST(dl, exec_with_ld_config_file_with_ld_preload) { |
| 323 | #if defined(__BIONIC__) |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 324 | SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config"; |
Ryan Prichard | 546723b | 2021-06-04 17:27:39 -0700 | [diff] [blame] | 325 | if (is_user_build()) { |
Ryan Prichard | 55f9a24 | 2019-12-10 14:45:20 -0800 | [diff] [blame] | 326 | GTEST_SKIP() << "LD_CONFIG_FILE is not supported on user build"; |
Jiyong Park | 41704cd | 2017-09-19 09:48:07 +0900 | [diff] [blame] | 327 | } |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 328 | std::string helper = GetTestlibRoot() + |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 329 | "/ld_config_test_helper/ld_config_test_helper"; |
Ryan Prichard | 0044cd1 | 2018-04-03 20:03:12 -0700 | [diff] [blame] | 330 | TemporaryFile config_file; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 331 | create_ld_config_file(config_file.path); |
| 332 | std::string env = std::string("LD_CONFIG_FILE=") + config_file.path; |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 333 | std::string env2 = std::string("LD_PRELOAD=") + GetTestlibRoot() + "/ld_config_test_helper_lib3.so"; |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 334 | chmod(helper.c_str(), 0755); |
| 335 | ExecTestHelper eth; |
| 336 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 337 | eth.SetEnv({ env.c_str(), env2.c_str(), nullptr }); |
Ryan Prichard | 058eb8f | 2020-12-17 22:59:04 -0800 | [diff] [blame] | 338 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, |
| 339 | "foo lib3\n" |
| 340 | "foo lib1\n" |
| 341 | "lib1_call_funcs\n" |
| 342 | "foo lib3\n" |
| 343 | "foo lib1\n" |
| 344 | "bar lib3\n" |
| 345 | "lib3_call_funcs\n" |
| 346 | "foo lib3\n" |
| 347 | "foo lib1\n" |
| 348 | "bar lib3\n"); |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 349 | #endif |
| 350 | } |
| 351 | |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 352 | // ensures that LD_CONFIG_FILE env var does not work for production builds. |
| 353 | // The test input is the same as exec_with_ld_config_file, but it must fail in |
| 354 | // this case. |
| 355 | TEST(dl, disable_ld_config_file) { |
| 356 | #if defined(__BIONIC__) |
| 357 | if (getuid() == 0) { |
| 358 | // when executed from the shell (e.g. not as part of CTS), skip the test. |
| 359 | // This test is only for CTS. |
Ryan Prichard | 55f9a24 | 2019-12-10 14:45:20 -0800 | [diff] [blame] | 360 | GTEST_SKIP() << "test is not supported with root uid"; |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 361 | } |
Ryan Prichard | 546723b | 2021-06-04 17:27:39 -0700 | [diff] [blame] | 362 | if (!is_user_build()) { |
| 363 | GTEST_SKIP() << "test requires user build"; |
Jiyong Park | 4945d8f | 2017-08-30 11:30:53 +0900 | [diff] [blame] | 364 | } |
| 365 | |
Ryan Prichard | 0f67214 | 2019-12-10 14:50:11 -0800 | [diff] [blame] | 366 | std::string error_message = std::string("CANNOT LINK EXECUTABLE ") + |
| 367 | "\"" + GetTestlibRoot() + "/ld_config_test_helper/ld_config_test_helper\": " + |
| 368 | "library \"ld_config_test_helper_lib1.so\" not found: needed by main executable\n"; |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 369 | std::string helper = GetTestlibRoot() + |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 370 | "/ld_config_test_helper/ld_config_test_helper"; |
Ryan Prichard | 0044cd1 | 2018-04-03 20:03:12 -0700 | [diff] [blame] | 371 | TemporaryFile config_file; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 372 | create_ld_config_file(config_file.path); |
| 373 | std::string env = std::string("LD_CONFIG_FILE=") + config_file.path; |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 374 | chmod(helper.c_str(), 0755); |
| 375 | ExecTestHelper eth; |
| 376 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 377 | eth.SetEnv({ env.c_str(), nullptr }); |
Jiyong Park | 9828386 | 2017-11-28 13:37:03 +0900 | [diff] [blame] | 378 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str()); |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 379 | #endif |
| 380 | } |
Elliott Hughes | 6dd1f58 | 2020-01-28 12:18:35 -0800 | [diff] [blame] | 381 | |
| 382 | static void RelocationsTest(const char* lib, const char* expectation) { |
| 383 | #if defined(__BIONIC__) |
| 384 | // Does readelf think the .so file looks right? |
| 385 | const std::string path = GetTestlibRoot() + "/" + lib; |
| 386 | ExecTestHelper eth; |
| 387 | eth.SetArgs({ "readelf", "-SW", path.c_str(), nullptr }); |
| 388 | eth.Run([&]() { execvpe("readelf", eth.GetArgs(), eth.GetEnv()); }, 0, nullptr); |
Elliott Hughes | ec580d3 | 2021-04-12 15:55:29 -0700 | [diff] [blame] | 389 | |
| 390 | ASSERT_TRUE(std::regex_search(eth.GetOutput(), std::regex(expectation))) << eth.GetOutput(); |
Elliott Hughes | 6dd1f58 | 2020-01-28 12:18:35 -0800 | [diff] [blame] | 391 | |
| 392 | // Can we load it? |
| 393 | void* handle = dlopen(lib, RTLD_NOW); |
| 394 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 395 | #else |
| 396 | UNUSED(lib); |
| 397 | UNUSED(expectation); |
| 398 | GTEST_SKIP() << "test is not supported on glibc"; |
| 399 | #endif |
| 400 | } |
| 401 | |
| 402 | TEST(dl, relocations_RELR) { |
Elliott Hughes | ec580d3 | 2021-04-12 15:55:29 -0700 | [diff] [blame] | 403 | RelocationsTest("librelocations-RELR.so", "\\.relr\\.dyn * RELR"); |
Elliott Hughes | 6dd1f58 | 2020-01-28 12:18:35 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | TEST(dl, relocations_ANDROID_RELR) { |
Elliott Hughes | ec580d3 | 2021-04-12 15:55:29 -0700 | [diff] [blame] | 407 | RelocationsTest("librelocations-ANDROID_RELR.so", "\\.relr\\.dyn * ANDROID_RELR"); |
Elliott Hughes | 6dd1f58 | 2020-01-28 12:18:35 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | TEST(dl, relocations_ANDROID_REL) { |
| 411 | RelocationsTest("librelocations-ANDROID_REL.so", |
| 412 | #if __LP64__ |
Elliott Hughes | ec580d3 | 2021-04-12 15:55:29 -0700 | [diff] [blame] | 413 | "\\.rela\\.dyn * ANDROID_RELA" |
Elliott Hughes | 6dd1f58 | 2020-01-28 12:18:35 -0800 | [diff] [blame] | 414 | #else |
Elliott Hughes | ec580d3 | 2021-04-12 15:55:29 -0700 | [diff] [blame] | 415 | "\\.rel\\.dyn * ANDROID_REL" |
Elliott Hughes | 6dd1f58 | 2020-01-28 12:18:35 -0800 | [diff] [blame] | 416 | #endif |
Elliott Hughes | ec580d3 | 2021-04-12 15:55:29 -0700 | [diff] [blame] | 417 | ); |
Elliott Hughes | 6dd1f58 | 2020-01-28 12:18:35 -0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | TEST(dl, relocations_fat) { |
| 421 | RelocationsTest("librelocations-fat.so", |
| 422 | #if __LP64__ |
Elliott Hughes | ec580d3 | 2021-04-12 15:55:29 -0700 | [diff] [blame] | 423 | "\\.rela\\.dyn * RELA" |
Elliott Hughes | 6dd1f58 | 2020-01-28 12:18:35 -0800 | [diff] [blame] | 424 | #else |
Elliott Hughes | ec580d3 | 2021-04-12 15:55:29 -0700 | [diff] [blame] | 425 | "\\.rel\\.dyn * REL" |
Elliott Hughes | 6dd1f58 | 2020-01-28 12:18:35 -0800 | [diff] [blame] | 426 | #endif |
Elliott Hughes | ec580d3 | 2021-04-12 15:55:29 -0700 | [diff] [blame] | 427 | ); |
Elliott Hughes | 6dd1f58 | 2020-01-28 12:18:35 -0800 | [diff] [blame] | 428 | } |