From e753ffef5499bc0150827a06d44b50abe78b36dd Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Mon, 23 Sep 2019 09:47:02 -0700 Subject: Idmap format changes for bidirectional lookup This change modifies the idmap file format to allow for target resources to map to arbitrary type/value combinations and to allow overlay resources to be mapped back to target resource ids so references to overlay resources can appear as references to target resources at runtime. The mappings of target resources to overlay resources and vice-versa are both encoded as sparse arrays. Instead of looking up a resource by indexing into an array that maps to the overlay resource id, the runtime will binary search over the sparse array to find the type and value that overlays the target resource. Bug: 135943783 Test: idmap2_tests Change-Id: I5d5344cdb7fe35f4f2e8d6781016299dea5d1e20 --- cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp | 44 ++++++++++++++++----------- 1 file changed, 27 insertions(+), 17 deletions(-) (limited to 'cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp') diff --git a/cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp b/cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp index dee2d219cbe1..3b0940ae06ef 100644 --- a/cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp +++ b/cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp @@ -24,6 +24,14 @@ namespace android::idmap2 { +void BinaryStreamVisitor::Write(const void* value, size_t length) { + stream_.write(reinterpret_cast(value), length); +} + +void BinaryStreamVisitor::Write8(uint8_t value) { + stream_.write(reinterpret_cast(&value), sizeof(uint8_t)); +} + void BinaryStreamVisitor::Write16(uint16_t value) { uint16_t x = htodl(value); stream_.write(reinterpret_cast(&x), sizeof(uint16_t)); @@ -54,26 +62,28 @@ void BinaryStreamVisitor::visit(const IdmapHeader& header) { WriteString(header.GetOverlayPath()); } -void BinaryStreamVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) { - // nothing to do -} +void BinaryStreamVisitor::visit(const IdmapData& data) { + for (const auto& target_entry : data.GetTargetEntries()) { + Write32(target_entry.target_id); + Write8(target_entry.data_type); + Write32(target_entry.data_value); + } -void BinaryStreamVisitor::visit(const IdmapData::Header& header) { - Write16(header.GetTargetPackageId()); - Write16(header.GetTypeCount()); -} + for (const auto& overlay_entry : data.GetOverlayEntries()) { + Write32(overlay_entry.overlay_id); + Write32(overlay_entry.target_id); + } -void BinaryStreamVisitor::visit(const IdmapData::TypeEntry& type_entry) { - const uint16_t entryCount = type_entry.GetEntryCount(); + Write(data.GetStringPoolData(), data.GetHeader()->GetStringPoolLength()); +} - Write16(type_entry.GetTargetTypeId()); - Write16(type_entry.GetOverlayTypeId()); - Write16(entryCount); - Write16(type_entry.GetEntryOffset()); - for (uint16_t i = 0; i < entryCount; i++) { - EntryId entry_id = type_entry.GetEntry(i); - Write32(entry_id != kNoEntry ? static_cast(entry_id) : kPadding); - } +void BinaryStreamVisitor::visit(const IdmapData::Header& header) { + Write8(header.GetTargetPackageId()); + Write8(header.GetOverlayPackageId()); + Write32(header.GetTargetEntryCount()); + Write32(header.GetOverlayEntryCount()); + Write32(header.GetStringPoolIndexOffset()); + Write32(header.GetStringPoolLength()); } } // namespace android::idmap2 -- cgit v1.2.3 From d7e8a534d0fbf0b0f8f27f59f90dbbeb0b5641db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Kongstad?= Date: Fri, 11 Oct 2019 08:32:04 +0200 Subject: idmap2: add debug information to idmap file format Add a new variable length string to the idmap file format. This string will hold debug information like fulfilled policies and any warnings triggered while generating the file. Bump the idmap version to 3. Adjust the idmap header definition in ResourceType.h to take the new string into account. Example debug info: $ idmap2 create \ --target-apk-path frameworks/base/cmds/idmap2/tests/data/target/target.apk \ --overlay-apk-path frameworks/base/cmds/idmap2/tests/data/overlay/overlay.apk \ --idmap-path /tmp/a.idmap \ --policy public \ --policy oem $ idmap2 dump --idmap-path /tmp/a.idmap target apk path : frameworks/base/cmds/idmap2/tests/data/target/target.apk overlay apk path : frameworks/base/cmds/idmap2/tests/data/overlay/overlay.apk I fulfilled_policies=oem|public enforce_overlayable=true W failed to find resource "integer/not_in_target" in target resources 0x7f010000 -> 0x7f010000 integer/int1 0x7f02000c -> 0x7f020000 string/str1 [...] $ idmap2 dump --idmap-path /tmp/a.idmap --verbose 00000000: 504d4449 magic 00000004: 00000003 version 00000008: 76a20829 target crc 0000000c: c054fb26 overlay crc 00000010: ........ target path: frameworks/base/cmds/idmap2/tests/data/target/target.apk 00000110: ........ overlay path: frameworks/base/cmds/idmap2/tests/data/overlay/overlay.apk 00000210: ........ debug info: ... 00000294: 7f target package id 00000295: 7f overlay package id [...] Also, tell cpplint to accept non-const references as function parameters: they make more sense as out-parameters than pointers that are assumed to be non-null. Also, switch to regular expressions in the RawPrintVisitorTests: no more manual fixups of the stream offsets! Tell cpplint that the header is OK to use. Bug: 140790707 Test: idmap2_tests Change-Id: Ib94684a3b4001240321801e21af8e132fbcf6609 --- cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp') diff --git a/cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp b/cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp index 3b0940ae06ef..362dcb36007a 100644 --- a/cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp +++ b/cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp @@ -42,13 +42,21 @@ void BinaryStreamVisitor::Write32(uint32_t value) { stream_.write(reinterpret_cast(&x), sizeof(uint32_t)); } -void BinaryStreamVisitor::WriteString(const StringPiece& value) { +void BinaryStreamVisitor::WriteString256(const StringPiece& value) { char buf[kIdmapStringLength]; memset(buf, 0, sizeof(buf)); memcpy(buf, value.data(), std::min(value.size(), sizeof(buf))); stream_.write(buf, sizeof(buf)); } +void BinaryStreamVisitor::WriteString(const std::string& value) { + // pad with null to nearest word boundary; include at least one terminating null + size_t padding_size = 4 - (value.size() % 4); + Write32(value.size() + padding_size); + stream_.write(value.c_str(), value.size()); + stream_.write("\0\0\0\0", padding_size); +} + void BinaryStreamVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) { // nothing to do } @@ -58,8 +66,9 @@ void BinaryStreamVisitor::visit(const IdmapHeader& header) { Write32(header.GetVersion()); Write32(header.GetTargetCrc()); Write32(header.GetOverlayCrc()); - WriteString(header.GetTargetPath()); - WriteString(header.GetOverlayPath()); + WriteString256(header.GetTargetPath()); + WriteString256(header.GetOverlayPath()); + WriteString(header.GetDebugInfo()); } void BinaryStreamVisitor::visit(const IdmapData& data) { -- cgit v1.2.3 From a707013b78cea3586fdadf9a2f04932e823d7504 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Wed, 13 May 2020 14:17:52 -0700 Subject: Add policies and enforce overlayable to header If the fulfilled policies change without the contents of the target and overlay APKs changing, the idmap for the overlay should be regenerated. This change adds fulfilled policies and enforce overlayable to the idmap header so that idmap2d can determine if the polices or enforce overlayable changed from what was used to generate the idmap. Bug: 119328308 Test: idmap2_tests Test: atest RegenerateIdmapTest Change-Id: I96f970e82b5243be01b205ac2cb6ab249c6100bc --- cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp') diff --git a/cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp b/cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp index 362dcb36007a..255212ad4c66 100644 --- a/cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp +++ b/cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp @@ -66,6 +66,8 @@ void BinaryStreamVisitor::visit(const IdmapHeader& header) { Write32(header.GetVersion()); Write32(header.GetTargetCrc()); Write32(header.GetOverlayCrc()); + Write32(header.GetFulfilledPolicies()); + Write8(static_cast(header.GetEnforceOverlayable())); WriteString256(header.GetTargetPath()); WriteString256(header.GetOverlayPath()); WriteString(header.GetDebugInfo()); -- cgit v1.2.3