blob: aea92b442e6f3689b35cf81f469ce599b3d4f06c [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>
28
29#include <string>
Jiyong Park02586a22017-05-20 01:01:24 +090030#include <iostream>
31#include <fstream>
Dmitriy Ivanov0416d882014-11-04 09:38:18 -080032
Elliott Hugheseb04ed52017-03-29 13:48:02 -070033#include "gtest_globals.h"
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080034#include <android-base/file.h>
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -080035#include "utils.h"
36
Dmitriy Ivanov0416d882014-11-04 09:38:18 -080037extern "C" int main_global_default_serial() {
38 return 3370318;
39}
40
41extern "C" int main_global_protected_serial() {
42 return 2716057;
43}
44
45// The following functions are defined in DT_NEEDED
46// libdl_preempt_test.so library.
47
48// This one calls main_global_default_serial
49extern "C" int main_global_default_get_serial();
50
51// This one calls main_global_protected_serial
52extern "C" int main_global_protected_get_serial();
53
54// This one calls lib_global_default_serial
55extern "C" int lib_global_default_get_serial();
56
57// This one calls lib_global_protected_serial
58extern "C" int lib_global_protected_get_serial();
59
60// This test verifies that the global default function
61// main_global_default_serial() is preempted by
62// the function defined above.
63TEST(dl, main_preempts_global_default) {
64 ASSERT_EQ(3370318, main_global_default_get_serial());
65}
66
67// This one makes sure that the global protected
68// symbols do not get preempted
69TEST(dl, main_does_not_preempt_global_protected) {
70 ASSERT_EQ(3370318, main_global_protected_get_serial());
71}
72
73// check same things for lib
74TEST(dl, lib_preempts_global_default) {
75 ASSERT_EQ(3370318, lib_global_default_get_serial());
76}
77
78TEST(dl, lib_does_not_preempt_global_protected) {
79 ASSERT_EQ(3370318, lib_global_protected_get_serial());
80}
81
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -080082#if defined(__BIONIC__)
83#if defined(__LP64__)
84 static constexpr const char* kPathToLinker = "/system/bin/linker64";
85#else
86 static constexpr const char* kPathToLinker = "/system/bin/linker";
87#endif
Ryan Prichard8f639a42018-10-01 23:10:05 -070088#endif
89
90TEST(dl, exec_linker) {
91#if defined(__BIONIC__)
92 std::string usage_prefix = std::string("Usage: ") + kPathToLinker;
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -080093 ExecTestHelper eth;
Ryan Prichard8f639a42018-10-01 23:10:05 -070094 eth.SetArgs({ kPathToLinker, nullptr });
95 eth.Run([&]() { execve(kPathToLinker, eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
96 ASSERT_EQ(0u, eth.GetOutput().find(usage_prefix)) << "Test output:\n" << eth.GetOutput();
97#endif
98}
99
100TEST(dl, exec_linker_load_file) {
101#if defined(__BIONIC__)
102 std::string helper = GetTestlibRoot() +
103 "/exec_linker_helper/exec_linker_helper";
104 std::string expected_output =
105 "ctor: argc=1 argv[0]=" + helper + "\n" +
106 "main: argc=1 argv[0]=" + helper + "\n" +
Ryan Prichard48b11592018-11-22 02:41:36 -0800107 "__progname=" + helper + "\n" +
Ryan Prichard8f639a42018-10-01 23:10:05 -0700108 "helper_func called\n";
109 ExecTestHelper eth;
110 eth.SetArgs({ kPathToLinker, helper.c_str(), nullptr });
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -0800111 eth.Run([&]() { execve(kPathToLinker, eth.GetArgs(), eth.GetEnv()); }, 0, expected_output.c_str());
112#endif
113}
114
Ryan Prichard8f639a42018-10-01 23:10:05 -0700115TEST(dl, exec_linker_load_from_zip) {
116#if defined(__BIONIC__)
117 std::string helper = GetTestlibRoot() +
118 "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip!/libdir/exec_linker_helper";
119 std::string expected_output =
120 "ctor: argc=1 argv[0]=" + helper + "\n" +
121 "main: argc=1 argv[0]=" + helper + "\n" +
Ryan Prichard48b11592018-11-22 02:41:36 -0800122 "__progname=" + helper + "\n" +
Ryan Prichard8f639a42018-10-01 23:10:05 -0700123 "helper_func called\n";
124 ExecTestHelper eth;
125 eth.SetArgs({ kPathToLinker, helper.c_str(), nullptr });
126 eth.Run([&]() { execve(kPathToLinker, eth.GetArgs(), eth.GetEnv()); }, 0, expected_output.c_str());
127#endif
128}
129
130TEST(dl, exec_linker_load_self) {
131#if defined(__BIONIC__)
132 std::string error_message = "error: linker cannot load itself\n";
133 ExecTestHelper eth;
134 eth.SetArgs({ kPathToLinker, kPathToLinker, nullptr });
135 eth.Run([&]() { execve(kPathToLinker, eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
136#endif
137}
138
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700139TEST(dl, preinit_system_calls) {
140#if defined(__BIONIC__)
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800141 SKIP_WITH_HWASAN; // hwasan not initialized in preinit_array
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700142 std::string helper = GetTestlibRoot() +
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700143 "/preinit_syscall_test_helper/preinit_syscall_test_helper";
144 chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
145 ExecTestHelper eth;
146 eth.SetArgs({ helper.c_str(), nullptr });
147 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
148#endif
149}
150
Ryan Prichard5a664902018-11-22 02:14:14 -0800151TEST(dl, preinit_getauxval) {
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700152#if defined(__BIONIC__)
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700153 std::string helper = GetTestlibRoot() +
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700154 "/preinit_getauxval_test_helper/preinit_getauxval_test_helper";
155 chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
156 ExecTestHelper eth;
157 eth.SetArgs({ helper.c_str(), nullptr });
158 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
Christopher Ferris01db9bd2018-11-07 14:39:43 -0800159#else
160 // Force a failure when not compiled for bionic so the test is considered a pass.
161 ASSERT_TRUE(false);
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700162#endif
163}
164
Jiyong Park02586a22017-05-20 01:01:24 +0900165
166TEST(dl, exec_without_ld_preload) {
167#if defined(__BIONIC__)
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700168 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900169 "/ld_preload_test_helper/ld_preload_test_helper";
170 chmod(helper.c_str(), 0755);
171 ExecTestHelper eth;
172 eth.SetArgs({ helper.c_str(), nullptr });
173 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "12345");
174#endif
175}
176
177TEST(dl, exec_with_ld_preload) {
178#if defined(__BIONIC__)
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700179 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900180 "/ld_preload_test_helper/ld_preload_test_helper";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700181 std::string env = std::string("LD_PRELOAD=") + GetTestlibRoot() + "/ld_preload_test_helper_lib2.so";
Jiyong Park02586a22017-05-20 01:01:24 +0900182 chmod(helper.c_str(), 0755);
183 ExecTestHelper eth;
184 eth.SetArgs({ helper.c_str(), nullptr });
185 eth.SetEnv({ env.c_str(), nullptr });
186 // ld_preload_test_helper calls get_value_from_lib() and returns the value.
187 // The symbol is defined by two libs: ld_preload_test_helper_lib.so and
188 // ld_preloaded_lib.so. The former is DT_NEEDED and the latter is LD_PRELOADED
189 // via this execution. The main executable is linked to the LD_PRELOADED lib
190 // and the value given from the lib is returned.
191 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321");
192#endif
193}
194
195
196// ld_config_test_helper must fail because it is depending on a lib which is not
197// in the search path
198//
199// Call sequence is...
200// _helper -- (get_value_from_lib()) -->
201// _lib1.so -- (get_value_from_another_lib()) -->
202// _lib2.so (returns 12345)
203// The two libs are in ns2/ subdir.
204TEST(dl, exec_without_ld_config_file) {
205#if defined(__BIONIC__)
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700206 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";
207 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900208 "/ld_config_test_helper/ld_config_test_helper";
209 chmod(helper.c_str(), 0755);
210 ExecTestHelper eth;
211 eth.SetArgs({ helper.c_str(), nullptr });
dimitry04f7a792017-09-29 11:52:17 +0200212 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
Jiyong Park02586a22017-05-20 01:01:24 +0900213#endif
214}
215
216#if defined(__BIONIC__)
dimitry1280cf52018-05-09 14:37:47 +0200217extern "C" void android_get_LD_LIBRARY_PATH(char*, size_t);
Ryan Prichard0044cd12018-04-03 20:03:12 -0700218static void create_ld_config_file(const char* config_file) {
dimitry1280cf52018-05-09 14:37:47 +0200219 char default_search_paths[PATH_MAX];
220 android_get_LD_LIBRARY_PATH(default_search_paths, sizeof(default_search_paths));
221
Ryan Prichard0044cd12018-04-03 20:03:12 -0700222 std::ofstream fout(config_file, std::ios::out);
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700223 fout << "dir.test = " << GetTestlibRoot() << "/ld_config_test_helper/" << std::endl
Jiyong Park02586a22017-05-20 01:01:24 +0900224 << "[test]" << std::endl
225 << "additional.namespaces = ns2" << std::endl
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700226 << "namespace.default.search.paths = " << GetTestlibRoot() << std::endl
Jiyong Park02586a22017-05-20 01:01:24 +0900227 << "namespace.default.links = ns2" << std::endl
228 << "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 -0700229 << "namespace.ns2.search.paths = " << default_search_paths << ":" << GetTestlibRoot() << "/ns2" << std::endl;
Jiyong Park02586a22017-05-20 01:01:24 +0900230 fout.close();
231}
232#endif
233
Jiyong Park41704cd2017-09-19 09:48:07 +0900234#if defined(__BIONIC__)
dimitry59d30622017-10-18 13:23:08 +0200235static bool is_debuggable_build() {
236 return android::base::GetBoolProperty("ro.debuggable", false);
Jiyong Park41704cd2017-09-19 09:48:07 +0900237}
238#endif
Jiyong Park02586a22017-05-20 01:01:24 +0900239
240// _lib1.so and _lib2.so are now searchable by having another namespace 'ns2'
241// whose search paths include the 'ns2/' subdir.
242TEST(dl, exec_with_ld_config_file) {
243#if defined(__BIONIC__)
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800244 SKIP_WITH_HWASAN; // libclang_rt.hwasan is not found with custom ld config
dimitry59d30622017-10-18 13:23:08 +0200245 if (!is_debuggable_build()) {
Jiyong Park41704cd2017-09-19 09:48:07 +0900246 // LD_CONFIG_FILE is not supported on user build
247 return;
248 }
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700249 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900250 "/ld_config_test_helper/ld_config_test_helper";
Ryan Prichard0044cd12018-04-03 20:03:12 -0700251 TemporaryFile config_file;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800252 create_ld_config_file(config_file.path);
253 std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
Jiyong Park02586a22017-05-20 01:01:24 +0900254 chmod(helper.c_str(), 0755);
255 ExecTestHelper eth;
256 eth.SetArgs({ helper.c_str(), nullptr });
257 eth.SetEnv({ env.c_str(), nullptr });
258 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "12345");
259#endif
260}
261
262// _lib3.so has same symbol as lib2.so but returns 54321. _lib3.so is
263// LD_PRELOADed. This test is to ensure LD_PRELOADed libs are available to
264// additional namespaces other than the default namespace.
265TEST(dl, exec_with_ld_config_file_with_ld_preload) {
266#if defined(__BIONIC__)
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800267 SKIP_WITH_HWASAN; // libclang_rt.hwasan is not found with custom ld config
dimitry59d30622017-10-18 13:23:08 +0200268 if (!is_debuggable_build()) {
Jiyong Park41704cd2017-09-19 09:48:07 +0900269 // LD_CONFIG_FILE is not supported on user build
270 return;
271 }
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700272 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900273 "/ld_config_test_helper/ld_config_test_helper";
Ryan Prichard0044cd12018-04-03 20:03:12 -0700274 TemporaryFile config_file;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800275 create_ld_config_file(config_file.path);
276 std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700277 std::string env2 = std::string("LD_PRELOAD=") + GetTestlibRoot() + "/ld_config_test_helper_lib3.so";
Jiyong Park02586a22017-05-20 01:01:24 +0900278 chmod(helper.c_str(), 0755);
279 ExecTestHelper eth;
280 eth.SetArgs({ helper.c_str(), nullptr });
281 eth.SetEnv({ env.c_str(), env2.c_str(), nullptr });
282 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321");
283#endif
284}
285
Jiyong Park02586a22017-05-20 01:01:24 +0900286// ensures that LD_CONFIG_FILE env var does not work for production builds.
287// The test input is the same as exec_with_ld_config_file, but it must fail in
288// this case.
289TEST(dl, disable_ld_config_file) {
290#if defined(__BIONIC__)
291 if (getuid() == 0) {
292 // when executed from the shell (e.g. not as part of CTS), skip the test.
293 // This test is only for CTS.
294 return;
295 }
dimitry59d30622017-10-18 13:23:08 +0200296 if (is_debuggable_build()) {
Jiyong Park4945d8f2017-08-30 11:30:53 +0900297 // Skip the test for non production devices
298 return;
299 }
300
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700301 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";
302 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900303 "/ld_config_test_helper/ld_config_test_helper";
Ryan Prichard0044cd12018-04-03 20:03:12 -0700304 TemporaryFile config_file;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800305 create_ld_config_file(config_file.path);
306 std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
Jiyong Park02586a22017-05-20 01:01:24 +0900307 chmod(helper.c_str(), 0755);
308 ExecTestHelper eth;
309 eth.SetArgs({ helper.c_str(), nullptr });
310 eth.SetEnv({ env.c_str(), nullptr });
Jiyong Park98283862017-11-28 13:37:03 +0900311 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
Jiyong Park02586a22017-05-20 01:01:24 +0900312#endif
313}