blob: 05bba0516182b02c9863dba489ab5f4100cecf14 [file] [log] [blame]
Dmitriy Ivanov0416d882014-11-04 09:38:18 -08001/*
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 Park4945d8f2017-08-30 11:30:53 +090019#if defined(__BIONIC__)
20#include <android-base/properties.h>
21#endif
22
Dmitriy Ivanov0416d882014-11-04 09:38:18 -080023#include <dlfcn.h>
24#include <libgen.h>
25#include <limits.h>
26#include <stdio.h>
27#include <stdint.h>
Dmytro Chystiakov595c3812019-10-01 11:28:49 -070028#include <sys/stat.h>
Dmitriy Ivanov0416d882014-11-04 09:38:18 -080029
30#include <string>
Jiyong Park02586a22017-05-20 01:01:24 +090031#include <iostream>
32#include <fstream>
Dmitriy Ivanov0416d882014-11-04 09:38:18 -080033
Elliott Hugheseb04ed52017-03-29 13:48:02 -070034#include "gtest_globals.h"
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080035#include <android-base/file.h>
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -080036#include "utils.h"
37
Dmitriy Ivanov0416d882014-11-04 09:38:18 -080038extern "C" int main_global_default_serial() {
39 return 3370318;
40}
41
42extern "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
50extern "C" int main_global_default_get_serial();
51
52// This one calls main_global_protected_serial
53extern "C" int main_global_protected_get_serial();
54
55// This one calls lib_global_default_serial
56extern "C" int lib_global_default_get_serial();
57
58// This one calls lib_global_protected_serial
59extern "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.
64TEST(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
70TEST(dl, main_does_not_preempt_global_protected) {
71 ASSERT_EQ(3370318, main_global_protected_get_serial());
72}
73
74// check same things for lib
75TEST(dl, lib_preempts_global_default) {
76 ASSERT_EQ(3370318, lib_global_default_get_serial());
77}
78
79TEST(dl, lib_does_not_preempt_global_protected) {
80 ASSERT_EQ(3370318, lib_global_protected_get_serial());
81}
82
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -080083#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 Chystiakov595c3812019-10-01 11:28:49 -070089
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 Prichard8f639a42018-10-01 23:10:05 -0700103#endif
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700104#else
105#error "Unknown architecture"
106#endif
107
108const 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 Prichard8f639a42018-10-01 23:10:05 -0700118
119TEST(dl, exec_linker) {
120#if defined(__BIONIC__)
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700121 const char* path_to_linker = PathToLinker();
122 std::string usage_prefix = std::string("Usage: ") + path_to_linker;
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -0800123 ExecTestHelper eth;
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700124 eth.SetArgs({ path_to_linker, nullptr });
125 eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
Ryan Prichard8f639a42018-10-01 23:10:05 -0700126 ASSERT_EQ(0u, eth.GetOutput().find(usage_prefix)) << "Test output:\n" << eth.GetOutput();
127#endif
128}
129
130TEST(dl, exec_linker_load_file) {
131#if defined(__BIONIC__)
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700132 const char* path_to_linker = PathToLinker();
Ryan Prichard8f639a42018-10-01 23:10:05 -0700133 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 Prichard48b11592018-11-22 02:41:36 -0800138 "__progname=" + helper + "\n" +
Ryan Prichard8f639a42018-10-01 23:10:05 -0700139 "helper_func called\n";
140 ExecTestHelper eth;
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700141 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 Ivanov2a6955e2017-02-23 11:53:43 -0800143#endif
144}
145
Ryan Prichard8f639a42018-10-01 23:10:05 -0700146TEST(dl, exec_linker_load_from_zip) {
147#if defined(__BIONIC__)
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700148 const char* path_to_linker = PathToLinker();
Ryan Prichard8f639a42018-10-01 23:10:05 -0700149 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 Prichard48b11592018-11-22 02:41:36 -0800154 "__progname=" + helper + "\n" +
Ryan Prichard8f639a42018-10-01 23:10:05 -0700155 "helper_func called\n";
156 ExecTestHelper eth;
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700157 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 Prichard8f639a42018-10-01 23:10:05 -0700159#endif
160}
161
162TEST(dl, exec_linker_load_self) {
163#if defined(__BIONIC__)
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700164 const char* path_to_linker = PathToLinker();
Ryan Prichard8f639a42018-10-01 23:10:05 -0700165 std::string error_message = "error: linker cannot load itself\n";
166 ExecTestHelper eth;
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700167 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 Prichard8f639a42018-10-01 23:10:05 -0700169#endif
170}
171
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700172TEST(dl, preinit_system_calls) {
173#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800174 SKIP_WITH_HWASAN << "hwasan not initialized in preinit_array, b/124007027";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700175 std::string helper = GetTestlibRoot() +
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700176 "/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 Prichard5a664902018-11-22 02:14:14 -0800184TEST(dl, preinit_getauxval) {
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700185#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800186 SKIP_WITH_HWASAN << "hwasan not initialized in preinit_array, b/124007027";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700187 std::string helper = GetTestlibRoot() +
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700188 "/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 Ferris01db9bd2018-11-07 14:39:43 -0800193#else
194 // Force a failure when not compiled for bionic so the test is considered a pass.
195 ASSERT_TRUE(false);
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700196#endif
197}
198
Jiyong Park02586a22017-05-20 01:01:24 +0900199
200TEST(dl, exec_without_ld_preload) {
201#if defined(__BIONIC__)
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700202 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900203 "/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
211TEST(dl, exec_with_ld_preload) {
212#if defined(__BIONIC__)
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700213 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900214 "/ld_preload_test_helper/ld_preload_test_helper";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700215 std::string env = std::string("LD_PRELOAD=") + GetTestlibRoot() + "/ld_preload_test_helper_lib2.so";
Jiyong Park02586a22017-05-20 01:01:24 +0900216 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.
238TEST(dl, exec_without_ld_config_file) {
239#if defined(__BIONIC__)
Josh Gao16269572019-10-29 13:41:00 -0700240 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 Ferris6d2c0bd2018-08-21 18:13:10 -0700244 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900245 "/ld_config_test_helper/ld_config_test_helper";
246 chmod(helper.c_str(), 0755);
247 ExecTestHelper eth;
248 eth.SetArgs({ helper.c_str(), nullptr });
dimitry04f7a792017-09-29 11:52:17 +0200249 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
Jiyong Park02586a22017-05-20 01:01:24 +0900250#endif
251}
252
253#if defined(__BIONIC__)
dimitry1280cf52018-05-09 14:37:47 +0200254extern "C" void android_get_LD_LIBRARY_PATH(char*, size_t);
Ryan Prichard0044cd12018-04-03 20:03:12 -0700255static void create_ld_config_file(const char* config_file) {
dimitry1280cf52018-05-09 14:37:47 +0200256 char default_search_paths[PATH_MAX];
257 android_get_LD_LIBRARY_PATH(default_search_paths, sizeof(default_search_paths));
258
Ryan Prichard0044cd12018-04-03 20:03:12 -0700259 std::ofstream fout(config_file, std::ios::out);
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700260 fout << "dir.test = " << GetTestlibRoot() << "/ld_config_test_helper/" << std::endl
Jiyong Park02586a22017-05-20 01:01:24 +0900261 << "[test]" << std::endl
262 << "additional.namespaces = ns2" << std::endl
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700263 << "namespace.default.search.paths = " << GetTestlibRoot() << std::endl
Jiyong Park02586a22017-05-20 01:01:24 +0900264 << "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 Ferris6d2c0bd2018-08-21 18:13:10 -0700266 << "namespace.ns2.search.paths = " << default_search_paths << ":" << GetTestlibRoot() << "/ns2" << std::endl;
Jiyong Park02586a22017-05-20 01:01:24 +0900267 fout.close();
268}
269#endif
270
Jiyong Park41704cd2017-09-19 09:48:07 +0900271#if defined(__BIONIC__)
dimitry59d30622017-10-18 13:23:08 +0200272static bool is_debuggable_build() {
273 return android::base::GetBoolProperty("ro.debuggable", false);
Jiyong Park41704cd2017-09-19 09:48:07 +0900274}
275#endif
Jiyong Park02586a22017-05-20 01:01:24 +0900276
277// _lib1.so and _lib2.so are now searchable by having another namespace 'ns2'
278// whose search paths include the 'ns2/' subdir.
279TEST(dl, exec_with_ld_config_file) {
280#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800281 SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config";
dimitry59d30622017-10-18 13:23:08 +0200282 if (!is_debuggable_build()) {
Ryan Prichard55f9a242019-12-10 14:45:20 -0800283 GTEST_SKIP() << "LD_CONFIG_FILE is not supported on user build";
Jiyong Park41704cd2017-09-19 09:48:07 +0900284 }
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700285 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900286 "/ld_config_test_helper/ld_config_test_helper";
Ryan Prichard0044cd12018-04-03 20:03:12 -0700287 TemporaryFile config_file;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800288 create_ld_config_file(config_file.path);
289 std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
Jiyong Park02586a22017-05-20 01:01:24 +0900290 chmod(helper.c_str(), 0755);
291 ExecTestHelper eth;
292 eth.SetArgs({ helper.c_str(), nullptr });
293 eth.SetEnv({ env.c_str(), nullptr });
294 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "12345");
295#endif
296}
297
298// _lib3.so has same symbol as lib2.so but returns 54321. _lib3.so is
299// LD_PRELOADed. This test is to ensure LD_PRELOADed libs are available to
300// additional namespaces other than the default namespace.
301TEST(dl, exec_with_ld_config_file_with_ld_preload) {
302#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800303 SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config";
dimitry59d30622017-10-18 13:23:08 +0200304 if (!is_debuggable_build()) {
Ryan Prichard55f9a242019-12-10 14:45:20 -0800305 GTEST_SKIP() << "LD_CONFIG_FILE is not supported on user build";
Jiyong Park41704cd2017-09-19 09:48:07 +0900306 }
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700307 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900308 "/ld_config_test_helper/ld_config_test_helper";
Ryan Prichard0044cd12018-04-03 20:03:12 -0700309 TemporaryFile config_file;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800310 create_ld_config_file(config_file.path);
311 std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700312 std::string env2 = std::string("LD_PRELOAD=") + GetTestlibRoot() + "/ld_config_test_helper_lib3.so";
Jiyong Park02586a22017-05-20 01:01:24 +0900313 chmod(helper.c_str(), 0755);
314 ExecTestHelper eth;
315 eth.SetArgs({ helper.c_str(), nullptr });
316 eth.SetEnv({ env.c_str(), env2.c_str(), nullptr });
317 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321");
318#endif
319}
320
Jiyong Park02586a22017-05-20 01:01:24 +0900321// ensures that LD_CONFIG_FILE env var does not work for production builds.
322// The test input is the same as exec_with_ld_config_file, but it must fail in
323// this case.
324TEST(dl, disable_ld_config_file) {
325#if defined(__BIONIC__)
326 if (getuid() == 0) {
327 // when executed from the shell (e.g. not as part of CTS), skip the test.
328 // This test is only for CTS.
Ryan Prichard55f9a242019-12-10 14:45:20 -0800329 GTEST_SKIP() << "test is not supported with root uid";
Jiyong Park02586a22017-05-20 01:01:24 +0900330 }
dimitry59d30622017-10-18 13:23:08 +0200331 if (is_debuggable_build()) {
Ryan Prichard55f9a242019-12-10 14:45:20 -0800332 GTEST_SKIP() << "test is not supported on debuggable build";
Jiyong Park4945d8f2017-08-30 11:30:53 +0900333 }
334
Ryan Prichard0f672142019-12-10 14:50:11 -0800335 std::string error_message = std::string("CANNOT LINK EXECUTABLE ") +
336 "\"" + GetTestlibRoot() + "/ld_config_test_helper/ld_config_test_helper\": " +
337 "library \"ld_config_test_helper_lib1.so\" not found: needed by main executable\n";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700338 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900339 "/ld_config_test_helper/ld_config_test_helper";
Ryan Prichard0044cd12018-04-03 20:03:12 -0700340 TemporaryFile config_file;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800341 create_ld_config_file(config_file.path);
342 std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
Jiyong Park02586a22017-05-20 01:01:24 +0900343 chmod(helper.c_str(), 0755);
344 ExecTestHelper eth;
345 eth.SetArgs({ helper.c_str(), nullptr });
346 eth.SetEnv({ env.c_str(), nullptr });
Jiyong Park98283862017-11-28 13:37:03 +0900347 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
Jiyong Park02586a22017-05-20 01:01:24 +0900348#endif
349}