blob: 5d75e76af0351c047303d45068e7e7352df3ca24 [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 Lesinskice5e56e2016-10-21 17:56:45 -070037void SymbolTable::AppendSource(std::unique_ptr<ISymbolSource> source) {
38 sources_.push_back(std::move(source));
Adam Lesinski64587af2016-02-18 18:33:06 -080039
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 // We do not clear the cache, because sources earlier in the list take
41 // precedent.
Adam Lesinski64587af2016-02-18 18:33:06 -080042}
43
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044void SymbolTable::PrependSource(std::unique_ptr<ISymbolSource> source) {
45 sources_.insert(sources_.begin(), std::move(source));
Adam Lesinski64587af2016-02-18 18:33:06 -080046
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 // We must clear the cache in case we did a lookup before adding this
48 // resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 cache_.clear();
Adam Lesinski64587af2016-02-18 18:33:06 -080050}
51
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052const SymbolTable::Symbol* SymbolTable::FindByName(const ResourceName& name) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080053 const ResourceName* name_with_package = &name;
54
55 // Fill in the package name if necessary.
56 // If there is no package in `name`, we will need to copy the ResourceName
57 // and store it somewhere; we use the Maybe<> class to reserve storage.
58 Maybe<ResourceName> name_with_package_impl;
59 if (name.package.empty()) {
60 name_with_package_impl = ResourceName(mangler_->GetTargetPackageName(), name.type, name.entry);
61 name_with_package = &name_with_package_impl.value();
62 }
63
64 // We store the name unmangled in the cache, so look it up as-is.
65 if (const std::shared_ptr<Symbol>& s = cache_.get(*name_with_package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 return s.get();
67 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080069 // The name was not found in the cache. Mangle it (if necessary) and find it in our sources.
70 // Again, here we use a Maybe<> object to reserve storage if we need to mangle.
71 const ResourceName* mangled_name = name_with_package;
72 Maybe<ResourceName> mangled_name_impl;
73 if (mangler_->ShouldMangle(name_with_package->package)) {
74 mangled_name_impl = mangler_->MangleName(*name_with_package);
75 mangled_name = &mangled_name_impl.value();
76 }
77
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 for (auto& symbolSource : sources_) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080079 std::unique_ptr<Symbol> symbol = symbolSource->FindByName(*mangled_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 if (symbol) {
81 // Take ownership of the symbol into a shared_ptr. We do this because
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080082 // LruCache doesn't support unique_ptr.
83 std::shared_ptr<Symbol> shared_symbol(std::move(symbol));
84
85 // Since we look in the cache with the unmangled, but package prefixed
86 // name, we must put the same name into the cache.
87 cache_.put(*name_with_package, shared_symbol);
Adam Lesinski7656554f2016-03-10 21:55:04 -080088
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 if (shared_symbol->id) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 // The symbol has an ID, so we can also cache this!
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 id_cache_.put(shared_symbol->id.value(), shared_symbol);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 }
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080093
94 // Returns the raw pointer. Callers are not expected to hold on to this
95 // between calls to Find*.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 return shared_symbol.get();
Adam Lesinski64587af2016-02-18 18:33:06 -080097 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 }
99 return nullptr;
Adam Lesinski64587af2016-02-18 18:33:06 -0800100}
101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102const SymbolTable::Symbol* SymbolTable::FindById(const ResourceId& id) {
103 if (const std::shared_ptr<Symbol>& s = id_cache_.get(id)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 return s.get();
105 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 // We did not find it in the cache, so look through the sources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 for (auto& symbolSource : sources_) {
109 std::unique_ptr<Symbol> symbol = symbolSource->FindById(id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 if (symbol) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800111 // Take ownership of the symbol into a shared_ptr. We do this because LruCache
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 // doesn't support unique_ptr.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800113 std::shared_ptr<Symbol> shared_symbol(std::move(symbol));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 id_cache_.put(id, shared_symbol);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800115
116 // Returns the raw pointer. Callers are not expected to hold on to this
117 // between calls to Find*.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 return shared_symbol.get();
Adam Lesinski64587af2016-02-18 18:33:06 -0800119 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 }
121 return nullptr;
Adam Lesinski64587af2016-02-18 18:33:06 -0800122}
123
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124const SymbolTable::Symbol* SymbolTable::FindByReference(const Reference& ref) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800125 // First try the ID. This is because when we lookup by ID, we only fill in the ID cache.
126 // Looking up by name fills in the name and ID cache. So a cache miss will cause a failed
127 // ID lookup, then a successful name lookup. Subsequent look ups will hit immediately
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 // because the ID is cached too.
129 //
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800130 // 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 -0700131 // succeeded to lookup by ID. Subsequent lookups will miss then hit.
132 const SymbolTable::Symbol* symbol = nullptr;
133 if (ref.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 symbol = FindById(ref.id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 }
Adam Lesinski7656554f2016-03-10 21:55:04 -0800136
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 if (ref.name && !symbol) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 symbol = FindByName(ref.name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 }
140 return symbol;
Adam Lesinski7656554f2016-03-10 21:55:04 -0800141}
142
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143std::unique_ptr<SymbolTable::Symbol> ResourceTableSymbolSource::FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 const ResourceName& name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 Maybe<ResourceTable::SearchResult> result = table_->FindResource(name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 if (!result) {
147 if (name.type == ResourceType::kAttr) {
148 // Recurse and try looking up a private attribute.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800149 return FindByName(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 }
151 return {};
152 }
153
154 ResourceTable::SearchResult sr = result.value();
155
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800156 std::unique_ptr<SymbolTable::Symbol> symbol = util::make_unique<SymbolTable::Symbol>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 symbol->is_public = (sr.entry->symbol_status.state == SymbolState::kPublic);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158
159 if (sr.package->id && sr.type->id && sr.entry->id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800160 symbol->id = ResourceId(sr.package->id.value(), sr.type->id.value(), sr.entry->id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 }
162
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800163 if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 const ConfigDescription kDefaultConfig;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 ResourceConfigValue* config_value = sr.entry->FindValue(kDefaultConfig);
166 if (config_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 // This resource has an Attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 if (Attribute* attr = ValueCast<Attribute>(config_value->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 symbol->attribute = std::make_shared<Attribute>(*attr);
170 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700171 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 }
175 return symbol;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700176}
177
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178bool AssetManagerSymbolSource::AddAssetPath(const StringPiece& path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 int32_t cookie = 0;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800180 return assets_.addAssetPath(android::String8(path.data(), path.size()), &cookie);
181}
182
183std::map<size_t, std::string> AssetManagerSymbolSource::GetAssignedPackageIds() const {
184 std::map<size_t, std::string> package_map;
185 const android::ResTable& table = assets_.getResources(false);
186 const size_t package_count = table.getBasePackageCount();
187 for (size_t i = 0; i < package_count; i++) {
188 package_map[table.getBasePackageId(i)] =
189 util::Utf16ToUtf8(android::StringPiece16(table.getBasePackageName(i).string()));
190 }
191 return package_map;
Adam Lesinski64587af2016-02-18 18:33:06 -0800192}
193
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194static std::unique_ptr<SymbolTable::Symbol> LookupAttributeInTable(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 const android::ResTable& table, ResourceId id) {
196 // Try as a bag.
197 const android::ResTable::bag_entry* entry;
198 ssize_t count = table.lockBag(id.id, &entry);
199 if (count < 0) {
200 table.unlockBag(entry);
201 return nullptr;
202 }
203
204 // We found a resource.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800205 std::unique_ptr<SymbolTable::Symbol> s = util::make_unique<SymbolTable::Symbol>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 s->id = id;
207
208 // Check to see if it is an attribute.
209 for (size_t i = 0; i < (size_t)count; i++) {
210 if (entry[i].map.name.ident == android::ResTable_map::ATTR_TYPE) {
211 s->attribute = std::make_shared<Attribute>(false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 s->attribute->type_mask = entry[i].map.value.data;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700214 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700216
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 if (s->attribute) {
218 for (size_t i = 0; i < (size_t)count; i++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 const android::ResTable_map& map_entry = entry[i].map;
220 if (Res_INTERNALID(map_entry.name.ident)) {
221 switch (map_entry.name.ident) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 case android::ResTable_map::ATTR_MIN:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 s->attribute->min_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 break;
225 case android::ResTable_map::ATTR_MAX:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700226 s->attribute->max_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700227 break;
228 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 continue;
230 }
231
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 android::ResTable::resource_name entry_name;
233 if (!table.getResourceName(map_entry.name.ident, false, &entry_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 table.unlockBag(entry);
235 return nullptr;
236 }
237
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800238 Maybe<ResourceName> parsed_name = ResourceUtils::ToResourceName(entry_name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 if (!parsed_name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 return nullptr;
241 }
242
243 Attribute::Symbol symbol;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 symbol.symbol.name = parsed_name.value();
245 symbol.symbol.id = ResourceId(map_entry.name.ident);
246 symbol.value = map_entry.value.data;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 s->attribute->symbols.push_back(std::move(symbol));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700248 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 }
250 table.unlockBag(entry);
251 return s;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700252}
253
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 const ResourceName& name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 const android::ResTable& table = assets_.getResources(false);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700257
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 const std::u16string package16 = util::Utf8ToUtf16(name.package);
259 const std::u16string type16 = util::Utf8ToUtf16(ToString(name.type));
260 const std::u16string entry16 = util::Utf8ToUtf16(name.entry);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700261
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 uint32_t type_spec_flags = 0;
263 ResourceId res_id = table.identifierForName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 entry16.data(), entry16.size(), type16.data(), type16.size(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 package16.data(), package16.size(), &type_spec_flags);
266 if (!res_id.is_valid()) {
Adam Lesinski64587af2016-02-18 18:33:06 -0800267 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700268 }
269
270 std::unique_ptr<SymbolTable::Symbol> s;
271 if (name.type == ResourceType::kAttr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 s = LookupAttributeInTable(table, res_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 } else {
274 s = util::make_unique<SymbolTable::Symbol>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 s->id = res_id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 }
277
278 if (s) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800279 s->is_public = (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 return s;
281 }
282 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700283}
284
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285static Maybe<ResourceName> GetResourceName(const android::ResTable& table,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 ResourceId id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287 android::ResTable::resource_name res_name = {};
288 if (!table.getResourceName(id.id, true, &res_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 return {};
290 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291 return ResourceUtils::ToResourceName(res_name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800292}
293
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindById(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295 ResourceId id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 const android::ResTable& table = assets_.getResources(false);
297 Maybe<ResourceName> maybe_name = GetResourceName(table, id);
298 if (!maybe_name) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700299 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 }
301
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 uint32_t type_spec_flags = 0;
303 table.getResourceFlags(id.id, &type_spec_flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304
305 std::unique_ptr<SymbolTable::Symbol> s;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700306 if (maybe_name.value().type == ResourceType::kAttr) {
307 s = LookupAttributeInTable(table, id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308 } else {
309 s = util::make_unique<SymbolTable::Symbol>();
310 s->id = id;
311 }
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 -0700320std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByReference(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 const Reference& ref) {
322 // AssetManager always prefers IDs.
323 if (ref.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 return FindById(ref.id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700325 } else if (ref.name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700326 return FindByName(ref.name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327 }
328 return {};
Adam Lesinski7656554f2016-03-10 21:55:04 -0800329}
330
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700331} // namespace aapt