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 class Namespace {
31  public:
32   explicit Namespace(std::string name, bool is_isolated = false,
33                      bool is_visible = false)
is_isolated_(is_isolated)34       : is_isolated_(is_isolated),
35         is_visible_(is_visible),
36         name_(std::move(name)) {
37   }
38 
39   Namespace(const Namespace& ns) = delete;
40   Namespace(Namespace&& ns) = default;
41   Namespace& operator=(Namespace&& ns) = default;
42 
43   // Add path to search path
44   void AddSearchPath(const std::string& path);
45 
46   // Add path to permitted path
47   void AddPermittedPath(const std::string& path);
48 
49   // Returns a link from this namespace to the given one. If one already exists
50   // it is returned, otherwise one is created and pushed back to tail.
51   Link& GetLink(const std::string& target_namespace);
52 
53   void WriteConfig(ConfigWriter& writer);
54   void AddWhitelisted(const std::string& path);
55 
56   std::string GetName() const;
57 
SetVisible(bool visible)58   void SetVisible(bool visible) {
59     is_visible_ = visible;
60   }
IsVisible()61   bool IsVisible() const {
62     return is_visible_;
63   }
64 
65   // For test usage
Links()66   const std::vector<Link>& Links() const {
67     return links_;
68   }
SearchPaths()69   std::vector<std::string> SearchPaths() const {
70     return search_paths_;
71   }
PermittedPaths()72   std::vector<std::string> PermittedPaths() const {
73     return permitted_paths_;
74   }
AsanSearchPaths()75   std::vector<std::string> AsanSearchPaths() const {
76     return asan_search_paths_;
77   }
AsanPermittedPaths()78   std::vector<std::string> AsanPermittedPaths() const {
79     return asan_permitted_paths_;
80   }
81 
82   template <typename Vec>
AddProvides(const Vec & list)83   void AddProvides(const Vec& list) {
84     provides_.insert(list.begin(), list.end());
85   }
86   template <typename Vec>
AddRequires(const Vec & list)87   void AddRequires(const Vec& list) {
88     requires_.insert(list.begin(), list.end());
89   }
GetProvides()90   const std::set<std::string>& GetProvides() const {
91     return provides_;
92   }
GetRequires()93   const std::set<std::string>& GetRequires() const {
94     return requires_;
95   }
96 
97  private:
98   bool is_isolated_;
99   bool is_visible_;
100   std::string name_;
101   std::vector<std::string> search_paths_;
102   std::vector<std::string> permitted_paths_;
103   std::vector<std::string> asan_search_paths_;
104   std::vector<std::string> asan_permitted_paths_;
105   std::vector<std::string> whitelisted_;
106   std::vector<Link> links_;
107   std::set<std::string> provides_;
108   std::set<std::string> requires_;
109 
110   void WritePathString(ConfigWriter& writer, const std::string& path_type,
111                        const std::vector<std::string>& path_list);
112   bool RequiresAsanPath(const std::string& path);
113   const std::string CreateAsanPath(const std::string& path);
114 };
115 
116 void InitializeWithApex(Namespace& ns, const ApexInfo& apex_info);
117 }  // namespace modules
118 }  // namespace linkerconfig
119 }  // namespace android
120