forked from sonic-net/sonic-buildimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add possibility to compile applications with address sanitizer. (soni…
…c-net#2186) **What I did** To add a possibility to compile SONiC applications with address sanitizer (ASAN). > ASAN is a memory error detector for C/C++. It finds: > - Use after free (dangling pointer dereference) > - Heap buffer overflow > - Stack buffer overflow > - Global buffer overflow > - Use after return > - Use after the scope > - Initialization order bugs > - Memory leaks Add SIGTERM handling as a trigger to run a memory leak checker. **Why I did it** To add a possibility to detect memory errors in applications. **How I verified it** Compile SWSS with **--enable-asan** option. Install applications to DUT. Run tests that trigger different kinds of memory usage inside of applications. Stop applications with **SIGTERM** signal to trigger ASAN report generation. Reports will be placed in **/var/log/asan/** directory.
- Loading branch information
1 parent
f06114c
commit 62916a1
Showing
18 changed files
with
272 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
INCLUDES = -I $(top_srcdir)/lib -I $(top_srcdir) -I $(top_srcdir)/warmrestart -I $(top_srcdir)/cfgmgr | ||
|
||
bin_PROGRAMS = gearsyncd | ||
bin_PROGRAMS = gearsyncd | ||
|
||
if DEBUG | ||
DBGFLAGS = -ggdb -DDEBUG | ||
else | ||
DBGFLAGS = -g | ||
endif | ||
|
||
gearsyncd_SOURCES = $(top_srcdir)/lib/gearboxutils.cpp gearsyncd.cpp gearparserbase.cpp gearboxparser.cpp phyparser.cpp $(top_srcdir)/cfgmgr/shellcmd.h | ||
gearsyncd_SOURCES = $(top_srcdir)/lib/gearboxutils.cpp gearsyncd.cpp gearparserbase.cpp gearboxparser.cpp phyparser.cpp $(top_srcdir)/cfgmgr/shellcmd.h | ||
|
||
gearsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(COV_CFLAGS) $(ASAN_CFLAGS) | ||
gearsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(COV_CFLAGS) $(CFLAGS_ASAN) | ||
|
||
gearsyncd_LDADD = -lnl-3 -lnl-route-3 -lswsscommon $(COV_LDFLAGS) $(ASAN_LDFLAGS) | ||
gearsyncd_LDADD = $(LDFLAGS_ASAN) -lnl-3 -lnl-route-3 -lswsscommon $(COV_LDFLAGS) | ||
|
||
if GCOV_ENABLED | ||
gearsyncd_LDADD += -lgcovpreload | ||
endif | ||
|
||
if ASAN_ENABLED | ||
gearsyncd_SOURCES += $(top_srcdir)/lib/asan.cpp | ||
endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <unistd.h> | ||
#include <signal.h> | ||
#include <sanitizer/lsan_interface.h> | ||
|
||
#include <logger.h> | ||
|
||
static void swss_asan_sigterm_handler(int signo) | ||
{ | ||
SWSS_LOG_ENTER(); | ||
|
||
__lsan_do_leak_check(); | ||
|
||
struct sigaction sigact; | ||
if (sigaction(SIGTERM, NULL, &sigact)) | ||
{ | ||
SWSS_LOG_ERROR("failed to get current SIGTERM action handler"); | ||
_exit(EXIT_FAILURE); | ||
} | ||
|
||
// Check the currently set signal handler. | ||
// If it is ASAN's signal handler this means that the application didn't set its own handler. | ||
// To preserve default behavior set the default signal handler and raise the signal to trigger its execution. | ||
// Otherwise, the application installed its own signal handler. | ||
// In this case, just trigger a leak check and do nothing else. | ||
if (sigact.sa_handler == swss_asan_sigterm_handler) { | ||
sigemptyset(&sigact.sa_mask); | ||
sigact.sa_flags = 0; | ||
sigact.sa_handler = SIG_DFL; | ||
if (sigaction(SIGTERM, &sigact, NULL)) | ||
{ | ||
SWSS_LOG_ERROR("failed to setup SIGTERM action handler"); | ||
_exit(EXIT_FAILURE); | ||
} | ||
|
||
raise(signo); | ||
} | ||
} | ||
|
||
__attribute__((constructor)) | ||
static void swss_asan_init() | ||
{ | ||
SWSS_LOG_ENTER(); | ||
|
||
struct sigaction sigact = {}; | ||
sigact.sa_handler = swss_asan_sigterm_handler; | ||
if (sigaction(SIGTERM, &sigact, NULL)) | ||
{ | ||
SWSS_LOG_ERROR("failed to setup SIGTERM action handler"); | ||
exit(EXIT_FAILURE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.