SymbolTable.cpp revision 64587af8179affd38ee26543b748f2d63b7f67bb
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 "ConfigDescription.h" 18#include "Resource.h" 19#include "ValueVisitor.h" 20#include "process/SymbolTable.h" 21#include "util/Util.h" 22 23#include <androidfw/AssetManager.h> 24#include <androidfw/ResourceTypes.h> 25 26namespace aapt { 27 28void SymbolTable::appendSource(std::unique_ptr<ISymbolSource> source) { 29 mSources.push_back(std::move(source)); 30 31 // We do not clear the cache, because sources earlier in the list take precedent. 32} 33 34void SymbolTable::prependSource(std::unique_ptr<ISymbolSource> source) { 35 mSources.insert(mSources.begin(), std::move(source)); 36 37 // We must clear the cache in case we did a lookup before adding this resource. 38 mCache.clear(); 39} 40 41const SymbolTable::Symbol* SymbolTable::findByName(const ResourceName& name) { 42 if (const std::shared_ptr<Symbol>& s = mCache.get(name)) { 43 return s.get(); 44 } 45 46 // We did not find it in the cache, so look through the sources. 47 for (auto& symbolSource : mSources) { 48 std::unique_ptr<Symbol> symbol = symbolSource->findByName(name); 49 if (symbol) { 50 // Take ownership of the symbol into a shared_ptr. We do this because LruCache 51 // doesn't support unique_ptr. 52 std::shared_ptr<Symbol> sharedSymbol = std::shared_ptr<Symbol>(symbol.release()); 53 mCache.put(name, sharedSymbol); 54 return sharedSymbol.get(); 55 } 56 } 57 return nullptr; 58} 59 60const SymbolTable::Symbol* SymbolTable::findById(ResourceId id) { 61 if (const std::shared_ptr<Symbol>& s = mIdCache.get(id)) { 62 return s.get(); 63 } 64 65 // We did not find it in the cache, so look through the sources. 66 for (auto& symbolSource : mSources) { 67 std::unique_ptr<Symbol> symbol = symbolSource->findById(id); 68 if (symbol) { 69 // Take ownership of the symbol into a shared_ptr. We do this because LruCache 70 // doesn't support unique_ptr. 71 std::shared_ptr<Symbol> sharedSymbol = std::shared_ptr<Symbol>(symbol.release()); 72 mIdCache.put(id, sharedSymbol); 73 return sharedSymbol.get(); 74 } 75 } 76 return nullptr; 77} 78 79std::unique_ptr<SymbolTable::Symbol> ResourceTableSymbolSource::findByName( 80 const ResourceName& name) { 81 Maybe<ResourceTable::SearchResult> result = mTable->findResource(name); 82 if (!result) { 83 if (name.type == ResourceType::kAttr) { 84 // Recurse and try looking up a private attribute. 85 return findByName(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry)); 86 } 87 return {}; 88 } 89 90 ResourceTable::SearchResult sr = result.value(); 91 92 std::unique_ptr<SymbolTable::Symbol> symbol = util::make_unique<SymbolTable::Symbol>(); 93 symbol->isPublic = (sr.entry->symbolStatus.state == SymbolState::kPublic); 94 95 if (sr.package->id && sr.type->id && sr.entry->id) { 96 symbol->id = ResourceId(sr.package->id.value(), sr.type->id.value(), sr.entry->id.value()); 97 } 98 99 if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) { 100 const ConfigDescription kDefaultConfig; 101 ResourceConfigValue* configValue = sr.entry->findValue(kDefaultConfig); 102 if (configValue) { 103 // This resource has an Attribute. 104 if (Attribute* attr = valueCast<Attribute>(configValue->value.get())) { 105 symbol->attribute = util::make_unique<Attribute>(*attr); 106 } else { 107 return {}; 108 } 109 } 110 } 111 return symbol; 112} 113 114bool AssetManagerSymbolSource::addAssetPath(const StringPiece& path) { 115 int32_t cookie = 0; 116 return mAssets.addAssetPath(android::String8(path.data(), path.size()), &cookie); 117} 118 119static std::unique_ptr<SymbolTable::Symbol> lookupAttributeInTable(const android::ResTable& table, 120 ResourceId id) { 121 // Try as a bag. 122 const android::ResTable::bag_entry* entry; 123 ssize_t count = table.lockBag(id.id, &entry); 124 if (count < 0) { 125 table.unlockBag(entry); 126 return nullptr; 127 } 128 129 // We found a resource. 130 std::unique_ptr<SymbolTable::Symbol> s = util::make_unique<SymbolTable::Symbol>(); 131 s->id = id; 132 133 // Check to see if it is an attribute. 134 for (size_t i = 0; i < (size_t) count; i++) { 135 if (entry[i].map.name.ident == android::ResTable_map::ATTR_TYPE) { 136 s->attribute = util::make_unique<Attribute>(false); 137 s->attribute->typeMask = entry[i].map.value.data; 138 break; 139 } 140 } 141 142 if (s->attribute) { 143 for (size_t i = 0; i < (size_t) count; i++) { 144 const android::ResTable_map& mapEntry = entry[i].map; 145 if (Res_INTERNALID(mapEntry.name.ident)) { 146 switch (mapEntry.name.ident) { 147 case android::ResTable_map::ATTR_MIN: 148 s->attribute->minInt = static_cast<int32_t>(mapEntry.value.data); 149 break; 150 case android::ResTable_map::ATTR_MAX: 151 s->attribute->maxInt = static_cast<int32_t>(mapEntry.value.data); 152 break; 153 } 154 continue; 155 } 156 157 android::ResTable::resource_name entryName; 158 if (!table.getResourceName(mapEntry.name.ident, false, &entryName)) { 159 table.unlockBag(entry); 160 return nullptr; 161 } 162 163 const ResourceType* parsedType = parseResourceType( 164 StringPiece16(entryName.type, entryName.typeLen)); 165 if (!parsedType) { 166 table.unlockBag(entry); 167 return nullptr; 168 } 169 170 Attribute::Symbol symbol; 171 symbol.symbol.name = ResourceName( 172 StringPiece16(entryName.package, entryName.packageLen), 173 *parsedType, 174 StringPiece16(entryName.name, entryName.nameLen)); 175 symbol.symbol.id = ResourceId(mapEntry.name.ident); 176 symbol.value = mapEntry.value.data; 177 s->attribute->symbols.push_back(std::move(symbol)); 178 } 179 } 180 table.unlockBag(entry); 181 return s; 182} 183 184std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::findByName( 185 const ResourceName& name) { 186 const android::ResTable& table = mAssets.getResources(false); 187 StringPiece16 typeStr = toString(name.type); 188 uint32_t typeSpecFlags = 0; 189 ResourceId resId = table.identifierForName(name.entry.data(), name.entry.size(), 190 typeStr.data(), typeStr.size(), 191 name.package.data(), name.package.size(), 192 &typeSpecFlags); 193 if (!resId.isValid()) { 194 return {}; 195 } 196 197 std::unique_ptr<SymbolTable::Symbol> s; 198 if (name.type == ResourceType::kAttr) { 199 s = lookupAttributeInTable(table, resId); 200 } else { 201 s = util::make_unique<SymbolTable::Symbol>(); 202 s->id = resId; 203 } 204 205 if (s) { 206 s->isPublic = (typeSpecFlags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0; 207 return s; 208 } 209 return {}; 210} 211 212static Maybe<ResourceName> getResourceName(const android::ResTable& table, ResourceId id) { 213 android::ResTable::resource_name resName = {}; 214 if (!table.getResourceName(id.id, true, &resName)) { 215 return {}; 216 } 217 218 ResourceName name; 219 if (resName.package) { 220 name.package = StringPiece16(resName.package, resName.packageLen).toString(); 221 } 222 223 const ResourceType* type; 224 if (resName.type) { 225 type = parseResourceType(StringPiece16(resName.type, resName.typeLen)); 226 227 } else if (resName.type8) { 228 type = parseResourceType(util::utf8ToUtf16(StringPiece(resName.type8, resName.typeLen))); 229 } else { 230 return {}; 231 } 232 233 if (!type) { 234 return {}; 235 } 236 237 name.type = *type; 238 239 if (resName.name) { 240 name.entry = StringPiece16(resName.name, resName.nameLen).toString(); 241 } else if (resName.name8) { 242 name.entry = util::utf8ToUtf16(StringPiece(resName.name8, resName.nameLen)); 243 } else { 244 return {}; 245 } 246 247 return name; 248} 249 250std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::findById(ResourceId id) { 251 const android::ResTable& table = mAssets.getResources(false); 252 Maybe<ResourceName> maybeName = getResourceName(table, id); 253 if (!maybeName) { 254 return {}; 255 } 256 257 uint32_t typeSpecFlags = 0; 258 table.getResourceFlags(id.id, &typeSpecFlags); 259 260 std::unique_ptr<SymbolTable::Symbol> s; 261 if (maybeName.value().type == ResourceType::kAttr) { 262 s = lookupAttributeInTable(table, id); 263 } else { 264 s = util::make_unique<SymbolTable::Symbol>(); 265 s->id = id; 266 } 267 268 if (s) { 269 s->isPublic = (typeSpecFlags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0; 270 return s; 271 } 272 return {}; 273} 274 275} // namespace aapt 276