Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #define ATRACE_TAG ATRACE_TAG_RESOURCES |
| 18 | |
| 19 | #include "androidfw/LoadedArsc.h" |
| 20 | |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 21 | #include <algorithm> |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 22 | #include <cstddef> |
| 23 | #include <limits> |
| 24 | |
| 25 | #include "android-base/logging.h" |
| 26 | #include "android-base/stringprintf.h" |
| 27 | #include "utils/ByteOrder.h" |
| 28 | #include "utils/Trace.h" |
| 29 | |
| 30 | #ifdef _WIN32 |
| 31 | #ifdef ERROR |
| 32 | #undef ERROR |
| 33 | #endif |
| 34 | #endif |
| 35 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 36 | #include "androidfw/ByteBucketArray.h" |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 37 | #include "androidfw/Chunk.h" |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 38 | #include "androidfw/ResourceUtils.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 39 | #include "androidfw/Util.h" |
| 40 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 41 | using ::android::base::StringPrintf; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 42 | |
| 43 | namespace android { |
| 44 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 45 | constexpr const static int kAppPackageId = 0x7f; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 47 | namespace { |
| 48 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 49 | // Builder that helps accumulate Type structs and then create a single |
| 50 | // contiguous block of memory to store both the TypeSpec struct and |
| 51 | // the Type structs. |
| 52 | class TypeSpecPtrBuilder { |
| 53 | public: |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame^] | 54 | explicit TypeSpecPtrBuilder(const ResTable_typeSpec* header) |
| 55 | : header_(header) { |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 56 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 57 | |
| 58 | void AddType(const ResTable_type* type) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 59 | types_.push_back(type); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | TypeSpecPtr Build() { |
| 63 | // Check for overflow. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 64 | using ElementType = const ResTable_type*; |
| 65 | if ((std::numeric_limits<size_t>::max() - sizeof(TypeSpec)) / sizeof(ElementType) < |
| 66 | types_.size()) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 67 | return {}; |
| 68 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 69 | TypeSpec* type_spec = |
| 70 | (TypeSpec*)::malloc(sizeof(TypeSpec) + (types_.size() * sizeof(ElementType))); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 71 | type_spec->type_spec = header_; |
| 72 | type_spec->type_count = types_.size(); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 73 | memcpy(type_spec + 1, types_.data(), types_.size() * sizeof(ElementType)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 74 | return TypeSpecPtr(type_spec); |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | DISALLOW_COPY_AND_ASSIGN(TypeSpecPtrBuilder); |
| 79 | |
| 80 | const ResTable_typeSpec* header_; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 81 | std::vector<const ResTable_type*> types_; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | } // namespace |
| 85 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 86 | LoadedPackage::LoadedPackage() = default; |
| 87 | LoadedPackage::~LoadedPackage() = default; |
| 88 | |
| 89 | // Precondition: The header passed in has already been verified, so reading any fields and trusting |
| 90 | // the ResChunk_header is safe. |
| 91 | static bool VerifyResTableType(const ResTable_type* header) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 92 | if (header->id == 0) { |
| 93 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE has invalid ID 0."; |
| 94 | return false; |
| 95 | } |
| 96 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 97 | const size_t entry_count = dtohl(header->entryCount); |
| 98 | if (entry_count > std::numeric_limits<uint16_t>::max()) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 99 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE has too many entries (" << entry_count << ")."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
| 103 | // Make sure that there is enough room for the entry offsets. |
| 104 | const size_t offsets_offset = dtohs(header->header.headerSize); |
| 105 | const size_t entries_offset = dtohl(header->entriesStart); |
| 106 | const size_t offsets_length = sizeof(uint32_t) * entry_count; |
| 107 | |
| 108 | if (offsets_offset > entries_offset || entries_offset - offsets_offset < offsets_length) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 109 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 110 | return false; |
| 111 | } |
| 112 | |
| 113 | if (entries_offset > dtohl(header->header.size)) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 114 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets extend beyond chunk."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | |
| 118 | if (entries_offset & 0x03) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 119 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE entries start at unaligned address."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 120 | return false; |
| 121 | } |
| 122 | return true; |
| 123 | } |
| 124 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 125 | static bool VerifyResTableEntry(const ResTable_type* type, uint32_t entry_offset) { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 126 | // Check that the offset is aligned. |
| 127 | if (entry_offset & 0x03) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 128 | LOG(ERROR) << "Entry at offset " << entry_offset << " is not 4-byte aligned."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 129 | return false; |
| 130 | } |
| 131 | |
| 132 | // Check that the offset doesn't overflow. |
| 133 | if (entry_offset > std::numeric_limits<uint32_t>::max() - dtohl(type->entriesStart)) { |
| 134 | // Overflow in offset. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 135 | LOG(ERROR) << "Entry at offset " << entry_offset << " is too large."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | |
| 139 | const size_t chunk_size = dtohl(type->header.size); |
| 140 | |
| 141 | entry_offset += dtohl(type->entriesStart); |
| 142 | if (entry_offset > chunk_size - sizeof(ResTable_entry)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 143 | LOG(ERROR) << "Entry at offset " << entry_offset |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 144 | << " is too large. No room for ResTable_entry."; |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | const ResTable_entry* entry = reinterpret_cast<const ResTable_entry*>( |
| 149 | reinterpret_cast<const uint8_t*>(type) + entry_offset); |
| 150 | |
| 151 | const size_t entry_size = dtohs(entry->size); |
| 152 | if (entry_size < sizeof(*entry)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 153 | LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 154 | << " is too small."; |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | if (entry_size > chunk_size || entry_offset > chunk_size - entry_size) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 159 | LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 160 | << " is too large."; |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | if (entry_size < sizeof(ResTable_map_entry)) { |
| 165 | // There needs to be room for one Res_value struct. |
| 166 | if (entry_offset + entry_size > chunk_size - sizeof(Res_value)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 167 | LOG(ERROR) << "No room for Res_value after ResTable_entry at offset " << entry_offset |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 168 | << " for type " << (int)type->id << "."; |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | const Res_value* value = |
| 173 | reinterpret_cast<const Res_value*>(reinterpret_cast<const uint8_t*>(entry) + entry_size); |
| 174 | const size_t value_size = dtohs(value->size); |
| 175 | if (value_size < sizeof(Res_value)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 176 | LOG(ERROR) << "Res_value at offset " << entry_offset << " is too small."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | |
| 180 | if (value_size > chunk_size || entry_offset + entry_size > chunk_size - value_size) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 181 | LOG(ERROR) << "Res_value size " << value_size << " at offset " << entry_offset |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 182 | << " is too large."; |
| 183 | return false; |
| 184 | } |
| 185 | } else { |
| 186 | const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(entry); |
| 187 | const size_t map_entry_count = dtohl(map->count); |
| 188 | size_t map_entries_start = entry_offset + entry_size; |
| 189 | if (map_entries_start & 0x03) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 190 | LOG(ERROR) << "Map entries at offset " << entry_offset << " start at unaligned offset."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 191 | return false; |
| 192 | } |
| 193 | |
| 194 | // Each entry is sizeof(ResTable_map) big. |
| 195 | if (map_entry_count > ((chunk_size - map_entries_start) / sizeof(ResTable_map))) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 196 | LOG(ERROR) << "Too many map entries in ResTable_map_entry at offset " << entry_offset << "."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | } |
| 200 | return true; |
| 201 | } |
| 202 | |
MÃ¥rten Kongstad | 3f1f4fc | 2018-03-02 09:34:18 +0100 | [diff] [blame] | 203 | LoadedPackage::iterator::iterator(const LoadedPackage* lp, size_t ti, size_t ei) |
| 204 | : loadedPackage_(lp), |
| 205 | typeIndex_(ti), |
| 206 | entryIndex_(ei), |
| 207 | typeIndexEnd_(lp->resource_ids_.size() + 1) { |
| 208 | while (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] == 0) { |
| 209 | typeIndex_++; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | LoadedPackage::iterator& LoadedPackage::iterator::operator++() { |
| 214 | while (typeIndex_ < typeIndexEnd_) { |
| 215 | if (entryIndex_ + 1 < loadedPackage_->resource_ids_[typeIndex_]) { |
| 216 | entryIndex_++; |
| 217 | break; |
| 218 | } |
| 219 | entryIndex_ = 0; |
| 220 | typeIndex_++; |
| 221 | if (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] != 0) { |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | return *this; |
| 226 | } |
| 227 | |
| 228 | uint32_t LoadedPackage::iterator::operator*() const { |
| 229 | if (typeIndex_ >= typeIndexEnd_) { |
| 230 | return 0; |
| 231 | } |
| 232 | return make_resid(loadedPackage_->package_id_, typeIndex_ + loadedPackage_->type_id_offset_, |
| 233 | entryIndex_); |
| 234 | } |
| 235 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 236 | const ResTable_entry* LoadedPackage::GetEntry(const ResTable_type* type_chunk, |
| 237 | uint16_t entry_index) { |
| 238 | uint32_t entry_offset = GetEntryOffset(type_chunk, entry_index); |
| 239 | if (entry_offset == ResTable_type::NO_ENTRY) { |
| 240 | return nullptr; |
| 241 | } |
| 242 | return GetEntryFromOffset(type_chunk, entry_offset); |
| 243 | } |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 244 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 245 | uint32_t LoadedPackage::GetEntryOffset(const ResTable_type* type_chunk, uint16_t entry_index) { |
| 246 | // The configuration matches and is better than the previous selection. |
| 247 | // Find the entry value if it exists for this configuration. |
| 248 | const size_t entry_count = dtohl(type_chunk->entryCount); |
| 249 | const size_t offsets_offset = dtohs(type_chunk->header.headerSize); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 250 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 251 | // Check if there is the desired entry in this type. |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 252 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 253 | if (type_chunk->flags & ResTable_type::FLAG_SPARSE) { |
| 254 | // This is encoded as a sparse map, so perform a binary search. |
| 255 | const ResTable_sparseTypeEntry* sparse_indices = |
| 256 | reinterpret_cast<const ResTable_sparseTypeEntry*>( |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 257 | reinterpret_cast<const uint8_t*>(type_chunk) + offsets_offset); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 258 | const ResTable_sparseTypeEntry* sparse_indices_end = sparse_indices + entry_count; |
| 259 | const ResTable_sparseTypeEntry* result = |
| 260 | std::lower_bound(sparse_indices, sparse_indices_end, entry_index, |
| 261 | [](const ResTable_sparseTypeEntry& entry, uint16_t entry_idx) { |
| 262 | return dtohs(entry.idx) < entry_idx; |
| 263 | }); |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 264 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 265 | if (result == sparse_indices_end || dtohs(result->idx) != entry_index) { |
| 266 | // No entry found. |
| 267 | return ResTable_type::NO_ENTRY; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 268 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 269 | |
| 270 | // Extract the offset from the entry. Each offset must be a multiple of 4 so we store it as |
| 271 | // the real offset divided by 4. |
| 272 | return uint32_t{dtohs(result->offset)} * 4u; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 275 | // This type is encoded as a dense array. |
| 276 | if (entry_index >= entry_count) { |
| 277 | // This entry cannot be here. |
| 278 | return ResTable_type::NO_ENTRY; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 281 | const uint32_t* entry_offsets = reinterpret_cast<const uint32_t*>( |
| 282 | reinterpret_cast<const uint8_t*>(type_chunk) + offsets_offset); |
| 283 | return dtohl(entry_offsets[entry_index]); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 286 | const ResTable_entry* LoadedPackage::GetEntryFromOffset(const ResTable_type* type_chunk, |
| 287 | uint32_t offset) { |
| 288 | if (UNLIKELY(!VerifyResTableEntry(type_chunk, offset))) { |
| 289 | return nullptr; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 290 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 291 | return reinterpret_cast<const ResTable_entry*>(reinterpret_cast<const uint8_t*>(type_chunk) + |
| 292 | offset + dtohl(type_chunk->entriesStart)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 295 | void LoadedPackage::CollectConfigurations(bool exclude_mipmap, |
| 296 | std::set<ResTable_config>* out_configs) const { |
| 297 | const static std::u16string kMipMap = u"mipmap"; |
| 298 | const size_t type_count = type_specs_.size(); |
| 299 | for (size_t i = 0; i < type_count; i++) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 300 | const TypeSpecPtr& type_spec = type_specs_[i]; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 301 | if (type_spec != nullptr) { |
| 302 | if (exclude_mipmap) { |
| 303 | const int type_idx = type_spec->type_spec->id - 1; |
| 304 | size_t type_name_len; |
| 305 | const char16_t* type_name16 = type_string_pool_.stringAt(type_idx, &type_name_len); |
| 306 | if (type_name16 != nullptr) { |
| 307 | if (kMipMap.compare(0, std::u16string::npos, type_name16, type_name_len) == 0) { |
| 308 | // This is a mipmap type, skip collection. |
| 309 | continue; |
| 310 | } |
| 311 | } |
| 312 | const char* type_name = type_string_pool_.string8At(type_idx, &type_name_len); |
| 313 | if (type_name != nullptr) { |
| 314 | if (strncmp(type_name, "mipmap", type_name_len) == 0) { |
| 315 | // This is a mipmap type, skip collection. |
| 316 | continue; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 321 | const auto iter_end = type_spec->types + type_spec->type_count; |
| 322 | for (auto iter = type_spec->types; iter != iter_end; ++iter) { |
| 323 | ResTable_config config; |
| 324 | config.copyFromDtoH((*iter)->config); |
| 325 | out_configs->insert(config); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | void LoadedPackage::CollectLocales(bool canonicalize, std::set<std::string>* out_locales) const { |
| 332 | char temp_locale[RESTABLE_MAX_LOCALE_LEN]; |
| 333 | const size_t type_count = type_specs_.size(); |
| 334 | for (size_t i = 0; i < type_count; i++) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 335 | const TypeSpecPtr& type_spec = type_specs_[i]; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 336 | if (type_spec != nullptr) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 337 | const auto iter_end = type_spec->types + type_spec->type_count; |
| 338 | for (auto iter = type_spec->types; iter != iter_end; ++iter) { |
| 339 | ResTable_config configuration; |
| 340 | configuration.copyFromDtoH((*iter)->config); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 341 | if (configuration.locale != 0) { |
| 342 | configuration.getBcp47Locale(temp_locale, canonicalize); |
| 343 | std::string locale(temp_locale); |
| 344 | out_locales->insert(std::move(locale)); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 351 | uint32_t LoadedPackage::FindEntryByName(const std::u16string& type_name, |
| 352 | const std::u16string& entry_name) const { |
| 353 | ssize_t type_idx = type_string_pool_.indexOfString(type_name.data(), type_name.size()); |
| 354 | if (type_idx < 0) { |
| 355 | return 0u; |
| 356 | } |
| 357 | |
| 358 | ssize_t key_idx = key_string_pool_.indexOfString(entry_name.data(), entry_name.size()); |
| 359 | if (key_idx < 0) { |
| 360 | return 0u; |
| 361 | } |
| 362 | |
| 363 | const TypeSpec* type_spec = type_specs_[type_idx].get(); |
| 364 | if (type_spec == nullptr) { |
| 365 | return 0u; |
| 366 | } |
| 367 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 368 | const auto iter_end = type_spec->types + type_spec->type_count; |
| 369 | for (auto iter = type_spec->types; iter != iter_end; ++iter) { |
| 370 | const ResTable_type* type = *iter; |
| 371 | size_t entry_count = dtohl(type->entryCount); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 372 | for (size_t entry_idx = 0; entry_idx < entry_count; entry_idx++) { |
| 373 | const uint32_t* entry_offsets = reinterpret_cast<const uint32_t*>( |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 374 | reinterpret_cast<const uint8_t*>(type) + dtohs(type->header.headerSize)); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 375 | const uint32_t offset = dtohl(entry_offsets[entry_idx]); |
| 376 | if (offset != ResTable_type::NO_ENTRY) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 377 | const ResTable_entry* entry = reinterpret_cast<const ResTable_entry*>( |
| 378 | reinterpret_cast<const uint8_t*>(type) + dtohl(type->entriesStart) + offset); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 379 | if (dtohl(entry->key.index) == static_cast<uint32_t>(key_idx)) { |
| 380 | // The package ID will be overridden by the caller (due to runtime assignment of package |
| 381 | // IDs for shared libraries). |
| 382 | return make_resid(0x00, type_idx + type_id_offset_ + 1, entry_idx); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | return 0u; |
| 388 | } |
| 389 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 390 | const LoadedPackage* LoadedArsc::GetPackageById(uint8_t package_id) const { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 391 | for (const auto& loaded_package : packages_) { |
| 392 | if (loaded_package->GetPackageId() == package_id) { |
| 393 | return loaded_package.get(); |
| 394 | } |
| 395 | } |
| 396 | return nullptr; |
| 397 | } |
| 398 | |
| 399 | std::unique_ptr<const LoadedPackage> LoadedPackage::Load(const Chunk& chunk, |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 400 | bool system, |
| 401 | bool load_as_shared_library, |
| 402 | bool for_loader) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 403 | ATRACE_NAME("LoadedPackage::Load"); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 404 | std::unique_ptr<LoadedPackage> loaded_package(new LoadedPackage()); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 405 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 406 | // typeIdOffset was added at some point, but we still must recognize apps built before this |
| 407 | // was added. |
Adam Lesinski | 33af6c7 | 2017-03-29 13:00:35 -0700 | [diff] [blame] | 408 | constexpr size_t kMinPackageSize = |
| 409 | sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset); |
| 410 | const ResTable_package* header = chunk.header<ResTable_package, kMinPackageSize>(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 411 | if (header == nullptr) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 412 | LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 413 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 416 | loaded_package->system_ = system; |
| 417 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 418 | loaded_package->package_id_ = dtohl(header->id); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 419 | if (loaded_package->package_id_ == 0 || |
| 420 | (loaded_package->package_id_ == kAppPackageId && load_as_shared_library)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 421 | // Package ID of 0 means this is a shared library. |
| 422 | loaded_package->dynamic_ = true; |
| 423 | } |
| 424 | |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 425 | if (for_loader) { |
| 426 | loaded_package->custom_loader_ = true; |
| 427 | } |
| 428 | |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 429 | if (header->header.headerSize >= sizeof(ResTable_package)) { |
| 430 | uint32_t type_id_offset = dtohl(header->typeIdOffset); |
| 431 | if (type_id_offset > std::numeric_limits<uint8_t>::max()) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 432 | LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE type ID offset too large."; |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 433 | return {}; |
| 434 | } |
| 435 | loaded_package->type_id_offset_ = static_cast<int>(type_id_offset); |
| 436 | } |
| 437 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 438 | util::ReadUtf16StringFromDevice(header->name, arraysize(header->name), |
| 439 | &loaded_package->package_name_); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 440 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 441 | // A map of TypeSpec builders, each associated with an type index. |
| 442 | // We use these to accumulate the set of Types available for a TypeSpec, and later build a single, |
| 443 | // contiguous block of memory that holds all the Types together with the TypeSpec. |
| 444 | std::unordered_map<int, std::unique_ptr<TypeSpecPtrBuilder>> type_builder_map; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 445 | |
| 446 | ChunkIterator iter(chunk.data_ptr(), chunk.data_size()); |
| 447 | while (iter.HasNext()) { |
| 448 | const Chunk child_chunk = iter.Next(); |
| 449 | switch (child_chunk.type()) { |
| 450 | case RES_STRING_POOL_TYPE: { |
| 451 | const uintptr_t pool_address = |
| 452 | reinterpret_cast<uintptr_t>(child_chunk.header<ResChunk_header>()); |
| 453 | const uintptr_t header_address = reinterpret_cast<uintptr_t>(header); |
| 454 | if (pool_address == header_address + dtohl(header->typeStrings)) { |
| 455 | // This string pool is the type string pool. |
| 456 | status_t err = loaded_package->type_string_pool_.setTo( |
| 457 | child_chunk.header<ResStringPool_header>(), child_chunk.size()); |
| 458 | if (err != NO_ERROR) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 459 | LOG(ERROR) << "RES_STRING_POOL_TYPE for types corrupt."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 460 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 461 | } |
| 462 | } else if (pool_address == header_address + dtohl(header->keyStrings)) { |
| 463 | // This string pool is the key string pool. |
| 464 | status_t err = loaded_package->key_string_pool_.setTo( |
| 465 | child_chunk.header<ResStringPool_header>(), child_chunk.size()); |
| 466 | if (err != NO_ERROR) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 467 | LOG(ERROR) << "RES_STRING_POOL_TYPE for keys corrupt."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 468 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 469 | } |
| 470 | } else { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 471 | LOG(WARNING) << "Too many RES_STRING_POOL_TYPEs found in RES_TABLE_PACKAGE_TYPE."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 472 | } |
| 473 | } break; |
| 474 | |
| 475 | case RES_TABLE_TYPE_SPEC_TYPE: { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 476 | const ResTable_typeSpec* type_spec = child_chunk.header<ResTable_typeSpec>(); |
| 477 | if (type_spec == nullptr) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 478 | LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 479 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | if (type_spec->id == 0) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 483 | LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 484 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 487 | if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) > |
| 488 | std::numeric_limits<uint8_t>::max()) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 489 | LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has out of range ID."; |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 490 | return {}; |
| 491 | } |
| 492 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 493 | // The data portion of this chunk contains entry_count 32bit entries, |
| 494 | // each one representing a set of flags. |
| 495 | // Here we only validate that the chunk is well formed. |
| 496 | const size_t entry_count = dtohl(type_spec->entryCount); |
| 497 | |
| 498 | // There can only be 2^16 entries in a type, because that is the ID |
| 499 | // space for entries (EEEE) in the resource ID 0xPPTTEEEE. |
| 500 | if (entry_count > std::numeric_limits<uint16_t>::max()) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 501 | LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has too many entries (" << entry_count << ")."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 502 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | if (entry_count * sizeof(uint32_t) > chunk.data_size()) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 506 | LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small to hold entries."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 507 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 510 | std::unique_ptr<TypeSpecPtrBuilder>& builder_ptr = type_builder_map[type_spec->id - 1]; |
| 511 | if (builder_ptr == nullptr) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame^] | 512 | builder_ptr = util::make_unique<TypeSpecPtrBuilder>(type_spec); |
MÃ¥rten Kongstad | 3f1f4fc | 2018-03-02 09:34:18 +0100 | [diff] [blame] | 513 | loaded_package->resource_ids_.set(type_spec->id, entry_count); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 514 | } else { |
| 515 | LOG(WARNING) << StringPrintf("RES_TABLE_TYPE_SPEC_TYPE already defined for ID %02x", |
| 516 | type_spec->id); |
| 517 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 518 | } break; |
| 519 | |
| 520 | case RES_TABLE_TYPE_TYPE: { |
Adam Lesinski | 136fd07 | 2017-03-03 13:50:21 -0800 | [diff] [blame] | 521 | const ResTable_type* type = child_chunk.header<ResTable_type, kResTableTypeMinSize>(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 522 | if (type == nullptr) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 523 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 524 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 527 | if (!VerifyResTableType(type)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 528 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | // Type chunks must be preceded by their TypeSpec chunks. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 532 | std::unique_ptr<TypeSpecPtrBuilder>& builder_ptr = type_builder_map[type->id - 1]; |
| 533 | if (builder_ptr != nullptr) { |
| 534 | builder_ptr->AddType(type); |
| 535 | } else { |
| 536 | LOG(ERROR) << StringPrintf( |
| 537 | "RES_TABLE_TYPE_TYPE with ID %02x found without preceding RES_TABLE_TYPE_SPEC_TYPE.", |
| 538 | type->id); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 539 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 540 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 541 | } break; |
| 542 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 543 | case RES_TABLE_LIBRARY_TYPE: { |
| 544 | const ResTable_lib_header* lib = child_chunk.header<ResTable_lib_header>(); |
| 545 | if (lib == nullptr) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 546 | LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 547 | return {}; |
| 548 | } |
| 549 | |
| 550 | if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 551 | LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small to hold entries."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 552 | return {}; |
| 553 | } |
| 554 | |
| 555 | loaded_package->dynamic_package_map_.reserve(dtohl(lib->count)); |
| 556 | |
| 557 | const ResTable_lib_entry* const entry_begin = |
| 558 | reinterpret_cast<const ResTable_lib_entry*>(child_chunk.data_ptr()); |
| 559 | const ResTable_lib_entry* const entry_end = entry_begin + dtohl(lib->count); |
| 560 | for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) { |
| 561 | std::string package_name; |
| 562 | util::ReadUtf16StringFromDevice(entry_iter->packageName, |
| 563 | arraysize(entry_iter->packageName), &package_name); |
| 564 | |
| 565 | if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 566 | LOG(ERROR) << StringPrintf( |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 567 | "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.", |
| 568 | dtohl(entry_iter->packageId), package_name.c_str()); |
| 569 | return {}; |
| 570 | } |
| 571 | |
| 572 | loaded_package->dynamic_package_map_.emplace_back(std::move(package_name), |
| 573 | dtohl(entry_iter->packageId)); |
| 574 | } |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 575 | } break; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 576 | |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 577 | case RES_TABLE_OVERLAYABLE_TYPE: { |
| 578 | const ResTable_overlayable_header* header = |
| 579 | child_chunk.header<ResTable_overlayable_header>(); |
| 580 | if (header == nullptr) { |
| 581 | LOG(ERROR) << "RES_TABLE_OVERLAYABLE_TYPE too small."; |
| 582 | return {}; |
| 583 | } |
| 584 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 585 | std::string name; |
| 586 | util::ReadUtf16StringFromDevice(header->name, arraysize(header->name), &name); |
| 587 | std::string actor; |
| 588 | util::ReadUtf16StringFromDevice(header->actor, arraysize(header->actor), &actor); |
| 589 | |
MÃ¥rten Kongstad | c92c4dd | 2019-02-05 01:29:59 +0100 | [diff] [blame] | 590 | if (loaded_package->overlayable_map_.find(name) != |
| 591 | loaded_package->overlayable_map_.end()) { |
| 592 | LOG(ERROR) << "Multiple <overlayable> blocks with the same name '" << name << "'."; |
| 593 | return {}; |
| 594 | } |
| 595 | loaded_package->overlayable_map_.emplace(name, actor); |
| 596 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 597 | // Iterate over the overlayable policy chunks contained within the overlayable chunk data |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 598 | ChunkIterator overlayable_iter(child_chunk.data_ptr(), child_chunk.data_size()); |
| 599 | while (overlayable_iter.HasNext()) { |
| 600 | const Chunk overlayable_child_chunk = overlayable_iter.Next(); |
| 601 | |
| 602 | switch (overlayable_child_chunk.type()) { |
| 603 | case RES_TABLE_OVERLAYABLE_POLICY_TYPE: { |
| 604 | const ResTable_overlayable_policy_header* policy_header = |
| 605 | overlayable_child_chunk.header<ResTable_overlayable_policy_header>(); |
| 606 | if (policy_header == nullptr) { |
| 607 | LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small."; |
| 608 | return {}; |
| 609 | } |
| 610 | |
| 611 | if ((overlayable_child_chunk.data_size() / sizeof(ResTable_ref)) |
| 612 | < dtohl(policy_header->entry_count)) { |
| 613 | LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small to hold entries."; |
| 614 | return {}; |
| 615 | } |
| 616 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 617 | // Retrieve all the resource ids belonging to this policy chunk |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 618 | std::unordered_set<uint32_t> ids; |
| 619 | const auto ids_begin = |
| 620 | reinterpret_cast<const ResTable_ref*>(overlayable_child_chunk.data_ptr()); |
| 621 | const auto ids_end = ids_begin + dtohl(policy_header->entry_count); |
| 622 | for (auto id_iter = ids_begin; id_iter != ids_end; ++id_iter) { |
| 623 | ids.insert(dtohl(id_iter->ident)); |
| 624 | } |
| 625 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 626 | // Add the pairing of overlayable properties and resource ids to the package |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 627 | OverlayableInfo overlayable_info{}; |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 628 | overlayable_info.name = name; |
| 629 | overlayable_info.actor = actor; |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 630 | overlayable_info.policy_flags = policy_header->policy_flags; |
| 631 | loaded_package->overlayable_infos_.push_back(std::make_pair(overlayable_info, ids)); |
Ryan Mitchell | 1982345 | 2019-01-29 12:01:24 -0800 | [diff] [blame] | 632 | loaded_package->defines_overlayable_ = true; |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 633 | break; |
| 634 | } |
| 635 | |
| 636 | default: |
| 637 | LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
| 638 | break; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | if (overlayable_iter.HadError()) { |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 643 | LOG(ERROR) << StringPrintf("Error parsing RES_TABLE_OVERLAYABLE_TYPE: %s", |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 644 | overlayable_iter.GetLastError().c_str()); |
| 645 | if (overlayable_iter.HadFatalError()) { |
| 646 | return {}; |
| 647 | } |
| 648 | } |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 649 | } break; |
| 650 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 651 | default: |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 652 | LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 653 | break; |
| 654 | } |
| 655 | } |
| 656 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 657 | if (iter.HadError()) { |
| 658 | LOG(ERROR) << iter.GetLastError(); |
Todd Kennedy | 28e663c | 2018-07-12 13:15:54 -0700 | [diff] [blame] | 659 | if (iter.HadFatalError()) { |
| 660 | return {}; |
| 661 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | // Flatten and construct the TypeSpecs. |
| 665 | for (auto& entry : type_builder_map) { |
| 666 | uint8_t type_idx = static_cast<uint8_t>(entry.first); |
| 667 | TypeSpecPtr type_spec_ptr = entry.second->Build(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 668 | if (type_spec_ptr == nullptr) { |
| 669 | LOG(ERROR) << "Too many type configurations, overflow detected."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 670 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 671 | } |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 672 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame^] | 673 | loaded_package->type_specs_.editItemAt(type_idx) = std::move(type_spec_ptr); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 676 | return std::move(loaded_package); |
| 677 | } |
| 678 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 679 | bool LoadedArsc::LoadTable(const Chunk& chunk, const LoadedIdmap* loaded_idmap, |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 680 | bool load_as_shared_library, bool for_loader) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 681 | const ResTable_header* header = chunk.header<ResTable_header>(); |
| 682 | if (header == nullptr) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 683 | LOG(ERROR) << "RES_TABLE_TYPE too small."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 684 | return false; |
| 685 | } |
| 686 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame^] | 687 | if (loaded_idmap != nullptr) { |
| 688 | global_string_pool_ = util::make_unique<OverlayStringPool>(loaded_idmap); |
| 689 | } |
| 690 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 691 | const size_t package_count = dtohl(header->packageCount); |
| 692 | size_t packages_seen = 0; |
| 693 | |
| 694 | packages_.reserve(package_count); |
| 695 | |
| 696 | ChunkIterator iter(chunk.data_ptr(), chunk.data_size()); |
| 697 | while (iter.HasNext()) { |
| 698 | const Chunk child_chunk = iter.Next(); |
| 699 | switch (child_chunk.type()) { |
| 700 | case RES_STRING_POOL_TYPE: |
| 701 | // Only use the first string pool. Ignore others. |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame^] | 702 | if (global_string_pool_->getError() == NO_INIT) { |
| 703 | status_t err = global_string_pool_->setTo(child_chunk.header<ResStringPool_header>(), |
| 704 | child_chunk.size()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 705 | if (err != NO_ERROR) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 706 | LOG(ERROR) << "RES_STRING_POOL_TYPE corrupt."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 707 | return false; |
| 708 | } |
| 709 | } else { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 710 | LOG(WARNING) << "Multiple RES_STRING_POOL_TYPEs found in RES_TABLE_TYPE."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 711 | } |
| 712 | break; |
| 713 | |
| 714 | case RES_TABLE_PACKAGE_TYPE: { |
| 715 | if (packages_seen + 1 > package_count) { |
| 716 | LOG(ERROR) << "More package chunks were found than the " << package_count |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 717 | << " declared in the header."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 718 | return false; |
| 719 | } |
| 720 | packages_seen++; |
| 721 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 722 | std::unique_ptr<const LoadedPackage> loaded_package = |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame^] | 723 | LoadedPackage::Load(child_chunk, system_, load_as_shared_library, for_loader); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 724 | if (!loaded_package) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 725 | return false; |
| 726 | } |
| 727 | packages_.push_back(std::move(loaded_package)); |
| 728 | } break; |
| 729 | |
| 730 | default: |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 731 | LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 732 | break; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | if (iter.HadError()) { |
| 737 | LOG(ERROR) << iter.GetLastError(); |
Todd Kennedy | 28e663c | 2018-07-12 13:15:54 -0700 | [diff] [blame] | 738 | if (iter.HadFatalError()) { |
| 739 | return false; |
| 740 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 741 | } |
| 742 | return true; |
| 743 | } |
| 744 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 745 | std::unique_ptr<const LoadedArsc> LoadedArsc::Load(const StringPiece& data, |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 746 | const LoadedIdmap* loaded_idmap, |
| 747 | bool system, |
| 748 | bool load_as_shared_library, |
| 749 | bool for_loader) { |
| 750 | ATRACE_NAME("LoadedArsc::Load"); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 751 | |
| 752 | // Not using make_unique because the constructor is private. |
| 753 | std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc()); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 754 | loaded_arsc->system_ = system; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 755 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 756 | ChunkIterator iter(data.data(), data.size()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 757 | while (iter.HasNext()) { |
| 758 | const Chunk chunk = iter.Next(); |
| 759 | switch (chunk.type()) { |
| 760 | case RES_TABLE_TYPE: |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 761 | if (!loaded_arsc->LoadTable(chunk, |
| 762 | loaded_idmap, |
| 763 | load_as_shared_library, |
| 764 | for_loader)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 765 | return {}; |
| 766 | } |
| 767 | break; |
| 768 | |
| 769 | default: |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 770 | LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 771 | break; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | if (iter.HadError()) { |
| 776 | LOG(ERROR) << iter.GetLastError(); |
Todd Kennedy | 28e663c | 2018-07-12 13:15:54 -0700 | [diff] [blame] | 777 | if (iter.HadFatalError()) { |
| 778 | return {}; |
| 779 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 780 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 781 | |
| 782 | // Need to force a move for mingw32. |
| 783 | return std::move(loaded_arsc); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 786 | std::unique_ptr<const LoadedArsc> LoadedArsc::CreateEmpty() { |
| 787 | return std::unique_ptr<LoadedArsc>(new LoadedArsc()); |
| 788 | } |
| 789 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 790 | } // namespace android |