From 7fb6eaf125a3f75866e565f64a62ca913db8eb53 Mon Sep 17 00:00:00 2001 From: zhongjianfei Date: Wed, 23 Mar 2022 16:52:52 +0800 Subject: [PATCH] fixed 4f3f271 from https://gitee.com/zhongjianfei/aafwk_standard/pulls/1348 fix crash when get sa object Signed-off-by: zhongjianfei Change-Id: Ic5b7ea1b324282c44940dbcdcdf10a3eb7c82fc6 --- .../kits/ability/native/src/ability.cpp | 41 +++++++++++-------- .../native/app/include/sys_mgr_client.h | 4 +- .../appkit/native/app/src/sys_mgr_client.cpp | 25 +++++++---- services/abilitymgr/include/sa_mgr_client.h | 4 +- services/abilitymgr/src/sa_mgr_client.cpp | 23 +++++++---- .../appmgr/include/remote_client_manager.h | 3 +- services/appmgr/src/remote_client_manager.cpp | 26 +++++++++--- services/formmgr/include/form_bms_helper.h | 3 +- services/formmgr/src/form_bms_helper.cpp | 41 +++++++++++-------- 9 files changed, 105 insertions(+), 65 deletions(-) diff --git a/frameworks/kits/ability/native/src/ability.cpp b/frameworks/kits/ability/native/src/ability.cpp index 0597162fb60..51f1a782e5e 100644 --- a/frameworks/kits/ability/native/src/ability.cpp +++ b/frameworks/kits/ability/native/src/ability.cpp @@ -71,6 +71,9 @@ namespace AppExecFwk { using PermissionKit = OHOS::Security::Permission::PermissionKit; using PermissionState = OHOS::Security::Permission::PermissionState; +static OHOS::sptr bundleMgr_ = nullptr; +static std::mutex bundleMgrMutex_; + // REGISTER_AA(Ability) const std::string Ability::SYSTEM_UI("com.ohos.systemui"); const std::string Ability::STATUS_BAR("com.ohos.systemui.statusbar.MainAbility"); @@ -3089,24 +3092,27 @@ std::string Ability::GetErrorMsg(const ErrCode errorCode) */ sptr Ability::GetBundleMgr() { - HILOG_INFO("%{public}s called.", __func__); - if (iBundleMgr_ == nullptr) { - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (remoteObject == nullptr) { - HILOG_ERROR("%{public}s error, failed to get bundle manager service.", __func__); - return nullptr; - } - - iBundleMgr_ = iface_cast(remoteObject); - if (iBundleMgr_ == nullptr) { - HILOG_ERROR("%{public}s error, failed to get bundle manager service", __func__); - return nullptr; + if (bundleMgr_ == nullptr) { + std::lock_guard lock(bundleMgrMutex_); + if (bundleMgr_ == nullptr) { + auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + HILOG_ERROR("GetBundleMgr GetSystemAbilityManager is null"); + return nullptr; + } + auto bundleMgrSa = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (bundleMgrSa == nullptr) { + HILOG_ERROR("GetBundleMgr GetSystemAbility is null"); + return nullptr; + } + auto bundleMgr = OHOS::iface_cast(bundleMgrSa); + if (bundleMgr == nullptr) { + HILOG_ERROR("GetBundleMgr iface_cast get null"); + } + bundleMgr_ = bundleMgr; } } - - return iBundleMgr_; + return bundleMgr_; } /** @@ -3116,8 +3122,7 @@ sptr Ability::GetBundleMgr() void Ability::SetBundleManager(const sptr &bundleManager) { HILOG_INFO("%{public}s called.", __func__); - - iBundleMgr_ = bundleManager; + bundleMgr_ = bundleManager; } #ifdef SUPPORT_GRAPHICS diff --git a/frameworks/kits/appkit/native/app/include/sys_mgr_client.h b/frameworks/kits/appkit/native/app/include/sys_mgr_client.h index e5b7be41f9c..2f8103d516f 100644 --- a/frameworks/kits/appkit/native/app/include/sys_mgr_client.h +++ b/frameworks/kits/appkit/native/app/include/sys_mgr_client.h @@ -56,9 +56,9 @@ public: void UnregisterSystemAbility(const int32_t systemAbilityId); private: - OHOS::sptr abilityManager_; - std::mutex saMutex_; std::unordered_map> servicesMap_; + static sptr remoteObject_; + static std::mutex remoteObjectMutex_; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/kits/appkit/native/app/src/sys_mgr_client.cpp b/frameworks/kits/appkit/native/app/src/sys_mgr_client.cpp index c3c371f720d..fc903fb9376 100644 --- a/frameworks/kits/appkit/native/app/src/sys_mgr_client.cpp +++ b/frameworks/kits/appkit/native/app/src/sys_mgr_client.cpp @@ -23,7 +23,10 @@ namespace OHOS { namespace AppExecFwk { -SysMrgClient::SysMrgClient() : abilityManager_(nullptr) +OHOS::sptr SysMrgClient::remoteObject_ = nullptr; +std::mutex SysMrgClient::remoteObjectMutex_; + +SysMrgClient::SysMrgClient() {} SysMrgClient::~SysMrgClient() @@ -37,18 +40,22 @@ SysMrgClient::~SysMrgClient() */ sptr SysMrgClient::GetSystemAbility(const int32_t systemAbilityId) { - // use single instance of abilityManager_ - if (abilityManager_ == nullptr) { - std::lock_guard lock(saMutex_); - if (abilityManager_ == nullptr) { - abilityManager_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (abilityManager_ == nullptr) { - HILOG_ERROR("fail to GetSystemAbility abilityManager_ == nullptr."); + if (remoteObject_ == nullptr) { + std::lock_guard lock(remoteObjectMutex_); + if (remoteObject_ == nullptr) { + auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + HILOG_ERROR("GetSystemAbilityManager for %{public}d failed.", systemAbilityId); + return nullptr; + } + remoteObject_ = systemAbilityManager->GetSystemAbility(systemAbilityId); + if (remoteObject_ == nullptr) { + HILOG_ERROR("GetSystemAbility for %{public}d failed.", systemAbilityId); return nullptr; } } } - return abilityManager_->GetSystemAbility(systemAbilityId); + return remoteObject_; } /** diff --git a/services/abilitymgr/include/sa_mgr_client.h b/services/abilitymgr/include/sa_mgr_client.h index dfc80b19134..8477f660465 100644 --- a/services/abilitymgr/include/sa_mgr_client.h +++ b/services/abilitymgr/include/sa_mgr_client.h @@ -38,9 +38,9 @@ public: void RegisterSystemAbility(const int32_t systemAbilityId, sptr broker); private: - OHOS::sptr saMgr_; - std::mutex saMutex_; std::unordered_map> servicesMap_; + static OHOS::sptr remoteObject_; + static std::mutex saMutex_; }; } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/sa_mgr_client.cpp b/services/abilitymgr/src/sa_mgr_client.cpp index d155c1ae260..0d0019c9f27 100644 --- a/services/abilitymgr/src/sa_mgr_client.cpp +++ b/services/abilitymgr/src/sa_mgr_client.cpp @@ -23,7 +23,10 @@ namespace OHOS { namespace AAFwk { -SaMgrClient::SaMgrClient() : saMgr_(nullptr) +OHOS::sptr SaMgrClient::remoteObject_ = nullptr; +std::mutex SaMgrClient::saMutex_; + +SaMgrClient::SaMgrClient() {} SaMgrClient::~SaMgrClient() @@ -31,18 +34,22 @@ SaMgrClient::~SaMgrClient() sptr SaMgrClient::GetSystemAbility(const int32_t systemAbilityId) { - // use single instance of saMgr_ - if (saMgr_ == nullptr) { + if (remoteObject_ == nullptr) { std::lock_guard lock(saMutex_); - if (saMgr_ == nullptr) { - saMgr_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (saMgr_ == nullptr) { - HILOG_ERROR("Fail to get registry."); + if (remoteObject_ == nullptr) { + auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + HILOG_ERROR("GetSystemAbilityManager for %{public}d failed.", systemAbilityId); + return nullptr; + } + remoteObject_ = systemAbilityManager->GetSystemAbility(systemAbilityId); + if (remoteObject_ == nullptr) { + HILOG_ERROR("GetSystemAbility for %{public}d failed.", systemAbilityId); return nullptr; } } } - return saMgr_->GetSystemAbility(systemAbilityId); + return remoteObject_; } void SaMgrClient::RegisterSystemAbility( diff --git a/services/appmgr/include/remote_client_manager.h b/services/appmgr/include/remote_client_manager.h index 2372b5cf282..4aef9a4f5ea 100644 --- a/services/appmgr/include/remote_client_manager.h +++ b/services/appmgr/include/remote_client_manager.h @@ -61,8 +61,9 @@ public: private: std::shared_ptr appSpawnClient_; - sptr bundleManager_; std::shared_ptr nwebSpawnClient_; + static sptr bundleManager_; + static std::mutex bundleMgrMutex_; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appmgr/src/remote_client_manager.cpp b/services/appmgr/src/remote_client_manager.cpp index a1b5cda7a17..6280ff40491 100644 --- a/services/appmgr/src/remote_client_manager.cpp +++ b/services/appmgr/src/remote_client_manager.cpp @@ -22,6 +22,9 @@ namespace OHOS { namespace AppExecFwk { +sptr RemoteClientManager::bundleManager_ = nullptr; +std::mutex RemoteClientManager::bundleMgrMutex_; + RemoteClientManager::RemoteClientManager() : appSpawnClient_(std::make_shared()), nwebSpawnClient_(std::make_shared(true)) {} @@ -45,12 +48,23 @@ void RemoteClientManager::SetSpawnClient(const std::shared_ptr & sptr RemoteClientManager::GetBundleManager() { if (bundleManager_ == nullptr) { - sptr systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (systemManager != nullptr) { - bundleManager_ = - iface_cast(systemManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID)); - } else { - HILOG_ERROR("AppMgrServiceInner::GetBundleManager fail to get SAMGR"); + std::lock_guard lock(bundleMgrMutex_); + if (bundleManager_ == nullptr) { + auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + HILOG_ERROR("GetBundleMgr GetSystemAbilityManager is null"); + return nullptr; + } + auto bundleMgrSa = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (bundleMgrSa == nullptr) { + HILOG_ERROR("GetBundleMgr GetSystemAbility is null"); + return nullptr; + } + auto bundleMgr = OHOS::iface_cast(bundleMgrSa); + if (bundleMgr == nullptr) { + HILOG_ERROR("GetBundleMgr iface_cast get null"); + } + bundleManager_ = bundleMgr; } } return bundleManager_; diff --git a/services/formmgr/include/form_bms_helper.h b/services/formmgr/include/form_bms_helper.h index 4e5523ba3bc..bb34553e1e6 100644 --- a/services/formmgr/include/form_bms_helper.h +++ b/services/formmgr/include/form_bms_helper.h @@ -71,7 +71,8 @@ private: std::string GenerateModuleKey(const std::string &bundleName, const std::string &moduleName) const; private: - sptr iBundleMgr_ = nullptr; + static sptr bundleMgr_; + static std::mutex bundleMgrMutex_; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/services/formmgr/src/form_bms_helper.cpp b/services/formmgr/src/form_bms_helper.cpp index de4de2ed6fb..25979a87967 100644 --- a/services/formmgr/src/form_bms_helper.cpp +++ b/services/formmgr/src/form_bms_helper.cpp @@ -24,6 +24,9 @@ namespace OHOS { namespace AppExecFwk { +sptr FormBmsHelper::bundleMgr_ = nullptr; +std::mutex FormBmsHelper::bundleMgrMutex_; + FormBmsHelper::FormBmsHelper() {} @@ -36,24 +39,27 @@ FormBmsHelper::~FormBmsHelper() */ sptr FormBmsHelper::GetBundleMgr() { - HILOG_INFO("%{public}s called.", __func__); - - if (iBundleMgr_ == nullptr) { - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (remoteObject == nullptr) { - HILOG_ERROR("%{public}s error, failed to get bundle manager service.", __func__); - return nullptr; - } - - iBundleMgr_ = iface_cast(remoteObject); - if (iBundleMgr_ == nullptr) { - HILOG_ERROR("%{public}s error, failed to get bundle manager service", __func__); - return nullptr; + if (bundleMgr_ == nullptr) { + std::lock_guard lock(bundleMgrMutex_); + if (bundleMgr_ == nullptr) { + auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + HILOG_ERROR("GetBundleMgr GetSystemAbilityManager is null"); + return nullptr; + } + auto bundleMgrSa = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (bundleMgrSa == nullptr) { + HILOG_ERROR("GetBundleMgr GetSystemAbility is null"); + return nullptr; + } + auto bundleMgr = OHOS::iface_cast(bundleMgrSa); + if (bundleMgr == nullptr) { + HILOG_ERROR("GetBundleMgr iface_cast get null"); + } + bundleMgr_ = bundleMgr; } } - return iBundleMgr_; + return bundleMgr_; } /** @@ -63,8 +69,7 @@ sptr FormBmsHelper::GetBundleMgr() void FormBmsHelper::SetBundleManager(const sptr &bundleManager) { HILOG_INFO("%{public}s called.", __func__); - - iBundleMgr_ = bundleManager; + bundleMgr_ = bundleManager; } /** * @brief Notify module removable. -- Gitee