From 08de95d233f0dd47833a7226ea91e3b54ceee407 Mon Sep 17 00:00:00 2001 From: xia-bubai Date: Sun, 15 Jun 2025 17:53:53 +0800 Subject: [PATCH] add memory manager part in el5 module Signed-off-by: xia-bubai --- access_token.gni | 7 ++ bundle.json | 3 +- .../El5FilekeyManagerInterface.idl | 2 + services/el5filekeymanager/BUILD.gn | 6 ++ .../include/el5_filekey_manager_service.h | 2 + .../include/el5_memory_manager.h | 47 ++++++++ .../src/el5_filekey_manager_service.cpp | 25 +++++ .../el5_filekey_manager_service_ability.cpp | 1 - .../src/el5_memory_manager.cpp | 100 ++++++++++++++++++ services/el5filekeymanager/test/BUILD.gn | 20 +++- 10 files changed, 210 insertions(+), 3 deletions(-) create mode 100644 services/el5filekeymanager/include/el5_memory_manager.h create mode 100644 services/el5filekeymanager/src/el5_memory_manager.cpp diff --git a/access_token.gni b/access_token.gni index 303cfde59..b9641e6b0 100644 --- a/access_token.gni +++ b/access_token.gni @@ -160,3 +160,10 @@ if (!defined(global_parts_info) || } else { power_manager_enable = false } + +if (defined(global_parts_info) && + defined(global_parts_info.resourceschedule_memmgr_override)) { + memory_manager_enable = true +} else { + memory_manager_enable = false +} diff --git a/bundle.json b/bundle.json index 963dbf16e..47a33d4a0 100644 --- a/bundle.json +++ b/bundle.json @@ -60,7 +60,8 @@ "sqlite", "window_manager", "zlib", - "runtime_core" + "runtime_core", + "memmgr" ], "third_party": [] }, diff --git a/interfaces/inner_api/el5filekeymanager/El5FilekeyManagerInterface.idl b/interfaces/inner_api/el5filekeymanager/El5FilekeyManagerInterface.idl index 27070333b..1a6f7ea27 100644 --- a/interfaces/inner_api/el5filekeymanager/El5FilekeyManagerInterface.idl +++ b/interfaces/inner_api/el5filekeymanager/El5FilekeyManagerInterface.idl @@ -19,6 +19,8 @@ import El5FilekeyCallbackInterface; sequenceable OHOS.Security.AccessToken.UserAppKeyInfo; sequenceable OHOS.Security.AccessToken.AppKeyLoadInfo; +option_stub_hooks on; + interface OHOS.Security.AccessToken.El5FilekeyManagerInterface{ void AcquireAccess([in] DataLockType type); void ReleaseAccess([in] DataLockType type); diff --git a/services/el5filekeymanager/BUILD.gn b/services/el5filekeymanager/BUILD.gn index 92cafd73c..05f997b1d 100644 --- a/services/el5filekeymanager/BUILD.gn +++ b/services/el5filekeymanager/BUILD.gn @@ -98,6 +98,12 @@ if (is_standard_system && ability_base_enable == true) { external_deps += [ "screenlock_mgr:screenlock_client" ] } + if (memory_manager_enable) { + sources += [ "src/el5_memory_manager.cpp" ] + cflags_cc += [ "-DMEMORY_MANAGER_ENABLE" ] + external_deps += [ "memmgr:memmgrclient" ] + } + subsystem_name = "security" part_name = "access_token" } diff --git a/services/el5filekeymanager/include/el5_filekey_manager_service.h b/services/el5filekeymanager/include/el5_filekey_manager_service.h index 2d660a112..0738b4684 100644 --- a/services/el5filekeymanager/include/el5_filekey_manager_service.h +++ b/services/el5filekeymanager/include/el5_filekey_manager_service.h @@ -60,6 +60,8 @@ public: void PostDelayedUnloadTask(uint32_t delayedTime); void CancelDelayedUnloadTask(); int Dump(int fd, const std::vector& args) override; + int32_t CallbackEnter(uint32_t code) override; + int32_t CallbackExit(uint32_t code, int32_t result) override; private: ServiceRunningState serviceRunningState_ = ServiceRunningState::STATE_NOT_START; diff --git a/services/el5filekeymanager/include/el5_memory_manager.h b/services/el5filekeymanager/include/el5_memory_manager.h new file mode 100644 index 000000000..1891fd7df --- /dev/null +++ b/services/el5filekeymanager/include/el5_memory_manager.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2025-2025 Huawei Device Co., Ltd. + * 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. + */ + +#ifndef EL5_MEMORY_MANAGER_H +#define EL5_MEMORY_MANAGER_H + +#include +#include +#include + +namespace OHOS { +namespace Security { +namespace AccessToken { + +class El5MemoryManager { +public: + El5MemoryManager() {}; + virtual ~El5MemoryManager() {}; + + static El5MemoryManager& GetInstance(); + void SetSelfPid(const pid_t &pid); + void SetProcessAliveStatus(); + void SetProcessDeadStatus(); + +private: + void SetCritical(const bool &isKeyService); + void NotifyProcessStatus(const int32_t &status); + pid_t pid_ = -1; + int32_t callFuncRunningNum_ = 0; + std::mutex callNumberMutex_; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // EL5_MEMORY_MANAGER_H diff --git a/services/el5filekeymanager/src/el5_filekey_manager_service.cpp b/services/el5filekeymanager/src/el5_filekey_manager_service.cpp index 450855093..92360a7cc 100644 --- a/services/el5filekeymanager/src/el5_filekey_manager_service.cpp +++ b/services/el5filekeymanager/src/el5_filekey_manager_service.cpp @@ -25,6 +25,9 @@ #include "ipc_skeleton.h" #include "iservice_registry.h" #include "el5_filekey_manager_log.h" +#ifdef MEMORY_MANAGER_ENABLE +#include "el5_memory_manager.h" +#endif #ifdef THEME_SCREENLOCK_MGR_ENABLE #include "screenlock_manager.h" #endif @@ -66,6 +69,22 @@ El5FilekeyManagerService::~El5FilekeyManagerService() LOG_INFO("Instance destroyed."); } +int32_t El5FilekeyManagerService::CallbackEnter(uint32_t code) +{ +#ifdef MEMORY_MANAGER_ENABLE + El5MemoryManager::GetInstance().SetProcessAliveStatus(); +#endif + return ERR_OK; +} + +int32_t El5FilekeyManagerService::CallbackExit(uint32_t code, int32_t result) +{ +#ifdef MEMORY_MANAGER_ENABLE + El5MemoryManager::GetInstance().SetProcessDeadStatus(); +#endif + return ERR_OK; +} + int32_t El5FilekeyManagerService::Init() { LOG_INFO("Ready to init."); @@ -94,6 +113,9 @@ int32_t El5FilekeyManagerService::Init() return EFM_ERR_CALL_POLICY_ERROR; } +#ifdef MEMORY_MANAGER_ENABLE + El5MemoryManager::GetInstance().SetSelfPid(getpid()); +#endif return EFM_SUCCESS; } @@ -103,6 +125,9 @@ void El5FilekeyManagerService::UnInit() if (service_) { service_->UnInit(); } +#ifdef MEMORY_MANAGER_ENABLE + El5MemoryManager::GetInstance().SetSelfPid(-1); +#endif } void El5FilekeyManagerService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) diff --git a/services/el5filekeymanager/src/el5_filekey_manager_service_ability.cpp b/services/el5filekeymanager/src/el5_filekey_manager_service_ability.cpp index fcd3f65fa..86b76ac28 100644 --- a/services/el5filekeymanager/src/el5_filekey_manager_service_ability.cpp +++ b/services/el5filekeymanager/src/el5_filekey_manager_service_ability.cpp @@ -59,7 +59,6 @@ void El5FilekeyManagerServiceAbility::OnStart(const SystemAbilityOnDemandReason AddSystemAbilityListener(TELEPHONY_CALL_MANAGER_SYS_ABILITY_ID); AddSystemAbilityListener(TIME_SERVICE_ID); AddSystemAbilityListener(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID); - if (reasonName == "usual.event.SCREEN_LOCKED") { service_->SetPolicyScreenLocked(); } else if (reasonName == "usual.event.USER_REMOVED" || reasonName == "usual.event.USER_STOPPED") { diff --git a/services/el5filekeymanager/src/el5_memory_manager.cpp b/services/el5filekeymanager/src/el5_memory_manager.cpp new file mode 100644 index 000000000..f8a3a528f --- /dev/null +++ b/services/el5filekeymanager/src/el5_memory_manager.cpp @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2025-2025 Huawei Device Co., Ltd. + * 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 "el5_memory_manager.h" + +#include "el5_filekey_manager_error.h" +#include "el5_filekey_manager_log.h" +#include "mem_mgr_client.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +constexpr int32_t MAX_RUNNING_NUM = 256; +std::recursive_mutex g_instanceMutex; +} + +El5MemoryManager& El5MemoryManager::GetInstance() +{ + static El5MemoryManager* instance = nullptr; + if (instance == nullptr) { + std::lock_guard lock(g_instanceMutex); + if (instance == nullptr) { + El5MemoryManager* tmp = new (std::nothrow) El5MemoryManager(); + instance = std::move(tmp); + } + } + return *instance; +} + +void El5MemoryManager::SetProcessAliveStatus() +{ + std::lock_guard lock(callNumberMutex_); + callFuncRunningNum_++; + if (pid_ == -1 || callFuncRunningNum_ <= 0) { + LOG_WARN("pid=%{public}d, callFuncRunningNum=%{public}d.", pid_, callFuncRunningNum_); + return; + } + + if (callFuncRunningNum_ >= MAX_RUNNING_NUM) { + LOG_WARN("callFuncRunningNum %{public}d over 256.", callFuncRunningNum_); + return; + } + + if (callFuncRunningNum_ == 1) { + NotifyProcessStatus(PROCESS_STATUS_STARTED); + SetCritical(true); + } +} + +void El5MemoryManager::SetProcessDeadStatus() +{ + std::lock_guard lock(callNumberMutex_); + callFuncRunningNum_--; + + if (pid_ == -1 || callFuncRunningNum_ < 0) { + LOG_WARN("pid=%{public}d, callFuncRunningNum=%{public}d.", pid_, callFuncRunningNum_); + return; + } + + if (callFuncRunningNum_ == 0) { + SetCritical(false); + NotifyProcessStatus(PROCESS_STATUS_DIED); + } +} + +void El5MemoryManager::SetCritical(const bool &isKeyService) { + int32_t ret = Memory::MemMgrClient::GetInstance().SetCritical(pid_, isKeyService, SANDBOX_MANAGER_SERVICE_ID); + if (ret != ERR_OK) { + LOG_WARN("Failed to set critical to %{public}d.", isKeyService); + } +} + +void El5MemoryManager::NotifyProcessStatus(const int32_t &status) +{ + int32_t ret = Memory::MemMgrClient::GetInstance().NotifyProcessStatus(pid_, PROCESS_TYPE_SA, status, SANDBOX_MANAGER_SERVICE_ID); + if (ret != ERR_OK) { + LOG_WARN("Failed to notify memory to %{public}d.", status); + } +} + +void El5MemoryManager::SetSelfPid(const pid_t &pid) +{ + pid_ = pid; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/services/el5filekeymanager/test/BUILD.gn b/services/el5filekeymanager/test/BUILD.gn index 3bc0d39c9..70fb21a59 100644 --- a/services/el5filekeymanager/test/BUILD.gn +++ b/services/el5filekeymanager/test/BUILD.gn @@ -80,6 +80,12 @@ if (is_standard_system && ability_base_enable == true) { cflags_cc += [ "-DTHEME_SCREENLOCK_MGR_ENABLE" ] external_deps += [ "screenlock_mgr:screenlock_client" ] } + + if (memory_manager_enable) { + sources += [ "../src/el5_memory_manager.cpp" ] + cflags_cc += [ "-DMEMORY_MANAGER_ENABLE" ] + external_deps += [ "memmgr:memmgrclient" ] + } } ohos_unittest("el5_filekey_manager_service_unittest") { @@ -143,6 +149,12 @@ if (is_standard_system && ability_base_enable == true) { cflags_cc += [ "-DTHEME_SCREENLOCK_MGR_ENABLE" ] external_deps += [ "screenlock_mgr:screenlock_client" ] } + + if (memory_manager_enable) { + sources += [ "../src/el5_memory_manager.cpp" ] + cflags_cc += [ "-DMEMORY_MANAGER_ENABLE" ] + external_deps += [ "memmgr:memmgrclient" ] + } } ohos_unittest("el5_filekey_manager_stub_unittest") { @@ -164,7 +176,7 @@ if (is_standard_system && ability_base_enable == true) { sources = [ "../src/el5_filekey_manager_service.cpp", - "src/el5_filekey_manager_stub_unittest.cpp", + "src/el5_filekey_manager_stub_unittest.cpp" ] configs = [ "${access_token_path}/config:coverage_flags" ] @@ -206,6 +218,12 @@ if (is_standard_system && ability_base_enable == true) { cflags_cc += [ "-DTHEME_SCREENLOCK_MGR_ENABLE" ] external_deps += [ "screenlock_mgr:screenlock_client" ] } + + if (memory_manager_enable) { + sources += [ "../src/el5_memory_manager.cpp" ] + cflags_cc += [ "-DMEMORY_MANAGER_ENABLE" ] + external_deps += [ "memmgr:memmgrclient" ] + } } group("unittest") { -- Gitee