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