Allow make test with ASan / --address-sanitizer

Andrew Bartlett abartlet at samba.org
Thu May 3 04:35:45 UTC 2018


I needed to get AddressSanitizer working to fix the LSA use-after-free
bug and the attached make it all work again.

Recent versions of GCC require that the library be in LD_PRELOAD before
anything that is linked with libasan.  I use a horrible ldd hack to
find the right library path (sorry, not sorry). 

Once we get a more modern sn-devel I'll enable this by default in
autobuild. 

Please review and push!

Andrew Bartlett
-- 
Andrew Bartlett
https://samba.org/~abartlet/
Authentication Developer, Samba Team         https://samba.org
Samba Development and Support, Catalyst IT   
https://catalyst.net.nz/services/samba



-------------- next part --------------
From f555031f968fadb15115dcdf9ffd884e44ec2d9f Mon Sep 17 00:00:00 2001
From: Andrew Bartlett <abartlet at samba.org>
Date: Thu, 3 May 2018 15:47:45 +1200
Subject: [PATCH] selftest: Allow make test to run with --address-sanitizer

Recent GCC versions enforce that the library must be in LD_PRELOAD if linked to a plugin
(like a python module).

Signed-off-by: Andrew Bartlett <abartlet at samba.org>
---
 selftest/selftest.pl | 11 +++++++++++
 selftest/wscript     | 24 ++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/selftest/selftest.pl b/selftest/selftest.pl
index 995abd71a04..bd72841199f 100755
--- a/selftest/selftest.pl
+++ b/selftest/selftest.pl
@@ -60,6 +60,7 @@ my $opt_libnss_wrapper_so_path = "";
 my $opt_libresolv_wrapper_so_path = "";
 my $opt_libsocket_wrapper_so_path = "";
 my $opt_libuid_wrapper_so_path = "";
+my $opt_libasan_so_path = "";
 my $opt_use_dns_faking = 0;
 my @testlists = ();
 
@@ -225,6 +226,7 @@ Preload cwrap:
  --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
  --socket_wrapper_so_path=FILE the socket_wrapper library to preload
  --uid_wrapper_so_path=FILE the uid_wrapper library to preload
+ --asan_so_path=FILE the asan library to preload
 
 DNS:
   --use-dns-faking          Fake DNS entries rather than talking to our
@@ -275,6 +277,7 @@ my $result = GetOptions (
 		'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
 		'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
 		'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
+		'asan_so_path=s' => \$opt_libasan_so_path,
 		'use-dns-faking' => \$opt_use_dns_faking
 	    );
 
@@ -378,6 +381,14 @@ if ($opt_socket_wrapper_pcap) {
 
 my $ld_preload = $ENV{LD_PRELOAD};
 
+if ($opt_libasan_so_path) {
+	if ($ld_preload) {
+		$ld_preload = "$ld_preload:$opt_libasan_so_path";
+	} else {
+		$ld_preload = "$opt_libasan_so_path";
+	}
+}
+
 if ($opt_libnss_wrapper_so_path) {
 	if ($ld_preload) {
 		$ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
diff --git a/selftest/wscript b/selftest/wscript
index 8264d96c8a2..3321554a491 100644
--- a/selftest/wscript
+++ b/selftest/wscript
@@ -249,9 +249,32 @@ def cmd_testonly(opt):
         # GSS_KRB5_CRED_NO_CI_FLAGS_X
         env.OPTIONS += " --exclude=${srcdir}/selftest/skip.no-GSS_KRB5_CRED_NO_CI_FLAGS_X"
 
+    if env.ADDRESS_SANITIZER:
+        # We try to find the correct libasan automatically
+        libasan = Utils.cmd_output('ldd bin/texpect | grep libasan| cut -f 3 -d \ ',
+                                   silent=True).strip()
+
+        # Have the selftest.pl LD_PRELOAD libasan in the right spot
+        env.OPTIONS += " --asan_so_path=" + libasan
+
     subunit_cache = None
     # We use the full path rather than relative path to avoid problems on some platforms (ie. solaris 8).
     env.CORE_COMMAND = '${PERL} ${srcdir}/selftest/selftest.pl --target=${SELFTEST_TARGET} --prefix=${SELFTEST_PREFIX} --srcdir=${srcdir} --exclude=${srcdir}/selftest/skip ${TESTLISTS} ${OPTIONS} ${TESTS}'
+        
+    if env.ADDRESS_SANITIZER:
+        # For now we cannot run with leak detection
+        no_leak_check = "ASAN_OPTIONS=detect_leaks=0"
+        env.CORE_COMMAND = no_leak_check + " " + env.CORE_COMMAND
+
+        # We need to have the subunit filter and formatter preload
+        # libasan otherwise the tests fail at startup.
+        #
+        # Also, we do not care about leaks in python
+
+        asan_envs = no_leak_check + " LD_PRELOAD=" + libasan + ' '
+        env.FILTER_OPTIONS = asan_envs + env.FILTER_OPTIONS
+        env.SUBUNIT_FORMATTER = asan_envs + env.SUBUNIT_FORMATTER
+        
     if Options.options.LIST:
         cmd = '${CORE_COMMAND} --list'
     else:
@@ -263,6 +286,7 @@ def cmd_testonly(opt):
             cmd += ' | tee %s | ${FORMAT_TEST_OUTPUT}' % subunit_cache
         else:
             cmd += ' | ${FILTER_OPTIONS}'
+
     runcmd = EXPAND_VARIABLES(opt, cmd)
 
     print("test: running %s" % runcmd)
-- 
2.11.0



More information about the samba-technical mailing list