| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 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 |  | 
| Elliott Hughes | cbc80ba | 2018-02-13 14:26:29 -0800 | [diff] [blame] | 29 | #pragma once | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 30 |  | 
|  | 31 | #include <android/api-level.h> | 
|  | 32 |  | 
|  | 33 | #include <stdlib.h> | 
|  | 34 | #include <limits.h> | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 35 |  | 
|  | 36 | #include <memory> | 
|  | 37 | #include <string> | 
|  | 38 | #include <vector> | 
|  | 39 | #include <unordered_map> | 
|  | 40 |  | 
| Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 41 | #include <android-base/macros.h> | 
|  | 42 |  | 
| Ryan Prichard | 4d4087d | 2019-12-02 16:55:48 -0800 | [diff] [blame] | 43 | #if defined(__LP64__) | 
|  | 44 | static constexpr const char* kLibPath = "lib64"; | 
|  | 45 | #else | 
|  | 46 | static constexpr const char* kLibPath = "lib"; | 
|  | 47 | #endif | 
|  | 48 |  | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 49 | class NamespaceLinkConfig { | 
|  | 50 | public: | 
|  | 51 | NamespaceLinkConfig() = default; | 
| Logan Chien | 9ee4591 | 2018-01-18 12:05:09 +0800 | [diff] [blame] | 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) {} | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 56 |  | 
|  | 57 | const std::string& ns_name() const { | 
|  | 58 | return ns_name_; | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | const std::string& shared_libs() const { | 
|  | 62 | return shared_libs_; | 
|  | 63 | } | 
|  | 64 |  | 
| Logan Chien | 9ee4591 | 2018-01-18 12:05:09 +0800 | [diff] [blame] | 65 | bool allow_all_shared_libs() const { | 
|  | 66 | return allow_all_shared_libs_; | 
|  | 67 | } | 
|  | 68 |  | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 69 | private: | 
|  | 70 | std::string ns_name_; | 
|  | 71 | std::string shared_libs_; | 
| Logan Chien | 9ee4591 | 2018-01-18 12:05:09 +0800 | [diff] [blame] | 72 | bool allow_all_shared_libs_; | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 73 | }; | 
|  | 74 |  | 
|  | 75 | class NamespaceConfig { | 
|  | 76 | public: | 
|  | 77 | explicit NamespaceConfig(const std::string& name) | 
| Jiyong Park | d7c4832 | 2017-04-03 23:10:37 +0900 | [diff] [blame] | 78 | : name_(name), isolated_(false), visible_(false) | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 79 | {} | 
|  | 80 |  | 
|  | 81 | const char* name() const { | 
|  | 82 | return name_.c_str(); | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | bool isolated() const { | 
|  | 86 | return isolated_; | 
|  | 87 | } | 
|  | 88 |  | 
| Jiyong Park | d7c4832 | 2017-04-03 23:10:37 +0900 | [diff] [blame] | 89 | bool visible() const { | 
|  | 90 | return visible_; | 
|  | 91 | } | 
|  | 92 |  | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 93 | const std::vector<std::string>& search_paths() const { | 
|  | 94 | return search_paths_; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | const std::vector<std::string>& permitted_paths() const { | 
|  | 98 | return permitted_paths_; | 
|  | 99 | } | 
|  | 100 |  | 
| Luke Huang | 30f2f05 | 2020-07-30 15:09:18 +0800 | [diff] [blame] | 101 | const std::vector<std::string>& allowed_libs() const { return allowed_libs_; } | 
| Vic Yang | 2d020e4 | 2019-01-12 21:03:25 -0800 | [diff] [blame] | 102 |  | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 103 | const std::vector<NamespaceLinkConfig>& links() const { | 
|  | 104 | return namespace_links_; | 
|  | 105 | } | 
|  | 106 |  | 
| Logan Chien | 9ee4591 | 2018-01-18 12:05:09 +0800 | [diff] [blame] | 107 | void add_namespace_link(const std::string& ns_name, const std::string& shared_libs, | 
|  | 108 | bool allow_all_shared_libs) { | 
|  | 109 | namespace_links_.push_back(NamespaceLinkConfig(ns_name, shared_libs, allow_all_shared_libs)); | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
|  | 112 | void set_isolated(bool isolated) { | 
|  | 113 | isolated_ = isolated; | 
|  | 114 | } | 
|  | 115 |  | 
| Jiyong Park | d7c4832 | 2017-04-03 23:10:37 +0900 | [diff] [blame] | 116 | void set_visible(bool visible) { | 
|  | 117 | visible_ = visible; | 
|  | 118 | } | 
|  | 119 |  | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 120 | void set_search_paths(std::vector<std::string>&& search_paths) { | 
| Vic Yang | 976d4b4 | 2019-03-12 13:38:30 -0700 | [diff] [blame] | 121 | search_paths_ = std::move(search_paths); | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 122 | } | 
|  | 123 |  | 
|  | 124 | void set_permitted_paths(std::vector<std::string>&& permitted_paths) { | 
| Vic Yang | 976d4b4 | 2019-03-12 13:38:30 -0700 | [diff] [blame] | 125 | permitted_paths_ = std::move(permitted_paths); | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 126 | } | 
| Vic Yang | 2d020e4 | 2019-01-12 21:03:25 -0800 | [diff] [blame] | 127 |  | 
| Luke Huang | 30f2f05 | 2020-07-30 15:09:18 +0800 | [diff] [blame] | 128 | void set_allowed_libs(std::vector<std::string>&& allowed_libs) { | 
|  | 129 | allowed_libs_ = std::move(allowed_libs); | 
| Vic Yang | 2d020e4 | 2019-01-12 21:03:25 -0800 | [diff] [blame] | 130 | } | 
| Luke Huang | 30f2f05 | 2020-07-30 15:09:18 +0800 | [diff] [blame] | 131 |  | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 132 | private: | 
|  | 133 | const std::string name_; | 
|  | 134 | bool isolated_; | 
| Jiyong Park | d7c4832 | 2017-04-03 23:10:37 +0900 | [diff] [blame] | 135 | bool visible_; | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 136 | std::vector<std::string> search_paths_; | 
|  | 137 | std::vector<std::string> permitted_paths_; | 
| Luke Huang | 30f2f05 | 2020-07-30 15:09:18 +0800 | [diff] [blame] | 138 | std::vector<std::string> allowed_libs_; | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 139 | std::vector<NamespaceLinkConfig> namespace_links_; | 
|  | 140 |  | 
|  | 141 | DISALLOW_IMPLICIT_CONSTRUCTORS(NamespaceConfig); | 
|  | 142 | }; | 
|  | 143 |  | 
|  | 144 | class Config { | 
|  | 145 | public: | 
|  | 146 | Config() : target_sdk_version_(__ANDROID_API__) {} | 
|  | 147 |  | 
|  | 148 | const std::vector<std::unique_ptr<NamespaceConfig>>& namespace_configs() const { | 
|  | 149 | return namespace_configs_; | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | const NamespaceConfig* default_namespace_config() const { | 
|  | 153 | auto it = namespace_configs_map_.find("default"); | 
|  | 154 | return it == namespace_configs_map_.end() ? nullptr : it->second; | 
|  | 155 | } | 
|  | 156 |  | 
| Elliott Hughes | ff1428a | 2018-11-12 16:01:37 -0800 | [diff] [blame] | 157 | int target_sdk_version() const { | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 158 | return target_sdk_version_; | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | // note that this is one time event and therefore there is no need to | 
|  | 162 | // read every section of the config. Every linker instance needs at | 
|  | 163 | // most one configuration. | 
|  | 164 | // Returns false in case of an error. If binary config was not found | 
|  | 165 | // sets *config = nullptr. | 
|  | 166 | static bool read_binary_config(const char* ld_config_file_path, | 
|  | 167 | const char* binary_realpath, | 
|  | 168 | bool is_asan, | 
|  | 169 | const Config** config, | 
|  | 170 | std::string* error_msg); | 
| Justin Yun | 53ce742 | 2017-11-27 16:28:07 +0900 | [diff] [blame] | 171 |  | 
| Jooyung Han | a365ac1 | 2019-10-16 22:37:10 +0000 | [diff] [blame] | 172 | static std::string get_vndk_version_string(const char delimiter); | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 173 | private: | 
|  | 174 | void clear(); | 
|  | 175 |  | 
| Elliott Hughes | ff1428a | 2018-11-12 16:01:37 -0800 | [diff] [blame] | 176 | void set_target_sdk_version(int target_sdk_version) { | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 177 | target_sdk_version_ = target_sdk_version; | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | NamespaceConfig* create_namespace_config(const std::string& name); | 
|  | 181 |  | 
|  | 182 | std::vector<std::unique_ptr<NamespaceConfig>> namespace_configs_; | 
|  | 183 | std::unordered_map<std::string, NamespaceConfig*> namespace_configs_map_; | 
| Elliott Hughes | ff1428a | 2018-11-12 16:01:37 -0800 | [diff] [blame] | 184 | int target_sdk_version_; | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 185 |  | 
|  | 186 | DISALLOW_COPY_AND_ASSIGN(Config); | 
|  | 187 | }; |