From 98fc234e4b55e9df4cdf45eddbb517ac40b8f7a3 Mon Sep 17 00:00:00 2001 From: Yakiv Huryk Date: Thu, 10 Mar 2022 13:56:14 +0000 Subject: [PATCH] [asan] add address sanitizer support for syncd * add --enable-asan for syncd compilation * add SIGTERM handler that calls __lsan_do_leak_check() to generate a report Signed-off-by: Yakiv Huryk --- configure.ac | 20 ++++++++++++++++++++ debian/rules | 7 ++++++- syncd/Makefile.am | 4 ++-- syncd/main.cpp | 22 ++++++++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index 467422957..23da9c2bd 100644 --- a/configure.ac +++ b/configure.ac @@ -92,6 +92,26 @@ else AM_CONDITIONAL(ARCH64, test `getconf LONG_BIT` = "64") fi +AC_ARG_ENABLE(asan, +[ --enable-asan Compile with address sanitizer], +[case "${enableval}" in + yes) asan_enabled=true ;; + no) asan_enabled=false ;; + *) AC_MSG_ERROR(bad value ${enableval} for --enable-asan) ;; +esac],[asan_enabled=false]) + +if test "x$asan_enabled" = "xtrue"; then + CFLAGS_ASAN+=" -fsanitize=address" + CFLAGS_ASAN+=" -DASAN_ENABLED" + CFLAGS_ASAN+=" -ggdb -fno-omit-frame-pointer -U_FORTIFY_SOURCE" + AC_SUBST(CFLAGS_ASAN) + + LDFLAGS_ASAN+=" -lasan" + AC_SUBST(LDFLAGS_ASAN) +fi + +AM_CONDITIONAL(ASAN_ENABLED, test x$asan_enabled = xtrue) + AC_PATH_PROGS(SWIG, [swig3.0 swig]) CXXFLAGS_COMMON="" diff --git a/debian/rules b/debian/rules index f83631959..09f063dbd 100755 --- a/debian/rules +++ b/debian/rules @@ -67,9 +67,14 @@ binary-syncd-vs: # dh_auto_configure -- \ # -DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH) +configure_opts = +ifeq ($(ENABLE_ASAN), y) + configure_opts += --enable-asan +endif + override_dh_auto_configure: ./autogen.sh - dh_auto_configure -- $(DEB_CONFIGURE_EXTRA_FLAGS) $(shell cat /tmp/syncd-build) ${SWSS_COMMON_CONFIG} + dh_auto_configure -- $(DEB_CONFIGURE_EXTRA_FLAGS) $(shell cat /tmp/syncd-build) ${SWSS_COMMON_CONFIG} $(configure_opts) override_dh_install: dh_install diff --git a/syncd/Makefile.am b/syncd/Makefile.am index 1a7c63644..6fc3ec53e 100644 --- a/syncd/Makefile.am +++ b/syncd/Makefile.am @@ -57,10 +57,10 @@ libSyncd_a_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) $(CODE_COVER syncd_SOURCES = main.cpp syncd_CPPFLAGS = $(CODE_COVERAGE_CPPFLAGS) -syncd_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) $(CODE_COVERAGE_CXXFLAGS) +syncd_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) $(CODE_COVERAGE_CXXFLAGS) $(CFLAGS_ASAN) syncd_LDADD = libSyncd.a $(top_srcdir)/lib/libSaiRedis.a -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta \ -ldl -lhiredis -lswsscommon $(SAILIB) -lpthread -lzmq $(CODE_COVERAGE_LIBS) -syncd_LDFLAGS = -rdynamic +syncd_LDFLAGS = $(LDFLAGS_ASAN) -rdynamic if SAITHRIFT libSyncd_a_CXXFLAGS += -DSAITHRIFT=yes diff --git a/syncd/main.cpp b/syncd/main.cpp index fa1f38087..68f5694b0 100644 --- a/syncd/main.cpp +++ b/syncd/main.cpp @@ -1,10 +1,32 @@ #include "swss/logger.h" +#if defined(ASAN_ENABLED) +#include +#include + +void sigterm_handler(int signo) +{ + SWSS_LOG_ENTER(); + + __lsan_do_leak_check(); + signal(signo, SIG_DFL); + raise(signo); +} +#endif + int syncd_main(int argc, char **argv); int main(int argc, char **argv) { SWSS_LOG_ENTER(); +#if defined(ASAN_ENABLED) + if (signal(SIGTERM, sigterm_handler) == SIG_ERR) + { + SWSS_LOG_ERROR("failed to setup SIGTERM action"); + exit(1); + } +#endif + return syncd_main(argc, argv); }