Debian Codegraph @

cmds/idmap2/libidmap2/Idmap.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2018 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 #include "idmap2/Idmap.h"
18
19 #include <algorithm>
20 #include <cassert>
21 #include <iostream>
22 #include <iterator>
23 #include <limits>
24 #include <memory>
25 #include <string>
26 #include <utility>
27
28 #include "android-base/macros.h"
29 #include "androidfw/AssetManager2.h"
30 #include "idmap2/ResourceMapping.h"
31 #include "idmap2/ResourceUtils.h"
32 #include "idmap2/Result.h"
33 #include "idmap2/SysTrace.h"
34
35 namespace android::idmap2 {
36
37 namespace {
38
39 bool WARN_UNUSED Read8(std::istream& stream, uint8_t* out) {
40 uint8_t value;
41 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint8_t))) {
42 *out = value;
43 return true;
44 }
45 return false;
46 }
47
48 bool WARN_UNUSED Read16(std::istream& stream, uint16_t* out) {
49 uint16_t value;
50 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint16_t))) {
51 *out = dtohs(value);
52 return true;
53 }
54 return false;
55 }
56
57 bool WARN_UNUSED Read32(std::istream& stream, uint32_t* out) {
58 uint32_t value;
59 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint32_t))) {
60 *out = dtohl(value);
61 return true;
62 }
63 return false;
64 }
65
66 bool WARN_UNUSED ReadString(std::istream& stream, std::string* out) {
67 uint32_t size;
68 if (!Read32(stream, &size)) {
69 return false;
70 }
71 if (size == 0) {
72 *out = "";
73 return true;
74 }
75 std::string buf(size, '\0');
76 if (!stream.read(buf.data(), size)) {
77 return false;
78 }
79 uint32_t padding_size = CalculatePadding(size);
80 std::string padding(padding_size, '\0');
81 if (!stream.read(padding.data(), padding_size)) {
82 return false;
83 }
84 *out = buf;
85 return true;
86 }
87
88 } // namespace
89
90 std::unique_ptr<const IdmapHeader> IdmapHeader::FromBinaryStream(std::istream& stream) {
91 std::unique_ptr<IdmapHeader> idmap_header(new IdmapHeader());
92 if (!Read32(stream, &idmap_header->magic_) || !Read32(stream, &idmap_header->version_)) {
93 return nullptr;
94 }
95
96 if (idmap_header->magic_ != kIdmapMagic || idmap_header->version_ != kIdmapCurrentVersion) {
97 // Do not continue parsing if the file is not a current version idmap.
98 return nullptr;
99 }
100
101 uint32_t enforce_overlayable;
102 if (!Read32(stream, &idmap_header->target_crc_) || !Read32(stream, &idmap_header->overlay_crc_) ||
103 !Read32(stream, &idmap_header->fulfilled_policies_) ||
104 !Read32(stream, &enforce_overlayable) || !ReadString(stream, &idmap_header->target_path_) ||
105 !ReadString(stream, &idmap_header->overlay_path_) ||
106 !ReadString(stream, &idmap_header->overlay_name_) ||
107 !ReadString(stream, &idmap_header->debug_info_)) {
108 return nullptr;
109 }
110
111 idmap_header->enforce_overlayable_ = enforce_overlayable != 0U;
112 return std::move(idmap_header);
113 }
114
115 Result<Unit> IdmapHeader::IsUpToDate(const TargetResourceContainer& target,
116 const OverlayResourceContainer& overlay,
117 const std::string& overlay_name,
118 PolicyBitmask fulfilled_policies,
119 bool enforce_overlayable) const {
120 const Result<uint32_t> target_crc = target.GetCrc();
121 if (!target_crc) {
122 return Error("failed to get target crc");
123 }
124
125 const Result<uint32_t> overlay_crc = overlay.GetCrc();
126 if (!overlay_crc) {
127 return Error("failed to get overlay crc");
128 }
129
130 return IsUpToDate(target.GetPath(), overlay.GetPath(), overlay_name, *target_crc, *overlay_crc,
131 fulfilled_policies, enforce_overlayable);
132 }
133
134 Result<Unit> IdmapHeader::IsUpToDate(const std::string& target_path,
135 const std::string& overlay_path,
136 const std::string& overlay_name, uint32_t target_crc,
137 uint32_t overlay_crc, PolicyBitmask fulfilled_policies,
138 bool enforce_overlayable) const {
139 if (magic_ != kIdmapMagic) {
140 return Error("bad magic: actual 0x%08x, expected 0x%08x", magic_, kIdmapMagic);
141 }
142
143 if (version_ != kIdmapCurrentVersion) {
144 return Error("bad version: actual 0x%08x, expected 0x%08x", version_, kIdmapCurrentVersion);
145 }
146
147 if (target_crc_ != target_crc) {
148 return Error("bad target crc: idmap version 0x%08x, file system version 0x%08x", target_crc_,
149 target_crc);
150 }
151
152 if (overlay_crc_ != overlay_crc) {
153 return Error("bad overlay crc: idmap version 0x%08x, file system version 0x%08x", overlay_crc_,
154 overlay_crc);
155 }
156
157 if (fulfilled_policies_ != fulfilled_policies) {
158 return Error("bad fulfilled policies: idmap version 0x%08x, file system version 0x%08x",
159 fulfilled_policies, fulfilled_policies_);
160 }
161
162 if (enforce_overlayable != enforce_overlayable_) {
163 return Error("bad enforce overlayable: idmap version %s, file system version %s",
164 enforce_overlayable ? "true" : "false", enforce_overlayable_ ? "true" : "false");
165 }
166
167 if (target_path != target_path_) {
168 return Error("bad target path: idmap version %s, file system version %s", target_path.c_str(),
169 target_path_.c_str());
170 }
171
172 if (overlay_path != overlay_path_) {
173 return Error("bad overlay path: idmap version %s, file system version %s", overlay_path.c_str(),
174 overlay_path_.c_str());
175 }
176
177 if (overlay_name != overlay_name_) {
178 return Error("bad overlay name: idmap version %s, file system version %s", overlay_name.c_str(),
179 overlay_name_.c_str());
180 }
181
182 return Unit{};
183 }
184
185 std::unique_ptr<const IdmapData::Header> IdmapData::Header::FromBinaryStream(std::istream& stream) {
186 std::unique_ptr<IdmapData::Header> idmap_data_header(new IdmapData::Header());
187 if (!Read32(stream, &idmap_data_header->target_entry_count) ||
188 !Read32(stream, &idmap_data_header->target_entry_inline_count) ||
189 !Read32(stream, &idmap_data_header->overlay_entry_count) ||
190 !Read32(stream, &idmap_data_header->string_pool_index_offset)) {
191 return nullptr;
192 }
193
194 return std::move(idmap_data_header);
195 }
196
197 std::unique_ptr<const IdmapData> IdmapData::FromBinaryStream(std::istream& stream) {
198 std::unique_ptr<IdmapData> data(new IdmapData());
199 data->header_ = IdmapData::Header::FromBinaryStream(stream);
200 if (!data->header_) {
201 return nullptr;
202 }
203
204 // Read the mapping of target resource id to overlay resource value.
205 for (size_t i = 0; i < data->header_->GetTargetEntryCount(); i++) {
206 TargetEntry target_entry{};
207 if (!Read32(stream, &target_entry.target_id) || !Read32(stream, &target_entry.overlay_id)) {
208 return nullptr;
209 }
210 data->target_entries_.push_back(target_entry);
211 }
212
213 // Read the mapping of target resource id to inline overlay values.
214 uint8_t unused1;
215 uint16_t unused2;
216 for (size_t i = 0; i < data->header_->GetTargetInlineEntryCount(); i++) {
217 TargetInlineEntry target_entry{};
218 if (!Read32(stream, &target_entry.target_id) || !Read16(stream, &unused2) ||
219 !Read8(stream, &unused1) || !Read8(stream, &target_entry.value.data_type) ||
220 !Read32(stream, &target_entry.value.data_value)) {
221 return nullptr;
222 }
223 data->target_inline_entries_.push_back(target_entry);
224 }
225
226 // Read the mapping of overlay resource id to target resource id.
227 for (size_t i = 0; i < data->header_->GetOverlayEntryCount(); i++) {
228 OverlayEntry overlay_entry{};
229 if (!Read32(stream, &overlay_entry.overlay_id) || !Read32(stream, &overlay_entry.target_id)) {
230 return nullptr;
231 }
232 data->overlay_entries_.emplace_back(overlay_entry);
233 }
234
235 // Read raw string pool bytes.
236 if (!ReadString(stream, &data->string_pool_data_)) {
237 return nullptr;
238 }
239 return std::move(data);
240 }
241
242 std::string Idmap::CanonicalIdmapPathFor(const std::string& absolute_dir,
243 const std::string& absolute_apk_path) {
244 assert(absolute_dir.size() > 0 && absolute_dir[0] == "/");
245 assert(absolute_apk_path.size() > 0 && absolute_apk_path[0] == "/");
246 std::string copy(++absolute_apk_path.cbegin(), absolute_apk_path.cend());
247 replace(copy.begin(), copy.end(), '/', '@');
248 return absolute_dir + "/" + copy + "@idmap";
249 }
250
251 Result<std::unique_ptr<const Idmap>> Idmap::FromBinaryStream(std::istream& stream) {
252 SYSTRACE << "Idmap::FromBinaryStream";
253 std::unique_ptr<Idmap> idmap(new Idmap());
254
255 idmap->header_ = IdmapHeader::FromBinaryStream(stream);
256 if (!idmap->header_) {
257 return Error("failed to parse idmap header");
258 }
259
260 // idmap version 0x01 does not specify the number of data blocks that follow
261 // the idmap header; assume exactly one data block
262 for (int i = 0; i < 1; i++) {
263 std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream);
264 if (!data) {
265 return Error("failed to parse data block %d", i);
266 }
267 idmap->data_.push_back(std::move(data));
268 }
269
270 return {std::move(idmap)};
271 }
272
273 Result<std::unique_ptr<const IdmapData>> IdmapData::FromResourceMapping(
274 const ResourceMapping& resource_mapping) {
275 if (resource_mapping.GetTargetToOverlayMap().empty()) {
276 return Error("no resources were overlaid");
277 }
278
279 std::unique_ptr<IdmapData> data(new IdmapData());
280 data->string_pool_data_ = resource_mapping.GetStringPoolData().to_string();
281 for (const auto& mapping : resource_mapping.GetTargetToOverlayMap()) {
282 if (auto overlay_resource = std::get_if<ResourceId>(&mapping.second)) {
283 data->target_entries_.push_back({mapping.first, *overlay_resource});
284 } else {
285 data->target_inline_entries_.push_back(
286 {mapping.first, std::get<TargetValue>(mapping.second)});
287 }
288 }
289
290 for (const auto& mapping : resource_mapping.GetOverlayToTargetMap()) {
291 data->overlay_entries_.emplace_back(IdmapData::OverlayEntry{mapping.first, mapping.second});
292 }
293
294 std::unique_ptr<IdmapData::Header> data_header(new IdmapData::Header());
295 data_header->target_entry_count = static_cast<uint32_t>(data->target_entries_.size());
296 data_header->target_entry_inline_count =
297 static_cast<uint32_t>(data->target_inline_entries_.size());
298 data_header->overlay_entry_count = static_cast<uint32_t>(data->overlay_entries_.size());
299 data_header->string_pool_index_offset = resource_mapping.GetStringPoolOffset();
300 data->header_ = std::move(data_header);
301 return {std::move(data)};
302 }
303
304 Result<std::unique_ptr<const Idmap>> Idmap::FromContainers(const TargetResourceContainer& target,
305 const OverlayResourceContainer& overlay,
306 const std::string& overlay_name,
307 const PolicyBitmask& fulfilled_policies,
308 bool enforce_overlayable) {
309 SYSTRACE << "Idmap::FromApkAssets";
310 std::unique_ptr<IdmapHeader> header(new IdmapHeader());
311 header->magic_ = kIdmapMagic;
312 header->version_ = kIdmapCurrentVersion;
313
314 const auto target_crc = target.GetCrc();
315 if (!target_crc) {
316 return Error(target_crc.GetError(), "failed to get zip CRC for '%s'", target.GetPath().data());
317 }
318 header->target_crc_ = *target_crc;
319
320 const auto overlay_crc = overlay.GetCrc();
321 if (!overlay_crc) {
322 return Error(overlay_crc.GetError(), "failed to get zip CRC for '%s'",
323 overlay.GetPath().data());
324 }
325 header->overlay_crc_ = *overlay_crc;
326
327 header->fulfilled_policies_ = fulfilled_policies;
328 header->enforce_overlayable_ = enforce_overlayable;
329 header->target_path_ = target.GetPath();
330 header->overlay_path_ = overlay.GetPath();
331 header->overlay_name_ = overlay_name;
332
333 auto info = overlay.FindOverlayInfo(overlay_name);
334 if (!info) {
335 return Error(info.GetError(), "failed to get overlay info for '%s'", overlay.GetPath().data());
336 }
337
338 LogInfo log_info;
339 auto resource_mapping = ResourceMapping::FromContainers(
340 target, overlay, *info, fulfilled_policies, enforce_overlayable, log_info);
341 if (!resource_mapping) {
342 return Error(resource_mapping.GetError(), "failed to generate resource map for '%s'",
343 overlay.GetPath().data());
344 }
345
346 auto idmap_data = IdmapData::FromResourceMapping(*resource_mapping);
347 if (!idmap_data) {
348 return idmap_data.GetError();
349 }
350
351 std::unique_ptr<Idmap> idmap(new Idmap());
352 header->debug_info_ = log_info.GetString();
353 idmap->header_ = std::move(header);
354 idmap->data_.push_back(std::move(*idmap_data));
355
356 return {std::move(idmap)};
357 }
358
359 void IdmapHeader::accept(Visitor* v) const {
360 assert(v != nullptr);
361 v->visit(*this);
362 }
363
364 void IdmapData::Header::accept(Visitor* v) const {
365 assert(v != nullptr);
366 v->visit(*this);
367 }
368
369 void IdmapData::accept(Visitor* v) const {
370 assert(v != nullptr);
371 header_->accept(v);
372 v->visit(*this);
373 }
374
375 void Idmap::accept(Visitor* v) const {
376 assert(v != nullptr);
377 header_->accept(v);
378 v->visit(*this);
379 auto end = data_.cend();
380 for (auto iter = data_.cbegin(); iter != end; ++iter) {
381 (*iter)->accept(v);
382 }
383 }
384
385 } // namespace android::idmap2