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