blob: 7cebb6d7950297d63d72f1738db5b9ffcaa30143 [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 Lesinski73f6f9da2017-11-14 10:18:05 -080021#include <algorithm>
Adam Lesinski7ad11102016-10-28 16:39:15 -070022#include <cstddef>
23#include <limits>
Yurii Zubrytskyi4f48ffd2023-11-10 15:38:16 -080024#include <optional>
Adam Lesinski7ad11102016-10-28 16:39:15 -070025
26#include "android-base/logging.h"
27#include "android-base/stringprintf.h"
28#include "utils/ByteOrder.h"
29#include "utils/Trace.h"
30
31#ifdef _WIN32
32#ifdef ERROR
33#undef ERROR
34#endif
35#endif
36
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
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000041using android::base::StringPrintf;
Adam Lesinski7ad11102016-10-28 16:39:15 -070042
43namespace android {
44
Ryan Mitchell2fedba92021-04-23 07:47:38 -070045constexpr const static int kFrameworkPackageId = 0x01;
Adam Lesinskida431a22016-12-29 16:08:16 -050046constexpr const static int kAppPackageId = 0x7f;
Adam Lesinski7ad11102016-10-28 16:39:15 -070047
Adam Lesinskida431a22016-12-29 16:08:16 -050048namespace {
49
Adam Lesinski7ad11102016-10-28 16:39:15 -070050// Builder that helps accumulate Type structs and then create a single
51// contiguous block of memory to store both the TypeSpec struct and
52// the Type structs.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080053struct TypeSpecBuilder {
Yurii Zubrytskyi4f48ffd2023-11-10 15:38:16 -080054 explicit TypeSpecBuilder(incfs::verified_map_ptr<ResTable_typeSpec> header) : header_(header) {
55 type_entries.reserve(dtohs(header_->typesCount));
56 }
Adam Lesinski7ad11102016-10-28 16:39:15 -070057
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000058 void AddType(incfs::verified_map_ptr<ResTable_type> type) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080059 TypeSpec::TypeEntry& entry = type_entries.emplace_back();
60 entry.config.copyFromDtoH(type->config);
61 entry.type = type;
Adam Lesinski7ad11102016-10-28 16:39:15 -070062 }
63
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080064 TypeSpec Build() {
Yurii Zubrytskyi4f48ffd2023-11-10 15:38:16 -080065 type_entries.shrink_to_fit();
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080066 return {header_, std::move(type_entries)};
Adam Lesinski7ad11102016-10-28 16:39:15 -070067 }
68
69 private:
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080070 DISALLOW_COPY_AND_ASSIGN(TypeSpecBuilder);
Adam Lesinski7ad11102016-10-28 16:39:15 -070071
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000072 incfs::verified_map_ptr<ResTable_typeSpec> header_;
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080073 std::vector<TypeSpec::TypeEntry> type_entries;
Adam Lesinski7ad11102016-10-28 16:39:15 -070074};
75
76} // namespace
77
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070078// Precondition: The header passed in has already been verified, so reading any fields and trusting
79// the ResChunk_header is safe.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000080static bool VerifyResTableType(incfs::map_ptr<ResTable_type> header) {
Adam Lesinski498f6052017-11-29 13:24:29 -080081 if (header->id == 0) {
82 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has invalid ID 0.";
83 return false;
84 }
85
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070086 const size_t entry_count = dtohl(header->entryCount);
87 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -080088 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has too many entries (" << entry_count << ").";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070089 return false;
90 }
91
92 // Make sure that there is enough room for the entry offsets.
93 const size_t offsets_offset = dtohs(header->header.headerSize);
94 const size_t entries_offset = dtohl(header->entriesStart);
Eric Miaoa1f2bce2022-09-12 11:37:37 -070095 const size_t offsets_length = header->flags & ResTable_type::FLAG_OFFSET16
96 ? sizeof(uint16_t) * entry_count
97 : sizeof(uint32_t) * entry_count;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070098
99 if (offsets_offset > entries_offset || entries_offset - offsets_offset < offsets_length) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800100 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700101 return false;
102 }
103
104 if (entries_offset > dtohl(header->header.size)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800105 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets extend beyond chunk.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700106 return false;
107 }
108
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000109 if (entries_offset & 0x03U) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800110 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entries start at unaligned address.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700111 return false;
112 }
113 return true;
114}
115
Eric Miao368cd192022-09-09 15:46:14 -0700116static base::expected<incfs::verified_map_ptr<ResTable_entry>, NullOrIOError>
117VerifyResTableEntry(incfs::verified_map_ptr<ResTable_type> type, uint32_t entry_offset) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700118 // Check that the offset is aligned.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000119 if (UNLIKELY(entry_offset & 0x03U)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800120 LOG(ERROR) << "Entry at offset " << entry_offset << " is not 4-byte aligned.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000121 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700122 }
123
124 // Check that the offset doesn't overflow.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000125 if (UNLIKELY(entry_offset > std::numeric_limits<uint32_t>::max() - dtohl(type->entriesStart))) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700126 // Overflow in offset.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800127 LOG(ERROR) << "Entry at offset " << entry_offset << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000128 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700129 }
130
131 const size_t chunk_size = dtohl(type->header.size);
132
133 entry_offset += dtohl(type->entriesStart);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000134 if (UNLIKELY(entry_offset > chunk_size - sizeof(ResTable_entry))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800135 LOG(ERROR) << "Entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700136 << " is too large. No room for ResTable_entry.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000137 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700138 }
139
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000140 auto entry = type.offset(entry_offset).convert<ResTable_entry>();
141 if (UNLIKELY(!entry)) {
142 return base::unexpected(IOError::PAGES_MISSING);
143 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700144
Eric Miao368cd192022-09-09 15:46:14 -0700145 const size_t entry_size = entry->size();
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000146 if (UNLIKELY(entry_size < sizeof(entry.value()))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800147 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700148 << " is too small.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000149 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700150 }
151
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000152 if (UNLIKELY(entry_size > chunk_size || entry_offset > chunk_size - entry_size)) {
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 large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000155 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700156 }
157
Eric Miao368cd192022-09-09 15:46:14 -0700158 // If entry is compact, value is already encoded, and a compact entry
159 // cannot be a map_entry, we are done verifying
160 if (entry->is_compact())
161 return entry.verified();
162
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700163 if (entry_size < sizeof(ResTable_map_entry)) {
164 // There needs to be room for one Res_value struct.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000165 if (UNLIKELY(entry_offset + entry_size > chunk_size - sizeof(Res_value))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800166 LOG(ERROR) << "No room for Res_value after ResTable_entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700167 << " for type " << (int)type->id << ".";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000168 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700169 }
170
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000171 auto value = entry.offset(entry_size).convert<Res_value>();
172 if (UNLIKELY(!value)) {
173 return base::unexpected(IOError::PAGES_MISSING);
174 }
175
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700176 const size_t value_size = dtohs(value->size);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000177 if (UNLIKELY(value_size < sizeof(Res_value))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800178 LOG(ERROR) << "Res_value at offset " << entry_offset << " is too small.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000179 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700180 }
181
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000182 if (UNLIKELY(value_size > chunk_size || entry_offset + entry_size > chunk_size - value_size)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800183 LOG(ERROR) << "Res_value size " << value_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700184 << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000185 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700186 }
187 } else {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000188 auto map = entry.convert<ResTable_map_entry>();
189 if (UNLIKELY(!map)) {
190 return base::unexpected(IOError::PAGES_MISSING);
191 }
192
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700193 const size_t map_entry_count = dtohl(map->count);
194 size_t map_entries_start = entry_offset + entry_size;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000195 if (UNLIKELY(map_entries_start & 0x03U)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800196 LOG(ERROR) << "Map entries at offset " << entry_offset << " start at unaligned offset.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000197 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700198 }
199
200 // Each entry is sizeof(ResTable_map) big.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000201 if (UNLIKELY(map_entry_count > ((chunk_size - map_entries_start) / sizeof(ResTable_map)))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800202 LOG(ERROR) << "Too many map entries in ResTable_map_entry at offset " << entry_offset << ".";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000203 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700204 }
205 }
Eric Miao368cd192022-09-09 15:46:14 -0700206 return entry.verified();
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700207}
208
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100209LoadedPackage::iterator::iterator(const LoadedPackage* lp, size_t ti, size_t ei)
210 : loadedPackage_(lp),
211 typeIndex_(ti),
212 entryIndex_(ei),
213 typeIndexEnd_(lp->resource_ids_.size() + 1) {
214 while (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] == 0) {
215 typeIndex_++;
216 }
217}
218
219LoadedPackage::iterator& LoadedPackage::iterator::operator++() {
220 while (typeIndex_ < typeIndexEnd_) {
221 if (entryIndex_ + 1 < loadedPackage_->resource_ids_[typeIndex_]) {
222 entryIndex_++;
223 break;
224 }
225 entryIndex_ = 0;
226 typeIndex_++;
227 if (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] != 0) {
228 break;
229 }
230 }
231 return *this;
232}
233
234uint32_t LoadedPackage::iterator::operator*() const {
235 if (typeIndex_ >= typeIndexEnd_) {
236 return 0;
237 }
238 return make_resid(loadedPackage_->package_id_, typeIndex_ + loadedPackage_->type_id_offset_,
239 entryIndex_);
240}
241
Eric Miao368cd192022-09-09 15:46:14 -0700242base::expected<incfs::verified_map_ptr<ResTable_entry>, NullOrIOError> LoadedPackage::GetEntry(
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000243 incfs::verified_map_ptr<ResTable_type> type_chunk, uint16_t entry_index) {
244 base::expected<uint32_t, NullOrIOError> entry_offset = GetEntryOffset(type_chunk, entry_index);
245 if (UNLIKELY(!entry_offset.has_value())) {
246 return base::unexpected(entry_offset.error());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800247 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000248 return GetEntryFromOffset(type_chunk, entry_offset.value());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800249}
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700250
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000251base::expected<uint32_t, NullOrIOError> LoadedPackage::GetEntryOffset(
252 incfs::verified_map_ptr<ResTable_type> type_chunk, uint16_t entry_index) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800253 // The configuration matches and is better than the previous selection.
254 // Find the entry value if it exists for this configuration.
255 const size_t entry_count = dtohl(type_chunk->entryCount);
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700256 const auto offsets = type_chunk.offset(dtohs(type_chunk->header.headerSize));
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700257
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800258 // Check if there is the desired entry in this type.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800259 if (type_chunk->flags & ResTable_type::FLAG_SPARSE) {
260 // This is encoded as a sparse map, so perform a binary search.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000261 bool error = false;
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700262 auto sparse_indices = offsets.convert<ResTable_sparseTypeEntry>().iterator();
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000263 auto sparse_indices_end = sparse_indices + entry_count;
264 auto result = std::lower_bound(sparse_indices, sparse_indices_end, entry_index,
265 [&error](const incfs::map_ptr<ResTable_sparseTypeEntry>& entry,
266 uint16_t entry_idx) {
267 if (UNLIKELY(!entry)) {
268 return error = true;
269 }
270 return dtohs(entry->idx) < entry_idx;
271 });
Adam Lesinski73f6f9da2017-11-14 10:18:05 -0800272
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000273 if (result == sparse_indices_end) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800274 // No entry found.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000275 return base::unexpected(std::nullopt);
276 }
277
278 const incfs::verified_map_ptr<ResTable_sparseTypeEntry> entry = (*result).verified();
279 if (dtohs(entry->idx) != entry_index) {
280 if (error) {
281 return base::unexpected(IOError::PAGES_MISSING);
282 }
283 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700284 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800285
286 // Extract the offset from the entry. Each offset must be a multiple of 4 so we store it as
287 // the real offset divided by 4.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000288 return uint32_t{dtohs(entry->offset)} * 4u;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700289 }
290
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800291 // This type is encoded as a dense array.
292 if (entry_index >= entry_count) {
293 // This entry cannot be here.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000294 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700295 }
296
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700297 uint32_t result;
298
299 if (type_chunk->flags & ResTable_type::FLAG_OFFSET16) {
300 const auto entry_offset_ptr = offsets.convert<uint16_t>() + entry_index;
301 if (UNLIKELY(!entry_offset_ptr)) {
302 return base::unexpected(IOError::PAGES_MISSING);
303 }
304 result = offset_from16(entry_offset_ptr.value());
305 } else {
306 const auto entry_offset_ptr = offsets.convert<uint32_t>() + entry_index;
307 if (UNLIKELY(!entry_offset_ptr)) {
308 return base::unexpected(IOError::PAGES_MISSING);
309 }
310 result = dtohl(entry_offset_ptr.value());
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700311 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000312
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700313 if (result == ResTable_type::NO_ENTRY) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000314 return base::unexpected(std::nullopt);
315 }
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700316 return result;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700317}
318
Eric Miao368cd192022-09-09 15:46:14 -0700319base::expected<incfs::verified_map_ptr<ResTable_entry>, NullOrIOError>
320LoadedPackage::GetEntryFromOffset(incfs::verified_map_ptr<ResTable_type> type_chunk,
321 uint32_t offset) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000322 auto valid = VerifyResTableEntry(type_chunk, offset);
323 if (UNLIKELY(!valid.has_value())) {
324 return base::unexpected(valid.error());
325 }
Eric Miao368cd192022-09-09 15:46:14 -0700326 return valid;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000327}
328
329base::expected<std::monostate, IOError> LoadedPackage::CollectConfigurations(
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700330 bool exclude_mipmap, std::set<ResTable_config>* out_configs) const {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800331 for (const auto& type_spec : type_specs_) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000332 if (exclude_mipmap) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800333 const int type_idx = type_spec.first - 1;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000334 const auto type_name16 = type_string_pool_.stringAt(type_idx);
335 if (UNLIKELY(IsIOError(type_name16))) {
336 return base::unexpected(GetIOError(type_name16.error()));
337 }
338 if (type_name16.has_value()) {
339 if (strncmp16(type_name16->data(), u"mipmap", type_name16->size()) == 0) {
340 // This is a mipmap type, skip collection.
341 continue;
Adam Lesinski0c405242017-01-13 20:47:26 -0800342 }
343 }
344
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000345 const auto type_name = type_string_pool_.string8At(type_idx);
346 if (UNLIKELY(IsIOError(type_name))) {
347 return base::unexpected(GetIOError(type_name.error()));
348 }
349 if (type_name.has_value()) {
350 if (strncmp(type_name->data(), "mipmap", type_name->size()) == 0) {
351 // This is a mipmap type, skip collection.
352 continue;
353 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700354 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700355 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000356
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800357 for (const auto& type_entry : type_spec.second.type_entries) {
358 out_configs->insert(type_entry.config);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000359 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800360 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000361 return {};
Adam Lesinski0c405242017-01-13 20:47:26 -0800362}
363
Yurii Zubrytskyi984a84e2025-03-20 12:17:05 -0700364void LoadedPackage::CollectLocales(bool canonicalize,
365 Locales* out_locales) const {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800366 for (const auto& type_spec : type_specs_) {
367 for (const auto& type_entry : type_spec.second.type_entries) {
368 if (type_entry.config.locale != 0) {
Yurii Zubrytskyi984a84e2025-03-20 12:17:05 -0700369 char temp_locale[RESTABLE_MAX_LOCALE_LEN];
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800370 type_entry.config.getBcp47Locale(temp_locale, canonicalize);
Yurii Zubrytskyi984a84e2025-03-20 12:17:05 -0700371 auto locale_sv = std::string_view(temp_locale);
372 if (auto it = out_locales->lower_bound(locale_sv);
373 it == out_locales->end() || *it != locale_sv) {
374 out_locales->emplace_hint(it, locale_sv);
375 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800376 }
377 }
378 }
379}
380
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000381base::expected<uint32_t, NullOrIOError> LoadedPackage::FindEntryByName(
382 const std::u16string& type_name, const std::u16string& entry_name) const {
383 const base::expected<size_t, NullOrIOError> type_idx = type_string_pool_.indexOfString(
384 type_name.data(), type_name.size());
385 if (!type_idx.has_value()) {
386 return base::unexpected(type_idx.error());
Adam Lesinski929d6512017-01-16 19:11:19 -0800387 }
388
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000389 const base::expected<size_t, NullOrIOError> key_idx = key_string_pool_.indexOfString(
390 entry_name.data(), entry_name.size());
391 if (!key_idx.has_value()) {
392 return base::unexpected(key_idx.error());
Adam Lesinski929d6512017-01-16 19:11:19 -0800393 }
394
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800395 const TypeSpec* type_spec = GetTypeSpecByTypeIndex(*type_idx);
Adam Lesinski929d6512017-01-16 19:11:19 -0800396 if (type_spec == nullptr) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000397 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -0800398 }
399
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800400 for (const auto& type_entry : type_spec->type_entries) {
401 const incfs::verified_map_ptr<ResTable_type>& type = type_entry.type;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000402
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700403 const size_t entry_count = dtohl(type->entryCount);
404 const auto entry_offsets = type.offset(dtohs(type->header.headerSize));
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000405
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700406 for (size_t entry_idx = 0; entry_idx < entry_count; entry_idx++) {
Jason O'Brien984e8972021-08-26 19:07:47 -0500407 uint32_t offset;
408 uint16_t res_idx;
409 if (type->flags & ResTable_type::FLAG_SPARSE) {
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700410 auto sparse_entry = entry_offsets.convert<ResTable_sparseTypeEntry>() + entry_idx;
411 if (!sparse_entry) {
412 return base::unexpected(IOError::PAGES_MISSING);
413 }
Jason O'Brien984e8972021-08-26 19:07:47 -0500414 offset = dtohs(sparse_entry->offset) * 4u;
415 res_idx = dtohs(sparse_entry->idx);
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700416 } else if (type->flags & ResTable_type::FLAG_OFFSET16) {
417 auto entry = entry_offsets.convert<uint16_t>() + entry_idx;
418 if (!entry) {
419 return base::unexpected(IOError::PAGES_MISSING);
420 }
421 offset = offset_from16(entry.value());
422 res_idx = entry_idx;
Jason O'Brien984e8972021-08-26 19:07:47 -0500423 } else {
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700424 auto entry = entry_offsets.convert<uint32_t>() + entry_idx;
425 if (!entry) {
426 return base::unexpected(IOError::PAGES_MISSING);
427 }
428 offset = dtohl(entry.value());
Jason O'Brien984e8972021-08-26 19:07:47 -0500429 res_idx = entry_idx;
430 }
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700431
Adam Lesinski929d6512017-01-16 19:11:19 -0800432 if (offset != ResTable_type::NO_ENTRY) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000433 auto entry = type.offset(dtohl(type->entriesStart) + offset).convert<ResTable_entry>();
434 if (!entry) {
435 return base::unexpected(IOError::PAGES_MISSING);
436 }
437
Eric Miao368cd192022-09-09 15:46:14 -0700438 if (entry->key() == static_cast<uint32_t>(*key_idx)) {
Adam Lesinski929d6512017-01-16 19:11:19 -0800439 // The package ID will be overridden by the caller (due to runtime assignment of package
440 // IDs for shared libraries).
Jason O'Brien984e8972021-08-26 19:07:47 -0500441 return make_resid(0x00, *type_idx + type_id_offset_ + 1, res_idx);
Adam Lesinski929d6512017-01-16 19:11:19 -0800442 }
443 }
444 }
445 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000446 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -0800447}
448
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800449const LoadedPackage* LoadedArsc::GetPackageById(uint8_t package_id) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700450 for (const auto& loaded_package : packages_) {
451 if (loaded_package->GetPackageId() == package_id) {
452 return loaded_package.get();
453 }
454 }
455 return nullptr;
456}
457
458std::unique_ptr<const LoadedPackage> LoadedPackage::Load(const Chunk& chunk,
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800459 package_property_t property_flags) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800460 ATRACE_NAME("LoadedPackage::Load");
Yurii Zubrytskyi596fa172023-11-10 16:05:38 -0800461 const bool optimize_name_lookups = (property_flags & PROPERTY_OPTIMIZE_NAME_LOOKUPS) != 0;
462 std::unique_ptr<LoadedPackage> loaded_package(new LoadedPackage(optimize_name_lookups));
Adam Lesinskida431a22016-12-29 16:08:16 -0500463
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700464 // typeIdOffset was added at some point, but we still must recognize apps built before this
465 // was added.
Adam Lesinski33af6c72017-03-29 13:00:35 -0700466 constexpr size_t kMinPackageSize =
467 sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000468 const incfs::map_ptr<ResTable_package> header = chunk.header<ResTable_package, kMinPackageSize>();
469 if (!header) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800470 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500471 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700472 }
473
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800474 if ((property_flags & PROPERTY_SYSTEM) != 0) {
475 loaded_package->property_flags_ |= PROPERTY_SYSTEM;
476 }
477
478 if ((property_flags & PROPERTY_LOADER) != 0) {
479 loaded_package->property_flags_ |= PROPERTY_LOADER;
480 }
481
482 if ((property_flags & PROPERTY_OVERLAY) != 0) {
483 // Overlay resources must have an exclusive resource id space for referencing internal
484 // resources.
485 loaded_package->property_flags_ |= PROPERTY_OVERLAY | PROPERTY_DYNAMIC;
486 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700487
Adam Lesinski7ad11102016-10-28 16:39:15 -0700488 loaded_package->package_id_ = dtohl(header->id);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700489 if (loaded_package->package_id_ == 0 ||
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800490 (loaded_package->package_id_ == kAppPackageId && (property_flags & PROPERTY_DYNAMIC) != 0)) {
491 loaded_package->property_flags_ |= PROPERTY_DYNAMIC;
Winson9947f1e2019-08-16 10:20:39 -0700492 }
493
Adam Lesinskic6aada92017-01-13 15:34:14 -0800494 if (header->header.headerSize >= sizeof(ResTable_package)) {
495 uint32_t type_id_offset = dtohl(header->typeIdOffset);
496 if (type_id_offset > std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800497 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE type ID offset too large.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800498 return {};
499 }
500 loaded_package->type_id_offset_ = static_cast<int>(type_id_offset);
501 }
502
Adam Lesinskida431a22016-12-29 16:08:16 -0500503 util::ReadUtf16StringFromDevice(header->name, arraysize(header->name),
504 &loaded_package->package_name_);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700505
Yurii Zubrytskyi75663392023-05-02 10:53:14 -0700506 const bool only_overlayable = (property_flags & PROPERTY_ONLY_OVERLAYABLES) != 0;
507
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800508 // A map of TypeSpec builders, each associated with an type index.
509 // We use these to accumulate the set of Types available for a TypeSpec, and later build a single,
510 // contiguous block of memory that holds all the Types together with the TypeSpec.
Yurii Zubrytskyi4f48ffd2023-11-10 15:38:16 -0800511 std::unordered_map<int, std::optional<TypeSpecBuilder>> type_builder_map;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700512
513 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
514 while (iter.HasNext()) {
515 const Chunk child_chunk = iter.Next();
Yurii Zubrytskyi75663392023-05-02 10:53:14 -0700516 if (only_overlayable && child_chunk.type() != RES_TABLE_OVERLAYABLE_TYPE) {
517 continue;
518 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700519 switch (child_chunk.type()) {
520 case RES_STRING_POOL_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000521 const auto pool_address = child_chunk.header<ResChunk_header>();
522 if (!pool_address) {
523 LOG(ERROR) << "RES_STRING_POOL_TYPE is incomplete due to incremental installation.";
524 return {};
525 }
526
527 if (pool_address == header.offset(dtohl(header->typeStrings)).convert<ResChunk_header>()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700528 // This string pool is the type string pool.
529 status_t err = loaded_package->type_string_pool_.setTo(
530 child_chunk.header<ResStringPool_header>(), child_chunk.size());
531 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800532 LOG(ERROR) << "RES_STRING_POOL_TYPE for types corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500533 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700534 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000535 } else if (pool_address == header.offset(dtohl(header->keyStrings))
536 .convert<ResChunk_header>()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700537 // This string pool is the key string pool.
538 status_t err = loaded_package->key_string_pool_.setTo(
539 child_chunk.header<ResStringPool_header>(), child_chunk.size());
540 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800541 LOG(ERROR) << "RES_STRING_POOL_TYPE for keys corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500542 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700543 }
544 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800545 LOG(WARNING) << "Too many RES_STRING_POOL_TYPEs found in RES_TABLE_PACKAGE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700546 }
547 } break;
548
549 case RES_TABLE_TYPE_SPEC_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000550 const auto type_spec = child_chunk.header<ResTable_typeSpec>();
551 if (!type_spec) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800552 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500553 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700554 }
555
556 if (type_spec->id == 0) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800557 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500558 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700559 }
560
Adam Lesinskic6aada92017-01-13 15:34:14 -0800561 if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) >
562 std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800563 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has out of range ID.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800564 return {};
565 }
566
Adam Lesinski7ad11102016-10-28 16:39:15 -0700567 // The data portion of this chunk contains entry_count 32bit entries,
568 // each one representing a set of flags.
569 // Here we only validate that the chunk is well formed.
570 const size_t entry_count = dtohl(type_spec->entryCount);
571
572 // There can only be 2^16 entries in a type, because that is the ID
573 // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
574 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800575 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has too many entries (" << entry_count << ").";
Adam Lesinskida431a22016-12-29 16:08:16 -0500576 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700577 }
578
Yurii Zubrytskyi4f48ffd2023-11-10 15:38:16 -0800579 if (entry_count * sizeof(uint32_t) > child_chunk.data_size()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800580 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500581 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700582 }
583
Yurii Zubrytskyi4f48ffd2023-11-10 15:38:16 -0800584 auto& maybe_type_builder = type_builder_map[type_spec->id];
585 if (!maybe_type_builder) {
586 maybe_type_builder.emplace(type_spec.verified());
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100587 loaded_package->resource_ids_.set(type_spec->id, entry_count);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800588 } else {
589 LOG(WARNING) << StringPrintf("RES_TABLE_TYPE_SPEC_TYPE already defined for ID %02x",
590 type_spec->id);
591 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700592 } break;
593
594 case RES_TABLE_TYPE_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000595 const auto type = child_chunk.header<ResTable_type, kResTableTypeMinSize>();
596 if (!type) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800597 LOG(ERROR) << "RES_TABLE_TYPE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500598 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700599 }
600
Adam Lesinski498f6052017-11-29 13:24:29 -0800601 if (!VerifyResTableType(type)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500602 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700603 }
604
605 // Type chunks must be preceded by their TypeSpec chunks.
Yurii Zubrytskyi4f48ffd2023-11-10 15:38:16 -0800606 auto& maybe_type_builder = type_builder_map[type->id];
607 if (maybe_type_builder) {
608 maybe_type_builder->AddType(type.verified());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800609 } else {
610 LOG(ERROR) << StringPrintf(
611 "RES_TABLE_TYPE_TYPE with ID %02x found without preceding RES_TABLE_TYPE_SPEC_TYPE.",
612 type->id);
Adam Lesinskida431a22016-12-29 16:08:16 -0500613 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700614 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700615 } break;
616
Adam Lesinskida431a22016-12-29 16:08:16 -0500617 case RES_TABLE_LIBRARY_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000618 const auto lib = child_chunk.header<ResTable_lib_header>();
619 if (!lib) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800620 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500621 return {};
622 }
623
624 if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800625 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500626 return {};
627 }
628
629 loaded_package->dynamic_package_map_.reserve(dtohl(lib->count));
630
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000631 const auto entry_begin = child_chunk.data_ptr().convert<ResTable_lib_entry>();
632 const auto entry_end = entry_begin + dtohl(lib->count);
Adam Lesinskida431a22016-12-29 16:08:16 -0500633 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000634 if (!entry_iter) {
635 return {};
636 }
637
Adam Lesinskida431a22016-12-29 16:08:16 -0500638 std::string package_name;
639 util::ReadUtf16StringFromDevice(entry_iter->packageName,
640 arraysize(entry_iter->packageName), &package_name);
641
642 if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800643 LOG(ERROR) << StringPrintf(
Adam Lesinskida431a22016-12-29 16:08:16 -0500644 "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.",
645 dtohl(entry_iter->packageId), package_name.c_str());
646 return {};
647 }
648
649 loaded_package->dynamic_package_map_.emplace_back(std::move(package_name),
650 dtohl(entry_iter->packageId));
651 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800652 } break;
Adam Lesinskida431a22016-12-29 16:08:16 -0500653
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800654 case RES_TABLE_OVERLAYABLE_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000655 const auto overlayable = child_chunk.header<ResTable_overlayable_header>();
656 if (!overlayable) {
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800657 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_TYPE too small.";
658 return {};
659 }
660
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800661 std::string name;
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800662 util::ReadUtf16StringFromDevice(overlayable->name, std::size(overlayable->name), &name);
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800663 std::string actor;
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800664 util::ReadUtf16StringFromDevice(overlayable->actor, std::size(overlayable->actor), &actor);
Yurii Zubrytskyi9d225372022-11-29 11:12:18 -0800665 auto [name_to_actor_it, inserted] =
666 loaded_package->overlayable_map_.emplace(std::move(name), std::move(actor));
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800667 if (!inserted) {
Yurii Zubrytskyi9d225372022-11-29 11:12:18 -0800668 LOG(ERROR) << "Multiple <overlayable> blocks with the same name '"
669 << name_to_actor_it->first << "'.";
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100670 return {};
671 }
Yurii Zubrytskyi75663392023-05-02 10:53:14 -0700672 if (only_overlayable) {
673 break;
674 }
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100675
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800676 // Iterate over the overlayable policy chunks contained within the overlayable chunk data
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800677 ChunkIterator overlayable_iter(child_chunk.data_ptr(), child_chunk.data_size());
678 while (overlayable_iter.HasNext()) {
679 const Chunk overlayable_child_chunk = overlayable_iter.Next();
680
681 switch (overlayable_child_chunk.type()) {
682 case RES_TABLE_OVERLAYABLE_POLICY_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000683 const auto policy_header =
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800684 overlayable_child_chunk.header<ResTable_overlayable_policy_header>();
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000685 if (!policy_header) {
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800686 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small.";
687 return {};
688 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800689 if ((overlayable_child_chunk.data_size() / sizeof(ResTable_ref))
690 < dtohl(policy_header->entry_count)) {
691 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small to hold entries.";
692 return {};
693 }
694
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800695 // Retrieve all the resource ids belonging to this policy chunk
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000696 const auto ids_begin = overlayable_child_chunk.data_ptr().convert<ResTable_ref>();
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800697 const auto ids_end = ids_begin + dtohl(policy_header->entry_count);
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800698 std::unordered_set<uint32_t> ids;
699 ids.reserve(ids_end - ids_begin);
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800700 for (auto id_iter = ids_begin; id_iter != ids_end; ++id_iter) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000701 if (!id_iter) {
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800702 LOG(ERROR) << "NULL ResTable_ref record??";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000703 return {};
704 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800705 ids.insert(dtohl(id_iter->ident));
706 }
707
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800708 // Add the pairing of overlayable properties and resource ids to the package
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800709 OverlayableInfo overlayable_info {
Yurii Zubrytskyi9d225372022-11-29 11:12:18 -0800710 .name = name_to_actor_it->first,
711 .actor = name_to_actor_it->second,
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800712 .policy_flags = policy_header->policy_flags
713 };
714 loaded_package->overlayable_infos_.emplace_back(std::move(overlayable_info), std::move(ids));
Ryan Mitchell19823452019-01-29 12:01:24 -0800715 loaded_package->defines_overlayable_ = true;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800716 break;
717 }
718
719 default:
720 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
721 break;
722 }
723 }
724
725 if (overlayable_iter.HadError()) {
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800726 LOG(ERROR) << StringPrintf("Error parsing RES_TABLE_OVERLAYABLE_TYPE: %s",
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800727 overlayable_iter.GetLastError().c_str());
728 if (overlayable_iter.HadFatalError()) {
729 return {};
730 }
731 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500732 } break;
733
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700734 case RES_TABLE_STAGED_ALIAS_TYPE: {
735 if (loaded_package->package_id_ != kFrameworkPackageId) {
736 LOG(WARNING) << "Alias chunk ignored for non-framework package '"
737 << loaded_package->package_name_ << "'";
738 break;
739 }
740
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700741 const auto lib_alias = child_chunk.header<ResTable_staged_alias_header>();
742 if (!lib_alias) {
Yurii Zubrytskyi80020342021-11-29 23:55:24 -0800743 LOG(ERROR) << "RES_TABLE_STAGED_ALIAS_TYPE is too small.";
744 return {};
745 }
746 if ((child_chunk.data_size() / sizeof(ResTable_staged_alias_entry))
747 < dtohl(lib_alias->count)) {
748 LOG(ERROR) << "RES_TABLE_STAGED_ALIAS_TYPE is too small to hold entries.";
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700749 return {};
750 }
751 const auto entry_begin = child_chunk.data_ptr().convert<ResTable_staged_alias_entry>();
752 const auto entry_end = entry_begin + dtohl(lib_alias->count);
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800753 std::unordered_set<uint32_t> finalized_ids;
754 finalized_ids.reserve(entry_end - entry_begin);
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800755 loaded_package->alias_id_map_.reserve(entry_end - entry_begin);
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700756 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
757 if (!entry_iter) {
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800758 LOG(ERROR) << "NULL ResTable_staged_alias_entry record??";
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700759 return {};
760 }
761 auto finalized_id = dtohl(entry_iter->finalizedResId);
762 if (!finalized_ids.insert(finalized_id).second) {
763 LOG(ERROR) << StringPrintf("Repeated finalized resource id '%08x' in staged aliases.",
764 finalized_id);
765 return {};
766 }
767
768 auto staged_id = dtohl(entry_iter->stagedResId);
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800769 loaded_package->alias_id_map_.emplace_back(staged_id, finalized_id);
770 }
771
772 std::sort(loaded_package->alias_id_map_.begin(), loaded_package->alias_id_map_.end(),
773 [](auto&& l, auto&& r) { return l.first < r.first; });
774 const auto duplicate_it =
775 std::adjacent_find(loaded_package->alias_id_map_.begin(),
776 loaded_package->alias_id_map_.end(),
777 [](auto&& l, auto&& r) { return l.first == r.first; });
778 if (duplicate_it != loaded_package->alias_id_map_.end()) {
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700779 LOG(ERROR) << StringPrintf("Repeated staged resource id '%08x' in staged aliases.",
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800780 duplicate_it->first);
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700781 return {};
782 }
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700783 } break;
784
Adam Lesinski7ad11102016-10-28 16:39:15 -0700785 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800786 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700787 break;
788 }
789 }
790
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800791 if (iter.HadError()) {
792 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700793 if (iter.HadFatalError()) {
794 return {};
795 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800796 }
797
798 // Flatten and construct the TypeSpecs.
799 for (auto& entry : type_builder_map) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800800 TypeSpec type_spec = entry.second->Build();
801 uint8_t type_id = static_cast<uint8_t>(entry.first);
802 loaded_package->type_specs_[type_id] = std::move(type_spec);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700803 }
804
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700805 return std::move(loaded_package);
806}
807
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700808bool LoadedArsc::LoadTable(const Chunk& chunk, const LoadedIdmap* loaded_idmap,
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800809 package_property_t property_flags) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000810 incfs::map_ptr<ResTable_header> header = chunk.header<ResTable_header>();
811 if (!header) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800812 LOG(ERROR) << "RES_TABLE_TYPE too small.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700813 return false;
814 }
815
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700816 if (loaded_idmap != nullptr) {
817 global_string_pool_ = util::make_unique<OverlayStringPool>(loaded_idmap);
818 }
819
Yurii Zubrytskyi75663392023-05-02 10:53:14 -0700820 const bool only_overlayable = (property_flags & PROPERTY_ONLY_OVERLAYABLES) != 0;
821
Adam Lesinski7ad11102016-10-28 16:39:15 -0700822 const size_t package_count = dtohl(header->packageCount);
823 size_t packages_seen = 0;
824
Yurii Zubrytskyi75663392023-05-02 10:53:14 -0700825 if (!only_overlayable) {
826 packages_.reserve(package_count);
827 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700828
829 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
830 while (iter.HasNext()) {
831 const Chunk child_chunk = iter.Next();
Yurii Zubrytskyi75663392023-05-02 10:53:14 -0700832 if (only_overlayable && child_chunk.type() != RES_TABLE_PACKAGE_TYPE) {
833 continue;
834 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700835 switch (child_chunk.type()) {
836 case RES_STRING_POOL_TYPE:
837 // Only use the first string pool. Ignore others.
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700838 if (global_string_pool_->getError() == NO_INIT) {
839 status_t err = global_string_pool_->setTo(child_chunk.header<ResStringPool_header>(),
840 child_chunk.size());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700841 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800842 LOG(ERROR) << "RES_STRING_POOL_TYPE corrupt.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700843 return false;
844 }
845 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800846 LOG(WARNING) << "Multiple RES_STRING_POOL_TYPEs found in RES_TABLE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700847 }
848 break;
849
850 case RES_TABLE_PACKAGE_TYPE: {
851 if (packages_seen + 1 > package_count) {
852 LOG(ERROR) << "More package chunks were found than the " << package_count
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700853 << " declared in the header.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700854 return false;
855 }
856 packages_seen++;
857
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700858 std::unique_ptr<const LoadedPackage> loaded_package =
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800859 LoadedPackage::Load(child_chunk, property_flags);
Adam Lesinskida431a22016-12-29 16:08:16 -0500860 if (!loaded_package) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700861 return false;
862 }
863 packages_.push_back(std::move(loaded_package));
Yurii Zubrytskyi75663392023-05-02 10:53:14 -0700864 if (only_overlayable) {
865 // Overlayable is always in the first package, no need to process anything else.
866 return true;
867 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700868 } break;
869
870 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800871 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700872 break;
873 }
874 }
875
876 if (iter.HadError()) {
877 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700878 if (iter.HadFatalError()) {
879 return false;
880 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700881 }
882 return true;
883}
884
Jeremy Meyer65871022022-07-12 22:45:08 +0000885bool LoadedArsc::LoadStringPool(const LoadedIdmap* loaded_idmap) {
886 if (loaded_idmap != nullptr) {
887 global_string_pool_ = util::make_unique<OverlayStringPool>(loaded_idmap);
888 }
889 return true;
890}
891
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800892std::unique_ptr<LoadedArsc> LoadedArsc::Load(incfs::map_ptr<void> data,
893 const size_t length,
894 const LoadedIdmap* loaded_idmap,
895 const package_property_t property_flags) {
Winson9947f1e2019-08-16 10:20:39 -0700896 ATRACE_NAME("LoadedArsc::Load");
Adam Lesinski7ad11102016-10-28 16:39:15 -0700897
898 // Not using make_unique because the constructor is private.
899 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
900
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000901 ChunkIterator iter(data, length);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700902 while (iter.HasNext()) {
903 const Chunk chunk = iter.Next();
904 switch (chunk.type()) {
905 case RES_TABLE_TYPE:
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800906 if (!loaded_arsc->LoadTable(chunk, loaded_idmap, property_flags)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700907 return {};
908 }
909 break;
910
911 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800912 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700913 break;
914 }
915 }
916
917 if (iter.HadError()) {
918 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700919 if (iter.HadFatalError()) {
920 return {};
921 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700922 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800923
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800924 return loaded_arsc;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700925}
926
Jeremy Meyer65871022022-07-12 22:45:08 +0000927std::unique_ptr<LoadedArsc> LoadedArsc::Load(const LoadedIdmap* loaded_idmap) {
928 ATRACE_NAME("LoadedArsc::Load");
929
930 // Not using make_unique because the constructor is private.
931 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
932 loaded_arsc->LoadStringPool(loaded_idmap);
933 return loaded_arsc;
934}
935
936
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800937std::unique_ptr<LoadedArsc> LoadedArsc::CreateEmpty() {
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700938 return std::unique_ptr<LoadedArsc>(new LoadedArsc());
939}
940
Adam Lesinski7ad11102016-10-28 16:39:15 -0700941} // namespace android