blob: 1a648bf080b5562861b8a6aee0a55a4b746ff58c [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
Adam Lesinskicacb28f2016-10-19 12:18:14 -070017#include "process/SymbolTable.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080019#include <iostream>
20
21#include "android-base/logging.h"
22#include "android-base/stringprintf.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023#include "androidfw/AssetManager.h"
24#include "androidfw/ResourceTypes.h"
25
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "ConfigDescription.h"
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080027#include "NameMangler.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "Resource.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070029#include "ResourceUtils.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070030#include "ValueVisitor.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070031#include "util/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032
Adam Lesinskid5083f62017-01-16 15:07:21 -080033using android::StringPiece;
34
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035namespace aapt {
36
Adam Lesinski1e4b0e52017-04-27 15:01:10 -070037SymbolTable::SymbolTable(NameMangler* mangler)
38 : mangler_(mangler),
39 delegate_(util::make_unique<DefaultSymbolTableDelegate>()),
40 cache_(200),
41 id_cache_(200) {
42}
43
44void SymbolTable::SetDelegate(std::unique_ptr<ISymbolTableDelegate> delegate) {
45 CHECK(delegate != nullptr) << "can't set a nullptr delegate";
46 delegate_ = std::move(delegate);
47
48 // Clear the cache in case this delegate changes the order of lookup.
49 cache_.clear();
50}
51
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052void SymbolTable::AppendSource(std::unique_ptr<ISymbolSource> source) {
53 sources_.push_back(std::move(source));
Adam Lesinski64587af2016-02-18 18:33:06 -080054
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 // We do not clear the cache, because sources earlier in the list take
56 // precedent.
Adam Lesinski64587af2016-02-18 18:33:06 -080057}
58
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059void SymbolTable::PrependSource(std::unique_ptr<ISymbolSource> source) {
60 sources_.insert(sources_.begin(), std::move(source));
Adam Lesinski64587af2016-02-18 18:33:06 -080061
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 // We must clear the cache in case we did a lookup before adding this
63 // resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 cache_.clear();
Adam Lesinski64587af2016-02-18 18:33:06 -080065}
66
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067const SymbolTable::Symbol* SymbolTable::FindByName(const ResourceName& name) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080068 const ResourceName* name_with_package = &name;
69
70 // Fill in the package name if necessary.
71 // If there is no package in `name`, we will need to copy the ResourceName
72 // and store it somewhere; we use the Maybe<> class to reserve storage.
73 Maybe<ResourceName> name_with_package_impl;
74 if (name.package.empty()) {
75 name_with_package_impl = ResourceName(mangler_->GetTargetPackageName(), name.type, name.entry);
76 name_with_package = &name_with_package_impl.value();
77 }
78
79 // We store the name unmangled in the cache, so look it up as-is.
80 if (const std::shared_ptr<Symbol>& s = cache_.get(*name_with_package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 return s.get();
82 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080084 // The name was not found in the cache. Mangle it (if necessary) and find it in our sources.
85 // Again, here we use a Maybe<> object to reserve storage if we need to mangle.
86 const ResourceName* mangled_name = name_with_package;
87 Maybe<ResourceName> mangled_name_impl;
88 if (mangler_->ShouldMangle(name_with_package->package)) {
89 mangled_name_impl = mangler_->MangleName(*name_with_package);
90 mangled_name = &mangled_name_impl.value();
91 }
92
Adam Lesinski1e4b0e52017-04-27 15:01:10 -070093 std::unique_ptr<Symbol> symbol = delegate_->FindByName(*mangled_name, sources_);
94 if (symbol == nullptr) {
95 return nullptr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 }
Adam Lesinski1e4b0e52017-04-27 15:01:10 -070097
98 // Take ownership of the symbol into a shared_ptr. We do this because
99 // LruCache doesn't support unique_ptr.
100 std::shared_ptr<Symbol> shared_symbol(std::move(symbol));
101
102 // Since we look in the cache with the unmangled, but package prefixed
103 // name, we must put the same name into the cache.
104 cache_.put(*name_with_package, shared_symbol);
105
106 if (shared_symbol->id) {
107 // The symbol has an ID, so we can also cache this!
108 id_cache_.put(shared_symbol->id.value(), shared_symbol);
109 }
110
111 // Returns the raw pointer. Callers are not expected to hold on to this
112 // between calls to Find*.
113 return shared_symbol.get();
Adam Lesinski64587af2016-02-18 18:33:06 -0800114}
115
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116const SymbolTable::Symbol* SymbolTable::FindById(const ResourceId& id) {
117 if (const std::shared_ptr<Symbol>& s = id_cache_.get(id)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 return s.get();
119 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800120
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 // We did not find it in the cache, so look through the sources.
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700122 std::unique_ptr<Symbol> symbol = delegate_->FindById(id, sources_);
123 if (symbol == nullptr) {
124 return nullptr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 }
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700126
127 // Take ownership of the symbol into a shared_ptr. We do this because LruCache
128 // doesn't support unique_ptr.
129 std::shared_ptr<Symbol> shared_symbol(std::move(symbol));
130 id_cache_.put(id, shared_symbol);
131
132 // Returns the raw pointer. Callers are not expected to hold on to this
133 // between calls to Find*.
134 return shared_symbol.get();
Adam Lesinski64587af2016-02-18 18:33:06 -0800135}
136
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137const SymbolTable::Symbol* SymbolTable::FindByReference(const Reference& ref) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800138 // First try the ID. This is because when we lookup by ID, we only fill in the ID cache.
139 // Looking up by name fills in the name and ID cache. So a cache miss will cause a failed
140 // ID lookup, then a successful name lookup. Subsequent look ups will hit immediately
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 // because the ID is cached too.
142 //
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800143 // If we looked up by name first, a cache miss would mean we failed to lookup by name, then
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 // succeeded to lookup by ID. Subsequent lookups will miss then hit.
145 const SymbolTable::Symbol* symbol = nullptr;
146 if (ref.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 symbol = FindById(ref.id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 }
Adam Lesinski7656554f2016-03-10 21:55:04 -0800149
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 if (ref.name && !symbol) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 symbol = FindByName(ref.name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 }
153 return symbol;
Adam Lesinski7656554f2016-03-10 21:55:04 -0800154}
155
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700156std::unique_ptr<SymbolTable::Symbol> DefaultSymbolTableDelegate::FindByName(
157 const ResourceName& name, const std::vector<std::unique_ptr<ISymbolSource>>& sources) {
158 for (auto& source : sources) {
159 std::unique_ptr<SymbolTable::Symbol> symbol = source->FindByName(name);
160 if (symbol) {
161 return symbol;
162 }
163 }
164 return {};
165}
166
167std::unique_ptr<SymbolTable::Symbol> DefaultSymbolTableDelegate::FindById(
168 ResourceId id, const std::vector<std::unique_ptr<ISymbolSource>>& sources) {
169 for (auto& source : sources) {
170 std::unique_ptr<SymbolTable::Symbol> symbol = source->FindById(id);
171 if (symbol) {
172 return symbol;
173 }
174 }
175 return {};
176}
177
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178std::unique_ptr<SymbolTable::Symbol> ResourceTableSymbolSource::FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 const ResourceName& name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 Maybe<ResourceTable::SearchResult> result = table_->FindResource(name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 if (!result) {
182 if (name.type == ResourceType::kAttr) {
183 // Recurse and try looking up a private attribute.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800184 return FindByName(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 }
186 return {};
187 }
188
189 ResourceTable::SearchResult sr = result.value();
190
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800191 std::unique_ptr<SymbolTable::Symbol> symbol = util::make_unique<SymbolTable::Symbol>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 symbol->is_public = (sr.entry->symbol_status.state == SymbolState::kPublic);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193
194 if (sr.package->id && sr.type->id && sr.entry->id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800195 symbol->id = ResourceId(sr.package->id.value(), sr.type->id.value(), sr.entry->id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 }
197
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800198 if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 const ConfigDescription kDefaultConfig;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 ResourceConfigValue* config_value = sr.entry->FindValue(kDefaultConfig);
201 if (config_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 // This resource has an Attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700203 if (Attribute* attr = ValueCast<Attribute>(config_value->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 symbol->attribute = std::make_shared<Attribute>(*attr);
205 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700206 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700208 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 }
210 return symbol;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700211}
212
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213bool AssetManagerSymbolSource::AddAssetPath(const StringPiece& path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 int32_t cookie = 0;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800215 return assets_.addAssetPath(android::String8(path.data(), path.size()), &cookie);
216}
217
218std::map<size_t, std::string> AssetManagerSymbolSource::GetAssignedPackageIds() const {
219 std::map<size_t, std::string> package_map;
220 const android::ResTable& table = assets_.getResources(false);
221 const size_t package_count = table.getBasePackageCount();
222 for (size_t i = 0; i < package_count; i++) {
223 package_map[table.getBasePackageId(i)] =
224 util::Utf16ToUtf8(android::StringPiece16(table.getBasePackageName(i).string()));
225 }
226 return package_map;
Adam Lesinski64587af2016-02-18 18:33:06 -0800227}
228
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229static std::unique_ptr<SymbolTable::Symbol> LookupAttributeInTable(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 const android::ResTable& table, ResourceId id) {
231 // Try as a bag.
232 const android::ResTable::bag_entry* entry;
233 ssize_t count = table.lockBag(id.id, &entry);
234 if (count < 0) {
235 table.unlockBag(entry);
236 return nullptr;
237 }
238
239 // We found a resource.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800240 std::unique_ptr<SymbolTable::Symbol> s = util::make_unique<SymbolTable::Symbol>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 s->id = id;
242
243 // Check to see if it is an attribute.
244 for (size_t i = 0; i < (size_t)count; i++) {
245 if (entry[i].map.name.ident == android::ResTable_map::ATTR_TYPE) {
246 s->attribute = std::make_shared<Attribute>(false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 s->attribute->type_mask = entry[i].map.value.data;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700249 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700251
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 if (s->attribute) {
253 for (size_t i = 0; i < (size_t)count; i++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 const android::ResTable_map& map_entry = entry[i].map;
255 if (Res_INTERNALID(map_entry.name.ident)) {
256 switch (map_entry.name.ident) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 case android::ResTable_map::ATTR_MIN:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 s->attribute->min_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 break;
260 case android::ResTable_map::ATTR_MAX:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 s->attribute->max_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700262 break;
263 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 continue;
265 }
266
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 android::ResTable::resource_name entry_name;
268 if (!table.getResourceName(map_entry.name.ident, false, &entry_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 table.unlockBag(entry);
270 return nullptr;
271 }
272
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800273 Maybe<ResourceName> parsed_name = ResourceUtils::ToResourceName(entry_name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 if (!parsed_name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 return nullptr;
276 }
277
278 Attribute::Symbol symbol;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 symbol.symbol.name = parsed_name.value();
280 symbol.symbol.id = ResourceId(map_entry.name.ident);
281 symbol.value = map_entry.value.data;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 s->attribute->symbols.push_back(std::move(symbol));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700283 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 }
285 table.unlockBag(entry);
286 return s;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700287}
288
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 const ResourceName& name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291 const android::ResTable& table = assets_.getResources(false);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700292
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293 const std::u16string package16 = util::Utf8ToUtf16(name.package);
294 const std::u16string type16 = util::Utf8ToUtf16(ToString(name.type));
295 const std::u16string entry16 = util::Utf8ToUtf16(name.entry);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700296
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 uint32_t type_spec_flags = 0;
298 ResourceId res_id = table.identifierForName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 entry16.data(), entry16.size(), type16.data(), type16.size(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 package16.data(), package16.size(), &type_spec_flags);
301 if (!res_id.is_valid()) {
Adam Lesinski64587af2016-02-18 18:33:06 -0800302 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 }
304
305 std::unique_ptr<SymbolTable::Symbol> s;
306 if (name.type == ResourceType::kAttr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 s = LookupAttributeInTable(table, res_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308 } else {
309 s = util::make_unique<SymbolTable::Symbol>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310 s->id = res_id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 }
312
313 if (s) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800314 s->is_public = (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 return s;
316 }
317 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700318}
319
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320static Maybe<ResourceName> GetResourceName(const android::ResTable& table,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 ResourceId id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 android::ResTable::resource_name res_name = {};
323 if (!table.getResourceName(id.id, true, &res_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324 return {};
325 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700326 return ResourceUtils::ToResourceName(res_name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800327}
328
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindById(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330 ResourceId id) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800331 if (!id.is_valid()) {
332 // Exit early and avoid the error logs from AssetManager.
333 return {};
334 }
335
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 const android::ResTable& table = assets_.getResources(false);
337 Maybe<ResourceName> maybe_name = GetResourceName(table, id);
338 if (!maybe_name) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700339 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 }
341
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 uint32_t type_spec_flags = 0;
343 table.getResourceFlags(id.id, &type_spec_flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344
345 std::unique_ptr<SymbolTable::Symbol> s;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 if (maybe_name.value().type == ResourceType::kAttr) {
347 s = LookupAttributeInTable(table, id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 } else {
349 s = util::make_unique<SymbolTable::Symbol>();
350 s->id = id;
351 }
352
353 if (s) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800354 s->is_public = (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355 return s;
356 }
357 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700358}
359
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByReference(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 const Reference& ref) {
362 // AssetManager always prefers IDs.
363 if (ref.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700364 return FindById(ref.id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 } else if (ref.name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700366 return FindByName(ref.name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700367 }
368 return {};
Adam Lesinski7656554f2016-03-10 21:55:04 -0800369}
370
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700371} // namespace aapt