2014-11-04 18:38:18 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2017-08-30 04:30:53 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
#include <android-base/properties.h>
|
|
|
|
#endif
|
|
|
|
|
2014-11-04 18:38:18 +01:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
2019-10-01 20:28:49 +02:00
|
|
|
#include <sys/stat.h>
|
2014-11-04 18:38:18 +01:00
|
|
|
|
2017-05-19 18:01:24 +02:00
|
|
|
#include <fstream>
|
2021-04-13 00:55:29 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <regex>
|
|
|
|
#include <string>
|
2014-11-04 18:38:18 +01:00
|
|
|
|
2018-11-13 16:35:21 +01:00
|
|
|
#include <android-base/file.h>
|
2022-10-19 18:16:54 +02:00
|
|
|
#include <android-base/macros.h>
|
2022-04-16 00:54:47 +02:00
|
|
|
#include <android-base/test_utils.h>
|
2022-10-19 18:16:54 +02:00
|
|
|
#include "gtest_globals.h"
|
2017-02-23 20:53:43 +01:00
|
|
|
#include "utils.h"
|
|
|
|
|
2014-11-04 18:38:18 +01:00
|
|
|
extern "C" int main_global_default_serial() {
|
|
|
|
return 3370318;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int main_global_protected_serial() {
|
|
|
|
return 2716057;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following functions are defined in DT_NEEDED
|
|
|
|
// libdl_preempt_test.so library.
|
|
|
|
|
|
|
|
// This one calls main_global_default_serial
|
|
|
|
extern "C" int main_global_default_get_serial();
|
|
|
|
|
|
|
|
// This one calls main_global_protected_serial
|
|
|
|
extern "C" int main_global_protected_get_serial();
|
|
|
|
|
|
|
|
// This one calls lib_global_default_serial
|
|
|
|
extern "C" int lib_global_default_get_serial();
|
|
|
|
|
|
|
|
// This one calls lib_global_protected_serial
|
|
|
|
extern "C" int lib_global_protected_get_serial();
|
|
|
|
|
|
|
|
// This test verifies that the global default function
|
|
|
|
// main_global_default_serial() is preempted by
|
|
|
|
// the function defined above.
|
|
|
|
TEST(dl, main_preempts_global_default) {
|
|
|
|
ASSERT_EQ(3370318, main_global_default_get_serial());
|
|
|
|
}
|
|
|
|
|
|
|
|
// This one makes sure that the global protected
|
|
|
|
// symbols do not get preempted
|
|
|
|
TEST(dl, main_does_not_preempt_global_protected) {
|
|
|
|
ASSERT_EQ(3370318, main_global_protected_get_serial());
|
|
|
|
}
|
|
|
|
|
|
|
|
// check same things for lib
|
|
|
|
TEST(dl, lib_preempts_global_default) {
|
|
|
|
ASSERT_EQ(3370318, lib_global_default_get_serial());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(dl, lib_does_not_preempt_global_protected) {
|
|
|
|
ASSERT_EQ(3370318, lib_global_protected_get_serial());
|
|
|
|
}
|
|
|
|
|
2017-02-23 20:53:43 +01:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
#if defined(__LP64__)
|
2022-10-19 18:16:54 +02:00
|
|
|
#define LINKER_NAME "linker64"
|
2019-10-01 20:28:49 +02:00
|
|
|
#else
|
2022-10-19 18:16:54 +02:00
|
|
|
#define LINKER_NAME "linker"
|
2019-10-01 20:28:49 +02:00
|
|
|
#endif
|
2022-10-19 18:16:54 +02:00
|
|
|
static constexpr const char* kPathToLinker = "/system/bin/" LINKER_NAME;
|
|
|
|
static constexpr const char* kAlternatePathToLinker = "/system/bin/" ABI_STRING "/" LINKER_NAME;
|
|
|
|
#undef LINKER_NAME
|
2019-10-01 20:28:49 +02:00
|
|
|
|
|
|
|
const char* PathToLinker() {
|
|
|
|
// On the systems with emulated architecture linker would be of different
|
|
|
|
// architecture. Try to use alternate paths first.
|
|
|
|
struct stat buffer;
|
|
|
|
if (stat(kAlternatePathToLinker, &buffer) == 0) {
|
|
|
|
return kAlternatePathToLinker;
|
|
|
|
}
|
|
|
|
return kPathToLinker;
|
|
|
|
}
|
|
|
|
#endif // defined(__BIONIC__)
|
2018-10-02 08:10:05 +02:00
|
|
|
|
|
|
|
TEST(dl, exec_linker) {
|
|
|
|
#if defined(__BIONIC__)
|
2019-10-01 20:28:49 +02:00
|
|
|
const char* path_to_linker = PathToLinker();
|
|
|
|
std::string usage_prefix = std::string("Usage: ") + path_to_linker;
|
2018-10-02 08:10:05 +02:00
|
|
|
ExecTestHelper eth;
|
2019-10-01 20:28:49 +02:00
|
|
|
eth.SetArgs({ path_to_linker, nullptr });
|
|
|
|
eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
|
2018-10-02 08:10:05 +02:00
|
|
|
ASSERT_EQ(0u, eth.GetOutput().find(usage_prefix)) << "Test output:\n" << eth.GetOutput();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(dl, exec_linker_load_file) {
|
|
|
|
#if defined(__BIONIC__)
|
2019-10-01 20:28:49 +02:00
|
|
|
const char* path_to_linker = PathToLinker();
|
2021-09-25 02:49:58 +02:00
|
|
|
std::string helper = GetTestlibRoot() + "/exec_linker_helper";
|
2018-10-02 08:10:05 +02:00
|
|
|
std::string expected_output =
|
|
|
|
"ctor: argc=1 argv[0]=" + helper + "\n" +
|
|
|
|
"main: argc=1 argv[0]=" + helper + "\n" +
|
2020-01-23 05:46:12 +01:00
|
|
|
"__progname=exec_linker_helper\n" +
|
2018-10-02 08:10:05 +02:00
|
|
|
"helper_func called\n";
|
2017-02-23 20:53:43 +01:00
|
|
|
ExecTestHelper eth;
|
2019-10-01 20:28:49 +02:00
|
|
|
eth.SetArgs({ path_to_linker, helper.c_str(), nullptr });
|
2021-10-01 19:12:15 +02:00
|
|
|
eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
|
|
|
|
ASSERT_EQ(expected_output, eth.GetOutput());
|
2017-02-23 20:53:43 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-10-02 08:10:05 +02:00
|
|
|
TEST(dl, exec_linker_load_from_zip) {
|
|
|
|
#if defined(__BIONIC__)
|
2019-10-01 20:28:49 +02:00
|
|
|
const char* path_to_linker = PathToLinker();
|
2018-10-02 08:10:05 +02:00
|
|
|
std::string helper = GetTestlibRoot() +
|
|
|
|
"/libdlext_test_zip/libdlext_test_zip_zipaligned.zip!/libdir/exec_linker_helper";
|
|
|
|
std::string expected_output =
|
|
|
|
"ctor: argc=1 argv[0]=" + helper + "\n" +
|
|
|
|
"main: argc=1 argv[0]=" + helper + "\n" +
|
2020-01-23 05:46:12 +01:00
|
|
|
"__progname=exec_linker_helper\n" +
|
2018-10-02 08:10:05 +02:00
|
|
|
"helper_func called\n";
|
|
|
|
ExecTestHelper eth;
|
2019-10-01 20:28:49 +02:00
|
|
|
eth.SetArgs({ path_to_linker, helper.c_str(), nullptr });
|
2021-10-01 19:12:15 +02:00
|
|
|
eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
|
|
|
|
ASSERT_EQ(expected_output, eth.GetOutput());
|
2018-10-02 08:10:05 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(dl, exec_linker_load_self) {
|
|
|
|
#if defined(__BIONIC__)
|
2019-10-01 20:28:49 +02:00
|
|
|
const char* path_to_linker = PathToLinker();
|
2018-10-02 08:10:05 +02:00
|
|
|
std::string error_message = "error: linker cannot load itself\n";
|
|
|
|
ExecTestHelper eth;
|
2019-10-01 20:28:49 +02:00
|
|
|
eth.SetArgs({ path_to_linker, path_to_linker, nullptr });
|
|
|
|
eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
|
2018-10-02 08:10:05 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-03-29 22:48:02 +02:00
|
|
|
TEST(dl, preinit_system_calls) {
|
|
|
|
#if defined(__BIONIC__)
|
2019-03-09 00:20:23 +01:00
|
|
|
SKIP_WITH_HWASAN << "hwasan not initialized in preinit_array, b/124007027";
|
2021-09-25 02:49:58 +02:00
|
|
|
std::string helper = GetTestlibRoot() + "/preinit_syscall_test_helper";
|
2017-03-29 22:48:02 +02:00
|
|
|
chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
|
|
|
|
ExecTestHelper eth;
|
|
|
|
eth.SetArgs({ helper.c_str(), nullptr });
|
|
|
|
eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-11-22 11:14:14 +01:00
|
|
|
TEST(dl, preinit_getauxval) {
|
2017-03-29 22:48:02 +02:00
|
|
|
#if defined(__BIONIC__)
|
2019-03-09 00:20:23 +01:00
|
|
|
SKIP_WITH_HWASAN << "hwasan not initialized in preinit_array, b/124007027";
|
2021-09-25 02:49:58 +02:00
|
|
|
std::string helper = GetTestlibRoot() + "/preinit_getauxval_test_helper";
|
2017-03-29 22:48:02 +02:00
|
|
|
chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
|
|
|
|
ExecTestHelper eth;
|
|
|
|
eth.SetArgs({ helper.c_str(), nullptr });
|
|
|
|
eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
|
2018-11-07 23:39:43 +01:00
|
|
|
#else
|
|
|
|
// Force a failure when not compiled for bionic so the test is considered a pass.
|
|
|
|
ASSERT_TRUE(false);
|
2017-03-29 22:48:02 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-05-19 18:01:24 +02:00
|
|
|
|
|
|
|
TEST(dl, exec_without_ld_preload) {
|
|
|
|
#if defined(__BIONIC__)
|
2021-09-25 02:49:58 +02:00
|
|
|
std::string helper = GetTestlibRoot() + "/ld_preload_test_helper";
|
2017-05-19 18:01:24 +02:00
|
|
|
chmod(helper.c_str(), 0755);
|
|
|
|
ExecTestHelper eth;
|
|
|
|
eth.SetArgs({ helper.c_str(), nullptr });
|
|
|
|
eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "12345");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(dl, exec_with_ld_preload) {
|
|
|
|
#if defined(__BIONIC__)
|
2021-09-25 02:49:58 +02:00
|
|
|
std::string helper = GetTestlibRoot() + "/ld_preload_test_helper";
|
2018-08-22 03:13:10 +02:00
|
|
|
std::string env = std::string("LD_PRELOAD=") + GetTestlibRoot() + "/ld_preload_test_helper_lib2.so";
|
2017-05-19 18:01:24 +02:00
|
|
|
chmod(helper.c_str(), 0755);
|
|
|
|
ExecTestHelper eth;
|
|
|
|
eth.SetArgs({ helper.c_str(), nullptr });
|
|
|
|
eth.SetEnv({ env.c_str(), nullptr });
|
|
|
|
// ld_preload_test_helper calls get_value_from_lib() and returns the value.
|
|
|
|
// The symbol is defined by two libs: ld_preload_test_helper_lib.so and
|
|
|
|
// ld_preloaded_lib.so. The former is DT_NEEDED and the latter is LD_PRELOADED
|
|
|
|
// via this execution. The main executable is linked to the LD_PRELOADED lib
|
|
|
|
// and the value given from the lib is returned.
|
|
|
|
eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ld_config_test_helper must fail because it is depending on a lib which is not
|
|
|
|
// in the search path
|
|
|
|
//
|
|
|
|
// Call sequence is...
|
|
|
|
// _helper -- (get_value_from_lib()) -->
|
|
|
|
// _lib1.so -- (get_value_from_another_lib()) -->
|
|
|
|
// _lib2.so (returns 12345)
|
|
|
|
// The two libs are in ns2/ subdir.
|
|
|
|
TEST(dl, exec_without_ld_config_file) {
|
|
|
|
#if defined(__BIONIC__)
|
2021-09-25 02:49:58 +02:00
|
|
|
std::string error_message = "CANNOT LINK EXECUTABLE \"" + GetTestlibRoot() +
|
|
|
|
"/ld_config_test_helper\": library \"ld_config_test_helper_lib1.so\" "
|
|
|
|
"not found: needed by main executable\n";
|
|
|
|
std::string helper = GetTestlibRoot() + "/ld_config_test_helper";
|
2017-05-19 18:01:24 +02:00
|
|
|
chmod(helper.c_str(), 0755);
|
|
|
|
ExecTestHelper eth;
|
|
|
|
eth.SetArgs({ helper.c_str(), nullptr });
|
2017-09-29 11:52:17 +02:00
|
|
|
eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
|
2017-05-19 18:01:24 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__BIONIC__)
|
2018-05-09 14:37:47 +02:00
|
|
|
extern "C" void android_get_LD_LIBRARY_PATH(char*, size_t);
|
2018-04-04 05:03:12 +02:00
|
|
|
static void create_ld_config_file(const char* config_file) {
|
2018-05-09 14:37:47 +02:00
|
|
|
char default_search_paths[PATH_MAX];
|
|
|
|
android_get_LD_LIBRARY_PATH(default_search_paths, sizeof(default_search_paths));
|
|
|
|
|
2018-04-04 05:03:12 +02:00
|
|
|
std::ofstream fout(config_file, std::ios::out);
|
2021-09-25 02:49:58 +02:00
|
|
|
fout << "dir.test = " << GetTestlibRoot() << "/" << std::endl
|
2017-05-19 18:01:24 +02:00
|
|
|
<< "[test]" << std::endl
|
|
|
|
<< "additional.namespaces = ns2" << std::endl
|
2018-08-22 03:13:10 +02:00
|
|
|
<< "namespace.default.search.paths = " << GetTestlibRoot() << std::endl
|
2017-05-19 18:01:24 +02:00
|
|
|
<< "namespace.default.links = ns2" << std::endl
|
2021-09-25 02:49:58 +02:00
|
|
|
<< "namespace.default.link.ns2.shared_libs = "
|
|
|
|
"libc.so:libm.so:libdl.so:ld_config_test_helper_lib1.so"
|
|
|
|
<< std::endl
|
|
|
|
<< "namespace.ns2.search.paths = " << default_search_paths << ":" << GetTestlibRoot()
|
|
|
|
<< "/ns2" << std::endl;
|
2017-05-19 18:01:24 +02:00
|
|
|
fout.close();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-09-19 02:48:07 +02:00
|
|
|
#if defined(__BIONIC__)
|
2021-06-05 02:27:39 +02:00
|
|
|
// This test can't rely on ro.debuggable, because it might have been forced on
|
|
|
|
// in a user build ("Force Debuggable"). In that configuration, ro.debuggable is
|
|
|
|
// true, but Bionic's LD_CONFIG_FILE testing support is still disabled.
|
|
|
|
static bool is_user_build() {
|
|
|
|
return android::base::GetProperty("ro.build.type", "user") == std::string("user");
|
2017-09-19 02:48:07 +02:00
|
|
|
}
|
|
|
|
#endif
|
2017-05-19 18:01:24 +02:00
|
|
|
|
Ensure same order of global group members in all NS's
During "step 1" of find_libraries, the linker finds the transitive
closure of dependencies, in BFS order. As it finds each library, it
adds the library to its primary namespace (so that, if some other
library also depends on it, find_loaded_library_by_soname can find the
library in the process of being loaded).
LD_PRELOAD libraries are automatically marked DF_1_GLOBAL, and any
DF_1_GLOBAL library is added to every linker namespace. Previously,
this secondary namespace registration happened after step 1. The result
is that across different namespaces, the order of libraries could vary.
In general, a namespace's primary members will all appear before
secondary members. This is undesirable for libsigchain.so, which we
want to have appear before any other non-preloaded library.
Instead, when an soinfo is added to its primary namespace, immediately
add it to all the other namespaces, too. This ensures that the order of
soinfo objects is the same across namespaces.
Expand the dl.exec_with_ld_config_file_with_ld_preload and
dl.exec_with_ld_config_file tests to cover the new behavior. Mark
lib1.so DF_1_GLOBAL and use a "foo" symbol to mimic the behavior of a
signal API interposed by (e.g.) libsigchain.so and a ASAN preload.
Test: bionic unit tests
Bug: http://b/143219447
Change-Id: I9fd90f6f0d14caf1aca6d414b3e9aab77deca3ff
2020-12-18 07:59:04 +01:00
|
|
|
// lib1.so and lib2.so are now searchable by having another namespace 'ns2'
|
2017-05-19 18:01:24 +02:00
|
|
|
// whose search paths include the 'ns2/' subdir.
|
Ensure same order of global group members in all NS's
During "step 1" of find_libraries, the linker finds the transitive
closure of dependencies, in BFS order. As it finds each library, it
adds the library to its primary namespace (so that, if some other
library also depends on it, find_loaded_library_by_soname can find the
library in the process of being loaded).
LD_PRELOAD libraries are automatically marked DF_1_GLOBAL, and any
DF_1_GLOBAL library is added to every linker namespace. Previously,
this secondary namespace registration happened after step 1. The result
is that across different namespaces, the order of libraries could vary.
In general, a namespace's primary members will all appear before
secondary members. This is undesirable for libsigchain.so, which we
want to have appear before any other non-preloaded library.
Instead, when an soinfo is added to its primary namespace, immediately
add it to all the other namespaces, too. This ensures that the order of
soinfo objects is the same across namespaces.
Expand the dl.exec_with_ld_config_file_with_ld_preload and
dl.exec_with_ld_config_file tests to cover the new behavior. Mark
lib1.so DF_1_GLOBAL and use a "foo" symbol to mimic the behavior of a
signal API interposed by (e.g.) libsigchain.so and a ASAN preload.
Test: bionic unit tests
Bug: http://b/143219447
Change-Id: I9fd90f6f0d14caf1aca6d414b3e9aab77deca3ff
2020-12-18 07:59:04 +01:00
|
|
|
//
|
|
|
|
// lib1.so is linked with DF_1_GLOBAL, so both it and the executable are added
|
|
|
|
// to every namespace.
|
|
|
|
//
|
|
|
|
// namespace configuration ('*' indicates primary ns)
|
|
|
|
// - default: exe[*], lib1.so
|
|
|
|
// - ns2: exe, lib1.so[*], lib2.so[*]
|
|
|
|
//
|
2017-05-19 18:01:24 +02:00
|
|
|
TEST(dl, exec_with_ld_config_file) {
|
|
|
|
#if defined(__BIONIC__)
|
2019-03-09 00:20:23 +01:00
|
|
|
SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config";
|
2021-06-05 02:27:39 +02:00
|
|
|
if (is_user_build()) {
|
2019-12-10 23:45:20 +01:00
|
|
|
GTEST_SKIP() << "LD_CONFIG_FILE is not supported on user build";
|
2017-09-19 02:48:07 +02:00
|
|
|
}
|
2021-09-25 02:49:58 +02:00
|
|
|
std::string helper = GetTestlibRoot() + "/ld_config_test_helper";
|
2018-04-04 05:03:12 +02:00
|
|
|
TemporaryFile config_file;
|
2018-11-13 16:35:21 +01:00
|
|
|
create_ld_config_file(config_file.path);
|
|
|
|
std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
|
2017-05-19 18:01:24 +02:00
|
|
|
chmod(helper.c_str(), 0755);
|
|
|
|
ExecTestHelper eth;
|
|
|
|
eth.SetArgs({ helper.c_str(), nullptr });
|
|
|
|
eth.SetEnv({ env.c_str(), nullptr });
|
Ensure same order of global group members in all NS's
During "step 1" of find_libraries, the linker finds the transitive
closure of dependencies, in BFS order. As it finds each library, it
adds the library to its primary namespace (so that, if some other
library also depends on it, find_loaded_library_by_soname can find the
library in the process of being loaded).
LD_PRELOAD libraries are automatically marked DF_1_GLOBAL, and any
DF_1_GLOBAL library is added to every linker namespace. Previously,
this secondary namespace registration happened after step 1. The result
is that across different namespaces, the order of libraries could vary.
In general, a namespace's primary members will all appear before
secondary members. This is undesirable for libsigchain.so, which we
want to have appear before any other non-preloaded library.
Instead, when an soinfo is added to its primary namespace, immediately
add it to all the other namespaces, too. This ensures that the order of
soinfo objects is the same across namespaces.
Expand the dl.exec_with_ld_config_file_with_ld_preload and
dl.exec_with_ld_config_file tests to cover the new behavior. Mark
lib1.so DF_1_GLOBAL and use a "foo" symbol to mimic the behavior of a
signal API interposed by (e.g.) libsigchain.so and a ASAN preload.
Test: bionic unit tests
Bug: http://b/143219447
Change-Id: I9fd90f6f0d14caf1aca6d414b3e9aab77deca3ff
2020-12-18 07:59:04 +01:00
|
|
|
eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0,
|
|
|
|
"foo lib1\n"
|
|
|
|
"lib1_call_funcs\n"
|
|
|
|
"foo lib1\n"
|
|
|
|
"bar lib2\n");
|
2017-05-19 18:01:24 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
Ensure same order of global group members in all NS's
During "step 1" of find_libraries, the linker finds the transitive
closure of dependencies, in BFS order. As it finds each library, it
adds the library to its primary namespace (so that, if some other
library also depends on it, find_loaded_library_by_soname can find the
library in the process of being loaded).
LD_PRELOAD libraries are automatically marked DF_1_GLOBAL, and any
DF_1_GLOBAL library is added to every linker namespace. Previously,
this secondary namespace registration happened after step 1. The result
is that across different namespaces, the order of libraries could vary.
In general, a namespace's primary members will all appear before
secondary members. This is undesirable for libsigchain.so, which we
want to have appear before any other non-preloaded library.
Instead, when an soinfo is added to its primary namespace, immediately
add it to all the other namespaces, too. This ensures that the order of
soinfo objects is the same across namespaces.
Expand the dl.exec_with_ld_config_file_with_ld_preload and
dl.exec_with_ld_config_file tests to cover the new behavior. Mark
lib1.so DF_1_GLOBAL and use a "foo" symbol to mimic the behavior of a
signal API interposed by (e.g.) libsigchain.so and a ASAN preload.
Test: bionic unit tests
Bug: http://b/143219447
Change-Id: I9fd90f6f0d14caf1aca6d414b3e9aab77deca3ff
2020-12-18 07:59:04 +01:00
|
|
|
// lib3.so has same foo and bar symbols as lib2.so. lib3.so is LD_PRELOADed.
|
|
|
|
// This test ensures that LD_PRELOADed libs are available to all namespaces.
|
|
|
|
//
|
|
|
|
// namespace configuration ('*' indicates primary ns)
|
|
|
|
// - default: exe[*], lib3.so[*], lib1.so
|
|
|
|
// - ns2: exe, lib3.so, lib1.so[*], lib2.so[*]
|
|
|
|
//
|
|
|
|
// Ensure that, in both namespaces, a call to foo calls the lib3.so symbol,
|
|
|
|
// which then calls the lib1.so symbol using RTLD_NEXT. Ensure that RTLD_NEXT
|
|
|
|
// finds nothing when called from lib1.so.
|
|
|
|
//
|
|
|
|
// For the bar symbol, lib3.so's primary namespace is the default namespace, but
|
|
|
|
// lib2.so is not in the default namespace, so using RTLD_NEXT from lib3.so
|
|
|
|
// doesn't find the symbol in lib2.so.
|
2017-05-19 18:01:24 +02:00
|
|
|
TEST(dl, exec_with_ld_config_file_with_ld_preload) {
|
|
|
|
#if defined(__BIONIC__)
|
2019-03-09 00:20:23 +01:00
|
|
|
SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config";
|
2021-06-05 02:27:39 +02:00
|
|
|
if (is_user_build()) {
|
2019-12-10 23:45:20 +01:00
|
|
|
GTEST_SKIP() << "LD_CONFIG_FILE is not supported on user build";
|
2017-09-19 02:48:07 +02:00
|
|
|
}
|
2021-09-25 02:49:58 +02:00
|
|
|
std::string helper = GetTestlibRoot() + "/ld_config_test_helper";
|
2018-04-04 05:03:12 +02:00
|
|
|
TemporaryFile config_file;
|
2018-11-13 16:35:21 +01:00
|
|
|
create_ld_config_file(config_file.path);
|
|
|
|
std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
|
2018-08-22 03:13:10 +02:00
|
|
|
std::string env2 = std::string("LD_PRELOAD=") + GetTestlibRoot() + "/ld_config_test_helper_lib3.so";
|
2017-05-19 18:01:24 +02:00
|
|
|
chmod(helper.c_str(), 0755);
|
|
|
|
ExecTestHelper eth;
|
|
|
|
eth.SetArgs({ helper.c_str(), nullptr });
|
|
|
|
eth.SetEnv({ env.c_str(), env2.c_str(), nullptr });
|
Ensure same order of global group members in all NS's
During "step 1" of find_libraries, the linker finds the transitive
closure of dependencies, in BFS order. As it finds each library, it
adds the library to its primary namespace (so that, if some other
library also depends on it, find_loaded_library_by_soname can find the
library in the process of being loaded).
LD_PRELOAD libraries are automatically marked DF_1_GLOBAL, and any
DF_1_GLOBAL library is added to every linker namespace. Previously,
this secondary namespace registration happened after step 1. The result
is that across different namespaces, the order of libraries could vary.
In general, a namespace's primary members will all appear before
secondary members. This is undesirable for libsigchain.so, which we
want to have appear before any other non-preloaded library.
Instead, when an soinfo is added to its primary namespace, immediately
add it to all the other namespaces, too. This ensures that the order of
soinfo objects is the same across namespaces.
Expand the dl.exec_with_ld_config_file_with_ld_preload and
dl.exec_with_ld_config_file tests to cover the new behavior. Mark
lib1.so DF_1_GLOBAL and use a "foo" symbol to mimic the behavior of a
signal API interposed by (e.g.) libsigchain.so and a ASAN preload.
Test: bionic unit tests
Bug: http://b/143219447
Change-Id: I9fd90f6f0d14caf1aca6d414b3e9aab77deca3ff
2020-12-18 07:59:04 +01:00
|
|
|
eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0,
|
|
|
|
"foo lib3\n"
|
|
|
|
"foo lib1\n"
|
|
|
|
"lib1_call_funcs\n"
|
|
|
|
"foo lib3\n"
|
|
|
|
"foo lib1\n"
|
|
|
|
"bar lib3\n"
|
|
|
|
"lib3_call_funcs\n"
|
|
|
|
"foo lib3\n"
|
|
|
|
"foo lib1\n"
|
|
|
|
"bar lib3\n");
|
2017-05-19 18:01:24 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensures that LD_CONFIG_FILE env var does not work for production builds.
|
|
|
|
// The test input is the same as exec_with_ld_config_file, but it must fail in
|
|
|
|
// this case.
|
|
|
|
TEST(dl, disable_ld_config_file) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
if (getuid() == 0) {
|
|
|
|
// when executed from the shell (e.g. not as part of CTS), skip the test.
|
|
|
|
// This test is only for CTS.
|
2019-12-10 23:45:20 +01:00
|
|
|
GTEST_SKIP() << "test is not supported with root uid";
|
2017-05-19 18:01:24 +02:00
|
|
|
}
|
2021-06-05 02:27:39 +02:00
|
|
|
if (!is_user_build()) {
|
|
|
|
GTEST_SKIP() << "test requires user build";
|
2017-08-30 04:30:53 +02:00
|
|
|
}
|
|
|
|
|
2021-09-25 02:49:58 +02:00
|
|
|
std::string error_message =
|
|
|
|
std::string("CANNOT LINK EXECUTABLE ") + "\"" + GetTestlibRoot() +
|
|
|
|
"/ld_config_test_helper\": " +
|
2019-12-10 23:50:11 +01:00
|
|
|
"library \"ld_config_test_helper_lib1.so\" not found: needed by main executable\n";
|
2021-09-25 02:49:58 +02:00
|
|
|
std::string helper = GetTestlibRoot() + "/ld_config_test_helper";
|
2018-04-04 05:03:12 +02:00
|
|
|
TemporaryFile config_file;
|
2018-11-13 16:35:21 +01:00
|
|
|
create_ld_config_file(config_file.path);
|
|
|
|
std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
|
2017-05-19 18:01:24 +02:00
|
|
|
chmod(helper.c_str(), 0755);
|
|
|
|
ExecTestHelper eth;
|
|
|
|
eth.SetArgs({ helper.c_str(), nullptr });
|
|
|
|
eth.SetEnv({ env.c_str(), nullptr });
|
2017-11-28 05:37:03 +01:00
|
|
|
eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
|
2017-05-19 18:01:24 +02:00
|
|
|
#endif
|
|
|
|
}
|
2020-01-28 21:18:35 +01:00
|
|
|
|
|
|
|
static void RelocationsTest(const char* lib, const char* expectation) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
// Does readelf think the .so file looks right?
|
|
|
|
const std::string path = GetTestlibRoot() + "/" + lib;
|
|
|
|
ExecTestHelper eth;
|
|
|
|
eth.SetArgs({ "readelf", "-SW", path.c_str(), nullptr });
|
|
|
|
eth.Run([&]() { execvpe("readelf", eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
|
2021-04-13 00:55:29 +02:00
|
|
|
|
|
|
|
ASSERT_TRUE(std::regex_search(eth.GetOutput(), std::regex(expectation))) << eth.GetOutput();
|
2020-01-28 21:18:35 +01:00
|
|
|
|
|
|
|
// Can we load it?
|
|
|
|
void* handle = dlopen(lib, RTLD_NOW);
|
|
|
|
ASSERT_TRUE(handle != nullptr) << dlerror();
|
|
|
|
#else
|
|
|
|
UNUSED(lib);
|
|
|
|
UNUSED(expectation);
|
|
|
|
GTEST_SKIP() << "test is not supported on glibc";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(dl, relocations_RELR) {
|
2021-04-13 00:55:29 +02:00
|
|
|
RelocationsTest("librelocations-RELR.so", "\\.relr\\.dyn * RELR");
|
2020-01-28 21:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(dl, relocations_ANDROID_RELR) {
|
2021-04-13 00:55:29 +02:00
|
|
|
RelocationsTest("librelocations-ANDROID_RELR.so", "\\.relr\\.dyn * ANDROID_RELR");
|
2020-01-28 21:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(dl, relocations_ANDROID_REL) {
|
|
|
|
RelocationsTest("librelocations-ANDROID_REL.so",
|
|
|
|
#if __LP64__
|
2021-04-13 00:55:29 +02:00
|
|
|
"\\.rela\\.dyn * ANDROID_RELA"
|
2020-01-28 21:18:35 +01:00
|
|
|
#else
|
2021-04-13 00:55:29 +02:00
|
|
|
"\\.rel\\.dyn * ANDROID_REL"
|
2020-01-28 21:18:35 +01:00
|
|
|
#endif
|
2021-04-13 00:55:29 +02:00
|
|
|
);
|
2020-01-28 21:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(dl, relocations_fat) {
|
|
|
|
RelocationsTest("librelocations-fat.so",
|
|
|
|
#if __LP64__
|
2021-04-13 00:55:29 +02:00
|
|
|
"\\.rela\\.dyn * RELA"
|
2020-01-28 21:18:35 +01:00
|
|
|
#else
|
2021-04-13 00:55:29 +02:00
|
|
|
"\\.rel\\.dyn * REL"
|
2020-01-28 21:18:35 +01:00
|
|
|
#endif
|
2021-04-13 00:55:29 +02:00
|
|
|
);
|
2020-01-28 21:18:35 +01:00
|
|
|
}
|