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