Revert "libandroidfw hardening for IncFs"

Revert "Move map_ptr to incfs namspace"

Revert submission 12787270

Reason for revert: b/173250495
Reverted Changes:
I5cd1bc8a2:libandroidfw hardening for IncFs
Ice5dbcfb2:Move map_ptr to incfs namspace
I29ccdc8ed:Do not cache bag parent stack until requested
I1e9e9acaa:Cache resolved theme values

Change-Id: Ib90ef68339710086df41e9abe0833a542d03a74f
diff --git a/cmds/idmap2/idmap2/Lookup.cpp b/cmds/idmap2/idmap2/Lookup.cpp
index 8323d0b..c441709 100644
--- a/cmds/idmap2/idmap2/Lookup.cpp
+++ b/cmds/idmap2/idmap2/Lookup.cpp
@@ -45,8 +45,11 @@
 using android::ApkAssetsCookie;
 using android::AssetManager2;
 using android::ConfigDescription;
+using android::is_valid_resid;
+using android::kInvalidCookie;
 using android::Res_value;
 using android::ResStringPool;
+using android::ResTable_config;
 using android::StringPiece16;
 using android::base::StringPrintf;
 using android::idmap2::CommandLineOptions;
@@ -56,6 +59,7 @@
 using android::idmap2::Result;
 using android::idmap2::Unit;
 using android::idmap2::utils::ExtractOverlayManifestInfo;
