blob: 418cbda37fa68dae6282c38e368764d61ab1e7c8 [file] [log] [blame]
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <stdlib.h>
30#include <string.h>
31#include <sys/mman.h>
32
33#include <gtest/gtest.h>
34
35#include "../linker_config.h"
36
37#include <unistd.h>
38
Tom Cherryb8ab6182017-04-05 16:20:29 -070039#include <android-base/scopeguard.h>
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080040#include <android-base/stringprintf.h>
41#include <android-base/file.h>
42#include <android-base/test_utils.h>
43
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080044
45static const char* config_str =
46 "# comment \n"
47 "dir.test = /data/local/tmp\n"
48 "\n"
49 "[test]\n"
50 "\n"
51 "enable.target.sdk.version = true\n"
52 "additional.namespaces=system\n"
53 "namespace.default.isolated = true\n"
54 "namespace.default.search.paths = /vendor/${LIB}\n"
55 "namespace.default.permitted.paths = /vendor/${LIB}\n"
56 "namespace.default.asan.search.paths = /data:/vendor/${LIB}\n"
57 "namespace.default.asan.permitted.paths = /data:/vendor\n"
58 "namespace.default.links = system\n"
59 "namespace.default.link.system.shared_libs = libc.so:libm.so:libdl.so:libstdc++.so\n"
60 "namespace.system.isolated = true\n"
61 "namespace.system.search.paths = /system/${LIB}\n"
62 "namespace.system.permitted.paths = /system/${LIB}\n"
63 "namespace.system.asan.search.paths = /data:/system/${LIB}\n"
64 "namespace.system.asan.permitted.paths = /data:/system\n"
65 "\n";
66
67static bool write_version(const std::string& path, uint32_t version) {
68 std::string content = android::base::StringPrintf("%d", version);
69 return android::base::WriteStringToFile(content, path);
70}
71
72static void run_linker_config_smoke_test(bool is_asan) {
73#if defined(__LP64__)
74 const std::vector<std::string> kExpectedDefaultSearchPath = is_asan ?
75 std::vector<std::string>({ "/data", "/vendor/lib64"}) :
76 std::vector<std::string>({ "/vendor/lib64" });
77
78 const std::vector<std::string> kExpectedDefaultPermittedPath = is_asan ?
79 std::vector<std::string>({ "/data", "/vendor" }) :
80 std::vector<std::string>({ "/vendor/lib64" });
81
82 const std::vector<std::string> kExpectedSystemSearchPath = is_asan ?
83 std::vector<std::string>({ "/data", "/system/lib64" }) :
84 std::vector<std::string>({ "/system/lib64" });
85
86 const std::vector<std::string> kExpectedSystemPermittedPath = is_asan ?
87 std::vector<std::string>({ "/data", "/system" }) :
88 std::vector<std::string>({ "/system/lib64" });
89#else
90 const std::vector<std::string> kExpectedDefaultSearchPath = is_asan ?
91 std::vector<std::string>({ "/data", "/vendor/lib"}) :
92 std::vector<std::string>({ "/vendor/lib" });
93
94 const std::vector<std::string> kExpectedDefaultPermittedPath = is_asan ?
95 std::vector<std::string>({ "/data", "/vendor" }) :
96 std::vector<std::string>({ "/vendor/lib" });
97
98 const std::vector<std::string> kExpectedSystemSearchPath = is_asan ?
99 std::vector<std::string>({ "/data", "/system/lib" }) :
100 std::vector<std::string>({ "/system/lib" });
101
102 const std::vector<std::string> kExpectedSystemPermittedPath = is_asan ?
103 std::vector<std::string>({ "/data", "/system" }) :
104 std::vector<std::string>({ "/system/lib" });
105#endif
106
107 TemporaryFile tmp_file;
108 close(tmp_file.fd);
109 tmp_file.fd = -1;
110
111 android::base::WriteStringToFile(config_str, tmp_file.path);
112
113 TemporaryDir tmp_dir;
114
115 std::string executable_path = std::string(tmp_dir.path) + "/some-binary";
116 std::string version_file = std::string(tmp_dir.path) + "/.version";
117
Tom Cherryb8ab6182017-04-05 16:20:29 -0700118 auto file_guard =
119 android::base::make_scope_guard([&version_file] { unlink(version_file.c_str()); });
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800120
121 ASSERT_TRUE(write_version(version_file, 113U)) << strerror(errno);
122
123 // read config
124 const Config* config = nullptr;
125 std::string error_msg;
126 ASSERT_TRUE(Config::read_binary_config(tmp_file.path,
127 executable_path.c_str(),
128 is_asan,
129 &config,
130 &error_msg)) << error_msg;
131 ASSERT_TRUE(config != nullptr);
132 ASSERT_TRUE(error_msg.empty());
133
134 ASSERT_EQ(113U, config->target_sdk_version());
135
136 const NamespaceConfig* default_ns_config = config->default_namespace_config();
137 ASSERT_TRUE(default_ns_config != nullptr);
138
139 ASSERT_TRUE(default_ns_config->isolated());
140 ASSERT_EQ(kExpectedDefaultSearchPath, default_ns_config->search_paths());
141 ASSERT_EQ(kExpectedDefaultPermittedPath, default_ns_config->permitted_paths());
142
143 const auto& default_ns_links = default_ns_config->links();
144 ASSERT_EQ(1U, default_ns_links.size());
145 ASSERT_EQ("system", default_ns_links[0].ns_name());
146 ASSERT_EQ("libc.so:libm.so:libdl.so:libstdc++.so", default_ns_links[0].shared_libs());
147
148 auto& ns_configs = config->namespace_configs();
149 ASSERT_EQ(2U, ns_configs.size());
150
151 // find second namespace
152 const NamespaceConfig* ns_system = nullptr;
153 for (auto& ns : ns_configs) {
154 std::string ns_name = ns->name();
155 ASSERT_TRUE(ns_name == "system" || ns_name == "default")
156 << "unexpected ns name: " << ns->name();
157
158 if (ns_name == "system") {
159 ns_system = ns.get();
160 }
161 }
162
163 ASSERT_TRUE(ns_system != nullptr) << "system namespace was not found";
164
165 ASSERT_TRUE(ns_system->isolated());
166 ASSERT_EQ(kExpectedSystemSearchPath, ns_system->search_paths());
167 ASSERT_EQ(kExpectedSystemPermittedPath, ns_system->permitted_paths());
168}
169
170TEST(linker_config, smoke) {
171 run_linker_config_smoke_test(false);
172}
173
174TEST(linker_config, asan_smoke) {
175 run_linker_config_smoke_test(true);
176}