From 89815cd38382dd119160c0c6e429266dff758e87 Mon Sep 17 00:00:00 2001
From: Sergey Bronnikov <sergeyb@tarantool.org>
Date: Mon, 30 Sep 2024 15:30:52 +0300
Subject: [PATCH] cmake: fix build with GCC's AddressSanitizer

GNU GCC compiler has AddressSanitizer support since 4.8.0 [1],
but it was unsupported in tarantool's build. The patch
fixes a build by GNU GCC with enabled AddressSanitizer.

1. https://gcc.gnu.org/gcc-4.8/changes.html

NO_CHANGELOG=build
NO_DOC=build
NO_TEST=build

(cherry picked from commit ef91f92a22c6d7910ecdd00ab14da359343a2ec2)
---
 cmake/CheckDependencies.cmake |  3 +++
 cmake/profile.cmake           | 17 +++++++++++++----
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/cmake/CheckDependencies.cmake b/cmake/CheckDependencies.cmake
index aee1d561b2..e0baa6298c 100644
--- a/cmake/CheckDependencies.cmake
+++ b/cmake/CheckDependencies.cmake
@@ -34,6 +34,9 @@ elseif(UNIX)
     if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND ENABLE_ASAN)
         set(ALLOWLIST ${ALLOWLIST} libresolv)
     endif()
+    if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND ENABLE_ASAN)
+        set(ALLOWLIST ${ALLOWLIST} libasan)
+    endif()
 else()
     message(FATAL_ERROR "Unknown platform")
 endif()
diff --git a/cmake/profile.cmake b/cmake/profile.cmake
index 16492efb16..23fb46664e 100644
--- a/cmake/profile.cmake
+++ b/cmake/profile.cmake
@@ -74,16 +74,25 @@ endif()
 
 option(ENABLE_ASAN "Enable AddressSanitizer, a fast memory error detector based on compiler instrumentation" OFF)
 if (ENABLE_ASAN)
-    if (CMAKE_COMPILER_IS_GNUCC)
+    # AddressSanitizer has been added to GCC since version 4.8.0,
+    # see https://gcc.gnu.org/gcc-4.8/changes.html.
+    if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8.0)
         message(FATAL_ERROR
             "\n"
-            " Tarantool does not support GCC's AddressSanitizer. Use clang:\n"
+            " GCC has AddressSanitizer support since 4.8.0. Update GCC version\n"
+            " or use Clang:\n"
             " $ git clean -xfd; git submodule foreach --recursive git clean -xfd\n"
             " $ CC=clang CXX=clang++ cmake . <...> -DENABLE_ASAN=ON && make -j\n"
             "\n")
     endif()
 
-    set(CMAKE_REQUIRED_FLAGS "-fsanitize=address -fsanitize-blacklist=${PROJECT_SOURCE_DIR}/asan/asan.supp")
+    set(ASAN_FLAGS "-fsanitize=address")
+    if (CMAKE_C_COMPILER STREQUAL "Clang")
+        # Bug 61978 - implement blacklist for sanitizer,
+        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61978
+        set(ASAN_FLAGS "${ASAN_FLAGS} -fsanitize-blacklist=${PROJECT_SOURCE_DIR}/asan/asan.supp")
+    endif()
+    set(CMAKE_REQUIRED_FLAGS ${ASAN_FLAGS})
     check_c_source_compiles("int main(void) {
         #include <sanitizer/asan_interface.h>
         void *x;
@@ -106,5 +115,5 @@ if (ENABLE_ASAN)
         message(FATAL_ERROR "Cannot enable AddressSanitizer")
     endif()
 
-    add_compile_flags("C;CXX" -fsanitize=address -fsanitize-blacklist=${PROJECT_SOURCE_DIR}/asan/asan.supp)
+    add_compile_flags("C;CXX" ${ASAN_FLAGS})
 endif()
-- 
GitLab