1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "linkerconfig/namespace.h"
18
19 #include "linkerconfig/log.h"
20
21 #define LOG_TAG "linkerconfig"
22
23 namespace android {
24 namespace linkerconfig {
25 namespace modules {
26
27 constexpr const char* kDataAsanPath = "/data/asan";
28
WritePathString(ConfigWriter & writer,const std::string & path_type,const std::vector<std::string> & path_list)29 void Namespace::WritePathString(ConfigWriter& writer,
30 const std::string& path_type,
31 const std::vector<std::string>& path_list) {
32 std::string prefix = path_type + ".paths ";
33 bool is_first = true;
34 for (auto& path : path_list) {
35 writer.WriteLine(prefix + (is_first ? "= " : "+= ") + path);
36 is_first = false;
37 }
38 }
39
CreateLink(const std::string & target_namespace,bool allow_all_shared_libs)40 std::shared_ptr<Link> Namespace::CreateLink(const std::string& target_namespace,
41 bool allow_all_shared_libs) {
42 auto new_link =
43 std::make_shared<Link>(name_, target_namespace, allow_all_shared_libs);
44
45 if (links_.find(target_namespace) != links_.end()) {
46 LOG(INFO) << "Link to " << target_namespace
47 << " already exists. Overwriting link.";
48 }
49
50 links_[target_namespace] = new_link;
51 return new_link;
52 }
53
WriteConfig(ConfigWriter & writer)54 void Namespace::WriteConfig(ConfigWriter& writer) {
55 writer.SetPrefix("namespace." + name_ + ".");
56
57 writer.WriteLine("isolated = %s", is_isolated_ ? "true" : "false");
58
59 if (is_visible_) {
60 writer.WriteLine("visible = true");
61 }
62
63 WritePathString(writer, "search", search_paths_);
64 WritePathString(writer, "permitted", permitted_paths_);
65 WritePathString(writer, "asan.search", asan_search_paths_);
66 WritePathString(writer, "asan.permitted", asan_permitted_paths_);
67
68 if (!links_.empty()) {
69 std::string link_list = "";
70
71 bool is_first = true;
72 for (auto& link : links_) {
73 if (!is_first) {
74 link_list += ",";
75 }
76 link_list += link.first;
77 is_first = false;
78 }
79
80 writer.WriteLine("links = " + link_list);
81
82 for (auto& link : links_) {
83 link.second->WriteConfig(writer);
84 }
85 }
86
87 writer.ResetPrefix();
88 }
89
AddSearchPath(const std::string & path,bool in_asan,bool with_data_asan)90 void Namespace::AddSearchPath(const std::string& path, bool in_asan,
91 bool with_data_asan) {
92 search_paths_.push_back(path);
93
94 if (in_asan) {
95 asan_search_paths_.push_back(path);
96 if (with_data_asan) {
97 asan_search_paths_.push_back(kDataAsanPath + path);
98 }
99 }
100 }
101
AddPermittedPath(const std::string & path,bool in_asan,bool with_data_asan)102 void Namespace::AddPermittedPath(const std::string& path, bool in_asan,
103 bool with_data_asan) {
104 permitted_paths_.push_back(path);
105
106 if (in_asan) {
107 asan_permitted_paths_.push_back(path);
108 if (with_data_asan) {
109 asan_permitted_paths_.push_back(kDataAsanPath + path);
110 }
111 }
112 }
113
114 } // namespace modules
115 } // namespace linkerconfig
116 } // namespace android