Use reference counted pointers for ApkAssets
The primary reason for memory corruption is freed ApkAssets
Java expected them to only be freed in the finalizers, but
there are explicit close() calls now, destroying objects that
are still in use in some AssetManager2 objects
This CL makes sure those AssetManagers don't assume ApkAssets
always exist, but instead tries to lock them in memory for any
access
It also adds logging in case of deleting an assets object with
any weak pointers still existing. Those will get into the
bugreports attached to related bugs to help with investigation.
Benchmarks don't regress, and the device appears to be working.
Given that the crashes used to be pretty rare, let's wait for
any new reports or lack of those.
+ add a missing .clang-format file to the jni directory
+ enabled C++23 in the project that uses AssetManager headers
Bug: 197260547
Bug: 276922628
Test: unit tests + boot + benchmarks
Change-Id: I495fd9e012fe370a1f725dbb0265b4ee1be8d805
diff --git a/cmds/idmap2/idmap2/Lookup.cpp b/cmds/idmap2/idmap2/Lookup.cpp
index f41e57c..add0d8d 100644
--- a/cmds/idmap2/idmap2/Lookup.cpp
+++ b/cmds/idmap2/idmap2/Lookup.cpp
@@ -174,7 +174,7 @@
return Error("failed to parse config");
}
- std::vector<std::unique_ptr<const ApkAssets>> apk_assets;
+ std::vector<AssetManager2::ApkAssetsPtr> apk_assets;
std::string target_path;
std::string target_package_name;
for (size_t i = 0; i < idmap_paths.size(); i++) {
@@ -217,24 +217,21 @@
apk_assets.push_back(std::move(overlay_apk));
}
- // AssetManager2::SetApkAssets requires raw ApkAssets pointers, not unique_ptrs
- std::vector<const ApkAssets*> raw_pointer_apk_assets;
- std::transform(apk_assets.cbegin(), apk_assets.cend(), std::back_inserter(raw_pointer_apk_assets),
- [](const auto& p) -> const ApkAssets* { return p.get(); });
- AssetManager2 am;
- am.SetApkAssets(raw_pointer_apk_assets);
- am.SetConfiguration(config);
+ {
+ // Make sure |apk_assets| vector outlives the asset manager as it doesn't own the assets.
+ AssetManager2 am(apk_assets, config);
- const Result<ResourceId> resid = ParseResReference(am, resid_str, target_package_name);
- if (!resid) {
- return Error(resid.GetError(), "failed to parse resource ID");
- }
+ const Result<ResourceId> resid = ParseResReference(am, resid_str, target_package_name);
+ if (!resid) {
+ return Error(resid.GetError(), "failed to parse resource ID");
+ }
- const Result<std::string> value = GetValue(&am, *resid);
- if (!value) {
- return Error(value.GetError(), "resource 0x%08x not found", *resid);
+ const Result<std::string> value = GetValue(&am, *resid);
+ if (!value) {
+ return Error(value.GetError(), "resource 0x%08x not found", *resid);
+ }
+ std::cout << *value << std::endl;
}
- std::cout << *value << std::endl;
return Unit{};
}
diff --git a/cmds/idmap2/libidmap2/ResourceContainer.cpp b/cmds/idmap2/libidmap2/ResourceContainer.cpp
index 0e35904..7869fbd 100644
--- a/cmds/idmap2/libidmap2/ResourceContainer.cpp
+++ b/cmds/idmap2/libidmap2/ResourceContainer.cpp
@@ -262,7 +262,7 @@
}
struct ResState {
- std::unique_ptr<ApkAssets> apk_assets;
+ AssetManager2::ApkAssetsPtr apk_assets;
const LoadedArsc* arsc;
const LoadedPackage* package;
std::unique_ptr<AssetManager2> am;
@@ -284,7 +284,7 @@
}
state.am = std::make_unique<AssetManager2>();
- if (!state.am->SetApkAssets({state.apk_assets.get()})) {
+ if (!state.am->SetApkAssets({state.apk_assets})) {
return Error("failed to create asset manager");
}
diff --git a/cmds/idmap2/tests/ResourceUtilsTests.cpp b/cmds/idmap2/tests/ResourceUtilsTests.cpp
index 6914208..011040b 100644
--- a/cmds/idmap2/tests/ResourceUtilsTests.cpp
+++ b/cmds/idmap2/tests/ResourceUtilsTests.cpp
@@ -38,7 +38,7 @@
apk_assets_ = ApkAssets::Load(GetTargetApkPath());
ASSERT_THAT(apk_assets_, NotNull());
- am_.SetApkAssets({apk_assets_.get()});
+ am_.SetApkAssets({apk_assets_});
}
const AssetManager2& GetAssetManager() {
@@ -47,7 +47,7 @@
private:
AssetManager2 am_;
- std::unique_ptr<const ApkAssets> apk_assets_;
+ AssetManager2::ApkAssetsPtr apk_assets_;
};
TEST_F(ResourceUtilsTests, ResToTypeEntryName) {