blob: c9d5e074271b10c737191034d440259a881fddc3 [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(
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700326 bool exclude_mipmap, std::set<ResTable_config>* out_configs) const {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800327 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
Yurii Zubrytskyi75663392023-05-02 10:53:14 -0700497 const bool only_overlayable = (property_flags & PROPERTY_ONLY_OVERLAYABLES) != 0;
498
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800499 // A map of TypeSpec builders, each associated with an type index.
500 // We use these to accumulate the set of Types available for a TypeSpec, and later build a single,
501 // contiguous block of memory that holds all the Types together with the TypeSpec.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800502 std::unordered_map<int, std::unique_ptr<TypeSpecBuilder>> type_builder_map;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700503
504 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
505 while (iter.HasNext()) {
506 const Chunk child_chunk = iter.Next();
Yurii Zubrytskyi75663392023-05-02 10:53:14 -0700507 if (only_overlayable && child_chunk.type() != RES_TABLE_OVERLAYABLE_TYPE) {
508 continue;
509 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700510 switch (child_chunk.type()) {
511 case RES_STRING_POOL_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000512 const auto pool_address = child_chunk.header<ResChunk_header>();
513 if (!pool_address) {
514 LOG(ERROR) << "RES_STRING_POOL_TYPE is incomplete due to incremental installation.";
515 return {};
516 }
517
518 if (pool_address == header.offset(dtohl(header->typeStrings)).convert<ResChunk_header>()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700519 // This string pool is the type string pool.
520 status_t err = loaded_package->type_string_pool_.setTo(
521 child_chunk.header<ResStringPool_header>(), child_chunk.size());
522 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800523 LOG(ERROR) << "RES_STRING_POOL_TYPE for types corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500524 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700525 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000526 } else if (pool_address == header.offset(dtohl(header->keyStrings))
527 .convert<ResChunk_header>()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700528 // This string pool is the key string pool.
529 status_t err = loaded_package->key_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 keys corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500533 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700534 }
535 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800536 LOG(WARNING) << "Too many RES_STRING_POOL_TYPEs found in RES_TABLE_PACKAGE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700537 }
538 } break;
539
540 case RES_TABLE_TYPE_SPEC_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000541 const auto type_spec = child_chunk.header<ResTable_typeSpec>();
542 if (!type_spec) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800543 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500544 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700545 }
546
547 if (type_spec->id == 0) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800548 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500549 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700550 }
551
Adam Lesinskic6aada92017-01-13 15:34:14 -0800552 if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) >
553 std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800554 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has out of range ID.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800555 return {};
556 }
557
Adam Lesinski7ad11102016-10-28 16:39:15 -0700558 // The data portion of this chunk contains entry_count 32bit entries,
559 // each one representing a set of flags.
560 // Here we only validate that the chunk is well formed.
561 const size_t entry_count = dtohl(type_spec->entryCount);
562
563 // There can only be 2^16 entries in a type, because that is the ID
564 // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
565 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800566 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has too many entries (" << entry_count << ").";
Adam Lesinskida431a22016-12-29 16:08:16 -0500567 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700568 }
569
570 if (entry_count * sizeof(uint32_t) > chunk.data_size()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800571 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500572 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700573 }
574
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800575 std::unique_ptr<TypeSpecBuilder>& builder_ptr = type_builder_map[type_spec->id];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800576 if (builder_ptr == nullptr) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800577 builder_ptr = util::make_unique<TypeSpecBuilder>(type_spec.verified());
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100578 loaded_package->resource_ids_.set(type_spec->id, entry_count);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800579 } else {
580 LOG(WARNING) << StringPrintf("RES_TABLE_TYPE_SPEC_TYPE already defined for ID %02x",
581 type_spec->id);
582 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700583 } break;
584
585 case RES_TABLE_TYPE_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000586 const auto type = child_chunk.header<ResTable_type, kResTableTypeMinSize>();
587 if (!type) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800588 LOG(ERROR) << "RES_TABLE_TYPE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500589 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700590 }
591
Adam Lesinski498f6052017-11-29 13:24:29 -0800592 if (!VerifyResTableType(type)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500593 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700594 }
595
596 // Type chunks must be preceded by their TypeSpec chunks.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800597 std::unique_ptr<TypeSpecBuilder>& builder_ptr = type_builder_map[type->id];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800598 if (builder_ptr != nullptr) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000599 builder_ptr->AddType(type.verified());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800600 } else {
601 LOG(ERROR) << StringPrintf(
602 "RES_TABLE_TYPE_TYPE with ID %02x found without preceding RES_TABLE_TYPE_SPEC_TYPE.",
603 type->id);
Adam Lesinskida431a22016-12-29 16:08:16 -0500604 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700605 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700606 } break;
607
Adam Lesinskida431a22016-12-29 16:08:16 -0500608 case RES_TABLE_LIBRARY_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000609 const auto lib = child_chunk.header<ResTable_lib_header>();
610 if (!lib) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800611 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500612 return {};
613 }
614
615 if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800616 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500617 return {};
618 }
619
620 loaded_package->dynamic_package_map_.reserve(dtohl(lib->count));
621
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000622 const auto entry_begin = child_chunk.data_ptr().convert<ResTable_lib_entry>();
623 const auto entry_end = entry_begin + dtohl(lib->count);
Adam Lesinskida431a22016-12-29 16:08:16 -0500624 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000625 if (!entry_iter) {
626 return {};
627 }
628
Adam Lesinskida431a22016-12-29 16:08:16 -0500629 std::string package_name;
630 util::ReadUtf16StringFromDevice(entry_iter->packageName,
631 arraysize(entry_iter->packageName), &package_name);
632
633 if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800634 LOG(ERROR) << StringPrintf(
Adam Lesinskida431a22016-12-29 16:08:16 -0500635 "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.",
636 dtohl(entry_iter->packageId), package_name.c_str());
637 return {};
638 }
639
640 loaded_package->dynamic_package_map_.emplace_back(std::move(package_name),
641 dtohl(entry_iter->packageId));
642 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800643 } break;
Adam Lesinskida431a22016-12-29 16:08:16 -0500644
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800645 case RES_TABLE_OVERLAYABLE_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000646 const auto overlayable = child_chunk.header<ResTable_overlayable_header>();
647 if (!overlayable) {
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800648 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_TYPE too small.";
649 return {};
650 }
651
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800652 std::string name;
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800653 util::ReadUtf16StringFromDevice(overlayable->name, std::size(overlayable->name), &name);
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800654 std::string actor;
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800655 util::ReadUtf16StringFromDevice(overlayable->actor, std::size(overlayable->actor), &actor);
Yurii Zubrytskyi9d225372022-11-29 11:12:18 -0800656 auto [name_to_actor_it, inserted] =
657 loaded_package->overlayable_map_.emplace(std::move(name), std::move(actor));
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800658 if (!inserted) {
Yurii Zubrytskyi9d225372022-11-29 11:12:18 -0800659 LOG(ERROR) << "Multiple <overlayable> blocks with the same name '"
660 << name_to_actor_it->first << "'.";
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100661 return {};
662 }
Yurii Zubrytskyi75663392023-05-02 10:53:14 -0700663 if (only_overlayable) {
664 break;
665 }
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100666
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800667 // Iterate over the overlayable policy chunks contained within the overlayable chunk data
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800668 ChunkIterator overlayable_iter(child_chunk.data_ptr(), child_chunk.data_size());
669 while (overlayable_iter.HasNext()) {
670 const Chunk overlayable_child_chunk = overlayable_iter.Next();
671
672 switch (overlayable_child_chunk.type()) {
673 case RES_TABLE_OVERLAYABLE_POLICY_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000674 const auto policy_header =
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800675 overlayable_child_chunk.header<ResTable_overlayable_policy_header>();
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000676 if (!policy_header) {
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800677 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small.";
678 return {};
679 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800680 if ((overlayable_child_chunk.data_size() / sizeof(ResTable_ref))
681 < dtohl(policy_header->entry_count)) {
682 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small to hold entries.";
683 return {};
684 }
685
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800686 // Retrieve all the resource ids belonging to this policy chunk
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000687 const auto ids_begin = overlayable_child_chunk.data_ptr().convert<ResTable_ref>();
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800688 const auto ids_end = ids_begin + dtohl(policy_header->entry_count);
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800689 std::unordered_set<uint32_t> ids;
690 ids.reserve(ids_end - ids_begin);
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800691 for (auto id_iter = ids_begin; id_iter != ids_end; ++id_iter) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000692 if (!id_iter) {
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800693 LOG(ERROR) << "NULL ResTable_ref record??";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000694 return {};
695 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800696 ids.insert(dtohl(id_iter->ident));
697 }
698
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800699 // Add the pairing of overlayable properties and resource ids to the package
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800700 OverlayableInfo overlayable_info {
Yurii Zubrytskyi9d225372022-11-29 11:12:18 -0800701 .name = name_to_actor_it->first,
702 .actor = name_to_actor_it->second,
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800703 .policy_flags = policy_header->policy_flags
704 };
705 loaded_package->overlayable_infos_.emplace_back(std::move(overlayable_info), std::move(ids));
Ryan Mitchell19823452019-01-29 12:01:24 -0800706 loaded_package->defines_overlayable_ = true;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800707 break;
708 }
709
710 default:
711 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
712 break;
713 }
714 }
715
716 if (overlayable_iter.HadError()) {
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800717 LOG(ERROR) << StringPrintf("Error parsing RES_TABLE_OVERLAYABLE_TYPE: %s",
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800718 overlayable_iter.GetLastError().c_str());
719 if (overlayable_iter.HadFatalError()) {
720 return {};
721 }
722 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500723 } break;
724
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700725 case RES_TABLE_STAGED_ALIAS_TYPE: {
726 if (loaded_package->package_id_ != kFrameworkPackageId) {
727 LOG(WARNING) << "Alias chunk ignored for non-framework package '"
728 << loaded_package->package_name_ << "'";
729 break;
730 }
731
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700732 const auto lib_alias = child_chunk.header<ResTable_staged_alias_header>();
733 if (!lib_alias) {
Yurii Zubrytskyi80020342021-11-29 23:55:24 -0800734 LOG(ERROR) << "RES_TABLE_STAGED_ALIAS_TYPE is too small.";
735 return {};
736 }
737 if ((child_chunk.data_size() / sizeof(ResTable_staged_alias_entry))
738 < dtohl(lib_alias->count)) {
739 LOG(ERROR) << "RES_TABLE_STAGED_ALIAS_TYPE is too small to hold entries.";
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700740 return {};
741 }
742 const auto entry_begin = child_chunk.data_ptr().convert<ResTable_staged_alias_entry>();
743 const auto entry_end = entry_begin + dtohl(lib_alias->count);
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800744 std::unordered_set<uint32_t> finalized_ids;
745 finalized_ids.reserve(entry_end - entry_begin);
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800746 loaded_package->alias_id_map_.reserve(entry_end - entry_begin);
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700747 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
748 if (!entry_iter) {
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800749 LOG(ERROR) << "NULL ResTable_staged_alias_entry record??";
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700750 return {};
751 }
752 auto finalized_id = dtohl(entry_iter->finalizedResId);
753 if (!finalized_ids.insert(finalized_id).second) {
754 LOG(ERROR) << StringPrintf("Repeated finalized resource id '%08x' in staged aliases.",
755 finalized_id);
756 return {};
757 }
758
759 auto staged_id = dtohl(entry_iter->stagedResId);
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800760 loaded_package->alias_id_map_.emplace_back(staged_id, finalized_id);
761 }
762
763 std::sort(loaded_package->alias_id_map_.begin(), loaded_package->alias_id_map_.end(),
764 [](auto&& l, auto&& r) { return l.first < r.first; });
765 const auto duplicate_it =
766 std::adjacent_find(loaded_package->alias_id_map_.begin(),
767 loaded_package->alias_id_map_.end(),
768 [](auto&& l, auto&& r) { return l.first == r.first; });
769 if (duplicate_it != loaded_package->alias_id_map_.end()) {
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700770 LOG(ERROR) << StringPrintf("Repeated staged resource id '%08x' in staged aliases.",
Yurii Zubrytskyi6e8c6032022-11-14 23:45:58 -0800771 duplicate_it->first);
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700772 return {};
773 }
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700774 } break;
775
Adam Lesinski7ad11102016-10-28 16:39:15 -0700776 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800777 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700778 break;
779 }
780 }
781
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800782 if (iter.HadError()) {
783 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700784 if (iter.HadFatalError()) {
785 return {};
786 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800787 }
788
789 // Flatten and construct the TypeSpecs.
790 for (auto& entry : type_builder_map) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800791 TypeSpec type_spec = entry.second->Build();
792 uint8_t type_id = static_cast<uint8_t>(entry.first);
793 loaded_package->type_specs_[type_id] = std::move(type_spec);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700794 }
795
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700796 return std::move(loaded_package);
797}
798
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700799bool LoadedArsc::LoadTable(const Chunk& chunk, const LoadedIdmap* loaded_idmap,
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800800 package_property_t property_flags) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000801 incfs::map_ptr<ResTable_header> header = chunk.header<ResTable_header>();
802 if (!header) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800803 LOG(ERROR) << "RES_TABLE_TYPE too small.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700804 return false;
805 }
806
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700807 if (loaded_idmap != nullptr) {
808 global_string_pool_ = util::make_unique<OverlayStringPool>(loaded_idmap);
809 }
810
Yurii Zubrytskyi75663392023-05-02 10:53:14 -0700811 const bool only_overlayable = (property_flags & PROPERTY_ONLY_OVERLAYABLES) != 0;
812
Adam Lesinski7ad11102016-10-28 16:39:15 -0700813 const size_t package_count = dtohl(header->packageCount);
814 size_t packages_seen = 0;
815
Yurii Zubrytskyi75663392023-05-02 10:53:14 -0700816 if (!only_overlayable) {
817 packages_.reserve(package_count);
818 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700819
820 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
821 while (iter.HasNext()) {
822 const Chunk child_chunk = iter.Next();
Yurii Zubrytskyi75663392023-05-02 10:53:14 -0700823 if (only_overlayable && child_chunk.type() != RES_TABLE_PACKAGE_TYPE) {
824 continue;
825 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700826 switch (child_chunk.type()) {
827 case RES_STRING_POOL_TYPE:
828 // Only use the first string pool. Ignore others.
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700829 if (global_string_pool_->getError() == NO_INIT) {
830 status_t err = global_string_pool_->setTo(child_chunk.header<ResStringPool_header>(),
831 child_chunk.size());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700832 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800833 LOG(ERROR) << "RES_STRING_POOL_TYPE corrupt.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700834 return false;
835 }
836 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800837 LOG(WARNING) << "Multiple RES_STRING_POOL_TYPEs found in RES_TABLE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700838 }
839 break;
840
841 case RES_TABLE_PACKAGE_TYPE: {
842 if (packages_seen + 1 > package_count) {
843 LOG(ERROR) << "More package chunks were found than the " << package_count
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700844 << " declared in the header.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700845 return false;
846 }
847 packages_seen++;
848
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700849 std::unique_ptr<const LoadedPackage> loaded_package =
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800850 LoadedPackage::Load(child_chunk, property_flags);
Adam Lesinskida431a22016-12-29 16:08:16 -0500851 if (!loaded_package) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700852 return false;
853 }
854 packages_.push_back(std::move(loaded_package));
Yurii Zubrytskyi75663392023-05-02 10:53:14 -0700855 if (only_overlayable) {
856 // Overlayable is always in the first package, no need to process anything else.
857 return true;
858 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700859 } break;
860
861 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800862 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700863 break;
864 }
865 }
866
867 if (iter.HadError()) {
868 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700869 if (iter.HadFatalError()) {
870 return false;
871 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700872 }
873 return true;
874}
875
Jeremy Meyer65871022022-07-12 22:45:08 +0000876bool LoadedArsc::LoadStringPool(const LoadedIdmap* loaded_idmap) {
877 if (loaded_idmap != nullptr) {
878 global_string_pool_ = util::make_unique<OverlayStringPool>(loaded_idmap);
879 }
880 return true;
881}
882
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800883std::unique_ptr<LoadedArsc> LoadedArsc::Load(incfs::map_ptr<void> data,
884 const size_t length,
885 const LoadedIdmap* loaded_idmap,
886 const package_property_t property_flags) {
Winson9947f1e2019-08-16 10:20:39 -0700887 ATRACE_NAME("LoadedArsc::Load");
Adam Lesinski7ad11102016-10-28 16:39:15 -0700888
889 // Not using make_unique because the constructor is private.
890 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
891
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000892 ChunkIterator iter(data, length);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700893 while (iter.HasNext()) {
894 const Chunk chunk = iter.Next();
895 switch (chunk.type()) {
896 case RES_TABLE_TYPE:
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800897 if (!loaded_arsc->LoadTable(chunk, loaded_idmap, property_flags)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700898 return {};
899 }
900 break;
901
902 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800903 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700904 break;
905 }
906 }
907
908 if (iter.HadError()) {
909 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700910 if (iter.HadFatalError()) {
911 return {};
912 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700913 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800914
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800915 return loaded_arsc;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700916}
917
Jeremy Meyer65871022022-07-12 22:45:08 +0000918std::unique_ptr<LoadedArsc> LoadedArsc::Load(const LoadedIdmap* loaded_idmap) {
919 ATRACE_NAME("LoadedArsc::Load");
920
921 // Not using make_unique because the constructor is private.
922 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
923 loaded_arsc->LoadStringPool(loaded_idmap);
924 return loaded_arsc;
925}
926
927
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800928std::unique_ptr<LoadedArsc> LoadedArsc::CreateEmpty() {
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700929 return std::unique_ptr<LoadedArsc>(new LoadedArsc());
930}
931
Adam Lesinski7ad11102016-10-28 16:39:15 -0700932} // namespace android