Skip to content

Commit

Permalink
configure: Add --enable-sanitize-memory
Browse files Browse the repository at this point in the history
Add support for clang Memory Sanitizer [1], which detects the usage of
uninitialized values. While elfutils itself is already checked with
valgrind, checking code that depends on elfutils requires elfutils to
be built with MSan.

Unlike the other sanitizers, MSan needs to be configured fairly early,
since we need to drop -D_FORTIFY_SOURCE [2], -Wl,-z,defs and
--no-undefined.

[1] https://clang.llvm.org/docs/MemorySanitizer.html
[2] google/sanitizers#247

Signed-off-by: Ilya Leoshkevich <[email protected]>
  • Loading branch information
iii-i committed Feb 5, 2023
1 parent eccce54 commit cca57aa
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
24 changes: 24 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,29 @@ AC_SUBST([fpie_CFLAGS])

dso_LDFLAGS="-shared"

NO_UNDEFINED=-Wl,--no-undefined
AC_ARG_ENABLE([sanitize-memory],
AS_HELP_STRING([--enable-sanitize-memory],
[Use clang memory sanitizer]),
[use_msan=$enableval], [use_msan=no])
if test "$use_msan" = yes; then
old_CFLAGS="$CFLAGS"
old_CXXFLAGS="$CXXFLAGS"
old_LDFLAGS="$LDFLAGS"
# -fsanitize=memory is not compatible with -D_FORTIFY_SOURCE, -Wl,-z,defs and --no-undefined
CFLAGS="$CFLAGS -fsanitize=memory -fsanitize-memory-track-origins -D_FORTIFY_SOURCE=0"
CXXFLAGS="$CXXFLAGS -fsanitize=memory -fsanitize-memory-track-origins -D_FORTIFY_SOURCE=0"
LDFLAGS="-shared"
AC_LINK_IFELSE([AC_LANG_SOURCE([int main (int argc, char **argv) { return 0; }])], use_msan=yes, use_msan=no)
AS_IF([test "x$use_msan" == xyes],
ac_cv_zdefs=no NO_UNDEFINED=,
AC_MSG_WARN([clang memory sanitizer not available])
CFLAGS="$old_CFLAGS" CXXFLAGS="$old_CXXFLAGS")
LDFLAGS="$old_LDFLAGS"
fi
AC_SUBST(NO_UNDEFINED)
AM_CONDITIONAL(USE_MEMORY_SANITIZER, test "$use_msan" = yes)

ZDEFS_LDFLAGS="-Wl,-z,defs"
AC_CACHE_CHECK([whether gcc supports $ZDEFS_LDFLAGS], ac_cv_zdefs, [dnl
save_LDFLAGS="$LDFLAGS"
Expand Down Expand Up @@ -887,6 +910,7 @@ AC_MSG_NOTICE([
run all tests under valgrind : ${use_valgrind}
gcc undefined behaviour sanitizer : ${use_undefined}
gcc address sanitizer : ${use_address}
clang memory sanitizer : ${use_msan}
use rpath in tests : ${tests_use_rpath}
test biarch : ${utrace_cv_cc_biarch}
])
Expand Down
3 changes: 2 additions & 1 deletion debuginfod/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ endif
$(LIBDEBUGINFOD_SONAME): $(srcdir)/libdebuginfod.map $(libdebuginfod_so_LIBS)
$(AM_V_CCLD)$(LINK) $(dso_LDFLAGS) -o $@ \
-Wl,--soname,$(LIBDEBUGINFOD_SONAME) \
-Wl,--version-script,$<,--no-undefined \
-Wl,--version-script,$< \
$(NO_UNDEFINED) \
-Wl,--whole-archive $(libdebuginfod_so_LIBS) -Wl,--no-whole-archive \
$(libdebuginfod_so_LDLIBS)
@$(textrel_check)
Expand Down
3 changes: 2 additions & 1 deletion libasm/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ libasm_so_LIBS = libasm_pic.a
libasm.so: $(srcdir)/libasm.map $(libasm_so_LIBS) $(libasm_so_DEPS)
$(AM_V_CCLD)$(LINK) $(dso_LDFLAGS) -o $@ \
-Wl,--soname,$@.$(VERSION) \
-Wl,--version-script,$<,--no-undefined \
-Wl,--version-script,$< \
$(NO_UNDEFINED) \
-Wl,--whole-archive $(libasm_so_LIBS) -Wl,--no-whole-archive \
$(libasm_so_LDLIBS)
@$(textrel_check)
Expand Down
3 changes: 2 additions & 1 deletion libdw/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ libdw_so_LDLIBS = $(libdw_so_DEPS) -ldl -lz $(argp_LDADD) $(fts_LIBS) $(obstack_
libdw.so: $(srcdir)/libdw.map $(libdw_so_LIBS) $(libdw_so_DEPS)
$(AM_V_CCLD)$(LINK) $(dso_LDFLAGS) -o $@ \
-Wl,--soname,$@.$(VERSION),--enable-new-dtags \
-Wl,--version-script,$<,--no-undefined \
-Wl,--version-script,$< \
$(NO_UNDEFINED) \
-Wl,--whole-archive $(libdw_so_LIBS) -Wl,--no-whole-archive \
$(libdw_so_LDLIBS)
@$(textrel_check)
Expand Down
3 changes: 2 additions & 1 deletion libelf/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ libelf_so_LIBS = libelf_pic.a
libelf.so: $(srcdir)/libelf.map $(libelf_so_LIBS) $(libelf_so_DEPS)
$(AM_V_CCLD)$(LINK) $(dso_LDFLAGS) -o $@ \
-Wl,--soname,$@.$(VERSION) \
-Wl,--version-script,$<,--no-undefined \
-Wl,--version-script,$< \
$(NO_UNDEFINED) \
-Wl,--whole-archive $(libelf_so_LIBS) -Wl,--no-whole-archive \
$(libelf_so_LDLIBS)
@$(textrel_check)
Expand Down
4 changes: 4 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ endif
if USE_ADDRESS_SANITIZER
EXTRA_NLIST_CFLAGS=-fsanitize=address
else
if USE_MEMORY_SANITIZER
EXTRA_NLIST_CFLAGS=-fsanitize=memory -fsanitize-memory-track-origins
else
EXTRA_NLIST_CFLAGS=
endif
endif

test-nlist$(EXEEXT): test-nlist.c
$(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
Expand Down

0 comments on commit cca57aa

Please sign in to comment.