| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 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 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 17 | #include "process/SymbolTable.h" | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 |  | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 19 | #include <iostream> | 
 | 20 |  | 
 | 21 | #include "android-base/logging.h" | 
 | 22 | #include "android-base/stringprintf.h" | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 23 | #include "androidfw/Asset.h" | 
 | 24 | #include "androidfw/AssetManager2.h" | 
| Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 25 | #include "androidfw/ConfigDescription.h" | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | #include "androidfw/ResourceTypes.h" | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 27 | #include "androidfw/ResourceUtils.h" | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 28 |  | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 29 | #include "NameMangler.h" | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | #include "Resource.h" | 
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 31 | #include "ResourceUtils.h" | 
| Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 32 | #include "ValueVisitor.h" | 
| Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 33 | #include "trace/TraceBuffer.h" | 
| Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 34 | #include "util/Util.h" | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 |  | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 36 | using ::android::ApkAssets; | 
| Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 37 | using ::android::ConfigDescription; | 
| Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 38 | using ::android::StringPiece; | 
 | 39 | using ::android::StringPiece16; | 
| Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 40 |  | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 41 | namespace aapt { | 
 | 42 |  | 
| Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 43 | SymbolTable::SymbolTable(NameMangler* mangler) | 
 | 44 |     : mangler_(mangler), | 
 | 45 |       delegate_(util::make_unique<DefaultSymbolTableDelegate>()), | 
 | 46 |       cache_(200), | 
 | 47 |       id_cache_(200) { | 
 | 48 | } | 
 | 49 |  | 
 | 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 |  | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 58 | void SymbolTable::AppendSource(std::unique_ptr<ISymbolSource> source) { | 
 | 59 |   sources_.push_back(std::move(source)); | 
| Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 60 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 |   // We do not clear the cache, because sources earlier in the list take | 
 | 62 |   // precedent. | 
| Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 63 | } | 
 | 64 |  | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | void SymbolTable::PrependSource(std::unique_ptr<ISymbolSource> source) { | 
 | 66 |   sources_.insert(sources_.begin(), std::move(source)); | 
| Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 67 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 |   // We must clear the cache in case we did a lookup before adding this | 
 | 69 |   // resource. | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 |   cache_.clear(); | 
| Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 71 | } | 
 | 72 |  | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | const SymbolTable::Symbol* SymbolTable::FindByName(const ResourceName& name) { | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 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 | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 78 |   // and store it somewhere; we use the std::optional<> class to reserve storage. | 
 | 79 |   std::optional<ResourceName> name_with_package_impl; | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 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)) { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 87 |     return s.get(); | 
 | 88 |   } | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 89 |  | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 90 |   // The name was not found in the cache. Mangle it (if necessary) and find it in our sources. | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 91 |   // Again, here we use a std::optional<> object to reserve storage if we need to mangle. | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 92 |   const ResourceName* mangled_name = name_with_package; | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 93 |   std::optional<ResourceName> mangled_name_impl; | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 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 |  | 
| Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 99 |   std::unique_ptr<Symbol> symbol = delegate_->FindByName(*mangled_name, sources_); | 
 | 100 |   if (symbol == nullptr) { | 
 | 101 |     return nullptr; | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 102 |   } | 
| Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 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(); | 
| Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 120 | } | 
 | 121 |  | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 122 | const SymbolTable::Symbol* SymbolTable::FindById(const ResourceId& id) { | 
 | 123 |   if (const std::shared_ptr<Symbol>& s = id_cache_.get(id)) { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 124 |     return s.get(); | 
 | 125 |   } | 
| Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 126 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 |   // We did not find it in the cache, so look through the sources. | 
| Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 128 |   std::unique_ptr<Symbol> symbol = delegate_->FindById(id, sources_); | 
 | 129 |   if (symbol == nullptr) { | 
 | 130 |     return nullptr; | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 |   } | 
| Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 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(); | 
| Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 141 | } | 
 | 142 |  | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 143 | const SymbolTable::Symbol* SymbolTable::FindByReference(const Reference& ref) { | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 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 | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 147 |   // because the ID is cached too. | 
 | 148 |   // | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 149 |   // If we looked up by name first, a cache miss would mean we failed to lookup by name, then | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 |   // succeeded to lookup by ID. Subsequent lookups will miss then hit. | 
 | 151 |   const SymbolTable::Symbol* symbol = nullptr; | 
 | 152 |   if (ref.id) { | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 153 |     symbol = FindById(ref.id.value()); | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 154 |   } | 
| Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 155 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 156 |   if (ref.name && !symbol) { | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 157 |     symbol = FindByName(ref.name.value()); | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 158 |   } | 
 | 159 |   return symbol; | 
| Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 160 | } | 
 | 161 |  | 
| Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 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 |  | 
 | 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 |  | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 184 | std::unique_ptr<SymbolTable::Symbol> ResourceTableSymbolSource::FindByName( | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 185 |     const ResourceName& name) { | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 186 |   std::optional<ResourceTable::SearchResult> result = table_->FindResource(name); | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 187 |   if (!result) { | 
| Iurii Makhno | cff10ce | 2022-02-15 19:33:50 +0000 | [diff] [blame] | 188 |     if (name.type.type == ResourceType::kAttr) { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 189 |       // Recurse and try looking up a private attribute. | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 190 |       return FindByName(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry)); | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 191 |     } | 
 | 192 |     return {}; | 
 | 193 |   } | 
 | 194 |  | 
 | 195 |   ResourceTable::SearchResult sr = result.value(); | 
 | 196 |  | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 197 |   std::unique_ptr<SymbolTable::Symbol> symbol = util::make_unique<SymbolTable::Symbol>(); | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 198 |   symbol->is_public = (sr.entry->visibility.level == Visibility::Level::kPublic); | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 199 |  | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 200 |   if (sr.entry->id) { | 
 | 201 |     symbol->id = sr.entry->id.value(); | 
| Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 202 |     symbol->is_dynamic = | 
 | 203 |         (sr.entry->id.value().package_id() == 0) || sr.entry->visibility.staged_api; | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 204 |   } | 
 | 205 |  | 
| Iurii Makhno | cff10ce | 2022-02-15 19:33:50 +0000 | [diff] [blame] | 206 |   if (name.type.type == ResourceType::kAttr || name.type.type == ResourceType::kAttrPrivate) { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 207 |     const ConfigDescription kDefaultConfig; | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 208 |     ResourceConfigValue* config_value = sr.entry->FindValue(kDefaultConfig); | 
 | 209 |     if (config_value) { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 210 |       // This resource has an Attribute. | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 211 |       if (Attribute* attr = ValueCast<Attribute>(config_value->value.get())) { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 212 |         symbol->attribute = std::make_shared<Attribute>(*attr); | 
 | 213 |       } else { | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 214 |         return {}; | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 215 |       } | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 216 |     } | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 217 |   } | 
 | 218 |   return symbol; | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 219 | } | 
 | 220 |  | 
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 221 | bool AssetManagerSymbolSource::AddAssetPath(StringPiece path) { | 
| Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 222 |   TRACE_CALL(); | 
| Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 223 |   if (auto apk = ApkAssets::Load(path.data())) { | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 224 |     apk_assets_.push_back(std::move(apk)); | 
| Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 225 |     asset_manager_.SetApkAssets(apk_assets_); | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 226 |     return true; | 
 | 227 |   } | 
 | 228 |   return false; | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 229 | } | 
 | 230 |  | 
 | 231 | std::map<size_t, std::string> AssetManagerSymbolSource::GetAssignedPackageIds() const { | 
| Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 232 |   TRACE_CALL(); | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 233 |   std::map<size_t, std::string> package_map; | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 234 |   asset_manager_.ForEachPackage([&package_map](const std::string& name, uint8_t id) -> bool { | 
 | 235 |     package_map.insert(std::make_pair(id, name)); | 
 | 236 |     return true; | 
 | 237 |   }); | 
 | 238 |  | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 239 |   return package_map; | 
| Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 240 | } | 
 | 241 |  | 
