blob: 06eaf63ad4427dd854f719edaec99c45719b9110 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinskice5e56e2016-10-21 17:56:45 -070020#include <algorithm>
21#include <memory>
22#include <vector>
23
24#include "android-base/macros.h"
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080025#include "androidfw/Asset.h"
26#include "androidfw/AssetManager2.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027#include "utils/JenkinsHash.h"
28#include "utils/LruCache.h"
29
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "Resource.h"
31#include "ResourceTable.h"
32#include "ResourceValues.h"
33#include "util/Util.h"
34
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035namespace aapt {
36
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037inline android::hash_t hash_type(const ResourceName& name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 std::hash<std::string> str_hash;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 android::hash_t hash = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 hash = android::JenkinsHashMix(hash, (uint32_t)str_hash(name.package));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 hash = android::JenkinsHashMix(hash, (uint32_t)name.type);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 hash = android::JenkinsHashMix(hash, (uint32_t)str_hash(name.entry));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 return hash;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044}
45
46inline android::hash_t hash_type(const ResourceId& id) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 return android::hash_type(id.id);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048}
49
Adam Lesinski64587af2016-02-18 18:33:06 -080050class ISymbolSource;
Adam Lesinski1e4b0e52017-04-27 15:01:10 -070051class ISymbolTableDelegate;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080052class NameMangler;
Adam Lesinski64587af2016-02-18 18:33:06 -080053
54class SymbolTable {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 public:
56 struct Symbol {
Adam Lesinskic744ae82017-05-17 19:28:38 -070057 Symbol() = default;
Adam Lesinski626b3db2016-04-07 13:24:59 -070058
Adam Lesinskic744ae82017-05-17 19:28:38 -070059 explicit Symbol(const Maybe<ResourceId>& i, const std::shared_ptr<Attribute>& attr = {},
60 bool pub = false)
61 : id(i), attribute(attr), is_public(pub) {
62 }
Adam Lesinski626b3db2016-04-07 13:24:59 -070063
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 Symbol(const Symbol&) = default;
65 Symbol(Symbol&&) = default;
66 Symbol& operator=(const Symbol&) = default;
67 Symbol& operator=(Symbol&&) = default;
Adam Lesinski626b3db2016-04-07 13:24:59 -070068
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 Maybe<ResourceId> id;
70 std::shared_ptr<Attribute> attribute;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 bool is_public = false;
Todd Kennedy32512992018-04-25 16:45:59 -070072 bool is_dynamic = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 };
Adam Lesinski64587af2016-02-18 18:33:06 -080074
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -080075 explicit SymbolTable(NameMangler* mangler);
Adam Lesinski1e4b0e52017-04-27 15:01:10 -070076
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 Lesinski64587af2016-02-18 18:33:06 -080080
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080081 // Appends a symbol source. The cache is not cleared since entries that
82 // have already been found would take precedence due to ordering.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 void AppendSource(std::unique_ptr<ISymbolSource> source);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080084
85 // Prepends a symbol source so that its symbols take precedence. This will
86 // cause the existing cache to be cleared.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 void PrependSource(std::unique_ptr<ISymbolSource> source);
Adam Lesinski64587af2016-02-18 18:33:06 -080088
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080089 // 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 Lesinskice5e56e2016-10-21 17:56:45 -070091 const Symbol* FindByName(const ResourceName& name);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080092
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 Lesinskice5e56e2016-10-21 17:56:45 -070095 const Symbol* FindById(const ResourceId& id);
Adam Lesinski64587af2016-02-18 18:33:06 -080096
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080097 // 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 Lesinskice5e56e2016-10-21 17:56:45 -0700101 const Symbol* FindByReference(const Reference& ref);
Adam Lesinski76565542016-03-10 21:55:04 -0800102
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 private:
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800104 NameMangler* mangler_;
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700105 std::unique_ptr<ISymbolTableDelegate> delegate_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 std::vector<std::unique_ptr<ISymbolSource>> sources_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 // We use shared_ptr because unique_ptr is not supported and
109 // we need automatic deletion.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 android::LruCache<ResourceName, std::shared_ptr<Symbol>> cache_;
111 android::LruCache<ResourceId, std::shared_ptr<Symbol>> id_cache_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 DISALLOW_COPY_AND_ASSIGN(SymbolTable);
Adam Lesinski64587af2016-02-18 18:33:06 -0800114};
115
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700116// Allows the customization of the lookup strategy/order of a symbol from a set of
117// symbol sources.
118class 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
134class 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 Lesinski64587af2016-02-18 18:33:06 -0800151class ISymbolSource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 public:
153 virtual ~ISymbolSource() = default;
Adam Lesinski64587af2016-02-18 18:33:06 -0800154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 virtual std::unique_ptr<SymbolTable::Symbol> FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 const ResourceName& name) = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 virtual std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) = 0;
Adam Lesinski76565542016-03-10 21:55:04 -0800158
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700159 // Default implementation tries the name if it exists, else the ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 virtual std::unique_ptr<SymbolTable::Symbol> FindByReference(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 const Reference& ref) {
162 if (ref.name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 return FindByName(ref.name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 } else if (ref.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 return FindById(ref.id.value());
Adam Lesinski76565542016-03-10 21:55:04 -0800166 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 return {};
168 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800169};
170
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700171// 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 Lesinski64587af2016-02-18 18:33:06 -0800174class ResourceTableSymbolSource : public ISymbolSource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 explicit ResourceTableSymbolSource(ResourceTable* table) : table_(table) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700177
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 std::unique_ptr<SymbolTable::Symbol> FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 const ResourceName& name) override;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700180
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 return {};
183 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800184
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 ResourceTable* table_;
Adam Lesinski64587af2016-02-18 18:33:06 -0800187
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 DISALLOW_COPY_AND_ASSIGN(ResourceTableSymbolSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700189};
190
Adam Lesinski64587af2016-02-18 18:33:06 -0800191class AssetManagerSymbolSource : public ISymbolSource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 public:
193 AssetManagerSymbolSource() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700194
Adam Lesinskid5083f62017-01-16 15:07:21 -0800195 bool AddAssetPath(const android::StringPiece& path);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800196 std::map<size_t, std::string> GetAssignedPackageIds() const;
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700197 bool IsPackageDynamic(uint32_t packageId, const std::string& package_name) const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700198
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 std::unique_ptr<SymbolTable::Symbol> FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 const ResourceName& name) override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) override;
202 std::unique_ptr<SymbolTable::Symbol> FindByReference(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 const Reference& ref) override;
Adam Lesinski64587af2016-02-18 18:33:06 -0800204
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800205 android::AssetManager2* GetAssetManager() {
206 return &asset_manager_;
Adam Lesinskic6284372017-12-04 13:46:23 -0800207 }
208
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 private:
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800210 android::AssetManager2 asset_manager_;
211 std::vector<std::unique_ptr<const android::ApkAssets>> apk_assets_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700212
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 DISALLOW_COPY_AND_ASSIGN(AssetManagerSymbolSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700214};
215
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700217
218#endif /* AAPT_PROCESS_SYMBOLTABLE_H */