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 <regex>
header is OK to use.

Bug: 140790707
Test: idmap2_tests
Change-Id: Ib94684a3b4001240321801e21af8e132fbcf6609
diff --git a/cmds/idmap2/tests/IdmapTests.cpp b/cmds/idmap2/tests/IdmapTests.cpp
index cd816dd..4bc6255 100644
--- a/cmds/idmap2/tests/IdmapTests.cpp
+++ b/cmds/idmap2/tests/IdmapTests.cpp
@@ -30,6 +30,7 @@
 #include "idmap2/BinaryStreamVisitor.h"
 #include "idmap2/CommandLineOptions.h"
 #include "idmap2/Idmap.h"
+#include "idmap2/LogInfo.h"
 
 using android::Res_value;
 using ::testing::IsNull;
@@ -57,11 +58,12 @@
   std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(stream);
   ASSERT_THAT(header, NotNull());
   ASSERT_EQ(header->GetMagic(), 0x504d4449U);
-  ASSERT_EQ(header->GetVersion(), 0x02U);
+  ASSERT_EQ(header->GetVersion(), 0x03U);
   ASSERT_EQ(header->GetTargetCrc(), 0x1234U);
   ASSERT_EQ(header->GetOverlayCrc(), 0x5678U);
   ASSERT_EQ(header->GetTargetPath().to_string(), "targetX.apk");
   ASSERT_EQ(header->GetOverlayPath().to_string(), "overlayX.apk");
+  ASSERT_EQ(header->GetDebugInfo(), "debug");
 }
 
 TEST(IdmapTests, FailToCreateIdmapHeaderFromBinaryStreamIfPathTooLong) {
@@ -76,7 +78,7 @@
 }
 
 TEST(IdmapTests, CreateIdmapDataHeaderFromBinaryStream) {
-  const size_t offset = 0x210;
+  const size_t offset = 0x21c;
   std::string raw(reinterpret_cast<const char*>(idmap_raw_data + offset),
                   idmap_raw_data_len - offset);
   std::istringstream stream(raw);
@@ -88,7 +90,7 @@
 }
 
 TEST(IdmapTests, CreateIdmapDataFromBinaryStream) {
-  const size_t offset = 0x210;
+  const size_t offset = 0x21c;
   std::string raw(reinterpret_cast<const char*>(idmap_raw_data + offset),
                   idmap_raw_data_len - offset);
   std::istringstream stream(raw);
@@ -122,7 +124,7 @@
 
   ASSERT_THAT(idmap->GetHeader(), NotNull());
   ASSERT_EQ(idmap->GetHeader()->GetMagic(), 0x504d4449U);
-  ASSERT_EQ(idmap->GetHeader()->GetVersion(), 0x02U);
+  ASSERT_EQ(idmap->GetHeader()->GetVersion(), 0x03U);
   ASSERT_EQ(idmap->GetHeader()->GetTargetCrc(), 0x1234U);
   ASSERT_EQ(idmap->GetHeader()->GetOverlayCrc(), 0x5678U);
   ASSERT_EQ(idmap->GetHeader()->GetTargetPath().to_string(), "targetX.apk");
@@ -174,7 +176,7 @@
 
   ASSERT_THAT(idmap->GetHeader(), NotNull());
   ASSERT_EQ(idmap->GetHeader()->GetMagic(), 0x504d4449U);
-  ASSERT_EQ(idmap->GetHeader()->GetVersion(), 0x02U);
+  ASSERT_EQ(idmap->GetHeader()->GetVersion(), 0x03U);
   ASSERT_EQ(idmap->GetHeader()->GetTargetCrc(), 0x76a20829);
   ASSERT_EQ(idmap->GetHeader()->GetOverlayCrc(), 0xc054fb26);
   ASSERT_EQ(idmap->GetHeader()->GetTargetPath().to_string(), target_apk_path);
@@ -197,8 +199,9 @@
     return Error(R"(Failed to load overlay apk "%s")", overlay_apk_path.data());
   }
 
