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

Commit b894c274 authored by Ryan Mitchell's avatar Ryan Mitchell
Browse files

Revert "Assign shared libraries stable package ids"

This change must be reverted because it broke packages with the same
package name but different package ids loaded at once.

Bug: 146685730
Test: MultiSplitInstallTest
This reverts commit fe50d739.

Change-Id: If6364fd660c76284452f77e7d4f09a3df9dede1d
parent 393c9121
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -214,12 +214,8 @@ static jint CopyValue(JNIEnv* env, ApkAssetsCookie cookie, const Res_value& valu

// ----------------------------------------------------------------------------

static std::unique_ptr<DynamicLibManager> sDynamicLibManager =
    std::make_unique<DynamicLibManager>();

// Let the opaque type AAssetManager refer to a guarded AssetManager2 instance.
struct GuardedAssetManager : public ::AAssetManager {
  GuardedAssetManager() : guarded_assetmanager(sDynamicLibManager.get()) {}
  Guarded<AssetManager2> guarded_assetmanager;
};

+0 −1
Original line number Diff line number Diff line
@@ -44,7 +44,6 @@ cc_library {
        "AttributeResolution.cpp",
        "ChunkIterator.cpp",
        "ConfigDescription.cpp",
        "DynamicLibManager.cpp",
        "Idmap.cpp",
        "LoadedArsc.cpp",
        "Locale.cpp",
+19 −53
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@

#include "android-base/logging.h"
#include "android-base/stringprintf.h"
#include "androidfw/DynamicLibManager.h"
#include "androidfw/ResourceUtils.h"
#include "androidfw/Util.h"
#include "utils/ByteOrder.h"
@@ -67,12 +66,7 @@ struct FindEntryResult {
  StringPoolRef entry_string_ref;
};

AssetManager2::AssetManager2() : dynamic_lib_manager_(std::make_unique<DynamicLibManager>()) {
  memset(&configuration_, 0, sizeof(configuration_));
}

AssetManager2::AssetManager2(DynamicLibManager* dynamic_lib_manager)
    : dynamic_lib_manager_(dynamic_lib_manager) {
AssetManager2::AssetManager2() {
  memset(&configuration_, 0, sizeof(configuration_));
}

@@ -91,45 +85,25 @@ void AssetManager2::BuildDynamicRefTable() {
  package_groups_.clear();
  package_ids_.fill(0xff);

  // Overlay resources are not directly referenced by an application so their resource ids
  // can change throughout the application's lifetime. Assign overlay package ids last.
  std::vector<const ApkAssets*> sorted_apk_assets(apk_assets_);
  std::stable_partition(sorted_apk_assets.begin(), sorted_apk_assets.end(), [](const ApkAssets* a) {
    return !a->IsOverlay();
  });

  // A mapping from apk assets path to the runtime package id of its first loaded package.
  std::unordered_map<std::string, uint8_t> apk_assets_package_ids;
  std::unordered_map<std::string, uint8_t> package_name_package_ids;

  // Assign stable package ids to application packages.
  uint8_t next_available_package_id = 0U;
  for (const auto& apk_assets : sorted_apk_assets) {
    for (const auto& package : apk_assets->GetLoadedArsc()->GetPackages()) {
      uint8_t package_id = package->GetPackageId();
      if (package->IsOverlay()) {
        package_id = GetDynamicLibManager()->FindUnassignedId(next_available_package_id);
        next_available_package_id = package_id + 1;
      } else if (package->IsDynamic()) {
        package_id = GetDynamicLibManager()->GetAssignedId(package->GetPackageName());
      }

      // Map the path of the apk assets to the package id of its first loaded package.
      apk_assets_package_ids[apk_assets->GetPath()] = package_id;
  // 0x01 is reserved for the android package.
  int next_package_id = 0x02;
  const size_t apk_assets_count = apk_assets_.size();
  for (size_t i = 0; i < apk_assets_count; i++) {
    const ApkAssets* apk_assets = apk_assets_[i];
    const LoadedArsc* loaded_arsc = apk_assets->GetLoadedArsc();

      // Map the package name of the package to the first loaded package with that package id.
      package_name_package_ids[package->GetPackageName()] = package_id;
    }
    for (const std::unique_ptr<const LoadedPackage>& package : loaded_arsc->GetPackages()) {
      // Get the package ID or assign one if a shared library.
      int package_id;
      if (package->IsDynamic()) {
        package_id = next_package_id++;
      } else {
        package_id = package->GetPackageId();
      }

  const int apk_assets_count = apk_assets_.size();
  for (int i = 0; i < apk_assets_count; i++) {
    const auto& apk_assets = apk_assets_[i];
    for (const auto& package : apk_assets->GetLoadedArsc()->GetPackages()) {
      const auto package_id_entry = package_name_package_ids.find(package->GetPackageName());
      CHECK(package_id_entry != package_name_package_ids.end())
          << "no package id assgined to package " << package->GetPackageName();
      const uint8_t package_id = package_id_entry->second;

      // Add the mapping for package ID to index if not present.
      uint8_t idx = package_ids_[package_id];
      if (idx == 0xff) {
@@ -152,7 +126,7 @@ void AssetManager2::BuildDynamicRefTable() {

            PackageGroup& target_package_group = package_groups_[target_idx];

            // Create a special dynamic reference table for the overlay to rewrite references to
            // Create a special dynamic reference table for the overlay to rewite references to
            // overlay resources as references to the target resources they overlay.
            auto overlay_table = std::make_shared<OverlayDynamicRefTable>(
                loaded_idmap->GetOverlayDynamicRefTable(target_package_id));
@@ -182,6 +156,8 @@ void AssetManager2::BuildDynamicRefTable() {
        package_group->dynamic_ref_table->mEntries.replaceValueFor(
            package_name, static_cast<uint8_t>(entry.package_id));
      }

      apk_assets_package_ids.insert(std::make_pair(apk_assets->GetPath(), package_id));
    }
  }

@@ -1329,16 +1305,6 @@ uint8_t AssetManager2::GetAssignedPackageId(const LoadedPackage* package) const
  return 0;
}

DynamicLibManager* AssetManager2::GetDynamicLibManager() const {
  auto dynamic_lib_manager =
      std::get_if<std::unique_ptr<DynamicLibManager>>(&dynamic_lib_manager_);
  if (dynamic_lib_manager) {
    return (*dynamic_lib_manager).get();
  } else {
    return *std::get_if<DynamicLibManager*>(&dynamic_lib_manager_);
  }
}

std::unique_ptr<Theme> AssetManager2::NewTheme() {
  return std::unique_ptr<Theme>(new Theme(this));
}
+0 −34
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "androidfw/DynamicLibManager.h"

namespace android {

uint8_t DynamicLibManager::GetAssignedId(const std::string& library_package_name) {
  auto lib_entry = shared_lib_package_ids_.find(library_package_name);
  if (lib_entry != shared_lib_package_ids_.end()) {
    return lib_entry->second;
  }

  return shared_lib_package_ids_[library_package_name] = next_package_id_++;
}

uint8_t DynamicLibManager::FindUnassignedId(uint8_t start_package_id) {
  return (start_package_id < next_package_id_) ? next_package_id_ : start_package_id;
}

} // namespace android
+0 −7
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@
#include "androidfw/ApkAssets.h"
#include "androidfw/Asset.h"
#include "androidfw/AssetManager.h"
#include "androidfw/DynamicLibManager.h"
#include "androidfw/ResourceTypes.h"
#include "androidfw/Util.h"

@@ -95,7 +94,6 @@ class AssetManager2 {
  };

  AssetManager2();
  explicit AssetManager2(DynamicLibManager* dynamic_lib_manager);

  // Sets/resets the underlying ApkAssets for this AssetManager. The ApkAssets
  // are not owned by the AssetManager, and must have a longer lifetime.
@@ -373,8 +371,6 @@ class AssetManager2 {
  // Retrieve the assigned package id of the package if loaded into this AssetManager
  uint8_t GetAssignedPackageId(const LoadedPackage* package) const;

  DynamicLibManager* GetDynamicLibManager() const;

  // The ordered list of ApkAssets to search. These are not owned by the AssetManager, and must
  // have a longer lifetime.
  std::vector<const ApkAssets*> apk_assets_;
@@ -393,9 +389,6 @@ class AssetManager2 {
  // may need to be purged.
  ResTable_config configuration_;

  // Component responsible for assigning package ids to shared libraries.
  std::variant<std::unique_ptr<DynamicLibManager>, DynamicLibManager*> dynamic_lib_manager_;

  // Cached set of bags. These are cached because they can inherit keys from parent bags,
  // which involves some calculation.
  std::unordered_map<uint32_t, util::unique_cptr<ResolvedBag>> cached_bags_;
Loading