scons: Allow building with Address Sanitizer.
authorJose Fonseca <jfonseca@vmware.com>
Sat, 9 Apr 2016 19:26:42 +0000 (20:26 +0100)
committerJose Fonseca <jfonseca@vmware.com>
Wed, 13 Apr 2016 05:54:32 +0000 (06:54 +0100)
libasan is never linked to shared objects (which doesn't go well with
-z,defs).  It must either be linked to the main executable, or (more
practically for OpenGL drivers) be pre-loaded via LD_PRELOAD.

Otherwise works.

I didn't find anything with llvmpipe.  I suspect the fact that the
JIT compiled code isn't instrumented means there are lots of errors it
can't catch.

But for non-JIT drivers, the Address/Leak Sanitizers seem like a faster
alternative to Valgrind.

Usage (Ubuntu 15.10):

   scons asan=1 libgl-xlib
   export LD_LIBRARY_PATH=$PWD/build/linux-x86_64-debug/gallium/targets/libgl-xlib
   LD_PRELOAD=libasan.so.2 any-opengl-application

Acked-by: Roland Scheidegger <sroland@vmware.com>
common.py
scons/gallium.py
src/gallium/targets/libgl-xlib/SConscript
src/mesa/drivers/x11/SConscript

index 7a939417e2555b54b0b8881d7318491911618e0e..70e6708a2f9e9982be15ea65b3023b7d05bbf602 100644 (file)
--- a/common.py
+++ b/common.py
@@ -97,6 +97,7 @@ def AddOptions(opts):
     opts.Add(BoolOption('embedded', 'embedded build', 'no'))
     opts.Add(BoolOption('analyze',
                         'enable static code analysis where available', 'no'))
+    opts.Add(BoolOption('asan', 'enable Address Sanitizer', 'no'))
     opts.Add('toolchain', 'compiler toolchain', default_toolchain)
     opts.Add(BoolOption('gles', 'EXPERIMENTAL: enable OpenGL ES support',
                         'no'))
index 46520168a02fe96694bc1290b97966e5b466bc4f..f37042d9af10d53888256f45660c717dd6e49d8b 100755 (executable)
@@ -410,7 +410,7 @@ def generate(env):
         # Work around aliasing bugs - developers should comment this out
         ccflags += ['-fno-strict-aliasing']
         ccflags += ['-g']
-        if env['build'] in ('checked', 'profile'):
+        if env['build'] in ('checked', 'profile') or env['asan']:
             # See http://code.google.com/p/jrfonseca/wiki/Gprof2Dot#Which_options_should_I_pass_to_gcc_when_compiling_for_profiling?
             ccflags += [
                 '-fno-omit-frame-pointer',
@@ -540,6 +540,16 @@ def generate(env):
             # scan-build will produce more comprehensive output
             env.Append(CCFLAGS = ['--analyze'])
 
+    # https://github.com/google/sanitizers/wiki/AddressSanitizer
+    if env['asan']:
+        if gcc_compat:
+            env.Append(CCFLAGS = [
+                '-fsanitize=address',
+            ])
+            env.Append(LINKFLAGS = [
+                '-fsanitize=address',
+            ])
+
     # Assembler options
     if gcc_compat:
         if env['machine'] == 'x86':
index e1c78dd06a0cd0d57abadb84d3e2cf8aa12a52ee..1c816ff7762c286ae3bf1a21f12b71e596ca19cd 100644 (file)
@@ -48,11 +48,15 @@ if env['llvm']:
     env.Prepend(LIBS = [llvmpipe])
 
 if env['platform'] != 'darwin':
+    # Disallow undefined symbols, except with Address Sanitizer, since libasan
+    # is not linked on shared libs, as it should be LD_PRELOAD'ed instead
+    if not env['asan']:
+        env.Append(SHLINKFLAGS = [
+            '-Wl,-z,defs',
+        ])
     env.Append(SHLINKFLAGS = [
-       # Disallow undefined symbols
-       '-Wl,-z,defs',
-       # Restrict exported symbols
-       '-Wl,--version-script=%s' % File("libgl-xlib.sym").srcnode().path,
+        # Restrict exported symbols
+        '-Wl,--version-script=%s' % File("libgl-xlib.sym").srcnode().path,
     ])
 
 # libGL.so.1.5
index 45419973d39d02e003be0465eb379299ca591f6d..59c8df4b3c235579c0aa559940e9ccd42180afa6 100644 (file)
@@ -34,9 +34,13 @@ sources = [
        'xm_tri.c',
 ]
 
-# Disallow undefined symbols
 if env['platform'] != 'darwin':
-    env.Append(SHLINKFLAGS = ['-Wl,-z,defs'])
+    # Disallow undefined symbols, except with Address Sanitizer, since libasan
+    # is not linked on shared libs, as it should be LD_PRELOAD'ed instead
+    if not env['asan']:
+        env.Append(SHLINKFLAGS = [
+            '-Wl,-z,defs',
+        ])
 
 # libGL.so.1.6
 libgl_1_6 = env.SharedLibrary(