From f5f78bba8aada121bd7cd28227b412d462f7ce87 Mon Sep 17 00:00:00 2001 From: j30052480 Date: Fri, 10 May 2024 16:35:42 +0800 Subject: [PATCH 1/3] feat: host support secure heap allocator config Signed-off-by: j30052480 --- adapter/uhdf2/host/devhost.c | 38 ++++++++++++++++-- .../tools/hc-gen/src/startup_cfg_gen.cpp | 40 +++++++++++++------ framework/tools/hc-gen/src/startup_cfg_gen.h | 4 ++ 3 files changed, 65 insertions(+), 17 deletions(-) diff --git a/adapter/uhdf2/host/devhost.c b/adapter/uhdf2/host/devhost.c index 3b2712840..aceea6783 100644 --- a/adapter/uhdf2/host/devhost.c +++ b/adapter/uhdf2/host/devhost.c @@ -23,6 +23,7 @@ #include "securec.h" #include "parameter.h" +#include "malloc.h" #include "devhost_dump.h" #include "devhost_service.h" #include "devhost_service_full.h" @@ -31,13 +32,13 @@ #include "hdf_power_manager.h" #define HDF_LOG_TAG hdf_device_host -#define DEVHOST_INPUT_PARAM_NUM 3 -#define DEVHOST_PARAM_NUM_WITH_PRI 5 +#define DEVHOST_MIN_INPUT_PARAM_NUM 5 #define DEVHOST_INPUT_PARAM_HOSTID_POS 1 #define DEVHOST_HOST_NAME_POS 2 #define DEVHOST_PROCESS_PRI_POS 3 #define DEVHOST_THREAD_PRI_POS 4 #define PARAM_BUF_LEN 128 +#define INVALID_PRIORITY "0" static void StartMemoryHook(const char* processName) { @@ -116,9 +117,34 @@ static void HdfSetProcPriority(char **argv) } } +static void SetMallopt(int argc, char **argv) +{ + for (int i = DEVHOST_MIN_INPUT_PARAM_NUM; i < argc - 1; i += 2) { + int32_t malloptKey = 0; + int32_t malloptValue = 0; + int ret = 0; + if (!HdfStringToInt(argv[i], &malloptKey)) { + HDF_LOGE("malloptKey parameter error:%{public}s", argv[i]); + continue; + } + if (!HdfStringToInt(argv[i + 1], &malloptValue)) { + HDF_LOGE("malloptValue parameter error:%{public}s", argv[i + 1]); + continue; + } + ret = mallopt(malloptKey, malloptValue); + if (ret != 1) { + HDF_LOGE("mallopt failed, malloptKey:%{public}d, malloptValue:%{public}d, ret:%{public}d", + malloptKey, malloptValue, ret); + continue; + } + HDF_LOGI("host set mallopt succeed, mallopt:%{public}d %{public}d", malloptKey, malloptValue); + } + return; +} + int main(int argc, char **argv) { - if ((argc != DEVHOST_INPUT_PARAM_NUM) && (argc != DEVHOST_PARAM_NUM_WITH_PRI)) { + if (argc < DEVHOST_MIN_INPUT_PARAM_NUM) { HDF_LOGE("Devhost main parameter error, argc: %{public}d", argc); return HDF_ERR_INVALID_PARAM; } @@ -135,9 +161,13 @@ int main(int argc, char **argv) HDF_LOGI("hdf device host %{public}s %{public}d start", hostName, hostId); SetProcTitle(argv, hostName); StartMemoryHook(hostName); - if (argc == DEVHOST_PARAM_NUM_WITH_PRI) { + if ((strcmp(argv[DEVHOST_PROCESS_PRI_POS], INVALID_PRIORITY) != 0) && + (strcmp(argv[DEVHOST_THREAD_PRI_POS], INVALID_PRIORITY) != 0)) { HdfSetProcPriority(argv); } + if (argc > DEVHOST_MIN_INPUT_PARAM_NUM) { + SetMallopt(argc, argv); + } struct IDevHostService *instance = DevHostServiceNewInstance(hostId, hostName); if (instance == NULL || instance->StartService == NULL) { diff --git a/framework/tools/hc-gen/src/startup_cfg_gen.cpp b/framework/tools/hc-gen/src/startup_cfg_gen.cpp index 2fe7ca862..b6f8b4daa 100644 --- a/framework/tools/hc-gen/src/startup_cfg_gen.cpp +++ b/framework/tools/hc-gen/src/startup_cfg_gen.cpp @@ -38,9 +38,10 @@ static constexpr const char *CAPS_INFO = " \"caps\" : ["; static constexpr const char *DYNAMIC_INFO = " \"ondemand\" : true,\n"; static constexpr const char *SECON_INFO = " \"secon\" : \"u:r:"; static constexpr const char *CRITICAL_INFO = " \"critical\" : ["; -static constexpr uint32_t INVALID_PRIORITY = 0xffffffff; +static constexpr uint32_t INVALID_PRIORITY = 0; static constexpr const char *SAND_BOX_INFO = " \"sandbox\" : "; static constexpr uint32_t INVALID_SAND_BOX = 0xffffffff; +static constexpr const char *MALLOPT_SEPARATOR = ":"; StartupCfgGen::StartupCfgGen(const std::shared_ptr &ast) : Generator(ast) { } @@ -106,18 +107,13 @@ void StartupCfgGen::EmitDynamicLoad(const std::string &name, std::set &configedKeywords) { - bool flag = false; - if ((hostInfoMap_[name].processPriority != INVALID_PRIORITY) && - (hostInfoMap_[name].threadPriority != INVALID_PRIORITY)) { - flag = true; - } if ((configedKeywords.find("path") == configedKeywords.end())) { - if (flag) { - ofs_ << PATH_INFO << "\"" << hostInfoMap_[name].hostId << "\", \"" << name << "\", \"" << - hostInfoMap_[name].processPriority << "\", \"" << hostInfoMap_[name].threadPriority << "\"],\n"; - } else { - ofs_ << PATH_INFO << "\"" << hostInfoMap_[name].hostId << "\", \"" << name << "\"],\n"; + ofs_ << PATH_INFO << "\"" << hostInfoMap_[name].hostId << "\", \"" << name << "\", \"" << + hostInfoMap_[name].processPriority << "\", \"" << hostInfoMap_[name].threadPriority; + for (auto iter : hostInfoMap_[name].mallocOpt) { + ofs_ << "\", \"" << iter.first << "\", \"" << iter.second; } + ofs_ << "\"],\n"; } } @@ -218,11 +214,12 @@ void StartupCfgGen::InitHostInfo(HostInfo &hostData) hostData.hostUID = ""; hostData.hostGID = ""; hostData.initConfig = {}; + hostData.mallocOpt = {}; hostData.hostPriority = 0; hostData.hostId = 0; hostData.hostCritical = ""; - hostData.processPriority = INVALID_PRIORITY; - hostData.threadPriority = INVALID_PRIORITY; + hostData.processPriority = INVALID_PRIORITY; // -20(high) - 19(low), default 0 + hostData.threadPriority = INVALID_PRIORITY; // 1(low) - 99(high) hostData.sandBox = INVALID_SAND_BOX; } @@ -355,6 +352,20 @@ void StartupCfgGen::GetProcessPriority(const std::shared_ptr &term, H } } +void StartupCfgGen::GetMallocOpt(const std::shared_ptr &term, + std::vector> &config) +{ + std::vector mallocOptions = {}; + GetConfigVector(term, mallocOptions); + for (auto mallocOption : mallocOptions) { + int separatorPos = mallocOption.find(MALLOPT_SEPARATOR); + std::string malloptKey = mallocOption.substr(0, separatorPos); + std::string malloptValue = mallocOption.substr(separatorPos + 1, + mallocOption.length() - (separatorPos + 1)); + config.push_back({malloptKey, malloptValue}); + } +} + void StartupCfgGen::GetHostLoadMode(const std::shared_ptr &hostInfo, HostInfo &hostData) { uint32_t preload; @@ -461,6 +472,9 @@ bool StartupCfgGen::GetHostInfo() object = hostInfo->Lookup("initconfig", PARSEROP_CONFTERM); GetConfigVector(object, hostData.initConfig); + object = hostInfo->Lookup("mallocopt", PARSEROP_CONFTERM); + GetMallocOpt(object, hostData.mallocOpt); + hostData.hostId = hostId; hostInfoMap_.insert(make_pair(serviceName, hostData)); hostId++; diff --git a/framework/tools/hc-gen/src/startup_cfg_gen.h b/framework/tools/hc-gen/src/startup_cfg_gen.h index 39f164536..064af0402 100644 --- a/framework/tools/hc-gen/src/startup_cfg_gen.h +++ b/framework/tools/hc-gen/src/startup_cfg_gen.h @@ -24,6 +24,7 @@ struct HostInfo { std::string hostGID; std::string hostCritical; std::vector initConfig; + std::vector> mallocOpt; uint32_t hostPriority; int32_t processPriority; int32_t threadPriority; @@ -93,6 +94,9 @@ private: static void GetProcessPriority(const std::shared_ptr &term, HostInfo &hostData); + static void GetMallocOpt(const std::shared_ptr &term, + std::vector> &config); + std::ofstream ofs_; std::string outFileName_; std::map hostInfoMap_; -- Gitee From 1143e9e92da00802048d586d032de09f4fe0a826 Mon Sep 17 00:00:00 2001 From: j30052480 Date: Sat, 11 May 2024 09:40:17 +0800 Subject: [PATCH 2/3] feat: host support secure heap allocator config Signed-off-by: j30052480 --- adapter/uhdf2/host/devhost.c | 4 ++-- framework/tools/hc-gen/src/startup_cfg_gen.cpp | 6 +++--- framework/tools/hc-gen/src/startup_cfg_gen.h | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/adapter/uhdf2/host/devhost.c b/adapter/uhdf2/host/devhost.c index aceea6783..ff633388b 100644 --- a/adapter/uhdf2/host/devhost.c +++ b/adapter/uhdf2/host/devhost.c @@ -38,6 +38,7 @@ #define DEVHOST_PROCESS_PRI_POS 3 #define DEVHOST_THREAD_PRI_POS 4 #define PARAM_BUF_LEN 128 +#define MALLOPT_PARA_CNT 2 #define INVALID_PRIORITY "0" static void StartMemoryHook(const char* processName) @@ -119,7 +120,7 @@ static void HdfSetProcPriority(char **argv) static void SetMallopt(int argc, char **argv) { - for (int i = DEVHOST_MIN_INPUT_PARAM_NUM; i < argc - 1; i += 2) { + for (int i = DEVHOST_MIN_INPUT_PARAM_NUM; i < argc - 1; i += MALLOPT_PARA_CNT) { int32_t malloptKey = 0; int32_t malloptValue = 0; int ret = 0; @@ -194,7 +195,6 @@ int main(int argc, char **argv) looper->Start(looper); } - HdfPowerManagerExit(); DevHostDumpDeInit(); HDF_LOGI("hdf device host %{public}s %{public}d exit", hostName, hostId); diff --git a/framework/tools/hc-gen/src/startup_cfg_gen.cpp b/framework/tools/hc-gen/src/startup_cfg_gen.cpp index b6f8b4daa..05af2a74c 100644 --- a/framework/tools/hc-gen/src/startup_cfg_gen.cpp +++ b/framework/tools/hc-gen/src/startup_cfg_gen.cpp @@ -352,9 +352,10 @@ void StartupCfgGen::GetProcessPriority(const std::shared_ptr &term, H } } -void StartupCfgGen::GetMallocOpt(const std::shared_ptr &term, +void StartupCfgGen::GetMallocOpt(const std::shared_ptr &hostInfo, std::vector> &config) { + std::shared_ptr term = hostInfo->Lookup("mallocopt", PARSEROP_CONFTERM); std::vector mallocOptions = {}; GetConfigVector(term, mallocOptions); for (auto mallocOption : mallocOptions) { @@ -472,8 +473,7 @@ bool StartupCfgGen::GetHostInfo() object = hostInfo->Lookup("initconfig", PARSEROP_CONFTERM); GetConfigVector(object, hostData.initConfig); - object = hostInfo->Lookup("mallocopt", PARSEROP_CONFTERM); - GetMallocOpt(object, hostData.mallocOpt); + GetMallocOpt(hostInfo, hostData.mallocOpt); hostData.hostId = hostId; hostInfoMap_.insert(make_pair(serviceName, hostData)); diff --git a/framework/tools/hc-gen/src/startup_cfg_gen.h b/framework/tools/hc-gen/src/startup_cfg_gen.h index 064af0402..b8b51dbd7 100644 --- a/framework/tools/hc-gen/src/startup_cfg_gen.h +++ b/framework/tools/hc-gen/src/startup_cfg_gen.h @@ -94,7 +94,7 @@ private: static void GetProcessPriority(const std::shared_ptr &term, HostInfo &hostData); - static void GetMallocOpt(const std::shared_ptr &term, + static void GetMallocOpt(const std::shared_ptr &hostInfo, std::vector> &config); std::ofstream ofs_; -- Gitee From fbcdee504bcc26d11be3d0a76e9dc6f2138db8d5 Mon Sep 17 00:00:00 2001 From: j30052480 Date: Mon, 13 May 2024 11:58:08 +0800 Subject: [PATCH 3/3] feat: host support secure heap allocator config Signed-off-by: j30052480 --- framework/tools/hc-gen/src/startup_cfg_gen.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/framework/tools/hc-gen/src/startup_cfg_gen.cpp b/framework/tools/hc-gen/src/startup_cfg_gen.cpp index 05af2a74c..ec219cb9f 100644 --- a/framework/tools/hc-gen/src/startup_cfg_gen.cpp +++ b/framework/tools/hc-gen/src/startup_cfg_gen.cpp @@ -38,6 +38,7 @@ static constexpr const char *CAPS_INFO = " \"caps\" : ["; static constexpr const char *DYNAMIC_INFO = " \"ondemand\" : true,\n"; static constexpr const char *SECON_INFO = " \"secon\" : \"u:r:"; static constexpr const char *CRITICAL_INFO = " \"critical\" : ["; +static constexpr uint32_t DEFAULT_PROCESS_PRIORITY = 0; static constexpr uint32_t INVALID_PRIORITY = 0; static constexpr const char *SAND_BOX_INFO = " \"sandbox\" : "; static constexpr uint32_t INVALID_SAND_BOX = 0xffffffff; @@ -218,7 +219,7 @@ void StartupCfgGen::InitHostInfo(HostInfo &hostData) hostData.hostPriority = 0; hostData.hostId = 0; hostData.hostCritical = ""; - hostData.processPriority = INVALID_PRIORITY; // -20(high) - 19(low), default 0 + hostData.processPriority = DEFAULT_PROCESS_PRIORITY; // -20(high) - 19(low), default 0 hostData.threadPriority = INVALID_PRIORITY; // 1(low) - 99(high) hostData.sandBox = INVALID_SAND_BOX; } -- Gitee