scons: Enable building with the gcc/clang Address Sanitizer
authorAndreas Hansson <andreas.hansson@arm.com>
Wed, 17 Feb 2016 08:56:20 +0000 (03:56 -0500)
committerAndreas Hansson <andreas.hansson@arm.com>
Wed, 17 Feb 2016 08:56:20 +0000 (03:56 -0500)
Allow the user to easily build gem5 with the Address Sanitizer, part
of both gcc and clang these days.

SConstruct
src/SConscript

index c291265fcbf22d78b7346150dcd25b52110d5b03..d791033e79e93e976919bb3a2c39b1ab84941349 100755 (executable)
@@ -194,6 +194,8 @@ AddLocalOption('--without-tcmalloc', dest='without_tcmalloc',
                help='Disable linking against tcmalloc')
 AddLocalOption('--with-ubsan', dest='with_ubsan', action='store_true',
                help='Build with Undefined Behavior Sanitizer if available')
+AddLocalOption('--with-asan', dest='with_asan', action='store_true',
+               help='Build with Address Sanitizer if available')
 
 termcap = get_termcap(GetOption('use_colors'))
 
@@ -209,6 +211,7 @@ use_vars = set([ 'AS', 'AR', 'CC', 'CXX', 'HOME', 'LD_LIBRARY_PATH',
                  'PYTHONPATH', 'RANLIB', 'SWIG', 'TERM' ])
 
 use_prefixes = [
+    "ASAN_",           # address sanitizer symbolizer path and settings
     "CCACHE_",         # ccache (caching compiler wrapper) configuration
     "CCC_",            # clang static analyzer configuration
     "DISTCC_",         # distcc (distributed compiler wrapper) configuration
index 6cef4708993a542f5ca90a8543e7d48be74b14dd..9b6c2170be3166e1f3f8bc5a268d4771f54b6e94 100755 (executable)
@@ -1083,17 +1083,27 @@ def makeEnv(env, label, objsfx, strip = False, **kwargs):
             new_env.Append(CCFLAGS='-fsanitize=undefined')
             new_env.Append(LINKFLAGS='-fsanitize=undefined')
 
+        # The address sanitizer is available for gcc >= 4.8
+        if GetOption('with_asan')  and \
+                compareVersions(env['GCC_VERSION'], '4.8') >= 0:
+            new_env.Append(CCFLAGS='-fsanitize=address')
+            new_env.Append(LINKFLAGS='-fsanitize=address')
+
     if env['CLANG']:
         swig_env.Append(CCFLAGS=['-Wno-sometimes-uninitialized',
                                  '-Wno-deprecated-register',
                                  '-Wno-tautological-compare'])
 
-        # All supported clang versions have support for UBSan, so if
-        # asked to use it, append the compiler and linker flags.
+        # We require clang >= 3.1, so there is no need to check any
+        # versions here.
         if GetOption('with_ubsan'):
             new_env.Append(CCFLAGS='-fsanitize=undefined')
             new_env.Append(LINKFLAGS='-fsanitize=undefined')
 
+        if GetOption('with_asan'):
+            new_env.Append(CCFLAGS='-fsanitize=address')
+            new_env.Append(LINKFLAGS='-fsanitize=address')
+
     werror_env = new_env.Clone()
     # Treat warnings as errors but white list some warnings that we
     # want to allow (e.g., deprecation warnings).