blob: 1d2c597c4c8c7cfa5298308e5ae3bd0d77f39876 [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:
Adam Lesinski970bd8d2017-09-25 13:21:55 -070054 explicit TypeSpecPtrBuilder(const ResTable_typeSpec* header,
55 const IdmapEntry_header* idmap_header)
56 : header_(header), idmap_header_(idmap_header) {
57 }
Adam Lesinski7ad11102016-10-28 16:39:15 -070058
59 void AddType(const ResTable_type* type) {
Adam Lesinski64ee69d2018-01-08 17:38:30 -080060 types_.push_back(type);
Adam Lesinski7ad11102016-10-28 16:39:15 -070061 }
62
63 TypeSpecPtr Build() {
64 // Check for overflow.
Adam Lesinski64ee69d2018-01-08 17:38:30 -080065 using ElementType = const ResTable_type*;
66 if ((std::numeric_limits<size_t>::max() - sizeof(TypeSpec)) / sizeof(ElementType) <
67 types_.size()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -070068 return {};
69 }
Adam Lesinski64ee69d2018-01-08 17:38:30 -080070 TypeSpec* type_spec =
71 (TypeSpec*)::malloc(sizeof(TypeSpec) + (types_.size() * sizeof(ElementType)));
Adam Lesinski7ad11102016-10-28 16:39:15 -070072 type_spec->type_spec = header_;
Adam Lesinski970bd8d2017-09-25 13:21:55 -070073 type_spec->idmap_entries = idmap_header_;
Adam Lesinski7ad11102016-10-28 16:39:15 -070074 type_spec->type_count = types_.size();
Adam Lesinski64ee69d2018-01-08 17:38:30 -080075 memcpy(type_spec + 1, types_.data(), types_.size() * sizeof(ElementType));
Adam Lesinski7ad11102016-10-28 16:39:15 -070076 return TypeSpecPtr(type_spec);
77 }
78
79 private:
80 DISALLOW_COPY_AND_ASSIGN(TypeSpecPtrBuilder);
81
82 const ResTable_typeSpec* header_;
Adam Lesinski970bd8d2017-09-25 13:21:55 -070083 const IdmapEntry_header* idmap_header_;
Adam Lesinski64ee69d2018-01-08 17:38:30 -080084 std::vector<const ResTable_type*> types_;
Adam Lesinski7ad11102016-10-28 16:39:15 -070085};
86
87} // namespace
88
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070089LoadedPackage::LoadedPackage() = default;
90LoadedPackage::~LoadedPackage() = default;
91
92// Precondition: The header passed in has already been verified, so reading any fields and trusting
93// the ResChunk_header is safe.
94static bool VerifyResTableType(const ResTable_type* header) {
Adam Lesinski498f6052017-11-29 13:24:29 -080095 if (header->id == 0) {
96 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has invalid ID 0.";
97 return false;
98 }
99
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700100 const size_t entry_count = dtohl(header->entryCount);
101 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800102 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has too many entries (" << entry_count << ").";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700103 return false;
104 }
105
106 // Make sure that there is enough room for the entry offsets.
107 const size_t offsets_offset = dtohs(header->header.headerSize);
108 const size_t entries_offset = dtohl(header->entriesStart);
109 const size_t offsets_length = sizeof(uint32_t) * entry_count;
110
111 if (offsets_offset > entries_offset || entries_offset - offsets_offset < offsets_length) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800112 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700113 return false;
114 }
115
116 if (entries_offset > dtohl(header->header.size)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800117 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets extend beyond chunk.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700118 return false;
119 }
120
121 if (entries_offset & 0x03) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800122 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entries start at unaligned address.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700123 return false;
124 }
125 return true;
126}
127
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800128static bool VerifyResTableEntry(const ResTable_type* type, uint32_t entry_offset) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700129 // Check that the offset is aligned.
130 if (entry_offset & 0x03) {
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800131 LOG(ERROR) << "Entry at offset " << entry_offset << " is not 4-byte aligned.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700132 return false;
133 }
134
135 // Check that the offset doesn't overflow.
136 if (entry_offset > std::numeric_limits<uint32_t>::max() - dtohl(type->entriesStart)) {
137 // Overflow in offset.
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800138 LOG(ERROR) << "Entry at offset " << entry_offset << " is too large.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700139 return false;
140 }
141
142 const size_t chunk_size = dtohl(type->header.size);
143
144 entry_offset += dtohl(type->entriesStart);
145 if (entry_offset > chunk_size - sizeof(ResTable_entry)) {
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800146 LOG(ERROR) << "Entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700147 << " is too large. No room for ResTable_entry.";
148 return false;
149 }
150
151 const ResTable_entry* entry = reinterpret_cast<const ResTable_entry*>(
152 reinterpret_cast<const uint8_t*>(type) + entry_offset);
153
154 const size_t entry_size = dtohs(entry->size);
155 if (entry_size < sizeof(*entry)) {
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800156 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700157 << " is too small.";
158 return false;
159 }
160
161 if (entry_size > chunk_size || entry_offset > chunk_size - entry_size) {
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800162 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700163 << " is too large.";
164 return false;
165 }
166
167 if (entry_size < sizeof(ResTable_map_entry)) {
168 // There needs to be room for one Res_value struct.
169 if (entry_offset + entry_size > chunk_size - sizeof(Res_value)) {
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800170 LOG(ERROR) << "No room for Res_value after ResTable_entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700171 << " for type " << (int)type->id << ".";
172 return false;
173 }
174
175 const Res_value* value =
176 reinterpret_cast<const Res_value*>(reinterpret_cast<const uint8_t*>(entry) + entry_size);
177 const size_t value_size = dtohs(value->size);
178 if (value_size < sizeof(Res_value)) {
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800179 LOG(ERROR) << "Res_value at offset " << entry_offset << " is too small.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700180 return false;
181 }
182
183 if (value_size > chunk_size || entry_offset + entry_size > chunk_size - value_size) {
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800184 LOG(ERROR) << "Res_value size " << value_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700185 << " is too large.";
186 return false;
187 }
188 } else {
189 const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(entry);
190 const size_t map_entry_count = dtohl(map->count);
191 size_t map_entries_start = entry_offset + entry_size;
192 if (map_entries_start & 0x03) {
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800193 LOG(ERROR) << "Map entries at offset " << entry_offset << " start at unaligned offset.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700194 return false;
195 }
196
197 // Each entry is sizeof(ResTable_map) big.
198 if (map_entry_count > ((chunk_size - map_entries_start) / sizeof(ResTable_map))) {
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800199 LOG(ERROR) << "Too many map entries in ResTable_map_entry at offset " << entry_offset << ".";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700200 return false;
201 }
202 }
203 return true;
204}
205
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800206const ResTable_entry* LoadedPackage::GetEntry(const ResTable_type* type_chunk,
207 uint16_t entry_index) {
208 uint32_t entry_offset = GetEntryOffset(type_chunk, entry_index);
209 if (entry_offset == ResTable_type::NO_ENTRY) {
210 return nullptr;
211 }
212 return GetEntryFromOffset(type_chunk, entry_offset);
213}
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700214
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800215uint32_t LoadedPackage::GetEntryOffset(const ResTable_type* type_chunk, uint16_t entry_index) {
216 // The configuration matches and is better than the previous selection.
217 // Find the entry value if it exists for this configuration.
218 const size_t entry_count = dtohl(type_chunk->entryCount);
219 const size_t offsets_offset = dtohs(type_chunk->header.headerSize);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700220
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800221 // Check if there is the desired entry in this type.
Adam Lesinski73f6f9d2017-11-14 10:18:05 -0800222
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800223 if (type_chunk->flags & ResTable_type::FLAG_SPARSE) {
224 // This is encoded as a sparse map, so perform a binary search.
225 const ResTable_sparseTypeEntry* sparse_indices =
226 reinterpret_cast<const ResTable_sparseTypeEntry*>(
Adam Lesinski73f6f9d2017-11-14 10:18:05 -0800227 reinterpret_cast<const uint8_t*>(type_chunk) + offsets_offset);
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800228 const ResTable_sparseTypeEntry* sparse_indices_end = sparse_indices + entry_count;
229 const ResTable_sparseTypeEntry* result =
230 std::lower_bound(sparse_indices, sparse_indices_end, entry_index,
231 [](const ResTable_sparseTypeEntry& entry, uint16_t entry_idx) {
232 return dtohs(entry.idx) < entry_idx;
233 });
Adam Lesinski73f6f9d2017-11-14 10:18:05 -0800234
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800235 if (result == sparse_indices_end || dtohs(result->idx) != entry_index) {
236 // No entry found.
237 return ResTable_type::NO_ENTRY;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700238 }
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800239
240 // Extract the offset from the entry. Each offset must be a multiple of 4 so we store it as
241 // the real offset divided by 4.
242 return uint32_t{dtohs(result->offset)} * 4u;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700243 }
244
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800245 // This type is encoded as a dense array.
246 if (entry_index >= entry_count) {
247 // This entry cannot be here.
248 return ResTable_type::NO_ENTRY;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700249 }
250
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800251 const uint32_t* entry_offsets = reinterpret_cast<const uint32_t*>(
252 reinterpret_cast<const uint8_t*>(type_chunk) + offsets_offset);
253 return dtohl(entry_offsets[entry_index]);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700254}
255
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800256const ResTable_entry* LoadedPackage::GetEntryFromOffset(const ResTable_type* type_chunk,
257 uint32_t offset) {
258 if (UNLIKELY(!VerifyResTableEntry(type_chunk, offset))) {
259 return nullptr;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700260 }
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800261 return reinterpret_cast<const ResTable_entry*>(reinterpret_cast<const uint8_t*>(type_chunk) +
262 offset + dtohl(type_chunk->entriesStart));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700263}
264
Adam Lesinski0c405242017-01-13 20:47:26 -0800265void LoadedPackage::CollectConfigurations(bool exclude_mipmap,
266 std::set<ResTable_config>* out_configs) const {
267 const static std::u16string kMipMap = u"mipmap";
268 const size_t type_count = type_specs_.size();
269 for (size_t i = 0; i < type_count; i++) {
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800270 const TypeSpecPtr& type_spec = type_specs_[i];
Adam Lesinski0c405242017-01-13 20:47:26 -0800271 if (type_spec != nullptr) {
272 if (exclude_mipmap) {
273 const int type_idx = type_spec->type_spec->id - 1;
274 size_t type_name_len;
275 const char16_t* type_name16 = type_string_pool_.stringAt(type_idx, &type_name_len);
276 if (type_name16 != nullptr) {
277 if (kMipMap.compare(0, std::u16string::npos, type_name16, type_name_len) == 0) {
278 // This is a mipmap type, skip collection.
279 continue;
280 }
281 }
282 const char* type_name = type_string_pool_.string8At(type_idx, &type_name_len);
283 if (type_name != nullptr) {
284 if (strncmp(type_name, "mipmap", type_name_len) == 0) {
285 // This is a mipmap type, skip collection.
286 continue;
287 }
288 }
289 }
290
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800291 const auto iter_end = type_spec->types + type_spec->type_count;
292 for (auto iter = type_spec->types; iter != iter_end; ++iter) {
293 ResTable_config config;
294 config.copyFromDtoH((*iter)->config);
295 out_configs->insert(config);
Adam Lesinski0c405242017-01-13 20:47:26 -0800296 }
297 }
298 }
299}
300
301void LoadedPackage::CollectLocales(bool canonicalize, std::set<std::string>* out_locales) const {
302 char temp_locale[RESTABLE_MAX_LOCALE_LEN];
303 const size_t type_count = type_specs_.size();
304 for (size_t i = 0; i < type_count; i++) {
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800305 const TypeSpecPtr& type_spec = type_specs_[i];
Adam Lesinski0c405242017-01-13 20:47:26 -0800306 if (type_spec != nullptr) {
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800307 const auto iter_end = type_spec->types + type_spec->type_count;
308 for (auto iter = type_spec->types; iter != iter_end; ++iter) {
309 ResTable_config configuration;
310 configuration.copyFromDtoH((*iter)->config);
Adam Lesinski0c405242017-01-13 20:47:26 -0800311 if (configuration.locale != 0) {
312 configuration.getBcp47Locale(temp_locale, canonicalize);
313 std::string locale(temp_locale);
314 out_locales->insert(std::move(locale));
315 }
316 }
317 }
318 }
319}
320
Adam Lesinski929d6512017-01-16 19:11:19 -0800321uint32_t LoadedPackage::FindEntryByName(const std::u16string& type_name,
322 const std::u16string& entry_name) const {
323 ssize_t type_idx = type_string_pool_.indexOfString(type_name.data(), type_name.size());
324 if (type_idx < 0) {
325 return 0u;
326 }
327
328 ssize_t key_idx = key_string_pool_.indexOfString(entry_name.data(), entry_name.size());
329 if (key_idx < 0) {
330 return 0u;
331 }
332
333 const TypeSpec* type_spec = type_specs_[type_idx].get();
334 if (type_spec == nullptr) {
335 return 0u;
336 }
337
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800338 const auto iter_end = type_spec->types + type_spec->type_count;
339 for (auto iter = type_spec->types; iter != iter_end; ++iter) {
340 const ResTable_type* type = *iter;
341 size_t entry_count = dtohl(type->entryCount);
Adam Lesinski929d6512017-01-16 19:11:19 -0800342 for (size_t entry_idx = 0; entry_idx < entry_count; entry_idx++) {
343 const uint32_t* entry_offsets = reinterpret_cast<const uint32_t*>(
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800344 reinterpret_cast<const uint8_t*>(type) + dtohs(type->header.headerSize));
Adam Lesinski929d6512017-01-16 19:11:19 -0800345 const uint32_t offset = dtohl(entry_offsets[entry_idx]);
346 if (offset != ResTable_type::NO_ENTRY) {
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800347 const ResTable_entry* entry = reinterpret_cast<const ResTable_entry*>(
348 reinterpret_cast<const uint8_t*>(type) + dtohl(type->entriesStart) + offset);
Adam Lesinski929d6512017-01-16 19:11:19 -0800349 if (dtohl(entry->key.index) == static_cast<uint32_t>(key_idx)) {
350 // The package ID will be overridden by the caller (due to runtime assignment of package
351 // IDs for shared libraries).
352 return make_resid(0x00, type_idx + type_id_offset_ + 1, entry_idx);
353 }
354 }
355 }
356 }
357 return 0u;
358}
359
Adam Lesinski64ee69d2018-01-08 17:38:30 -0800360const LoadedPackage* LoadedArsc::GetPackageById(uint8_t package_id) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700361 for (const auto& loaded_package : packages_) {
362 if (loaded_package->GetPackageId() == package_id) {
363 return loaded_package.get();
364 }
365 }
366 return nullptr;
367}
368
369std::unique_ptr<const LoadedPackage> LoadedPackage::Load(const Chunk& chunk,
370 const LoadedIdmap* loaded_idmap,
371 bool system, bool load_as_shared_library) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700372 ATRACE_CALL();
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700373 std::unique_ptr<LoadedPackage> loaded_package(new LoadedPackage());
Adam Lesinskida431a22016-12-29 16:08:16 -0500374
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700375 // typeIdOffset was added at some point, but we still must recognize apps built before this
376 // was added.
Adam Lesinski33af6c72017-03-29 13:00:35 -0700377 constexpr size_t kMinPackageSize =
378 sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset);
379 const ResTable_package* header = chunk.header<ResTable_package, kMinPackageSize>();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700380 if (header == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800381 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500382 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700383 }
384
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700385 loaded_package->system_ = system;
386
Adam Lesinski7ad11102016-10-28 16:39:15 -0700387 loaded_package->package_id_ = dtohl(header->id);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700388 if (loaded_package->package_id_ == 0 ||
389 (loaded_package->package_id_ == kAppPackageId && load_as_shared_library)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500390 // Package ID of 0 means this is a shared library.
391 loaded_package->dynamic_ = true;
392 }
393
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700394 if (loaded_idmap != nullptr) {
395 // This is an overlay and so it needs to pretend to be the target package.
396 loaded_package->package_id_ = loaded_idmap->TargetPackageId();
397 loaded_package->overlay_ = true;
398 }
399
Adam Lesinskic6aada92017-01-13 15:34:14 -0800400 if (header->header.headerSize >= sizeof(ResTable_package)) {
401 uint32_t type_id_offset = dtohl(header->typeIdOffset);
402 if (type_id_offset > std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800403 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE type ID offset too large.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800404 return {};
405 }
406 loaded_package->type_id_offset_ = static_cast<int>(type_id_offset);
407 }
408
Adam Lesinskida431a22016-12-29 16:08:16 -0500409 util::ReadUtf16StringFromDevice(header->name, arraysize(header->name),
410 &loaded_package->package_name_);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700411
412 // A TypeSpec builder. We use this to accumulate the set of Types
413 // available for a TypeSpec, and later build a single, contiguous block
414 // of memory that holds all the Types together with the TypeSpec.
415 std::unique_ptr<TypeSpecPtrBuilder> types_builder;
416
417 // Keep track of the last seen type index. Since type IDs are 1-based,
418 // this records their index, which is 0-based (type ID - 1).
419 uint8_t last_type_idx = 0;
420
421 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
422 while (iter.HasNext()) {
423 const Chunk child_chunk = iter.Next();
424 switch (child_chunk.type()) {
425 case RES_STRING_POOL_TYPE: {
426 const uintptr_t pool_address =
427 reinterpret_cast<uintptr_t>(child_chunk.header<ResChunk_header>());
428 const uintptr_t header_address = reinterpret_cast<uintptr_t>(header);
429 if (pool_address == header_address + dtohl(header->typeStrings)) {
430 // This string pool is the type string pool.
431 status_t err = loaded_package->type_string_pool_.setTo(
432 child_chunk.header<ResStringPool_header>(), child_chunk.size());
433 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800434 LOG(ERROR) << "RES_STRING_POOL_TYPE for types corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500435 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700436 }
437 } else if (pool_address == header_address + dtohl(header->keyStrings)) {
438 // This string pool is the key string pool.
439 status_t err = loaded_package->key_string_pool_.setTo(
440 child_chunk.header<ResStringPool_header>(), child_chunk.size());
441 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800442 LOG(ERROR) << "RES_STRING_POOL_TYPE for keys corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500443 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700444 }
445 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800446 LOG(WARNING) << "Too many RES_STRING_POOL_TYPEs found in RES_TABLE_PACKAGE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700447 }
448 } break;
449
450 case RES_TABLE_TYPE_SPEC_TYPE: {
451 ATRACE_NAME("LoadTableTypeSpec");
452
453 // Starting a new TypeSpec, so finish the old one if there was one.
454 if (types_builder) {
455 TypeSpecPtr type_spec_ptr = types_builder->Build();
456 if (type_spec_ptr == nullptr) {
457 LOG(ERROR) << "Too many type configurations, overflow detected.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500458 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700459 }
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700460
461 // We only add the type to the package if there is no IDMAP, or if the type is
462 // overlaying something.
463 if (loaded_idmap == nullptr || type_spec_ptr->idmap_entries != nullptr) {
464 // If this is an overlay, insert it at the target type ID.
465 if (type_spec_ptr->idmap_entries != nullptr) {
466 last_type_idx = dtohs(type_spec_ptr->idmap_entries->target_type_id) - 1;
467 }
468 loaded_package->type_specs_.editItemAt(last_type_idx) = std::move(type_spec_ptr);
469 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700470
471 types_builder = {};
472 last_type_idx = 0;
473 }
474
475 const ResTable_typeSpec* type_spec = child_chunk.header<ResTable_typeSpec>();
476 if (type_spec == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800477 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500478 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700479 }
480
481 if (type_spec->id == 0) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800482 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500483 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700484 }
485
Adam Lesinskic6aada92017-01-13 15:34:14 -0800486 if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) >
487 std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800488 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has out of range ID.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800489 return {};
490 }
491
Adam Lesinski7ad11102016-10-28 16:39:15 -0700492 // The data portion of this chunk contains entry_count 32bit entries,
493 // each one representing a set of flags.
494 // Here we only validate that the chunk is well formed.
495 const size_t entry_count = dtohl(type_spec->entryCount);
496
497 // There can only be 2^16 entries in a type, because that is the ID
498 // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
499 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800500 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has too many entries (" << entry_count << ").";
Adam Lesinskida431a22016-12-29 16:08:16 -0500501 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700502 }
503
504 if (entry_count * sizeof(uint32_t) > chunk.data_size()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800505 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500506 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700507 }
508
509 last_type_idx = type_spec->id - 1;
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700510
511 // If this is an overlay, associate the mapping of this type to the target type
512 // from the IDMAP.
513 const IdmapEntry_header* idmap_entry_header = nullptr;
514 if (loaded_idmap != nullptr) {
515 idmap_entry_header = loaded_idmap->GetEntryMapForType(type_spec->id);
516 }
517
518 types_builder = util::make_unique<TypeSpecPtrBuilder>(type_spec, idmap_entry_header);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700519 } break;
520
521 case RES_TABLE_TYPE_TYPE: {
Adam Lesinski136fd072017-03-03 13:50:21 -0800522 const ResTable_type* type = child_chunk.header<ResTable_type, kResTableTypeMinSize>();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700523 if (type == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800524 LOG(ERROR) << "RES_TABLE_TYPE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500525 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700526 }
527
Adam Lesinski498f6052017-11-29 13:24:29 -0800528 if (!VerifyResTableType(type)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500529 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700530 }
531
532 // Type chunks must be preceded by their TypeSpec chunks.
533 if (!types_builder || type->id - 1 != last_type_idx) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800534 LOG(ERROR) << "RES_TABLE_TYPE_TYPE found without preceding RES_TABLE_TYPE_SPEC_TYPE.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500535 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700536 }
537
Adam Lesinski7ad11102016-10-28 16:39:15 -0700538 types_builder->AddType(type);
539 } break;
540
Adam Lesinskida431a22016-12-29 16:08:16 -0500541 case RES_TABLE_LIBRARY_TYPE: {
542 const ResTable_lib_header* lib = child_chunk.header<ResTable_lib_header>();
543 if (lib == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800544 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500545 return {};
546 }
547
548 if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800549 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500550 return {};
551 }
552
553 loaded_package->dynamic_package_map_.reserve(dtohl(lib->count));
554
555 const ResTable_lib_entry* const entry_begin =
556 reinterpret_cast<const ResTable_lib_entry*>(child_chunk.data_ptr());
557 const ResTable_lib_entry* const entry_end = entry_begin + dtohl(lib->count);
558 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
559 std::string package_name;
560 util::ReadUtf16StringFromDevice(entry_iter->packageName,
561 arraysize(entry_iter->packageName), &package_name);
562
563 if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) {
564 LOG(ERROR) << base::StringPrintf(
565 "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.",
566 dtohl(entry_iter->packageId), package_name.c_str());
567 return {};
568 }
569
570 loaded_package->dynamic_package_map_.emplace_back(std::move(package_name),
571 dtohl(entry_iter->packageId));
572 }
573
574 } break;
575
Adam Lesinski7ad11102016-10-28 16:39:15 -0700576 default:
577 LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type());
578 break;
579 }
580 }
581
582 // Finish the last TypeSpec.
583 if (types_builder) {
584 TypeSpecPtr type_spec_ptr = types_builder->Build();
585 if (type_spec_ptr == nullptr) {
586 LOG(ERROR) << "Too many type configurations, overflow detected.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500587 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700588 }
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700589
590 // We only add the type to the package if there is no IDMAP, or if the type is
591 // overlaying something.
592 if (loaded_idmap == nullptr || type_spec_ptr->idmap_entries != nullptr) {
593 // If this is an overlay, insert it at the target type ID.
594 if (type_spec_ptr->idmap_entries != nullptr) {
595 last_type_idx = dtohs(type_spec_ptr->idmap_entries->target_type_id) - 1;
596 }
597 loaded_package->type_specs_.editItemAt(last_type_idx) = std::move(type_spec_ptr);
598 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700599 }
600
601 if (iter.HadError()) {
602 LOG(ERROR) << iter.GetLastError();
Adam Lesinskida431a22016-12-29 16:08:16 -0500603 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700604 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700605 return std::move(loaded_package);
606}
607
Adam Lesinski7ad11102016-10-28 16:39:15 -0700608
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700609bool LoadedArsc::LoadTable(const Chunk& chunk, const LoadedIdmap* loaded_idmap,
610 bool load_as_shared_library) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700611 ATRACE_CALL();
612 const ResTable_header* header = chunk.header<ResTable_header>();
613 if (header == nullptr) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800614 LOG(ERROR) << "RES_TABLE_TYPE too small.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700615 return false;
616 }
617
618 const size_t package_count = dtohl(header->packageCount);
619 size_t packages_seen = 0;
620
621 packages_.reserve(package_count);
622
623 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
624 while (iter.HasNext()) {
625 const Chunk child_chunk = iter.Next();
626 switch (child_chunk.type()) {
627 case RES_STRING_POOL_TYPE:
628 // Only use the first string pool. Ignore others.
629 if (global_string_pool_.getError() == NO_INIT) {
630 status_t err = global_string_pool_.setTo(child_chunk.header<ResStringPool_header>(),
631 child_chunk.size());
632 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800633 LOG(ERROR) << "RES_STRING_POOL_TYPE corrupt.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700634 return false;
635 }
636 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800637 LOG(WARNING) << "Multiple RES_STRING_POOL_TYPEs found in RES_TABLE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700638 }
639 break;
640
641 case RES_TABLE_PACKAGE_TYPE: {
642 if (packages_seen + 1 > package_count) {
643 LOG(ERROR) << "More package chunks were found than the " << package_count
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700644 << " declared in the header.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700645 return false;
646 }
647 packages_seen++;
648
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700649 std::unique_ptr<const LoadedPackage> loaded_package =
650 LoadedPackage::Load(child_chunk, loaded_idmap, system_, load_as_shared_library);
Adam Lesinskida431a22016-12-29 16:08:16 -0500651 if (!loaded_package) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700652 return false;
653 }
654 packages_.push_back(std::move(loaded_package));
655 } break;
656
657 default:
658 LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type());
659 break;
660 }
661 }
662
663 if (iter.HadError()) {
664 LOG(ERROR) << iter.GetLastError();
665 return false;
666 }
667 return true;
668}
669
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700670std::unique_ptr<const LoadedArsc> LoadedArsc::Load(const StringPiece& data,
671 const LoadedIdmap* loaded_idmap, bool system,
Adam Lesinski0c405242017-01-13 20:47:26 -0800672 bool load_as_shared_library) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700673 ATRACE_CALL();
674
675 // Not using make_unique because the constructor is private.
676 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
Adam Lesinski0c405242017-01-13 20:47:26 -0800677 loaded_arsc->system_ = system;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700678
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700679 ChunkIterator iter(data.data(), data.size());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700680 while (iter.HasNext()) {
681 const Chunk chunk = iter.Next();
682 switch (chunk.type()) {
683 case RES_TABLE_TYPE:
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700684 if (!loaded_arsc->LoadTable(chunk, loaded_idmap, load_as_shared_library)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700685 return {};
686 }
687 break;
688
689 default:
690 LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type());
691 break;
692 }
693 }
694
695 if (iter.HadError()) {
696 LOG(ERROR) << iter.GetLastError();
697 return {};
698 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800699
700 // Need to force a move for mingw32.
701 return std::move(loaded_arsc);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700702}
703
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700704std::unique_ptr<const LoadedArsc> LoadedArsc::CreateEmpty() {
705 return std::unique_ptr<LoadedArsc>(new LoadedArsc());
706}
707
Adam Lesinski7ad11102016-10-28 16:39:15 -0700708} // namespace android