From 8c745226eac926edfd8d0b6b9170f1aeae6ec8ce Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Wed, 11 Oct 2023 15:43:10 -0400
Subject: [PATCH] debug: allow easier enabling of ASAN for libjami and contrib
 builds

Change-Id: I0e142f271fa0db3c3d4af1e57919ef1f65beb8c2
---
 configure.ac         | 10 ++++++++++
 contrib/bootstrap    |  8 +++++++-
 contrib/src/main.mak |  6 ++++++
 3 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index 045f0f46e2..a693c7e305 100644
--- a/configure.ac
+++ b/configure.ac
@@ -40,6 +40,16 @@ AS_IF([test "x$enable_debug" = "xyes"],
   [DAEMONCFLAGS+=" -DNDEBUG=1 -O3"
    DAEMONCXXFLAGS+=" -DNDEBUG=1 -O3 -Wno-deprecated"])
 
+dnl Build using address sanitizer
+AC_ARG_ENABLE([asan],
+  AS_HELP_STRING([--enable-asan],
+    [Enable address sanitization]))
+
+AS_IF([test "x$enable_asan" = "xyes"],
+  [DAEMONCXXFLAGS+=" -fsanitize=address -fno-omit-frame-pointer"
+   DAEMONLDFLAGS+=" -fsanitize=address"
+   AC_MSG_NOTICE([WARNING: Address sanitization enabled for libjami])], [])
+
 AC_ARG_ENABLE([fuzzing],
   AS_HELP_STRING([--enable-fuzzing],
     [Build fuzzing tools]))
diff --git a/contrib/bootstrap b/contrib/bootstrap
index eb7eaac6a4..28672daf11 100755
--- a/contrib/bootstrap
+++ b/contrib/bootstrap
@@ -26,8 +26,9 @@ usage()
 	echo "  --prefix=PREFIX     install files in PREFIX"
 	echo "  --no-checksums      don't verify checksums (allows to replace tarballs)"
 	echo "  --disable-downloads don't download packages from the internet"
-        echo '  --cache-dir=DIR     the directory where contrib tarballs are cached'
+    echo "  --cache-dir=DIR     the directory where contrib tarballs are cached"
 	echo "  --enable-debug      build with debug symbol and extra checks (disabled by default)"
+	echo "  --enable-asan       build with address sanitizer (disabled by default)"
 	echo "  --disable-FOO       configure to not build package FOO"
 	echo "  --enable-FOO        configure to build package FOO"
 }
@@ -39,6 +40,7 @@ PKGS_ENABLE=
 PKGS_DISABLE=
 CACHE_BUILD=
 CONF_TARBALLS=
+ENABLE_ASAN=
 
 if test ! -f "../src/main.mak"
 then
@@ -72,6 +74,9 @@ do
 		--enable-debug)
 			ENABLE_DEBUG=1
 			;;
+		--enable-asan)
+			ENABLE_ASAN=1
+			;;
 		--host=*)
 			HOST="${1#--host=}"
 			;;
@@ -136,6 +141,7 @@ CONF_TARBALLS := $CONF_TARBALLS
 CACHE_DIR := $CACHE_DIR
 CACHE_BUILD := $CACHE_BUILD
 ENABLE_DEBUG := $ENABLE_DEBUG
+ENABLE_ASAN := $ENABLE_ASAN
 EOF
 
 add_make()
diff --git a/contrib/src/main.mak b/contrib/src/main.mak
index 1d3c23f790..11bd9ba1db 100644
--- a/contrib/src/main.mak
+++ b/contrib/src/main.mak
@@ -190,6 +190,12 @@ EXTRA_COMMON += -m32
 endif
 endif
 
+ifdef ENABLE_ASAN
+$(info Address sanitization enabled for contrib builds)
+EXTRA_COMMON += -fsanitize=address
+EXTRA_LDFLAGS += -fsanitize=address
+endif
+
 ifdef ENABLE_DEBUG
 EXTRA_COMMON += -g -fno-omit-frame-pointer
 else
-- 
GitLab