• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #include <android/api-level.h>
32 
33 #include <stdlib.h>
34 #include <limits.h>
35 
36 #include <memory>
37 #include <string>
38 #include <vector>
39 #include <unordered_map>
40 
41 #include <android-base/macros.h>
42 
43 #if defined(__LP64__)
44 static constexpr const char* kLibPath = "lib64";
45 #else
46 static constexpr const char* kLibPath = "lib";
47 #endif
48 
49 class NamespaceLinkConfig {
50  public:
51   NamespaceLinkConfig() = default;
NamespaceLinkConfig(const std::string & ns_name,const std::string & shared_libs,bool allow_all_shared_libs)52   NamespaceLinkConfig(const std::string& ns_name, const std::string& shared_libs,
53                       bool allow_all_shared_libs)
54       : ns_name_(ns_name), shared_libs_(shared_libs),
55         allow_all_shared_libs_(allow_all_shared_libs) {}
56 
ns_name()57   const std::string& ns_name() const {
58     return ns_name_;
59   }
60 
shared_libs()61   const std::string& shared_libs() const {
62     return shared_libs_;
63   }
64 
allow_all_shared_libs()65   bool allow_all_shared_libs() const {
66     return allow_all_shared_libs_;
67   }
68 
69  private:
70   std::string ns_name_;
71   std::string shared_libs_;
72   bool allow_all_shared_libs_;
73 };
74 
75 class NamespaceConfig {
76  public:
NamespaceConfig(const std::string & name)77   explicit NamespaceConfig(const std::string& name)
78       : name_(name), isolated_(false), visible_(false)
79   {}
80 
name()81   const char* name() const {
82     return name_.c_str();
83   }
84 
isolated()85   bool isolated() const {
86     return isolated_;
87   }
88 
visible()89   bool visible() const {
90     return visible_;
91   }
92 
search_paths()93   const std::vector<std::string>& search_paths() const {
94     return search_paths_;
95   }
96 
permitted_paths()97   const std::vector<std::string>& permitted_paths() const {
98     return permitted_paths_;
99   }
100 
whitelisted_libs()101   const std::vector<std::string>& whitelisted_libs() const {
102     return whitelisted_libs_;
103   }
104 
links()105   const std::vector<NamespaceLinkConfig>& links() const {
106     return namespace_links_;
107   }
108 
add_namespace_link(const std::string & ns_name,const std::string & shared_libs,bool allow_all_shared_libs)109   void add_namespace_link(const std::string& ns_name, const std::string& shared_libs,
110                           bool allow_all_shared_libs) {
111     namespace_links_.push_back(NamespaceLinkConfig(ns_name, shared_libs, allow_all_shared_libs));
112   }
113 
set_isolated(bool isolated)114   void set_isolated(bool isolated) {
115     isolated_ = isolated;
116   }
117 
set_visible(bool visible)118   void set_visible(bool visible) {
119     visible_ = visible;
120   }
121 
set_search_paths(std::vector<std::string> && search_paths)122   void set_search_paths(std::vector<std::string>&& search_paths) {
123     search_paths_ = std::move(search_paths);
124   }
125 
set_permitted_paths(std::vector<std::string> && permitted_paths)126   void set_permitted_paths(std::vector<std::string>&& permitted_paths) {
127     permitted_paths_ = std::move(permitted_paths);
128   }
129 
set_whitelisted_libs(std::vector<std::string> && whitelisted_libs)130   void set_whitelisted_libs(std::vector<std::string>&& whitelisted_libs) {
131     whitelisted_libs_ = std::move(whitelisted_libs);
132   }
133  private:
134   const std::string name_;
135   bool isolated_;
136   bool visible_;
137   std::vector<std::string> search_paths_;
138   std::vector<std::string> permitted_paths_;
139   std::vector<std::string> whitelisted_libs_;
140   std::vector<NamespaceLinkConfig> namespace_links_;
141 
142   DISALLOW_IMPLICIT_CONSTRUCTORS(NamespaceConfig);
143 };
144 
145 class Config {
146  public:
Config()147   Config() : target_sdk_version_(__ANDROID_API__) {}
148 
namespace_configs()149   const std::vector<std::unique_ptr<NamespaceConfig>>& namespace_configs() const {
150     return namespace_configs_;
151   }
152 
default_namespace_config()153   const NamespaceConfig* default_namespace_config() const {
154     auto it = namespace_configs_map_.find("default");
155     return it == namespace_configs_map_.end() ? nullptr : it->second;
156   }
157 
target_sdk_version()158   int target_sdk_version() const {
159     return target_sdk_version_;
160   }
161 
162   // note that this is one time event and therefore there is no need to
163   // read every section of the config. Every linker instance needs at
164   // most one configuration.
165   // Returns false in case of an error. If binary config was not found
166   // sets *config = nullptr.
167   static bool read_binary_config(const char* ld_config_file_path,
168                                  const char* binary_realpath,
169                                  bool is_asan,
170                                  const Config** config,
171                                  std::string* error_msg);
172 
173   static std::string get_vndk_version_string(const char delimiter);
174  private:
175   void clear();
176 
set_target_sdk_version(int target_sdk_version)177   void set_target_sdk_version(int target_sdk_version) {
178     target_sdk_version_ = target_sdk_version;
179   }
180 
181   NamespaceConfig* create_namespace_config(const std::string& name);
182 
183   std::vector<std::unique_ptr<NamespaceConfig>> namespace_configs_;
184   std::unordered_map<std::string, NamespaceConfig*> namespace_configs_map_;
185   int target_sdk_version_;
186 
187   DISALLOW_COPY_AND_ASSIGN(Config);
188 };
189