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