diff --git a/CMakeLists.txt b/CMakeLists.txt index f889fb9b3..bbe2bb05d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -226,6 +226,17 @@ if(SEAL_USE_INTEL_HEXL) endif() endif() +# [option] SEAL_USE_ADDRESS_SANITIZER (default: OFF) +option(SEAL_USE_ADDRESS_SANITIZER_OPTION_STR "Compiles and links with Address Sanitizer" OFF) +option(SEAL_USE_ADDRESS_SANITIZER ${SEAL_USE_ADDRESS_SANITIZER_OPTION_STR} OFF) +message(STATUS "SEAL_USE_ADDRESS_SANITIZER: ${SEAL_USE_ADDRESS_SANITIZER}") + +if(SEAL_USE_ADDRESS_SANITIZER) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize=leak") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address -fsanitize=leak") + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -fsanitize=address -fsanitize=leak") +endif() + #################### # SEAL C++ library # #################### diff --git a/README.md b/README.md index 842485d19..3d2c6f2c1 100644 --- a/README.md +++ b/README.md @@ -398,6 +398,7 @@ The following options can be used with CMake to configure the build. The default | SEAL_BUILD_SEAL_C | ON / **OFF** | Build the C wrapper library SEAL_C. This is used by the C# wrapper and most users should have no reason to build it. | | SEAL_USE_CXX17 | **ON** / OFF | Set to `ON` to build Microsoft SEAL as C++17 for a positive performance impact. | | SEAL_USE_INTRIN | **ON** / OFF | Set to `ON` to use compiler intrinsics for improved performance. CMake will automatically detect which intrinsics are available and enable them accordingly. | +| SEAL_USE_ADDRESS_SANITIZER | ON / *OFF* | Set to `ON` to enable [AddressSanitizer](https://github.com/google/sanitizers) for diagnosing memory corruption issues As usual, these options can be passed to CMake with the `-D` flag. For example, one could run diff --git a/native/src/seal/util/clang.h b/native/src/seal/util/clang.h index 776e5147a..e22b1b018 100644 --- a/native/src/seal/util/clang.h +++ b/native/src/seal/util/clang.h @@ -17,6 +17,7 @@ #include #define SEAL_MALLOC(size) \ static_cast((((size)&63) == 0) ? ::aligned_alloc(64, (size)) : std::malloc((size))) +#define SEAL_FREE(ptr) std::free(ptr) #endif // Are intrinsics enabled? diff --git a/native/src/seal/util/defines.h b/native/src/seal/util/defines.h index c4305b122..39ee432ed 100644 --- a/native/src/seal/util/defines.h +++ b/native/src/seal/util/defines.h @@ -163,7 +163,7 @@ namespace seal // Allocate "size" bytes in memory and returns a seal_byte pointer // If SEAL_USE_ALIGNED_ALLOC is defined, use _aligned_malloc and ::aligned_alloc (or std::malloc) -// Use `new sealbytes[size]` as fallback +// Use `new seal_byte[size]` as fallback #ifndef SEAL_MALLOC #define SEAL_MALLOC(size) (new seal_byte[size]) #endif