blob: c0fdfe25da2193b62d979eede693961f1363ce19 [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 Lesinskida431a22016-12-29 16:08:16 -050036#include "androidfw/Chunk.h"
Adam Lesinski929d6512017-01-16 19:11:19 -080037#include "androidfw/ResourceUtils.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070038#include "androidfw/Util.h"
39
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000040using android::base::StringPrintf;
Adam Lesinski7ad11102016-10-28 16:39:15 -070041
42namespace android {
43
Ryan Mitchell2fedba92021-04-23 07:47:38 -070044constexpr const static int kFrameworkPackageId = 0x01;
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.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080052struct TypeSpecBuilder {
53 explicit TypeSpecBuilder(incfs::verified_map_ptr<ResTable_typeSpec> header) : header_(header) {}
Adam Lesinski7ad11102016-10-28 16:39:15 -070054
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000055 void AddType(incfs::verified_map_ptr<ResTable_type> type) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080056 TypeSpec::TypeEntry& entry = type_entries.emplace_back();
57 entry.config.copyFromDtoH(type->config);
58 entry.type = type;
Adam Lesinski7ad11102016-10-28 16:39:15 -070059 }
60
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080061 TypeSpec Build() {
62 return {header_, std::move(type_entries)};
Adam Lesinski7ad11102016-10-28 16:39:15 -070063 }
64
65 private:
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080066 DISALLOW_COPY_AND_ASSIGN(TypeSpecBuilder);
Adam Lesinski7ad11102016-10-28 16:39:15 -070067
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000068 incfs::verified_map_ptr<ResTable_typeSpec> header_;
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080069 std::vector<TypeSpec::TypeEntry> type_entries;
Adam Lesinski7ad11102016-10-28 16:39:15 -070070};
71
72} // namespace
73
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070074// Precondition: The header passed in has already been verified, so reading any fields and trusting
75// the ResChunk_header is safe.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000076static bool VerifyResTableType(incfs::map_ptr<ResTable_type> header) {
Adam Lesinski498f6052017-11-29 13:24:29 -080077 if (header->id == 0) {
78 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has invalid ID 0.";
79 return false;
80 }
81
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070082 const size_t entry_count = dtohl(header->entryCount);
83 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -080084 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has too many entries (" << entry_count << ").";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070085 return false;
86 }
87
88 // Make sure that there is enough room for the entry offsets.
89 const size_t offsets_offset = dtohs(header->header.headerSize);
90 const size_t entries_offset = dtohl(header->entriesStart);
Eric Miaoa1f2bce2022-09-12 11:37:37 -070091 const size_t offsets_length = header->flags & ResTable_type::FLAG_OFFSET16
92 ? sizeof(uint16_t) * entry_count
93 : sizeof(uint32_t) * entry_count;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070094
95 if (offsets_offset > entries_offset || entries_offset - offsets_offset < offsets_length) {
Adam Lesinski498f6052017-11-29 13:24:29 -080096 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070097 return false;
98 }
99
100 if (entries_offset > dtohl(header->header.size)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800101 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets extend beyond chunk.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700102 return false;
103 }
104
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000105 if (entries_offset & 0x03U) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800106 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entries start at unaligned address.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700107 return false;
108 }
109 return true;
110}
111
Eric Miao368cd192022-09-09 15:46:14 -0700112static base::expected<incfs::verified_map_ptr<ResTable_entry>, NullOrIOError>
113VerifyResTableEntry(incfs::verified_map_ptr<ResTable_type> type, uint32_t entry_offset) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700114 // Check that the offset is aligned.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000115 if (UNLIKELY(entry_offset & 0x03U)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800116 LOG(ERROR) << "Entry at offset " << entry_offset << " is not 4-byte aligned.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000117 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700118 }
119
120 // Check that the offset doesn't overflow.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000121 if (UNLIKELY(entry_offset > std::numeric_limits<uint32_t>::max() - dtohl(type->entriesStart))) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700122 // Overflow in offset.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800123 LOG(ERROR) << "Entry at offset " << entry_offset << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000124 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700125 }
126
127 const size_t chunk_size = dtohl(type->header.size);
128
129 entry_offset += dtohl(type->entriesStart);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000130 if (UNLIKELY(entry_offset > chunk_size - sizeof(ResTable_entry))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800131 LOG(ERROR) << "Entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700132 << " is too large. No room for ResTable_entry.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000133 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700134 }
135
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000136 auto entry = type.offset(entry_offset).convert<ResTable_entry>();
137 if (UNLIKELY(!entry)) {
138 return base::unexpected(IOError::PAGES_MISSING);
139 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700140
Eric Miao368cd192022-09-09 15:46:14 -0700141 const size_t entry_size = entry->size();
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000142 if (UNLIKELY(entry_size < sizeof(entry.value()))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800143 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700144 << " is too small.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000145 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700146 }
147
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000148 if (UNLIKELY(entry_size > chunk_size || entry_offset > chunk_size - entry_size)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800149 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700150 << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000151 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700152 }
153
Eric Miao368cd192022-09-09 15:46:14 -0700154 // If entry is compact, value is already encoded, and a compact entry
155 // cannot be a map_entry, we are done verifying
156 if (entry->is_compact())
157 return entry.verified();
158
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700159 if (entry_size < sizeof(ResTable_map_entry)) {
160 // There needs to be room for one Res_value struct.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000161 if (UNLIKELY(entry_offset + entry_size > chunk_size - sizeof(Res_value))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800162 LOG(ERROR) << "No room for Res_value after ResTable_entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700163 << " for type " << (int)type->id << ".";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000164 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700165 }
166
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000167 auto value = entry.offset(entry_size).convert<Res_value>();
168 if (UNLIKELY(!value)) {
169 return base::unexpected(IOError::PAGES_MISSING);
170 }
171
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700172 const size_t value_size = dtohs(value->size);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000173 if (UNLIKELY(value_size < sizeof(Res_value))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800174 LOG(ERROR) << "Res_value at offset " << entry_offset << " is too small.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000175 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700176 }
177
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000178 if (UNLIKELY(value_size > chunk_size || entry_offset + entry_size > chunk_size - value_size)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800179 LOG(ERROR) << "Res_value size " << value_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700180 << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000181 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700182 }
183 } else {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000184 auto map = entry.convert<ResTable_map_entry>();
185 if (UNLIKELY(!map)) {
186 return base::unexpected(IOError::PAGES_MISSING);
187 }
188
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700189 const size_t map_entry_count = dtohl(map->count);
190 size_t map_entries_start = entry_offset + entry_size;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000191 if (UNLIKELY(map_entries_start & 0x03U)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800192 LOG(ERROR) << "Map entries at offset " << entry_offset << " start at unaligned offset.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000193 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700194 }
195
196 // Each entry is sizeof(ResTable_map) big.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000197 if (UNLIKELY(map_entry_count > ((chunk_size - map_entries_start) / sizeof(ResTable_map)))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800198 LOG(ERROR) << "Too many map entries in ResTable_map_entry at offset " << entry_offset << ".";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000199 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700200 }
201 }
Eric Miao368cd192022-09-09 15:46:14 -0700202 return entry.verified();
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700203}
204
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100205LoadedPackage::iterator::iterator(const LoadedPackage* lp, size_t ti, size_t ei)
206 : loadedPackage_(lp),
207 typeIndex_(ti),
208 entryIndex_(ei),
209 typeIndexEnd_(lp->resource_ids_.size() + 1) {
210 while (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] == 0) {
211 typeIndex_++;
212 }
213}
214
215LoadedPackage::iterator& LoadedPackage::iterator::operator++() {
216 while (typeIndex_ < typeIndexEnd_) {
217 if (entryIndex_ + 1 < loadedPackage_->resource_ids_[typeIndex_]) {
218 entryIndex_++;
219 break;
220 }
221 entryIndex_ = 0;
222 typeIndex_++;
223 if (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] != 0) {
224 break;
225 }
226 }
227 return *this;
228}
229
230uint32_t LoadedPackage::iterator::operator*() const {
231 if (typeIndex_ >= typeIndexEnd_) {
232 return 0;
233 }
234 return make_resid(loadedPackage_->package_id_, typeIndex_ + loadedPackage_->type_id_offset_,
235 entryIndex_);
236}
237
Eric Miao368cd192022-09-09 15:46:14 -0700238base::expected<incfs::verified_map_ptr<ResTable_entry>, NullOrIOError> LoadedPackage::GetEntry(
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000239 incfs::verified_map_ptr<ResTable_type> type_chunk, uint16_t entry_index) {
240 base::expected<uint32_t, NullOrIOError> entry_offset = GetEntryOffset(type_chunk, entry_index);
241 if (UNLIKELY(!entry_offset.has_value())) {
242 return base::unexpected(entry_offset.error());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800243 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000244 return GetEntryFromOffset(type_chunk, entry_offset.value());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800245}
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700246
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000247base::expected<uint32_t, NullOrIOError> LoadedPackage::GetEntryOffset(
248 incfs::verified_map_ptr<ResTable_type> type_chunk, uint16_t entry_index) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800249 // The configuration matches and is better than the previous selection.
250 // Find the entry value if it exists for this configuration.
251 const size_t entry_count = dtohl(type_chunk->entryCount);
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700252 const auto offsets = type_chunk.offset(dtohs(type_chunk->header.headerSize));
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700253
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800254 // Check if there is the desired entry in this type.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800255 if (type_chunk->flags & ResTable_type::FLAG_SPARSE) {
256 // This is encoded as a sparse map, so perform a binary search.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000257 bool error = false;
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700258 auto sparse_indices = offsets.convert<ResTable_sparseTypeEntry>().iterator();
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000259 auto sparse_indices_end = sparse_indices + entry_count;
260 auto result = std::lower_bound(sparse_indices, sparse_indices_end, entry_index,
261 [&error](const incfs::map_ptr<ResTable_sparseTypeEntry>& entry,
262 uint16_t entry_idx) {
263 if (UNLIKELY(!entry)) {
264 return error = true;
265 }
266 return dtohs(entry->idx) < entry_idx;
267 });
Adam Lesinski73f6f9d2017-11-14 10:18:05 -0800268
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000269 if (result == sparse_indices_end) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800270 // No entry found.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000271 return base::unexpected(std::nullopt);
272 }
273
274 const incfs::verified_map_ptr<ResTable_sparseTypeEntry> entry = (*result).verified();
275 if (dtohs(entry->idx) != entry_index) {
276 if (error) {
277 return base::unexpected(IOError::PAGES_MISSING);
278 }
279 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700280 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800281
282 // Extract the offset from the entry. Each offset must be a multiple of 4 so we store it as
283 // the real offset divided by 4.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000284 return uint32_t{dtohs(entry->offset)} * 4u;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700285 }
286
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800287 // This type is encoded as a dense array.
288 if (entry_index >= entry_count) {
289 // This entry cannot be here.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000290 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700291 }
292
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700293 uint32_t result;
294
295 if (type_chunk->flags & ResTable_type::FLAG_OFFSET16) {
296 const auto entry_offset_ptr = offsets.convert<uint16_t>() + entry_index;
297 if (UNLIKELY(!entry_offset_ptr)) {
298 return base::unexpected(IOError::PAGES_MISSING);
299 }
300 result = offset_from16(entry_offset_ptr.value());
301 } else {
302 const auto entry_offset_ptr = offsets.convert<uint32_t>() + entry_index;
303 if (UNLIKELY(!entry_offset_ptr)) {
304 return base::unexpected(IOError::PAGES_MISSING);
305 }
306 result = dtohl(entry_offset_ptr.value());
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700307 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000308
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700309 if (result == ResTable_type::NO_ENTRY) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000310 return base::unexpected(std::nullopt);
311 }
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700312 return result;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700313}
314
Eric Miao368cd192022-09-09 15:46:14 -0700315base::expected<incfs::verified_map_ptr<ResTable_entry>, NullOrIOError>
316LoadedPackage::GetEntryFromOffset(incfs::verified_map_ptr<ResTable_type> type_chunk,
317 uint32_t offset) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000318 auto valid = VerifyResTableEntry(type_chunk, offset);
319 if (UNLIKELY(!valid.has_value())) {
320 return base::unexpected(valid.error());
321 }
Eric Miao368cd192022-09-09 15:46:14 -0700322 return valid;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000323}
324
325base::expected<std::monostate, IOError> LoadedPackage::CollectConfigurations(
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800326 bool exclude_mipmap, std::set<ResTable_config>* out_configs) const {\
327 for (const auto& type_spec : type_specs_) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000328 if (exclude_mipmap) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800329 const int type_idx = type_spec.first - 1;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000330 const auto type_name16 = type_string_pool_.stringAt(type_idx);
331 if (UNLIKELY(IsIOError(type_name16))) {
332 return base::unexpected(GetIOError(type_name16.error()));
333 }
334 if (type_name16.has_value()) {
335 if (strncmp16(type_name16->data(), u"mipmap", type_name16->size()) == 0) {
336 // This is a mipmap type, skip collection.
337 continue;
Adam Lesinski0c405242017-01-13 20:47:26 -0800338 }
339 }
340
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000341 const auto type_name = type_string_pool_.string8At(type_idx);
342 if (UNLIKELY(IsIOError(type_name))) {
343 return base::unexpected(GetIOError(type_name.error()));
344 }
345 if (type_name.has_value()) {
346 if (strncmp(type_name->data(), "mipmap", type_name->size()) == 0) {
347 // This is a mipmap type, skip collection.
348 continue;
349 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700350 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700351 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000352
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800353 for (const auto& type_entry : type_spec.second.type_entries) {
354 out_configs->insert(type_entry.config);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000355 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800356 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000357 return {};
Adam Lesinski0c405242017-01-13 20:47:26 -0800358}
359
360void LoadedPackage::CollectLocales(bool canonicalize, std::set<std::string>* out_locales) const {
361 char temp_locale[RESTABLE_MAX_LOCALE_LEN];
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800362 for (const auto& type_spec : type_specs_) {
363 for (const auto& type_entry : type_spec.second.type_entries) {
364 if (type_entry.config.locale != 0) {
365 type_entry.config.getBcp47Locale(temp_locale, canonicalize);
366 std::string locale(temp_locale);
367 out_locales->insert(std::move(locale));
Adam Lesinski0c405242017-01-13 20:47:26 -0800368 }
369 }
370 }
371}
372
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000373base::expected<uint32_t, NullOrIOError> LoadedPackage::FindEntryByName(
374 const std::u16string& type_name, const std::u16string& entry_name) const {
375 const base::expected<size_t, NullOrIOError> type_idx = type_string_pool_.indexOfString(
376 type_name.data(), type_name.size());
377 if (!type_idx.has_value()) {
378 return base::unexpected(type_idx.error());
Adam Lesinski929d6512017-01-16 19:11:19 -0800379 }
380
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000381 const base::expected<size_t, NullOrIOError> key_idx = key_string_pool_.indexOfString(
382 entry_name.data(), entry_name.size());
383 if (!key_idx.has_value()) {
384 return base::unexpected(key_idx.error());
Adam Lesinski929d6512017-01-16 19:11:19 -0800385 }
386
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800387 const TypeSpec* type_spec = GetTypeSpecByTypeIndex(*type_idx);
Adam Lesinski929d6512017-01-16 19:11:19 -0800388 if (type_spec == nullptr) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000389 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -0800390 }
391
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800392 for (const auto& type_entry : type_spec->type_entries) {
393 const incfs::verified_map_ptr<ResTable_type>& type = type_entry.type;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000394
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700395 const size_t entry_count = dtohl(type->entryCount);
396 const auto entry_offsets = type.offset(dtohs(type->header.headerSize));
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000397
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700398 for (size_t entry_idx = 0; entry_idx < entry_count; entry_idx++) {
Jason O'Brien984e8972021-08-26 19:07:47 -0500399 uint32_t offset;
400 uint16_t res_idx;
401 if (type->flags & ResTable_type::FLAG_SPARSE) {
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700402 auto sparse_entry = entry_offsets.convert<ResTable_sparseTypeEntry>() + entry_idx;
403 if (!sparse_entry) {
404 return base::unexpected(IOError::PAGES_MISSING);
405 }
Jason O'Brien984e8972021-08-26 19:07:47 -0500406 offset = dtohs(sparse_entry->offset) * 4u;
407 res_idx = dtohs(sparse_entry->idx);
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700408 } else if (type->flags & ResTable_type::FLAG_OFFSET16) {
409 auto entry = entry_offsets.convert<uint16_t>() + entry_idx;
410 if (!entry) {
411 return base::unexpected(IOError::PAGES_MISSING);
412 }
413 offset = offset_from16(entry.value());
414 res_idx = entry_idx;
Jason O'Brien984e8972021-08-26 19:07:47 -0500415 } else {
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700416 auto entry = entry_offsets.convert<uint32_t>() + entry_idx;
417 if (!entry) {
418 return base::unexpected(IOError::PAGES_MISSING);
419 }
420 offset = dtohl(entry.value());
Jason O'Brien984e8972021-08-26 19:07:47 -0500421 res_idx = entry_idx;
422 }
Eric Miaoa1f2bce2022-09-12 11:37:37 -0700423
Adam Lesinski929d6512017-01-16 19:11:19 -0800424 if (offset != ResTable_type::NO_ENTRY) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000425 auto entry = type.offset(dtohl(type->entriesStart) + offset).convert<ResTable_entry>();
426 if (!entry) {
427 return base::unexpected(IOError::PAGES_MISSING);
428 }
429
Eric Miao368cd192022-09-09 15:46:14 -0700430 if (entry->key() == static_cast<uint32_t>(*key_idx)) {
Adam Lesinski929d6512017-01-16 19:11:19 -0800431 // The package ID will be overridden by the caller (due to runtime assignment of package
432 // IDs for shared libraries).
Jason O'Brien984e8972021-08-26 19:07:47 -0500433 return make_resid(0x00, *type_idx + type_id_offset_ + 1, res_idx);
Adam Lesinski929d6512017-01-16 19:11:19 -0800434 }
435 }
436 }
437 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000438 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -0800439}
440
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800441const LoadedPackage* LoadedArsc::GetPackageById(uint8_t package_id) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700442 for (const auto& loaded_package : packages_) {
443 if (loaded_package->GetPackageId() == package_id) {
444 return loaded_package.get();
445 }
446 }
447 return nullptr;
448}
449
450std::unique_ptr<const LoadedPackage> LoadedPackage::Load(const Chunk& chunk,
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800451 package_property_t property_flags) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800452 ATRACE_NAME("LoadedPackage::Load");
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700453 std::unique_ptr<LoadedPackage> loaded_package(new LoadedPackage());
Adam Lesinskida431a22016-12-29 16:08:16 -0500454
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700455 // typeIdOffset was added at some point, but we still must recognize apps built before this
456 // was added.
Adam Lesinski33af6c72017-03-29 13:00:35 -0700457 constexpr size_t kMinPackageSize =
458 sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000459 const incfs::map_ptr<ResTable_package> header = chunk.header<ResTable_package, kMinPackageSize>();
460 if (!header) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800461 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500462 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700463 }
464
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800465 if ((property_flags & PROPERTY_SYSTEM) != 0) {
466 loaded_package->property_flags_ |= PROPERTY_SYSTEM;
467 }
468
469 if ((property_flags & PROPERTY_LOADER) != 0) {
470 loaded_package->property_flags_ |= PROPERTY_LOADER;
471 }
472
473 if ((property_flags & PROPERTY_OVERLAY) != 0) {
474 // Overlay resources must have an exclusive resource id space for referencing internal
475 // resources.
476 loaded_package->property_flags_ |= PROPERTY_OVERLAY | PROPERTY_DYNAMIC;
477 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700478
Adam Lesinski7ad11102016-10-28 16:39:15 -0700479 loaded_package->package_id_ = dtohl(header->id);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700480 if (loaded_package->package_id_ == 0 ||
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800481 (loaded_package->package_id_ == kAppPackageId && (property_flags & PROPERTY_DYNAMIC) != 0)) {
482 loaded_package->property_flags_ |= PROPERTY_DYNAMIC;
Winson9947f1e2019-08-16 10:20:39 -0700483 }
484
Adam Lesinskic6aada92017-01-13 15:34:14 -0800485 if (header->header.headerSize >= sizeof(ResTable_package)) {
486 uint32_t type_id_offset = dtohl(header->typeIdOffset);
487 if (type_id_offset > std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800488 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE type ID offset too large.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800489 return {};
490 }
491 loaded_package->type_id_offset_ = static_cast<int>(type_id_offset);
492 }
493
Adam Lesinskida431a22016-12-29 16:08:16 -0500494 util::ReadUtf16StringFromDevice(header->name, arraysize(header->name),
495 &loaded_package->package_name_);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700496
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800497 // A map of TypeSpec builders, each associated with an type index.
498 // We use these to accumulate the set of Types available for a TypeSpec, and later build a single,
499 // contiguous block of memory that holds all the Types together with the TypeSpec.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800500 std::unordered_map<int, std::unique_ptr<TypeSpecBuilder>> type_builder_map;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700501
502 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
503 while (iter.HasNext()) {
504 const Chunk child_chunk = iter.Next();
505 switch (child_chunk.type()) {
506 case RES_STRING_POOL_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000507 const auto pool_address = child_chunk.header<ResChunk_header>();
508 if (!pool_address) {
509 LOG(ERROR) << "RES_STRING_POOL_TYPE is incomplete due to incremental installation.";
510 return {};
511 }
512
513 if (pool_address == header.offset(dtohl(header->typeStrings)).convert<ResChunk_header>()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700514 // This string pool is the type string pool.
515 status_t err = loaded_package->type_string_pool_.setTo(
516 child_chunk.header<ResStringPool_header>(), child_chunk.size());
517 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800518 LOG(ERROR) << "RES_STRING_POOL_TYPE for types corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500519 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700520 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000521 } else if (pool_address == header.offset(dtohl(header->keyStrings))
522 .convert<ResChunk_header>()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700523 // This string pool is the key string pool.
524 status_t err = loaded_package->key_string_pool_.setTo(
525 child_chunk.header<ResStringPool_header>(), child_chunk.size());
526 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800527 LOG(ERROR) << "RES_STRING_POOL_TYPE for keys corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500528 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700529 }
530 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800531 LOG(WARNING) << "Too many RES_STRING_POOL_TYPEs found in RES_TABLE_PACKAGE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700532 }
533 } break;
534
535 case RES_TABLE_TYPE_SPEC_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000536 const auto type_spec = child_chunk.header<ResTable_typeSpec>();
537 if (!type_spec) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800538 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500539 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700540 }
541
542 if (type_spec->id == 0) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800543 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500544 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700545 }
546
Adam Lesinskic6aada92017-01-13 15:34:14 -0800547 if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) >
548 std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800549 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has out of range ID.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800550 return {};
551 }
552
Adam Lesinski7ad11102016-10-28 16:39:15 -0700553 // The data portion of this chunk contains entry_count 32bit entries,
554 // each one representing a set of flags.
555 // Here we only validate that the chunk is well formed.
556 const size_t entry_count = dtohl(type_spec->entryCount);
557
558 // There can only be 2^16 entries in a type, because that is the ID
559 // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
560 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800561 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has too many entries (" << entry_count << ").";
Adam Lesinskida431a22016-12-29 16:08:16 -0500562 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700563 }
564
565 if (entry_count * sizeof(uint32_t) > chunk.data_size()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800566 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500567 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700568 }
569
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800570 std::unique_ptr<TypeSpecBuilder>& builder_ptr = type_builder_map[type_spec->id];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800571 if (builder_ptr == nullptr) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800572 builder_ptr = util::make_unique<TypeSpecBuilder>(type_spec.verified());
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100573 loaded_package->resource_ids_.set(type_spec->id, entry_count);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800574 } else {
575 LOG(WARNING) << StringPrintf("RES_TABLE_TYPE_SPEC_TYPE already defined for ID %02x",
576 type_spec->id);
577 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700578 } break;
579
580 case RES_TABLE_TYPE_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000581 const auto type = child_chunk.header<ResTable_type, kResTableTypeMinSize>();
582 if (!type) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800583 LOG(ERROR) << "RES_TABLE_TYPE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500584 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700585 }
586
Adam Lesinski498f6052017-11-29 13:24:29 -0800587 if (!VerifyResTableType(type)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500588 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700589 }
590
591 // Type chunks must be preceded by their TypeSpec chunks.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800592 std::unique_ptr<TypeSpecBuilder>& builder_ptr = type_builder_map[type->id];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800593 if (builder_ptr != nullptr) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000594 builder_ptr->AddType(type.verified());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800595 } else {
596 LOG(ERROR) << StringPrintf(
597 "RES_TABLE_TYPE_TYPE with ID %02x found without preceding RES_TABLE_TYPE_SPEC_TYPE.",
598 type->id);
Adam Lesinskida431a22016-12-29 16:08:16 -0500599 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700600 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700601 } break;
602
Adam Lesinskida431a22016-12-29 16:08:16 -0500603 case RES_TABLE_LIBRARY_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000604 const auto lib = child_chunk.header<ResTable_lib_header>();
605 if (!lib) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800606 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500607 return {};
608 }
609
610 if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800611 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500612 return {};
613 }
614
615 loaded_package->dynamic_package_map_.reserve(dtohl(lib->count));
616
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000617 const auto entry_begin = child_chunk.data_ptr().convert<ResTable_lib_entry>();
618 const auto entry_end = entry_begin + dtohl(lib->count);
Adam Lesinskida431a22016-12-29 16:08:16 -0500619 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000620 if (!entry_iter) {
621 return {};
622 }
623
Adam Lesinskida431a22016-12-29 16:08:16 -0500624 std::string package_name;
625 util::ReadUtf16StringFromDevice(entry_iter->packageName,
626 arraysize(entry_iter->packageName), &package_name);
627
628 if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800629 LOG(ERROR) << StringPrintf(
Adam Lesinskida431a22016-12-29 16:08:16 -0500630 "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.",
631 dtohl(entry_iter->packageId), package_name.c_str());
632 return {};
633 }
634
635 loaded_package->dynamic_package_map_.emplace_back(std::move(package_name),
636 dtohl(entry_iter->packageId));
637 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800638 } break;
Adam Lesinskida431a22016-12-29 16:08:16 -0500639
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800640 case RES_TABLE_OVERLAYABLE_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000641 const auto overlayable = child_chunk.header<ResTable_overlayable_header>();
642 if (!overlayable) {
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800643 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_TYPE too small.";
644 return {};
645 }
646
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800647 std::string name;
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800648 util::ReadUtf16StringFromDevice(overlayable->name, std::size(overlayable->name), &name);
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800649 std::string actor;
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800650 util::ReadUtf16StringFromDevice(overlayable->actor, std::size(overlayable->actor), &actor);
Yurii Zubrytskyi9d225372022-11-29 11:12:18 -0800651 auto [name_to_actor_it, inserted] =
652 loaded_package->overlayable_map_.emplace(std::move(name), std::move(actor));
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800653 if (!inserted) {
Yurii Zubrytskyi9d225372022-11-29 11:12:18 -0800654 LOG(ERROR) << "Multiple <overlayable> blocks with the same name '"
655 << name_to_actor_it->first << "'.";
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100656 return {};
657 }
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100658
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800659 // Iterate over the overlayable policy chunks contained within the overlayable chunk data
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800660 ChunkIterator overlayable_iter(child_chunk.data_ptr(), child_chunk.data_size());
661 while (overlayable_iter.HasNext()) {
662 const Chunk overlayable_child_chunk = overlayable_iter.Next();
663
664 switch (overlayable_child_chunk.type()) {
665 case RES_TABLE_OVERLAYABLE_POLICY_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000666 const auto policy_header =
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800667 overlayable_child_chunk.header<ResTable_overlayable_policy_header>();
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000668 if (!policy_header) {
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800669 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small.";
670 return {};
671 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800672 if ((overlayable_child_chunk.data_size() / sizeof(ResTable_ref))
673 < dtohl(policy_header->entry_count)) {
674 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small to hold entries.";
675 return {};
676 }
677
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800678 // Retrieve all the resource ids belonging to this policy chunk
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000679 const auto ids_begin = overlayable_child_chunk.data_ptr().convert<ResTable_ref>();
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800680 const auto ids_end = ids_begin + dtohl(policy_header->entry_count);
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800681 std::unordered_set<uint32_t> ids;
682 ids.reserve(ids_end - ids_begin);
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800683 for (auto id_iter = ids_begin; id_iter != ids_end; ++id_iter) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000684 if (!id_iter) {
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800685 LOG(ERROR) << "NULL ResTable_ref record??";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000686 return {};
687 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800688 ids.insert(dtohl(id_iter->ident));
689 }
690
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800691 // Add the pairing of overlayable properties and resource ids to the package
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800692 OverlayableInfo overlayable_info {
Yurii Zubrytskyi9d225372022-11-29 11:12:18 -0800693 .name = name_to_actor_it->first,
694 .actor = name_to_actor_it->second,
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800695 .policy_flags = policy_header->policy_flags
696 };
697 loaded_package->overlayable_infos_.emplace_back(std::move(overlayable_info), std::move(ids));
Ryan Mitchell19823452019-01-29 12:01:24 -0800698 loaded_package->defines_overlayable_ = true;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800699 break;
700 }
701
702 default:
703 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
704 break;
705 }
706 }
707
708 if (overlayable_iter.HadError()) {
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800709 LOG(ERROR) << StringPrintf("Error parsing RES_TABLE_OVERLAYABLE_TYPE: %s",
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800710 overlayable_iter.GetLastError().c_str());
711 if (overlayable_iter.HadFatalError()) {
712 return {};
713 }
714 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500715 } break;
716
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700717 case RES_TABLE_STAGED_ALIAS_TYPE: {
718 if (loaded_package->package_id_ != kFrameworkPackageId) {
719 LOG(WARNING) << "Alias chunk ignored for non-framework package '"
720 << loaded_package->package_name_ << "'";
721 break;
722 }
723
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700724 const auto lib_alias = child_chunk.header<ResTable_staged_alias_header>();
725 if (!lib_alias) {
Yurii Zubrytskyi80020342021-11-29 23:55:24 -0800726 LOG(ERROR) << "RES_TABLE_STAGED_ALIAS_TYPE is too small.";
727 return {};
728 }
729 if ((child_chunk.data_size() / sizeof(ResTable_staged_alias_entry))
730 < dtohl(lib_alias->count)) {
731 LOG(ERROR) << "RES_TABLE_STAGED_ALIAS_TYPE is too small to hold entries.";
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700732 return {};
733 }
734 const auto entry_begin = child_chunk.data_ptr().convert<ResTable_staged_alias_entry>();
735 const auto entry_end = entry_begin + dtohl(lib_alias->count);
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800736 std::unordered_set<uint32_t> finalized_ids;
737 finalized_ids.reserve(entry_end - entry_begin);
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800738 loaded_package->alias_id_map_.reserve(entry_end - entry_begin);
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700739 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
740 if (!entry_iter) {
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800741 LOG(ERROR) << "NULL ResTable_staged_alias_entry record??";
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700742 return {};
743 }
744 auto finalized_id = dtohl(entry_iter->finalizedResId);
745 if (!finalized_ids.insert(finalized_id).second) {
746 LOG(ERROR) << StringPrintf("Repeated finalized resource id '%08x' in staged aliases.",
747 finalized_id);
748 return {};
749 }
750
751 auto staged_id = dtohl(entry_iter->stagedResId);
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800752 loaded_package->alias_id_map_.emplace_back(staged_id, finalized_id);
753 }
754
755 std::sort(loaded_package->alias_id_map_.begin(), loaded_package->alias_id_map_.end(),
756 [](auto&& l, auto&& r) { return l.first < r.first; });
757 const auto duplicate_it =
758 std::adjacent_find(loaded_package->alias_id_map_.begin(),
759 loaded_package->alias_id_map_.end(),
760 [](auto&& l, auto&& r) { return l.first == r.first; });
761 if (duplicate_it != loaded_package->alias_id_map_.end()) {
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700762 LOG(ERROR) << StringPrintf("Repeated staged resource id '%08x' in staged aliases.",
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800763 duplicate_it->first);
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700764 return {};
765 }
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700766 } break;
767
Adam Lesinski7ad11102016-10-28 16:39:15 -0700768 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800769 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700770 break;
771 }
772 }
773
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800774 if (iter.HadError()) {
775 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700776 if (iter.HadFatalError()) {
777 return {};
778 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800779 }
780
781 // Flatten and construct the TypeSpecs.
782 for (auto& entry : type_builder_map) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800783 TypeSpec type_spec = entry.second->Build();
784 uint8_t type_id = static_cast<uint8_t>(entry.first);
785 loaded_package->type_specs_[type_id] = std::move(type_spec);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700786 }
787
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700788 return std::move(loaded_package);
789}
790
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700791bool LoadedArsc::LoadTable(const Chunk& chunk, const LoadedIdmap* loaded_idmap,
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800792 package_property_t property_flags) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000793 incfs::map_ptr<ResTable_header> header = chunk.header<ResTable_header>();
794 if (!header) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800795 LOG(ERROR) << "RES_TABLE_TYPE too small.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700796 return false;
797 }
798
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700799 if (loaded_idmap != nullptr) {
800 global_string_pool_ = util::make_unique<OverlayStringPool>(loaded_idmap);
801 }
802
Adam Lesinski7ad11102016-10-28 16:39:15 -0700803 const size_t package_count = dtohl(header->packageCount);
804 size_t packages_seen = 0;
805
806 packages_.reserve(package_count);
807
808 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
809 while (iter.HasNext()) {
810 const Chunk child_chunk = iter.Next();
811 switch (child_chunk.type()) {
812 case RES_STRING_POOL_TYPE:
813 // Only use the first string pool. Ignore others.
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700814 if (global_string_pool_->getError() == NO_INIT) {
815 status_t err = global_string_pool_->setTo(child_chunk.header<ResStringPool_header>(),
816 child_chunk.size());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700817 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800818 LOG(ERROR) << "RES_STRING_POOL_TYPE corrupt.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700819 return false;
820 }
821 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800822 LOG(WARNING) << "Multiple RES_STRING_POOL_TYPEs found in RES_TABLE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700823 }
824 break;
825
826 case RES_TABLE_PACKAGE_TYPE: {
827 if (packages_seen + 1 > package_count) {
828 LOG(ERROR) << "More package chunks were found than the " << package_count
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700829 << " declared in the header.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700830 return false;
831 }
832 packages_seen++;
833
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700834 std::unique_ptr<const LoadedPackage> loaded_package =
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800835 LoadedPackage::Load(child_chunk, property_flags);
Adam Lesinskida431a22016-12-29 16:08:16 -0500836 if (!loaded_package) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700837 return false;
838 }
839 packages_.push_back(std::move(loaded_package));
840 } break;
841
842 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800843 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700844 break;
845 }
846 }
847
848 if (iter.HadError()) {
849 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700850 if (iter.HadFatalError()) {
851 return false;
852 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700853 }
854 return true;
855}
856
Jeremy Meyer65871022022-07-12 22:45:08 +0000857bool LoadedArsc::LoadStringPool(const LoadedIdmap* loaded_idmap) {
858 if (loaded_idmap != nullptr) {
859 global_string_pool_ = util::make_unique<OverlayStringPool>(loaded_idmap);
860 }
861 return true;
862}
863
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800864std::unique_ptr<LoadedArsc> LoadedArsc::Load(incfs::map_ptr<void> data,
865 const size_t length,
866 const LoadedIdmap* loaded_idmap,
867 const package_property_t property_flags) {
Winson9947f1e2019-08-16 10:20:39 -0700868 ATRACE_NAME("LoadedArsc::Load");
Adam Lesinski7ad11102016-10-28 16:39:15 -0700869
870 // Not using make_unique because the constructor is private.
871 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
872
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000873 ChunkIterator iter(data, length);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700874 while (iter.HasNext()) {
875 const Chunk chunk = iter.Next();
876 switch (chunk.type()) {
877 case RES_TABLE_TYPE:
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800878 if (!loaded_arsc->LoadTable(chunk, loaded_idmap, property_flags)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700879 return {};
880 }
881 break;
882
883 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800884 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700885 break;
886 }
887 }
888
889 if (iter.HadError()) {
890 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700891 if (iter.HadFatalError()) {
892 return {};
893 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700894 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800895
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800896 return loaded_arsc;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700897}
898
Jeremy Meyer65871022022-07-12 22:45:08 +0000899std::unique_ptr<LoadedArsc> LoadedArsc::Load(const LoadedIdmap* loaded_idmap) {
900 ATRACE_NAME("LoadedArsc::Load");
901
902 // Not using make_unique because the constructor is private.
903 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
904 loaded_arsc->LoadStringPool(loaded_idmap);
905 return loaded_arsc;
906}
907
908
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800909std::unique_ptr<LoadedArsc> LoadedArsc::CreateEmpty() {
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700910 return std::unique_ptr<LoadedArsc>(new LoadedArsc());
911}
912
Adam Lesinski7ad11102016-10-28 16:39:15 -0700913} // namespace android