aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2024-07-25 11:31:16 +0000
committersaturneric <[email protected]>2024-07-25 11:31:16 +0000
commit6728888b87806328b05f30b15825d05ca34026bc (patch)
tree2376e198984f1770cd3e240469e550fb4fef101d
parentfeat: update mimalloc to v2.1.7 (diff)
downloadGpgFrontend-6728888b87806328b05f30b15825d05ca34026bc.tar.gz
GpgFrontend-6728888b87806328b05f30b15825d05ca34026bc.zip
feat: add an ASAN option to enable a memory debugging mode
-rw-r--r--CMakeLists.txt16
-rw-r--r--src/core/CMakeLists.txt16
-rw-r--r--src/core/function/SecureMemoryAllocator.cpp18
-rw-r--r--third_party/CMakeLists.txt26
4 files changed, 42 insertions, 34 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8e21e33f..49cd9853 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -68,7 +68,7 @@ option(GPGFRONTEND_BUILD_TYPE_STABLE
option(GPGFRONTEND_BUILD_TYPE_SDK "Generate a usable SDK" OFF)
option(GPGFRONTEND_QT5_BUILD "Swith to Qt5 building mode" OFF)
option(GPGFRONTEND_GENERATE_LINUX_INSTALL_SOFTWARE "Generate an installable version" OFF)
-
+option(GPGFRONTEND_ENABLE_ASAN "Enable ASAN" OFF)
option(GPGFRONTEND_XCODE_TEAM_ID "GpgFrontend Apple Team ID" "NONE")
option(GPGFRONTEND_XOCDE_CODE_SIGN_IDENTITY "GpgFrontend Signing Certificate" "NONE")
@@ -207,6 +207,20 @@ if (CMAKE_BUILD_TYPE STREQUAL "Release" AND APPLE)
unset(GPGFRONTEND_BUILD_CONFIG)
endif ()
+# if enable ASAN
+if(GPGFRONTEND_ENABLE_ASAN)
+
+ # check compiler
+ if(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
+ message(FATAL_ERROR "Use GPGFRONTEND_ENABLE_ASAN only when using the clang compiler.")
+ endif()
+
+ add_compile_options(-fsanitize=address)
+ add_link_options(-fsanitize=address)
+
+ set(ENABLE_ASAN 1)
+endif()
+
set(AppName GpgFrontend)
# Get Git Information
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 6ae14968..a5be5827 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -44,15 +44,13 @@ add_library(gpgfrontend_core SHARED ${CORE_SOURCE})
set(_export_file "${CMAKE_CURRENT_SOURCE_DIR}/GpgFrontendCoreExport.h")
generate_export_header(gpgfrontend_core EXPORT_FILE_NAME "${_export_file}")
-if(NOT APPLE)
- target_link_libraries(gpgfrontend_core PUBLIC mimalloc)
- if(MINGW)
- set_target_properties(mimalloc
- PROPERTIES
- LIBRARY_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}"
- RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}"
- )
- endif()
+target_link_libraries(gpgfrontend_core PUBLIC mimalloc)
+if(MINGW)
+ set_target_properties(mimalloc
+ PROPERTIES
+ LIBRARY_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}"
+ RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}"
+ )
endif()
# qt-aes
diff --git a/src/core/function/SecureMemoryAllocator.cpp b/src/core/function/SecureMemoryAllocator.cpp
index fccd09fe..f854f816 100644
--- a/src/core/function/SecureMemoryAllocator.cpp
+++ b/src/core/function/SecureMemoryAllocator.cpp
@@ -28,36 +28,20 @@
#include "SecureMemoryAllocator.h"
-#if !defined(MACOS) && defined(DEBUG)
#include <mimalloc.h>
-#endif
namespace GpgFrontend {
auto SecureMemoryAllocator::Allocate(std::size_t size) -> void* {
-#if !defined(MACOS) && defined(DEBUG)
auto* addr = mi_malloc(size);
-#else
- auto* addr = malloc(size);
-#endif
return addr;
}
auto SecureMemoryAllocator::Reallocate(void* ptr, std::size_t size) -> void* {
-#if !defined(MACOS) && defined(DEBUG)
- auto* addr = mi_realloc(ptr, size);
-#else
auto* addr = realloc(ptr, size);
-#endif
return addr;
}
-void SecureMemoryAllocator::Deallocate(void* p) {
-#if !defined(MACOS) && defined(DEBUG)
- mi_free(p);
-#else
- free(p);
-#endif
-}
+void SecureMemoryAllocator::Deallocate(void* p) { mi_free(p); }
} // namespace GpgFrontend \ No newline at end of file
diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt
index 534d71da..8752a291 100644
--- a/third_party/CMakeLists.txt
+++ b/third_party/CMakeLists.txt
@@ -33,15 +33,27 @@ if (MINGW)
add_subdirectory(libarchive EXCLUDE_FROM_ALL)
endif()
-# not working at macOS
-if (NOT APPLE)
- set(MI_SECURE ON)
- if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
- # set(MI_TRACK_VALGRIND ON)
- set(MI_TRACK_ASAN ON)
+set(MI_SECURE ON)
+
+# fix the segment fault issue on M1 chip platform
+# refer to https://github.com/microsoft/mimalloc/issues/343
+if(APPLE)
+ set(MI_OSX_ZONE ON)
+ set(MI_OSX_INTERPOSE OFF)
+endif()
+
+# ASAN checking
+if(${CMAKE_BUILD_TYPE} STREQUAL "Debug" AND ENABLE_ASAN)
+
+ if(APPLE)
+ set(MI_OVERRIDE OFF)
endif()
- add_subdirectory(mimalloc EXCLUDE_FROM_ALL)
+
+ set(MI_TRACK_VALGRIND ON)
+ set(MI_TRACK_ASAN ON)
endif()
+add_subdirectory(mimalloc EXCLUDE_FROM_ALL)
+
set(INSTALL_GTEST OFF)
add_subdirectory(googletest EXCLUDE_FROM_ALL) \ No newline at end of file