selftest: Allow make test to run with --address-sanitizer
authorAndrew Bartlett <abartlet@samba.org>
Thu, 3 May 2018 03:47:45 +0000 (15:47 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 3 May 2018 06:17:44 +0000 (08:17 +0200)
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@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
selftest/selftest.pl
selftest/wscript

index 995abd71a04f17c4eff9db97248acb3d1e4f0b1d..bd72841199f4b7b6e4111fac70b3c958fd868e35 100755 (executable)
@@ -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";
index 8264d96c8a240beb41a7498a52a4d899538c4d05..6a6dcb5c644c42c1627136ebb14dbdd808b76cdb 100644 (file)
@@ -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)