Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | #ifndef AAPT_PROCESS_SYMBOLTABLE_H |
| 18 | #define AAPT_PROCESS_SYMBOLTABLE_H |
| 19 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | #include <memory> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "android-base/macros.h" |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 25 | #include "androidfw/Asset.h" |
| 26 | #include "androidfw/AssetManager2.h" |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 27 | #include "utils/JenkinsHash.h" |
| 28 | #include "utils/LruCache.h" |
| 29 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | #include "Resource.h" |
| 31 | #include "ResourceTable.h" |
| 32 | #include "ResourceValues.h" |
| 33 | #include "util/Util.h" |
| 34 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 | namespace aapt { |
| 36 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 | inline android::hash_t hash_type(const ResourceName& name) { |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 38 | std::hash<std::string> str_hash; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 39 | android::hash_t hash = 0; |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 40 | hash = android::JenkinsHashMix(hash, (uint32_t)str_hash(name.package)); |
Iurii Makhno | cff10ce | 2022-02-15 19:33:50 +0000 | [diff] [blame] | 41 | hash = android::JenkinsHashMix(hash, (uint32_t)str_hash(name.type.name)); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 42 | hash = android::JenkinsHashMix(hash, (uint32_t)str_hash(name.entry)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | return hash; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | inline android::hash_t hash_type(const ResourceId& id) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 47 | return android::hash_type(id.id); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 50 | class ISymbolSource; |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 51 | class ISymbolTableDelegate; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 52 | class NameMangler; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 53 | |
| 54 | class SymbolTable { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | public: |
| 56 | struct Symbol { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 57 | Symbol() = default; |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 58 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 59 | explicit Symbol(const std::optional<ResourceId>& i, const std::shared_ptr<Attribute>& attr = {}, |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 60 | bool pub = false) |
| 61 | : id(i), attribute(attr), is_public(pub) { |
| 62 | } |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | Symbol(const Symbol&) = default; |
| 65 | Symbol(Symbol&&) = default; |
| 66 | Symbol& operator=(const Symbol&) = default; |
| 67 | Symbol& operator=(Symbol&&) = default; |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 68 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 69 | std::optional<ResourceId> id; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 70 | std::shared_ptr<Attribute> attribute; |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | bool is_public = false; |
Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 72 | bool is_dynamic = false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | }; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 74 | |
Chih-Hung Hsieh | 1fc78e1 | 2018-12-20 13:37:44 -0800 | [diff] [blame] | 75 | explicit SymbolTable(NameMangler* mangler); |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 76 | |
| 77 | // Overrides the default ISymbolTableDelegate, which allows a custom defined strategy for |
| 78 | // looking up resources from a set of sources. |
| 79 | void SetDelegate(std::unique_ptr<ISymbolTableDelegate> delegate); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 80 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 81 | // Appends a symbol source. The cache is not cleared since entries that |
| 82 | // have already been found would take precedence due to ordering. |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | void AppendSource(std::unique_ptr<ISymbolSource> source); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 84 | |
| 85 | // Prepends a symbol source so that its symbols take precedence. This will |
| 86 | // cause the existing cache to be cleared. |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | void PrependSource(std::unique_ptr<ISymbolSource> source); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 88 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 89 | // NOTE: Never hold on to the result between calls to FindByXXX. The |
| 90 | // results are stored in a cache which may evict entries on subsequent calls. |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 91 | const Symbol* FindByName(const ResourceName& name); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 92 | |
| 93 | // NOTE: Never hold on to the result between calls to FindByXXX. The |
| 94 | // results are stored in a cache which may evict entries on subsequent calls. |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | const Symbol* FindById(const ResourceId& id); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 96 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 97 | // Let's the ISymbolSource decide whether looking up by name or ID is faster, |
| 98 | // if both are available. |
| 99 | // NOTE: Never hold on to the result between calls to FindByXXX. The |
| 100 | // results are stored in a cache which may evict entries on subsequent calls. |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 101 | const Symbol* FindByReference(const Reference& ref); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 102 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | private: |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 104 | NameMangler* mangler_; |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 105 | std::unique_ptr<ISymbolTableDelegate> delegate_; |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 106 | std::vector<std::unique_ptr<ISymbolSource>> sources_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 107 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 108 | // We use shared_ptr because unique_ptr is not supported and |
| 109 | // we need automatic deletion. |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | android::LruCache<ResourceName, std::shared_ptr<Symbol>> cache_; |
| 111 | android::LruCache<ResourceId, std::shared_ptr<Symbol>> id_cache_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 112 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | DISALLOW_COPY_AND_ASSIGN(SymbolTable); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 116 | // Allows the customization of the lookup strategy/order of a symbol from a set of |
| 117 | // symbol sources. |
| 118 | class ISymbolTableDelegate { |
| 119 | public: |
| 120 | ISymbolTableDelegate() = default; |
| 121 | virtual ~ISymbolTableDelegate() = default; |
| 122 | |
| 123 | // The name is already mangled and does not need further processing. |
| 124 | virtual std::unique_ptr<SymbolTable::Symbol> FindByName( |
| 125 | const ResourceName& name, const std::vector<std::unique_ptr<ISymbolSource>>& sources) = 0; |
| 126 | |
| 127 | virtual std::unique_ptr<SymbolTable::Symbol> FindById( |
| 128 | ResourceId id, const std::vector<std::unique_ptr<ISymbolSource>>& sources) = 0; |
| 129 | |
| 130 | private: |
| 131 | DISALLOW_COPY_AND_ASSIGN(ISymbolTableDelegate); |
| 132 | }; |
| 133 | |
| 134 | class DefaultSymbolTableDelegate : public ISymbolTableDelegate { |
| 135 | public: |
| 136 | DefaultSymbolTableDelegate() = default; |
| 137 | virtual ~DefaultSymbolTableDelegate() = default; |
| 138 | |
| 139 | virtual std::unique_ptr<SymbolTable::Symbol> FindByName( |
| 140 | const ResourceName& name, |
| 141 | const std::vector<std::unique_ptr<ISymbolSource>>& sources) override; |
| 142 | virtual std::unique_ptr<SymbolTable::Symbol> FindById( |
| 143 | ResourceId id, const std::vector<std::unique_ptr<ISymbolSource>>& sources) override; |
| 144 | |
| 145 | private: |
| 146 | DISALLOW_COPY_AND_ASSIGN(DefaultSymbolTableDelegate); |
| 147 | }; |
| 148 | |
| 149 | // An interface that a symbol source implements in order to surface symbol information |
| 150 | // to the symbol table. |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 151 | class ISymbolSource { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 152 | public: |
| 153 | virtual ~ISymbolSource() = default; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 154 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 155 | virtual std::unique_ptr<SymbolTable::Symbol> FindByName( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 156 | const ResourceName& name) = 0; |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 157 | virtual std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) = 0; |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 158 | |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 159 | // Default implementation tries the name if it exists, else the ID. |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 160 | virtual std::unique_ptr<SymbolTable::Symbol> FindByReference( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 161 | const Reference& ref) { |
| 162 | if (ref.name) { |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 163 | return FindByName(ref.name.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 164 | } else if (ref.id) { |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 165 | return FindById(ref.id.value()); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 166 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 167 | return {}; |
| 168 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 169 | }; |
| 170 | |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 171 | // Exposes the resources in a ResourceTable as symbols for SymbolTable. |
| 172 | // Instances of this class must outlive the encompassed ResourceTable. |
| 173 | // Lookups by ID are ignored. |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 174 | class ResourceTableSymbolSource : public ISymbolSource { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 175 | public: |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 176 | explicit ResourceTableSymbolSource(ResourceTable* table) : table_(table) {} |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 177 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 178 | std::unique_ptr<SymbolTable::Symbol> FindByName( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | const ResourceName& name) override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 180 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 181 | std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 182 | return {}; |
| 183 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 184 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 185 | private: |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | ResourceTable* table_; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 187 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 188 | DISALLOW_COPY_AND_ASSIGN(ResourceTableSymbolSource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 191 | class AssetManagerSymbolSource : public ISymbolSource { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 192 | public: |
| 193 | AssetManagerSymbolSource() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 194 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 195 | bool AddAssetPath(android::StringPiece path); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 196 | std::map<size_t, std::string> GetAssignedPackageIds() const; |
Ryan Mitchell | ee4a564 | 2019-10-16 08:32:55 -0700 | [diff] [blame] | 197 | bool IsPackageDynamic(uint32_t packageId, const std::string& package_name) const; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 198 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 199 | std::unique_ptr<SymbolTable::Symbol> FindByName( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 200 | const ResourceName& name) override; |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 201 | std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) override; |
| 202 | std::unique_ptr<SymbolTable::Symbol> FindByReference( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | const Reference& ref) override; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 204 | |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 205 | android::AssetManager2* GetAssetManager() { |
| 206 | return &asset_manager_; |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 209 | private: |
Yurii Zubrytskyi | 1cf7493 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 210 | std::vector<android::AssetManager2::ApkAssetsPtr> apk_assets_; |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 211 | android::AssetManager2 asset_manager_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 212 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 213 | DISALLOW_COPY_AND_ASSIGN(AssetManagerSymbolSource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 214 | }; |
| 215 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 216 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 217 | |
| 218 | #endif /* AAPT_PROCESS_SYMBOLTABLE_H */ |