From e3af0df8983b3136e2e2581511cd5c3dc552a039 Mon Sep 17 00:00:00 2001 From: Yakiv Huryk <62013282+Yakiv-Huryk@users.noreply.github.com> Date: Tue, 29 Mar 2022 14:33:19 +0300 Subject: [PATCH] [asan] add address sanitizer support for syncd (#1020) add --enable-asan for syncd compilation add SIGTERM handler that calls __lsan_do_leak_check() to generate a report --- configure.ac | 20 ++++++++++++++++++++ debian/rules | 7 ++++++- syncd/Asan.cpp | 25 +++++++++++++++++++++++++ syncd/Makefile.am | 7 +++++-- 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 syncd/Asan.cpp 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/Asan.cpp b/syncd/Asan.cpp new file mode 100644 index 000000000..d3f0c95d5 --- /dev/null +++ b/syncd/Asan.cpp @@ -0,0 +1,25 @@ +#include "swss/logger.h" + +#include +#include + +static void sigterm_handler(int signo) +{ + SWSS_LOG_ENTER(); + + __lsan_do_leak_check(); + signal(signo, SIG_DFL); + raise(signo); +} + +__attribute__((constructor)) +static void asan_init() +{ + SWSS_LOG_ENTER(); + + if (signal(SIGTERM, sigterm_handler) == SIG_ERR) + { + SWSS_LOG_ERROR("failed to setup SIGTERM action"); + exit(1); + } +} diff --git a/syncd/Makefile.am b/syncd/Makefile.am index 1a7c63644..cea5baeff 100644 --- a/syncd/Makefile.am +++ b/syncd/Makefile.am @@ -56,11 +56,14 @@ libSyncd_a_CPPFLAGS = $(CODE_COVERAGE_CPPFLAGS) libSyncd_a_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) $(CODE_COVERAGE_CXXFLAGS) syncd_SOURCES = main.cpp +if ASAN_ENABLED +syncd_SOURCES += Asan.cpp +endif 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