blob: 28548e27baf050d32dfa466b4f41ffca718f480b [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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -0800305 if (best_type == nullptr) {
306 return false;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700307 }
308
Adam Lesinski50706b62018-01-23 03:16:16 -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 Lesinski50706b62018-01-23 03:16:16 -0800325bool LoadedPackage::FindEntry(uint8_t type_idx, uint16_t entry_idx, const ResTable_config& config,
326 FindEntryResult* out_entry) const {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800327 ATRACE_CALL();
328
Adam Lesinski50706b62018-01-23 03:16:16 -0800329 // 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 Lesinski7ad11102016-10-28 16:39:15 -0700334 }
Adam Lesinski50706b62018-01-23 03:16:16 -0800335
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 Lesinski7ad11102016-10-28 16:39:15 -0700344}
345
Adam Lesinski0c405242017-01-13 20:47:26 -0800346void 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 Lesinski50706b62018-01-23 03:16:16 -0800351 const util::unique_cptr<TypeSpec>& type_spec = type_specs_[i];
Adam Lesinski0c405242017-01-13 20:47:26 -0800352 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 Lesinski50706b62018-01-23 03:16:16 -0800372 for (size_t j = 0; j < type_spec->type_count; j++) {
373 out_configs->insert(type_spec->types[j].configuration);
Adam Lesinski0c405242017-01-13 20:47:26 -0800374 }
375 }
376 }
377}
378
379void 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 Lesinski50706b62018-01-23 03:16:16 -0800383 const util::unique_cptr<TypeSpec>& type_spec = type_specs_[i];
Adam Lesinski0c405242017-01-13 20:47:26 -0800384 if (type_spec != nullptr) {
Adam Lesinski50706b62018-01-23 03:16:16 -0800385 for (size_t j = 0; j < type_spec->type_count; j++) {
386 const ResTable_config& configuration = type_spec->types[j].configuration;
Adam Lesinski0c405242017-01-13 20:47:26 -0800387 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 Lesinski929d6512017-01-16 19:11:19 -0800397uint32_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 Lesinski50706b62018-01-23 03:16:16 -0800414 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 Lesinski929d6512017-01-16 19:11:19 -0800417 for (size_t entry_idx = 0; entry_idx < entry_count; entry_idx++) {
418 const uint32_t* entry_offsets = reinterpret_cast<const uint32_t*>(
Adam Lesinski50706b62018-01-23 03:16:16 -0800419 reinterpret_cast<const uint8_t*>(type->type) + dtohs(type->type->header.headerSize));
Adam Lesinski929d6512017-01-16 19:11:19 -0800420 const uint32_t offset = dtohl(entry_offsets[entry_idx]);
421 if (offset != ResTable_type::NO_ENTRY) {
Adam Lesinski50706b62018-01-23 03:16:16 -0800422 const ResTable_entry* entry =
423 reinterpret_cast<const ResTable_entry*>(reinterpret_cast<const uint8_t*>(type->type) +
424 dtohl(type->type->entriesStart) + offset);
Adam Lesinski929d6512017-01-16 19:11:19 -0800425 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 Lesinski50706b62018-01-23 03:16:16 -0800436const LoadedPackage* LoadedArsc::GetPackageForId(uint32_t resid) const {
437 const uint8_t package_id = get_package_id(resid);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700438 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
446std::unique_ptr<const LoadedPackage> LoadedPackage::Load(const Chunk& chunk,
447 const LoadedIdmap* loaded_idmap,
448 bool system, bool load_as_shared_library) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700449 ATRACE_CALL();
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700450 std::unique_ptr<LoadedPackage> loaded_package(new LoadedPackage());
Adam Lesinskida431a22016-12-29 16:08:16 -0500451
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700452 // typeIdOffset was added at some point, but we still must recognize apps built before this
453 // was added.
Adam Lesinski33af6c72017-03-29 13:00:35 -0700454 constexpr size_t kMinPackageSize =
455 sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset);
456 const ResTable_package* header = chunk.header<ResTable_package, kMinPackageSize>();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700457 if (header == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800458 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500459 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700460 }
461
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700462 loaded_package->system_ = system;
463
Adam Lesinski7ad11102016-10-28 16:39:15 -0700464 loaded_package->package_id_ = dtohl(header->id);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700465 if (loaded_package->package_id_ == 0 ||
466 (loaded_package->package_id_ == kAppPackageId && load_as_shared_library)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500467 // Package ID of 0 means this is a shared library.
468 loaded_package->dynamic_ = true;
469 }
470
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700471 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 Lesinskic6aada92017-01-13 15:34:14 -0800477 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 Lesinski498f6052017-11-29 13:24:29 -0800480 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE type ID offset too large.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800481 return {};
482 }
483 loaded_package->type_id_offset_ = static_cast<int>(type_id_offset);
484 }
485
Adam Lesinskida431a22016-12-29 16:08:16 -0500486 util::ReadUtf16StringFromDevice(header->name, arraysize(header->name),
487 &loaded_package->package_name_);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700488
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;
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 Lesinski498f6052017-11-29 13:24:29 -0800511 LOG(ERROR) << "RES_STRING_POOL_TYPE for types corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500512 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700513 }
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 Lesinski498f6052017-11-29 13:24:29 -0800519 LOG(ERROR) << "RES_STRING_POOL_TYPE for keys corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500520 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700521 }
522 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800523 LOG(WARNING) << "Too many RES_STRING_POOL_TYPEs found in RES_TABLE_PACKAGE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700524 }
525 } break;
526
527 case RES_TABLE_TYPE_SPEC_TYPE: {
528 ATRACE_NAME("LoadTableTypeSpec");
529
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.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500535 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700536 }
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700537
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 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700547
548 types_builder = {};
549 last_type_idx = 0;
550 }
551
552 const ResTable_typeSpec* type_spec = child_chunk.header<ResTable_typeSpec>();
553 if (type_spec == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800554 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500555 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700556 }
557
558 if (type_spec->id == 0) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800559 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500560 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700561 }
562
Adam Lesinskic6aada92017-01-13 15:34:14 -0800563 if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) >
564 std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800565 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has out of range ID.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800566 return {};
567 }
568
Adam Lesinski7ad11102016-10-28 16:39:15 -0700569 // 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 Lesinski498f6052017-11-29 13:24:29 -0800577 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has too many entries (" << entry_count << ").";
Adam Lesinskida431a22016-12-29 16:08:16 -0500578 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700579 }
580
581 if (entry_count * sizeof(uint32_t) > chunk.data_size()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800582 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500583 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700584 }
585
586 last_type_idx = type_spec->id - 1;
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700587
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
595 types_builder = util::make_unique<TypeSpecPtrBuilder>(type_spec, idmap_entry_header);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700596 } break;
597
598 case RES_TABLE_TYPE_TYPE: {
Adam Lesinski136fd072017-03-03 13:50:21 -0800599 const ResTable_type* type = child_chunk.header<ResTable_type, kResTableTypeMinSize>();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700600 if (type == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800601 LOG(ERROR) << "RES_TABLE_TYPE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500602 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700603 }
604
Adam Lesinski498f6052017-11-29 13:24:29 -0800605 if (!VerifyResTableType(type)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500606 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700607 }
608
609 // Type chunks must be preceded by their TypeSpec chunks.
610 if (!types_builder || type->id - 1 != last_type_idx) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800611 LOG(ERROR) << "RES_TABLE_TYPE_TYPE found without preceding RES_TABLE_TYPE_SPEC_TYPE.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500612 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700613 }
614
Adam Lesinski7ad11102016-10-28 16:39:15 -0700615 types_builder->AddType(type);
616 } break;
617
Adam Lesinskida431a22016-12-29 16:08:16 -0500618 case RES_TABLE_LIBRARY_TYPE: {
619 const ResTable_lib_header* lib = child_chunk.header<ResTable_lib_header>();
620 if (lib == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800621 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500622 return {};
623 }
624
625 if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800626 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500627 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()) {
641 LOG(ERROR) << base::StringPrintf(
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 Lesinski7ad11102016-10-28 16:39:15 -0700653 default:
654 LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type());
655 break;
656 }
657 }
658
659 // Finish the last TypeSpec.
660 if (types_builder) {
661 TypeSpecPtr type_spec_ptr = types_builder->Build();
662 if (type_spec_ptr == nullptr) {
663 LOG(ERROR) << "Too many type configurations, overflow detected.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500664 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700665 }
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700666
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) {
672 last_type_idx = dtohs(type_spec_ptr->idmap_entries->target_type_id) - 1;
673 }
674 loaded_package->type_specs_.editItemAt(last_type_idx) = std::move(type_spec_ptr);
675 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700676 }
677
678 if (iter.HadError()) {
679 LOG(ERROR) << iter.GetLastError();
Adam Lesinskida431a22016-12-29 16:08:16 -0500680 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700681 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700682 return std::move(loaded_package);
683}
684
Adam Lesinski50706b62018-01-23 03:16:16 -0800685bool 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 Lesinski7ad11102016-10-28 16:39:15 -0700705
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700706bool LoadedArsc::LoadTable(const Chunk& chunk, const LoadedIdmap* loaded_idmap,
707 bool load_as_shared_library) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700708 ATRACE_CALL();
709 const ResTable_header* header = chunk.header<ResTable_header>();
710 if (header == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800711 LOG(ERROR) << "RES_TABLE_TYPE too small.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700712 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 Lesinski498f6052017-11-29 13:24:29 -0800730 LOG(ERROR) << "RES_STRING_POOL_TYPE corrupt.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700731 return false;
732 }
733 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800734 LOG(WARNING) << "Multiple RES_STRING_POOL_TYPEs found in RES_TABLE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700735 }
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 Lesinski970bd8d2017-09-25 13:21:55 -0700741 << " declared in the header.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700742 return false;
743 }
744 packages_seen++;
745
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700746 std::unique_ptr<const LoadedPackage> loaded_package =
747 LoadedPackage::Load(child_chunk, loaded_idmap, system_, load_as_shared_library);
Adam Lesinskida431a22016-12-29 16:08:16 -0500748 if (!loaded_package) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700749 return false;
750 }
751 packages_.push_back(std::move(loaded_package));
752 } break;
753
754 default:
755 LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type());
756 break;
757 }
758 }
759
760 if (iter.HadError()) {
761 LOG(ERROR) << iter.GetLastError();
762 return false;
763 }
764 return true;
765}
766
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700767std::unique_ptr<const LoadedArsc> LoadedArsc::Load(const StringPiece& data,
768 const LoadedIdmap* loaded_idmap, bool system,
Adam Lesinski0c405242017-01-13 20:47:26 -0800769 bool load_as_shared_library) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700770 ATRACE_CALL();
771
772 // Not using make_unique because the constructor is private.
773 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
Adam Lesinski0c405242017-01-13 20:47:26 -0800774 loaded_arsc->system_ = system;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700775
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700776 ChunkIterator iter(data.data(), data.size());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700777 while (iter.HasNext()) {
778 const Chunk chunk = iter.Next();
779 switch (chunk.type()) {
780 case RES_TABLE_TYPE:
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700781 if (!loaded_arsc->LoadTable(chunk, loaded_idmap, load_as_shared_library)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700782 return {};
783 }
784 break;
785
786 default:
787 LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type());
788 break;
789 }
790 }
791
792 if (iter.HadError()) {
793 LOG(ERROR) << iter.GetLastError();
794 return {};
795 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800796
797 // Need to force a move for mingw32.
798 return std::move(loaded_arsc);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700799}
800
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700801std::unique_ptr<const LoadedArsc> LoadedArsc::CreateEmpty() {
802 return std::unique_ptr<LoadedArsc>(new LoadedArsc());
803}
804
Adam Lesinski7ad11102016-10-28 16:39:15 -0700805} // namespace android