Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2ed8bfa7 authored by Ryan Mitchell's avatar Ryan Mitchell
Browse files

Add fabricated RRO generation to libidmap2

Fabricated Runtime Resource Overlays are overlays that are generated
at runtime and are stored in the data/ partition.

The system can fabricate RROs at runtime to dynamically theme the
device. Idmaps can now be created from APK RROs and fabricated RROs.

Rather than operating on ApkAssets, libidmap2 now operates on abstract
resource "containers" that supply resource values. Target resource
containers implement methods needed to query overlayable and target
overlay information. Currently only APKs can be loaded as target
resource containers. Overlay resource containers implement methods to
supply the mapping of target resource to overlay value and other
overlay information.

The format of a fabricated RRO is as follows:
0x00 - 0x04 : fabricated overlay magic (always FRRO)
0x04 - 0x08 : file format version
0x08 - 0x0c : crc of version + proto data
0x0c - EOF  : proto fabricated overlay data

The magic is used to quickly detect if the file is a fabricated overlay.
The version is incremented whenever backwards incompatible changes are
made to the proto file format. Idmap must always be able to upgrade
fabricated overlays from previous versions to new versions, so all
previous versions must be checked into the tree.

Bug: 172471315
Test: libidmap2_tests && libandroidfw_tests
Change-Id: I4c9f29da278672e5695fb57d131a44c11a835180
parent 5c8ba793
Loading
Loading
Loading
Loading
+46 −12
Original line number Diff line number Diff line
@@ -51,13 +51,18 @@ cc_library {
            static: {
                enabled: false,
            },
            static_libs: [
                "libidmap2_protos",
            ],
            shared_libs: [
                "libandroidfw",
                "libbase",
                "libcutils",
                "libidmap2_policies",
                "libprotobuf-cpp-lite",
                "libutils",
                "libz",
                "libziparchive",
                "libidmap2_policies",
            ],
        },
        host: {
@@ -68,14 +73,29 @@ cc_library {
                "libandroidfw",
                "libbase",
                "libcutils",
                "libidmap2_policies",
                "libidmap2_protos",
                "libprotobuf-cpp-lite",
                "libutils",
                "libz",
                "libziparchive",
                "libidmap2_policies",
            ],
        },
    },
}

cc_library {
    name: "libidmap2_protos",
    srcs: [
        "libidmap2/proto/*.proto",
    ],
    host_supported: true,
    proto: {
        type: "lite",
        export_proto_headers: true,
    },
}

