From patchwork Mon Feb 13 16:45:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilya Leoshkevich X-Patchwork-Id: 64908 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C53CB385842B for ; Mon, 13 Feb 2023 16:46:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C53CB385842B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1676306789; bh=r3ikuHlJ4ihTzCMZjD8HHru1gdfJ17tX5L10aa655G8=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Help:List-Subscribe:From: Reply-To:From; b=NJy1SGWLLtOmiATfEVpRUiJ/kqhnPUmkjk0zS+cZDuU5WxaVZ3JahlBXSKeC2+UIa LWDWsRvibzC/aYpGmCAYFKnYFK01ACZBJHsagOQKvgrV+n6j88OCKx3rZQHgFQaPhQ CFY8x+ZKEv8hGEFs1DRVOkKQXmfRyOa4ncB/2os8= X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 97074385B516 for ; Mon, 13 Feb 2023 16:46:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 97074385B516 Received: from pps.filterd (m0098410.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 31DGg2WK014760; Mon, 13 Feb 2023 16:46:02 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3nqrxx0342-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 13 Feb 2023 16:46:02 +0000 Received: from m0098410.ppops.net (m0098410.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 31DGi3ws023707; Mon, 13 Feb 2023 16:46:01 GMT Received: from ppma04ams.nl.ibm.com (63.31.33a9.ip4.static.sl-reverse.com [169.51.49.99]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3nqrxx032x-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 13 Feb 2023 16:46:01 +0000 Received: from pps.filterd (ppma04ams.nl.ibm.com [127.0.0.1]) by ppma04ams.nl.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 31DDGAig010684; Mon, 13 Feb 2023 16:45:59 GMT Received: from smtprelay06.fra02v.mail.ibm.com ([9.218.2.230]) by ppma04ams.nl.ibm.com (PPS) with ESMTPS id 3np2n6tx4v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 13 Feb 2023 16:45:59 +0000 Received: from smtpav05.fra02v.mail.ibm.com (smtpav05.fra02v.mail.ibm.com [10.20.54.104]) by smtprelay06.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 31DGjuwM21168384 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 13 Feb 2023 16:45:57 GMT Received: from smtpav05.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id BCB9C2004B; Mon, 13 Feb 2023 16:45:56 +0000 (GMT) Received: from smtpav05.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6819F20040; Mon, 13 Feb 2023 16:45:56 +0000 (GMT) Received: from heavy.ibmuc.com (unknown [9.171.19.58]) by smtpav05.fra02v.mail.ibm.com (Postfix) with ESMTP; Mon, 13 Feb 2023 16:45:56 +0000 (GMT) To: Mark Wielaard , "Frank Ch . Eigler" Cc: elfutils-devel@sourceware.org, Ilya Leoshkevich Subject: [PATCH v3 4/4] configure: Add --enable-sanitize-memory Date: Mon, 13 Feb 2023 17:45:51 +0100 Message-Id: <20230213164551.550400-5-iii@linux.ibm.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230213164551.550400-1-iii@linux.ibm.com> References: <20230213164551.550400-1-iii@linux.ibm.com> X-TM-AS-GCONF: 00 X-Proofpoint-GUID: liXLbISu0p_lAIkXqhcnFgnvkvpQMafh X-Proofpoint-ORIG-GUID: sypaY5gDXSKCP-1gOtfApBqy0k7lXBnm X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.930,Hydra:6.0.562,FMLib:17.11.170.22 definitions=2023-02-13_11,2023-02-13_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 suspectscore=0 mlxlogscore=999 adultscore=0 impostorscore=0 malwarescore=0 mlxscore=0 spamscore=0 lowpriorityscore=0 phishscore=0 priorityscore=1501 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2302130150 X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-Patchwork-Original-From: Ilya Leoshkevich via Elfutils-devel From: Ilya Leoshkevich Reply-To: Ilya Leoshkevich Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org Sender: "Elfutils-devel" 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. MSan is not linked into shared libraries, and is linked into executables statically. Therefore, 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. Disable a few tests that run for more than 5 minutes due to test files being statically linked with MSan. [1] https://clang.llvm.org/docs/MemorySanitizer.html [2] https://github.com/google/sanitizers/issues/247 Signed-off-by: Ilya Leoshkevich --- configure.ac | 24 ++++++++++++++++++++++++ debuginfod/Makefile.am | 3 ++- libasm/Makefile.am | 3 ++- libdw/Makefile.am | 3 ++- libelf/Makefile.am | 3 ++- tests/Makefile.am | 10 +++++++++- tests/run-readelf-self.sh | 5 +++++ tests/run-strip-reloc.sh | 5 +++++ tests/run-varlocs-self.sh | 5 +++++ 9 files changed, 56 insertions(+), 5 deletions(-) diff --git a/configure.ac b/configure.ac index 4efb2a9c..18951947 100644 --- a/configure.ac +++ b/configure.ac @@ -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" @@ -894,6 +917,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} ]) diff --git a/debuginfod/Makefile.am b/debuginfod/Makefile.am index f27d6e2e..125be97b 100644 --- a/debuginfod/Makefile.am +++ b/debuginfod/Makefile.am @@ -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) diff --git a/libasm/Makefile.am b/libasm/Makefile.am index c2b54811..1e6b63e8 100644 --- a/libasm/Makefile.am +++ b/libasm/Makefile.am @@ -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) diff --git a/libdw/Makefile.am b/libdw/Makefile.am index 1b6fead4..e548f38c 100644 --- a/libdw/Makefile.am +++ b/libdw/Makefile.am @@ -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) diff --git a/libelf/Makefile.am b/libelf/Makefile.am index 24c25cf8..aabce43e 100644 --- a/libelf/Makefile.am +++ b/libelf/Makefile.am @@ -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) diff --git a/tests/Makefile.am b/tests/Makefile.am index bfc03061..fb7714d0 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -88,12 +88,16 @@ endif # test_nlist checks its own symbol table, and expects various symbols # to be in the order as specified in the source file. Explicitly set -# minimal CFLAGS. But add address sanitizer if in use. +# minimal CFLAGS. But add sanitizers if in use. 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) \ @@ -225,6 +229,10 @@ if USE_ZSTD_COMPRESS export ELFUTILS_ZSTD = 1 endif +if USE_MEMORY_SANITIZER +export ELFUTILS_MEMORY_SANITIZER = 1 +endif + if DEBUGINFOD check_PROGRAMS += debuginfod_build_id_find # With the dummy delegation doesn't work diff --git a/tests/run-readelf-self.sh b/tests/run-readelf-self.sh index 7ffb3577..f5984597 100755 --- a/tests/run-readelf-self.sh +++ b/tests/run-readelf-self.sh @@ -17,5 +17,10 @@ . $srcdir/test-subr.sh +if test -n "$ELFUTILS_MEMORY_SANITIZER"; then + echo "binaries linked with memory sanitizer are too big" + exit 77 +fi + # Just makes sure readelf doesn't crash testrun_on_self_quiet ${abs_top_builddir}/src/readelf -a -w diff --git a/tests/run-strip-reloc.sh b/tests/run-strip-reloc.sh index 033ed278..02e626af 100755 --- a/tests/run-strip-reloc.sh +++ b/tests/run-strip-reloc.sh @@ -17,6 +17,11 @@ . $srcdir/test-subr.sh +if test -n "$ELFUTILS_MEMORY_SANITIZER"; then + echo "binaries linked with memory sanitizer are too big" + exit 77 +fi + testfiles hello_i386.ko hello_x86_64.ko hello_ppc64.ko hello_s390.ko \ hello_aarch64.ko hello_m68k.ko hello_riscv64.ko hello_csky.ko \ hello_arc_hs4.ko diff --git a/tests/run-varlocs-self.sh b/tests/run-varlocs-self.sh index 5454fc70..760fe4d7 100755 --- a/tests/run-varlocs-self.sh +++ b/tests/run-varlocs-self.sh @@ -17,6 +17,11 @@ . $srcdir/test-subr.sh +if test -n "$ELFUTILS_MEMORY_SANITIZER"; then + echo "binaries linked with memory sanitizer are too big" + exit 77 +fi + # Make sure varlocs doesn't crash, doesn't trigger self-check/asserts # or leaks running under valgrind. testrun_on_self_exe ${abs_top_builddir}/tests/varlocs -e