From aa4d5fdf471f8052124eec0421943052d27881e1 Mon Sep 17 00:00:00 2001 From: small_leek Date: Fri, 4 Jul 2025 17:00:34 +0800 Subject: [PATCH] check haps when create startup cache Signed-off-by: small_leek --- .../bundlemgr/include/base_bundle_installer.h | 2 + services/bundlemgr/include/bundle_data_mgr.h | 1 + .../bundlemgr/include/inner_bundle_info.h | 1 + .../bundlemgr/src/base_bundle_installer.cpp | 49 ++++++++++++------- services/bundlemgr/src/bundle_data_mgr.cpp | 17 +++++++ services/bundlemgr/src/inner_bundle_info.cpp | 5 ++ .../bms_bundle_installer_permission_test.cpp | 22 ++++++++- .../bms_bundle_installer_test.cpp | 37 ++++++++++++++ 8 files changed, 116 insertions(+), 18 deletions(-) diff --git a/services/bundlemgr/include/base_bundle_installer.h b/services/bundlemgr/include/base_bundle_installer.h index 7cc0bd38f3..49945509f7 100644 --- a/services/bundlemgr/include/base_bundle_installer.h +++ b/services/bundlemgr/include/base_bundle_installer.h @@ -747,6 +747,8 @@ private: ErrCode DeleteEl1ShaderCache(const InnerBundleInfo &oldInfo, const std::string &bundleName, int32_t userId) const; ErrCode DeleteBundleClonesShaderCache(const std::vector allAppIndexes, const std::string &bundleName, int32_t userId) const; + ErrCode ProcessArkStartupCache(const ArkStartupCache &createArk, + int32_t moduleNum, int32_t userId) const; ErrCode CreateArkStartupCache(const ArkStartupCache &createArk) const; ErrCode CleanArkStartupCache(const std::string &cacheDir, const std::string &bundleName, int32_t userId) const; ErrCode DeleteArkStartupCache(const std::string &cacheDir, const std::string &bundleName, int32_t userId) const; diff --git a/services/bundlemgr/include/bundle_data_mgr.h b/services/bundlemgr/include/bundle_data_mgr.h index f06c2d5c74..77c2d7a027 100644 --- a/services/bundlemgr/include/bundle_data_mgr.h +++ b/services/bundlemgr/include/bundle_data_mgr.h @@ -1166,6 +1166,7 @@ public: std::vector>> &cloneInfos) const; void FilterShortcutJson(nlohmann::json &jsonResult); ErrCode IsSystemApp(const std::string &bundleName, bool &isSystemApp); + int32_t GetInnerBundleInfoModuleSize(const std::string &bundleName); void UpdateDesktopShortcutInfo(const std::string &bundleName); private: diff --git a/services/bundlemgr/include/inner_bundle_info.h b/services/bundlemgr/include/inner_bundle_info.h index 0db0162832..ef87fc6ef0 100644 --- a/services/bundlemgr/include/inner_bundle_info.h +++ b/services/bundlemgr/include/inner_bundle_info.h @@ -2351,6 +2351,7 @@ public: void GetAllDynamicIconInfo(const int32_t userId, std::vector &dynamicIconInfos) const; std::string GetApplicationCodeLanguage() const; void UpdateHasCloudkitConfig(); + int32_t GetModuleSize() const; private: bool IsExistLauncherAbility() const; diff --git a/services/bundlemgr/src/base_bundle_installer.cpp b/services/bundlemgr/src/base_bundle_installer.cpp index ccb410203b..69be165946 100644 --- a/services/bundlemgr/src/base_bundle_installer.cpp +++ b/services/bundlemgr/src/base_bundle_installer.cpp @@ -1454,11 +1454,11 @@ ErrCode BaseBundleInstaller::ProcessBundleInstall(const std::vector BundleAgingMgr::AgingTriggertype::FREE_INSTALL); } #endif + InnerBundleInfo cacheInfo; + tempInfo_.GetTempBundleInfo(cacheInfo); #ifdef BUNDLE_FRAMEWORK_QUICK_FIX if (needDeleteQuickFixInfo_) { LOG_D(BMS_TAG_INSTALLER, "module update, quick fix old patch need to delete:%{public}s", bundleName_.c_str()); - InnerBundleInfo cacheInfo; - tempInfo_.GetTempBundleInfo(cacheInfo); if (!oldInfo.GetAppQuickFix().deployedAppqfInfo.hqfInfos.empty()) { LOG_D(BMS_TAG_INSTALLER, "quickFixInfo need disable, bundleName:%{public}s", bundleName_.c_str()); auto quickFixSwitcher = std::make_unique(bundleName_, false); @@ -1469,6 +1469,26 @@ ErrCode BaseBundleInstaller::ProcessBundleInstall(const std::vector tempInfo_.SetTempBundleInfo(cacheInfo); } #endif + // process ark startup cache + std::string el1ArkStartupCachePath = ServiceConstants::SYSTEM_OPTIMIZE_PATH + + bundleName_ + ServiceConstants::ARK_STARTUP_CACHE_DIR; + el1ArkStartupCachePath = el1ArkStartupCachePath.replace(el1ArkStartupCachePath.find("%"), 1, + std::to_string(userId_)); + ArkStartupCache ceateArk; + ceateArk.bundleName = bundleName_; + ceateArk.bundleType = oldInfo.GetApplicationBundleType(); + ceateArk.cacheDir = el1ArkStartupCachePath; + ceateArk.mode = ServiceConstants::SYSTEM_OPTIMIZE_MODE; + InnerBundleUserInfo newInnerBundleUserInfo; + if (!cacheInfo.GetInnerBundleUserInfo(userId_, newInnerBundleUserInfo)) { + LOG_E(BMS_TAG_INSTALLER, "bundle(%{public}s) get user(%{public}d) failed", + bundleName_.c_str(), userId_); + return ERR_APPEXECFWK_USER_NOT_INSTALL_HAP; + } + ceateArk.uid = newInnerBundleUserInfo.uid; + ceateArk.gid = newInnerBundleUserInfo.uid; + ProcessArkStartupCache(ceateArk, cacheInfo.GetModuleSize(), userId_); + DeleteUninstallBundleInfo(bundleName_); UpdateEncryptedStatus(oldInfo); GetInstallEventInfo(oldInfo, sysEventInfo_); @@ -3247,20 +3267,6 @@ ErrCode BaseBundleInstaller::CreateBundleDataDir(InnerBundleInfo &info) const if (userId_ == Constants::START_USERID) { CreateCloudShader(info.GetBundleName(), createDirParam.uid, createDirParam.gid); } - - // create ark startup cache - std::string el1ArkStartupCachePath = ServiceConstants::SYSTEM_OPTIMIZE_PATH + - info.GetBundleName() + ServiceConstants::ARK_STARTUP_CACHE_DIR; - el1ArkStartupCachePath = el1ArkStartupCachePath.replace(el1ArkStartupCachePath.find("%"), 1, - std::to_string(userId_)); - ArkStartupCache ceateArk; - ceateArk.bundleName = info.GetBundleName(); - ceateArk.bundleType = info.GetApplicationBundleType(); - ceateArk.cacheDir = el1ArkStartupCachePath; - ceateArk.mode = ServiceConstants::SYSTEM_OPTIMIZE_MODE; - ceateArk.uid = createDirParam.uid; - ceateArk.gid = createDirParam.gid; - CreateArkStartupCache(ceateArk); // create asan log directory when asanEnabled is true // In update condition, delete asan log directory when asanEnabled is false if directory is exist @@ -6696,6 +6702,15 @@ ErrCode BaseBundleInstaller::DeleteEl1ShaderCache(const InnerBundleInfo &oldInfo return DeleteBundleClonesShaderCache(allAppIndexes, bundleName, userId); } +ErrCode BaseBundleInstaller::ProcessArkStartupCache(const ArkStartupCache &createArk, + int32_t moduleNum, int32_t userId) const +{ + if (moduleNum != 1) { + return DeleteArkStartupCache(ServiceConstants::SYSTEM_OPTIMIZE_PATH, createArk.bundleName, userId); + } + return CreateArkStartupCache(createArk); +} + ErrCode BaseBundleInstaller::CreateArkStartupCache(const ArkStartupCache &createArk) const { if (createArk.bundleType != BundleType::APP && createArk.bundleType != BundleType::ATOMIC_SERVICE) { @@ -6734,7 +6749,7 @@ ErrCode BaseBundleInstaller::CleanArkStartupCache(const std::string &cacheDir, ErrCode BaseBundleInstaller::DeleteArkStartupCache(const std::string &cacheDir, const std::string &bundleName, int32_t userId) const { - std::string el1ArkStartupCachePath = cacheDir + bundleName; + std::string el1ArkStartupCachePath = cacheDir + bundleName + ServiceConstants::ARK_STARTUP_CACHE_DIR; el1ArkStartupCachePath = el1ArkStartupCachePath.replace(el1ArkStartupCachePath.find("%"), 1, std::to_string(userId)); return InstalldClient::GetInstance()->RemoveDir(el1ArkStartupCachePath); diff --git a/services/bundlemgr/src/bundle_data_mgr.cpp b/services/bundlemgr/src/bundle_data_mgr.cpp index 253059a029..fc223f1bec 100644 --- a/services/bundlemgr/src/bundle_data_mgr.cpp +++ b/services/bundlemgr/src/bundle_data_mgr.cpp @@ -11165,6 +11165,23 @@ void BundleDataMgr::FilterShortcutJson(nlohmann::json &jsonResult) } } +int32_t BundleDataMgr::GetInnerBundleInfoModuleSize( + const std::string &bundleName) +{ + APP_LOGD("FetchInnerBundleInfo %{public}s", bundleName.c_str()); + if (bundleName.empty()) { + APP_LOGW("bundleName is empty"); + return 0; + } + std::shared_lock lock(bundleInfoMutex_); + auto infoItem = bundleInfos_.find(bundleName); + if (infoItem == bundleInfos_.end()) { + APP_LOGW_NOFUNC("FetchInnerBundleInfo not found %{public}s", bundleName.c_str()); + return 0; + } + return infoItem->second.GetModuleSize(); +} + void BundleDataMgr::UpdateDesktopShortcutInfo(const std::string &bundleName) { APP_LOGD("UpdateDesktopShortcutInfo begin"); diff --git a/services/bundlemgr/src/inner_bundle_info.cpp b/services/bundlemgr/src/inner_bundle_info.cpp index 689cdbcd39..d9ccbf4283 100644 --- a/services/bundlemgr/src/inner_bundle_info.cpp +++ b/services/bundlemgr/src/inner_bundle_info.cpp @@ -2895,6 +2895,11 @@ void InnerBundleInfo::GetModuleNames(std::vector &moduleNames) cons } } +int32_t InnerBundleInfo::GetModuleSize() const +{ + return innerModuleInfos_.size(); +} + void InnerBundleInfo::ResetBundleState(int32_t userId) { if (userId == Constants::ALL_USERID) { diff --git a/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_permission_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_permission_test.cpp index 31385fb2f3..86b03eed8b 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_permission_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_permission_test.cpp @@ -1162,7 +1162,7 @@ HWTEST_F(BmsBundleInstallerPermissionTest, CreateArkStartupCache_0030, Function ArkStartupCache ceateArk; ceateArk.bundleName = "com.test"; ceateArk.bundleType = BundleType::APP; - ceateArk.cacheDir = ServiceConstants::SYSTEM_OPTIMIZE_PATH; + ceateArk.cacheDir = ""; ceateArk.mode = ServiceConstants::SYSTEM_OPTIMIZE_MODE; ceateArk.uid = 0; ceateArk.gid = 0; @@ -1185,4 +1185,24 @@ HWTEST_F(BmsBundleInstallerPermissionTest, SetArkStartupCacheApl_0100, Function auto ret = impl.SetArkStartupCacheApl(""); EXPECT_EQ(ret, ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED); } + +/** + * @tc.number: ProcessArkStartupCache_0010 + * @tc.name: test ProcessArkStartupCache + * @tc.desc: 1.Test the ProcessArkStartupCache of BaseBundleInstaller +*/ +HWTEST_F(BmsBundleInstallerPermissionTest, ProcessArkStartupCache_0010, Function | SmallTest | Level0) +{ + // test no FOUNDATION_UID + ArkStartupCache ceateArk; + ceateArk.bundleName = "com.test"; + ceateArk.bundleType = BundleType::APP; + ceateArk.cacheDir = ""; + ceateArk.mode = ServiceConstants::SYSTEM_OPTIMIZE_MODE; + ceateArk.uid = 0; + ceateArk.gid = 0; + BaseBundleInstaller installer; + ErrCode ret = installer.ProcessArkStartupCache(cacheDir, 1, 100); + EXPECT_EQ(ret, ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED); +} } // OHOS diff --git a/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_test.cpp index 2beef9104c..4d45652993 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_test.cpp @@ -12195,4 +12195,41 @@ HWTEST_F(BmsBundleInstallerTest, CleanAllBundleCache_0010, Function | SmallTest auto ret = bundleCacheMgr.CleanAllBundleCache(cleanCache); EXPECT_EQ(ret, ERR_OK); } + +/** + * @tc.number: ProcessArkStartupCache_0010 + * @tc.name: test ProcessArkStartupCache + * @tc.desc: 1.Test the ProcessArkStartupCache of BaseBundleInstaller +*/ +HWTEST_F(BmsBundleInstallerTest, ProcessArkStartupCache_0010, Function | SmallTest | Level0) +{ + std::string testBudnleName = "com.example.ProcessArkStartupCache"; + std::string el1ArkStartupCachePath = ServiceConstants::SYSTEM_OPTIMIZE_PATH + + testBudnleName + ServiceConstants::ARK_STARTUP_CACHE_DIR; + el1ArkStartupCachePath = el1ArkStartupCachePath.replace(el1ArkStartupCachePath.find("%"), 1, + std::to_string(100)); + ArkStartupCache ceateArk; + ceateArk.bundleName = testBudnleName; + ceateArk.bundleType = BundleType::APP; + ceateArk.cacheDir = testBudnleName; + ceateArk.mode = ServiceConstants::SYSTEM_OPTIMIZE_MODE; + ceateArk.uid = 0; + ceateArk.gid = 0; + WriteToConfigFile(testBudnleName); + + // test bundlename in white list + BaseBundleInstaller installer3; + ErrCode ret = installer3.ProcessArkStartupCache(ceateArk, 1, 100); + EXPECT_EQ(ret, ERR_OK); + + ret = installer3.CleanArkStartupCache(ServiceConstants::SYSTEM_OPTIMIZE_PATH, testBudnleName, 100); + EXPECT_EQ(ret, ERR_OK); + + ret = installer3.DeleteArkStartupCache(ServiceConstants::SYSTEM_OPTIMIZE_PATH, testBudnleName, 101); + EXPECT_EQ(ret, ERR_OK); + + ret = installer3.ProcessArkStartupCache(ceateArk, 0, 100); + EXPECT_EQ(ret, ERR_OK); + setuid(Constants::ROOT_UID); +} } // OHOS -- Gitee