+using android::util::Utf16ToUtf8;
 
 namespace {
 
@@ -65,23 +69,25 @@
 
   // first, try to parse as a hex number
   char* endptr = nullptr;
-  const ResourceId parsed_resid = strtol(res.c_str(), &endptr, kBaseHex);
+  ResourceId resid;
+  resid = strtol(res.c_str(), &endptr, kBaseHex);
   if (*endptr == '\0') {
-    return parsed_resid;
+    return resid;
   }
 
   // next, try to parse as a package:type/name string
-  if (auto resid = am.GetResourceId(res, "", fallback_package)) {
-    return *resid;
+  resid = am.GetResourceId(res, "", fallback_package);
+  if (is_valid_resid(resid)) {
+    return resid;
   }
 
   // end of the road: res could not be parsed
   return Error("failed to obtain resource id for %s", res.c_str());
 }
 
-void PrintValue(AssetManager2* const am, const AssetManager2::SelectedValue& value,
+void PrintValue(AssetManager2* const am, const Res_value& value, const ApkAssetsCookie& cookie,
                 std::string* const out) {
-  switch (value.type) {
+  switch (value.dataType) {
     case Res_value::TYPE_INT_DEC:
       out->append(StringPrintf("%d", value.data));
       break;
@@ -92,21 +98,30 @@
       out->append(value.data != 0 ? "true" : "false");
       break;
     case Res_value::TYPE_STRING: {
-      const ResStringPool* pool = am->GetStringPoolForCookie(value.cookie);
+      const ResStringPool* pool = am->GetStringPoolForCookie(cookie);
       out->append("\"");
-      if (auto str = pool->string8ObjectAt(value.data)) {
-        out->append(*str);
+      size_t len;
+      if (pool->isUTF8()) {
+        const char* str = pool->string8At(value.data, &len);
+        out->append(str, len);
+      } else {
+        const char16_t* str16 = pool->stringAt(value.data, &len);
+        out->append(Utf16ToUtf8(StringPiece16(str16, len)));
       }
+      out->append("\"");
     } break;
     default:
-      out->append(StringPrintf("dataType=0x%02x data=0x%08x", value.type, value.data));
+      out->append(StringPrintf("dataType=0x%02x data=0x%08x", value.dataType, value.data));
       break;
   }
 }
 
 Result<std::string> WARN_UNUSED GetValue(AssetManager2* const am, ResourceId resid) {
-  auto value = am->GetResource(resid);
-  if (!value.has_value()) {
+  Res_value value;
+  ResTable_config config;
+  uint32_t flags;
+  ApkAssetsCookie cookie = am->GetResource(resid, true, 0, &value, &config, &flags);
+  if (cookie == kInvalidCookie) {
     return Error("no resource 0x%08x in asset manager", resid);
   }
 
@@ -114,35 +129,41 @@
 
   // TODO(martenkongstad): use optional parameter GetResource(..., std::string*
   // stacktrace = NULL) instead
-  out.append(StringPrintf("cookie=%d ", value->cookie));
+  out.append(StringPrintf("cookie=%d ", cookie));
 
   out.append("config='");
-  out.append(value->config.toString().c_str());
+  out.append(config.toString().c_str());
   out.append("' value=");
 
-  if (value->type == Res_value::TYPE_REFERENCE) {
-    auto bag_result = am->GetBag(static_cast<uint32_t>(value->data));
-    if (!bag_result.has_value()) {
-      out.append(StringPrintf("dataType=0x%02x data=0x%08x", value->type, value->data));
+  if (value.dataType == Res_value::TYPE_REFERENCE) {
+    const android::ResolvedBag* bag = am->GetBag(static_cast<uint32_t>(value.data));
+    if (bag == nullptr) {
+      out.append(StringPrintf("dataType=0x%02x data=0x%08x", value.dataType, value.data));
       return out;
     }
-
     out.append("[");
-    const android::ResolvedBag* bag = bag_result.value();
+    Res_value bag_val;
+    ResTable_config selected_config;
+    uint32_t flags;
+    uint32_t ref;
+    ApkAssetsCookie bag_cookie;
     for (size_t i = 0; i < bag->entry_count; ++i) {
-      AssetManager2::SelectedValue entry(bag, bag->entries[i]);
-      if (am->ResolveReference(entry).has_value()) {
-        out.append(StringPrintf("Error: dataType=0x%02x data=0x%08x", entry.type, entry.data));
+      const android::ResolvedBag::Entry& entry = bag->entries[i];
+      bag_val = entry.value;
+      bag_cookie = am->ResolveReference(entry.cookie, &bag_val, &selected_config, &flags, &ref);
+      if (bag_cookie == kInvalidCookie) {
+        out.append(
+            StringPrintf("Error: dataType=0x%02x data=0x%08x", bag_val.dataType, bag_val.data));
         continue;
       }
-      PrintValue(am, entry, &out);
+      PrintValue(am, bag_val, bag_cookie, &out);
       if (i != bag->entry_count - 1) {
         out.append(", ");
       }
     }
     out.append("]");
   } else {
-    PrintValue(am, *value, &out);
+    PrintValue(am, value, cookie, &out);
   }
 
   return out;
diff --git a/cmds/idmap2/idmap2d/Idmap2Service.h b/cmds/idmap2/idmap2d/Idmap2Service.h
index 0127e87..1d2090c 100644
--- a/cmds/idmap2/idmap2d/Idmap2Service.h
+++ b/cmds/idmap2/idmap2d/Idmap2Service.h
@@ -17,6 +17,8 @@
 #ifndef IDMAP2_IDMAP2D_IDMAP2SERVICE_H_
 #define IDMAP2_IDMAP2D_IDMAP2SERVICE_H_
 
+#include <string>
+
 #include <android-base/unique_fd.h>
 #include <binder/BinderService.h>
 
diff --git a/cmds/idmap2/include/idmap2/ResourceMapping.h b/cmds/idmap2/include/idmap2/ResourceMapping.h
index f66916c..0a58ec4 100644
--- a/cmds/idmap2/include/idmap2/ResourceMapping.h
+++ b/cmds/idmap2/include/idmap2/ResourceMapping.h
@@ -117,8 +117,7 @@
   static Result<ResourceMapping> CreateResourceMappingLegacy(const AssetManager2* target_am,
                                                              const AssetManager2* overlay_am,
                                                              const LoadedPackage* target_package,
-                                                             const LoadedPackage* overlay_package,
-                                                             LogInfo& log_info);
+                                                             const LoadedPackage* overlay_package);
 
   // Removes resources that do not pass policy or overlayable checks of the target package.
   void FilterOverlayableResources(const AssetManager2* target_am,
diff --git a/cmds/idmap2/libidmap2/PrettyPrintVisitor.cpp b/cmds/idmap2/libidmap2/PrettyPrintVisitor.cpp
index 3037a79..a93202a 100644
--- a/cmds/idmap2/libidmap2/PrettyPrintVisitor.cpp
+++ b/cmds/idmap2/libidmap2/PrettyPrintVisitor.cpp
@@ -100,9 +100,10 @@
     stream_ << TAB << base::StringPrintf("0x%08x -> ", target_entry.target_id)
             << utils::DataTypeToString(target_entry.value.data_type);
 
+    size_t unused;
     if (target_entry.value.data_type == Res_value::TYPE_STRING) {
-      auto str = string_pool.stringAt(target_entry.value.data_value - string_pool_offset);
-      stream_ << " \"" << str.value_or(StringPiece16(u"")) << "\"";
+      auto str = string_pool.stringAt(target_entry.value.data_value - string_pool_offset, &unused);
+      stream_ << " \"" << StringPiece16(str) << "\"";
     } else {
       stream_ << " " << base::StringPrintf("0x%08x", target_entry.value.data_value);
     }
diff --git a/cmds/idmap2/libidmap2/ResourceMapping.cpp b/cmds/idmap2/libidmap2/ResourceMapping.cpp
index d777cbf..31f1c16 100644
--- a/cmds/idmap2/libidmap2/ResourceMapping.cpp
+++ b/cmds/idmap2/libidmap2/ResourceMapping.cpp
@@ -71,10 +71,9 @@
   if (!target_package.DefinesOverlayable()) {
     return (sDefaultPolicies & fulfilled_policies) != 0
                ? Result<Unit>({})
-               : Error(
-                     "overlay must be preinstalled, signed with the same signature as the target,"
-                     " or signed with the same signature as the package referenced through"
-                     " <overlay-config-signature>.");
+               : Error("overlay must be preinstalled, signed with the same signature as the target,"
+                       " or signed with the same signature as the package referenced through"
+                       " <overlay-config-signature>.");
   }
 
   const OverlayableInfo* overlayable_info = target_package.GetOverlayableInfo(target_resource);
@@ -120,25 +119,32 @@
 
 Result<std::unique_ptr<Asset>> OpenNonAssetFromResource(const ResourceId& resource_id,
                                                         const AssetManager2& asset_manager) {
-  auto value = asset_manager.GetResource(resource_id);
-  if (!value.has_value()) {
+  Res_value value{};
+  ResTable_config selected_config{};
+  uint32_t flags;
+  auto cookie =
+      asset_manager.GetResource(resource_id, /* may_be_bag */ false,
+                                /* density_override */ 0U, &value, &selected_config, &flags);
+  if (cookie == kInvalidCookie) {
     return Error("failed to find resource for id 0x%08x", resource_id);
   }
 
-  if (value->type != Res_value::TYPE_STRING) {
+  if (value.dataType != Res_value::TYPE_STRING) {
     return Error("resource for is 0x%08x is not a file", resource_id);
   }
 
-  auto string_pool = asset_manager.GetStringPoolForCookie(value->cookie);
-  auto file = string_pool->string8ObjectAt(value->data);
-  if (!file.has_value()) {
-    return Error("failed to find string for index %d", value->data);
+  auto string_pool = asset_manager.GetStringPoolForCookie(cookie);
+  size_t len;
+  auto file_path16 = string_pool->stringAt(value.data, &len);
+  if (file_path16 == nullptr) {
+    return Error("failed to find string for index %d", value.data);
   }
 
   // Load the overlay resource mappings from the file specified using android:resourcesMap.
-  auto asset = asset_manager.OpenNonAsset(file->c_str(), Asset::AccessMode::ACCESS_BUFFER);
+  auto file_path = String8(String16(file_path16));
+  auto asset = asset_manager.OpenNonAsset(file_path.c_str(), Asset::AccessMode::ACCESS_BUFFER);
   if (asset == nullptr) {
-    return Error("file \"%s\" not found", file->c_str());
+    return Error("file \"%s\" not found", file_path.c_str());
   }
 
   return asset;
@@ -184,16 +190,16 @@
       return Error(R"(<item> tag missing expected attribute "value")");
     }
 
-    auto target_id_result =
+    ResourceId target_id =
         target_am->GetResourceId(*target_resource, "", target_package->GetPackageName());
-    if (!target_id_result.has_value()) {
+    if (target_id == 0U) {
       log_info.Warning(LogMessage() << "failed to find resource \"" << *target_resource
                                     << "\" in target resources");
       continue;
     }
 
     // Retrieve the compile-time resource id of the target resource.
-    uint32_t target_id = REWRITE_PACKAGE(*target_id_result, target_package_id);
+    target_id = REWRITE_PACKAGE(target_id, target_package_id);
 
     if (overlay_resource->dataType == Res_value::TYPE_STRING) {
       overlay_resource->data += string_pool_offset;
@@ -214,7 +220,7 @@
 
 Result<ResourceMapping> ResourceMapping::CreateResourceMappingLegacy(
     const AssetManager2* target_am, const AssetManager2* overlay_am,
-    const LoadedPackage* target_package, const LoadedPackage* overlay_package, LogInfo& log_info) {
+    const LoadedPackage* target_package, const LoadedPackage* overlay_package) {
   ResourceMapping resource_mapping;
   const uint8_t target_package_id = target_package->GetPackageId();
   const auto end = overlay_package->end();
@@ -228,15 +234,13 @@
     // Find the resource with the same type and entry name within the target package.
     const std::string full_name =
         base::StringPrintf("%s:%s", target_package->GetPackageName().c_str(), name->c_str());
-    auto target_resource_result = target_am->GetResourceId(full_name);
-    if (!target_resource_result.has_value()) {
-      log_info.Warning(LogMessage() << "failed to find resource \"" << full_name
-                                    << "\" in target resources");
+    ResourceId target_resource = target_am->GetResourceId(full_name);
+    if (target_resource == 0U) {
       continue;
     }
 
     // Retrieve the compile-time resource id of the target resource.
-    ResourceId target_resource = REWRITE_PACKAGE(*target_resource_result, target_package_id);
+    target_resource = REWRITE_PACKAGE(target_resource, target_package_id);
     resource_mapping.AddMapping(target_resource, overlay_resid,
                                 false /* rewrite_overlay_reference */);
   }
@@ -343,9 +347,7 @@
     auto& string_pool = (*parser)->get_strings();
     string_pool_data_length = string_pool.bytes();
     string_pool_data.reset(new uint8_t[string_pool_data_length]);
-
-    // Overlays should not be incrementally installed, so calling unsafe_ptr is fine here.
-    memcpy(string_pool_data.get(), string_pool.data().unsafe_ptr(), string_pool_data_length);
+    memcpy(string_pool_data.get(), string_pool.data(), string_pool_data_length);
 
     // Offset string indices by the size of the overlay resource table string pool.
     string_pool_offset = overlay_arsc->GetStringPool()->size();
@@ -356,7 +358,7 @@
     // If no file is specified using android:resourcesMap, it is assumed that the overlay only
     // defines resources intended to override target resources of the same type and name.
     resource_mapping = CreateResourceMappingLegacy(&target_asset_manager, &overlay_asset_manager,
-                                                   target_pkg, overlay_pkg, log_info);
+                                                   target_pkg, overlay_pkg);
   }
 
   if (!resource_mapping) {
diff --git a/cmds/idmap2/libidmap2/ResourceUtils.cpp b/cmds/idmap2/libidmap2/ResourceUtils.cpp
index e817140..98d026b 100644
--- a/cmds/idmap2/libidmap2/ResourceUtils.cpp
+++ b/cmds/idmap2/libidmap2/ResourceUtils.cpp
@@ -72,21 +72,21 @@
 }
 
 Result<std::string> ResToTypeEntryName(const AssetManager2& am, uint32_t resid) {
-  const auto name = am.GetResourceName(resid);
-  if (!name.has_value()) {
+  AssetManager2::ResourceName name;
+  if (!am.GetResourceName(resid, &name)) {
     return Error("no resource 0x%08x in asset manager", resid);
   }
   std::string out;
-  if (name->type != nullptr) {
-    out.append(name->type, name->type_len);
+  if (name.type != nullptr) {
+    out.append(name.type, name.type_len);
   } else {
-    out += Utf16ToUtf8(StringPiece16(name->type16, name->type_len));
+    out += Utf16ToUtf8(StringPiece16(name.type16, name.type_len));
   }
   out.append("/");
-  if (name->entry != nullptr) {
-    out.append(name->entry, name->entry_len);
+  if (name.entry != nullptr) {
+    out.append(name.entry, name.entry_len);
   } else {
-    out += Utf16ToUtf8(StringPiece16(name->entry16, name->entry_len));
+    out += Utf16ToUtf8(StringPiece16(name.entry16, name.entry_len));
   }
   return out;
 }
diff --git a/cmds/idmap2/libidmap2/XmlParser.cpp b/cmds/idmap2/libidmap2/XmlParser.cpp
index 7c55b64..526a560 100644
--- a/cmds/idmap2/libidmap2/XmlParser.cpp
+++ b/cmds/idmap2/libidmap2/XmlParser.cpp
@@ -98,19 +98,18 @@
 
   switch ((*value).dataType) {
     case Res_value::TYPE_STRING: {
-      if (auto str = parser_.getStrings().string8ObjectAt((*value).data)) {
-        return std::string(str->string());
-      }
-      break;
+      size_t len;
+      const String16 value16(parser_.getStrings().stringAt((*value).data, &len));
+      return std::string(String8(value16).c_str());
     }
     case Res_value::TYPE_INT_DEC:
     case Res_value::TYPE_INT_HEX:
     case Res_value::TYPE_INT_BOOLEAN: {
       return std::to_string((*value).data);
     }
+    default:
+      return Error(R"(Failed to convert attribute "%s" value to a string)", name.c_str());
   }
-
-  return Error(R"(Failed to convert attribute "%s" value to a string)", name.c_str());
 }
 
 Result<Res_value> XmlParser::Node::GetAttributeValue(const std::string& name) const {