+  LogInfo log_info;
   auto mapping = ResourceMapping::FromApkAssets(*target_apk, *overlay_apk, overlay_info,
-                                                fulfilled_policies, enforce_overlayable);
+                                                fulfilled_policies, enforce_overlayable, log_info);
 
   if (!mapping) {
     return mapping.GetError();
diff --git a/cmds/idmap2/tests/RawPrintVisitorTests.cpp b/cmds/idmap2/tests/RawPrintVisitorTests.cpp
index d387880..b22fdaf 100644
--- a/cmds/idmap2/tests/RawPrintVisitorTests.cpp
+++ b/cmds/idmap2/tests/RawPrintVisitorTests.cpp
@@ -16,6 +16,7 @@
 
 #include <cstdio>  // fclose
 #include <memory>
+#include <regex>
 #include <sstream>
 #include <string>
 
@@ -29,7 +30,16 @@
 
 namespace android::idmap2 {
 
+#define ASSERT_CONTAINS_REGEX(pattern, str)                       \
+  do {                                                            \
+    ASSERT_TRUE(std::regex_search(str, std::regex(pattern)))      \
+        << "pattern '" << pattern << "' not found in\n--------\n" \
+        << str << "--------";                                     \
+  } while (0)
+
 TEST(RawPrintVisitorTests, CreateRawPrintVisitor) {
+  fclose(stderr);  // silence expected warnings
+
   const std::string target_apk_path(GetTestDataPath() + "/target/target.apk");
   std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
   ASSERT_THAT(target_apk, NotNull());
@@ -46,22 +56,24 @@
   RawPrintVisitor visitor(stream);
   (*idmap)->accept(&visitor);
 
-  ASSERT_NE(stream.str().find("00000000: 504d4449  magic\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000004: 00000002  version\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000008: 76a20829  target crc\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("0000000c: c054fb26  overlay crc\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000210:       7f  target package id\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000211:       7f  overlay package id\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000212: 00000004  target entry count\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000216: 00000004  overlay entry count\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("0000021a: 00000008  string pool index offset\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("0000021e: 000000b4  string pool byte length\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000222: 7f010000  target id: integer/int1\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000226:       07  type: reference (dynamic)\n"),
-            std::string::npos);
-  ASSERT_NE(stream.str().find("00000227: 7f010000  value: integer/int1\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000246: 7f010000  overlay id: integer/int1\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("0000024a: 7f010000  target id: integer/int1\n"), std::string::npos);
+#define ADDRESS "[0-9a-f]{8}: "
+  ASSERT_CONTAINS_REGEX(ADDRESS "504d4449  magic\n", stream.str());
+  ASSERT_CONTAINS_REGEX(ADDRESS "00000003  version\n", stream.str());
+  ASSERT_CONTAINS_REGEX(ADDRESS "76a20829  target crc\n", stream.str());
+  ASSERT_CONTAINS_REGEX(ADDRESS "c054fb26  overlay crc\n", stream.str());
+  ASSERT_CONTAINS_REGEX(ADDRESS "      7f  target package id\n", stream.str());
+  ASSERT_CONTAINS_REGEX(ADDRESS "      7f  overlay package id\n", stream.str());
+  ASSERT_CONTAINS_REGEX(ADDRESS "00000004  target entry count\n", stream.str());
+  ASSERT_CONTAINS_REGEX(ADDRESS "00000004  overlay entry count\n", stream.str());
+  ASSERT_CONTAINS_REGEX(ADDRESS "00000004  overlay entry count\n", stream.str());
+  ASSERT_CONTAINS_REGEX(ADDRESS "00000008  string pool index offset\n", stream.str());
+  ASSERT_CONTAINS_REGEX(ADDRESS "000000b4  string pool byte length\n", stream.str());
+  ASSERT_CONTAINS_REGEX(ADDRESS "7f010000  target id: integer/int1\n", stream.str());
+  ASSERT_CONTAINS_REGEX(ADDRESS "      07  type: reference \\(dynamic\\)\n", stream.str());
+  ASSERT_CONTAINS_REGEX(ADDRESS "7f010000  value: integer/int1\n", stream.str());
+  ASSERT_CONTAINS_REGEX(ADDRESS "7f010000  overlay id: integer/int1\n", stream.str());
+  ASSERT_CONTAINS_REGEX(ADDRESS "7f010000  target id: integer/int1\n", stream.str());
+#undef ADDRESS
 }
 
 TEST(RawPrintVisitorTests, CreateRawPrintVisitorWithoutAccessToApks) {
@@ -78,21 +90,21 @@
   (*idmap)->accept(&visitor);
 
   ASSERT_NE(stream.str().find("00000000: 504d4449  magic\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000004: 00000002  version\n"), std::string::npos);
+  ASSERT_NE(stream.str().find("00000004: 00000003  version\n"), std::string::npos);
   ASSERT_NE(stream.str().find("00000008: 00001234  target crc\n"), std::string::npos);
   ASSERT_NE(stream.str().find("0000000c: 00005678  overlay crc\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000210:       7f  target package id\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000211:       7f  overlay package id\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000212: 00000003  target entry count\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000216: 00000003  overlay entry count\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("0000021a: 00000000  string pool index offset\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("0000021e: 00000000  string pool byte length\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000222: 7f020000  target id\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000226:       01  type: reference\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000227: 7f020000  value\n"), std::string::npos);
+  ASSERT_NE(stream.str().find("0000021c:       7f  target package id\n"), std::string::npos);
+  ASSERT_NE(stream.str().find("0000021d:       7f  overlay package id\n"), std::string::npos);
+  ASSERT_NE(stream.str().find("0000021e: 00000003  target entry count\n"), std::string::npos);
+  ASSERT_NE(stream.str().find("00000222: 00000003  overlay entry count\n"), std::string::npos);
+  ASSERT_NE(stream.str().find("00000226: 00000000  string pool index offset\n"), std::string::npos);
+  ASSERT_NE(stream.str().find("0000022a: 00000000  string pool byte length\n"), std::string::npos);
+  ASSERT_NE(stream.str().find("0000022e: 7f020000  target id\n"), std::string::npos);
+  ASSERT_NE(stream.str().find("00000232:       01  type: reference\n"), std::string::npos);
+  ASSERT_NE(stream.str().find("00000233: 7f020000  value\n"), std::string::npos);
 
-  ASSERT_NE(stream.str().find("0000023d: 7f020000  overlay id\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("00000241: 7f020000  target id\n"), std::string::npos);
+  ASSERT_NE(stream.str().find("00000249: 7f020000  overlay id\n"), std::string::npos);
+  ASSERT_NE(stream.str().find("0000024d: 7f020000  target id\n"), std::string::npos);
 }
 
 }  // namespace android::idmap2
diff --git a/cmds/idmap2/tests/ResourceMappingTests.cpp b/cmds/idmap2/tests/ResourceMappingTests.cpp
index 64304f6..39c4937 100644
--- a/cmds/idmap2/tests/ResourceMappingTests.cpp
+++ b/cmds/idmap2/tests/ResourceMappingTests.cpp
@@ -25,6 +25,7 @@
 #include "TestHelpers.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
+#include "idmap2/LogInfo.h"
 #include "idmap2/ResourceMapping.h"
 
 using android::Res_value;
@@ -55,8 +56,9 @@
     return Error(R"(Failed to load overlay apk "%s")", overlay_apk_path.data());
   }
 
+  LogInfo log_info;
   return ResourceMapping::FromApkAssets(*target_apk, *overlay_apk, overlay_info, fulfilled_policies,
-                                        enforce_overlayable);
+                                        enforce_overlayable, log_info);
 }
 
 Result<ResourceMapping> TestGetResourceMapping(const android::StringPiece& local_target_apk_path,
diff --git a/cmds/idmap2/tests/TestHelpers.h b/cmds/idmap2/tests/TestHelpers.h
index 8868b53..e899589 100644
--- a/cmds/idmap2/tests/TestHelpers.h
+++ b/cmds/idmap2/tests/TestHelpers.h
@@ -30,7 +30,7 @@
     0x49, 0x44, 0x4d, 0x50,
 
     // 0x4: version
-    0x02, 0x00, 0x00, 0x00,
+    0x03, 0x00, 0x00, 0x00,
 
     // 0x8: target crc
     0x34, 0x12, 0x00, 0x00,
@@ -74,64 +74,71 @@
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
+    // 0x210: debug string
+    // string length, including terminating null
+    0x08, 0x00, 0x00, 0x00,
+
+    // string contents "debug\0\0\0" (padded to word alignment)
+    0x64, 0x65, 0x62, 0x75, 0x67, 0x00, 0x00, 0x00,
+
     // DATA HEADER
-    // 0x210: target_package_id
+    // 0x21c: target_package_id
     0x7f,
 
-    // 0x211: overlay_package_id
+    // 0x21d: overlay_package_id
     0x7f,
 
-    // 0x212: target_entry_count
+    // 0x21e: target_entry_count
     0x03, 0x00, 0x00, 0x00,
 
-    // 0x216: overlay_entry_count
+    // 0x222: overlay_entry_count
     0x03, 0x00, 0x00, 0x00,
 
-    // 0x21a: string_pool_offset
+    // 0x226: string_pool_offset
     0x00, 0x00, 0x00, 0x00,
 
-    // 0x21e: string_pool_byte_length
+    // 0x22a: string_pool_byte_length
     0x00, 0x00, 0x00, 0x00,
 
     // TARGET ENTRIES
-    // 0x222: 0x7f020000
+    // 0x22e: 0x7f020000
     0x00, 0x00, 0x02, 0x7f,
 
-    // 0x226: TYPE_REFERENCE
+    // 0x232: TYPE_REFERENCE
     0x01,
 
-    // 0x227: 0x7f020000
+    // 0x233: 0x7f020000
     0x00, 0x00, 0x02, 0x7f,
 
-    // 0x22b: 0x7f030000
+    // 0x237: 0x7f030000
     0x00, 0x00, 0x03, 0x7f,
 
-    // 0x22f: TYPE_REFERENCE
+    // 0x23b: TYPE_REFERENCE
     0x01,
 
-    // 0x230: 0x7f030000
+    // 0x23c: 0x7f030000
     0x00, 0x00, 0x03, 0x7f,
 
-    // 0x234: 0x7f030002
+    // 0x240: 0x7f030002
     0x02, 0x00, 0x03, 0x7f,
 
-    // 0x238: TYPE_REFERENCE
+    // 0x244: TYPE_REFERENCE
     0x01,
 
-    // 0x239: 0x7f030001
+    // 0x245: 0x7f030001
     0x01, 0x00, 0x03, 0x7f,
 
     // OVERLAY ENTRIES
-    // 0x23d: 0x7f020000 -> 0x7f020000
+    // 0x249: 0x7f020000 -> 0x7f020000
     0x00, 0x00, 0x02, 0x7f, 0x00, 0x00, 0x02, 0x7f,
 
-    // 0x245: 0x7f030000 -> 0x7f030000
+    // 0x251: 0x7f030000 -> 0x7f030000
     0x00, 0x00, 0x03, 0x7f, 0x00, 0x00, 0x03, 0x7f,
 
-    // 0x24d: 0x7f030001 -> 0x7f030002
+    // 0x259: 0x7f030001 -> 0x7f030002
     0x01, 0x00, 0x03, 0x7f, 0x02, 0x00, 0x03, 0x7f};
 
-const unsigned int idmap_raw_data_len = 0x255;
+const unsigned int idmap_raw_data_len = 0x261;
 
 std::string GetTestDataPath();