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 #include "process/SymbolTable.h"
18
19 #include <iostream>
20
21 #include "android-base/logging.h"
22 #include "android-base/stringprintf.h"
23 #include "androidfw/Asset.h"
24 #include "androidfw/AssetManager2.h"
25 #include "androidfw/ConfigDescription.h"
26 #include "androidfw/ResourceTypes.h"
27 #include "androidfw/ResourceUtils.h"
28
29 #include "NameMangler.h"
30 #include "Resource.h"
31 #include "ResourceUtils.h"
32 #include "ValueVisitor.h"
33 #include "trace/TraceBuffer.h"
34 #include "util/Util.h"
35
36 using ::android::ApkAssets;
37 using ::android::ConfigDescription;
38 using ::android::StringPiece;
39 using ::android::StringPiece16;
40
41 namespace aapt {
42
SymbolTable(NameMangler * mangler)43 SymbolTable::SymbolTable(NameMangler* mangler)
44 : mangler_(mangler),
45 delegate_(util::make_unique<DefaultSymbolTableDelegate>()),
46 cache_(200),
47 id_cache_(200) {
48 }
49
SetDelegate(std::unique_ptr<ISymbolTableDelegate> delegate)50 void SymbolTable::SetDelegate(std::unique_ptr<ISymbolTableDelegate> delegate) {
51 CHECK(delegate != nullptr) << "can't set a nullptr delegate";
52 delegate_ = std::move(delegate);
53
54 // Clear the cache in case this delegate changes the order of lookup.
55 cache_.clear();
56 }
57
AppendSource(std::unique_ptr<ISymbolSource> source)58 void SymbolTable::AppendSource(std::unique_ptr<ISymbolSource> source) {
59 sources_.push_back(std::move(source));
60
61 // We do not clear the cache, because sources earlier in the list take
62 // precedent.
63 }
64
PrependSource(std::unique_ptr<ISymbolSource> source)65 void SymbolTable::PrependSource(std::unique_ptr<ISymbolSource> source) {
66 sources_.insert(sources_.begin(), std::move(source));
67
68 // We must clear the cache in case we did a lookup before adding this
69 // resource.
70 cache_.clear();
71 }
72
FindByName(const ResourceName & name)73 const SymbolTable::Symbol* SymbolTable::FindByName(const ResourceName& name) {
74 const ResourceName* name_with_package = &name;
75
76 // Fill in the package name if necessary.
77 // If there is no package in `name`, we will need to copy the ResourceName
78 // and store it somewhere; we use the Maybe<> class to reserve storage.
79 Maybe<ResourceName> name_with_package_impl;
80 if (name.package.empty()) {
81 name_with_package_impl = ResourceName(mangler_->GetTargetPackageName(), name.type, name.entry);
82 name_with_package = &name_with_package_impl.value();
83 }
84
85 // We store the name unmangled in the cache, so look it up as-is.
86 if (const std::shared_ptr<Symbol>& s = cache_.get(*name_with_package)) {
87 return s.get();
88 }
89
90 // The name was not found in the cache. Mangle it (if necessary) and find it in our sources.
91 // Again, here we use a Maybe<> object to reserve storage if we need to mangle.
92 const ResourceName* mangled_name = name_with_package;
93 Maybe<ResourceName> mangled_name_impl;
94 if (mangler_->ShouldMangle(name_with_package->package)) {
95 mangled_name_impl = mangler_->MangleName(*name_with_package);
96 mangled_name = &mangled_name_impl.value();
97 }
98
99 std::unique_ptr<Symbol> symbol = delegate_->FindByName(*mangled_name, sources_);
100 if (symbol == nullptr) {
101 return nullptr;
102 }
103
104 // Take ownership of the symbol into a shared_ptr. We do this because
105 // LruCache doesn't support unique_ptr.
106 std::shared_ptr<Symbol> shared_symbol(std::move(symbol));
107
108 // Since we look in the cache with the unmangled, but package prefixed
109 // name, we must put the same name into the cache.
110 cache_.put(*name_with_package, shared_symbol);
111
112 if (shared_symbol->id) {
113 // The symbol has an ID, so we can also cache this!
114 id_cache_.put(shared_symbol->id.value(), shared_symbol);
115 }
116
117 // Returns the raw pointer. Callers are not expected to hold on to this
118 // between calls to Find*.
119 return shared_symbol.get();
120 }
121
FindById(const ResourceId & id)122 const SymbolTable::Symbol* SymbolTable::FindById(const ResourceId& id) {
123 if (const std::shared_ptr<Symbol>& s = id_cache_.get(id)) {
124 return s.get();
125 }
126
127 // We did not find it in the cache, so look through the sources.
128 std::unique_ptr<Symbol> symbol = delegate_->FindById(id, sources_);
129 if (symbol == nullptr) {
130 return nullptr;
131 }
132
133 // Take ownership of the symbol into a shared_ptr. We do this because LruCache
134 // doesn't support unique_ptr.
135 std::shared_ptr<Symbol> shared_symbol(std::move(symbol));
136 id_cache_.put(id, shared_symbol);
137
138 // Returns the raw pointer. Callers are not expected to hold on to this
139 // between calls to Find*.
140 return shared_symbol.get();
141 }
142
FindByReference(const Reference & ref)143 const SymbolTable::Symbol* SymbolTable::FindByReference(const Reference& ref) {
144 // First try the ID. This is because when we lookup by ID, we only fill in the ID cache.
145 // Looking up by name fills in the name and ID cache. So a cache miss will cause a failed
146 // ID lookup, then a successful name lookup. Subsequent look ups will hit immediately
147 // because the ID is cached too.
148 //
149 // If we looked up by name first, a cache miss would mean we failed to lookup by name, then
150 // succeeded to lookup by ID. Subsequent lookups will miss then hit.
151 const SymbolTable::Symbol* symbol = nullptr;
152 if (ref.id) {
153 symbol = FindById(ref.id.value());
154 }
155
156 if (ref.name && !symbol) {
157 symbol = FindByName(ref.name.value());
158 }
159 return symbol;
160 }
161
FindByName(const ResourceName & name,const std::vector<std::unique_ptr<ISymbolSource>> & sources)162 std::unique_ptr<SymbolTable::Symbol> DefaultSymbolTableDelegate::FindByName(
163 const ResourceName& name, const std::vector<std::unique_ptr<ISymbolSource>>& sources) {
164 for (auto& source : sources) {
165 std::unique_ptr<SymbolTable::Symbol> symbol = source->FindByName(name);
166 if (symbol) {
167 return symbol;
168 }
169 }
170 return {};
171 }
172
FindById(ResourceId id,const std::vector<std::unique_ptr<ISymbolSource>> & sources)173 std::unique_ptr<SymbolTable::Symbol> DefaultSymbolTableDelegate::FindById(
174 ResourceId id, const std::vector<std::unique_ptr<ISymbolSource>>& sources) {
175 for (auto& source : sources) {
176 std::unique_ptr<SymbolTable::Symbol> symbol = source->FindById(id);
177 if (symbol) {
178 return symbol;
179 }
180 }
181 return {};
182 }
183
FindByName(const ResourceName & name)184 std::unique_ptr<SymbolTable::Symbol> ResourceTableSymbolSource::FindByName(
185 const ResourceName& name) {
186 Maybe<ResourceTable::SearchResult> result = table_->FindResource(name);
187 if (!result) {
188 if (name.type == ResourceType::kAttr) {
189 // Recurse and try looking up a private attribute.
190 return FindByName(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry));
191 }
192 return {};
193 }
194
195 ResourceTable::SearchResult sr = result.value();
196
197 std::unique_ptr<SymbolTable::Symbol> symbol = util::make_unique<SymbolTable::Symbol>();
198 symbol->is_public = (sr.entry->visibility.level == Visibility::Level::kPublic);
199
200 if (sr.entry->id) {
201 symbol->id = sr.entry->id.value();
202 symbol->is_dynamic =
203 (sr.entry->id.value().package_id() == 0) || sr.entry->visibility.staged_api;
204 }
205
206 if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) {
207 const ConfigDescription kDefaultConfig;
208 ResourceConfigValue* config_value = sr.entry->FindValue(kDefaultConfig);
209 if (config_value) {
210 // This resource has an Attribute.
211 if (Attribute* attr = ValueCast<Attribute>(config_value->value.get())) {
212 symbol->attribute = std::make_shared<Attribute>(*attr);
213 } else {
214 return {};
215 }
216 }
217 }
218 return symbol;
219 }
220
AddAssetPath(const StringPiece & path)221 bool AssetManagerSymbolSource::AddAssetPath(const StringPiece& path) {
222 TRACE_CALL();
223 if (std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(path.data())) {
224 apk_assets_.push_back(std::move(apk));
225
226 std::vector<const ApkAssets*> apk_assets;
227 for (const std::unique_ptr<const ApkAssets>& apk_asset : apk_assets_) {
228 apk_assets.push_back(apk_asset.get());
229 }
230
231 asset_manager_.SetApkAssets(apk_assets);
232 return true;
233 }
234 return false;
235 }
236
GetAssignedPackageIds() const237 std::map<size_t, std::string> AssetManagerSymbolSource::GetAssignedPackageIds() const {
238 TRACE_CALL();
239 std::map<size_t, std::string> package_map;
240 asset_manager_.ForEachPackage([&package_map](const std::string& name, uint8_t id) -> bool {
241 package_map.insert(std::make_pair(id, name));
242 return true;
243 });
244
245 return package_map;
246 }
247
IsPackageDynamic(uint32_t packageId,const std::string & package_name) const248 bool AssetManagerSymbolSource::IsPackageDynamic(uint32_t packageId,
249 const std::string& package_name) const {
250 if (packageId == 0) {
251 return true;
252 }
253
254 for (const std::unique_ptr<const ApkAssets>& assets : apk_assets_) {
255 for (const std::unique_ptr<const android::LoadedPackage>& loaded_package
256 : assets->GetLoadedArsc()->GetPackages()) {
257 if (package_name == loaded_package->GetPackageName() && loaded_package->IsDynamic()) {
258 return true;
259 }
260 }
261 }
262
263 return false;
264 }
265
LookupAttributeInTable(android::AssetManager2 & am,ResourceId id)266 static std::unique_ptr<SymbolTable::Symbol> LookupAttributeInTable(
267 android::AssetManager2& am, ResourceId id) {
268 using namespace android;
269 if (am.GetApkAssets().empty()) {
270 return {};
271 }
272
273 auto bag_result = am.GetBag(id.id);
274 if (!bag_result.has_value()) {
275 return nullptr;
276 }
277
278 // We found a resource.
279 std::unique_ptr<SymbolTable::Symbol> s = util::make_unique<SymbolTable::Symbol>(id);
280 const ResolvedBag* bag = *bag_result;
281 const size_t count = bag->entry_count;
282 for (uint32_t i = 0; i < count; i++) {
283 if (bag->entries[i].key == ResTable_map::ATTR_TYPE) {
284 s->attribute = std::make_shared<Attribute>(bag->entries[i].value.data);
285 break;
286 }
287 }
288
289 if (s->attribute) {
290 for (size_t i = 0; i < count; i++) {
291 const ResolvedBag::Entry& map_entry = bag->entries[i];
292 if (Res_INTERNALID(map_entry.key)) {
293 switch (map_entry.key) {
294 case ResTable_map::ATTR_MIN:
295 s->attribute->min_int = static_cast<int32_t>(map_entry.value.data);
296 break;
297 case ResTable_map::ATTR_MAX:
298 s->attribute->max_int = static_cast<int32_t>(map_entry.value.data);
299 break;
300 }
301 continue;
302 }
303
304 auto name = am.GetResourceName(map_entry.key);
305 if (!name.has_value()) {
306 return nullptr;
307 }
308
309 Maybe<ResourceName> parsed_name = ResourceUtils::ToResourceName(*name);
310 if (!parsed_name) {
311 return nullptr;
312 }
313
314 Attribute::Symbol symbol;
315 symbol.symbol.name = parsed_name.value();
316 symbol.symbol.id = ResourceId(map_entry.key);
317 symbol.value = map_entry.value.data;
318 symbol.type = map_entry.value.dataType;
319 s->attribute->symbols.push_back(std::move(symbol));
320 }
321 }
322
323 return s;
324 }
325
FindByName(const ResourceName & name)326 std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByName(
327 const ResourceName& name) {
328 const std::string mangled_entry = NameMangler::MangleEntry(name.package, name.entry);
329
330 bool found = false;
331 ResourceId res_id = 0;
332 uint32_t type_spec_flags = 0;
333 ResourceName real_name;
334
335 // There can be mangled resources embedded within other packages. Here we will
336 // look into each package and look-up the mangled name until we find the resource.
337 asset_manager_.ForEachPackage([&](const std::string& package_name, uint8_t id) -> bool {
338 real_name = ResourceName(name.package, name.type, name.entry);
339 if (package_name != name.package) {
340 real_name.entry = mangled_entry;
341 real_name.package = package_name;
342 }
343
344 auto real_res_id = asset_manager_.GetResourceId(real_name.to_string());
345 if (!real_res_id.has_value()) {
346 return true;
347 }
348
349 res_id.id = *real_res_id;
350 if (!res_id.is_valid_static()) {
351 return true;
352 }
353
354 auto flags = asset_manager_.GetResourceTypeSpecFlags(res_id.id);
355 if (flags.has_value()) {
356 type_spec_flags = *flags;
357 found = true;
358 return false;
359 }
360
361 return true;
362 });
363
364 if (!found) {
365 return {};
366 }
367
368 std::unique_ptr<SymbolTable::Symbol> s;
369 if (real_name.type == ResourceType::kAttr) {
370 s = LookupAttributeInTable(asset_manager_, res_id);
371 } else {
372 s = util::make_unique<SymbolTable::Symbol>();
373 s->id = res_id;
374 }
375
376 if (s) {
377 s->is_public = (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
378 s->is_dynamic = IsPackageDynamic(ResourceId(res_id).package_id(), real_name.package) ||
379 (type_spec_flags & android::ResTable_typeSpec::SPEC_STAGED_API) != 0;
380 return s;
381 }
382 return {};
383 }
384
GetResourceName(android::AssetManager2 & am,ResourceId id)385 static Maybe<ResourceName> GetResourceName(android::AssetManager2& am,
386 ResourceId id) {
387 auto name = am.GetResourceName(id.id);
388 if (!name.has_value()) {
389 return {};
390 }
391 return ResourceUtils::ToResourceName(*name);
392 }
393
FindById(ResourceId id)394 std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindById(
395 ResourceId id) {
396 if (!id.is_valid_static()) {
397 // Exit early and avoid the error logs from AssetManager.
398 return {};
399 }
400
401 if (apk_assets_.empty()) {
402 return {};
403 }
404
405 Maybe<ResourceName> maybe_name = GetResourceName(asset_manager_, id);
406 if (!maybe_name) {
407 return {};
408 }
409
410 auto flags = asset_manager_.GetResourceTypeSpecFlags(id.id);
411 if (!flags.has_value()) {
412 return {};
413 }
414
415 ResourceName& name = maybe_name.value();
416 std::unique_ptr<SymbolTable::Symbol> s;
417 if (name.type == ResourceType::kAttr) {
418 s = LookupAttributeInTable(asset_manager_, id);
419 } else {
420 s = util::make_unique<SymbolTable::Symbol>();
421 s->id = id;
422 }
423
424 if (s) {
425 s->is_public = (*flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
426 s->is_dynamic = IsPackageDynamic(ResourceId(id).package_id(), name.package) ||
427 (*flags & android::ResTable_typeSpec::SPEC_STAGED_API) != 0;
428 return s;
429 }
430 return {};
431 }
432
FindByReference(const Reference & ref)433 std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByReference(
434 const Reference& ref) {
435 // AssetManager always prefers IDs.
436 if (ref.id) {
437 return FindById(ref.id.value());
438 } else if (ref.name) {
439 return FindByName(ref.name.value());
440 }
441 return {};
442 }
443
444 } // namespace aapt
445