blob: c7d0fa5b0cf3cdea95efcab7a4e6b8ab16d6de80 [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 Lesinskida431a22016-12-29 16:08:16 -0500324std::unique_ptr<LoadedPackage> LoadedPackage::Load(const Chunk& chunk) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700325 ATRACE_CALL();
Adam Lesinskida431a22016-12-29 16:08:16 -0500326 std::unique_ptr<LoadedPackage> loaded_package{new LoadedPackage()};
327
Adam Lesinski7ad11102016-10-28 16:39:15 -0700328 const ResTable_package* header = chunk.header<ResTable_package>();
329 if (header == nullptr) {
330 LOG(ERROR) << "Chunk RES_TABLE_PACKAGE_TYPE is too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500331 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700332 }
333
334 loaded_package->package_id_ = dtohl(header->id);
Adam Lesinskida431a22016-12-29 16:08:16 -0500335 if (loaded_package->package_id_ == 0) {
336 // Package ID of 0 means this is a shared library.
337 loaded_package->dynamic_ = true;
338 }
339
Adam Lesinskic6aada92017-01-13 15:34:14 -0800340 if (header->header.headerSize >= sizeof(ResTable_package)) {
341 uint32_t type_id_offset = dtohl(header->typeIdOffset);
342 if (type_id_offset > std::numeric_limits<uint8_t>::max()) {
343 LOG(ERROR) << "Type ID offset in RES_TABLE_PACKAGE_TYPE is too large.";
344 return {};
345 }
346 loaded_package->type_id_offset_ = static_cast<int>(type_id_offset);
347 }
348
Adam Lesinskida431a22016-12-29 16:08:16 -0500349 util::ReadUtf16StringFromDevice(header->name, arraysize(header->name),
350 &loaded_package->package_name_);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700351
352 // A TypeSpec builder. We use this to accumulate the set of Types
353 // available for a TypeSpec, and later build a single, contiguous block
354 // of memory that holds all the Types together with the TypeSpec.
355 std::unique_ptr<TypeSpecPtrBuilder> types_builder;
356
357 // Keep track of the last seen type index. Since type IDs are 1-based,
358 // this records their index, which is 0-based (type ID - 1).
359 uint8_t last_type_idx = 0;
360
361 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
362 while (iter.HasNext()) {
363 const Chunk child_chunk = iter.Next();
364 switch (child_chunk.type()) {
365 case RES_STRING_POOL_TYPE: {
366 const uintptr_t pool_address =
367 reinterpret_cast<uintptr_t>(child_chunk.header<ResChunk_header>());
368 const uintptr_t header_address = reinterpret_cast<uintptr_t>(header);
369 if (pool_address == header_address + dtohl(header->typeStrings)) {
370 // This string pool is the type string pool.
371 status_t err = loaded_package->type_string_pool_.setTo(
372 child_chunk.header<ResStringPool_header>(), child_chunk.size());
373 if (err != NO_ERROR) {
374 LOG(ERROR) << "Corrupt package type string pool.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500375 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700376 }
377 } else if (pool_address == header_address + dtohl(header->keyStrings)) {
378 // This string pool is the key string pool.
379 status_t err = loaded_package->key_string_pool_.setTo(
380 child_chunk.header<ResStringPool_header>(), child_chunk.size());
381 if (err != NO_ERROR) {
382 LOG(ERROR) << "Corrupt package key string pool.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500383 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700384 }
385 } else {
386 LOG(WARNING) << "Too many string pool chunks found in package.";
387 }
388 } break;
389
390 case RES_TABLE_TYPE_SPEC_TYPE: {
391 ATRACE_NAME("LoadTableTypeSpec");
392
393 // Starting a new TypeSpec, so finish the old one if there was one.
394 if (types_builder) {
395 TypeSpecPtr type_spec_ptr = types_builder->Build();
396 if (type_spec_ptr == nullptr) {
397 LOG(ERROR) << "Too many type configurations, overflow detected.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500398 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700399 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700400 loaded_package->type_specs_.editItemAt(last_type_idx) = std::move(type_spec_ptr);
401
402 types_builder = {};
403 last_type_idx = 0;
404 }
405
406 const ResTable_typeSpec* type_spec = child_chunk.header<ResTable_typeSpec>();
407 if (type_spec == nullptr) {
408 LOG(ERROR) << "Chunk RES_TABLE_TYPE_SPEC_TYPE is too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500409 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700410 }
411
412 if (type_spec->id == 0) {
413 LOG(ERROR) << "Chunk RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500414 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700415 }
416
Adam Lesinskic6aada92017-01-13 15:34:14 -0800417 if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) >
418 std::numeric_limits<uint8_t>::max()) {
419 LOG(ERROR) << "Chunk RES_TABLE_TYPE_SPEC_TYPE has out of range ID.";
420 return {};
421 }
422
Adam Lesinski7ad11102016-10-28 16:39:15 -0700423 // The data portion of this chunk contains entry_count 32bit entries,
424 // each one representing a set of flags.
425 // Here we only validate that the chunk is well formed.
426 const size_t entry_count = dtohl(type_spec->entryCount);
427
428 // There can only be 2^16 entries in a type, because that is the ID
429 // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
430 if (entry_count > std::numeric_limits<uint16_t>::max()) {
431 LOG(ERROR) << "Too many entries in RES_TABLE_TYPE_SPEC_TYPE: " << entry_count << ".";
Adam Lesinskida431a22016-12-29 16:08:16 -0500432 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700433 }
434
435 if (entry_count * sizeof(uint32_t) > chunk.data_size()) {
436 LOG(ERROR) << "Chunk too small to hold entries in RES_TABLE_TYPE_SPEC_TYPE.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500437 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700438 }
439
440 last_type_idx = type_spec->id - 1;
441 types_builder = util::make_unique<TypeSpecPtrBuilder>(type_spec);
442 } break;
443
444 case RES_TABLE_TYPE_TYPE: {
445 const ResTable_type* type = child_chunk.header<ResTable_type>();
446 if (type == nullptr) {
447 LOG(ERROR) << "Chunk RES_TABLE_TYPE_TYPE is too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500448 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700449 }
450
451 if (type->id == 0) {
452 LOG(ERROR) << "Chunk RES_TABLE_TYPE_TYPE has invalid ID 0.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500453 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700454 }
455
456 // Type chunks must be preceded by their TypeSpec chunks.
457 if (!types_builder || type->id - 1 != last_type_idx) {
458 LOG(ERROR) << "Found RES_TABLE_TYPE_TYPE chunk without "
459 "RES_TABLE_TYPE_SPEC_TYPE.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500460 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700461 }
462
463 if (!VerifyType(child_chunk)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500464 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700465 }
466
467 types_builder->AddType(type);
468 } break;
469
Adam Lesinskida431a22016-12-29 16:08:16 -0500470 case RES_TABLE_LIBRARY_TYPE: {
471 const ResTable_lib_header* lib = child_chunk.header<ResTable_lib_header>();
472 if (lib == nullptr) {
473 LOG(ERROR) << "Chunk RES_TABLE_LIBRARY_TYPE is too small.";
474 return {};
475 }
476
477 if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) {
478 LOG(ERROR) << "Chunk too small to hold entries in RES_TABLE_LIBRARY_TYPE.";
479 return {};
480 }
481
482 loaded_package->dynamic_package_map_.reserve(dtohl(lib->count));
483
484 const ResTable_lib_entry* const entry_begin =
485 reinterpret_cast<const ResTable_lib_entry*>(child_chunk.data_ptr());
486 const ResTable_lib_entry* const entry_end = entry_begin + dtohl(lib->count);
487 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
488 std::string package_name;
489 util::ReadUtf16StringFromDevice(entry_iter->packageName,
490 arraysize(entry_iter->packageName), &package_name);
491
492 if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) {
493 LOG(ERROR) << base::StringPrintf(
494 "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.",
495 dtohl(entry_iter->packageId), package_name.c_str());
496 return {};
497 }
498
499 loaded_package->dynamic_package_map_.emplace_back(std::move(package_name),
500 dtohl(entry_iter->packageId));
501 }
502
503 } break;
504
Adam Lesinski7ad11102016-10-28 16:39:15 -0700505 default:
506 LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type());
507 break;
508 }
509 }
510
511 // Finish the last TypeSpec.
512 if (types_builder) {
513 TypeSpecPtr type_spec_ptr = types_builder->Build();
514 if (type_spec_ptr == nullptr) {
515 LOG(ERROR) << "Too many type configurations, overflow detected.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500516 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700517 }
518 loaded_package->type_specs_.editItemAt(last_type_idx) = std::move(type_spec_ptr);
519 }
520
521 if (iter.HadError()) {
522 LOG(ERROR) << iter.GetLastError();
Adam Lesinskida431a22016-12-29 16:08:16 -0500523 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700524 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500525 return loaded_package;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700526}
527
Adam Lesinskida431a22016-12-29 16:08:16 -0500528bool LoadedArsc::LoadTable(const Chunk& chunk, bool load_as_shared_library) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700529 ATRACE_CALL();
530 const ResTable_header* header = chunk.header<ResTable_header>();
531 if (header == nullptr) {
532 LOG(ERROR) << "Chunk RES_TABLE_TYPE is too small.";
533 return false;
534 }
535
536 const size_t package_count = dtohl(header->packageCount);
537 size_t packages_seen = 0;
538
539 packages_.reserve(package_count);
540
541 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
542 while (iter.HasNext()) {
543 const Chunk child_chunk = iter.Next();
544 switch (child_chunk.type()) {
545 case RES_STRING_POOL_TYPE:
546 // Only use the first string pool. Ignore others.
547 if (global_string_pool_.getError() == NO_INIT) {
548 status_t err = global_string_pool_.setTo(child_chunk.header<ResStringPool_header>(),
549 child_chunk.size());
550 if (err != NO_ERROR) {
551 LOG(ERROR) << "Corrupt string pool.";
552 return false;
553 }
554 } else {
555 LOG(WARNING) << "Multiple string pool chunks found in resource table.";
556 }
557 break;
558
559 case RES_TABLE_PACKAGE_TYPE: {
560 if (packages_seen + 1 > package_count) {
561 LOG(ERROR) << "More package chunks were found than the " << package_count
562 << " declared in the "
563 "header.";
564 return false;
565 }
566 packages_seen++;
567
Adam Lesinskida431a22016-12-29 16:08:16 -0500568 std::unique_ptr<LoadedPackage> loaded_package = LoadedPackage::Load(child_chunk);
569 if (!loaded_package) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700570 return false;
571 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500572
573 // Mark the package as dynamic if we are forcefully loading the Apk as a shared library.
574 if (loaded_package->package_id_ == kAppPackageId) {
575 loaded_package->dynamic_ = load_as_shared_library;
576 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700577 packages_.push_back(std::move(loaded_package));
578 } break;
579
580 default:
581 LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type());
582 break;
583 }
584 }
585
586 if (iter.HadError()) {
587 LOG(ERROR) << iter.GetLastError();
588 return false;
589 }
590 return true;
591}
592
Adam Lesinskida431a22016-12-29 16:08:16 -0500593std::unique_ptr<LoadedArsc> LoadedArsc::Load(const void* data, size_t len,
594 bool load_as_shared_library) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700595 ATRACE_CALL();
596
597 // Not using make_unique because the constructor is private.
598 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
599
600 ChunkIterator iter(data, len);
601 while (iter.HasNext()) {
602 const Chunk chunk = iter.Next();
603 switch (chunk.type()) {
604 case RES_TABLE_TYPE:
Adam Lesinskida431a22016-12-29 16:08:16 -0500605 if (!loaded_arsc->LoadTable(chunk, load_as_shared_library)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700606 return {};
607 }
608 break;
609
610 default:
611 LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type());
612 break;
613 }
614 }
615
616 if (iter.HadError()) {
617 LOG(ERROR) << iter.GetLastError();
618 return {};
619 }
620 return loaded_arsc;
621}
622
623} // namespace android