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