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 #pragma once 17 18 #include <set> 19 #include <string> 20 #include <vector> 21 22 #include "linkerconfig/apex.h" 23 #include "linkerconfig/configwriter.h" 24 #include "linkerconfig/link.h" 25 26 namespace android { 27 namespace linkerconfig { 28 namespace modules { 29 30 /** 31 * Explains if the path should be also added for ASAN 32 * 33 * NONE : the path should not be added for ASAN 34 * SAME_PATH : the path should be added for ASAN 35 * WITH_DATA_ASAN : the path and /data/asan/<path> should be added for ASAN 36 */ 37 enum class AsanPath { 38 NONE, 39 SAME_PATH, 40 WITH_DATA_ASAN, 41 }; 42 43 class Namespace { 44 public: 45 explicit Namespace(std::string name, bool is_isolated = false, 46 bool is_visible = false) is_isolated_(is_isolated)47 : is_isolated_(is_isolated), 48 is_visible_(is_visible), 49 name_(std::move(name)) { 50 } 51 52 Namespace(const Namespace& ns) = delete; 53 Namespace(Namespace&& ns) = default; 54 Namespace& operator=(Namespace&& ns) = default; 55 56 // Add path to search path 57 // This function will add path to namespace.<<namespace>>.search.paths 58 // If path_from_asan is SAME_PATH, this will add path also to 59 // namespace.<<namespace>>.asan.search.paths 60 // If path_from_asan is WITH_DATA_ASAN, 61 // this will also add asan path starts with /data/asan 62 // 63 // AddSearchPath("/system/${LIB}", AsanPath::NONE) : 64 // namespace.xxx.search.paths += /system/${LIB} 65 // AddSearchPath("/system/${LIB}", AsanPath::SAME_PATH) : 66 // namespace.xxx.search.paths += /system/${LIB} 67 // namespace.xxx.asan.search.paths += /system/${LIB} 68 // AddSearchPath("/system/${LIB}", AsanPath::WITH_DATA_ASAN) : 69 // namespace.xxx.search.paths += /system/${LIB} 70 // namespace.xxx.asan.search.paths += /data/asan/system/${LIB} 71 // namespace.xxx.asan.search.paths += /system/${LIB} 72 void AddSearchPath(const std::string& path, 73 AsanPath path_from_asan = AsanPath::SAME_PATH); 74 75 // Add path to permitted path 76 // This function will add path to namespace.<<namespace>>.permitted.paths 77 // If path_from_asan is SAME_PATH, this will add path also to 78 // namespace.<<namespace>>.asan.permitted.paths 79 // If path_from_asan is WITH_DATA_ASAN, 80 // this will also add asan path starts with /data/asan 81 // 82 // AddSearchPath("/system/${LIB}", AsanPath::NONE) : 83 // namespace.xxx.permitted.paths += /system/${LIB} 84 // AddSearchPath("/system/${LIB}", AsanPath::SAME_PATH) : 85 // namespace.xxx.permitted.paths += /system/${LIB} 86 // namespace.xxx.asan.permitted.paths += /system/${LIB} 87 // AddSearchPath("/system/${LIB}", AsanPath::WITH_DATA_ASAN) : 88 // namespace.xxx.permitted.paths += /system/${LIB} 89 // namespace.xxx.asan.permitted.paths += /data/asan/system/${LIB} 90 // namespace.xxx.asan.permitted.paths += /system/${LIB} 91 void AddPermittedPath(const std::string& path, 92 AsanPath path_from_asan = AsanPath::SAME_PATH); 93 94 // Returns a link from this namespace to the given one. If one already exists 95 // it is returned, otherwise one is created and pushed back to tail. 96 Link& GetLink(const std::string& target_namespace); 97 98 void WriteConfig(ConfigWriter& writer); 99 void AddWhitelisted(const std::string& path); 100 101 std::string GetName() const; 102 SetVisible(bool visible)103 void SetVisible(bool visible) { 104 is_visible_ = visible; 105 } 106 107 // For test usage Links()108 const std::vector<Link>& Links() const { 109 return links_; 110 } SearchPaths()111 std::vector<std::string> SearchPaths() const { 112 return search_paths_; 113 } 114 bool ContainsSearchPath(const std::string& path, AsanPath path_from_asan); 115 bool ContainsPermittedPath(const std::string& path, AsanPath path_from_asan); 116 117 template <typename Vec> AddProvides(const Vec & list)118 void AddProvides(const Vec& list) { 119 provides_.insert(list.begin(), list.end()); 120 } 121 template <typename Vec> AddRequires(const Vec & list)122 void AddRequires(const Vec& list) { 123 requires_.insert(list.begin(), list.end()); 124 } GetProvides()125 const std::set<std::string>& GetProvides() const { 126 return provides_; 127 } GetRequires()128 const std::set<std::string>& GetRequires() const { 129 return requires_; 130 } 131 132 private: 133 bool is_isolated_; 134 bool is_visible_; 135 std::string name_; 136 std::vector<std::string> search_paths_; 137 std::vector<std::string> permitted_paths_; 138 std::vector<std::string> asan_search_paths_; 139 std::vector<std::string> asan_permitted_paths_; 140 std::vector<std::string> whitelisted_; 141 std::vector<Link> links_; 142 std::set<std::string> provides_; 143 std::set<std::string> requires_; 144 145 void WritePathString(ConfigWriter& writer, const std::string& path_type, 146 const std::vector<std::string>& path_list); 147 }; 148 149 void InitializeWithApex(Namespace& ns, const ApexInfo& apex_info); 150 } // namespace modules 151 } // namespace linkerconfig 152 } // namespace android 153