Skip to content

Commit e3af0df

Browse files
authored
[asan] add address sanitizer support for syncd (sonic-net#1020)
add --enable-asan for syncd compilation add SIGTERM handler that calls __lsan_do_leak_check() to generate a report
1 parent 4b2638c commit e3af0df

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

configure.ac

+20
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,26 @@ else
9292
AM_CONDITIONAL(ARCH64, test `getconf LONG_BIT` = "64")
9393
fi
9494

95+
AC_ARG_ENABLE(asan,
96+
[ --enable-asan Compile with address sanitizer],
97+
[case "${enableval}" in
98+
yes) asan_enabled=true ;;
99+
no) asan_enabled=false ;;
100+
*) AC_MSG_ERROR(bad value ${enableval} for --enable-asan) ;;
101+
esac],[asan_enabled=false])
102+
103+
if test "x$asan_enabled" = "xtrue"; then
104+
CFLAGS_ASAN+=" -fsanitize=address"
105+
CFLAGS_ASAN+=" -DASAN_ENABLED"
106+
CFLAGS_ASAN+=" -ggdb -fno-omit-frame-pointer -U_FORTIFY_SOURCE"
107+
AC_SUBST(CFLAGS_ASAN)
108+
109+
LDFLAGS_ASAN+=" -lasan"
110+
AC_SUBST(LDFLAGS_ASAN)
111+
fi
112+
113+
AM_CONDITIONAL(ASAN_ENABLED, test x$asan_enabled = xtrue)
114+
95115
AC_PATH_PROGS(SWIG, [swig3.0 swig])
96116

97117
CXXFLAGS_COMMON=""

debian/rules

+6-1
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,14 @@ binary-syncd-vs:
6767
# dh_auto_configure -- \
6868
# -DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH)
6969

70+
configure_opts =
71+
ifeq ($(ENABLE_ASAN), y)
72+
configure_opts += --enable-asan
73+
endif
74+
7075
override_dh_auto_configure:
7176
./autogen.sh
72-
dh_auto_configure -- $(DEB_CONFIGURE_EXTRA_FLAGS) $(shell cat /tmp/syncd-build) ${SWSS_COMMON_CONFIG}
77+
dh_auto_configure -- $(DEB_CONFIGURE_EXTRA_FLAGS) $(shell cat /tmp/syncd-build) ${SWSS_COMMON_CONFIG} $(configure_opts)
7378

7479
override_dh_install:
7580
dh_install

syncd/Asan.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "swss/logger.h"
2+
3+
#include <csignal>
4+
#include <sanitizer/lsan_interface.h>
5+
6+
static void sigterm_handler(int signo)
7+
{
8+
SWSS_LOG_ENTER();
9+
10+
__lsan_do_leak_check();
11+
signal(signo, SIG_DFL);
12+
raise(signo);
13+
}
14+
15+
__attribute__((constructor))
16+
static void asan_init()
17+
{
18+
SWSS_LOG_ENTER();
19+
20+
if (signal(SIGTERM, sigterm_handler) == SIG_ERR)
21+
{
22+
SWSS_LOG_ERROR("failed to setup SIGTERM action");
23+
exit(1);
24+
}
25+
}

syncd/Makefile.am

+5-2
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ libSyncd_a_CPPFLAGS = $(CODE_COVERAGE_CPPFLAGS)
5656
libSyncd_a_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) $(CODE_COVERAGE_CXXFLAGS)
5757

5858
syncd_SOURCES = main.cpp
59+
if ASAN_ENABLED
60+
syncd_SOURCES += Asan.cpp
61+
endif
5962
syncd_CPPFLAGS = $(CODE_COVERAGE_CPPFLAGS)
60-
syncd_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) $(CODE_COVERAGE_CXXFLAGS)
63+
syncd_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) $(CODE_COVERAGE_CXXFLAGS) $(CFLAGS_ASAN)
6164
syncd_LDADD = libSyncd.a $(top_srcdir)/lib/libSaiRedis.a -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta \
6265
-ldl -lhiredis -lswsscommon $(SAILIB) -lpthread -lzmq $(CODE_COVERAGE_LIBS)
63-
syncd_LDFLAGS = -rdynamic
66+
syncd_LDFLAGS = $(LDFLAGS_ASAN) -rdynamic
6467

6568
if SAITHRIFT
6669
libSyncd_a_CXXFLAGS += -DSAITHRIFT=yes

0 commit comments

Comments
 (0)