• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #include "../linker_utils.h"
37 
38 #include <unistd.h>
39 
40 #include <android-base/scopeguard.h>
41 #include <android-base/stringprintf.h>
42 #include <android-base/file.h>
43 #include <android-base/test_utils.h>
44 
45 #if defined(__LP64__)
46 #define ARCH_SUFFIX "64"
47 #else
48 #define ARCH_SUFFIX ""
49 #endif
50 
51 static const char* config_str =
52   "# comment \n"
53   "dir.test = /data/local/tmp\n"
54   "\n"
55   "[test]\n"
56   "\n"
57   "enable.target.sdk.version = true\n"
58   "additional.namespaces=system\n"
59   "namespace.default.isolated = true\n"
60   "namespace.default.search.paths = /vendor/${LIB}\n"
61   "namespace.default.permitted.paths = /vendor/${LIB}\n"
62   "namespace.default.asan.search.paths = /data:/vendor/${LIB}\n"
63   "namespace.default.asan.permitted.paths = /data:/vendor\n"
64   "namespace.default.links = system\n"
65   "namespace.default.link.system.shared_libs = libc.so:libm.so:libdl.so:libstdc++.so\n"
66   "namespace.system.isolated = true\n"
67   "namespace.system.visible = true\n"
68   "namespace.system.search.paths = /system/${LIB}\n"
69   "namespace.system.permitted.paths = /system/${LIB}\n"
70   "namespace.system.asan.search.paths = /data:/system/${LIB}\n"
71   "namespace.system.asan.permitted.paths = /data:/system\n"
72   "\n";
73 
write_version(const std::string & path,uint32_t version)74 static bool write_version(const std::string& path, uint32_t version) {
75   std::string content = android::base::StringPrintf("%d", version);
76   return android::base::WriteStringToFile(content, path);
77 }
78 
resolve_paths(std::vector<std::string> paths)79 static std::vector<std::string> resolve_paths(std::vector<std::string> paths) {
80   std::vector<std::string> resolved_paths;
81   resolve_paths(paths, &resolved_paths);
82   return resolved_paths;
83 }
84 
run_linker_config_smoke_test(bool is_asan)85 static void run_linker_config_smoke_test(bool is_asan) {
86   const std::vector<std::string> kExpectedDefaultSearchPath =
87       resolve_paths(is_asan ? std::vector<std::string>({ "/data", "/vendor/lib" ARCH_SUFFIX }) :
88                               std::vector<std::string>({ "/vendor/lib" ARCH_SUFFIX }));
89 
90   const std::vector<std::string> kExpectedDefaultPermittedPath =
91       resolve_paths(is_asan ? std::vector<std::string>({ "/data", "/vendor" }) :
92                               std::vector<std::string>({ "/vendor/lib" ARCH_SUFFIX }));
93 
94   const std::vector<std::string> kExpectedSystemSearchPath =
95       resolve_paths(is_asan ? std::vector<std::string>({ "/data", "/system/lib" ARCH_SUFFIX }) :
96                               std::vector<std::string>({ "/system/lib" ARCH_SUFFIX }));
97 
98   const std::vector<std::string> kExpectedSystemPermittedPath =
99       resolve_paths(is_asan ? std::vector<std::string>({ "/data", "/system" }) :
100                               std::vector<std::string>({ "/system/lib" ARCH_SUFFIX }));
101 
102   TemporaryFile tmp_file;
103   close(tmp_file.fd);
104   tmp_file.fd = -1;
105 
106   android::base::WriteStringToFile(config_str, tmp_file.path);
107 
108   TemporaryDir tmp_dir;
109 
110   std::string executable_path = std::string(tmp_dir.path) + "/some-binary";
111   std::string version_file = std::string(tmp_dir.path) + "/.version";
112 
113   auto file_guard =
114       android::base::make_scope_guard([&version_file] { unlink(version_file.c_str()); });
115 
116   ASSERT_TRUE(write_version(version_file, 113U)) << strerror(errno);
117 
118   // read config
119   const Config* config = nullptr;
120   std::string error_msg;
121   ASSERT_TRUE(Config::read_binary_config(tmp_file.path,
122                                          executable_path.c_str(),
123                                          is_asan,
124                                          &config,
125                                          &error_msg)) << error_msg;
126   ASSERT_TRUE(config != nullptr);
127   ASSERT_TRUE(error_msg.empty());
128 
129   ASSERT_EQ(113U, config->target_sdk_version());
130 
131   const NamespaceConfig* default_ns_config = config->default_namespace_config();
132   ASSERT_TRUE(default_ns_config != nullptr);
133 
134   ASSERT_TRUE(default_ns_config->isolated());
135   ASSERT_FALSE(default_ns_config->visible());
136   ASSERT_EQ(kExpectedDefaultSearchPath, default_ns_config->search_paths());
137   ASSERT_EQ(kExpectedDefaultPermittedPath, default_ns_config->permitted_paths());
138 
139   const auto& default_ns_links = default_ns_config->links();
140   ASSERT_EQ(1U, default_ns_links.size());
141   ASSERT_EQ("system", default_ns_links[0].ns_name());
142   ASSERT_EQ("libc.so:libm.so:libdl.so:libstdc++.so", default_ns_links[0].shared_libs());
143 
144   auto& ns_configs = config->namespace_configs();
145   ASSERT_EQ(2U, ns_configs.size());
146 
147   // find second namespace
148   const NamespaceConfig* ns_system = nullptr;
149   for (auto& ns : ns_configs) {
150     std::string ns_name = ns->name();
151     ASSERT_TRUE(ns_name == "system" || ns_name == "default")
152         << "unexpected ns name: " << ns->name();
153 
154     if (ns_name == "system") {
155       ns_system = ns.get();
156     }
157   }
158 
159   ASSERT_TRUE(ns_system != nullptr) << "system namespace was not found";
160 
161   ASSERT_TRUE(ns_system->isolated());
162   ASSERT_TRUE(ns_system->visible());
163   ASSERT_EQ(kExpectedSystemSearchPath, ns_system->search_paths());
164   ASSERT_EQ(kExpectedSystemPermittedPath, ns_system->permitted_paths());
165 }
166 
TEST(linker_config,smoke)167 TEST(linker_config, smoke) {
168   run_linker_config_smoke_test(false);
169 }
170 
TEST(linker_config,asan_smoke)171 TEST(linker_config, asan_smoke) {
172   run_linker_config_smoke_test(true);
173 }
174