Remove malloc/free for inline overlay values

Remove malloc/free of android::ResTable_entry for inline overlay
values.

Add `target_entry_inline` to the idmap format to encode inline overlay
values separate from direct mapping of target resource to overlay
resource. This reduces the number of bytes needed to represent a direct
mapping of target resource to overlay resource from 9 bytes to 8 bytes
per entry.

Fixed all idmap alignment issues that required the framework to use
"#pragma pack(push, 1)" when loading idmaps.

Bug: 170341022
Test: idmap2_tests and libandroidfw_tests
Change-Id: Iab4d3902508f02773464724913e0ee966e3689e4
diff --git a/cmds/idmap2/tests/ResourceMappingTests.cpp b/cmds/idmap2/tests/ResourceMappingTests.cpp
index 3ec6ac2..185e929 100644
--- a/cmds/idmap2/tests/ResourceMappingTests.cpp
+++ b/cmds/idmap2/tests/ResourceMappingTests.cpp
@@ -77,30 +77,61 @@
                                 fulfilled_policies, enforce_overlayable);
 }
 
-Result<Unit> MappingExists(const ResourceMapping& mapping, const ResourceId& target_resource,
-                           const uint8_t type, const uint32_t value, bool rewrite) {
+Result<Unit> MappingExists(const ResourceMapping& mapping, ResourceId target_resource,
+                           ResourceId overlay_resource, bool rewrite) {
   auto target_map = mapping.GetTargetToOverlayMap();
   auto entry_map = target_map.find(target_resource);
   if (entry_map == target_map.end()) {
     return Error("Failed to find mapping for target resource");
   }
 
-  if (entry_map->second.data_type != type) {
-    return Error(R"(Expected type: "0x%02x" Actual type: "0x%02x")", type,
-                 entry_map->second.data_type);
+  auto actual_overlay_resource = std::get_if<ResourceId>(&entry_map->second);
+  if (actual_overlay_resource == nullptr) {
+    return Error("Target resource is not mapped to an overlay resource id");
   }
 
-  if (entry_map->second.data_value != value) {
-    return Error(R"(Expected value: "0x%08x" Actual value: "0x%08x")", type,
-                 entry_map->second.data_value);
+  if (*actual_overlay_resource != overlay_resource) {
+    return Error(R"(Expected id: "0x%02x" Actual id: "0x%02x")", overlay_resource,
+                 *actual_overlay_resource);
   }
 
   auto overlay_map = mapping.GetOverlayToTargetMap();
-  auto overlay_iter = overlay_map.find(entry_map->second.data_value);
+  auto overlay_iter = overlay_map.find(overlay_resource);
   if ((overlay_iter != overlay_map.end()) != rewrite) {
     return Error(R"(Expected rewriting: "%s")", rewrite ? "true" : "false");
   }
 
+  if (rewrite && overlay_iter->second != target_resource) {
+    return Error(R"(Expected rewrite id: "0x%02x" Actual id: "0x%02x")", target_resource,
+                 overlay_iter->second);
+  }
+
+  return Result<Unit>({});
+}
+
+Result<Unit> MappingExists(const ResourceMapping& mapping, const ResourceId& target_resource,
+                           const uint8_t type, const uint32_t value) {
+  auto target_map = mapping.GetTargetToOverlayMap();
+  auto entry_map = target_map.find(target_resource);
+  if (entry_map == target_map.end()) {
+    return Error("Failed to find mapping for target resource");
+  }
+
+  auto actual_overlay_value = std::get_if<TargetValue>(&entry_map->second);
+  if (actual_overlay_value == nullptr) {
+    return Error("Target resource is not mapped to an inline value");
+  }
+
+  if (actual_overlay_value->data_type != type) {
+    return Error(R"(Expected type: "0x%02x" Actual type: "0x%02x")", type,
+                 actual_overlay_value->data_type);
+  }
+
+  if (actual_overlay_value->data_value != value) {
+    return Error(R"(Expected value: "0x%08x" Actual value: "0x%08x")", type,
+                 actual_overlay_value->data_value);
+  }
+
   return Result<Unit>({});
 }
 
@@ -116,14 +147,14 @@
   ASSERT_TRUE(resources) << resources.GetErrorMessage();
   auto& res = *resources;
   ASSERT_EQ(res.GetTargetToOverlayMap().size(), 4U);
-  ASSERT_RESULT(MappingExists(res, R::target::integer::int1, Res_value::TYPE_REFERENCE,
-                              R::overlay::integer::int1, false /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::str1, Res_value::TYPE_REFERENCE,
-                              R::overlay::string::str1, false /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::str3, Res_value::TYPE_REFERENCE,
-                              R::overlay::string::str3, false /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::str4, Res_value::TYPE_REFERENCE,
-                              R::overlay::string::str4, false /* rewrite */));
+  ASSERT_RESULT(
+      MappingExists(res, R::target::integer::int1, R::overlay::integer::int1, false /* rewrite */));
+  ASSERT_RESULT(
+      MappingExists(res, R::target::string::str1, R::overlay::string::str1, false /* rewrite */));
+  ASSERT_RESULT(
+      MappingExists(res, R::target::string::str3, R::overlay::string::str3, false /* rewrite */));
+  ASSERT_RESULT(
+      MappingExists(res, R::target::string::str4, R::overlay::string::str4, false /* rewrite */));
 }
 
 TEST(ResourceMappingTests, ResourcesFromApkAssetsNonMatchingNames) {
@@ -138,12 +169,12 @@
   ASSERT_TRUE(resources) << resources.GetErrorMessage();
   auto& res = *resources;
   ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U);
-  ASSERT_RESULT(MappingExists(res, R::target::string::str1, Res_value::TYPE_DYNAMIC_REFERENCE,
-                              R::overlay::string::str4, true /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::str3, Res_value::TYPE_DYNAMIC_REFERENCE,
-                              R::overlay::string::str1, true /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::str4, Res_value::TYPE_DYNAMIC_REFERENCE,
-                              R::overlay::string::str3, true /* rewrite */));
+  ASSERT_RESULT(
+      MappingExists(res, R::target::string::str1, R::overlay::string::str4, true /* rewrite */));
+  ASSERT_RESULT(
+      MappingExists(res, R::target::string::str3, R::overlay::string::str1, true /* rewrite */));
+  ASSERT_RESULT(
+      MappingExists(res, R::target::string::str4, R::overlay::string::str3, true /* rewrite */));
 }
 
 TEST(ResourceMappingTests, DoNotRewriteNonOverlayResourceId) {
@@ -159,10 +190,9 @@
   auto& res = *resources;
   ASSERT_EQ(res.GetTargetToOverlayMap().size(), 2U);
   ASSERT_EQ(res.GetOverlayToTargetMap().size(), 1U);
-  ASSERT_RESULT(MappingExists(res, R::target::string::str1, Res_value::TYPE_REFERENCE, 0x0104000a,
+  ASSERT_RESULT(MappingExists(res, R::target::string::str1, 0x0104000a,
                               false /* rewrite */));  // -> android:string/ok
-  ASSERT_RESULT(MappingExists(res, R::target::string::str3, Res_value::TYPE_DYNAMIC_REFERENCE,
-                              0x7f020001, true /* rewrite */));
+  ASSERT_RESULT(MappingExists(res, R::target::string::str3, 0x7f020001, true /* rewrite */));
 }
 
 TEST(ResourceMappingTests, InlineResources) {
@@ -180,10 +210,8 @@
   ASSERT_EQ(res.GetTargetToOverlayMap().size(), 2U);
   ASSERT_EQ(res.GetOverlayToTargetMap().size(), 0U);
   ASSERT_RESULT(MappingExists(res, R::target::string::str1, Res_value::TYPE_STRING,
-                              overlay_string_pool_size + 0U,
-                              false /* rewrite */));  // -> "Hello World"
-  ASSERT_RESULT(MappingExists(res, R::target::integer::int1, Res_value::TYPE_INT_DEC, 73U,
-                              false /* rewrite */));  // -> 73
+                              overlay_string_pool_size + 0U));  // -> "Hello World"
+  ASSERT_RESULT(MappingExists(res, R::target::integer::int1, Res_value::TYPE_INT_DEC, 73U));
 }
 
 TEST(ResourceMappingTests, CreateIdmapFromApkAssetsPolicySystemPublic) {
@@ -195,13 +223,13 @@
   ASSERT_TRUE(resources) << resources.GetErrorMessage();
   auto& res = *resources;
   ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U);
-  ASSERT_RESULT(MappingExists(res, R::target::string::policy_public, Res_value::TYPE_REFERENCE,
+  ASSERT_RESULT(MappingExists(res, R::target::string::policy_public,
                               R::system_overlay::string::policy_public, false /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::policy_system, Res_value::TYPE_REFERENCE,
+  ASSERT_RESULT(MappingExists(res, R::target::string::policy_system,
                               R::system_overlay::string::policy_system, false /* rewrite */));
-  ASSERT_RESULT(
-      MappingExists(res, R::target::string::policy_system_vendor, Res_value::TYPE_REFERENCE,
-                    R::system_overlay::string::policy_system_vendor, false /* rewrite */));
+  ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor,
+                              R::system_overlay::string::policy_system_vendor,
+                              false /* rewrite */));
 }
 
 // Resources that are not declared as overlayable and resources that a protected by policies the
@@ -215,15 +243,15 @@
   ASSERT_TRUE(resources) << resources.GetErrorMessage();
   auto& res = *resources;
   ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U);
-  ASSERT_RESULT(MappingExists(res, R::target::string::policy_public, Res_value::TYPE_REFERENCE,
+  ASSERT_RESULT(MappingExists(res, R::target::string::policy_public,
                               R::system_overlay_invalid::string::policy_public,
                               false /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::policy_system, Res_value::TYPE_REFERENCE,
+  ASSERT_RESULT(MappingExists(res, R::target::string::policy_system,
                               R::system_overlay_invalid::string::policy_system,
                               false /* rewrite */));
-  ASSERT_RESULT(
-      MappingExists(res, R::target::string::policy_system_vendor, Res_value::TYPE_REFERENCE,
-                    R::system_overlay_invalid::string::policy_system_vendor, false /* rewrite */));
+  ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor,
+                              R::system_overlay_invalid::string::policy_system_vendor,
+                              false /* rewrite */));
 }
 
 // Resources that are not declared as overlayable and resources that a protected by policies the
@@ -238,37 +266,36 @@
   ASSERT_TRUE(resources) << resources.GetErrorMessage();
   auto& res = *resources;
   ASSERT_EQ(res.GetTargetToOverlayMap().size(), 11U);
-  ASSERT_RESULT(MappingExists(res, R::target::string::not_overlayable, Res_value::TYPE_REFERENCE,
+  ASSERT_RESULT(MappingExists(res, R::target::string::not_overlayable,
                               R::system_overlay_invalid::string::not_overlayable,
                               false /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::other, Res_value::TYPE_REFERENCE,
+  ASSERT_RESULT(MappingExists(res, R::target::string::other,
                               R::system_overlay_invalid::string::other, false /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::policy_actor, Res_value::TYPE_REFERENCE,
+  ASSERT_RESULT(MappingExists(res, R::target::string::policy_actor,
                               R::system_overlay_invalid::string::policy_actor,
                               false /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::policy_odm, Res_value::TYPE_REFERENCE,
+  ASSERT_RESULT(MappingExists(res, R::target::string::policy_odm,
                               R::system_overlay_invalid::string::policy_odm, false /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::policy_oem, Res_value::TYPE_REFERENCE,
+  ASSERT_RESULT(MappingExists(res, R::target::string::policy_oem,
                               R::system_overlay_invalid::string::policy_oem, false /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::policy_product, Res_value::TYPE_REFERENCE,
+  ASSERT_RESULT(MappingExists(res, R::target::string::policy_product,
                               R::system_overlay_invalid::string::policy_product,
                               false /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::policy_public, Res_value::TYPE_REFERENCE,
+  ASSERT_RESULT(MappingExists(res, R::target::string::policy_public,
                               R::system_overlay_invalid::string::policy_public,
                               false /* rewrite */));
   ASSERT_RESULT(MappingExists(res, R::target::string::policy_config_signature,
-                              Res_value::TYPE_REFERENCE,
                               R::system_overlay_invalid::string::policy_config_signature,
                               false /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::policy_signature, Res_value::TYPE_REFERENCE,
+  ASSERT_RESULT(MappingExists(res, R::target::string::policy_signature,
                               R::system_overlay_invalid::string::policy_signature,
                               false /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::policy_system, Res_value::TYPE_REFERENCE,
+  ASSERT_RESULT(MappingExists(res, R::target::string::policy_system,
                               R::system_overlay_invalid::string::policy_system,
                               false /* rewrite */));
-  ASSERT_RESULT(
-      MappingExists(res, R::target::string::policy_system_vendor, Res_value::TYPE_REFERENCE,
-                    R::system_overlay_invalid::string::policy_system_vendor, false /* rewrite */));
+  ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor,
+                              R::system_overlay_invalid::string::policy_system_vendor,
+                              false /* rewrite */));
 }
 
 // Overlays that do not target an <overlayable> tag can overlay resources defined within any
@@ -281,14 +308,14 @@
   ASSERT_TRUE(resources) << resources.GetErrorMessage();
   auto& res = *resources;
   ASSERT_EQ(res.GetTargetToOverlayMap().size(), 4U);
-  ASSERT_RESULT(MappingExists(res, R::target::integer::int1, Res_value::TYPE_REFERENCE,
-                              R::overlay::integer::int1, false /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::str1, Res_value::TYPE_REFERENCE,
-                              R::overlay::string::str1, false /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::str3, Res_value::TYPE_REFERENCE,
-                              R::overlay::string::str3, false /* rewrite */));
-  ASSERT_RESULT(MappingExists(res, R::target::string::str4, Res_value::TYPE_REFERENCE,
-                              R::overlay::string::str4, false /* rewrite */));
+  ASSERT_RESULT(
+      MappingExists(res, R::target::integer::int1, R::overlay::integer::int1, false /* rewrite */));
+  ASSERT_RESULT(
+      MappingExists(res, R::target::string::str1, R::overlay::string::str1, false /* rewrite */));
+  ASSERT_RESULT(
+      MappingExists(res, R::target::string::str3, R::overlay::string::str3, false /* rewrite */));
+  ASSERT_RESULT(
+      MappingExists(res, R::target::string::str4, R::overlay::string::str4, false /* rewrite */));
 }
 
 // Overlays that are neither pre-installed nor signed with the same signature as the target cannot
@@ -302,9 +329,9 @@
   ASSERT_EQ(resources->GetTargetToOverlayMap().size(), 0U);
 }
 
-// Overlays that are pre-installed or are signed with the same signature as the target  or are signed
-// with the same signature as the reference package can overlay packages that have not defined
-// overlayable resources.
+// Overlays that are pre-installed or are signed with the same signature as the target  or are
+// signed with the same signature as the reference package can overlay packages that have not
+// defined overlayable resources.
 TEST(ResourceMappingTests, ResourcesFromApkAssetsDefaultPolicies) {
   auto CheckEntries = [&](const PolicyBitmask& fulfilled_policies) -> void {
     auto resources = TestGetResourceMapping("/target/target-no-overlayable.apk",
@@ -315,39 +342,38 @@
     ASSERT_TRUE(resources) << resources.GetErrorMessage();
     auto& res = *resources;
     ASSERT_EQ(resources->GetTargetToOverlayMap().size(), 11U);
-    ASSERT_RESULT(MappingExists(res, R::target::string::not_overlayable, Res_value::TYPE_REFERENCE,
+    ASSERT_RESULT(MappingExists(res, R::target::string::not_overlayable,
                                 R::system_overlay_invalid::string::not_overlayable,
                                 false /* rewrite */));
-    ASSERT_RESULT(MappingExists(res, R::target::string::other, Res_value::TYPE_REFERENCE,
+    ASSERT_RESULT(MappingExists(res, R::target::string::other,
                                 R::system_overlay_invalid::string::other, false /* rewrite */));
-    ASSERT_RESULT(MappingExists(res, R::target::string::policy_actor, Res_value::TYPE_REFERENCE,
+    ASSERT_RESULT(MappingExists(res, R::target::string::policy_actor,
                                 R::system_overlay_invalid::string::policy_actor,
                                 false /* rewrite */));
-    ASSERT_RESULT(MappingExists(res, R::target::string::policy_odm, Res_value::TYPE_REFERENCE,
+    ASSERT_RESULT(MappingExists(res, R::target::string::policy_odm,
                                 R::system_overlay_invalid::string::policy_odm,
                                 false /* rewrite */));
-    ASSERT_RESULT(MappingExists(res, R::target::string::policy_oem, Res_value::TYPE_REFERENCE,
+    ASSERT_RESULT(MappingExists(res, R::target::string::policy_oem,
                                 R::system_overlay_invalid::string::policy_oem,
                                 false /* rewrite */));
-    ASSERT_RESULT(MappingExists(res, R::target::string::policy_product, Res_value::TYPE_REFERENCE,
+    ASSERT_RESULT(MappingExists(res, R::target::string::policy_product,
                                 R::system_overlay_invalid::string::policy_product,
                                 false /* rewrite */));
-    ASSERT_RESULT(MappingExists(res, R::target::string::policy_public, Res_value::TYPE_REFERENCE,
+    ASSERT_RESULT(MappingExists(res, R::target::string::policy_public,
                                 R::system_overlay_invalid::string::policy_public,
                                 false /* rewrite */));
     ASSERT_RESULT(MappingExists(res, R::target::string::policy_config_signature,
-                                Res_value::TYPE_REFERENCE,
                                 R::system_overlay_invalid::string::policy_config_signature,
                                 false /* rewrite */));
-    ASSERT_RESULT(MappingExists(res, R::target::string::policy_signature, Res_value::TYPE_REFERENCE,
+    ASSERT_RESULT(MappingExists(res, R::target::string::policy_signature,
                                 R::system_overlay_invalid::string::policy_signature,
                                 false /* rewrite */));
-    ASSERT_RESULT(MappingExists(res, R::target::string::policy_system, Res_value::TYPE_REFERENCE,
+    ASSERT_RESULT(MappingExists(res, R::target::string::policy_system,
                                 R::system_overlay_invalid::string::policy_system,
                                 false /* rewrite */));
-    ASSERT_RESULT(MappingExists(
-        res, R::target::string::policy_system_vendor, Res_value::TYPE_REFERENCE,
-        R::system_overlay_invalid::string::policy_system_vendor, false /* rewrite */));
+    ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor,
+                                R::system_overlay_invalid::string::policy_system_vendor,
+                                false /* rewrite */));
   };
 
   CheckEntries(PolicyFlags::SIGNATURE);