From 449877a446f2c6e4f79b4aa3413243a40a2ca4b2 Mon Sep 17 00:00:00 2001 From: kai-ma Date: Fri, 4 Jul 2025 15:00:48 +0800 Subject: [PATCH] add csrc/common --- accuracy_tools/msprobe/csrc/CMakeLists.txt | 51 ++++++++++++ .../msprobe/csrc/common/Toolkit.cpp | 82 +++++++++++++++++++ accuracy_tools/msprobe/csrc/common/Toolkit.h | 29 +++++++ 3 files changed, 162 insertions(+) create mode 100644 accuracy_tools/msprobe/csrc/CMakeLists.txt create mode 100644 accuracy_tools/msprobe/csrc/common/Toolkit.cpp create mode 100644 accuracy_tools/msprobe/csrc/common/Toolkit.h diff --git a/accuracy_tools/msprobe/csrc/CMakeLists.txt b/accuracy_tools/msprobe/csrc/CMakeLists.txt new file mode 100644 index 00000000000..fcb5f333c5b --- /dev/null +++ b/accuracy_tools/msprobe/csrc/CMakeLists.txt @@ -0,0 +1,51 @@ +cmake_minimum_required(VERSION 3.14) +cmake_policy(SET CMP0048 NEW) +project(msprobe VERSION 1.0.0 LANGUAGES CXX C) + +set(CMAKE_VERBOSE_MAKEFILE ON) + +find_package(cpython MODULE REQUIRED) +find_package(nlohmannjson MODULE REQUIRED) +find_package(ZLIB REQUIRED) + +add_library(msprobe_c SHARED) + +add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) +target_compile_definitions(msprobe_c PRIVATE _GLIBCXX_USE_CXX11_ABI=0) + +# 安全编译选项 +target_compile_options(msprobe_c PRIVATE "-Wall") +target_compile_options(msprobe_c PRIVATE "-fPIC") +target_compile_options(msprobe_c PRIVATE "-fstack-protector-all") +target_compile_options(msprobe_c PRIVATE "-ftrapv") +target_compile_options(msprobe_c PRIVATE "-D_FORTIFY_SOURCE=2") + +target_link_options(msprobe_c PRIVATE "-Wl,-z,relor") +target_link_options(msprobe_c PRIVATE "-Wl,-z,now") +target_link_options(msprobe_c PRIVATE "-Wl,-z,noexecstack") +target_link_options(msprobe_c PRIVATE "-Wl,--disable-new-dtags") +target_link_options(msprobe_c PRIVATE "-s") + +target_link_libraries(msprobe_c PUBLIC dl) +target_link_libraries(msprobe_c PUBLIC pthread) +target_link_libraries(msprobe_c PUBLIC ${cpython_LIBRARIES}) +target_link_libraries(msprobe_c PUBLIC ZLIB::ZLIB) + +if(DEFINED BUILD_TYPE AND "${BUILD_TYPE}" STREQUAL "debug") + target_compile_options(msprobe_c PRIVATE "-O0") + target_compile_options(msprobe_c PRIVATE "-g") + target_compile_definitions(msprobe_c PRIVATE __DEBUG__) +else() + target_compile_options(msprobe_c PRIVATE "-O2") +endif() + +target_include_directories(msprobe_c BEFORE PRIVATE /usr/include) +target_include_directories(msprobe_c PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + +get_target_property(INCLUDE_DIRS msprobe_c INCLUDE_DIRECTORIES) +message(STATUS "msprobe_c include dirs: ${INCLUDE_DIRS}") + +file(GLOB_RECURSE SOURCES "*.cpp") +target_sources(msprobe_c PRIVATE ${SOURCES}) + +install(TARGETS msprobe_c LIBRARY DESTINATION lib) diff --git a/accuracy_tools/msprobe/csrc/common/Toolkit.cpp b/accuracy_tools/msprobe/csrc/common/Toolkit.cpp new file mode 100644 index 00000000000..7633fe70a65 --- /dev/null +++ b/accuracy_tools/msprobe/csrc/common/Toolkit.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2025-2025. Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 "common/Toolkit.h" + +#include +#include +#include +#include + +#include "utils/Constant.h" +#include "utils/IO.h" +#include "utils/Log.h" +#include "utils/Str.h" + +namespace Kit { + inline const uint16_t maxLines = 50000; + inline const std::regex MALICIOUS_CSV_PATTERN(R"(^[+-=%@\+\-=%@]|;[+-=%@\+\-=%@])"); + + void SetLogLevel() { + static std::once_flag flag; + std::call_once(flag, []() { + const std::string logLevel = StrSpace::GetEnvVar(Cst::LINK_LOG_LEVEL); + uint8_t defaultValue = static_cast(Utility::Log::LogLevel::INFO); + Utility::Log::GetInstance().SetLogLevel( + StrSpace::Str2Int(logLevel.c_str(), defaultValue, Cst::LINK_LOG_LEVEL)); + }); + } + + bool IsValidKeys(const std::vector &keys, const Utility::ordered_json &json) { + bool allValid = true; + for (const auto &key : keys) { + if (json.find(key) == json.end()) { + LOG_ERROR << "Found invalid key: " << key; + allValid = false; + } + } + return allValid; + } + + uint16_t GetLineNum(const std::string &filePath) { + std::ifstream inFile(filePath); + if (!inFile.is_open()) { + LOG_ERROR << "Failed to open file for reading: " << filePath; + return 0; + } + std::string line; + uint16_t lineCount = 0; + while (std::getline(inFile, line) && lineCount < maxLines) { + ++lineCount; + } + if (lineCount >= maxLines) { + LOG_WARNING << "Contents in file [" << filePath << "] reached the maximum size of " << maxLines + << " lines."; + } + return lineCount; + } + + bool IsSpecialCharInjected(const std::string &value) { + if (value.empty()) { + return false; + } + if (std::regex_search(value, MALICIOUS_CSV_PATTERN)) { + LOG_ERROR << "Found malicious characters in value: " << value << ". Cannot write file!"; + return true; + } + return false; + } +} // namespace Kit diff --git a/accuracy_tools/msprobe/csrc/common/Toolkit.h b/accuracy_tools/msprobe/csrc/common/Toolkit.h new file mode 100644 index 00000000000..806ae13108c --- /dev/null +++ b/accuracy_tools/msprobe/csrc/common/Toolkit.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2025-2025. Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 TOOLKIT_H +#define TOOLKIT_H + +#include "utils/IO.h" + +namespace Kit { + void SetLogLevel(); + bool IsValidKeys(const std::vector &keys, const Utility::ordered_json &json); + uint16_t GetLineNum(const std::string &filePath); + bool IsSpecialCharInjected(const std::string &value); +} // namespace Kit + +#endif -- Gitee