blob: 5e5111369d91c01ce5f1cadec4c346e85fc8a6e9 [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"
Jiyong Park01de74e2017-04-03 23:10:37 +090061 "namespace.system.visible = true\n"
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080062 "namespace.system.search.paths = /system/${LIB}\n"
63 "namespace.system.permitted.paths = /system/${LIB}\n"
64 "namespace.system.asan.search.paths = /data:/system/${LIB}\n"
65 "namespace.system.asan.permitted.paths = /data:/system\n"
66 "\n";
67
68static bool write_version(const std::string& path, uint32_t version) {
69 std::string content = android::base::StringPrintf("%d", version);
70 return android::base::WriteStringToFile(content, path);
71}
72
73static void run_linker_config_smoke_test(bool is_asan) {
74#if defined(__LP64__)
75 const std::vector<std::string> kExpectedDefaultSearchPath = is_asan ?
76 std::vector<std::string>({ "/data", "/vendor/lib64"}) :
77 std::vector<std::string>({ "/vendor/lib64" });
78
79 const std::vector<std::string> kExpectedDefaultPermittedPath = is_asan ?
80 std::vector<std::string>({ "/data", "/vendor" }) :
81 std::vector<std::string>({ "/vendor/lib64" });
82
83 const std::vector<std::string> kExpectedSystemSearchPath = is_asan ?
84 std::vector<std::string>({ "/data", "/system/lib64" }) :
85 std::vector<std::string>({ "/system/lib64" });
86
87 const std::vector<std::string> kExpectedSystemPermittedPath = is_asan ?
88 std::vector<std::string>({ "/data", "/system" }) :
89 std::vector<std::string>({ "/system/lib64" });
90#else
91 const std::vector<std::string> kExpectedDefaultSearchPath = is_asan ?
92 std::vector<std::string>({ "/data", "/vendor/lib"}) :
93 std::vector<std::string>({ "/vendor/lib" });
94
95 const std::vector<std::string> kExpectedDefaultPermittedPath = is_asan ?
96 std::vector<std::string>({ "/data", "/vendor" }) :
97 std::vector<std::string>({ "/vendor/lib" });
98
99 const std::vector<std::string> kExpectedSystemSearchPath = is_asan ?
100 std::vector<std::string>({ "/data", "/system/lib" }) :
101 std::vector<std::string>({ "/system/lib" });
102
103 const std::vector<std::string> kExpectedSystemPermittedPath = is_asan ?
104 std::vector<std::string>({ "/data", "/system" }) :
105 std::vector<std::string>({ "/system/lib" });
106#endif
107
108 TemporaryFile tmp_file;
109 close(tmp_file.fd);
110 tmp_file.fd = -1;
111
112 android::base::WriteStringToFile(config_str, tmp_file.path);
113
114 TemporaryDir tmp_dir;
115
116 std::string executable_path = std::string(tmp_dir.path) + "/some-binary";
117 std::string version_file = std::string(tmp_dir.path) + "/.version";
118
Tom Cherryb8ab6182017-04-05 16:20:29 -0700119 auto file_guard =
120 android::base::make_scope_guard([&version_file] { unlink(version_file.c_str()); });
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800121
122 ASSERT_TRUE(write_version(version_file, 113U)) << strerror(errno);
123
124 // read config
125 const Config* config = nullptr;
126 std::string error_msg;
127 ASSERT_TRUE(Config::read_binary_config(tmp_file.path,
128 executable_path.c_str(),
129 is_asan,
130 &config,
131 &error_msg)) << error_msg;
132 ASSERT_TRUE(config != nullptr);
133 ASSERT_TRUE(error_msg.empty());
134
135 ASSERT_EQ(113U, config->target_sdk_version());
136
137 const NamespaceConfig* default_ns_config = config->default_namespace_config();
138 ASSERT_TRUE(default_ns_config != nullptr);
139
140 ASSERT_TRUE(default_ns_config->isolated());
Jiyong Park01de74e2017-04-03 23:10:37 +0900141 ASSERT_FALSE(default_ns_config->visible());
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800142 ASSERT_EQ(kExpectedDefaultSearchPath, default_ns_config->search_paths());
143 ASSERT_EQ(kExpectedDefaultPermittedPath, default_ns_config->permitted_paths());
144
145 const auto& default_ns_links = default_ns_config->links();
146 ASSERT_EQ(1U, default_ns_links.size());
147 ASSERT_EQ("system", default_ns_links[0].ns_name());
148 ASSERT_EQ("libc.so:libm.so:libdl.so:libstdc++.so", default_ns_links[0].shared_libs());
149
150 auto& ns_configs = config->namespace_configs();
151 ASSERT_EQ(2U, ns_configs.size());
152
153 // find second namespace
154 const NamespaceConfig* ns_system = nullptr;
155 for (auto& ns : ns_configs) {
156 std::string ns_name = ns->name();
157 ASSERT_TRUE(ns_name == "system" || ns_name == "default")
158 << "unexpected ns name: " << ns->name();
159
160 if (ns_name == "system") {
161 ns_system = ns.get();
162 }
163 }
164
165 ASSERT_TRUE(ns_system != nullptr) << "system namespace was not found";
166
167 ASSERT_TRUE(ns_system->isolated());
Jiyong Park01de74e2017-04-03 23:10:37 +0900168 ASSERT_TRUE(ns_system->visible());
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800169 ASSERT_EQ(kExpectedSystemSearchPath, ns_system->search_paths());
170 ASSERT_EQ(kExpectedSystemPermittedPath, ns_system->permitted_paths());
171}
172
173TEST(linker_config, smoke) {
174 run_linker_config_smoke_test(false);
175}
176
177TEST(linker_config, asan_smoke) {
178 run_linker_config_smoke_test(true);
179}