blob: 1a3da73b8ce093654440c4d549c779c1c09b3087 [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
19#include "androidfw/AssetManager.h"
20#include "androidfw/ResourceTypes.h"
21
Adam Lesinski1ab598f2015-08-14 14:26:04 -070022#include "ConfigDescription.h"
23#include "Resource.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070024#include "ResourceUtils.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070025#include "ValueVisitor.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070026#include "util/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027
Adam Lesinskid5083f62017-01-16 15:07:21 -080028using android::StringPiece;
29
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030namespace aapt {
31
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032void SymbolTable::AppendSource(std::unique_ptr<ISymbolSource> source) {
33 sources_.push_back(std::move(source));
Adam Lesinski64587af2016-02-18 18:33:06 -080034
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 // We do not clear the cache, because sources earlier in the list take
36 // precedent.
Adam Lesinski64587af2016-02-18 18:33:06 -080037}
38
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039void SymbolTable::PrependSource(std::unique_ptr<ISymbolSource> source) {
40 sources_.insert(sources_.begin(), std::move(source));
Adam Lesinski64587af2016-02-18 18:33:06 -080041
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 // We must clear the cache in case we did a lookup before adding this
43 // resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 cache_.clear();
Adam Lesinski64587af2016-02-18 18:33:06 -080045}
46
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047const SymbolTable::Symbol* SymbolTable::FindByName(const ResourceName& name) {
48 if (const std::shared_ptr<Symbol>& s = cache_.get(name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 return s.get();
50 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 // We did not find it in the cache, so look through the sources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 for (auto& symbolSource : sources_) {
54 std::unique_ptr<Symbol> symbol = symbolSource->FindByName(name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 if (symbol) {
56 // Take ownership of the symbol into a shared_ptr. We do this because
57 // LruCache
58 // doesn't support unique_ptr.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 std::shared_ptr<Symbol> shared_symbol =
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 std::shared_ptr<Symbol>(symbol.release());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 cache_.put(name, shared_symbol);
Adam Lesinski7656554f2016-03-10 21:55:04 -080062
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 if (shared_symbol->id) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 // The symbol has an ID, so we can also cache this!
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 id_cache_.put(shared_symbol->id.value(), shared_symbol);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 return shared_symbol.get();
Adam Lesinski64587af2016-02-18 18:33:06 -080068 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 }
70 return nullptr;
Adam Lesinski64587af2016-02-18 18:33:06 -080071}
72
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073const SymbolTable::Symbol* SymbolTable::FindById(const ResourceId& id) {
74 if (const std::shared_ptr<Symbol>& s = id_cache_.get(id)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 return s.get();
76 }
Adam Lesinski64587af2016-02-18 18:33:06 -080077
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 // We did not find it in the cache, so look through the sources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 for (auto& symbolSource : sources_) {
80 std::unique_ptr<Symbol> symbol = symbolSource->FindById(id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 if (symbol) {
82 // Take ownership of the symbol into a shared_ptr. We do this because
83 // LruCache
84 // doesn't support unique_ptr.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 std::shared_ptr<Symbol> shared_symbol =
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 std::shared_ptr<Symbol>(symbol.release());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 id_cache_.put(id, shared_symbol);
88 return shared_symbol.get();
Adam Lesinski64587af2016-02-18 18:33:06 -080089 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 }
91 return nullptr;
Adam Lesinski64587af2016-02-18 18:33:06 -080092}
93
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094const SymbolTable::Symbol* SymbolTable::FindByReference(const Reference& ref) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 // First try the ID. This is because when we lookup by ID, we only fill in the
96 // ID cache.
97 // Looking up by name fills in the name and ID cache. So a cache miss will
98 // cause a failed
99 // ID lookup, then a successful name lookup. Subsequent look ups will hit
100 // immediately
101 // because the ID is cached too.
102 //
103 // If we looked up by name first, a cache miss would mean we failed to lookup
104 // by name, then
105 // succeeded to lookup by ID. Subsequent lookups will miss then hit.
106 const SymbolTable::Symbol* symbol = nullptr;
107 if (ref.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 symbol = FindById(ref.id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 }
Adam Lesinski7656554f2016-03-10 21:55:04 -0800110
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 if (ref.name && !symbol) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 symbol = FindByName(ref.name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 }
114 return symbol;
Adam Lesinski7656554f2016-03-10 21:55:04 -0800115}
116
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117std::unique_ptr<SymbolTable::Symbol> ResourceTableSymbolSource::FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 const ResourceName& name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 Maybe<ResourceTable::SearchResult> result = table_->FindResource(name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 if (!result) {
121 if (name.type == ResourceType::kAttr) {
122 // Recurse and try looking up a private attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 return FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 ResourceName(name.package, ResourceType::kAttrPrivate, name.entry));
125 }
126 return {};
127 }
128
129 ResourceTable::SearchResult sr = result.value();
130
131 std::unique_ptr<SymbolTable::Symbol> symbol =
132 util::make_unique<SymbolTable::Symbol>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 symbol->is_public = (sr.entry->symbol_status.state == SymbolState::kPublic);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134
135 if (sr.package->id && sr.type->id && sr.entry->id) {
136 symbol->id = ResourceId(sr.package->id.value(), sr.type->id.value(),
137 sr.entry->id.value());
138 }
139
140 if (name.type == ResourceType::kAttr ||
141 name.type == ResourceType::kAttrPrivate) {
142 const ConfigDescription kDefaultConfig;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 ResourceConfigValue* config_value = sr.entry->FindValue(kDefaultConfig);
144 if (config_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 // This resource has an Attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 if (Attribute* attr = ValueCast<Attribute>(config_value->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 symbol->attribute = std::make_shared<Attribute>(*attr);
148 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700149 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 }
153 return symbol;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700154}
155
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156bool AssetManagerSymbolSource::AddAssetPath(const StringPiece& path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 int32_t cookie = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 return assets_.addAssetPath(android::String8(path.data(), path.size()),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 &cookie);
Adam Lesinski64587af2016-02-18 18:33:06 -0800160}
161
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162static std::unique_ptr<SymbolTable::Symbol> LookupAttributeInTable(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 const android::ResTable& table, ResourceId id) {
164 // Try as a bag.
165 const android::ResTable::bag_entry* entry;
166 ssize_t count = table.lockBag(id.id, &entry);
167 if (count < 0) {
168 table.unlockBag(entry);
169 return nullptr;
170 }
171
172 // We found a resource.
173 std::unique_ptr<SymbolTable::Symbol> s =
174 util::make_unique<SymbolTable::Symbol>();
175 s->id = id;
176
177 // Check to see if it is an attribute.
178 for (size_t i = 0; i < (size_t)count; i++) {
179 if (entry[i].map.name.ident == android::ResTable_map::ATTR_TYPE) {
180 s->attribute = std::make_shared<Attribute>(false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 s->attribute->type_mask = entry[i].map.value.data;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700183 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700185
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 if (s->attribute) {
187 for (size_t i = 0; i < (size_t)count; i++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188 const android::ResTable_map& map_entry = entry[i].map;
189 if (Res_INTERNALID(map_entry.name.ident)) {
190 switch (map_entry.name.ident) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 case android::ResTable_map::ATTR_MIN:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 s->attribute->min_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 break;
194 case android::ResTable_map::ATTR_MAX:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195 s->attribute->max_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700196 break;
197 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 continue;
199 }
200
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 android::ResTable::resource_name entry_name;
202 if (!table.getResourceName(map_entry.name.ident, false, &entry_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 table.unlockBag(entry);
204 return nullptr;
205 }
206
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 Maybe<ResourceName> parsed_name =
208 ResourceUtils::ToResourceName(entry_name);
209 if (!parsed_name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 return nullptr;
211 }
212
213 Attribute::Symbol symbol;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 symbol.symbol.name = parsed_name.value();
215 symbol.symbol.id = ResourceId(map_entry.name.ident);
216 symbol.value = map_entry.value.data;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 s->attribute->symbols.push_back(std::move(symbol));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700218 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 }
220 table.unlockBag(entry);
221 return s;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700222}
223
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 const ResourceName& name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700226 const android::ResTable& table = assets_.getResources(false);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700227
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700228 const std::u16string package16 = util::Utf8ToUtf16(name.package);
229 const std::u16string type16 = util::Utf8ToUtf16(ToString(name.type));
230 const std::u16string entry16 = util::Utf8ToUtf16(name.entry);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700231
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 uint32_t type_spec_flags = 0;
233 ResourceId res_id = table.identifierForName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 entry16.data(), entry16.size(), type16.data(), type16.size(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 package16.data(), package16.size(), &type_spec_flags);
236 if (!res_id.is_valid()) {
Adam Lesinski64587af2016-02-18 18:33:06 -0800237 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 }
239
240 std::unique_ptr<SymbolTable::Symbol> s;
241 if (name.type == ResourceType::kAttr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 s = LookupAttributeInTable(table, res_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 } else {
244 s = util::make_unique<SymbolTable::Symbol>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 s->id = res_id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 }
247
248 if (s) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 s->is_public =
250 (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251 return s;
252 }
253 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700254}
255
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256static Maybe<ResourceName> GetResourceName(const android::ResTable& table,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 ResourceId id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 android::ResTable::resource_name res_name = {};
259 if (!table.getResourceName(id.id, true, &res_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 return {};
261 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 return ResourceUtils::ToResourceName(res_name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800263}
264
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindById(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 ResourceId id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 const android::ResTable& table = assets_.getResources(false);
268 Maybe<ResourceName> maybe_name = GetResourceName(table, id);
269 if (!maybe_name) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700270 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 }
272
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 uint32_t type_spec_flags = 0;
274 table.getResourceFlags(id.id, &type_spec_flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275
276 std::unique_ptr<SymbolTable::Symbol> s;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 if (maybe_name.value().type == ResourceType::kAttr) {
278 s = LookupAttributeInTable(table, id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 } else {
280 s = util::make_unique<SymbolTable::Symbol>();
281 s->id = id;
282 }
283
284 if (s) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 s->is_public =
286 (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 return s;
288 }
289 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700290}
291
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByReference(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 const Reference& ref) {
294 // AssetManager always prefers IDs.
295 if (ref.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 return FindById(ref.id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 } else if (ref.name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 return FindByName(ref.name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 }
300 return {};
Adam Lesinski7656554f2016-03-10 21:55:04 -0800301}
302
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303} // namespace aapt