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 | |
| 30 | #include <string> |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 31 | #include <iostream> |
| 32 | #include <fstream> |
Dmitriy Ivanov | 0416d88 | 2014-11-04 09:38:18 -0800 | [diff] [blame] | 33 | |
Elliott Hughes | eb04ed5 | 2017-03-29 13:48:02 -0700 | [diff] [blame] | 34 | #include "gtest_globals.h" |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 35 | #include <android-base/file.h> |
Dimitry Ivanov | 2a6955e | 2017-02-23 11:53:43 -0800 | [diff] [blame] | 36 | #include "utils.h" |
| 37 | |
Dmitriy Ivanov | 0416d88 | 2014-11-04 09:38:18 -0800 | [diff] [blame] | 38 | extern "C" int main_global_default_serial() { |
| 39 | return 3370318; |
| 40 | } |
| 41 | |
| 42 | extern "C" int main_global_protected_serial() { |
| 43 | return 2716057; |
| 44 | } |
| 45 | |
| 46 | // The following functions are defined in DT_NEEDED |
| 47 | // libdl_preempt_test.so library. |
| 48 | |
| 49 | // This one calls main_global_default_serial |
| 50 | extern "C" int main_global_default_get_serial(); |
| 51 | |
| 52 | // This one calls main_global_protected_serial |
| 53 | extern "C" int main_global_protected_get_serial(); |
| 54 | |
| 55 | // This one calls lib_global_default_serial |
| 56 | extern "C" int lib_global_default_get_serial(); |
| 57 | |
| 58 | // This one calls lib_global_protected_serial |
| 59 | extern "C" int lib_global_protected_get_serial(); |
| 60 | |
| 61 | // This test verifies that the global default function |
| 62 | // main_global_default_serial() is preempted by |
| 63 | // the function defined above. |
| 64 | TEST(dl, main_preempts_global_default) { |
| 65 | ASSERT_EQ(3370318, main_global_default_get_serial()); |
| 66 | } |
| 67 | |
| 68 | // This one makes sure that the global protected |
| 69 | // symbols do not get preempted |
| 70 | TEST(dl, main_does_not_preempt_global_protected) { |
| 71 | ASSERT_EQ(3370318, main_global_protected_get_serial()); |
| 72 | } |
| 73 | |
| 74 | // check same things for lib |
| 75 | TEST(dl, lib_preempts_global_default) { |
| 76 | ASSERT_EQ(3370318, lib_global_default_get_serial()); |
| 77 | } |
| 78 | |
| 79 | TEST(dl, lib_does_not_preempt_global_protected) { |
| 80 | ASSERT_EQ(3370318, lib_global_protected_get_serial()); |
| 81 | } |
| 82 | |
Dimitry Ivanov | 2a6955e | 2017-02-23 11:53:43 -0800 | [diff] [blame] | 83 | #if defined(__BIONIC__) |
| 84 | #if defined(__LP64__) |
| 85 | static constexpr const char* kPathToLinker = "/system/bin/linker64"; |
| 86 | #else |
| 87 | static constexpr const char* kPathToLinker = "/system/bin/linker"; |
| 88 | #endif |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame^] | 89 | |
| 90 | #if defined (__aarch64__) |
| 91 | static constexpr const char* kAlternatePathToLinker = "/system/bin/arm64/linker64"; |
| 92 | #elif defined (__arm__) |
| 93 | static constexpr const char* kAlternatePathToLinker = "/system/bin/arm/linker"; |
| 94 | #elif defined (__x86_64__) |
| 95 | static constexpr const char* kAlternatePathToLinker = "/system/bin/x86_64/linker64"; |
| 96 | #elif defined (__i386__) |
| 97 | static constexpr const char* kAlternatePathToLinker = "/system/bin/x86/linker"; |
| 98 | #elif defined (__mips__) |
| 99 | #if defined(__LP64__) |
| 100 | static constexpr const char* kAlternatePathToLinker = "/system/bin/mips64/linker64"; |
| 101 | #else |
| 102 | static constexpr const char* kAlternatePathToLinker = "/system/bin/mips/linker"; |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 103 | #endif |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame^] | 104 | #else |
| 105 | #error "Unknown architecture" |
| 106 | #endif |
| 107 | |
| 108 | const char* PathToLinker() { |
| 109 | // On the systems with emulated architecture linker would be of different |
| 110 | // architecture. Try to use alternate paths first. |
| 111 | struct stat buffer; |
| 112 | if (stat(kAlternatePathToLinker, &buffer) == 0) { |
| 113 | return kAlternatePathToLinker; |
| 114 | } |
| 115 | return kPathToLinker; |
| 116 | } |
| 117 | #endif // defined(__BIONIC__) |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 118 | |
| 119 | TEST(dl, exec_linker) { |
| 120 | #if defined(__BIONIC__) |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame^] | 121 | const char* path_to_linker = PathToLinker(); |
| 122 | std::string usage_prefix = std::string("Usage: ") + path_to_linker; |
Dimitry Ivanov | 2a6955e | 2017-02-23 11:53:43 -0800 | [diff] [blame] | 123 | ExecTestHelper eth; |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame^] | 124 | eth.SetArgs({ path_to_linker, nullptr }); |
| 125 | eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, nullptr); |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 126 | ASSERT_EQ(0u, eth.GetOutput().find(usage_prefix)) << "Test output:\n" << eth.GetOutput(); |
| 127 | #endif |
| 128 | } |
| 129 | |
| 130 | TEST(dl, exec_linker_load_file) { |
| 131 | #if defined(__BIONIC__) |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame^] | 132 | const char* path_to_linker = PathToLinker(); |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 133 | std::string helper = GetTestlibRoot() + |
| 134 | "/exec_linker_helper/exec_linker_helper"; |
| 135 | std::string expected_output = |
| 136 | "ctor: argc=1 argv[0]=" + helper + "\n" + |
| 137 | "main: argc=1 argv[0]=" + helper + "\n" + |
Ryan Prichard | 48b1159 | 2018-11-22 02:41:36 -0800 | [diff] [blame] | 138 | "__progname=" + helper + "\n" + |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 139 | "helper_func called\n"; |
| 140 | ExecTestHelper eth; |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame^] | 141 | eth.SetArgs({ path_to_linker, helper.c_str(), nullptr }); |
| 142 | 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] | 143 | #endif |
| 144 | } |
| 145 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 146 | TEST(dl, exec_linker_load_from_zip) { |
| 147 | #if defined(__BIONIC__) |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame^] | 148 | const char* path_to_linker = PathToLinker(); |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 149 | std::string helper = GetTestlibRoot() + |
| 150 | "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip!/libdir/exec_linker_helper"; |
| 151 | std::string expected_output = |
| 152 | "ctor: argc=1 argv[0]=" + helper + "\n" + |
| 153 | "main: argc=1 argv[0]=" + helper + "\n" + |
Ryan Prichard | 48b1159 | 2018-11-22 02:41:36 -0800 | [diff] [blame] | 154 | "__progname=" + helper + "\n" + |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 155 | "helper_func called\n"; |
| 156 | ExecTestHelper eth; |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame^] | 157 | eth.SetArgs({ path_to_linker, helper.c_str(), nullptr }); |
| 158 | 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] | 159 | #endif |
| 160 | } |
| 161 | |
| 162 | TEST(dl, exec_linker_load_self) { |
| 163 | #if defined(__BIONIC__) |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame^] | 164 | const char* path_to_linker = PathToLinker(); |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 165 | std::string error_message = "error: linker cannot load itself\n"; |
| 166 | ExecTestHelper eth; |
Dmytro Chystiakov | 595c381 | 2019-10-01 11:28:49 -0700 | [diff] [blame^] | 167 | eth.SetArgs({ path_to_linker, path_to_linker, nullptr }); |
| 168 | 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] | 169 | #endif |
| 170 | } |
| 171 | |
Elliott Hughes | eb04ed5 | 2017-03-29 13:48:02 -0700 | [diff] [blame] | 172 | TEST(dl, preinit_system_calls) { |
| 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"; |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 175 | std::string helper = GetTestlibRoot() + |
Elliott Hughes | eb04ed5 | 2017-03-29 13:48:02 -0700 | [diff] [blame] | 176 | "/preinit_syscall_test_helper/preinit_syscall_test_helper"; |
| 177 | chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607 |
| 178 | ExecTestHelper eth; |
| 179 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 180 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr); |
| 181 | #endif |
| 182 | } |
| 183 | |
Ryan Prichard | 5a66490 | 2018-11-22 02:14:14 -0800 | [diff] [blame] | 184 | TEST(dl, preinit_getauxval) { |
Elliott Hughes | eb04ed5 | 2017-03-29 13:48:02 -0700 | [diff] [blame] | 185 | #if defined(__BIONIC__) |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 186 | SKIP_WITH_HWASAN << "hwasan not initialized in preinit_array, b/124007027"; |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 187 | std::string helper = GetTestlibRoot() + |
Elliott Hughes | eb04ed5 | 2017-03-29 13:48:02 -0700 | [diff] [blame] | 188 | "/preinit_getauxval_test_helper/preinit_getauxval_test_helper"; |
| 189 | chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607 |
| 190 | ExecTestHelper eth; |
| 191 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 192 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr); |
Christopher Ferris | 01db9bd | 2018-11-07 14:39:43 -0800 | [diff] [blame] | 193 | #else |
| 194 | // Force a failure when not compiled for bionic so the test is considered a pass. |
| 195 | ASSERT_TRUE(false); |
Elliott Hughes | eb04ed5 | 2017-03-29 13:48:02 -0700 | [diff] [blame] | 196 | #endif |
| 197 | } |
| 198 | |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 199 | |
| 200 | TEST(dl, exec_without_ld_preload) { |
| 201 | #if defined(__BIONIC__) |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 202 | std::string helper = GetTestlibRoot() + |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 203 | "/ld_preload_test_helper/ld_preload_test_helper"; |
| 204 | chmod(helper.c_str(), 0755); |
| 205 | ExecTestHelper eth; |
| 206 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 207 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "12345"); |
| 208 | #endif |
| 209 | } |
| 210 | |
| 211 | TEST(dl, exec_with_ld_preload) { |
| 212 | #if defined(__BIONIC__) |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 213 | std::string helper = GetTestlibRoot() + |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 214 | "/ld_preload_test_helper/ld_preload_test_helper"; |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 215 | 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] | 216 | chmod(helper.c_str(), 0755); |
| 217 | ExecTestHelper eth; |
| 218 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 219 | eth.SetEnv({ env.c_str(), nullptr }); |
| 220 | // ld_preload_test_helper calls get_value_from_lib() and returns the value. |
| 221 | // The symbol is defined by two libs: ld_preload_test_helper_lib.so and |
| 222 | // ld_preloaded_lib.so. The former is DT_NEEDED and the latter is LD_PRELOADED |
| 223 | // via this execution. The main executable is linked to the LD_PRELOADED lib |
| 224 | // and the value given from the lib is returned. |
| 225 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321"); |
| 226 | #endif |
| 227 | } |
| 228 | |
| 229 | |
| 230 | // ld_config_test_helper must fail because it is depending on a lib which is not |
| 231 | // in the search path |
| 232 | // |
| 233 | // Call sequence is... |
| 234 | // _helper -- (get_value_from_lib()) --> |
| 235 | // _lib1.so -- (get_value_from_another_lib()) --> |
| 236 | // _lib2.so (returns 12345) |
| 237 | // The two libs are in ns2/ subdir. |
| 238 | TEST(dl, exec_without_ld_config_file) { |
| 239 | #if defined(__BIONIC__) |
Josh Gao | 1626957 | 2019-10-29 13:41:00 -0700 | [diff] [blame] | 240 | std::string error_message = |
| 241 | "CANNOT LINK EXECUTABLE \"" + GetTestlibRoot() + |
| 242 | "/ld_config_test_helper/ld_config_test_helper\": library \"ld_config_test_helper_lib1.so\" " |
| 243 | "not found: needed by main executable\n"; |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 244 | std::string helper = GetTestlibRoot() + |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 245 | "/ld_config_test_helper/ld_config_test_helper"; |
| 246 | chmod(helper.c_str(), 0755); |
| 247 | ExecTestHelper eth; |
| 248 | eth.SetArgs({ helper.c_str(), nullptr }); |
dimitry | 04f7a79 | 2017-09-29 11:52:17 +0200 | [diff] [blame] | 249 | 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] | 250 | #endif |
| 251 | } |
| 252 | |
| 253 | #if defined(__BIONIC__) |
dimitry | 1280cf5 | 2018-05-09 14:37:47 +0200 | [diff] [blame] | 254 | extern "C" void android_get_LD_LIBRARY_PATH(char*, size_t); |
Ryan Prichard | 0044cd1 | 2018-04-03 20:03:12 -0700 | [diff] [blame] | 255 | static void create_ld_config_file(const char* config_file) { |
dimitry | 1280cf5 | 2018-05-09 14:37:47 +0200 | [diff] [blame] | 256 | char default_search_paths[PATH_MAX]; |
| 257 | android_get_LD_LIBRARY_PATH(default_search_paths, sizeof(default_search_paths)); |
| 258 | |
Ryan Prichard | 0044cd1 | 2018-04-03 20:03:12 -0700 | [diff] [blame] | 259 | std::ofstream fout(config_file, std::ios::out); |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 260 | fout << "dir.test = " << GetTestlibRoot() << "/ld_config_test_helper/" << std::endl |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 261 | << "[test]" << std::endl |
| 262 | << "additional.namespaces = ns2" << std::endl |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 263 | << "namespace.default.search.paths = " << GetTestlibRoot() << std::endl |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 264 | << "namespace.default.links = ns2" << std::endl |
| 265 | << "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] | 266 | << "namespace.ns2.search.paths = " << default_search_paths << ":" << GetTestlibRoot() << "/ns2" << std::endl; |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 267 | fout.close(); |
| 268 | } |
| 269 | #endif |
| 270 | |
Jiyong Park | 41704cd | 2017-09-19 09:48:07 +0900 | [diff] [blame] | 271 | #if defined(__BIONIC__) |
dimitry | 59d3062 | 2017-10-18 13:23:08 +0200 | [diff] [blame] | 272 | static bool is_debuggable_build() { |
| 273 | return android::base::GetBoolProperty("ro.debuggable", false); |
Jiyong Park | 41704cd | 2017-09-19 09:48:07 +0900 | [diff] [blame] | 274 | } |
| 275 | #endif |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 276 | |
| 277 | // _lib1.so and _lib2.so are now searchable by having another namespace 'ns2' |
| 278 | // whose search paths include the 'ns2/' subdir. |
| 279 | TEST(dl, exec_with_ld_config_file) { |
| 280 | #if defined(__BIONIC__) |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 281 | SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config"; |
dimitry | 59d3062 | 2017-10-18 13:23:08 +0200 | [diff] [blame] | 282 | if (!is_debuggable_build()) { |
Jiyong Park | 41704cd | 2017-09-19 09:48:07 +0900 | [diff] [blame] | 283 | // LD_CONFIG_FILE is not supported on user build |
| 284 | return; |
| 285 | } |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 286 | std::string helper = GetTestlibRoot() + |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 287 | "/ld_config_test_helper/ld_config_test_helper"; |
Ryan Prichard | 0044cd1 | 2018-04-03 20:03:12 -0700 | [diff] [blame] | 288 | TemporaryFile config_file; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 289 | create_ld_config_file(config_file.path); |
| 290 | std::string env = std::string("LD_CONFIG_FILE=") + config_file.path; |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 291 | chmod(helper.c_str(), 0755); |
| 292 | ExecTestHelper eth; |
| 293 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 294 | eth.SetEnv({ env.c_str(), nullptr }); |
| 295 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "12345"); |
| 296 | #endif |
| 297 | } |
| 298 | |
| 299 | // _lib3.so has same symbol as lib2.so but returns 54321. _lib3.so is |
| 300 | // LD_PRELOADed. This test is to ensure LD_PRELOADed libs are available to |
| 301 | // additional namespaces other than the default namespace. |
| 302 | TEST(dl, exec_with_ld_config_file_with_ld_preload) { |
| 303 | #if defined(__BIONIC__) |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 304 | SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config"; |
dimitry | 59d3062 | 2017-10-18 13:23:08 +0200 | [diff] [blame] | 305 | if (!is_debuggable_build()) { |
Jiyong Park | 41704cd | 2017-09-19 09:48:07 +0900 | [diff] [blame] | 306 | // LD_CONFIG_FILE is not supported on user build |
| 307 | return; |
| 308 | } |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 309 | std::string helper = GetTestlibRoot() + |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 310 | "/ld_config_test_helper/ld_config_test_helper"; |
Ryan Prichard | 0044cd1 | 2018-04-03 20:03:12 -0700 | [diff] [blame] | 311 | TemporaryFile config_file; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 312 | create_ld_config_file(config_file.path); |
| 313 | std::string env = std::string("LD_CONFIG_FILE=") + config_file.path; |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 314 | 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] | 315 | chmod(helper.c_str(), 0755); |
| 316 | ExecTestHelper eth; |
| 317 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 318 | eth.SetEnv({ env.c_str(), env2.c_str(), nullptr }); |
| 319 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321"); |
| 320 | #endif |
| 321 | } |
| 322 | |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 323 | // ensures that LD_CONFIG_FILE env var does not work for production builds. |
| 324 | // The test input is the same as exec_with_ld_config_file, but it must fail in |
| 325 | // this case. |
| 326 | TEST(dl, disable_ld_config_file) { |
| 327 | #if defined(__BIONIC__) |
| 328 | if (getuid() == 0) { |
| 329 | // when executed from the shell (e.g. not as part of CTS), skip the test. |
| 330 | // This test is only for CTS. |
| 331 | return; |
| 332 | } |
dimitry | 59d3062 | 2017-10-18 13:23:08 +0200 | [diff] [blame] | 333 | if (is_debuggable_build()) { |
Jiyong Park | 4945d8f | 2017-08-30 11:30:53 +0900 | [diff] [blame] | 334 | // Skip the test for non production devices |
| 335 | return; |
| 336 | } |
| 337 | |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 338 | std::string error_message = "CANNOT LINK EXECUTABLE \"" + GetTestlibRoot() + "/ld_config_test_helper/ld_config_test_helper\": library \"ld_config_test_helper_lib1.so\" not found\n"; |
| 339 | std::string helper = GetTestlibRoot() + |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 340 | "/ld_config_test_helper/ld_config_test_helper"; |
Ryan Prichard | 0044cd1 | 2018-04-03 20:03:12 -0700 | [diff] [blame] | 341 | TemporaryFile config_file; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 342 | create_ld_config_file(config_file.path); |
| 343 | std::string env = std::string("LD_CONFIG_FILE=") + config_file.path; |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 344 | chmod(helper.c_str(), 0755); |
| 345 | ExecTestHelper eth; |
| 346 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 347 | eth.SetEnv({ env.c_str(), nullptr }); |
Jiyong Park | 9828386 | 2017-11-28 13:37:03 +0900 | [diff] [blame] | 348 | 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] | 349 | #endif |
| 350 | } |