blob: c8962416d08229678b2c5d928fe176210a0832de [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 Lesinskida431a22016-12-29 16:08:16 -050047namespace {
48
Adam Lesinski7ad11102016-10-28 16:39:15 -070049// Builder that helps accumulate Type structs and then create a single
50// contiguous block of memory to store both the TypeSpec struct and
51// the Type structs.
52class TypeSpecPtrBuilder {
53 public:
Ryan Mitchell8a891d82019-07-01 09:48:23 -070054 explicit TypeSpecPtrBuilder(const ResTable_typeSpec* header)
55 : header_(header) {
Adam Lesinski970bd8d2017-09-25 13:21:55 -070056 }
Adam Lesinski7ad11102016-10-28 16:39:15 -070057
58 void AddType(const ResTable_type* type) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -080059 types_.push_back(type);
Adam Lesinski7ad11102016-10-28 16:39:15 -070060 }
61
62 TypeSpecPtr Build() {
63 // Check for overflow.
Adam Lesinskibebfcc42018-02-12 14:27:46 -080064 using ElementType = const ResTable_type*;
65 if ((std::numeric_limits<size_t>::max() - sizeof(TypeSpec)) / sizeof(ElementType) <
66 types_.size()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -070067 return {};
68 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -080069 TypeSpec* type_spec =
70 (TypeSpec*)::malloc(sizeof(TypeSpec) + (types_.size() * sizeof(ElementType)));
Adam Lesinski7ad11102016-10-28 16:39:15 -070071 type_spec->type_spec = header_;
72 type_spec->type_count = types_.size();
Adam Lesinskibebfcc42018-02-12 14:27:46 -080073 memcpy(type_spec + 1, types_.data(), types_.size() * sizeof(ElementType));
Adam Lesinski7ad11102016-10-28 16:39:15 -070074 return TypeSpecPtr(type_spec);
75 }
76
77 private:
78 DISALLOW_COPY_AND_ASSIGN(TypeSpecPtrBuilder);
79
80 const ResTable_typeSpec* header_;
Adam Lesinskibebfcc42018-02-12 14:27:46 -080081 std::vector<const ResTable_type*> types_;
Adam Lesinski7ad11102016-10-28 16:39:15 -070082};
83
84} // namespace
85
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070086LoadedPackage::LoadedPackage() = default;
87LoadedPackage::~LoadedPackage() = default;
88
89// Precondition: The header passed in has already been verified, so reading any fields and trusting
90// the ResChunk_header is safe.
91static bool VerifyResTableType(const ResTable_type* header) {
Adam Lesinski498f6052017-11-29 13:24:29 -080092 if (header->id == 0) {
93 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has invalid ID 0.";
94 return false;
95 }
96
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070097 const size_t entry_count = dtohl(header->entryCount);
98 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -080099 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has too many entries (" << entry_count << ").";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700100 return false;
101 }
102
103 // Make sure that there is enough room for the entry offsets.
104 const size_t offsets_offset = dtohs(header->header.headerSize);
105 const size_t entries_offset = dtohl(header->entriesStart);
106 const size_t offsets_length = sizeof(uint32_t) * entry_count;
107
108 if (offsets_offset > entries_offset || entries_offset - offsets_offset < offsets_length) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800109 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700110 return false;
111 }
112
113 if (entries_offset > dtohl(header->header.size)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800114 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets extend beyond chunk.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700115 return false;
116 }
117
118 if (entries_offset & 0x03) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800119 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entries start at unaligned address.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700120 return false;
121 }
122 return true;
123}
124
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800125static bool VerifyResTableEntry(const ResTable_type* type, uint32_t entry_offset) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700126 // Check that the offset is aligned.
127 if (entry_offset & 0x03) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800128 LOG(ERROR) << "Entry at offset " << entry_offset << " is not 4-byte aligned.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700129 return false;
130 }
131
132 // Check that the offset doesn't overflow.
133 if (entry_offset > std::numeric_limits<uint32_t>::max() - dtohl(type->entriesStart)) {
134 // Overflow in offset.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800135 LOG(ERROR) << "Entry at offset " << entry_offset << " is too large.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700136 return false;
137 }
138
139 const size_t chunk_size = dtohl(type->header.size);
140
141 entry_offset += dtohl(type->entriesStart);
142 if (entry_offset > chunk_size - sizeof(ResTable_entry)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800143 LOG(ERROR) << "Entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700144 << " is too large. No room for ResTable_entry.";
145 return false;
146 }
147
148 const ResTable_entry* entry = reinterpret_cast<const ResTable_entry*>(
149 reinterpret_cast<const uint8_t*>(type) + entry_offset);
150
151 const size_t entry_size = dtohs(entry->size);
152 if (entry_size < sizeof(*entry)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800153 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700154 << " is too small.";
155 return false;
156 }
157
158 if (entry_size > chunk_size || entry_offset > chunk_size - entry_size) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800159 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700160 << " is too large.";
161 return false;
162 }
163
164 if (entry_size < sizeof(ResTable_map_entry)) {
165 // There needs to be room for one Res_value struct.
166 if (entry_offset + entry_size > chunk_size - sizeof(Res_value)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800167 LOG(ERROR) << "No room for Res_value after ResTable_entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700168 << " for type " << (int)type->id << ".";
169 return false;
170 }
171
172 const Res_value* value =
173 reinterpret_cast<const Res_value*>(reinterpret_cast<const uint8_t*>(entry) + entry_size);
174 const size_t value_size = dtohs(value->size);
175 if (value_size < sizeof(Res_value)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800176 LOG(ERROR) << "Res_value at offset " << entry_offset << " is too small.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700177 return false;
178 }
179
180 if (value_size > chunk_size || entry_offset + entry_size > chunk_size - value_size) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800181 LOG(ERROR) << "Res_value size " << value_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700182 << " is too large.";
183 return false;
184 }
185 } else {
186 const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(entry);
187 const size_t map_entry_count = dtohl(map->count);
188 size_t map_entries_start = entry_offset + entry_size;
189 if (map_entries_start & 0x03) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800190 LOG(ERROR) << "Map entries at offset " << entry_offset << " start at unaligned offset.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700191 return false;
192 }
193
194 // Each entry is sizeof(ResTable_map) big.
195 if (map_entry_count > ((chunk_size - map_entries_start) / sizeof(ResTable_map))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800196 LOG(ERROR) << "Too many map entries in ResTable_map_entry at offset " << entry_offset << ".";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700197 return false;
198 }
199 }
200 return true;
201}
202
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100203LoadedPackage::iterator::iterator(const LoadedPackage* lp, size_t ti, size_t ei)
204 : loadedPackage_(lp),
205 typeIndex_(ti),
206 entryIndex_(ei),
207 typeIndexEnd_(lp->resource_ids_.size() + 1) {
208 while (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] == 0) {
209 typeIndex_++;
210 }
211}
212
213LoadedPackage::iterator& LoadedPackage::iterator::operator++() {
214 while (typeIndex_ < typeIndexEnd_) {
215 if (entryIndex_ + 1 < loadedPackage_->resource_ids_[typeIndex_]) {
216 entryIndex_++;
217 break;
218 }
219 entryIndex_ = 0;
220 typeIndex_++;
221 if (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] != 0) {
222 break;
223 }
224 }
225 return *this;
226}
227
228uint32_t LoadedPackage::iterator::operator*() const {
229 if (typeIndex_ >= typeIndexEnd_) {
230 return 0;
231 }
232 return make_resid(loadedPackage_->package_id_, typeIndex_ + loadedPackage_->type_id_offset_,
233 entryIndex_);
234}
235
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800236const ResTable_entry* LoadedPackage::GetEntry(const ResTable_type* type_chunk,
237 uint16_t entry_index) {
238 uint32_t entry_offset = GetEntryOffset(type_chunk, entry_index);
239 if (entry_offset == ResTable_type::NO_ENTRY) {
240 return nullptr;
241 }
242 return GetEntryFromOffset(type_chunk, entry_offset);
243}
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700244
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800245uint32_t LoadedPackage::GetEntryOffset(const ResTable_type* type_chunk, uint16_t entry_index) {
246 // The configuration matches and is better than the previous selection.
247 // Find the entry value if it exists for this configuration.
248 const size_t entry_count = dtohl(type_chunk->entryCount);
249 const size_t offsets_offset = dtohs(type_chunk->header.headerSize);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700250
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800251 // Check if there is the desired entry in this type.
Adam Lesinski73f6f9d2017-11-14 10:18:05 -0800252
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800253 if (type_chunk->flags & ResTable_type::FLAG_SPARSE) {
254 // This is encoded as a sparse map, so perform a binary search.
255 const ResTable_sparseTypeEntry* sparse_indices =
256 reinterpret_cast<const ResTable_sparseTypeEntry*>(
Adam Lesinski73f6f9d2017-11-14 10:18:05 -0800257 reinterpret_cast<const uint8_t*>(type_chunk) + offsets_offset);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800258 const ResTable_sparseTypeEntry* sparse_indices_end = sparse_indices + entry_count;
259 const ResTable_sparseTypeEntry* result =
260 std::lower_bound(sparse_indices, sparse_indices_end, entry_index,
261 [](const ResTable_sparseTypeEntry& entry, uint16_t entry_idx) {
262 return dtohs(entry.idx) < entry_idx;
263 });
Adam Lesinski73f6f9d2017-11-14 10:18:05 -0800264
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800265 if (result == sparse_indices_end || dtohs(result->idx) != entry_index) {
266 // No entry found.
267 return ResTable_type::NO_ENTRY;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700268 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800269
270 // Extract the offset from the entry. Each offset must be a multiple of 4 so we store it as
271 // the real offset divided by 4.
272 return uint32_t{dtohs(result->offset)} * 4u;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700273 }
274
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800275 // This type is encoded as a dense array.
276 if (entry_index >= entry_count) {
277 // This entry cannot be here.
278 return ResTable_type::NO_ENTRY;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700279 }
280
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800281 const uint32_t* entry_offsets = reinterpret_cast<const uint32_t*>(
282 reinterpret_cast<const uint8_t*>(type_chunk) + offsets_offset);
283 return dtohl(entry_offsets[entry_index]);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700284}
285
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800286const ResTable_entry* LoadedPackage::GetEntryFromOffset(const ResTable_type* type_chunk,
287 uint32_t offset) {
288 if (UNLIKELY(!VerifyResTableEntry(type_chunk, offset))) {
289 return nullptr;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700290 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800291 return reinterpret_cast<const ResTable_entry*>(reinterpret_cast<const uint8_t*>(type_chunk) +
292 offset + dtohl(type_chunk->entriesStart));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700293}
294
Adam Lesinski0c405242017-01-13 20:47:26 -0800295void LoadedPackage::CollectConfigurations(bool exclude_mipmap,
296 std::set<ResTable_config>* out_configs) const {
297 const static std::u16string kMipMap = u"mipmap";
298 const size_t type_count = type_specs_.size();
299 for (size_t i = 0; i < type_count; i++) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800300 const TypeSpecPtr& type_spec = type_specs_[i];
Adam Lesinski0c405242017-01-13 20:47:26 -0800301 if (type_spec != nullptr) {
302 if (exclude_mipmap) {
303 const int type_idx = type_spec->type_spec->id - 1;
304 size_t type_name_len;
305 const char16_t* type_name16 = type_string_pool_.stringAt(type_idx, &type_name_len);
306 if (type_name16 != nullptr) {
307 if (kMipMap.compare(0, std::u16string::npos, type_name16, type_name_len) == 0) {
308 // This is a mipmap type, skip collection.
309 continue;
310 }
311 }
312 const char* type_name = type_string_pool_.string8At(type_idx, &type_name_len);
313 if (type_name != nullptr) {
314 if (strncmp(type_name, "mipmap", type_name_len) == 0) {
315 // This is a mipmap type, skip collection.
316 continue;
317 }
318 }
319 }
320
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800321 const auto iter_end = type_spec->types + type_spec->type_count;
322 for (auto iter = type_spec->types; iter != iter_end; ++iter) {
323 ResTable_config config;
324 config.copyFromDtoH((*iter)->config);
325 out_configs->insert(config);
Adam Lesinski0c405242017-01-13 20:47:26 -0800326 }
327 }
328 }
329}
330
331void LoadedPackage::CollectLocales(bool canonicalize, std::set<std::string>* out_locales) const {
332 char temp_locale[RESTABLE_MAX_LOCALE_LEN];
333 const size_t type_count = type_specs_.size();
334 for (size_t i = 0; i < type_count; i++) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800335 const TypeSpecPtr& type_spec = type_specs_[i];
Adam Lesinski0c405242017-01-13 20:47:26 -0800336 if (type_spec != nullptr) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800337 const auto iter_end = type_spec->types + type_spec->type_count;
338 for (auto iter = type_spec->types; iter != iter_end; ++iter) {
339 ResTable_config configuration;
340 configuration.copyFromDtoH((*iter)->config);
Adam Lesinski0c405242017-01-13 20:47:26 -0800341 if (configuration.locale != 0) {
342 configuration.getBcp47Locale(temp_locale, canonicalize);
343 std::string locale(temp_locale);
344 out_locales->insert(std::move(locale));
345 }
346 }
347 }
348 }
349}
350
Adam Lesinski929d6512017-01-16 19:11:19 -0800351uint32_t LoadedPackage::FindEntryByName(const std::u16string& type_name,
352 const std::u16string& entry_name) const {
353 ssize_t type_idx = type_string_pool_.indexOfString(type_name.data(), type_name.size());
354 if (type_idx < 0) {
355 return 0u;
356 }
357
358 ssize_t key_idx = key_string_pool_.indexOfString(entry_name.data(), entry_name.size());
359 if (key_idx < 0) {
360 return 0u;
361 }
362
363 const TypeSpec* type_spec = type_specs_[type_idx].get();
364 if (type_spec == nullptr) {
365 return 0u;
366 }
367
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800368 const auto iter_end = type_spec->types + type_spec->type_count;
369 for (auto iter = type_spec->types; iter != iter_end; ++iter) {
370 const ResTable_type* type = *iter;
371 size_t entry_count = dtohl(type->entryCount);
Adam Lesinski929d6512017-01-16 19:11:19 -0800372 for (size_t entry_idx = 0; entry_idx < entry_count; entry_idx++) {
373 const uint32_t* entry_offsets = reinterpret_cast<const uint32_t*>(
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800374 reinterpret_cast<const uint8_t*>(type) + dtohs(type->header.headerSize));
Adam Lesinski929d6512017-01-16 19:11:19 -0800375 const uint32_t offset = dtohl(entry_offsets[entry_idx]);
376 if (offset != ResTable_type::NO_ENTRY) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800377 const ResTable_entry* entry = reinterpret_cast<const ResTable_entry*>(
378 reinterpret_cast<const uint8_t*>(type) + dtohl(type->entriesStart) + offset);
Adam Lesinski929d6512017-01-16 19:11:19 -0800379 if (dtohl(entry->key.index) == static_cast<uint32_t>(key_idx)) {
380 // The package ID will be overridden by the caller (due to runtime assignment of package
381 // IDs for shared libraries).
382 return make_resid(0x00, type_idx + type_id_offset_ + 1, entry_idx);
383 }
384 }
385 }
386 }
387 return 0u;
388}
389
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800390const LoadedPackage* LoadedArsc::GetPackageById(uint8_t package_id) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700391 for (const auto& loaded_package : packages_) {
392 if (loaded_package->GetPackageId() == package_id) {
393 return loaded_package.get();
394 }
395 }
396 return nullptr;
397}
398
399std::unique_ptr<const LoadedPackage> LoadedPackage::Load(const Chunk& chunk,
Winson9947f1e2019-08-16 10:20:39 -0700400 bool system,
401 bool load_as_shared_library,
402 bool for_loader) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800403 ATRACE_NAME("LoadedPackage::Load");
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700404 std::unique_ptr<LoadedPackage> loaded_package(new LoadedPackage());
Adam Lesinskida431a22016-12-29 16:08:16 -0500405
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700406 // typeIdOffset was added at some point, but we still must recognize apps built before this
407 // was added.
Adam Lesinski33af6c72017-03-29 13:00:35 -0700408 constexpr size_t kMinPackageSize =
409 sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset);
410 const ResTable_package* header = chunk.header<ResTable_package, kMinPackageSize>();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700411 if (header == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800412 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500413 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700414 }
415
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700416 loaded_package->system_ = system;
417
Adam Lesinski7ad11102016-10-28 16:39:15 -0700418 loaded_package->package_id_ = dtohl(header->id);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700419 if (loaded_package->package_id_ == 0 ||
420 (loaded_package->package_id_ == kAppPackageId && load_as_shared_library)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500421 // Package ID of 0 means this is a shared library.
422 loaded_package->dynamic_ = true;
423 }
424
Winson9947f1e2019-08-16 10:20:39 -0700425 if (for_loader) {
426 loaded_package->custom_loader_ = true;
427 }
428
Adam Lesinskic6aada92017-01-13 15:34:14 -0800429 if (header->header.headerSize >= sizeof(ResTable_package)) {
430 uint32_t type_id_offset = dtohl(header->typeIdOffset);
431 if (type_id_offset > std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800432 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE type ID offset too large.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800433 return {};
434 }
435 loaded_package->type_id_offset_ = static_cast<int>(type_id_offset);
436 }
437
Adam Lesinskida431a22016-12-29 16:08:16 -0500438 util::ReadUtf16StringFromDevice(header->name, arraysize(header->name),
439 &loaded_package->package_name_);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700440
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800441 // A map of TypeSpec builders, each associated with an type index.
442 // We use these to accumulate the set of Types available for a TypeSpec, and later build a single,
443 // contiguous block of memory that holds all the Types together with the TypeSpec.
444 std::unordered_map<int, std::unique_ptr<TypeSpecPtrBuilder>> type_builder_map;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700445
446 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
447 while (iter.HasNext()) {
448 const Chunk child_chunk = iter.Next();
449 switch (child_chunk.type()) {
450 case RES_STRING_POOL_TYPE: {
451 const uintptr_t pool_address =
452 reinterpret_cast<uintptr_t>(child_chunk.header<ResChunk_header>());
453 const uintptr_t header_address = reinterpret_cast<uintptr_t>(header);
454 if (pool_address == header_address + dtohl(header->typeStrings)) {
455 // This string pool is the type string pool.
456 status_t err = loaded_package->type_string_pool_.setTo(
457 child_chunk.header<ResStringPool_header>(), child_chunk.size());
458 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800459 LOG(ERROR) << "RES_STRING_POOL_TYPE for types corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500460 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700461 }
462 } else if (pool_address == header_address + dtohl(header->keyStrings)) {
463 // This string pool is the key string pool.
464 status_t err = loaded_package->key_string_pool_.setTo(
465 child_chunk.header<ResStringPool_header>(), child_chunk.size());
466 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800467 LOG(ERROR) << "RES_STRING_POOL_TYPE for keys corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500468 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700469 }
470 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800471 LOG(WARNING) << "Too many RES_STRING_POOL_TYPEs found in RES_TABLE_PACKAGE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700472 }
473 } break;
474
475 case RES_TABLE_TYPE_SPEC_TYPE: {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700476 const ResTable_typeSpec* type_spec = child_chunk.header<ResTable_typeSpec>();
477 if (type_spec == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800478 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500479 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700480 }
481
482 if (type_spec->id == 0) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800483 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500484 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700485 }
486
Adam Lesinskic6aada92017-01-13 15:34:14 -0800487 if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) >
488 std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800489 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has out of range ID.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800490 return {};
491 }
492
Adam Lesinski7ad11102016-10-28 16:39:15 -0700493 // The data portion of this chunk contains entry_count 32bit entries,
494 // each one representing a set of flags.
495 // Here we only validate that the chunk is well formed.
496 const size_t entry_count = dtohl(type_spec->entryCount);
497
498 // There can only be 2^16 entries in a type, because that is the ID
499 // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
500 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800501 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has too many entries (" << entry_count << ").";
Adam Lesinskida431a22016-12-29 16:08:16 -0500502 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700503 }
504
505 if (entry_count * sizeof(uint32_t) > chunk.data_size()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800506 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500507 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700508 }
509
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800510 std::unique_ptr<TypeSpecPtrBuilder>& builder_ptr = type_builder_map[type_spec->id - 1];
511 if (builder_ptr == nullptr) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700512 builder_ptr = util::make_unique<TypeSpecPtrBuilder>(type_spec);
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100513 loaded_package->resource_ids_.set(type_spec->id, entry_count);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800514 } else {
515 LOG(WARNING) << StringPrintf("RES_TABLE_TYPE_SPEC_TYPE already defined for ID %02x",
516 type_spec->id);
517 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700518 } break;
519
520 case RES_TABLE_TYPE_TYPE: {
Adam Lesinski136fd072017-03-03 13:50:21 -0800521 const ResTable_type* type = child_chunk.header<ResTable_type, kResTableTypeMinSize>();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700522 if (type == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800523 LOG(ERROR) << "RES_TABLE_TYPE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500524 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700525 }
526
Adam Lesinski498f6052017-11-29 13:24:29 -0800527 if (!VerifyResTableType(type)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500528 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700529 }
530
531 // Type chunks must be preceded by their TypeSpec chunks.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800532 std::unique_ptr<TypeSpecPtrBuilder>& builder_ptr = type_builder_map[type->id - 1];
533 if (builder_ptr != nullptr) {
534 builder_ptr->AddType(type);
535 } else {
536 LOG(ERROR) << StringPrintf(
537 "RES_TABLE_TYPE_TYPE with ID %02x found without preceding RES_TABLE_TYPE_SPEC_TYPE.",
538 type->id);
Adam Lesinskida431a22016-12-29 16:08:16 -0500539 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700540 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700541 } break;
542
Adam Lesinskida431a22016-12-29 16:08:16 -0500543 case RES_TABLE_LIBRARY_TYPE: {
544 const ResTable_lib_header* lib = child_chunk.header<ResTable_lib_header>();
545 if (lib == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800546 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500547 return {};
548 }
549
550 if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800551 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500552 return {};
553 }
554
555 loaded_package->dynamic_package_map_.reserve(dtohl(lib->count));
556
557 const ResTable_lib_entry* const entry_begin =
558 reinterpret_cast<const ResTable_lib_entry*>(child_chunk.data_ptr());
559 const ResTable_lib_entry* const entry_end = entry_begin + dtohl(lib->count);
560 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
561 std::string package_name;
562 util::ReadUtf16StringFromDevice(entry_iter->packageName,
563 arraysize(entry_iter->packageName), &package_name);
564
565 if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800566 LOG(ERROR) << StringPrintf(
Adam Lesinskida431a22016-12-29 16:08:16 -0500567 "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.",
568 dtohl(entry_iter->packageId), package_name.c_str());
569 return {};
570 }
571
572 loaded_package->dynamic_package_map_.emplace_back(std::move(package_name),
573 dtohl(entry_iter->packageId));
574 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800575 } break;
Adam Lesinskida431a22016-12-29 16:08:16 -0500576
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800577 case RES_TABLE_OVERLAYABLE_TYPE: {
578 const ResTable_overlayable_header* header =
579 child_chunk.header<ResTable_overlayable_header>();
580 if (header == nullptr) {
581 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_TYPE too small.";
582 return {};
583 }
584
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800585 std::string name;
586 util::ReadUtf16StringFromDevice(header->name, arraysize(header->name), &name);
587 std::string actor;
588 util::ReadUtf16StringFromDevice(header->actor, arraysize(header->actor), &actor);
589
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100590 if (loaded_package->overlayable_map_.find(name) !=
591 loaded_package->overlayable_map_.end()) {
592 LOG(ERROR) << "Multiple <overlayable> blocks with the same name '" << name << "'.";
593 return {};
594 }
595 loaded_package->overlayable_map_.emplace(name, actor);
596
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800597 // Iterate over the overlayable policy chunks contained within the overlayable chunk data
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800598 ChunkIterator overlayable_iter(child_chunk.data_ptr(), child_chunk.data_size());
599 while (overlayable_iter.HasNext()) {
600 const Chunk overlayable_child_chunk = overlayable_iter.Next();
601
602 switch (overlayable_child_chunk.type()) {
603 case RES_TABLE_OVERLAYABLE_POLICY_TYPE: {
604 const ResTable_overlayable_policy_header* policy_header =
605 overlayable_child_chunk.header<ResTable_overlayable_policy_header>();
606 if (policy_header == nullptr) {
607 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small.";
608 return {};
609 }
610
611 if ((overlayable_child_chunk.data_size() / sizeof(ResTable_ref))
612 < dtohl(policy_header->entry_count)) {
613 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small to hold entries.";
614 return {};
615 }
616
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800617 // Retrieve all the resource ids belonging to this policy chunk
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800618 std::unordered_set<uint32_t> ids;
619 const auto ids_begin =
620 reinterpret_cast<const ResTable_ref*>(overlayable_child_chunk.data_ptr());
621 const auto ids_end = ids_begin + dtohl(policy_header->entry_count);
622 for (auto id_iter = ids_begin; id_iter != ids_end; ++id_iter) {
623 ids.insert(dtohl(id_iter->ident));
624 }
625
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800626 // Add the pairing of overlayable properties and resource ids to the package
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800627 OverlayableInfo overlayable_info{};
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800628 overlayable_info.name = name;
629 overlayable_info.actor = actor;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800630 overlayable_info.policy_flags = policy_header->policy_flags;
631 loaded_package->overlayable_infos_.push_back(std::make_pair(overlayable_info, ids));
Ryan Mitchell19823452019-01-29 12:01:24 -0800632 loaded_package->defines_overlayable_ = true;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800633 break;
634 }
635
636 default:
637 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
638 break;
639 }
640 }
641
642 if (overlayable_iter.HadError()) {
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800643 LOG(ERROR) << StringPrintf("Error parsing RES_TABLE_OVERLAYABLE_TYPE: %s",
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800644 overlayable_iter.GetLastError().c_str());
645 if (overlayable_iter.HadFatalError()) {
646 return {};
647 }
648 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500649 } break;
650
Adam Lesinski7ad11102016-10-28 16:39:15 -0700651 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800652 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700653 break;
654 }
655 }
656
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800657 if (iter.HadError()) {
658 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700659 if (iter.HadFatalError()) {
660 return {};
661 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800662 }
663
664 // Flatten and construct the TypeSpecs.
665 for (auto& entry : type_builder_map) {
666 uint8_t type_idx = static_cast<uint8_t>(entry.first);
667 TypeSpecPtr type_spec_ptr = entry.second->Build();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700668 if (type_spec_ptr == nullptr) {
669 LOG(ERROR) << "Too many type configurations, overflow detected.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500670 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700671 }
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700672
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700673 loaded_package->type_specs_.editItemAt(type_idx) = std::move(type_spec_ptr);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700674 }
675
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700676 return std::move(loaded_package);
677}
678
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700679bool LoadedArsc::LoadTable(const Chunk& chunk, const LoadedIdmap* loaded_idmap,
Winson9947f1e2019-08-16 10:20:39 -0700680 bool load_as_shared_library, bool for_loader) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700681 const ResTable_header* header = chunk.header<ResTable_header>();
682 if (header == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800683 LOG(ERROR) << "RES_TABLE_TYPE too small.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700684 return false;
685 }
686
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700687 if (loaded_idmap != nullptr) {
688 global_string_pool_ = util::make_unique<OverlayStringPool>(loaded_idmap);
689 }
690
Adam Lesinski7ad11102016-10-28 16:39:15 -0700691 const size_t package_count = dtohl(header->packageCount);
692 size_t packages_seen = 0;
693
694 packages_.reserve(package_count);
695
696 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
697 while (iter.HasNext()) {
698 const Chunk child_chunk = iter.Next();
699 switch (child_chunk.type()) {
700 case RES_STRING_POOL_TYPE:
701 // Only use the first string pool. Ignore others.
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700702 if (global_string_pool_->getError() == NO_INIT) {
703 status_t err = global_string_pool_->setTo(child_chunk.header<ResStringPool_header>(),
704 child_chunk.size());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700705 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800706 LOG(ERROR) << "RES_STRING_POOL_TYPE corrupt.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700707 return false;
708 }
709 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800710 LOG(WARNING) << "Multiple RES_STRING_POOL_TYPEs found in RES_TABLE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700711 }
712 break;
713
714 case RES_TABLE_PACKAGE_TYPE: {
715 if (packages_seen + 1 > package_count) {
716 LOG(ERROR) << "More package chunks were found than the " << package_count
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700717 << " declared in the header.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700718 return false;
719 }
720 packages_seen++;
721
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700722 std::unique_ptr<const LoadedPackage> loaded_package =
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700723 LoadedPackage::Load(child_chunk, system_, load_as_shared_library, for_loader);
Adam Lesinskida431a22016-12-29 16:08:16 -0500724 if (!loaded_package) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700725 return false;
726 }
727 packages_.push_back(std::move(loaded_package));
728 } break;
729
730 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800731 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700732 break;
733 }
734 }
735
736 if (iter.HadError()) {
737 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700738 if (iter.HadFatalError()) {
739 return false;
740 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700741 }
742 return true;
743}
744
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700745std::unique_ptr<const LoadedArsc> LoadedArsc::Load(const StringPiece& data,
Winson9947f1e2019-08-16 10:20:39 -0700746 const LoadedIdmap* loaded_idmap,
747 bool system,
748 bool load_as_shared_library,
749 bool for_loader) {
750 ATRACE_NAME("LoadedArsc::Load");
Adam Lesinski7ad11102016-10-28 16:39:15 -0700751
752 // Not using make_unique because the constructor is private.
753 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
Adam Lesinski0c405242017-01-13 20:47:26 -0800754 loaded_arsc->system_ = system;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700755
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700756 ChunkIterator iter(data.data(), data.size());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700757 while (iter.HasNext()) {
758 const Chunk chunk = iter.Next();
759 switch (chunk.type()) {
760 case RES_TABLE_TYPE:
Winson9947f1e2019-08-16 10:20:39 -0700761 if (!loaded_arsc->LoadTable(chunk,
762 loaded_idmap,
763 load_as_shared_library,
764 for_loader)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700765 return {};
766 }
767 break;
768
769 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800770 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700771 break;
772 }
773 }
774
775 if (iter.HadError()) {
776 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700777 if (iter.HadFatalError()) {
778 return {};
779 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700780 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800781
782 // Need to force a move for mingw32.
783 return std::move(loaded_arsc);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700784}
785
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700786std::unique_ptr<const LoadedArsc> LoadedArsc::CreateEmpty() {
787 return std::unique_ptr<LoadedArsc>(new LoadedArsc());
788}
789
Adam Lesinski7ad11102016-10-28 16:39:15 -0700790} // namespace android