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