cc_library {
    name: "libidmap2_policies",
    defaults: [
@@ -88,9 +108,6 @@ cc_library {
            enabled: true,
        },
        android: {
            static: {
                enabled: false,
            },
            shared_libs: [
                "libandroidfw",
            ],
@@ -119,6 +136,7 @@ cc_test {
    srcs: [
        "tests/BinaryStreamVisitorTests.cpp",
        "tests/CommandLineOptionsTests.cpp",
        "tests/FabricatedOverlayTests.cpp",
        "tests/FileUtilsTests.cpp",
        "tests/Idmap2BinaryTests.cpp",
        "tests/IdmapTests.cpp",
@@ -130,20 +148,27 @@ cc_test {
        "tests/ResourceUtilsTests.cpp",
        "tests/ResultTests.cpp",
        "tests/XmlParserTests.cpp",
        "tests/ZipFileTests.cpp",
    ],
    static_libs: ["libgmock"],
    required: [
        "idmap2",
    ],
    static_libs: [
        "libgmock",
        "libidmap2_protos",
    ],
    target: {
        android: {
            shared_libs: [
                "libandroidfw",
                "libbase",
                "libidmap2",
                "libidmap2_policies",
                "liblog",
                "libprotobuf-cpp-lite",
                "libutils",
                "libz",
                "libz",
                "libziparchive",
                "libidmap2_policies",
            ],
        },
        host: {
@@ -152,10 +177,11 @@ cc_test {
                "libbase",
                "libcutils",
                "libidmap2",
                "libidmap2_policies",
                "liblog",
                "libprotobuf-cpp-lite",
                "libutils",
                "libziparchive",
                "libidmap2_policies",
            ],
            shared_libs: [
                "libz",
@@ -189,6 +215,9 @@ cc_binary {
        "idmap2/Lookup.cpp",
        "idmap2/Main.cpp",
    ],
    static_libs: [
        "libidmap2_protos",
    ],
    target: {
        android: {
            shared_libs: [
@@ -196,9 +225,11 @@ cc_binary {
                "libbase",
                "libcutils",
                "libidmap2",
                "libidmap2_policies",
                "libprotobuf-cpp-lite",
                "libutils",
                "libz",
                "libziparchive",
                "libidmap2_policies",
            ],
        },
        host: {
@@ -207,10 +238,11 @@ cc_binary {
                "libbase",
                "libcutils",
                "libidmap2",
                "libidmap2_policies",
                "liblog",
                "libprotobuf-cpp-lite",
                "libutils",
                "libziparchive",
                "libidmap2_policies",
            ],
            shared_libs: [
                "libz",
@@ -236,11 +268,13 @@ cc_binary {
        "libbinder",
        "libcutils",
        "libidmap2",
        "libidmap2_policies",
        "libprotobuf-cpp-lite",
        "libutils",
        "libziparchive",
        "libidmap2_policies",
    ],
    static_libs: [
        "libidmap2_protos",
        "libidmap2daidl",
    ],
    init_rc: ["idmap2d/idmap2d.rc"],
+14 −3
Original line number Diff line number Diff line
@@ -25,7 +25,9 @@

using android::idmap2::Error;
using android::idmap2::IdmapHeader;
using android::idmap2::OverlayResourceContainer;
using android::idmap2::Result;
using android::idmap2::TargetResourceContainer;
using android::idmap2::Unit;

Result<Unit> Verify(const std::string& idmap_path, const std::string& target_path,
@@ -39,11 +41,20 @@ Result<Unit> Verify(const std::string& idmap_path, const std::string& target_pat
    return Error("failed to parse idmap header");
  }

  const auto header_ok = header->IsUpToDate(target_path, overlay_path, overlay_name,
                                            fulfilled_policies, enforce_overlayable);
  auto target = TargetResourceContainer::FromPath(target_path);
  if (!target) {
    return Error("failed to load target '%s'", target_path.c_str());
  }

  auto overlay = OverlayResourceContainer::FromPath(overlay_path);
  if (!overlay) {
    return Error("failed to load overlay '%s'", overlay_path.c_str());
  }

  const auto header_ok = header->IsUpToDate(**target, **overlay, overlay_name, fulfilled_policies,
                                            enforce_overlayable);
  if (!header_ok) {
    return Error(header_ok.GetError(), "idmap not up to date");
  }

  return Unit{};
}
+13 −12
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@
#include <fstream>
#include <memory>
#include <ostream>
#include <string>
#include <vector>

#include "androidfw/ResourceTypes.h"
@@ -31,12 +30,13 @@
#include "idmap2/PolicyUtils.h"
#include "idmap2/SysTrace.h"

using android::ApkAssets;
using android::idmap2::BinaryStreamVisitor;
using android::idmap2::CommandLineOptions;
using android::idmap2::Error;
using android::idmap2::Idmap;
using android::idmap2::OverlayResourceContainer;
using android::idmap2::Result;
using android::idmap2::TargetResourceContainer;
using android::idmap2::Unit;
using android::idmap2::utils::kIdmapFilePermissionMask;
using android::idmap2::utils::PoliciesToBitmaskResult;
@@ -93,18 +93,18 @@ Result<Unit> Create(const std::vector<std::string>& args) {
    fulfilled_policies |= PolicyFlags::PUBLIC;
  }

  const std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
  if (!target_apk) {
    return Error("failed to load apk %s", target_apk_path.c_str());
  const auto target = TargetResourceContainer::FromPath(target_apk_path);
  if (!target) {
    return Error("failed to load target '%s'", target_apk_path.c_str());
  }

  const std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
  if (!overlay_apk) {
    return Error("failed to load apk %s", overlay_apk_path.c_str());
  const auto overlay = OverlayResourceContainer::FromPath(overlay_apk_path);
  if (!overlay) {
    return Error("failed to load apk overlay '%s'", overlay_apk_path.c_str());
  }

  const auto idmap = Idmap::FromApkAssets(*target_apk, *overlay_apk, overlay_name,
                                          fulfilled_policies, !ignore_overlayable);
  const auto idmap = Idmap::FromContainers(**target, **overlay, overlay_name, fulfilled_policies,
                                           !ignore_overlayable);
  if (!idmap) {
    return Error(idmap.GetError(), "failed to create idmap");
  }
@@ -112,13 +112,14 @@ Result<Unit> Create(const std::vector<std::string>& args) {
  umask(kIdmapFilePermissionMask);
  std::ofstream fout(idmap_path);
  if (fout.fail()) {
    return Error("failed to open idmap path %s", idmap_path.c_str());
    return Error("failed to open idmap path '%s'", idmap_path.c_str());
  }

  BinaryStreamVisitor visitor(fout);
  (*idmap)->accept(&visitor);
  fout.close();
  if (fout.fail()) {
    return Error("failed to write to idmap path %s", idmap_path.c_str());
    return Error("failed to write to idmap path '%s'", idmap_path.c_str());
  }

  return Unit{};
+9 −8
Original line number Diff line number Diff line
@@ -34,13 +34,14 @@
#include "idmap2/PolicyUtils.h"
#include "idmap2/SysTrace.h"

using android::ApkAssets;
using android::base::StringPrintf;
using android::idmap2::BinaryStreamVisitor;
using android::idmap2::CommandLineOptions;
using android::idmap2::Error;
using android::idmap2::Idmap;
using android::idmap2::OverlayResourceContainer;
using android::idmap2::Result;
using android::idmap2::TargetResourceContainer;
using android::idmap2::Unit;
using android::idmap2::utils::kIdmapCacheDir;
using android::idmap2::utils::kIdmapFilePermissionMask;
@@ -91,9 +92,9 @@ Result<Unit> CreateMultiple(const std::vector<std::string>& args) {
    fulfilled_policies |= PolicyFlags::PUBLIC;
  }

  const std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
  if (!target_apk) {
    return Error("failed to load apk %s", target_apk_path.c_str());
  const auto target = TargetResourceContainer::FromPath(target_apk_path);
  if (!target) {
    return Error("failed to load target '%s'", target_apk_path.c_str());
  }

  std::vector<std::string> idmap_paths;
@@ -108,14 +109,14 @@ Result<Unit> CreateMultiple(const std::vector<std::string>& args) {
    // TODO(b/175014391): Support multiple overlay tags in OverlayConfig
    if (!Verify(idmap_path, target_apk_path, overlay_apk_path, "", fulfilled_policies,
                !ignore_overlayable)) {
      const std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
      if (!overlay_apk) {
      const auto overlay = OverlayResourceContainer::FromPath(overlay_apk_path);
      if (!overlay) {
        LOG(WARNING) << "failed to load apk " << overlay_apk_path.c_str();
        continue;
      }

      const auto idmap = Idmap::FromApkAssets(*target_apk, *overlay_apk, "", fulfilled_policies,
                                              !ignore_overlayable);
      const auto idmap =
          Idmap::FromContainers(**target, **overlay, "", fulfilled_policies, !ignore_overlayable);
      if (!idmap) {
        LOG(WARNING) << "failed to create idmap";
        continue;
+9 −5
Original line number Diff line number Diff line
@@ -37,7 +37,6 @@
#include "idmap2/Result.h"
#include "idmap2/SysTrace.h"
#include "idmap2/XmlParser.h"
#include "idmap2/ZipFile.h"
#include "utils/String16.h"
#include "utils/String8.h"

@@ -52,10 +51,10 @@ using android::base::StringPrintf;
using android::idmap2::CommandLineOptions;
using android::idmap2::Error;
using android::idmap2::IdmapHeader;
using android::idmap2::OverlayResourceContainer;
using android::idmap2::ResourceId;
using android::idmap2::Result;
using android::idmap2::Unit;
using android::idmap2::utils::ExtractOverlayManifestInfo;

namespace {

@@ -195,12 +194,17 @@ Result<Unit> Lookup(const std::vector<std::string>& args) {
      }
      apk_assets.push_back(std::move(target_apk));

      auto manifest_info = ExtractOverlayManifestInfo(idmap_header->GetOverlayPath(),
                                                      idmap_header->GetOverlayName());
      auto overlay = OverlayResourceContainer::FromPath(idmap_header->GetOverlayPath());
      if (!overlay) {
        return overlay.GetError();
      }

      auto manifest_info = (*overlay)->FindOverlayInfo(idmap_header->GetOverlayName());
      if (!manifest_info) {
        return manifest_info.GetError();
      }
      target_package_name = manifest_info->target_package;

      target_package_name = (*manifest_info).target_package;
    } else if (target_path != idmap_header->GetTargetPath()) {
      return Error("different target APKs (expected target APK %s but %s has target APK %s)",
                   target_path.c_str(), idmap_path.c_str(), idmap_header->GetTargetPath().c_str());
Loading