From ee2feaa2305bcce7e99cf590d57927af4482d36a Mon Sep 17 00:00:00 2001 From: buzhenwang Date: Tue, 13 May 2025 19:20:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=90=86AI=E5=91=8A=E8=AD=A6=20Signed?= =?UTF-8?q?-off-by:leiguangyu=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: buzhenwang --- include/utilities.h | 3 +- src/subcommand_record.cpp | 20 +++++++-- src/utilities.cpp | 34 +++++++++++++-- src/virtual_runtime.cpp | 4 +- src/virtual_thread.cpp | 4 ++ .../unittest/common/native/utilities_test.cpp | 42 +++++++++++++++++++ 6 files changed, 97 insertions(+), 10 deletions(-) diff --git a/include/utilities.h b/include/utilities.h index b5a810e..0d9efff 100644 --- a/include/utilities.h +++ b/include/utilities.h @@ -335,6 +335,7 @@ bool PowerOfTwo(uint64_t n); bool IsNumeric(const std::string& str); bool IscontainDigits(const std::string& str); bool IsStringToIntSuccess(const std::string &str, int &num); +bool StringToUint64(const std::string &str, uint64_t &val); bool IsDirectoryExists(const std::string& fileName); bool CreateDirectory(const std::string& path, mode_t mode); bool IsValidOutPath(const std::string& path); @@ -379,7 +380,7 @@ void CollectPidsByAppname(std::set &pids, const Container& appPackage) } } -bool CheckAppIsRunning (std::vector &selectPids, const std::string &appPackage, int checkAppMs); +bool CheckAppIsRunning(std::vector &selectPids, const std::string &appPackage, int checkAppMs); bool IsExistDebugByApp(const std::string& bundleName, std::string& err); bool IsExistDebugByPid(const std::vector &pids, std::string& err); bool IsSupportNonDebuggableApp(); diff --git a/src/subcommand_record.cpp b/src/subcommand_record.cpp index 8f922fd..aa08861 100644 --- a/src/subcommand_record.cpp +++ b/src/subcommand_record.cpp @@ -1675,8 +1675,18 @@ uint32_t SubCommandRecord::GetCountFromFile(const std::string &fileName) ret++; std::vector vSubstr = StringSplit(subStr, "-"); static const size_t BEGIN_END = 2; - if (vSubstr.size() == BEGIN_END) { - ret += static_cast((std::stoi(vSubstr[1]) - std::stoi(vSubstr[0]))); + if (vSubstr.size() != BEGIN_END) { + continue; + } + int num1 = 0; + int num2 = 0; + if (vSubstr[1].size() >= 2 && StringEndsWith(vSubstr[1], "\n")) { + vSubstr[1].resize(vSubstr[1].size() - 1); + } + bool ret1 = IsStringToIntSuccess(vSubstr[1], num1); + bool ret2 = IsStringToIntSuccess(vSubstr[0], num2); + if (ret1 && ret2) { + ret += static_cast(num1 - num2); } } return ret; @@ -1746,7 +1756,11 @@ void SubCommandRecord::AddMemTotalFeature() } if ((it + 1) != subStrs.end()) { - uint64_t memTotal = std::stoul(*(it + 1)); + uint64_t memTotal = 0; + if (!StringToUint64(*(it + 1), memTotal)) { + HIPERF_HILOGE(MODULE_DEFAULT, "get uint64_t failed, paramValue: %{pubilc}s", (*(it + 1)).c_str()); + continue; + } fileWriter_->AddU64Feature(FEATURE::TOTAL_MEM, memTotal); } break; diff --git a/src/utilities.cpp b/src/utilities.cpp index 99aad1d..cacfe79 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -365,6 +365,19 @@ bool IsStringToIntSuccess(const std::string &str, int &val) return true; } +bool StringToUint64(const std::string &str, uint64_t &val) +{ + char *endPtr = nullptr; + errno = 0; + uint64_t num = std::strtoull(str.c_str(), &endPtr, 10); // 10 : decimal scale + if (endPtr == str.c_str() || *endPtr != '\0' || errno != 0 || num > ULLONG_MAX || str.c_str()[0] == '-') { + HIPERF_HILOGE(MODULE_DEFAULT, "get uint64 failed, str: %{public}s", str.c_str()); + return false; + } + val = num; + return true; +} + bool ReadIntFromProcFile(const std::string &path, int &value) { std::string s = ReadFileToString(path); @@ -372,7 +385,12 @@ bool ReadIntFromProcFile(const std::string &path, int &value) if (!IscontainDigits(s)) { return false; } - value = std::stoi(s); + if (s.size() >= 2 && StringEndsWith(s, "\n")) { + s.resize(s.size() - 1); + } + if (!IsStringToIntSuccess(s, value)) { + return false; + } return true; } @@ -538,7 +556,11 @@ std::vector GetSubthreadIDs(const pid_t pid) std::vector res {}; for (auto tidStr : tids) { if (!tidStr.empty()) { - pid_t tid = static_cast(std::stoul(tidStr, nullptr)); + uint64_t dest = 0; + if (!StringToUint64(tidStr, dest)) { + continue; + } + pid_t tid = static_cast(dest); if (tid == pid) { continue; } @@ -557,7 +579,11 @@ std::vector GetSubthreadIDs(const pid_t pid, std::map std::vector res{}; for (auto tidStr : tids) { ThreadInfos info; - pid_t tid = static_cast(std::stoul(tidStr, nullptr)); + uint64_t dest = 0; + if (!StringToUint64(tidStr, dest)) { + continue; + } + pid_t tid = static_cast(dest); info.tid = tid; info.pid = pid; thread_map[tid] = info; @@ -669,7 +695,7 @@ pid_t GetAppPackagePid(const std::string &appPackage, const pid_t oldPid, const return res; } -bool CheckAppIsRunning (std::vector &selectPids, const std::string &appPackage, int checkAppMs) +bool CheckAppIsRunning(std::vector &selectPids, const std::string &appPackage, int checkAppMs) { if (!appPackage.empty()) { pid_t appPid = GetAppPackagePid(appPackage, -1, checkAppMs, waitAppRunCheckTimeOut); diff --git a/src/virtual_runtime.cpp b/src/virtual_runtime.cpp index 14f43f4..e2be713 100644 --- a/src/virtual_runtime.cpp +++ b/src/virtual_runtime.cpp @@ -105,7 +105,7 @@ VirtualThread &VirtualRuntime::UpdateThread(pid_t pid, pid_t tid, const std::str const auto startTime = steady_clock::now(); #endif VirtualThread &thread = GetThread(pid, tid, name); - if (!name.empty() && thread.name_.empty()) { + if (!name.empty() && (thread.name_.empty() || !StringEndsWith(thread.name_, name))) { thread.name_ = name; } #ifdef HIPERF_DEBUG_TIME @@ -252,7 +252,7 @@ void VirtualRuntime::UpdateKernelModulesSpaceMaps() if (lineSize > 4096) { // 4096: line length continue; } - char *module = new char[lineSize]; + char *module = new char[lineSize + 1]; /* name size load map hi_mipi_rx 53248 0 - Live 0xbf109000 (O) diff --git a/src/virtual_thread.cpp b/src/virtual_thread.cpp index 78dd072..3d9f535 100644 --- a/src/virtual_thread.cpp +++ b/src/virtual_thread.cpp @@ -293,7 +293,11 @@ void VirtualThread::ParseServiceMap(const std::string &filename) if (mapTokens.size() == MMAP_LINE_MAX_TOKEN && mapTokens[MMAP_LINE_TOKEN_INDEX_NAME] == name_) { HLOGM("map line: %s", line.c_str()); + constexpr const int mmapAddrRangeToken = 2; std::vector addrRanges = StringSplit(mapTokens[0], "-"); + if (addrRanges.size() < mmapAddrRangeToken) { + continue; + } begin = std::stoull(addrRanges[0], nullptr, NUMBER_FORMAT_HEX_BASE); end = std::stoull(addrRanges[1], nullptr, NUMBER_FORMAT_HEX_BASE); break; diff --git a/test/unittest/common/native/utilities_test.cpp b/test/unittest/common/native/utilities_test.cpp index ff8f186..919dadd 100644 --- a/test/unittest/common/native/utilities_test.cpp +++ b/test/unittest/common/native/utilities_test.cpp @@ -894,6 +894,48 @@ HWTEST_F(UtilitiesTest, IsValidOutPathErr, TestSize.Level2) std::string file = "/data/log/hiperflog/perf.data"; EXPECT_EQ(IsValidOutPath(file), false); } + +/** + * @tc.name: StringToIntTest + * @tc.desc: Test StringToUint64 function. + * @tc.type: FUNC +*/ +HWTEST_F(UtilitiesTest, StringToIntTest, TestSize.Level2) +{ + std::string traceParamsStr = "1234567890"; + uint64_t paramsUint64 = 0; + EXPECT_TRUE(StringToUint64(traceParamsStr, paramsUint64)); + EXPECT_EQ(paramsUint64, 1234567890); // 1234567890: test value + traceParamsStr = "18446744073709551615"; + EXPECT_TRUE(StringToUint64(traceParamsStr, paramsUint64)); + EXPECT_EQ(paramsUint64, ULLONG_MAX); +} + +/** + * @tc.name: StringToUint64ErrorTest + * @tc.desc: Test StringToUint64 function. + * @tc.type: FUNC +*/ +HWTEST_F(UtilitiesTest, StringToUint64ErrorTest, TestSize.Level2) +{ + std::string traceParamsStr = "-1234567890"; + uint64_t paramsUint64 = 0; + EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64)); + traceParamsStr = "a123"; + EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64)); + traceParamsStr = ""; + EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64)); + traceParamsStr = "12a3"; + EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64)); + traceParamsStr = "abc"; + EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64)); + traceParamsStr = ".1"; + EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64)); + traceParamsStr = "1.1"; + EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64)); + traceParamsStr = "18446744073709551616"; + EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64)); +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS -- Gitee