| Ryan Mitchell | ee4a564 | 2019-10-16 08:32:55 -0700 | [diff] [blame] | 242 | bool AssetManagerSymbolSource::IsPackageDynamic(uint32_t packageId, | 
 | 243 |     const std::string& package_name) const { | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 244 |   if (packageId == 0) { | 
 | 245 |     return true; | 
 | 246 |   } | 
 | 247 |  | 
| Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 248 |   for (auto&& assets : apk_assets_) { | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 249 |     for (const std::unique_ptr<const android::LoadedPackage>& loaded_package | 
 | 250 |          : assets->GetLoadedArsc()->GetPackages()) { | 
| Ryan Mitchell | ee4a564 | 2019-10-16 08:32:55 -0700 | [diff] [blame] | 251 |       if (package_name == loaded_package->GetPackageName() && loaded_package->IsDynamic()) { | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 252 |         return true; | 
 | 253 |       } | 
 | 254 |     } | 
 | 255 |   } | 
 | 256 |  | 
 | 257 |   return false; | 
| Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 258 | } | 
 | 259 |  | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | static std::unique_ptr<SymbolTable::Symbol> LookupAttributeInTable( | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 261 |     android::AssetManager2& am, ResourceId id) { | 
| Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 262 |   using namespace android; | 
| Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 263 |   if (am.GetApkAssetsCount() == 0) { | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 264 |     return {}; | 
 | 265 |   } | 
 | 266 |  | 
| Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 267 |   auto op = am.StartOperation(); | 
| Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 268 |   auto bag_result = am.GetBag(id.id); | 
 | 269 |   if (!bag_result.has_value()) { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 270 |     return nullptr; | 
 | 271 |   } | 
 | 272 |  | 
 | 273 |   // We found a resource. | 
| Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 274 |   std::unique_ptr<SymbolTable::Symbol> s = util::make_unique<SymbolTable::Symbol>(id); | 
| Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 275 |   const ResolvedBag* bag = *bag_result; | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 276 |   const size_t count = bag->entry_count; | 
 | 277 |   for (uint32_t i = 0; i < count; i++) { | 
| Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 278 |     if (bag->entries[i].key == ResTable_map::ATTR_TYPE) { | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 279 |       s->attribute = std::make_shared<Attribute>(bag->entries[i].value.data); | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 280 |       break; | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 281 |     } | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 282 |   } | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 283 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 284 |   if (s->attribute) { | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 285 |     for (size_t i = 0; i < count; i++) { | 
| Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 286 |       const ResolvedBag::Entry& map_entry = bag->entries[i]; | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 287 |       if (Res_INTERNALID(map_entry.key)) { | 
 | 288 |         switch (map_entry.key) { | 
| Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 289 |           case ResTable_map::ATTR_MIN: | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 290 |             s->attribute->min_int = static_cast<int32_t>(map_entry.value.data); | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 291 |             break; | 
| Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 292 |           case ResTable_map::ATTR_MAX: | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 293 |             s->attribute->max_int = static_cast<int32_t>(map_entry.value.data); | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 294 |             break; | 
 | 295 |         } | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 296 |         continue; | 
 | 297 |       } | 
 | 298 |  | 
| Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 299 |       auto name = am.GetResourceName(map_entry.key); | 
 | 300 |       if (!name.has_value()) { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 301 |         return nullptr; | 
 | 302 |       } | 
 | 303 |  | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 304 |       std::optional<ResourceName> parsed_name = ResourceUtils::ToResourceName(*name); | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 305 |       if (!parsed_name) { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 306 |         return nullptr; | 
 | 307 |       } | 
 | 308 |  | 
 | 309 |       Attribute::Symbol symbol; | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 310 |       symbol.symbol.name = parsed_name.value(); | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 311 |       symbol.symbol.id = ResourceId(map_entry.key); | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 312 |       symbol.value = map_entry.value.data; | 
| Ryan Mitchell | c167680 | 2019-05-20 16:47:08 -0700 | [diff] [blame] | 313 |       symbol.type = map_entry.value.dataType; | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 314 |       s->attribute->symbols.push_back(std::move(symbol)); | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 315 |     } | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 316 |   } | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 317 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 318 |   return s; | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 319 | } | 
 | 320 |  | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 321 | std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByName( | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 322 |     const ResourceName& name) { | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 323 |   const std::string mangled_entry = NameMangler::MangleEntry(name.package, name.entry); | 
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 324 |  | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 325 |   bool found = false; | 
 | 326 |   ResourceId res_id = 0; | 
| Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 327 |   uint32_t type_spec_flags = 0; | 
| Ryan Mitchell | ee4a564 | 2019-10-16 08:32:55 -0700 | [diff] [blame] | 328 |   ResourceName real_name; | 
| Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 329 |  | 
 | 330 |   // There can be mangled resources embedded within other packages. Here we will | 
 | 331 |   // look into each package and look-up the mangled name until we find the resource. | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 332 |   asset_manager_.ForEachPackage([&](const std::string& package_name, uint8_t id) -> bool { | 
| Ryan Mitchell | ee4a564 | 2019-10-16 08:32:55 -0700 | [diff] [blame] | 333 |     real_name = ResourceName(name.package, name.type, name.entry); | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 334 |     if (package_name != name.package) { | 
 | 335 |       real_name.entry = mangled_entry; | 
 | 336 |       real_name.package = package_name; | 
| Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 337 |     } | 
 | 338 |  | 
| Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 339 |     auto real_res_id = asset_manager_.GetResourceId(real_name.to_string()); | 
 | 340 |     if (!real_res_id.has_value()) { | 
 | 341 |       return true; | 
 | 342 |     } | 
 | 343 |  | 
 | 344 |     res_id.id = *real_res_id; | 
 | 345 |     if (!res_id.is_valid_static()) { | 
 | 346 |       return true; | 
 | 347 |     } | 
 | 348 |  | 
| Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 349 |     auto flags = asset_manager_.GetResourceTypeSpecFlags(res_id.id); | 
 | 350 |     if (flags.has_value()) { | 
 | 351 |       type_spec_flags = *flags; | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 352 |       found = true; | 
 | 353 |       return false; | 
| Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 354 |     } | 
| Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 355 |  | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 356 |     return true; | 
 | 357 |   }); | 
 | 358 |  | 
 | 359 |   if (!found) { | 
| Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 360 |     return {}; | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 361 |   } | 
 | 362 |  | 
 | 363 |   std::unique_ptr<SymbolTable::Symbol> s; | 
