2017-03-07 20:19:05 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "../linker_config.h"
|
2017-06-16 07:43:16 +02:00
|
|
|
#include "../linker_utils.h"
|
2017-03-07 20:19:05 +01:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-04-06 01:20:29 +02:00
|
|
|
#include <android-base/scopeguard.h>
|
2017-03-07 20:19:05 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/test_utils.h>
|
|
|
|
|
2017-06-16 07:43:16 +02:00
|
|
|
#if defined(__LP64__)
|
|
|
|
#define ARCH_SUFFIX "64"
|
|
|
|
#else
|
|
|
|
#define ARCH_SUFFIX ""
|
|
|
|
#endif
|
2017-03-07 20:19:05 +01:00
|
|
|
|
|
|
|
static const char* config_str =
|
|
|
|
"# comment \n"
|
|
|
|
"dir.test = /data/local/tmp\n"
|
|
|
|
"\n"
|
|
|
|
"[test]\n"
|
|
|
|
"\n"
|
|
|
|
"enable.target.sdk.version = true\n"
|
|
|
|
"additional.namespaces=system\n"
|
2017-11-29 10:30:53 +01:00
|
|
|
"additional.namespaces+=vndk\n"
|
2017-03-07 20:19:05 +01:00
|
|
|
"namespace.default.isolated = true\n"
|
|
|
|
"namespace.default.search.paths = /vendor/${LIB}\n"
|
|
|
|
"namespace.default.permitted.paths = /vendor/${LIB}\n"
|
2017-11-29 10:30:53 +01:00
|
|
|
"namespace.default.asan.search.paths = /data\n"
|
|
|
|
"namespace.default.asan.search.paths += /vendor/${LIB}\n"
|
2017-03-07 20:19:05 +01:00
|
|
|
"namespace.default.asan.permitted.paths = /data:/vendor\n"
|
|
|
|
"namespace.default.links = system\n"
|
2017-11-29 10:30:53 +01:00
|
|
|
"namespace.default.links += vndk\n"
|
|
|
|
// irregular whitespaces are added intentionally for testing purpose
|
|
|
|
"namespace.default.link.system.shared_libs= libc.so\n"
|
|
|
|
"namespace.default.link.system.shared_libs += libm.so:libdl.so\n"
|
|
|
|
"namespace.default.link.system.shared_libs +=libstdc++.so\n"
|
|
|
|
"namespace.default.link.vndk.shared_libs = libcutils.so:libbase.so\n"
|
2017-03-07 20:19:05 +01:00
|
|
|
"namespace.system.isolated = true\n"
|
2017-04-03 16:10:37 +02:00
|
|
|
"namespace.system.visible = true\n"
|
2017-03-07 20:19:05 +01:00
|
|
|
"namespace.system.search.paths = /system/${LIB}\n"
|
|
|
|
"namespace.system.permitted.paths = /system/${LIB}\n"
|
|
|
|
"namespace.system.asan.search.paths = /data:/system/${LIB}\n"
|
|
|
|
"namespace.system.asan.permitted.paths = /data:/system\n"
|
2017-11-29 10:30:53 +01:00
|
|
|
"namespace.vndk.isolated = tr\n"
|
|
|
|
"namespace.vndk.isolated += ue\n" // should be ignored and return as 'false'.
|
|
|
|
"namespace.vndk.search.paths = /system/${LIB}/vndk\n"
|
|
|
|
"namespace.vndk.asan.search.paths = /data\n"
|
|
|
|
"namespace.vndk.asan.search.paths += /system/${LIB}/vndk\n"
|
2018-01-18 05:05:09 +01:00
|
|
|
"namespace.vndk.links = default\n"
|
|
|
|
"namespace.vndk.link.default.allow_all_shared_libs = true\n"
|
2017-03-07 20:19:05 +01:00
|
|
|
"\n";
|
|
|
|
|
|
|
|
static bool write_version(const std::string& path, uint32_t version) {
|
|
|
|
std::string content = android::base::StringPrintf("%d", version);
|
|
|
|
return android::base::WriteStringToFile(content, path);
|
|
|
|
}
|
|
|
|
|
2017-06-16 07:43:16 +02:00
|
|
|
static std::vector<std::string> resolve_paths(std::vector<std::string> paths) {
|
|
|
|
std::vector<std::string> resolved_paths;
|
|
|
|
resolve_paths(paths, &resolved_paths);
|
|
|
|
return resolved_paths;
|
|
|
|
}
|
2017-03-07 20:19:05 +01:00
|
|
|
|
2017-06-16 07:43:16 +02:00
|
|
|
static void run_linker_config_smoke_test(bool is_asan) {
|
|
|
|
const std::vector<std::string> kExpectedDefaultSearchPath =
|
|
|
|
resolve_paths(is_asan ? std::vector<std::string>({ "/data", "/vendor/lib" ARCH_SUFFIX }) :
|
|
|
|
std::vector<std::string>({ "/vendor/lib" ARCH_SUFFIX }));
|
2017-03-07 20:19:05 +01:00
|
|
|
|
2017-06-16 07:43:16 +02:00
|
|
|
const std::vector<std::string> kExpectedDefaultPermittedPath =
|
|
|
|
resolve_paths(is_asan ? std::vector<std::string>({ "/data", "/vendor" }) :
|
|
|
|
std::vector<std::string>({ "/vendor/lib" ARCH_SUFFIX }));
|
2017-03-07 20:19:05 +01:00
|
|
|
|
2017-06-16 07:43:16 +02:00
|
|
|
const std::vector<std::string> kExpectedSystemSearchPath =
|
|
|
|
resolve_paths(is_asan ? std::vector<std::string>({ "/data", "/system/lib" ARCH_SUFFIX }) :
|
|
|
|
std::vector<std::string>({ "/system/lib" ARCH_SUFFIX }));
|
2017-03-07 20:19:05 +01:00
|
|
|
|
2017-06-16 07:43:16 +02:00
|
|
|
const std::vector<std::string> kExpectedSystemPermittedPath =
|
|
|
|
resolve_paths(is_asan ? std::vector<std::string>({ "/data", "/system" }) :
|
|
|
|
std::vector<std::string>({ "/system/lib" ARCH_SUFFIX }));
|
2017-03-07 20:19:05 +01:00
|
|
|
|
2017-11-29 10:30:53 +01:00
|
|
|
const std::vector<std::string> kExpectedVndkSearchPath =
|
|
|
|
resolve_paths(is_asan ? std::vector<std::string>({ "/data", "/system/lib" ARCH_SUFFIX "/vndk"}) :
|
|
|
|
std::vector<std::string>({ "/system/lib" ARCH_SUFFIX "/vndk"}));
|
|
|
|
|
2017-03-07 20:19:05 +01:00
|
|
|
TemporaryFile tmp_file;
|
|
|
|
close(tmp_file.fd);
|
|
|
|
tmp_file.fd = -1;
|
|
|
|
|
|
|
|
android::base::WriteStringToFile(config_str, tmp_file.path);
|
|
|
|
|
|
|
|
TemporaryDir tmp_dir;
|
|
|
|
|
|
|
|
std::string executable_path = std::string(tmp_dir.path) + "/some-binary";
|
|
|
|
std::string version_file = std::string(tmp_dir.path) + "/.version";
|
|
|
|
|
2017-04-06 01:20:29 +02:00
|
|
|
auto file_guard =
|
|
|
|
android::base::make_scope_guard([&version_file] { unlink(version_file.c_str()); });
|
2017-03-07 20:19:05 +01:00
|
|
|
|
|
|
|
ASSERT_TRUE(write_version(version_file, 113U)) << strerror(errno);
|
|
|
|
|
|
|
|
// read config
|
|
|
|
const Config* config = nullptr;
|
|
|
|
std::string error_msg;
|
|
|
|
ASSERT_TRUE(Config::read_binary_config(tmp_file.path,
|
|
|
|
executable_path.c_str(),
|
|
|
|
is_asan,
|
|
|
|
&config,
|
|
|
|
&error_msg)) << error_msg;
|
|
|
|
ASSERT_TRUE(config != nullptr);
|
|
|
|
ASSERT_TRUE(error_msg.empty());
|
|
|
|
|
2018-11-13 01:01:37 +01:00
|
|
|
ASSERT_EQ(113, config->target_sdk_version());
|
2017-03-07 20:19:05 +01:00
|
|
|
|
|
|
|
const NamespaceConfig* default_ns_config = config->default_namespace_config();
|
|
|
|
ASSERT_TRUE(default_ns_config != nullptr);
|
|
|
|
|
|
|
|
ASSERT_TRUE(default_ns_config->isolated());
|
2017-04-03 16:10:37 +02:00
|
|
|
ASSERT_FALSE(default_ns_config->visible());
|
2017-03-07 20:19:05 +01:00
|
|
|
ASSERT_EQ(kExpectedDefaultSearchPath, default_ns_config->search_paths());
|
|
|
|
ASSERT_EQ(kExpectedDefaultPermittedPath, default_ns_config->permitted_paths());
|
|
|
|
|
|
|
|
const auto& default_ns_links = default_ns_config->links();
|
2017-11-29 10:30:53 +01:00
|
|
|
ASSERT_EQ(2U, default_ns_links.size());
|
2018-01-18 05:05:09 +01:00
|
|
|
|
2017-03-07 20:19:05 +01:00
|
|
|
ASSERT_EQ("system", default_ns_links[0].ns_name());
|
|
|
|
ASSERT_EQ("libc.so:libm.so:libdl.so:libstdc++.so", default_ns_links[0].shared_libs());
|
2018-01-18 05:05:09 +01:00
|
|
|
ASSERT_FALSE(default_ns_links[0].allow_all_shared_libs());
|
|
|
|
|
2017-11-29 10:30:53 +01:00
|
|
|
ASSERT_EQ("vndk", default_ns_links[1].ns_name());
|
|
|
|
ASSERT_EQ("libcutils.so:libbase.so", default_ns_links[1].shared_libs());
|
2018-01-18 05:05:09 +01:00
|
|
|
ASSERT_FALSE(default_ns_links[1].allow_all_shared_libs());
|
2017-03-07 20:19:05 +01:00
|
|
|
|
|
|
|
auto& ns_configs = config->namespace_configs();
|
2017-11-29 10:30:53 +01:00
|
|
|
ASSERT_EQ(3U, ns_configs.size());
|
2017-03-07 20:19:05 +01:00
|
|
|
|
|
|
|
// find second namespace
|
|
|
|
const NamespaceConfig* ns_system = nullptr;
|
2017-11-29 10:30:53 +01:00
|
|
|
const NamespaceConfig* ns_vndk = nullptr;
|
2017-03-07 20:19:05 +01:00
|
|
|
for (auto& ns : ns_configs) {
|
|
|
|
std::string ns_name = ns->name();
|
2017-11-29 10:30:53 +01:00
|
|
|
ASSERT_TRUE(ns_name == "system" || ns_name == "default" || ns_name == "vndk")
|
2017-03-07 20:19:05 +01:00
|
|
|
<< "unexpected ns name: " << ns->name();
|
|
|
|
|
|
|
|
if (ns_name == "system") {
|
|
|
|
ns_system = ns.get();
|
2017-11-29 10:30:53 +01:00
|
|
|
} else if (ns_name == "vndk") {
|
|
|
|
ns_vndk = ns.get();
|
2017-03-07 20:19:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_TRUE(ns_system != nullptr) << "system namespace was not found";
|
|
|
|
|
|
|
|
ASSERT_TRUE(ns_system->isolated());
|
2017-04-03 16:10:37 +02:00
|
|
|
ASSERT_TRUE(ns_system->visible());
|
2017-03-07 20:19:05 +01:00
|
|
|
ASSERT_EQ(kExpectedSystemSearchPath, ns_system->search_paths());
|
|
|
|
ASSERT_EQ(kExpectedSystemPermittedPath, ns_system->permitted_paths());
|
2017-11-29 10:30:53 +01:00
|
|
|
|
|
|
|
ASSERT_TRUE(ns_vndk != nullptr) << "vndk namespace was not found";
|
|
|
|
|
|
|
|
ASSERT_FALSE(ns_vndk->isolated()); // malformed bool property
|
2018-01-18 05:05:09 +01:00
|
|
|
ASSERT_FALSE(ns_vndk->visible()); // undefined bool property
|
2017-11-29 10:30:53 +01:00
|
|
|
ASSERT_EQ(kExpectedVndkSearchPath, ns_vndk->search_paths());
|
2018-01-18 05:05:09 +01:00
|
|
|
|
|
|
|
const auto& ns_vndk_links = ns_vndk->links();
|
|
|
|
ASSERT_EQ(1U, ns_vndk_links.size());
|
|
|
|
ASSERT_EQ("default", ns_vndk_links[0].ns_name());
|
|
|
|
ASSERT_TRUE(ns_vndk_links[0].allow_all_shared_libs());
|
2017-03-07 20:19:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(linker_config, smoke) {
|
|
|
|
run_linker_config_smoke_test(false);
|
|
|
|
}
|
|
|
|
|
2017-08-04 03:25:46 +02:00
|
|
|
TEST(linker_config, asan_smoke) {
|
|
|
|
run_linker_config_smoke_test(true);
|
|
|
|
}
|
2018-01-18 05:05:09 +01:00
|
|
|
|
|
|
|
TEST(linker_config, ns_link_shared_libs_invalid_settings) {
|
|
|
|
// This unit test ensures an error is emitted when a namespace link in ld.config.txt specifies
|
|
|
|
// both shared_libs and allow_all_shared_libs.
|
|
|
|
|
|
|
|
static const char config_str[] =
|
|
|
|
"dir.test = /data/local/tmp\n"
|
|
|
|
"\n"
|
|
|
|
"[test]\n"
|
|
|
|
"additional.namespaces = system\n"
|
|
|
|
"namespace.default.links = system\n"
|
|
|
|
"namespace.default.link.system.shared_libs = libc.so:libm.so\n"
|
|
|
|
"namespace.default.link.system.allow_all_shared_libs = true\n"
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
TemporaryFile tmp_file;
|
|
|
|
close(tmp_file.fd);
|
|
|
|
tmp_file.fd = -1;
|
|
|
|
|
|
|
|
android::base::WriteStringToFile(config_str, tmp_file.path);
|
|
|
|
|
|
|
|
TemporaryDir tmp_dir;
|
|
|
|
|
|
|
|
std::string executable_path = std::string(tmp_dir.path) + "/some-binary";
|
|
|
|
|
|
|
|
const Config* config = nullptr;
|
|
|
|
std::string error_msg;
|
|
|
|
ASSERT_FALSE(Config::read_binary_config(tmp_file.path,
|
|
|
|
executable_path.c_str(),
|
|
|
|
false,
|
|
|
|
&config,
|
|
|
|
&error_msg));
|
|
|
|
ASSERT_TRUE(config == nullptr);
|
|
|
|
ASSERT_EQ(std::string(tmp_file.path) + ":6: "
|
|
|
|
"error: both shared_libs and allow_all_shared_libs are set for default->system link.",
|
|
|
|
error_msg);
|
|
|
|
}
|
2018-06-18 08:30:18 +02:00
|
|
|
|
|
|
|
TEST(linker_config, dir_path_resolve) {
|
|
|
|
// This unit test ensures the linker resolves paths of dir.${section}
|
|
|
|
// properties to real path.
|
|
|
|
|
|
|
|
TemporaryDir tmp_dir;
|
|
|
|
|
|
|
|
std::string sub_dir = std::string(tmp_dir.path) + "/subdir";
|
|
|
|
mkdir(sub_dir.c_str(), 0755);
|
|
|
|
|
|
|
|
auto subdir_guard =
|
|
|
|
android::base::make_scope_guard([&sub_dir] { rmdir(sub_dir.c_str()); });
|
|
|
|
|
|
|
|
std::string symlink_path = std::string(tmp_dir.path) + "/symlink";
|
|
|
|
symlink(sub_dir.c_str(), symlink_path.c_str());
|
|
|
|
|
|
|
|
auto symlink_guard =
|
|
|
|
android::base::make_scope_guard([&symlink_path] { unlink(symlink_path.c_str()); });
|
|
|
|
|
|
|
|
std::string config_str =
|
|
|
|
"dir.test = " + symlink_path + "\n"
|
|
|
|
"\n"
|
|
|
|
"[test]\n";
|
|
|
|
|
|
|
|
TemporaryFile tmp_file;
|
|
|
|
close(tmp_file.fd);
|
|
|
|
tmp_file.fd = -1;
|
|
|
|
|
|
|
|
android::base::WriteStringToFile(config_str, tmp_file.path);
|
|
|
|
|
|
|
|
std::string executable_path = sub_dir + "/some-binary";
|
|
|
|
|
|
|
|
const Config* config = nullptr;
|
|
|
|
std::string error_msg;
|
|
|
|
|
|
|
|
ASSERT_TRUE(Config::read_binary_config(tmp_file.path,
|
|
|
|
executable_path.c_str(),
|
|
|
|
false,
|
|
|
|
&config,
|
|
|
|
&error_msg)) << error_msg;
|
|
|
|
|
|
|
|
ASSERT_TRUE(config != nullptr) << error_msg;
|
|
|
|
ASSERT_TRUE(error_msg.empty()) << error_msg;
|
|
|
|
}
|