blob: 64ab00f85852691d7ffdd0d5d2d1370d8a9c971a [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
39#include <android-base/stringprintf.h>
40#include <android-base/file.h>
41#include <android-base/test_utils.h>
42
43#include "private/ScopeGuard.h"
44
45
46static const char* config_str =
47 "# comment \n"
48 "dir.test = /data/local/tmp\n"
49 "\n"
50 "[test]\n"
51 "\n"
52 "enable.target.sdk.version = true\n"
53 "additional.namespaces=system\n"
54 "namespace.default.isolated = true\n"
55 "namespace.default.search.paths = /vendor/${LIB}\n"
56 "namespace.default.permitted.paths = /vendor/${LIB}\n"
57 "namespace.default.asan.search.paths = /data:/vendor/${LIB}\n"
58 "namespace.default.asan.permitted.paths = /data:/vendor\n"
59 "namespace.default.links = system\n"
60 "namespace.default.link.system.shared_libs = libc.so:libm.so:libdl.so:libstdc++.so\n"
61 "namespace.system.isolated = true\n"
62 "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
119 auto file_guard = make_scope_guard([&version_file] {
120 unlink(version_file.c_str());
121 });
122
123 ASSERT_TRUE(write_version(version_file, 113U)) << strerror(errno);
124
125 // read config
126 const Config* config = nullptr;
127 std::string error_msg;
128 ASSERT_TRUE(Config::read_binary_config(tmp_file.path,
129 executable_path.c_str(),
130 is_asan,
131 &config,
132 &error_msg)) << error_msg;
133 ASSERT_TRUE(config != nullptr);
134 ASSERT_TRUE(error_msg.empty());
135
136 ASSERT_EQ(113U, config->target_sdk_version());
137
138 const NamespaceConfig* default_ns_config = config->default_namespace_config();
139 ASSERT_TRUE(default_ns_config != nullptr);
140
141 ASSERT_TRUE(default_ns_config->isolated());
142 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());
168 ASSERT_EQ(kExpectedSystemSearchPath, ns_system->search_paths());
169 ASSERT_EQ(kExpectedSystemPermittedPath, ns_system->permitted_paths());
170}
171
172TEST(linker_config, smoke) {
173 run_linker_config_smoke_test(false);
174}
175
176TEST(linker_config, asan_smoke) {
177 run_linker_config_smoke_test(true);
178}