Skip to content

Commit

Permalink
[asan] add address sanitizer support for syncd (#1020)
Browse files Browse the repository at this point in the history
add --enable-asan for syncd compilation
add SIGTERM handler that calls __lsan_do_leak_check() to generate a report
  • Loading branch information
Yakiv-Huryk authored Mar 29, 2022
1 parent 4b2638c commit e3af0df
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
20 changes: 20 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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=""
Expand Down
7 changes: 6 additions & 1 deletion debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions syncd/Asan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "swss/logger.h"

#include <csignal>
#include <sanitizer/lsan_interface.h>

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);
}
}
7 changes: 5 additions & 2 deletions syncd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e3af0df

Please sign in to comment.