diff options
author | saturneric <[email protected]> | 2023-12-14 12:39:48 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2023-12-14 12:39:48 +0000 |
commit | beafe53c1b8671d8e84f0546eb404597300448c7 (patch) | |
tree | f9b3e2d1f1c94c344fe3f9df919f4be2e56b4cae | |
parent | fix: slove some memory issues (diff) | |
download | GpgFrontend-beafe53c1b8671d8e84f0546eb404597300448c7.tar.gz GpgFrontend-beafe53c1b8671d8e84f0546eb404597300448c7.zip |
fix: slove issues on memory and add asan support for debug
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | cmake/FlagsOverrides.cmake | 20 | ||||
-rw-r--r-- | src/GpgFrontendContext.cpp | 5 | ||||
-rw-r--r-- | src/GpgFrontendContext.h | 5 | ||||
-rw-r--r-- | src/core/GpgCoreInit.cpp | 83 | ||||
-rw-r--r-- | src/core/GpgCoreInit.h | 1 | ||||
-rw-r--r-- | src/core/function/SecureMemoryAllocator.h | 2 | ||||
-rw-r--r-- | src/core/function/gpg/GpgBasicOperator.cpp | 3 | ||||
-rw-r--r-- | src/core/function/gpg/GpgContext.cpp | 10 | ||||
-rw-r--r-- | src/core/function/gpg/GpgContext.h | 2 | ||||
-rw-r--r-- | src/init.cpp | 2 | ||||
-rw-r--r-- | src/main.cpp | 1 | ||||
-rw-r--r-- | src/test/GpgFrontendTest.cpp | 4 | ||||
-rw-r--r-- | src/test/core/GpgCoreTest.cpp | 5 | ||||
-rw-r--r-- | third_party/CMakeLists.txt | 5 |
15 files changed, 80 insertions, 70 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 17712e0d..b9e87ce0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later cmake_minimum_required(VERSION 3.16) -set(CMAKE_USER_MAKE_RULES_OVERRIDE "cmake/FlagsOverrides.cmake") +set(CMAKE_USER_MAKE_RULES_OVERRIDE "${CMAKE_SOURCE_DIR}/cmake/FlagsOverrides.cmake") # define project project(GpgFrontend diff --git a/cmake/FlagsOverrides.cmake b/cmake/FlagsOverrides.cmake index 989fe9fa..0ebbd70a 100644 --- a/cmake/FlagsOverrides.cmake +++ b/cmake/FlagsOverrides.cmake @@ -1,13 +1,13 @@ SET (CMAKE_GENERATOR "Ninja") -SET (CMAKE_C_FLAGS_INIT "-Wall -std=c11") -SET (CMAKE_C_FLAGS_DEBUG_INIT "-g") -SET (CMAKE_C_FLAGS_MINSIZEREL_INIT "-Os -DNDEBUG") -SET (CMAKE_C_FLAGS_RELEASE_INIT "-O3 -DNDEBUG") -SET (CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "-O2 -g") +SET (CMAKE_C_FLAGS "-Wall -std=c11") +SET (CMAKE_C_FLAGS_DEBUG "-g -fsanitize=address -fsanitize-recover=address") +SET (CMAKE_C_FLAGS_MINSIZERE "-Os -DNDEBUG") +SET (CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG") +SET (CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g") -SET (CMAKE_CXX_FLAGS_INIT "-Wall -std=c++17") -SET (CMAKE_CXX_FLAGS_DEBUG_INIT "-g") -SET (CMAKE_CXX_FLAGS_MINSIZEREL_INIT "-Os -DNDEBUG") -SET (CMAKE_CXX_FLAGS_RELEASE_INIT "-O3 -DNDEBUG") -SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "-O2 -g")
\ No newline at end of file +SET (CMAKE_CXX_FLAGS "-Wall -std=c++17") +SET (CMAKE_CXX_FLAGS_DEBUG "-g -fsanitize=address -fsanitize-recover=address") +SET (CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG") +SET (CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") +SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
\ No newline at end of file diff --git a/src/GpgFrontendContext.cpp b/src/GpgFrontendContext.cpp index 72d2b0c1..5f51975a 100644 --- a/src/GpgFrontendContext.cpp +++ b/src/GpgFrontendContext.cpp @@ -32,6 +32,7 @@ #include <memory> +#include "core/utils/MemoryUtils.h" #include "ui/GpgFrontendApplication.h" namespace GpgFrontend { @@ -52,11 +53,11 @@ auto GpgFrontendContext::GetInstance() -> std::weak_ptr<GpgFrontendContext> { } void GpgFrontendContext::InitCoreApplication() { - app = std::make_unique<QCoreApplication>(argc, argv); + app = SecureCreateUniqueObject<QCoreApplication>(argc, argv); } void GpgFrontendContext::InitGUIApplication() { - app = std::make_unique<UI::GpgFrontendApplication>(argc, argv); + app = SecureCreateUniqueObject<UI::GpgFrontendApplication>(argc, argv); } } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/GpgFrontendContext.h b/src/GpgFrontendContext.h index d72fb75b..fc027dd1 100644 --- a/src/GpgFrontendContext.h +++ b/src/GpgFrontendContext.h @@ -28,6 +28,8 @@ #pragma once +#include "core/function/SecureMemoryAllocator.h" + namespace GpgFrontend { struct GpgFrontendContext { @@ -36,9 +38,10 @@ struct GpgFrontendContext { spdlog::level::level_enum log_level; bool load_ui_env; - std::unique_ptr<QCoreApplication> app; + SecureUniquePtr<QCoreApplication> app; bool gather_external_gnupg_info; + bool load_default_gpg_context; /** * @brief Create a Instance object diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp index 05f3002c..2b2bc5be 100644 --- a/src/core/GpgCoreInit.cpp +++ b/src/core/GpgCoreInit.cpp @@ -304,41 +304,43 @@ void InitGpgFrontendCore(CoreInitArgs args) { custom_key_database_fs_path.u8string()); } - // init ctx, also checking the basical env - auto& ctx = GpgFrontend::GpgContext::CreateInstance( - kGpgFrontendDefaultChannel, [=]() -> ChannelObjectPtr { - GpgFrontend::GpgContextInitArgs args; - - // set key database path - if (use_custom_key_database_path && - !custom_key_database_path.empty()) { - args.db_path = custom_key_database_path; - } - - // set custom gnupg path - if (use_custom_gnupg_install_path) { - args.custom_gpgconf = true; - args.custom_gpgconf_path = - custom_gnupg_install_fs_path.u8string(); - } - - args.offline_mode = forbid_all_gnupg_connection; - args.auto_import_missing_key = auto_import_missing_key; - args.use_pinentry = use_pinentry_as_password_input_dialog; - - return ConvertToChannelObjectPtr<>( - SecureCreateUniqueObject<GpgContext>( - args, kGpgFrontendDefaultChannel)); - }); - - // exit if failed - if (!ctx.Good()) { - SPDLOG_ERROR("default gnupg context init error, abort"); - CoreSignalStation::GetInstance()->SignalBadGnupgEnv( - _("GpgME Context inilization failed")); - return -1; + if (args.load_default_gpg_context) { + // init ctx, also checking the basical env + auto& ctx = GpgFrontend::GpgContext::CreateInstance( + kGpgFrontendDefaultChannel, [=]() -> ChannelObjectPtr { + GpgFrontend::GpgContextInitArgs args; + + // set key database path + if (use_custom_key_database_path && + !custom_key_database_path.empty()) { + args.db_path = custom_key_database_path; + } + + // set custom gnupg path + if (use_custom_gnupg_install_path) { + args.custom_gpgconf = true; + args.custom_gpgconf_path = + custom_gnupg_install_fs_path.u8string(); + } + + args.offline_mode = forbid_all_gnupg_connection; + args.auto_import_missing_key = auto_import_missing_key; + args.use_pinentry = use_pinentry_as_password_input_dialog; + + return ConvertToChannelObjectPtr<>( + SecureCreateUniqueObject<GpgContext>( + args, kGpgFrontendDefaultChannel)); + }); + + // exit if failed + if (!ctx.Good()) { + SPDLOG_ERROR("default gnupg context init error, abort"); + CoreSignalStation::GetInstance()->SignalBadGnupgEnv( + _("GpgME Context inilization failed")); + return -1; + } + Module::UpsertRTValue("core", "env.state.ctx", std::string{"1"}); } - Module::UpsertRTValue("core", "env.state.ctx", std::string{"1"}); // if gnupg-info-gathering module activated if (args.gather_external_gnupg_info && @@ -371,8 +373,7 @@ void InitGpgFrontendCore(CoreInitArgs args) { // announce that all checkings were finished SPDLOG_INFO( "all env checking finished, including gpgme, " - "ctx and " - "gnupg"); + "ctx and gnupg"); Module::UpsertRTValue("core", "env.state.all", std::string{"1"}); } @@ -382,10 +383,12 @@ void InitGpgFrontendCore(CoreInitArgs args) { Module::UpsertRTValue("core", "env.state.all", std::string{"1"}); } - if (!GpgKeyGetter::GetInstance().FlushKeyCache()) { - CoreSignalStation::GetInstance()->SignalBadGnupgEnv( - _("Gpg Key Detabase inilization failed")); - }; + if (args.load_default_gpg_context) { + if (!GpgKeyGetter::GetInstance().FlushKeyCache()) { + CoreSignalStation::GetInstance()->SignalBadGnupgEnv( + _("Gpg Key Detabase inilization failed")); + }; + } SPDLOG_INFO( "basic env checking finished, including gpgme, ctx, and key " "infos"); diff --git a/src/core/GpgCoreInit.h b/src/core/GpgCoreInit.h index 33d40e58..e1458e95 100644 --- a/src/core/GpgCoreInit.h +++ b/src/core/GpgCoreInit.h @@ -34,6 +34,7 @@ namespace GpgFrontend { struct CoreInitArgs { bool gather_external_gnupg_info; + bool load_default_gpg_context; }; /** diff --git a/src/core/function/SecureMemoryAllocator.h b/src/core/function/SecureMemoryAllocator.h index 593172c5..b938429e 100644 --- a/src/core/function/SecureMemoryAllocator.h +++ b/src/core/function/SecureMemoryAllocator.h @@ -57,6 +57,6 @@ struct SecureObjectDeleter { }; template <typename T> -using UniquePtrWithSecureDeleter = std::unique_ptr<T, SecureObjectDeleter<T>>; +using SecureUniquePtr = std::unique_ptr<T, SecureObjectDeleter<T>>; } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/gpg/GpgBasicOperator.cpp b/src/core/function/gpg/GpgBasicOperator.cpp index 63def65f..7b6d45b2 100644 --- a/src/core/function/gpg/GpgBasicOperator.cpp +++ b/src/core/function/gpg/GpgBasicOperator.cpp @@ -43,8 +43,7 @@ void GpgFrontend::GpgBasicOperator::Encrypt(KeyListPtr keys, ConstBypeArrayRef in_buffer, const GpgOperationCallback& cb) { RunGpgOperaAsync( - [&](const DataObjectPtr& data_object) -> GpgError { - SPDLOG_DEBUG("key size: {}", keys->size()); + [=](const DataObjectPtr& data_object) -> GpgError { std::vector<gpgme_key_t> recipients(keys->size() + 1); for (const auto& key : *keys) { diff --git a/src/core/function/gpg/GpgContext.cpp b/src/core/function/gpg/GpgContext.cpp index 20ccf70f..0477b00a 100644 --- a/src/core/function/gpg/GpgContext.cpp +++ b/src/core/function/gpg/GpgContext.cpp @@ -200,7 +200,7 @@ class GpgContext::Impl : public SingletonFunctionObject<GpgContext::Impl> { db_path_c_str = nullptr; } - auto err = gpgme_ctx_set_engine_info(ctx, GPGME_PROTOCOL_OpenPGP, + auto err = gpgme_ctx_set_engine_info(ctx, gpgme_get_protocol(ctx), app_path_c_str, db_path_c_str); assert(CheckGpgError(err) == GPG_ERR_NO_ERROR); @@ -297,11 +297,11 @@ class GpgContext::Impl : public SingletonFunctionObject<GpgContext::Impl> { assert(p_ctx != nullptr); ctx_ref_ = p_ctx; - // if (!common_ctx_initialize(ctx_ref_, args)) { - // return false; - // } + if (!common_ctx_initialize(ctx_ref_, args)) { + return false; + } - // gpgme_set_armor(ctx_ref_, 1); + gpgme_set_armor(ctx_ref_, 1); return true; } }; diff --git a/src/core/function/gpg/GpgContext.h b/src/core/function/gpg/GpgContext.h index 5f6b5b86..ead0c89b 100644 --- a/src/core/function/gpg/GpgContext.h +++ b/src/core/function/gpg/GpgContext.h @@ -73,6 +73,6 @@ class GPGFRONTEND_CORE_EXPORT GpgContext private: class Impl; - UniquePtrWithSecureDeleter<Impl> p_; + SecureUniquePtr<Impl> p_; }; } // namespace GpgFrontend diff --git a/src/init.cpp b/src/init.cpp index 02e90836..56ed1d5d 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -188,6 +188,8 @@ void InitGlobalBasicalEnv(const GFCxtWPtr &p_ctx) { CoreInitArgs core_init_args; core_init_args.gather_external_gnupg_info = ctx->gather_external_gnupg_info; + core_init_args.load_default_gpg_context = ctx->load_default_gpg_context; + // then load core InitGpgFrontendCore(core_init_args); } diff --git a/src/main.cpp b/src/main.cpp index 0ee4f200..d75ba711 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -100,6 +100,7 @@ auto main(int argc, char* argv[]) -> int { if (vm.count("test") != 0U) { ctx->gather_external_gnupg_info = false; + ctx->load_default_gpg_context = false; InitGlobalBasicalEnv(p_ctx); return RunTest(ctx); } diff --git a/src/test/GpgFrontendTest.cpp b/src/test/GpgFrontendTest.cpp index 18157a00..0075c113 100644 --- a/src/test/GpgFrontendTest.cpp +++ b/src/test/GpgFrontendTest.cpp @@ -107,8 +107,6 @@ void ConfigureGpgContext() { std::filesystem::create_directory(db_path); } - SPDLOG_DEBUG("DEBUG--------<"); - GpgContext::CreateInstance( kGpgFrontendDefaultChannel, [&]() -> ChannelObjectPtr { GpgContextInitArgs args; @@ -119,8 +117,6 @@ void ConfigureGpgContext() { return ConvertToChannelObjectPtr<>(SecureCreateUniqueObject<GpgContext>( args, kGpgFrontendDefaultChannel)); }); - - SPDLOG_DEBUG("DEBUG-------->"); } auto ExecuteAllTestCase(GpgFrontendContext args) -> int { diff --git a/src/test/core/GpgCoreTest.cpp b/src/test/core/GpgCoreTest.cpp index f2616573..ecba016f 100644 --- a/src/test/core/GpgCoreTest.cpp +++ b/src/test/core/GpgCoreTest.cpp @@ -30,6 +30,7 @@ #include "core/function/gpg/GpgKeyImportExporter.h" #include "core/utils/IOUtils.h" +#include "core/utils/MemoryUtils.h" #include "spdlog/spdlog.h" namespace GpgFrontend::Test { @@ -43,9 +44,9 @@ void GpgCoreTest::import_private_keys(const libconfig::Setting& root) { private_key.lookupValue("filename", filename); auto data_file_path = data_path_ / filename; std::string data = ReadAllDataInFile(data_file_path.string()); - auto secret_key_copy = std::make_unique<std::string>(data); + auto secret_key_copy = SecureCreateSharedObject<std::string>(data); GpgKeyImportExporter::GetInstance(kGpgFrontendDefaultChannel) - .ImportKey(std::move(secret_key_copy)); + .ImportKey(secret_key_copy); } } } diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt index 68307dab..79cd322b 100644 --- a/third_party/CMakeLists.txt +++ b/third_party/CMakeLists.txt @@ -31,7 +31,10 @@ add_subdirectory(json EXCLUDE_FROM_ALL) add_subdirectory(spdlog EXCLUDE_FROM_ALL) set(MI_SECURE ON) -set(MI_TRACK_VALGRIND ON) +if(${CMAKE_BUILD_TYPE} STREQUAL "Debug") + # set(MI_TRACK_VALGRIND ON) + set(MI_TRACK_ASAN ON) +endif() add_subdirectory(mimalloc EXCLUDE_FROM_ALL) set(INSTALL_GTEST OFF) |