blob: e08848f891f6b6b20f0c186074c866b892e8e772 [file] [log] [blame]
Adam Lesinski7ad11102016-10-28 16:39:15 -07001/*
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 Lesinski73f6f9d2017-11-14 10:18:05 -080021#include <algorithm>
Adam Lesinski7ad11102016-10-28 16:39:15 -070022#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 Lesinski7ad11102016-10-28 16:39:15 -070036#include "androidfw/ByteBucketArray.h"
Adam Lesinskida431a22016-12-29 16:08:16 -050037#include "androidfw/Chunk.h"
Adam Lesinski929d6512017-01-16 19:11:19 -080038#include "androidfw/ResourceUtils.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070039#include "androidfw/Util.h"
40
Adam Lesinski970bd8d2017-09-25 13:21:55 -070041using ::android::base::StringPrintf;
Adam Lesinski7ad11102016-10-28 16:39:15 -070042
43namespace android {
44
Adam Lesinskida431a22016-12-29 16:08:16 -050045constexpr const static int kAppPackageId = 0x7f;
Adam Lesinski7ad11102016-10-28 16:39:15 -070046
Adam Lesinskib8b3a262018-02-09 11:01:45 -080047// Element of a TypeSpec array. See TypeSpec.
48struct 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.
59struct 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.
83using TypeSpecPtr = util::unique_cptr<TypeSpec>;
84
Adam Lesinskida431a22016-12-29 16:08:16 -050085namespace {
86
Adam Lesinski7ad11102016-10-28 16:39:15 -070087// 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.
90class TypeSpecPtrBuilder {
91 public:
Adam Lesinski970bd8d2017-09-25 13:21:55 -070092 explicit TypeSpecPtrBuilder(const ResTable_typeSpec* header,
93 const IdmapEntry_header* idmap_header)
94 : header_(header), idmap_header_(idmap_header) {
95 }
Adam Lesinski7ad11102016-10-28 16:39:15 -070096
97 void AddType(const ResTable_type* type) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -080098 ResTable_config config;
99 config.copyFromDtoH(type->config);
100 types_.push_back(Type{config, type});
Adam Lesinski7ad11102016-10-28 16:39:15 -0700101 }
102
103 TypeSpecPtr Build() {
104 // Check for overflow.
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800105 if ((std::numeric_limits<size_t>::max() - sizeof(TypeSpec)) / sizeof(Type) < types_.size()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700106 return {};
107 }
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800108 TypeSpec* type_spec = (TypeSpec*)::malloc(sizeof(TypeSpec) + (types_.size() * sizeof(Type)));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700109 type_spec->type_spec = header_;
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700110 type_spec->idmap_entries = idmap_header_;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700111 type_spec->type_count = types_.size();
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800112 memcpy(type_spec + 1, types_.data(), types_.size() * sizeof(Type));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700113 return TypeSpecPtr(type_spec);
114 }
115
116 private:
117 DISALLOW_COPY_AND_ASSIGN(TypeSpecPtrBuilder);
118
119 const ResTable_typeSpec* header_;
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700120 const IdmapEntry_header* idmap_header_;
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800121 std::vector<Type> types_;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700122};
123
124} // namespace
125
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700126LoadedPackage::LoadedPackage() = default;
127LoadedPackage::~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.
131static bool VerifyResTableType(const ResTable_type* header) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800132 if (header->id == 0) {
133 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has invalid ID 0.";
134 return false;
135 }
136
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700137 const size_t entry_count = dtohl(header->entryCount);
138 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800139 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has too many entries (" << entry_count << ").";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700140 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 Lesinski498f6052017-11-29 13:24:29 -0800149 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700150 return false;
151 }
152
153 if (entries_offset > dtohl(header->header.size)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800154 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets extend beyond chunk.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700155 return false;
156 }
157
158 if (entries_offset & 0x03) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800159 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entries start at unaligned address.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700160 return false;
161 }
162 return true;
163}
164
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800165static bool VerifyResTableEntry(const ResTable_type* type, uint32_t entry_offset,
166 size_t entry_idx) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700167 // Check that the offset is aligned.
168 if (entry_offset & 0x03) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800169 LOG(ERROR) << "Entry offset at index " << entry_idx << " is not 4-byte aligned.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700170 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 Lesinskib8b3a262018-02-09 11:01:45 -0800176 LOG(ERROR) << "Entry offset at index " << entry_idx << " is too large.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700177 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 Lesinskib8b3a262018-02-09 11:01:45 -0800184 LOG(ERROR) << "Entry offset at index " << entry_idx
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700185 << " 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 Lesinskib8b3a262018-02-09 11:01:45 -0800194 LOG(ERROR) << "ResTable_entry size " << entry_size << " at index " << entry_idx
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700195 << " is too small.";
196 return false;
197 }
198
199 if (entry_size > chunk_size || entry_offset > chunk_size - entry_size) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800200 LOG(ERROR) << "ResTable_entry size " << entry_size << " at index " << entry_idx
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700201 << " 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 Lesinskib8b3a262018-02-09 11:01:45 -0800208 LOG(ERROR) << "No room for Res_value after ResTable_entry at index " << entry_idx
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700209 << " 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 Lesinskib8b3a262018-02-09 11:01:45 -0800217 LOG(ERROR) << "Res_value at index " << entry_idx << " is too small.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700218 return false;
219 }
220
221 if (value_size > chunk_size || entry_offset + entry_size > chunk_size - value_size) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800222 LOG(ERROR) << "Res_value size " << value_size << " at index " << entry_idx
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700223 << " 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 Lesinskib8b3a262018-02-09 11:01:45 -0800231 LOG(ERROR) << "Map entries at index " << entry_idx << " start at unaligned offset.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700232 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 Lesinskib8b3a262018-02-09 11:01:45 -0800237 LOG(ERROR) << "Too many map entries in ResTable_map_entry at index " << entry_idx << ".";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700238 return false;
239 }
240 }
241 return true;
242}
243
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800244bool 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 Lesinski1a1e9c22017-10-13 15:45:34 -0700249
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800250 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 Lesinski1a1e9c22017-10-13 15:45:34 -0700253
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800254 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 Lesinski73f6f9d2017-11-14 10:18:05 -0800260
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800261 // 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 Lesinski73f6f9d2017-11-14 10:18:05 -0800290 reinterpret_cast<const uint8_t*>(type_chunk) + offsets_offset);
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800291 const uint32_t offset = dtohl(entry_offsets[entry_idx]);
292 if (offset == ResTable_type::NO_ENTRY) {
293 continue;
294 }
Adam Lesinski73f6f9d2017-11-14 10:18:05 -0800295
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800296 // 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 Lesinski1a1e9c22017-10-13 15:45:34 -0700302 }
303 }
304
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800305 if (best_type == nullptr) {
306 return false;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700307 }
308
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800309 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 Lesinski1a1e9c22017-10-13 15:45:34 -0700323}
324
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800325bool LoadedPackage::FindEntry(uint8_t type_idx, uint16_t entry_idx, const ResTable_config& config,
326 FindEntryResult* out_entry) const {
327 // If the type IDs are offset in this package, we need to take that into account when searching
328 // for a type.
329 const TypeSpecPtr& ptr = type_specs_[type_idx - type_id_offset_];
330 if (UNLIKELY(ptr == nullptr)) {
331 return false;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700332 }
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800333
334 // If there is an IDMAP supplied with this package, translate the entry ID.
335 if (ptr->idmap_entries != nullptr) {
336 if (!LoadedIdmap::Lookup(ptr->idmap_entries, entry_idx, &entry_idx)) {
337 // There is no mapping, so the resource is not meant to be in this overlay package.
338 return false;
339 }
340 }
341 return FindEntry(ptr, entry_idx, config, out_entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700342}
343
Adam Lesinski0c405242017-01-13 20:47:26 -0800344void LoadedPackage::CollectConfigurations(bool exclude_mipmap,
345 std::set<ResTable_config>* out_configs) const {
346 const static std::u16string kMipMap = u"mipmap";
347 const size_t type_count = type_specs_.size();
348 for (size_t i = 0; i < type_count; i++) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800349 const util::unique_cptr<TypeSpec>& type_spec = type_specs_[i];
Adam Lesinski0c405242017-01-13 20:47:26 -0800350 if (type_spec != nullptr) {
351 if (exclude_mipmap) {
352 const int type_idx = type_spec->type_spec->id - 1;
353 size_t type_name_len;
354 const char16_t* type_name16 = type_string_pool_.stringAt(type_idx, &type_name_len);
355 if (type_name16 != nullptr) {
356 if (kMipMap.compare(0, std::u16string::npos, type_name16, type_name_len) == 0) {
357 // This is a mipmap type, skip collection.
358 continue;
359 }
360 }
361 const char* type_name = type_string_pool_.string8At(type_idx, &type_name_len);
362 if (type_name != nullptr) {
363 if (strncmp(type_name, "mipmap", type_name_len) == 0) {
364 // This is a mipmap type, skip collection.
365 continue;
366 }
367 }
368 }
369
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800370 for (size_t j = 0; j < type_spec->type_count; j++) {
371 out_configs->insert(type_spec->types[j].configuration);
Adam Lesinski0c405242017-01-13 20:47:26 -0800372 }
373 }
374 }
375}
376
377void LoadedPackage::CollectLocales(bool canonicalize, std::set<std::string>* out_locales) const {
378 char temp_locale[RESTABLE_MAX_LOCALE_LEN];
379 const size_t type_count = type_specs_.size();
380 for (size_t i = 0; i < type_count; i++) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800381 const util::unique_cptr<TypeSpec>& type_spec = type_specs_[i];
Adam Lesinski0c405242017-01-13 20:47:26 -0800382 if (type_spec != nullptr) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800383 for (size_t j = 0; j < type_spec->type_count; j++) {
384 const ResTable_config& configuration = type_spec->types[j].configuration;
Adam Lesinski0c405242017-01-13 20:47:26 -0800385 if (configuration.locale != 0) {
386 configuration.getBcp47Locale(temp_locale, canonicalize);
387 std::string locale(temp_locale);
388 out_locales->insert(std::move(locale));
389 }
390 }
391 }
392 }
393}
394
Adam Lesinski929d6512017-01-16 19:11:19 -0800395uint32_t LoadedPackage::FindEntryByName(const std::u16string& type_name,
396 const std::u16string& entry_name) const {
397 ssize_t type_idx = type_string_pool_.indexOfString(type_name.data(), type_name.size());
398 if (type_idx < 0) {
399 return 0u;
400 }
401
402 ssize_t key_idx = key_string_pool_.indexOfString(entry_name.data(), entry_name.size());
403 if (key_idx < 0) {
404 return 0u;
405 }
406
407 const TypeSpec* type_spec = type_specs_[type_idx].get();
408 if (type_spec == nullptr) {
409 return 0u;
410 }
411
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800412 for (size_t ti = 0; ti < type_spec->type_count; ti++) {
413 const Type* type = &type_spec->types[ti];
414 size_t entry_count = dtohl(type->type->entryCount);
Adam Lesinski929d6512017-01-16 19:11:19 -0800415 for (size_t entry_idx = 0; entry_idx < entry_count; entry_idx++) {
416 const uint32_t* entry_offsets = reinterpret_cast<const uint32_t*>(
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800417 reinterpret_cast<const uint8_t*>(type->type) + dtohs(type->type->header.headerSize));
Adam Lesinski929d6512017-01-16 19:11:19 -0800418 const uint32_t offset = dtohl(entry_offsets[entry_idx]);
419 if (offset != ResTable_type::NO_ENTRY) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800420 const ResTable_entry* entry =
421 reinterpret_cast<const ResTable_entry*>(reinterpret_cast<const uint8_t*>(type->type) +
422 dtohl(type->type->entriesStart) + offset);
Adam Lesinski929d6512017-01-16 19:11:19 -0800423 if (dtohl(entry->key.index) == static_cast<uint32_t>(key_idx)) {
424 // The package ID will be overridden by the caller (due to runtime assignment of package
425 // IDs for shared libraries).
426 return make_resid(0x00, type_idx + type_id_offset_ + 1, entry_idx);
427 }
428 }
429 }
430 }
431 return 0u;
432}
433
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800434const LoadedPackage* LoadedArsc::GetPackageForId(uint32_t resid) const {
435 const uint8_t package_id = get_package_id(resid);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700436 for (const auto& loaded_package : packages_) {
437 if (loaded_package->GetPackageId() == package_id) {
438 return loaded_package.get();
439 }
440 }
441 return nullptr;
442}
443
444std::unique_ptr<const LoadedPackage> LoadedPackage::Load(const Chunk& chunk,
445 const LoadedIdmap* loaded_idmap,
446 bool system, bool load_as_shared_library) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700447 ATRACE_CALL();
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700448 std::unique_ptr<LoadedPackage> loaded_package(new LoadedPackage());
Adam Lesinskida431a22016-12-29 16:08:16 -0500449
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700450 // typeIdOffset was added at some point, but we still must recognize apps built before this
451 // was added.
Adam Lesinski33af6c72017-03-29 13:00:35 -0700452 constexpr size_t kMinPackageSize =
453 sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset);
454 const ResTable_package* header = chunk.header<ResTable_package, kMinPackageSize>();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700455 if (header == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800456 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500457 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700458 }
459
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700460 loaded_package->system_ = system;
461
Adam Lesinski7ad11102016-10-28 16:39:15 -0700462 loaded_package->package_id_ = dtohl(header->id);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700463 if (loaded_package->package_id_ == 0 ||
464 (loaded_package->package_id_ == kAppPackageId && load_as_shared_library)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500465 // Package ID of 0 means this is a shared library.
466 loaded_package->dynamic_ = true;
467 }
468
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700469 if (loaded_idmap != nullptr) {
470 // This is an overlay and so it needs to pretend to be the target package.
471 loaded_package->package_id_ = loaded_idmap->TargetPackageId();
472 loaded_package->overlay_ = true;
473 }
474
Adam Lesinskic6aada92017-01-13 15:34:14 -0800475 if (header->header.headerSize >= sizeof(ResTable_package)) {
476 uint32_t type_id_offset = dtohl(header->typeIdOffset);
477 if (type_id_offset > std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800478 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE type ID offset too large.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800479 return {};
480 }
481 loaded_package->type_id_offset_ = static_cast<int>(type_id_offset);
482 }
483
Adam Lesinskida431a22016-12-29 16:08:16 -0500484 util::ReadUtf16StringFromDevice(header->name, arraysize(header->name),
485 &loaded_package->package_name_);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700486
Adam Lesinskidfeb7ce2018-02-09 11:01:36 -0800487 // A TypeSpec builder. We use this to accumulate the set of Types
488 // available for a TypeSpec, and later build a single, contiguous block
489 // of memory that holds all the Types together with the TypeSpec.
490 std::unique_ptr<TypeSpecPtrBuilder> types_builder;
491
492 // Keep track of the last seen type index. Since type IDs are 1-based,
493 // this records their index, which is 0-based (type ID - 1).
494 uint8_t last_type_idx = 0;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700495
496 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
497 while (iter.HasNext()) {
498 const Chunk child_chunk = iter.Next();
499 switch (child_chunk.type()) {
500 case RES_STRING_POOL_TYPE: {
501 const uintptr_t pool_address =
502 reinterpret_cast<uintptr_t>(child_chunk.header<ResChunk_header>());
503 const uintptr_t header_address = reinterpret_cast<uintptr_t>(header);
504 if (pool_address == header_address + dtohl(header->typeStrings)) {
505 // This string pool is the type string pool.
506 status_t err = loaded_package->type_string_pool_.setTo(
507 child_chunk.header<ResStringPool_header>(), child_chunk.size());
508 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800509 LOG(ERROR) << "RES_STRING_POOL_TYPE for types corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500510 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700511 }
512 } else if (pool_address == header_address + dtohl(header->keyStrings)) {
513 // This string pool is the key string pool.
514 status_t err = loaded_package->key_string_pool_.setTo(
515 child_chunk.header<ResStringPool_header>(), child_chunk.size());
516 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800517 LOG(ERROR) << "RES_STRING_POOL_TYPE for keys corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500518 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700519 }
520 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800521 LOG(WARNING) << "Too many RES_STRING_POOL_TYPEs found in RES_TABLE_PACKAGE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700522 }
523 } break;
524
525 case RES_TABLE_TYPE_SPEC_TYPE: {
526 ATRACE_NAME("LoadTableTypeSpec");
527
Adam Lesinskidfeb7ce2018-02-09 11:01:36 -0800528 // Starting a new TypeSpec, so finish the old one if there was one.
529 if (types_builder) {
530 TypeSpecPtr type_spec_ptr = types_builder->Build();
531 if (type_spec_ptr == nullptr) {
532 LOG(ERROR) << "Too many type configurations, overflow detected.";
533 return {};
534 }
535
536 // We only add the type to the package if there is no IDMAP, or if the type is
537 // overlaying something.
538 if (loaded_idmap == nullptr || type_spec_ptr->idmap_entries != nullptr) {
539 // If this is an overlay, insert it at the target type ID.
540 if (type_spec_ptr->idmap_entries != nullptr) {
541 last_type_idx = dtohs(type_spec_ptr->idmap_entries->target_type_id) - 1;
542 }
543 loaded_package->type_specs_.editItemAt(last_type_idx) = std::move(type_spec_ptr);
544 }
545
546 types_builder = {};
547 last_type_idx = 0;
548 }
549
Adam Lesinski7ad11102016-10-28 16:39:15 -0700550 const ResTable_typeSpec* type_spec = child_chunk.header<ResTable_typeSpec>();
551 if (type_spec == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800552 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500553 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700554 }
555
556 if (type_spec->id == 0) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800557 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500558 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700559 }
560
Adam Lesinskic6aada92017-01-13 15:34:14 -0800561 if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) >
562 std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800563 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has out of range ID.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800564 return {};
565 }
566
Adam Lesinski7ad11102016-10-28 16:39:15 -0700567 // The data portion of this chunk contains entry_count 32bit entries,
568 // each one representing a set of flags.
569 // Here we only validate that the chunk is well formed.
570 const size_t entry_count = dtohl(type_spec->entryCount);
571
572 // There can only be 2^16 entries in a type, because that is the ID
573 // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
574 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800575 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has too many entries (" << entry_count << ").";
Adam Lesinskida431a22016-12-29 16:08:16 -0500576 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700577 }
578
579 if (entry_count * sizeof(uint32_t) > chunk.data_size()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800580 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500581 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700582 }
583
Adam Lesinskidfeb7ce2018-02-09 11:01:36 -0800584 last_type_idx = type_spec->id - 1;
585
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700586 // If this is an overlay, associate the mapping of this type to the target type
587 // from the IDMAP.
588 const IdmapEntry_header* idmap_entry_header = nullptr;
589 if (loaded_idmap != nullptr) {
590 idmap_entry_header = loaded_idmap->GetEntryMapForType(type_spec->id);
591 }
592
Adam Lesinskidfeb7ce2018-02-09 11:01:36 -0800593 types_builder = util::make_unique<TypeSpecPtrBuilder>(type_spec, idmap_entry_header);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700594 } break;
595
596 case RES_TABLE_TYPE_TYPE: {
Adam Lesinski136fd072017-03-03 13:50:21 -0800597 const ResTable_type* type = child_chunk.header<ResTable_type, kResTableTypeMinSize>();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700598 if (type == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800599 LOG(ERROR) << "RES_TABLE_TYPE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500600 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700601 }
602
Adam Lesinski498f6052017-11-29 13:24:29 -0800603 if (!VerifyResTableType(type)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500604 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700605 }
606
607 // Type chunks must be preceded by their TypeSpec chunks.
Adam Lesinskidfeb7ce2018-02-09 11:01:36 -0800608 if (!types_builder || type->id - 1 != last_type_idx) {
609 LOG(ERROR) << "RES_TABLE_TYPE_TYPE found without preceding RES_TABLE_TYPE_SPEC_TYPE.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500610 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700611 }
Adam Lesinskidfeb7ce2018-02-09 11:01:36 -0800612
613 types_builder->AddType(type);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700614 } break;
615
Adam Lesinskida431a22016-12-29 16:08:16 -0500616 case RES_TABLE_LIBRARY_TYPE: {
617 const ResTable_lib_header* lib = child_chunk.header<ResTable_lib_header>();
618 if (lib == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800619 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500620 return {};
621 }
622
623 if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800624 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500625 return {};
626 }
627
628 loaded_package->dynamic_package_map_.reserve(dtohl(lib->count));
629
630 const ResTable_lib_entry* const entry_begin =
631 reinterpret_cast<const ResTable_lib_entry*>(child_chunk.data_ptr());
632 const ResTable_lib_entry* const entry_end = entry_begin + dtohl(lib->count);
633 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
634 std::string package_name;
635 util::ReadUtf16StringFromDevice(entry_iter->packageName,
636 arraysize(entry_iter->packageName), &package_name);
637
638 if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) {
Adam Lesinskidfeb7ce2018-02-09 11:01:36 -0800639 LOG(ERROR) << base::StringPrintf(
Adam Lesinskida431a22016-12-29 16:08:16 -0500640 "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.",
641 dtohl(entry_iter->packageId), package_name.c_str());
642 return {};
643 }
644
645 loaded_package->dynamic_package_map_.emplace_back(std::move(package_name),
646 dtohl(entry_iter->packageId));
647 }
648
649 } break;
650
Adam Lesinski7ad11102016-10-28 16:39:15 -0700651 default:
Adam Lesinskidfeb7ce2018-02-09 11:01:36 -0800652 LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700653 break;
654 }
655 }
656
Adam Lesinskidfeb7ce2018-02-09 11:01:36 -0800657 // Finish the last TypeSpec.
658 if (types_builder) {
659 TypeSpecPtr type_spec_ptr = types_builder->Build();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700660 if (type_spec_ptr == nullptr) {
661 LOG(ERROR) << "Too many type configurations, overflow detected.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500662 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700663 }
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700664
665 // We only add the type to the package if there is no IDMAP, or if the type is
666 // overlaying something.
667 if (loaded_idmap == nullptr || type_spec_ptr->idmap_entries != nullptr) {
668 // If this is an overlay, insert it at the target type ID.
669 if (type_spec_ptr->idmap_entries != nullptr) {
Adam Lesinskidfeb7ce2018-02-09 11:01:36 -0800670 last_type_idx = dtohs(type_spec_ptr->idmap_entries->target_type_id) - 1;
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700671 }
Adam Lesinskidfeb7ce2018-02-09 11:01:36 -0800672 loaded_package->type_specs_.editItemAt(last_type_idx) = std::move(type_spec_ptr);
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700673 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700674 }
675
Adam Lesinskidfeb7ce2018-02-09 11:01:36 -0800676 if (iter.HadError()) {
677 LOG(ERROR) << iter.GetLastError();
678 return {};
679 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700680 return std::move(loaded_package);
681}
682
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800683bool LoadedArsc::FindEntry(uint32_t resid, const ResTable_config& config,
684 FindEntryResult* out_entry) const {
685 ATRACE_CALL();
686
687 const uint8_t package_id = get_package_id(resid);
688 const uint8_t type_id = get_type_id(resid);
689 const uint16_t entry_id = get_entry_id(resid);
690
691 if (UNLIKELY(type_id == 0)) {
692 LOG(ERROR) << base::StringPrintf("Invalid ID 0x%08x.", resid);
693 return false;
694 }
695
696 for (const auto& loaded_package : packages_) {
697 if (loaded_package->GetPackageId() == package_id) {
698 return loaded_package->FindEntry(type_id - 1, entry_id, config, out_entry);
699 }
700 }
701 return false;
702}
Adam Lesinskidfeb7ce2018-02-09 11:01:36 -0800703
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700704bool LoadedArsc::LoadTable(const Chunk& chunk, const LoadedIdmap* loaded_idmap,
705 bool load_as_shared_library) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700706 ATRACE_CALL();
707 const ResTable_header* header = chunk.header<ResTable_header>();
708 if (header == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800709 LOG(ERROR) << "RES_TABLE_TYPE too small.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700710 return false;
711 }
712
713 const size_t package_count = dtohl(header->packageCount);
714 size_t packages_seen = 0;
715
716 packages_.reserve(package_count);
717
718 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
719 while (iter.HasNext()) {
720 const Chunk child_chunk = iter.Next();
721 switch (child_chunk.type()) {
722 case RES_STRING_POOL_TYPE:
723 // Only use the first string pool. Ignore others.
724 if (global_string_pool_.getError() == NO_INIT) {
725 status_t err = global_string_pool_.setTo(child_chunk.header<ResStringPool_header>(),
726 child_chunk.size());
727 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800728 LOG(ERROR) << "RES_STRING_POOL_TYPE corrupt.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700729 return false;
730 }
731 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800732 LOG(WARNING) << "Multiple RES_STRING_POOL_TYPEs found in RES_TABLE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700733 }
734 break;
735
736 case RES_TABLE_PACKAGE_TYPE: {
737 if (packages_seen + 1 > package_count) {
738 LOG(ERROR) << "More package chunks were found than the " << package_count
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700739 << " declared in the header.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700740 return false;
741 }
742 packages_seen++;
743
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700744 std::unique_ptr<const LoadedPackage> loaded_package =
745 LoadedPackage::Load(child_chunk, loaded_idmap, system_, load_as_shared_library);
Adam Lesinskida431a22016-12-29 16:08:16 -0500746 if (!loaded_package) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700747 return false;
748 }
749 packages_.push_back(std::move(loaded_package));
750 } break;
751
752 default:
Adam Lesinskidfeb7ce2018-02-09 11:01:36 -0800753 LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700754 break;
755 }
756 }
757
758 if (iter.HadError()) {
759 LOG(ERROR) << iter.GetLastError();
760 return false;
761 }
762 return true;
763}
764
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700765std::unique_ptr<const LoadedArsc> LoadedArsc::Load(const StringPiece& data,
766 const LoadedIdmap* loaded_idmap, bool system,
Adam Lesinski0c405242017-01-13 20:47:26 -0800767 bool load_as_shared_library) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700768 ATRACE_CALL();
769
770 // Not using make_unique because the constructor is private.
771 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
Adam Lesinski0c405242017-01-13 20:47:26 -0800772 loaded_arsc->system_ = system;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700773
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700774 ChunkIterator iter(data.data(), data.size());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700775 while (iter.HasNext()) {
776 const Chunk chunk = iter.Next();
777 switch (chunk.type()) {
778 case RES_TABLE_TYPE:
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700779 if (!loaded_arsc->LoadTable(chunk, loaded_idmap, load_as_shared_library)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700780 return {};
781 }
782 break;
783
784 default:
Adam Lesinskidfeb7ce2018-02-09 11:01:36 -0800785 LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700786 break;
787 }
788 }
789
790 if (iter.HadError()) {
791 LOG(ERROR) << iter.GetLastError();
792 return {};
793 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800794
795 // Need to force a move for mingw32.
796 return std::move(loaded_arsc);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700797}
798
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700799std::unique_ptr<const LoadedArsc> LoadedArsc::CreateEmpty() {
800 return std::unique_ptr<LoadedArsc>(new LoadedArsc());
801}
802
Adam Lesinski7ad11102016-10-28 16:39:15 -0700803} // namespace android