blob: e78f91ee3f46377f3d6bea2830b2fd4a0a7435e9 [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);
91 const size_t offsets_length = sizeof(uint32_t) * entry_count;
92
93 if (offsets_offset > entries_offset || entries_offset - offsets_offset < offsets_length) {
Adam Lesinski498f6052017-11-29 13:24:29 -080094 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070095 return false;
96 }
97
98 if (entries_offset > dtohl(header->header.size)) {
Adam Lesinski498f6052017-11-29 13:24:29 -080099 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets extend beyond chunk.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700100 return false;
101 }
102
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000103 if (entries_offset & 0x03U) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800104 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entries start at unaligned address.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700105 return false;
106 }
107 return true;
108}
109
Eric Miao368cd192022-09-09 15:46:14 -0700110static base::expected<incfs::verified_map_ptr<ResTable_entry>, NullOrIOError>
111VerifyResTableEntry(incfs::verified_map_ptr<ResTable_type> type, uint32_t entry_offset) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700112 // Check that the offset is aligned.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000113 if (UNLIKELY(entry_offset & 0x03U)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800114 LOG(ERROR) << "Entry at offset " << entry_offset << " is not 4-byte aligned.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000115 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700116 }
117
118 // Check that the offset doesn't overflow.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000119 if (UNLIKELY(entry_offset > std::numeric_limits<uint32_t>::max() - dtohl(type->entriesStart))) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700120 // Overflow in offset.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800121 LOG(ERROR) << "Entry at offset " << entry_offset << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000122 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700123 }
124
125 const size_t chunk_size = dtohl(type->header.size);
126
127 entry_offset += dtohl(type->entriesStart);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000128 if (UNLIKELY(entry_offset > chunk_size - sizeof(ResTable_entry))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800129 LOG(ERROR) << "Entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700130 << " is too large. No room for ResTable_entry.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000131 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700132 }
133
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000134 auto entry = type.offset(entry_offset).convert<ResTable_entry>();
135 if (UNLIKELY(!entry)) {
136 return base::unexpected(IOError::PAGES_MISSING);
137 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700138
Eric Miao368cd192022-09-09 15:46:14 -0700139 const size_t entry_size = entry->size();
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000140 if (UNLIKELY(entry_size < sizeof(entry.value()))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800141 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700142 << " is too small.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000143 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700144 }
145
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000146 if (UNLIKELY(entry_size > chunk_size || entry_offset > chunk_size - entry_size)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800147 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700148 << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000149 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700150 }
151
Eric Miao368cd192022-09-09 15:46:14 -0700152 // If entry is compact, value is already encoded, and a compact entry
153 // cannot be a map_entry, we are done verifying
154 if (entry->is_compact())
155 return entry.verified();
156
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700157 if (entry_size < sizeof(ResTable_map_entry)) {
158 // There needs to be room for one Res_value struct.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000159 if (UNLIKELY(entry_offset + entry_size > chunk_size - sizeof(Res_value))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800160 LOG(ERROR) << "No room for Res_value after ResTable_entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700161 << " for type " << (int)type->id << ".";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000162 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700163 }
164
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000165 auto value = entry.offset(entry_size).convert<Res_value>();
166 if (UNLIKELY(!value)) {
167 return base::unexpected(IOError::PAGES_MISSING);
168 }
169
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700170 const size_t value_size = dtohs(value->size);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000171 if (UNLIKELY(value_size < sizeof(Res_value))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800172 LOG(ERROR) << "Res_value at offset " << entry_offset << " is too small.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000173 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700174 }
175
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000176 if (UNLIKELY(value_size > chunk_size || entry_offset + entry_size > chunk_size - value_size)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800177 LOG(ERROR) << "Res_value size " << value_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700178 << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000179 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700180 }
181 } else {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000182 auto map = entry.convert<ResTable_map_entry>();
183 if (UNLIKELY(!map)) {
184 return base::unexpected(IOError::PAGES_MISSING);
185 }
186
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700187 const size_t map_entry_count = dtohl(map->count);
188 size_t map_entries_start = entry_offset + entry_size;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000189 if (UNLIKELY(map_entries_start & 0x03U)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800190 LOG(ERROR) << "Map entries at offset " << entry_offset << " start at unaligned offset.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000191 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700192 }
193
194 // Each entry is sizeof(ResTable_map) big.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000195 if (UNLIKELY(map_entry_count > ((chunk_size - map_entries_start) / sizeof(ResTable_map)))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800196 LOG(ERROR) << "Too many map entries in ResTable_map_entry at offset " << entry_offset << ".";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000197 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700198 }
199 }
Eric Miao368cd192022-09-09 15:46:14 -0700200 return entry.verified();
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700201}
202
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100203LoadedPackage::iterator::iterator(const LoadedPackage* lp, size_t ti, size_t ei)
204 : loadedPackage_(lp),
205 typeIndex_(ti),
206 entryIndex_(ei),
207 typeIndexEnd_(lp->resource_ids_.size() + 1) {
208 while (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] == 0) {
209 typeIndex_++;
210 }
211}
212
213LoadedPackage::iterator& LoadedPackage::iterator::operator++() {
214 while (typeIndex_ < typeIndexEnd_) {
215 if (entryIndex_ + 1 < loadedPackage_->resource_ids_[typeIndex_]) {
216 entryIndex_++;
217 break;
218 }
219 entryIndex_ = 0;
220 typeIndex_++;
221 if (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] != 0) {
222 break;
223 }
224 }
225 return *this;
226}
227
228uint32_t LoadedPackage::iterator::operator*() const {
229 if (typeIndex_ >= typeIndexEnd_) {
230 return 0;
231 }
232 return make_resid(loadedPackage_->package_id_, typeIndex_ + loadedPackage_->type_id_offset_,
233 entryIndex_);
234}
235
Eric Miao368cd192022-09-09 15:46:14 -0700236base::expected<incfs::verified_map_ptr<ResTable_entry>, NullOrIOError> LoadedPackage::GetEntry(
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000237 incfs::verified_map_ptr<ResTable_type> type_chunk, uint16_t entry_index) {
238 base::expected<uint32_t, NullOrIOError> entry_offset = GetEntryOffset(type_chunk, entry_index);
239 if (UNLIKELY(!entry_offset.has_value())) {
240 return base::unexpected(entry_offset.error());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800241 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000242 return GetEntryFromOffset(type_chunk, entry_offset.value());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800243}
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700244
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000245base::expected<uint32_t, NullOrIOError> LoadedPackage::GetEntryOffset(
246 incfs::verified_map_ptr<ResTable_type> type_chunk, uint16_t entry_index) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800247 // The configuration matches and is better than the previous selection.
248 // Find the entry value if it exists for this configuration.
249 const size_t entry_count = dtohl(type_chunk->entryCount);
250 const size_t offsets_offset = dtohs(type_chunk->header.headerSize);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700251
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800252 // Check if there is the desired entry in this type.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800253 if (type_chunk->flags & ResTable_type::FLAG_SPARSE) {
254 // This is encoded as a sparse map, so perform a binary search.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000255 bool error = false;
256 auto sparse_indices = type_chunk.offset(offsets_offset)
257 .convert<ResTable_sparseTypeEntry>().iterator();
258 auto sparse_indices_end = sparse_indices + entry_count;
259 auto result = std::lower_bound(sparse_indices, sparse_indices_end, entry_index,
260 [&error](const incfs::map_ptr<ResTable_sparseTypeEntry>& entry,
261 uint16_t entry_idx) {
262 if (UNLIKELY(!entry)) {
263 return error = true;
264 }
265 return dtohs(entry->idx) < entry_idx;
266 });
Adam Lesinski73f6f9d2017-11-14 10:18:05 -0800267
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000268 if (result == sparse_indices_end) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800269 // No entry found.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000270 return base::unexpected(std::nullopt);
271 }
272
273 const incfs::verified_map_ptr<ResTable_sparseTypeEntry> entry = (*result).verified();
274 if (dtohs(entry->idx) != entry_index) {
275 if (error) {
276 return base::unexpected(IOError::PAGES_MISSING);
277 }
278 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700279 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800280
281 // Extract the offset from the entry. Each offset must be a multiple of 4 so we store it as
282 // the real offset divided by 4.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000283 return uint32_t{dtohs(entry->offset)} * 4u;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700284 }
285
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800286 // This type is encoded as a dense array.
287 if (entry_index >= entry_count) {
288 // This entry cannot be here.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000289 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700290 }
291
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000292 const auto entry_offset_ptr = type_chunk.offset(offsets_offset).convert<uint32_t>() + entry_index;
293 if (UNLIKELY(!entry_offset_ptr)) {
294 return base::unexpected(IOError::PAGES_MISSING);
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700295 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000296
297 const uint32_t value = dtohl(entry_offset_ptr.value());
298 if (value == ResTable_type::NO_ENTRY) {
299 return base::unexpected(std::nullopt);
300 }
301
302 return value;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700303}
304
Eric Miao368cd192022-09-09 15:46:14 -0700305base::expected<incfs::verified_map_ptr<ResTable_entry>, NullOrIOError>
306LoadedPackage::GetEntryFromOffset(incfs::verified_map_ptr<ResTable_type> type_chunk,
307 uint32_t offset) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000308 auto valid = VerifyResTableEntry(type_chunk, offset);
309 if (UNLIKELY(!valid.has_value())) {
310 return base::unexpected(valid.error());
311 }
Eric Miao368cd192022-09-09 15:46:14 -0700312 return valid;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000313}
314
315base::expected<std::monostate, IOError> LoadedPackage::CollectConfigurations(
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800316 bool exclude_mipmap, std::set<ResTable_config>* out_configs) const {\
317 for (const auto& type_spec : type_specs_) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000318 if (exclude_mipmap) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800319 const int type_idx = type_spec.first - 1;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000320 const auto type_name16 = type_string_pool_.stringAt(type_idx);
321 if (UNLIKELY(IsIOError(type_name16))) {
322 return base::unexpected(GetIOError(type_name16.error()));
323 }
324 if (type_name16.has_value()) {
325 if (strncmp16(type_name16->data(), u"mipmap", type_name16->size()) == 0) {
326 // This is a mipmap type, skip collection.
327 continue;
Adam Lesinski0c405242017-01-13 20:47:26 -0800328 }
329 }
330
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000331 const auto type_name = type_string_pool_.string8At(type_idx);
332 if (UNLIKELY(IsIOError(type_name))) {
333 return base::unexpected(GetIOError(type_name.error()));
334 }
335 if (type_name.has_value()) {
336 if (strncmp(type_name->data(), "mipmap", type_name->size()) == 0) {
337 // This is a mipmap type, skip collection.
338 continue;
339 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700340 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700341 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000342
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800343 for (const auto& type_entry : type_spec.second.type_entries) {
344 out_configs->insert(type_entry.config);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000345 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800346 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000347 return {};
Adam Lesinski0c405242017-01-13 20:47:26 -0800348}
349
350void LoadedPackage::CollectLocales(bool canonicalize, std::set<std::string>* out_locales) const {
351 char temp_locale[RESTABLE_MAX_LOCALE_LEN];
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800352 for (const auto& type_spec : type_specs_) {
353 for (const auto& type_entry : type_spec.second.type_entries) {
354 if (type_entry.config.locale != 0) {
355 type_entry.config.getBcp47Locale(temp_locale, canonicalize);
356 std::string locale(temp_locale);
357 out_locales->insert(std::move(locale));
Adam Lesinski0c405242017-01-13 20:47:26 -0800358 }
359 }
360 }
361}
362
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000363base::expected<uint32_t, NullOrIOError> LoadedPackage::FindEntryByName(
364 const std::u16string& type_name, const std::u16string& entry_name) const {
365 const base::expected<size_t, NullOrIOError> type_idx = type_string_pool_.indexOfString(
366 type_name.data(), type_name.size());
367 if (!type_idx.has_value()) {
368 return base::unexpected(type_idx.error());
Adam Lesinski929d6512017-01-16 19:11:19 -0800369 }
370
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000371 const base::expected<size_t, NullOrIOError> key_idx = key_string_pool_.indexOfString(
372 entry_name.data(), entry_name.size());
373 if (!key_idx.has_value()) {
374 return base::unexpected(key_idx.error());
Adam Lesinski929d6512017-01-16 19:11:19 -0800375 }
376
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800377 const TypeSpec* type_spec = GetTypeSpecByTypeIndex(*type_idx);
Adam Lesinski929d6512017-01-16 19:11:19 -0800378 if (type_spec == nullptr) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000379 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -0800380 }
381
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800382 for (const auto& type_entry : type_spec->type_entries) {
383 const incfs::verified_map_ptr<ResTable_type>& type = type_entry.type;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000384
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800385 size_t entry_count = dtohl(type->entryCount);
Adam Lesinski929d6512017-01-16 19:11:19 -0800386 for (size_t entry_idx = 0; entry_idx < entry_count; entry_idx++) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000387 auto entry_offset_ptr = type.offset(dtohs(type->header.headerSize)).convert<uint32_t>() +
388 entry_idx;
389 if (!entry_offset_ptr) {
390 return base::unexpected(IOError::PAGES_MISSING);
391 }
392
Jason O'Brien984e8972021-08-26 19:07:47 -0500393 uint32_t offset;
394 uint16_t res_idx;
395 if (type->flags & ResTable_type::FLAG_SPARSE) {
396 auto sparse_entry = entry_offset_ptr.convert<ResTable_sparseTypeEntry>();
397 offset = dtohs(sparse_entry->offset) * 4u;
398 res_idx = dtohs(sparse_entry->idx);
399 } else {
400 offset = dtohl(entry_offset_ptr.value());
401 res_idx = entry_idx;
402 }
Adam Lesinski929d6512017-01-16 19:11:19 -0800403 if (offset != ResTable_type::NO_ENTRY) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000404 auto entry = type.offset(dtohl(type->entriesStart) + offset).convert<ResTable_entry>();
405 if (!entry) {
406 return base::unexpected(IOError::PAGES_MISSING);
407 }
408
Eric Miao368cd192022-09-09 15:46:14 -0700409 if (entry->key() == static_cast<uint32_t>(*key_idx)) {
Adam Lesinski929d6512017-01-16 19:11:19 -0800410 // The package ID will be overridden by the caller (due to runtime assignment of package
411 // IDs for shared libraries).
Jason O'Brien984e8972021-08-26 19:07:47 -0500412 return make_resid(0x00, *type_idx + type_id_offset_ + 1, res_idx);
Adam Lesinski929d6512017-01-16 19:11:19 -0800413 }
414 }
415 }
416 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000417 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -0800418}
419
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800420const LoadedPackage* LoadedArsc::GetPackageById(uint8_t package_id) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700421 for (const auto& loaded_package : packages_) {
422 if (loaded_package->GetPackageId() == package_id) {
423 return loaded_package.get();
424 }
425 }
426 return nullptr;
427}
428
429std::unique_ptr<const LoadedPackage> LoadedPackage::Load(const Chunk& chunk,
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800430 package_property_t property_flags) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800431 ATRACE_NAME("LoadedPackage::Load");
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700432 std::unique_ptr<LoadedPackage> loaded_package(new LoadedPackage());
Adam Lesinskida431a22016-12-29 16:08:16 -0500433
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700434 // typeIdOffset was added at some point, but we still must recognize apps built before this
435 // was added.
Adam Lesinski33af6c72017-03-29 13:00:35 -0700436 constexpr size_t kMinPackageSize =
437 sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000438 const incfs::map_ptr<ResTable_package> header = chunk.header<ResTable_package, kMinPackageSize>();
439 if (!header) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800440 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500441 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700442 }
443
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800444 if ((property_flags & PROPERTY_SYSTEM) != 0) {
445 loaded_package->property_flags_ |= PROPERTY_SYSTEM;
446 }
447
448 if ((property_flags & PROPERTY_LOADER) != 0) {
449 loaded_package->property_flags_ |= PROPERTY_LOADER;
450 }
451
452 if ((property_flags & PROPERTY_OVERLAY) != 0) {
453 // Overlay resources must have an exclusive resource id space for referencing internal
454 // resources.
455 loaded_package->property_flags_ |= PROPERTY_OVERLAY | PROPERTY_DYNAMIC;
456 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700457
Adam Lesinski7ad11102016-10-28 16:39:15 -0700458 loaded_package->package_id_ = dtohl(header->id);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700459 if (loaded_package->package_id_ == 0 ||
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800460 (loaded_package->package_id_ == kAppPackageId && (property_flags & PROPERTY_DYNAMIC) != 0)) {
461 loaded_package->property_flags_ |= PROPERTY_DYNAMIC;
Winson9947f1e2019-08-16 10:20:39 -0700462 }
463
Adam Lesinskic6aada92017-01-13 15:34:14 -0800464 if (header->header.headerSize >= sizeof(ResTable_package)) {
465 uint32_t type_id_offset = dtohl(header->typeIdOffset);
466 if (type_id_offset > std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800467 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE type ID offset too large.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800468 return {};
469 }
470 loaded_package->type_id_offset_ = static_cast<int>(type_id_offset);
471 }
472
Adam Lesinskida431a22016-12-29 16:08:16 -0500473 util::ReadUtf16StringFromDevice(header->name, arraysize(header->name),
474 &loaded_package->package_name_);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700475
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800476 // A map of TypeSpec builders, each associated with an type index.
477 // We use these to accumulate the set of Types available for a TypeSpec, and later build a single,
478 // contiguous block of memory that holds all the Types together with the TypeSpec.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800479 std::unordered_map<int, std::unique_ptr<TypeSpecBuilder>> type_builder_map;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700480
481 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
482 while (iter.HasNext()) {
483 const Chunk child_chunk = iter.Next();
484 switch (child_chunk.type()) {
485 case RES_STRING_POOL_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000486 const auto pool_address = child_chunk.header<ResChunk_header>();
487 if (!pool_address) {
488 LOG(ERROR) << "RES_STRING_POOL_TYPE is incomplete due to incremental installation.";
489 return {};
490 }
491
492 if (pool_address == header.offset(dtohl(header->typeStrings)).convert<ResChunk_header>()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700493 // This string pool is the type string pool.
494 status_t err = loaded_package->type_string_pool_.setTo(
495 child_chunk.header<ResStringPool_header>(), child_chunk.size());
496 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800497 LOG(ERROR) << "RES_STRING_POOL_TYPE for types corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500498 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700499 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000500 } else if (pool_address == header.offset(dtohl(header->keyStrings))
501 .convert<ResChunk_header>()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700502 // This string pool is the key string pool.
503 status_t err = loaded_package->key_string_pool_.setTo(
504 child_chunk.header<ResStringPool_header>(), child_chunk.size());
505 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800506 LOG(ERROR) << "RES_STRING_POOL_TYPE for keys corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500507 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700508 }
509 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800510 LOG(WARNING) << "Too many RES_STRING_POOL_TYPEs found in RES_TABLE_PACKAGE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700511 }
512 } break;
513
514 case RES_TABLE_TYPE_SPEC_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000515 const auto type_spec = child_chunk.header<ResTable_typeSpec>();
516 if (!type_spec) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800517 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500518 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700519 }
520
521 if (type_spec->id == 0) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800522 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500523 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700524 }
525
Adam Lesinskic6aada92017-01-13 15:34:14 -0800526 if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) >
527 std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800528 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has out of range ID.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800529 return {};
530 }
531
Adam Lesinski7ad11102016-10-28 16:39:15 -0700532 // The data portion of this chunk contains entry_count 32bit entries,
533 // each one representing a set of flags.
534 // Here we only validate that the chunk is well formed.
535 const size_t entry_count = dtohl(type_spec->entryCount);
536
537 // There can only be 2^16 entries in a type, because that is the ID
538 // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
539 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800540 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has too many entries (" << entry_count << ").";
Adam Lesinskida431a22016-12-29 16:08:16 -0500541 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700542 }
543
544 if (entry_count * sizeof(uint32_t) > chunk.data_size()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800545 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500546 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700547 }
548
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800549 std::unique_ptr<TypeSpecBuilder>& builder_ptr = type_builder_map[type_spec->id];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800550 if (builder_ptr == nullptr) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800551 builder_ptr = util::make_unique<TypeSpecBuilder>(type_spec.verified());
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100552 loaded_package->resource_ids_.set(type_spec->id, entry_count);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800553 } else {
554 LOG(WARNING) << StringPrintf("RES_TABLE_TYPE_SPEC_TYPE already defined for ID %02x",
555 type_spec->id);
556 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700557 } break;
558
559 case RES_TABLE_TYPE_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000560 const auto type = child_chunk.header<ResTable_type, kResTableTypeMinSize>();
561 if (!type) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800562 LOG(ERROR) << "RES_TABLE_TYPE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500563 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700564 }
565
Adam Lesinski498f6052017-11-29 13:24:29 -0800566 if (!VerifyResTableType(type)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500567 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700568 }
569
570 // Type chunks must be preceded by their TypeSpec chunks.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800571 std::unique_ptr<TypeSpecBuilder>& builder_ptr = type_builder_map[type->id];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800572 if (builder_ptr != nullptr) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000573 builder_ptr->AddType(type.verified());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800574 } else {
575 LOG(ERROR) << StringPrintf(
576 "RES_TABLE_TYPE_TYPE with ID %02x found without preceding RES_TABLE_TYPE_SPEC_TYPE.",
577 type->id);
Adam Lesinskida431a22016-12-29 16:08:16 -0500578 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700579 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700580 } break;
581
Adam Lesinskida431a22016-12-29 16:08:16 -0500582 case RES_TABLE_LIBRARY_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000583 const auto lib = child_chunk.header<ResTable_lib_header>();
584 if (!lib) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800585 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500586 return {};
587 }
588
589 if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800590 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500591 return {};
592 }
593
594 loaded_package->dynamic_package_map_.reserve(dtohl(lib->count));
595
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000596 const auto entry_begin = child_chunk.data_ptr().convert<ResTable_lib_entry>();
597 const auto entry_end = entry_begin + dtohl(lib->count);
Adam Lesinskida431a22016-12-29 16:08:16 -0500598 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000599 if (!entry_iter) {
600 return {};
601 }
602
Adam Lesinskida431a22016-12-29 16:08:16 -0500603 std::string package_name;
604 util::ReadUtf16StringFromDevice(entry_iter->packageName,
605 arraysize(entry_iter->packageName), &package_name);
606
607 if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800608 LOG(ERROR) << StringPrintf(
Adam Lesinskida431a22016-12-29 16:08:16 -0500609 "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.",
610 dtohl(entry_iter->packageId), package_name.c_str());
611 return {};
612 }
613
614 loaded_package->dynamic_package_map_.emplace_back(std::move(package_name),
615 dtohl(entry_iter->packageId));
616 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800617 } break;
Adam Lesinskida431a22016-12-29 16:08:16 -0500618
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800619 case RES_TABLE_OVERLAYABLE_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000620 const auto overlayable = child_chunk.header<ResTable_overlayable_header>();
621 if (!overlayable) {
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800622 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_TYPE too small.";
623 return {};
624 }
625
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800626 std::string name;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000627 util::ReadUtf16StringFromDevice(overlayable->name, arraysize(overlayable->name), &name);
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800628 std::string actor;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000629 util::ReadUtf16StringFromDevice(overlayable->actor, arraysize(overlayable->actor), &actor);
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800630
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100631 if (loaded_package->overlayable_map_.find(name) !=
632 loaded_package->overlayable_map_.end()) {
633 LOG(ERROR) << "Multiple <overlayable> blocks with the same name '" << name << "'.";
634 return {};
635 }
636 loaded_package->overlayable_map_.emplace(name, actor);
637
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800638 // Iterate over the overlayable policy chunks contained within the overlayable chunk data
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800639 ChunkIterator overlayable_iter(child_chunk.data_ptr(), child_chunk.data_size());
640 while (overlayable_iter.HasNext()) {
641 const Chunk overlayable_child_chunk = overlayable_iter.Next();
642
643 switch (overlayable_child_chunk.type()) {
644 case RES_TABLE_OVERLAYABLE_POLICY_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000645 const auto policy_header =
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800646 overlayable_child_chunk.header<ResTable_overlayable_policy_header>();
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000647 if (!policy_header) {
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800648 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small.";
649 return {};
650 }
651
652 if ((overlayable_child_chunk.data_size() / sizeof(ResTable_ref))
653 < dtohl(policy_header->entry_count)) {
654 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small to hold entries.";
655 return {};
656 }
657
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800658 // Retrieve all the resource ids belonging to this policy chunk
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000659 const auto ids_begin = overlayable_child_chunk.data_ptr().convert<ResTable_ref>();
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800660 const auto ids_end = ids_begin + dtohl(policy_header->entry_count);
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800661 std::unordered_set<uint32_t> ids;
662 ids.reserve(ids_end - ids_begin);
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800663 for (auto id_iter = ids_begin; id_iter != ids_end; ++id_iter) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000664 if (!id_iter) {
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800665 LOG(ERROR) << "NULL ResTable_ref record??";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000666 return {};
667 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800668 ids.insert(dtohl(id_iter->ident));
669 }
670
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800671 // Add the pairing of overlayable properties and resource ids to the package
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800672 OverlayableInfo overlayable_info {
673 .name = name,
674 .actor = actor,
675 .policy_flags = policy_header->policy_flags
676 };
677 loaded_package->overlayable_infos_.emplace_back(std::move(overlayable_info), std::move(ids));
Ryan Mitchell19823452019-01-29 12:01:24 -0800678 loaded_package->defines_overlayable_ = true;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800679 break;
680 }
681
682 default:
683 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
684 break;
685 }
686 }
687
688 if (overlayable_iter.HadError()) {
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800689 LOG(ERROR) << StringPrintf("Error parsing RES_TABLE_OVERLAYABLE_TYPE: %s",
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800690 overlayable_iter.GetLastError().c_str());
691 if (overlayable_iter.HadFatalError()) {
692 return {};
693 }
694 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500695 } break;
696
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700697 case RES_TABLE_STAGED_ALIAS_TYPE: {
698 if (loaded_package->package_id_ != kFrameworkPackageId) {
699 LOG(WARNING) << "Alias chunk ignored for non-framework package '"
700 << loaded_package->package_name_ << "'";
701 break;
702 }
703
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700704 const auto lib_alias = child_chunk.header<ResTable_staged_alias_header>();
705 if (!lib_alias) {
Yurii Zubrytskyi80020342021-11-29 23:55:24 -0800706 LOG(ERROR) << "RES_TABLE_STAGED_ALIAS_TYPE is too small.";
707 return {};
708 }
709 if ((child_chunk.data_size() / sizeof(ResTable_staged_alias_entry))
710 < dtohl(lib_alias->count)) {
711 LOG(ERROR) << "RES_TABLE_STAGED_ALIAS_TYPE is too small to hold entries.";
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700712 return {};
713 }
714 const auto entry_begin = child_chunk.data_ptr().convert<ResTable_staged_alias_entry>();
715 const auto entry_end = entry_begin + dtohl(lib_alias->count);
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800716 std::unordered_set<uint32_t> finalized_ids;
717 finalized_ids.reserve(entry_end - entry_begin);
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700718 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
719 if (!entry_iter) {
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800720 LOG(ERROR) << "NULL ResTable_staged_alias_entry record??";
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700721 return {};
722 }
723 auto finalized_id = dtohl(entry_iter->finalizedResId);
724 if (!finalized_ids.insert(finalized_id).second) {
725 LOG(ERROR) << StringPrintf("Repeated finalized resource id '%08x' in staged aliases.",
726 finalized_id);
727 return {};
728 }
729
730 auto staged_id = dtohl(entry_iter->stagedResId);
Yurii Zubrytskyi72742462021-11-30 00:03:51 -0800731 auto [_, success] = loaded_package->alias_id_map_.emplace(staged_id, finalized_id);
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700732 if (!success) {
733 LOG(ERROR) << StringPrintf("Repeated staged resource id '%08x' in staged aliases.",
734 staged_id);
735 return {};
736 }
737 }
738 } break;
739
Adam Lesinski7ad11102016-10-28 16:39:15 -0700740 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800741 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700742 break;
743 }
744 }
745
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800746 if (iter.HadError()) {
747 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700748 if (iter.HadFatalError()) {
749 return {};
750 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800751 }
752
753 // Flatten and construct the TypeSpecs.
754 for (auto& entry : type_builder_map) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800755 TypeSpec type_spec = entry.second->Build();
756 uint8_t type_id = static_cast<uint8_t>(entry.first);
757 loaded_package->type_specs_[type_id] = std::move(type_spec);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700758 }
759
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700760 return std::move(loaded_package);
761}
762
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700763bool LoadedArsc::LoadTable(const Chunk& chunk, const LoadedIdmap* loaded_idmap,
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800764 package_property_t property_flags) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000765 incfs::map_ptr<ResTable_header> header = chunk.header<ResTable_header>();
766 if (!header) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800767 LOG(ERROR) << "RES_TABLE_TYPE too small.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700768 return false;
769 }
770
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700771 if (loaded_idmap != nullptr) {
772 global_string_pool_ = util::make_unique<OverlayStringPool>(loaded_idmap);
773 }
774
Adam Lesinski7ad11102016-10-28 16:39:15 -0700775 const size_t package_count = dtohl(header->packageCount);
776 size_t packages_seen = 0;
777
778 packages_.reserve(package_count);
779
780 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
781 while (iter.HasNext()) {
782 const Chunk child_chunk = iter.Next();
783 switch (child_chunk.type()) {
784 case RES_STRING_POOL_TYPE:
785 // Only use the first string pool. Ignore others.
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700786 if (global_string_pool_->getError() == NO_INIT) {
787 status_t err = global_string_pool_->setTo(child_chunk.header<ResStringPool_header>(),
788 child_chunk.size());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700789 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800790 LOG(ERROR) << "RES_STRING_POOL_TYPE corrupt.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700791 return false;
792 }
793 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800794 LOG(WARNING) << "Multiple RES_STRING_POOL_TYPEs found in RES_TABLE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700795 }
796 break;
797
798 case RES_TABLE_PACKAGE_TYPE: {
799 if (packages_seen + 1 > package_count) {
800 LOG(ERROR) << "More package chunks were found than the " << package_count
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700801 << " declared in the header.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700802 return false;
803 }
804 packages_seen++;
805
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700806 std::unique_ptr<const LoadedPackage> loaded_package =
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800807 LoadedPackage::Load(child_chunk, property_flags);
Adam Lesinskida431a22016-12-29 16:08:16 -0500808 if (!loaded_package) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700809 return false;
810 }
811 packages_.push_back(std::move(loaded_package));
812 } break;
813
814 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800815 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700816 break;
817 }
818 }
819
820 if (iter.HadError()) {
821 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700822 if (iter.HadFatalError()) {
823 return false;
824 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700825 }
826 return true;
827}
828
Jeremy Meyer65871022022-07-12 22:45:08 +0000829bool LoadedArsc::LoadStringPool(const LoadedIdmap* loaded_idmap) {
830 if (loaded_idmap != nullptr) {
831 global_string_pool_ = util::make_unique<OverlayStringPool>(loaded_idmap);
832 }
833 return true;
834}
835
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800836std::unique_ptr<LoadedArsc> LoadedArsc::Load(incfs::map_ptr<void> data,
837 const size_t length,
838 const LoadedIdmap* loaded_idmap,
839 const package_property_t property_flags) {
Winson9947f1e2019-08-16 10:20:39 -0700840 ATRACE_NAME("LoadedArsc::Load");
Adam Lesinski7ad11102016-10-28 16:39:15 -0700841
842 // Not using make_unique because the constructor is private.
843 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
844
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000845 ChunkIterator iter(data, length);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700846 while (iter.HasNext()) {
847 const Chunk chunk = iter.Next();
848 switch (chunk.type()) {
849 case RES_TABLE_TYPE:
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800850 if (!loaded_arsc->LoadTable(chunk, loaded_idmap, property_flags)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700851 return {};
852 }
853 break;
854
855 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800856 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700857 break;
858 }
859 }
860
861 if (iter.HadError()) {
862 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700863 if (iter.HadFatalError()) {
864 return {};
865 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700866 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800867
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800868 return loaded_arsc;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700869}
870
Jeremy Meyer65871022022-07-12 22:45:08 +0000871std::unique_ptr<LoadedArsc> LoadedArsc::Load(const LoadedIdmap* loaded_idmap) {
872 ATRACE_NAME("LoadedArsc::Load");
873
874 // Not using make_unique because the constructor is private.
875 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
876 loaded_arsc->LoadStringPool(loaded_idmap);
877 return loaded_arsc;
878}
879
880
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800881std::unique_ptr<LoadedArsc> LoadedArsc::CreateEmpty() {
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700882 return std::unique_ptr<LoadedArsc>(new LoadedArsc());
883}
884
Adam Lesinski7ad11102016-10-28 16:39:15 -0700885} // namespace android