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