blob: 6adba190600d50ebce462e17015b021363c17e5d [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";
Dmytro Chystiakov595c3812019-10-01 11:28:49 -070098#else
99#error "Unknown architecture"
100#endif
101
102const char* PathToLinker() {
103 // On the systems with emulated architecture linker would be of different
104 // architecture. Try to use alternate paths first.
105 struct stat buffer;
106 if (stat(kAlternatePathToLinker, &buffer) == 0) {
107 return kAlternatePathToLinker;
108 }
109 return kPathToLinker;
110}
111#endif // defined(__BIONIC__)
Ryan Prichard8f639a42018-10-01 23:10:05 -0700112
113TEST(dl, exec_linker) {
114#if defined(__BIONIC__)
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700115 const char* path_to_linker = PathToLinker();
116 std::string usage_prefix = std::string("Usage: ") + path_to_linker;
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -0800117 ExecTestHelper eth;
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700118 eth.SetArgs({ path_to_linker, nullptr });
119 eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
Ryan Prichard8f639a42018-10-01 23:10:05 -0700120 ASSERT_EQ(0u, eth.GetOutput().find(usage_prefix)) << "Test output:\n" << eth.GetOutput();
121#endif
122}
123
124TEST(dl, exec_linker_load_file) {
125#if defined(__BIONIC__)
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700126 const char* path_to_linker = PathToLinker();
Ryan Prichard8f639a42018-10-01 23:10:05 -0700127 std::string helper = GetTestlibRoot() +
128 "/exec_linker_helper/exec_linker_helper";
129 std::string expected_output =
130 "ctor: argc=1 argv[0]=" + helper + "\n" +
131 "main: argc=1 argv[0]=" + helper + "\n" +
Elliott Hughes75064c12020-01-22 20:46:12 -0800132 "__progname=exec_linker_helper\n" +
Ryan Prichard8f639a42018-10-01 23:10:05 -0700133 "helper_func called\n";
134 ExecTestHelper eth;
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700135 eth.SetArgs({ path_to_linker, helper.c_str(), nullptr });
136 eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, expected_output.c_str());
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -0800137#endif
138}
139
Ryan Prichard8f639a42018-10-01 23:10:05 -0700140TEST(dl, exec_linker_load_from_zip) {
141#if defined(__BIONIC__)
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700142 const char* path_to_linker = PathToLinker();
Ryan Prichard8f639a42018-10-01 23:10:05 -0700143 std::string helper = GetTestlibRoot() +
144 "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip!/libdir/exec_linker_helper";
145 std::string expected_output =
146 "ctor: argc=1 argv[0]=" + helper + "\n" +
147 "main: argc=1 argv[0]=" + helper + "\n" +
Elliott Hughes75064c12020-01-22 20:46:12 -0800148 "__progname=exec_linker_helper\n" +
Ryan Prichard8f639a42018-10-01 23:10:05 -0700149 "helper_func called\n";
150 ExecTestHelper eth;
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700151 eth.SetArgs({ path_to_linker, helper.c_str(), nullptr });
152 eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, expected_output.c_str());
Ryan Prichard8f639a42018-10-01 23:10:05 -0700153#endif
154}
155
156TEST(dl, exec_linker_load_self) {
157#if defined(__BIONIC__)
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700158 const char* path_to_linker = PathToLinker();
Ryan Prichard8f639a42018-10-01 23:10:05 -0700159 std::string error_message = "error: linker cannot load itself\n";
160 ExecTestHelper eth;
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700161 eth.SetArgs({ path_to_linker, path_to_linker, nullptr });
162 eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
Ryan Prichard8f639a42018-10-01 23:10:05 -0700163#endif
164}
165
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700166TEST(dl, preinit_system_calls) {
167#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800168 SKIP_WITH_HWASAN << "hwasan not initialized in preinit_array, b/124007027";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700169 std::string helper = GetTestlibRoot() +
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700170 "/preinit_syscall_test_helper/preinit_syscall_test_helper";
171 chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
172 ExecTestHelper eth;
173 eth.SetArgs({ helper.c_str(), nullptr });
174 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
175#endif
176}
177
Ryan Prichard5a664902018-11-22 02:14:14 -0800178TEST(dl, preinit_getauxval) {
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700179#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800180 SKIP_WITH_HWASAN << "hwasan not initialized in preinit_array, b/124007027";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700181 std::string helper = GetTestlibRoot() +
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700182 "/preinit_getauxval_test_helper/preinit_getauxval_test_helper";
183 chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
184 ExecTestHelper eth;
185 eth.SetArgs({ helper.c_str(), nullptr });
186 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
Christopher Ferris01db9bd2018-11-07 14:39:43 -0800187#else
188 // Force a failure when not compiled for bionic so the test is considered a pass.
189 ASSERT_TRUE(false);
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700190#endif
191}
192
Jiyong Park02586a22017-05-20 01:01:24 +0900193
194TEST(dl, exec_without_ld_preload) {
195#if defined(__BIONIC__)
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700196 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900197 "/ld_preload_test_helper/ld_preload_test_helper";
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
205TEST(dl, exec_with_ld_preload) {
206#if defined(__BIONIC__)
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700207 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900208 "/ld_preload_test_helper/ld_preload_test_helper";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700209 std::string env = std::string("LD_PRELOAD=") + GetTestlibRoot() + "/ld_preload_test_helper_lib2.so";
Jiyong Park02586a22017-05-20 01:01:24 +0900210 chmod(helper.c_str(), 0755);
211 ExecTestHelper eth;
212 eth.SetArgs({ helper.c_str(), nullptr });
213 eth.SetEnv({ env.c_str(), nullptr });
214 // ld_preload_test_helper calls get_value_from_lib() and returns the value.
215 // The symbol is defined by two libs: ld_preload_test_helper_lib.so and
216 // ld_preloaded_lib.so. The former is DT_NEEDED and the latter is LD_PRELOADED
217 // via this execution. The main executable is linked to the LD_PRELOADED lib
218 // and the value given from the lib is returned.
219 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321");
220#endif
221}
222
223
224// ld_config_test_helper must fail because it is depending on a lib which is not
225// in the search path
226//
227// Call sequence is...
228// _helper -- (get_value_from_lib()) -->
229// _lib1.so -- (get_value_from_another_lib()) -->
230// _lib2.so (returns 12345)
231// The two libs are in ns2/ subdir.
232TEST(dl, exec_without_ld_config_file) {
233#if defined(__BIONIC__)
Josh Gao16269572019-10-29 13:41:00 -0700234 std::string error_message =
235 "CANNOT LINK EXECUTABLE \"" + GetTestlibRoot() +
236 "/ld_config_test_helper/ld_config_test_helper\": library \"ld_config_test_helper_lib1.so\" "
237 "not found: needed by main executable\n";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700238 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900239 "/ld_config_test_helper/ld_config_test_helper";
240 chmod(helper.c_str(), 0755);
241 ExecTestHelper eth;
242 eth.SetArgs({ helper.c_str(), nullptr });
dimitry04f7a792017-09-29 11:52:17 +0200243 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
Jiyong Park02586a22017-05-20 01:01:24 +0900244#endif
245}
246
247#if defined(__BIONIC__)
dimitry1280cf52018-05-09 14:37:47 +0200248extern "C" void android_get_LD_LIBRARY_PATH(char*, size_t);
Ryan Prichard0044cd12018-04-03 20:03:12 -0700249static void create_ld_config_file(const char* config_file) {
dimitry1280cf52018-05-09 14:37:47 +0200250 char default_search_paths[PATH_MAX];
251 android_get_LD_LIBRARY_PATH(default_search_paths, sizeof(default_search_paths));
252
Ryan Prichard0044cd12018-04-03 20:03:12 -0700253 std::ofstream fout(config_file, std::ios::out);
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700254 fout << "dir.test = " << GetTestlibRoot() << "/ld_config_test_helper/" << std::endl
Jiyong Park02586a22017-05-20 01:01:24 +0900255 << "[test]" << std::endl
256 << "additional.namespaces = ns2" << std::endl
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700257 << "namespace.default.search.paths = " << GetTestlibRoot() << std::endl
Jiyong Park02586a22017-05-20 01:01:24 +0900258 << "namespace.default.links = ns2" << std::endl
259 << "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 -0700260 << "namespace.ns2.search.paths = " << default_search_paths << ":" << GetTestlibRoot() << "/ns2" << std::endl;
Jiyong Park02586a22017-05-20 01:01:24 +0900261 fout.close();
262}
263#endif
264
Jiyong Park41704cd2017-09-19 09:48:07 +0900265#if defined(__BIONIC__)
dimitry59d30622017-10-18 13:23:08 +0200266static bool is_debuggable_build() {
267 return android::base::GetBoolProperty("ro.debuggable", false);
Jiyong Park41704cd2017-09-19 09:48:07 +0900268}
269#endif
Jiyong Park02586a22017-05-20 01:01:24 +0900270
271// _lib1.so and _lib2.so are now searchable by having another namespace 'ns2'
272// whose search paths include the 'ns2/' subdir.
273TEST(dl, exec_with_ld_config_file) {
274#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800275 SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config";
dimitry59d30622017-10-18 13:23:08 +0200276 if (!is_debuggable_build()) {
Ryan Prichard55f9a242019-12-10 14:45:20 -0800277 GTEST_SKIP() << "LD_CONFIG_FILE is not supported on user build";
Jiyong Park41704cd2017-09-19 09:48:07 +0900278 }
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700279 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900280 "/ld_config_test_helper/ld_config_test_helper";
Ryan Prichard0044cd12018-04-03 20:03:12 -0700281 TemporaryFile config_file;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800282 create_ld_config_file(config_file.path);
283 std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
Jiyong Park02586a22017-05-20 01:01:24 +0900284 chmod(helper.c_str(), 0755);
285 ExecTestHelper eth;
286 eth.SetArgs({ helper.c_str(), nullptr });
287 eth.SetEnv({ env.c_str(), nullptr });
288 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "12345");
289#endif
290}
291
292// _lib3.so has same symbol as lib2.so but returns 54321. _lib3.so is
293// LD_PRELOADed. This test is to ensure LD_PRELOADed libs are available to
294// additional namespaces other than the default namespace.
295TEST(dl, exec_with_ld_config_file_with_ld_preload) {
296#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800297 SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config";
dimitry59d30622017-10-18 13:23:08 +0200298 if (!is_debuggable_build()) {
Ryan Prichard55f9a242019-12-10 14:45:20 -0800299 GTEST_SKIP() << "LD_CONFIG_FILE is not supported on user build";
Jiyong Park41704cd2017-09-19 09:48:07 +0900300 }
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700301 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900302 "/ld_config_test_helper/ld_config_test_helper";
Ryan Prichard0044cd12018-04-03 20:03:12 -0700303 TemporaryFile config_file;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800304 create_ld_config_file(config_file.path);
305 std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700306 std::string env2 = std::string("LD_PRELOAD=") + GetTestlibRoot() + "/ld_config_test_helper_lib3.so";
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(), env2.c_str(), nullptr });
311 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321");
312#endif
313}
314
Jiyong Park02586a22017-05-20 01:01:24 +0900315// ensures that LD_CONFIG_FILE env var does not work for production builds.
316// The test input is the same as exec_with_ld_config_file, but it must fail in
317// this case.
318TEST(dl, disable_ld_config_file) {
319#if defined(__BIONIC__)
320 if (getuid() == 0) {
321 // when executed from the shell (e.g. not as part of CTS), skip the test.
322 // This test is only for CTS.
Ryan Prichard55f9a242019-12-10 14:45:20 -0800323 GTEST_SKIP() << "test is not supported with root uid";
Jiyong Park02586a22017-05-20 01:01:24 +0900324 }
dimitry59d30622017-10-18 13:23:08 +0200325 if (is_debuggable_build()) {
Ryan Prichard55f9a242019-12-10 14:45:20 -0800326 GTEST_SKIP() << "test is not supported on debuggable build";
Jiyong Park4945d8f2017-08-30 11:30:53 +0900327 }
328
Ryan Prichard0f672142019-12-10 14:50:11 -0800329 std::string error_message = std::string("CANNOT LINK EXECUTABLE ") +
330 "\"" + GetTestlibRoot() + "/ld_config_test_helper/ld_config_test_helper\": " +
331 "library \"ld_config_test_helper_lib1.so\" not found: needed by main executable\n";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700332 std::string helper = GetTestlibRoot() +
Jiyong Park02586a22017-05-20 01:01:24 +0900333 "/ld_config_test_helper/ld_config_test_helper";
Ryan Prichard0044cd12018-04-03 20:03:12 -0700334 TemporaryFile config_file;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800335 create_ld_config_file(config_file.path);
336 std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
Jiyong Park02586a22017-05-20 01:01:24 +0900337 chmod(helper.c_str(), 0755);
338 ExecTestHelper eth;
339 eth.SetArgs({ helper.c_str(), nullptr });
340 eth.SetEnv({ env.c_str(), nullptr });
Jiyong Park98283862017-11-28 13:37:03 +0900341 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
Jiyong Park02586a22017-05-20 01:01:24 +0900342#endif
343}
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800344
345static void RelocationsTest(const char* lib, const char* expectation) {
346#if defined(__BIONIC__)
347 // Does readelf think the .so file looks right?
348 const std::string path = GetTestlibRoot() + "/" + lib;
349 ExecTestHelper eth;
350 eth.SetArgs({ "readelf", "-SW", path.c_str(), nullptr });
351 eth.Run([&]() { execvpe("readelf", eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
352 ASSERT_TRUE(eth.GetOutput().find(expectation) != std::string::npos) << eth.GetOutput();
353
354 // Can we load it?
355 void* handle = dlopen(lib, RTLD_NOW);
356 ASSERT_TRUE(handle != nullptr) << dlerror();
357#else
358 UNUSED(lib);
359 UNUSED(expectation);
360 GTEST_SKIP() << "test is not supported on glibc";
361#endif
362}
363
364TEST(dl, relocations_RELR) {
365 RelocationsTest("librelocations-RELR.so",
366 ".relr.dyn RELR");
367}
368
369TEST(dl, relocations_ANDROID_RELR) {
370 RelocationsTest("librelocations-ANDROID_RELR.so",
371 ".relr.dyn ANDROID_RELR");
372}
373
374TEST(dl, relocations_ANDROID_REL) {
375 RelocationsTest("librelocations-ANDROID_REL.so",
376#if __LP64__
377 ".rela.dyn ANDROID_RELA"
378#else
379 ".rel.dyn ANDROID_REL"
380#endif
381 );
382}
383
384TEST(dl, relocations_fat) {
385 RelocationsTest("librelocations-fat.so",
386#if __LP64__
387 ".rela.dyn RELA"
388#else
389 ".rel.dyn REL"
390#endif
391 );
392}