Skip to content

Commit

Permalink
build: add option for sanitizers
Browse files Browse the repository at this point in the history
  • Loading branch information
div72 committed Oct 15, 2022
1 parent d0882c7 commit e4bc935
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
36 changes: 36 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ AC_ARG_ENABLE([debug],
[enable_debug=$enableval],
[enable_debug=no])

dnl Enable different -fsanitize options
AC_ARG_WITH([sanitizers],
[AS_HELP_STRING([--with-sanitizers],
[comma separated list of extra sanitizers to build with (default is none enabled)])],
[use_sanitizers=$withval])

# Turn warnings into errors
AC_ARG_ENABLE([werror],
[AS_HELP_STRING([--enable-werror],
Expand All @@ -224,6 +230,33 @@ if test "$enable_debug" = "yes"; then
fi
fi

if test "$use_sanitizers" != ""; then
dnl First check if the compiler accepts flags. If an incompatible pair like
dnl -fsanitize=address,thread is used here, this check will fail. This will also
dnl fail if a bad argument is passed, e.g. -fsanitize=undfeined
AX_CHECK_COMPILE_FLAG(
[-fsanitize=$use_sanitizers],
[SANITIZER_CXXFLAGS="-fsanitize=$use_sanitizers"],
[AC_MSG_ERROR([compiler did not accept requested flags])])

dnl Some compilers (e.g. GCC) require additional libraries like libasan,
dnl libtsan, libubsan, etc. Make sure linking still works with the sanitize
dnl flag. This is a separate check so we can give a better error message when
dnl the sanitize flags are supported by the compiler but the actual sanitizer
dnl libs are missing.
AX_CHECK_LINK_FLAG(
[-fsanitize=$use_sanitizers],
[SANITIZER_LDFLAGS="-fsanitize=$use_sanitizers"],
[AC_MSG_ERROR([linker did not accept requested flags, you are missing required libraries])],
[],
[AC_LANG_PROGRAM([[
#include <cstdint>
#include <cstddef>
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { return 0; }
__attribute__((weak)) // allow for libFuzzer linking
]],[[]])])
fi

ERROR_CXXFLAGS=
if test "$enable_werror" = "yes"; then
if test "$CXXFLAG_WERROR" = ""; then
Expand Down Expand Up @@ -1236,6 +1269,8 @@ AC_SUBST(HARDENED_CPPFLAGS)
AC_SUBST(HARDENED_LDFLAGS)
AC_SUBST(PIC_FLAGS)
AC_SUBST(PIE_FLAGS)
AC_SUBST(SANITIZER_CXXFLAGS)
AC_SUBST(SANITIZER_LDFLAGS)
AC_SUBST(SSE42_CXXFLAGS)
AC_SUBST(SSE41_CXXFLAGS)
AC_SUBST(AVX2_CXXFLAGS)
Expand Down Expand Up @@ -1328,6 +1363,7 @@ echo " with zmq = $use_zmq"
echo " with test = $use_tests"
echo " with bench = $use_bench"
echo " with upnp = $use_upnp"
echo " sanitizers = $use_sanitizers"
echo " debug enabled = $enable_debug"
echo " werror = $enable_werror"
echo
Expand Down
4 changes: 2 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ print-%: FORCE

DIST_SUBDIRS = univalue secp256k1

AM_LDFLAGS = ${libcurl_LIBS} $(PTHREAD_CFLAGS) $(LIBTOOL_LDFLAGS) $(HARDENED_LDFLAGS)
AM_CXXFLAGS = $(HARDENED_CXXFLAGS) $(ERROR_CXXFLAGS)
AM_LDFLAGS = ${libcurl_LIBS} $(PTHREAD_CFLAGS) $(LIBTOOL_LDFLAGS) $(HARDENED_LDFLAGS) $(SANITIZER_LDFLAGS)
AM_CXXFLAGS = $(HARDENED_CXXFLAGS) $(ERROR_CXXFLAGS) $(SANITIZER_CXXFLAGS)
AM_CPPFLAGS = ${libcurl_CFLAGS} $(HARDENED_CPPFLAGS) -DSTATICLIB -DCURL_STATICLIB -DMINIUPNP_STATICLIB -DZIP_STATIC -DNN_STATIC_LIB
EXTRA_LIBRARIES =

Expand Down

0 comments on commit e4bc935

Please sign in to comment.