blob: cb589ec41034ab385938c7deee7759bc20f9d228 [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
21#include <cstddef>
22#include <limits>
23
24#include "android-base/logging.h"
25#include "android-base/stringprintf.h"
26#include "utils/ByteOrder.h"
27#include "utils/Trace.h"
28
29#ifdef _WIN32
30#ifdef ERROR
31#undef ERROR
32#endif
33#endif
34
Adam Lesinski7ad11102016-10-28 16:39:15 -070035#include "androidfw/ByteBucketArray.h"
Adam Lesinskida431a22016-12-29 16:08:16 -050036#include "androidfw/Chunk.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070037#include "androidfw/Util.h"
38
39using android::base::StringPrintf;
40
41namespace android {
42
Adam Lesinskida431a22016-12-29 16:08:16 -050043constexpr const static int kAppPackageId = 0x7f;
Adam Lesinski7ad11102016-10-28 16:39:15 -070044
45// Element of a TypeSpec array. See TypeSpec.
46struct Type {
47 // The configuration for which this type defines entries.
48 // This is already converted to host endianness.
49 ResTable_config configuration;
50
51 // Pointer to the mmapped data where entry definitions are kept.
52 const ResTable_type* type;
53};
54
55// TypeSpec is going to be immediately proceeded by
56// an array of Type structs, all in the same block of memory.
57struct TypeSpec {
58 // Pointer to the mmapped data where flags are kept.
59 // Flags denote whether the resource entry is public
60 // and under which configurations it varies.
61 const ResTable_typeSpec* type_spec;
62
63 // The number of types that follow this struct.
64 // There is a type for each configuration
65 // that entries are defined for.
66 size_t type_count;
67
68 // Trick to easily access a variable number of Type structs
69 // proceeding this struct, and to ensure their alignment.
70 const Type types[0];
71};
72
73// TypeSpecPtr points to the block of memory that holds
74// a TypeSpec struct, followed by an array of Type structs.
75// TypeSpecPtr is a managed pointer that knows how to delete
76// itself.
77using TypeSpecPtr = util::unique_cptr<TypeSpec>;
78
Adam Lesinskida431a22016-12-29 16:08:16 -050079namespace {
80
Adam Lesinski7ad11102016-10-28 16:39:15 -070081// Builder that helps accumulate Type structs and then create a single
82// contiguous block of memory to store both the TypeSpec struct and
83// the Type structs.
84class TypeSpecPtrBuilder {
85 public:
86 TypeSpecPtrBuilder(const ResTable_typeSpec* header) : header_(header) {}
87
88 void AddType(const ResTable_type* type) {
89 ResTable_config config;
90 config.copyFromDtoH(type->config);
91 types_.push_back(Type{config, type});
92 }
93
94 TypeSpecPtr Build() {
95 // Check for overflow.
96 if ((std::numeric_limits<size_t>::max() - sizeof(TypeSpec)) / sizeof(Type) < types_.size()) {
97 return {};
98 }
99 TypeSpec* type_spec = (TypeSpec*)::malloc(sizeof(TypeSpec) + (types_.size() * sizeof(Type)));
100 type_spec->type_spec = header_;
101 type_spec->type_count = types_.size();
102 memcpy(type_spec + 1, types_.data(), types_.size() * sizeof(Type));
103 return TypeSpecPtr(type_spec);
104 }
105
106 private:
107 DISALLOW_COPY_AND_ASSIGN(TypeSpecPtrBuilder);
108
109 const ResTable_typeSpec* header_;
110 std::vector<Type> types_;
111};
112
113} // namespace
114
Adam Lesinskida431a22016-12-29 16:08:16 -0500115bool LoadedPackage::FindEntry(uint8_t type_idx, uint16_t entry_idx, const ResTable_config& config,
116 LoadedArscEntry* out_entry, ResTable_config* out_selected_config,
Adam Lesinski7ad11102016-10-28 16:39:15 -0700117 uint32_t* out_flags) const {
Adam Lesinskida431a22016-12-29 16:08:16 -0500118 ATRACE_CALL();
Adam Lesinskic6aada92017-01-13 15:34:14 -0800119
120 // If the type IDs are offset in this package, we need to take that into account when searching
121 // for a type.
122 const TypeSpecPtr& ptr = type_specs_[type_idx - type_id_offset_];
Adam Lesinski7ad11102016-10-28 16:39:15 -0700123 if (ptr == nullptr) {
124 return false;
125 }
126
127 // Don't bother checking if the entry ID is larger than
128 // the number of entries.
Adam Lesinskida431a22016-12-29 16:08:16 -0500129 if (entry_idx >= dtohl(ptr->type_spec->entryCount)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700130 return false;
131 }
132
133 const ResTable_config* best_config = nullptr;
134 const ResTable_type* best_type = nullptr;
135 uint32_t best_offset = 0;
136
137 for (uint32_t i = 0; i < ptr->type_count; i++) {
138 const Type* type = &ptr->types[i];
139
140 if (type->configuration.match(config) &&
141 (best_config == nullptr || type->configuration.isBetterThan(*best_config, &config))) {
142 // The configuration matches and is better than the previous selection.
143 // Find the entry value if it exists for this configuration.
144 size_t entry_count = dtohl(type->type->entryCount);
Adam Lesinskida431a22016-12-29 16:08:16 -0500145 if (entry_idx < entry_count) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700146 const uint32_t* entry_offsets = reinterpret_cast<const uint32_t*>(
147 reinterpret_cast<const uint8_t*>(type->type) + dtohs(type->type->header.headerSize));
Adam Lesinskida431a22016-12-29 16:08:16 -0500148 const uint32_t offset = dtohl(entry_offsets[entry_idx]);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700149 if (offset != ResTable_type::NO_ENTRY) {
150 // There is an entry for this resource, record it.
151 best_config = &type->configuration;
152 best_type = type->type;
153 best_offset = offset + dtohl(type->type->entriesStart);
154 }
155 }
156 }
157 }
158
159 if (best_type == nullptr) {
160 return false;
161 }
162
163 const uint32_t* flags = reinterpret_cast<const uint32_t*>(ptr->type_spec + 1);
Adam Lesinskida431a22016-12-29 16:08:16 -0500164 *out_flags = dtohl(flags[entry_idx]);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700165 *out_selected_config = *best_config;
166
167 const ResTable_entry* best_entry = reinterpret_cast<const ResTable_entry*>(
168 reinterpret_cast<const uint8_t*>(best_type) + best_offset);
169 out_entry->entry = best_entry;
170 out_entry->type_string_ref = StringPoolRef(&type_string_pool_, best_type->id - 1);
171 out_entry->entry_string_ref = StringPoolRef(&key_string_pool_, dtohl(best_entry->key.index));
172 return true;
173}
174
175// The destructor gets generated into arbitrary translation units
176// if left implicit, which causes the compiler to complain about
177// forward declarations and incomplete types.
178LoadedArsc::~LoadedArsc() {}
179
Adam Lesinskida431a22016-12-29 16:08:16 -0500180bool LoadedArsc::FindEntry(uint32_t resid, const ResTable_config& config,
181 LoadedArscEntry* out_entry, ResTable_config* out_selected_config,
182 uint32_t* out_flags) const {
183 ATRACE_CALL();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700184 const uint8_t package_id = util::get_package_id(resid);
185 const uint8_t type_id = util::get_type_id(resid);
186 const uint16_t entry_id = util::get_entry_id(resid);
187
188 if (type_id == 0) {
189 LOG(ERROR) << "Invalid ID 0x" << std::hex << resid << std::dec << ".";
190 return false;
191 }
192
193 for (const auto& loaded_package : packages_) {
194 if (loaded_package->package_id_ == package_id) {
195 return loaded_package->FindEntry(type_id - 1, entry_id, config, out_entry,
196 out_selected_config, out_flags);
197 }
198 }
199 return false;
200}
201
Adam Lesinskida431a22016-12-29 16:08:16 -0500202const LoadedPackage* LoadedArsc::GetPackageForId(uint32_t resid) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700203 const uint8_t package_id = util::get_package_id(resid);
204 for (const auto& loaded_package : packages_) {
205 if (loaded_package->package_id_ == package_id) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500206 return loaded_package.get();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700207 }
208 }
209 return nullptr;
210}
211
212static bool VerifyType(const Chunk& chunk) {
213 ATRACE_CALL();
214 const ResTable_type* header = chunk.header<ResTable_type>();
215
216 const size_t entry_count = dtohl(header->entryCount);
217 if (entry_count > std::numeric_limits<uint16_t>::max()) {
218 LOG(ERROR) << "Too many entries in RES_TABLE_TYPE_TYPE.";
219 return false;
220 }
221
222 // Make sure that there is enough room for the entry offsets.
223 const size_t offsets_offset = chunk.header_size();
224 const size_t entries_offset = dtohl(header->entriesStart);
225 const size_t offsets_length = sizeof(uint32_t) * entry_count;
226
227 if (offsets_offset + offsets_length > entries_offset) {
228 LOG(ERROR) << "Entry offsets overlap actual entry data.";
229 return false;
230 }
231
232 if (entries_offset > chunk.size()) {
233 LOG(ERROR) << "Entry offsets extend beyond chunk.";
234 return false;
235 }
236
237 if (entries_offset & 0x03) {
238 LOG(ERROR) << "Entries start at unaligned address.";
239 return false;
240 }
241
242 // Check each entry offset.
243 const uint32_t* offsets =
244 reinterpret_cast<const uint32_t*>(reinterpret_cast<const uint8_t*>(header) + offsets_offset);
245 for (size_t i = 0; i < entry_count; i++) {
246 uint32_t offset = dtohl(offsets[i]);
247 if (offset != ResTable_type::NO_ENTRY) {
248 // Check that the offset is aligned.
249 if (offset & 0x03) {
250 LOG(ERROR) << "Entry offset at index " << i << " is not 4-byte aligned.";
251 return false;
252 }
253
254 // Check that the offset doesn't overflow.
255 if (offset > std::numeric_limits<uint32_t>::max() - entries_offset) {
256 // Overflow in offset.
257 LOG(ERROR) << "Entry offset at index " << i << " is too large.";
258 return false;
259 }
260
261 offset += entries_offset;
262 if (offset > chunk.size() - sizeof(ResTable_entry)) {
263 LOG(ERROR) << "Entry offset at index " << i << " is too large. No room for ResTable_entry.";
264 return false;
265 }
266
267 const ResTable_entry* entry = reinterpret_cast<const ResTable_entry*>(
268 reinterpret_cast<const uint8_t*>(header) + offset);
269 const size_t entry_size = dtohs(entry->size);
270 if (entry_size < sizeof(*entry)) {
271 LOG(ERROR) << "ResTable_entry size " << entry_size << " is too small.";
272 return false;
273 }
274
275 // Check the declared entrySize.
276 if (entry_size > chunk.size() || offset > chunk.size() - entry_size) {
277 LOG(ERROR) << "ResTable_entry size " << entry_size << " is too large.";
278 return false;
279 }
280
281 // If this is a map entry, then keep validating.
282 if (entry_size >= sizeof(ResTable_map_entry)) {
283 const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(entry);
284 const size_t map_entry_count = dtohl(map->count);
285
286 size_t map_entries_start = offset + entry_size;
287 if (map_entries_start & 0x03) {
288 LOG(ERROR) << "Map entries start at unaligned offset.";
289 return false;
290 }
291
292 // Each entry is sizeof(ResTable_map) big.
293 if (map_entry_count > ((chunk.size() - map_entries_start) / sizeof(ResTable_map))) {
294 LOG(ERROR) << "Too many map entries in ResTable_map_entry.";
295 return false;
296 }
297
298 // Great, all the map entries fit!.
299 } else {
300 // There needs to be room for one Res_value struct.
301 if (offset + entry_size > chunk.size() - sizeof(Res_value)) {
302 LOG(ERROR) << "No room for Res_value after ResTable_entry.";
303 return false;
304 }
305
306 const Res_value* value = reinterpret_cast<const Res_value*>(
307 reinterpret_cast<const uint8_t*>(entry) + entry_size);
308 const size_t value_size = dtohs(value->size);
309 if (value_size < sizeof(Res_value)) {
310 LOG(ERROR) << "Res_value is too small.";
311 return false;
312 }
313
314 if (value_size > chunk.size() || offset + entry_size > chunk.size() - value_size) {
315 LOG(ERROR) << "Res_value size is too large.";
316 return false;
317 }
318 }
319 }
320 }
321 return true;
322}
323
Adam Lesinski0c405242017-01-13 20:47:26 -0800324void LoadedPackage::CollectConfigurations(bool exclude_mipmap,
325 std::set<ResTable_config>* out_configs) const {
326 const static std::u16string kMipMap = u"mipmap";
327 const size_t type_count = type_specs_.size();
328 for (size_t i = 0; i < type_count; i++) {
329 const util::unique_cptr<TypeSpec>& type_spec = type_specs_[i];
330 if (type_spec != nullptr) {
331 if (exclude_mipmap) {
332 const int type_idx = type_spec->type_spec->id - 1;
333 size_t type_name_len;
334 const char16_t* type_name16 = type_string_pool_.stringAt(type_idx, &type_name_len);
335 if (type_name16 != nullptr) {
336 if (kMipMap.compare(0, std::u16string::npos, type_name16, type_name_len) == 0) {
337 // This is a mipmap type, skip collection.
338 continue;
339 }
340 }
341 const char* type_name = type_string_pool_.string8At(type_idx, &type_name_len);
342 if (type_name != nullptr) {
343 if (strncmp(type_name, "mipmap", type_name_len) == 0) {
344 // This is a mipmap type, skip collection.
345 continue;
346 }
347 }
348 }
349
350 for (size_t j = 0; j < type_spec->type_count; j++) {
351 out_configs->insert(type_spec->types[j].configuration);
352 }
353 }
354 }
355}
356
357void LoadedPackage::CollectLocales(bool canonicalize, std::set<std::string>* out_locales) const {
358 char temp_locale[RESTABLE_MAX_LOCALE_LEN];
359 const size_t type_count = type_specs_.size();
360 for (size_t i = 0; i < type_count; i++) {
361 const util::unique_cptr<TypeSpec>& type_spec = type_specs_[i];
362 if (type_spec != nullptr) {
363 for (size_t j = 0; j < type_spec->type_count; j++) {
364 const ResTable_config& configuration = type_spec->types[j].configuration;
365 if (configuration.locale != 0) {
366 configuration.getBcp47Locale(temp_locale, canonicalize);
367 std::string locale(temp_locale);
368 out_locales->insert(std::move(locale));
369 }
370 }
371 }
372 }
373}
374
Adam Lesinskida431a22016-12-29 16:08:16 -0500375std::unique_ptr<LoadedPackage> LoadedPackage::Load(const Chunk& chunk) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700376 ATRACE_CALL();
Adam Lesinskida431a22016-12-29 16:08:16 -0500377 std::unique_ptr<LoadedPackage> loaded_package{new LoadedPackage()};
378
Adam Lesinski7ad11102016-10-28 16:39:15 -0700379 const ResTable_package* header = chunk.header<ResTable_package>();
380 if (header == nullptr) {
381 LOG(ERROR) << "Chunk RES_TABLE_PACKAGE_TYPE is too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500382 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700383 }
384
385 loaded_package->package_id_ = dtohl(header->id);
Adam Lesinskida431a22016-12-29 16:08:16 -0500386 if (loaded_package->package_id_ == 0) {
387 // Package ID of 0 means this is a shared library.
388 loaded_package->dynamic_ = true;
389 }
390
Adam Lesinskic6aada92017-01-13 15:34:14 -0800391 if (header->header.headerSize >= sizeof(ResTable_package)) {
392 uint32_t type_id_offset = dtohl(header->typeIdOffset);
393 if (type_id_offset > std::numeric_limits<uint8_t>::max()) {
394 LOG(ERROR) << "Type ID offset in RES_TABLE_PACKAGE_TYPE is too large.";
395 return {};
396 }
397 loaded_package->type_id_offset_ = static_cast<int>(type_id_offset);
398 }
399
Adam Lesinskida431a22016-12-29 16:08:16 -0500400 util::ReadUtf16StringFromDevice(header->name, arraysize(header->name),
401 &loaded_package->package_name_);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700402
403 // A TypeSpec builder. We use this to accumulate the set of Types
404 // available for a TypeSpec, and later build a single, contiguous block
405 // of memory that holds all the Types together with the TypeSpec.
406 std::unique_ptr<TypeSpecPtrBuilder> types_builder;
407
408 // Keep track of the last seen type index. Since type IDs are 1-based,
409 // this records their index, which is 0-based (type ID - 1).
410 uint8_t last_type_idx = 0;
411
412 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
413 while (iter.HasNext()) {
414 const Chunk child_chunk = iter.Next();
415 switch (child_chunk.type()) {
416 case RES_STRING_POOL_TYPE: {
417 const uintptr_t pool_address =
418 reinterpret_cast<uintptr_t>(child_chunk.header<ResChunk_header>());
419 const uintptr_t header_address = reinterpret_cast<uintptr_t>(header);
420 if (pool_address == header_address + dtohl(header->typeStrings)) {
421 // This string pool is the type string pool.
422 status_t err = loaded_package->type_string_pool_.setTo(
423 child_chunk.header<ResStringPool_header>(), child_chunk.size());
424 if (err != NO_ERROR) {
425 LOG(ERROR) << "Corrupt package type string pool.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500426 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700427 }
428 } else if (pool_address == header_address + dtohl(header->keyStrings)) {
429 // This string pool is the key string pool.
430 status_t err = loaded_package->key_string_pool_.setTo(
431 child_chunk.header<ResStringPool_header>(), child_chunk.size());
432 if (err != NO_ERROR) {
433 LOG(ERROR) << "Corrupt package key string pool.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500434 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700435 }
436 } else {
437 LOG(WARNING) << "Too many string pool chunks found in package.";
438 }
439 } break;
440
441 case RES_TABLE_TYPE_SPEC_TYPE: {
442 ATRACE_NAME("LoadTableTypeSpec");
443
444 // Starting a new TypeSpec, so finish the old one if there was one.
445 if (types_builder) {
446 TypeSpecPtr type_spec_ptr = types_builder->Build();
447 if (type_spec_ptr == nullptr) {
448 LOG(ERROR) << "Too many type configurations, overflow detected.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500449 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700450 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700451 loaded_package->type_specs_.editItemAt(last_type_idx) = std::move(type_spec_ptr);
452
453 types_builder = {};
454 last_type_idx = 0;
455 }
456
457 const ResTable_typeSpec* type_spec = child_chunk.header<ResTable_typeSpec>();
458 if (type_spec == nullptr) {
459 LOG(ERROR) << "Chunk RES_TABLE_TYPE_SPEC_TYPE is too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500460 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700461 }
462
463 if (type_spec->id == 0) {
464 LOG(ERROR) << "Chunk RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500465 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700466 }
467
Adam Lesinskic6aada92017-01-13 15:34:14 -0800468 if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) >
469 std::numeric_limits<uint8_t>::max()) {
470 LOG(ERROR) << "Chunk RES_TABLE_TYPE_SPEC_TYPE has out of range ID.";
471 return {};
472 }
473
Adam Lesinski7ad11102016-10-28 16:39:15 -0700474 // The data portion of this chunk contains entry_count 32bit entries,
475 // each one representing a set of flags.
476 // Here we only validate that the chunk is well formed.
477 const size_t entry_count = dtohl(type_spec->entryCount);
478
479 // There can only be 2^16 entries in a type, because that is the ID
480 // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
481 if (entry_count > std::numeric_limits<uint16_t>::max()) {
482 LOG(ERROR) << "Too many entries in RES_TABLE_TYPE_SPEC_TYPE: " << entry_count << ".";
Adam Lesinskida431a22016-12-29 16:08:16 -0500483 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700484 }
485
486 if (entry_count * sizeof(uint32_t) > chunk.data_size()) {
487 LOG(ERROR) << "Chunk too small to hold entries in RES_TABLE_TYPE_SPEC_TYPE.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500488 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700489 }
490
491 last_type_idx = type_spec->id - 1;
492 types_builder = util::make_unique<TypeSpecPtrBuilder>(type_spec);
493 } break;
494
495 case RES_TABLE_TYPE_TYPE: {
496 const ResTable_type* type = child_chunk.header<ResTable_type>();
497 if (type == nullptr) {
498 LOG(ERROR) << "Chunk RES_TABLE_TYPE_TYPE is too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500499 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700500 }
501
502 if (type->id == 0) {
503 LOG(ERROR) << "Chunk RES_TABLE_TYPE_TYPE has invalid ID 0.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500504 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700505 }
506
507 // Type chunks must be preceded by their TypeSpec chunks.
508 if (!types_builder || type->id - 1 != last_type_idx) {
509 LOG(ERROR) << "Found RES_TABLE_TYPE_TYPE chunk without "
510 "RES_TABLE_TYPE_SPEC_TYPE.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500511 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700512 }
513
514 if (!VerifyType(child_chunk)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500515 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700516 }
517
518 types_builder->AddType(type);
519 } break;
520
Adam Lesinskida431a22016-12-29 16:08:16 -0500521 case RES_TABLE_LIBRARY_TYPE: {
522 const ResTable_lib_header* lib = child_chunk.header<ResTable_lib_header>();
523 if (lib == nullptr) {
524 LOG(ERROR) << "Chunk RES_TABLE_LIBRARY_TYPE is too small.";
525 return {};
526 }
527
528 if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) {
529 LOG(ERROR) << "Chunk too small to hold entries in RES_TABLE_LIBRARY_TYPE.";
530 return {};
531 }
532
533 loaded_package->dynamic_package_map_.reserve(dtohl(lib->count));
534
535 const ResTable_lib_entry* const entry_begin =
536 reinterpret_cast<const ResTable_lib_entry*>(child_chunk.data_ptr());
537 const ResTable_lib_entry* const entry_end = entry_begin + dtohl(lib->count);
538 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
539 std::string package_name;
540 util::ReadUtf16StringFromDevice(entry_iter->packageName,
541 arraysize(entry_iter->packageName), &package_name);
542
543 if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) {
544 LOG(ERROR) << base::StringPrintf(
545 "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.",
546 dtohl(entry_iter->packageId), package_name.c_str());
547 return {};
548 }
549
550 loaded_package->dynamic_package_map_.emplace_back(std::move(package_name),
551 dtohl(entry_iter->packageId));
552 }
553
554 } break;
555
Adam Lesinski7ad11102016-10-28 16:39:15 -0700556 default:
557 LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type());
558 break;
559 }
560 }
561
562 // Finish the last TypeSpec.
563 if (types_builder) {
564 TypeSpecPtr type_spec_ptr = types_builder->Build();
565 if (type_spec_ptr == nullptr) {
566 LOG(ERROR) << "Too many type configurations, overflow detected.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500567 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700568 }
569 loaded_package->type_specs_.editItemAt(last_type_idx) = std::move(type_spec_ptr);
570 }
571
572 if (iter.HadError()) {
573 LOG(ERROR) << iter.GetLastError();
Adam Lesinskida431a22016-12-29 16:08:16 -0500574 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700575 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500576 return loaded_package;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700577}
578
Adam Lesinskida431a22016-12-29 16:08:16 -0500579bool LoadedArsc::LoadTable(const Chunk& chunk, bool load_as_shared_library) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700580 ATRACE_CALL();
581 const ResTable_header* header = chunk.header<ResTable_header>();
582 if (header == nullptr) {
583 LOG(ERROR) << "Chunk RES_TABLE_TYPE is too small.";
584 return false;
585 }
586
587 const size_t package_count = dtohl(header->packageCount);
588 size_t packages_seen = 0;
589
590 packages_.reserve(package_count);
591
592 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
593 while (iter.HasNext()) {
594 const Chunk child_chunk = iter.Next();
595 switch (child_chunk.type()) {
596 case RES_STRING_POOL_TYPE:
597 // Only use the first string pool. Ignore others.
598 if (global_string_pool_.getError() == NO_INIT) {
599 status_t err = global_string_pool_.setTo(child_chunk.header<ResStringPool_header>(),
600 child_chunk.size());
601 if (err != NO_ERROR) {
602 LOG(ERROR) << "Corrupt string pool.";
603 return false;
604 }
605 } else {
606 LOG(WARNING) << "Multiple string pool chunks found in resource table.";
607 }
608 break;
609
610 case RES_TABLE_PACKAGE_TYPE: {
611 if (packages_seen + 1 > package_count) {
612 LOG(ERROR) << "More package chunks were found than the " << package_count
613 << " declared in the "
614 "header.";
615 return false;
616 }
617 packages_seen++;
618
Adam Lesinskida431a22016-12-29 16:08:16 -0500619 std::unique_ptr<LoadedPackage> loaded_package = LoadedPackage::Load(child_chunk);
620 if (!loaded_package) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700621 return false;
622 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500623
624 // Mark the package as dynamic if we are forcefully loading the Apk as a shared library.
625 if (loaded_package->package_id_ == kAppPackageId) {
626 loaded_package->dynamic_ = load_as_shared_library;
627 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800628 loaded_package->system_ = system_;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700629 packages_.push_back(std::move(loaded_package));
630 } break;
631
632 default:
633 LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type());
634 break;
635 }
636 }
637
638 if (iter.HadError()) {
639 LOG(ERROR) << iter.GetLastError();
640 return false;
641 }
642 return true;
643}
644
Adam Lesinski0c405242017-01-13 20:47:26 -0800645std::unique_ptr<const LoadedArsc> LoadedArsc::Load(const void* data, size_t len, bool system,
646 bool load_as_shared_library) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700647 ATRACE_CALL();
648
649 // Not using make_unique because the constructor is private.
650 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
Adam Lesinski0c405242017-01-13 20:47:26 -0800651 loaded_arsc->system_ = system;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700652
653 ChunkIterator iter(data, len);
654 while (iter.HasNext()) {
655 const Chunk chunk = iter.Next();
656 switch (chunk.type()) {
657 case RES_TABLE_TYPE:
Adam Lesinskida431a22016-12-29 16:08:16 -0500658 if (!loaded_arsc->LoadTable(chunk, load_as_shared_library)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700659 return {};
660 }
661 break;
662
663 default:
664 LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type());
665 break;
666 }
667 }
668
669 if (iter.HadError()) {
670 LOG(ERROR) << iter.GetLastError();
671 return {};
672 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800673
674 // Need to force a move for mingw32.
675 return std::move(loaded_arsc);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700676}
677
678} // namespace android