| Iurii Makhno | cff10ce | 2022-02-15 19:33:50 +0000 | [diff] [blame] | 364 |   if (real_name.type.type == ResourceType::kAttr) { | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 365 |     s = LookupAttributeInTable(asset_manager_, res_id); | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 366 |   } else { | 
 | 367 |     s = util::make_unique<SymbolTable::Symbol>(); | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 368 |     s->id = res_id; | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 369 |   } | 
 | 370 |  | 
 | 371 |   if (s) { | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 372 |     s->is_public = (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0; | 
| Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 373 |     s->is_dynamic = IsPackageDynamic(ResourceId(res_id).package_id(), real_name.package) || | 
 | 374 |                     (type_spec_flags & android::ResTable_typeSpec::SPEC_STAGED_API) != 0; | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 375 |     return s; | 
 | 376 |   } | 
 | 377 |   return {}; | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 378 | } | 
 | 379 |  | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 380 | static std::optional<ResourceName> GetResourceName(android::AssetManager2& am, ResourceId id) { | 
| Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 381 |   auto name = am.GetResourceName(id.id); | 
 | 382 |   if (!name.has_value()) { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 383 |     return {}; | 
 | 384 |   } | 
| Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 385 |   return ResourceUtils::ToResourceName(*name); | 
| Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 386 | } | 
 | 387 |  | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 388 | std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindById( | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 389 |     ResourceId id) { | 
| Ryan Mitchell | cd78feb | 2019-12-18 15:20:48 -0800 | [diff] [blame] | 390 |   if (!id.is_valid_static()) { | 
| Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 391 |     // Exit early and avoid the error logs from AssetManager. | 
 | 392 |     return {}; | 
 | 393 |   } | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 394 |  | 
 | 395 |   if (apk_assets_.empty()) { | 
 | 396 |     return {}; | 
 | 397 |   } | 
 | 398 |  | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 399 |   std::optional<ResourceName> maybe_name = GetResourceName(asset_manager_, id); | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 400 |   if (!maybe_name) { | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 401 |     return {}; | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 402 |   } | 
 | 403 |  | 
| Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 404 |   auto flags = asset_manager_.GetResourceTypeSpecFlags(id.id); | 
 | 405 |   if (!flags.has_value()) { | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 406 |     return {}; | 
 | 407 |   } | 
 | 408 |  | 
 | 409 |   ResourceName& name = maybe_name.value(); | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 410 |   std::unique_ptr<SymbolTable::Symbol> s; | 
| Iurii Makhno | cff10ce | 2022-02-15 19:33:50 +0000 | [diff] [blame] | 411 |   if (name.type.type == ResourceType::kAttr) { | 
| Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 412 |     s = LookupAttributeInTable(asset_manager_, id); | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 413 |   } else { | 
 | 414 |     s = util::make_unique<SymbolTable::Symbol>(); | 
 | 415 |     s->id = id; | 
 | 416 |   } | 
 | 417 |  | 
 | 418 |   if (s) { | 
| Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 419 |     s->is_public = (*flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0; | 
| Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 420 |     s->is_dynamic = IsPackageDynamic(ResourceId(id).package_id(), name.package) || | 
 | 421 |                     (*flags & android::ResTable_typeSpec::SPEC_STAGED_API) != 0; | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 422 |     return s; | 
 | 423 |   } | 
 | 424 |   return {}; | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 425 | } | 
 | 426 |  | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 427 | std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByReference( | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 428 |     const Reference& ref) { | 
 | 429 |   // AssetManager always prefers IDs. | 
 | 430 |   if (ref.id) { | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 431 |     return FindById(ref.id.value()); | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 432 |   } else if (ref.name) { | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 433 |     return FindByName(ref.name.value()); | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 434 |   } | 
 | 435 |   return {}; | 
| Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 436 | } | 
 | 437 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 438 | }  // namespace aapt |