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