Home | History | Annotate | Download | only in asan
      1 # -*- Python -*-
      2 
      3 import os
      4 import platform
      5 
      6 def get_required_attr(config, attr_name):
      7   attr_value = getattr(config, attr_name, None)
      8   if attr_value == None:
      9     lit_config.fatal(
     10       "No attribute %r in test configuration! You may need to run "
     11       "tests from your build directory or add this attribute "
     12       "to lit.site.cfg " % attr_name)
     13   return attr_value
     14 
     15 def push_ld_library_path(config, new_path):
     16   new_ld_library_path = os.path.pathsep.join(
     17     (new_path, config.environment['LD_LIBRARY_PATH']))
     18   config.environment['LD_LIBRARY_PATH'] = new_ld_library_path
     19 
     20 # Setup config name.
     21 config.name = 'AddressSanitizer' + config.name_suffix
     22 
     23 # Setup source root.
     24 config.test_source_root = os.path.dirname(__file__)
     25 
     26 # GCC-ASan doesn't link in all the necessary libraries automatically, so
     27 # we have to do it ourselves.
     28 if config.compiler_id == 'GNU':
     29   extra_linkflags = ["-pthread", "-lstdc++", "-ldl"]
     30 else:
     31   extra_linkflags = []
     32 # Setup default compiler flags used with -fsanitize=address option.
     33 # FIXME: Review the set of required flags and check if it can be reduced.
     34 target_cflags = [get_required_attr(config, "target_cflags")] + extra_linkflags
     35 target_cxxflags = config.cxx_mode_flags + target_cflags
     36 clang_asan_static_cflags = ["-fsanitize=address",
     37                             "-mno-omit-leaf-frame-pointer",
     38                             "-fno-omit-frame-pointer",
     39                             "-fno-optimize-sibling-calls",
     40                             "-g"] + target_cflags
     41 clang_asan_static_cxxflags = config.cxx_mode_flags + clang_asan_static_cflags
     42 
     43 if config.asan_dynamic:
     44   clang_asan_cflags = clang_asan_static_cflags + ['-shared-libasan']
     45   clang_asan_cxxflags = clang_asan_static_cxxflags + ['-shared-libasan']
     46   config.available_features.add("asan-dynamic-runtime")
     47 else:
     48   clang_asan_cflags = clang_asan_static_cflags
     49   clang_asan_cxxflags = clang_asan_static_cxxflags
     50   config.available_features.add("asan-static-runtime")
     51 
     52 asan_lit_source_dir = get_required_attr(config, "asan_lit_source_dir")
     53 if config.android == "TRUE":
     54   config.available_features.add('android')
     55   clang_wrapper = os.path.join(asan_lit_source_dir,
     56                                "android_commands", "android_compile.py") + " "
     57 else:
     58   clang_wrapper = ""
     59 
     60 def build_invocation(compile_flags):
     61   return " " + " ".join([clang_wrapper, config.clang] + compile_flags) + " "
     62 
     63 config.substitutions.append( ("%clang ", build_invocation(target_cflags)) )
     64 config.substitutions.append( ("%clangxx ", build_invocation(target_cxxflags)) )
     65 config.substitutions.append( ("%clang_asan ", build_invocation(clang_asan_cflags)) )
     66 config.substitutions.append( ("%clangxx_asan ", build_invocation(clang_asan_cxxflags)) )
     67 config.substitutions.append( ("%shared_libasan", "libclang_rt.asan-%s.so" % config.target_arch))
     68 if config.asan_dynamic:
     69   config.substitutions.append( ("%clang_asan_static ", build_invocation(clang_asan_static_cflags)) )
     70   config.substitutions.append( ("%clangxx_asan_static ", build_invocation(clang_asan_static_cxxflags)) )
     71 
     72 # Windows-specific tests might also use the clang-cl.exe driver.
     73 if platform.system() == 'Windows':
     74   clang_cl_asan_cxxflags = ["-fsanitize=address",
     75                             "-Wno-deprecated-declarations",
     76                             "-WX",
     77                             "-D_HAS_EXCEPTIONS=0",
     78                             "-Zi"] + target_cflags
     79   clang_invocation = build_invocation(clang_cl_asan_cxxflags)
     80   clang_cl_invocation = clang_invocation.replace("clang.exe","clang-cl.exe")
     81   config.substitutions.append( ("%clang_cl_asan ", clang_cl_invocation) )
     82 
     83 # FIXME: De-hardcode this path.
     84 asan_source_dir = os.path.join(
     85   get_required_attr(config, "compiler_rt_src_root"), "lib", "asan")
     86 # Setup path to asan_symbolize.py script.
     87 asan_symbolize = os.path.join(asan_source_dir, "scripts", "asan_symbolize.py")
     88 if not os.path.exists(asan_symbolize):
     89   lit_config.fatal("Can't find script on path %r" % asan_symbolize)
     90 python_exec = get_required_attr(config, "python_executable")
     91 config.substitutions.append( ("%asan_symbolize", python_exec + " " + asan_symbolize + " ") )
     92 # Setup path to sancov.py script.
     93 sanitizer_common_source_dir = os.path.join(
     94   get_required_attr(config, "compiler_rt_src_root"), "lib", "sanitizer_common")
     95 sancov = os.path.join(sanitizer_common_source_dir, "scripts", "sancov.py")
     96 if not os.path.exists(sancov):
     97   lit_config.fatal("Can't find script on path %r" % sancov)
     98 python_exec = get_required_attr(config, "python_executable")
     99 config.substitutions.append( ("%sancov", python_exec + " " + sancov + " ") )
    100 
    101 # Determine kernel bitness
    102 if config.host_arch.find('64') != -1 and config.android != "TRUE":
    103   kernel_bits = '64'
    104 else:
    105   kernel_bits = '32'
    106 
    107 # Define CHECK-%os to check for OS-dependent output.
    108 config.substitutions.append( ('CHECK-%os', ("CHECK-" + config.host_os)))
    109 config.substitutions.append( ('CHECK-%kernel_bits', ("CHECK-kernel-" + kernel_bits + "-bits")))
    110 
    111 config.available_features.add("asan-" + config.bits + "-bits")
    112 
    113 # Allow tests to use REQUIRES=stable-runtime.  For use when you cannot use XFAIL
    114 # because the test hangs.
    115 if config.target_arch != 'arm':
    116   config.available_features.add('stable-runtime')
    117 
    118 # Turn on leak detection on 64-bit Linux.
    119 if config.host_os == 'Linux' and config.bits == '64':
    120   config.environment['ASAN_OPTIONS'] = 'detect_leaks=1'
    121 
    122 # Set LD_LIBRARY_PATH to pick dynamic runtime up properly.
    123 push_ld_library_path(config, config.compiler_rt_libdir)
    124 
    125 # GCC-ASan uses dynamic runtime by default.
    126 if config.compiler_id == 'GNU':
    127   gcc_dir = os.path.dirname(config.clang)
    128   libasan_dir = os.path.join(gcc_dir, "..", "lib" + config.bits)
    129   push_ld_library_path(config, libasan_dir)
    130 
    131 # Default test suffixes.
    132 config.suffixes = ['.c', '.cc', '.cpp']
    133 
    134 if config.host_os == 'Darwin':
    135   config.suffixes.append('.mm')
    136 
    137 # AddressSanitizer tests are currently supported on Linux and Darwin only.
    138 if config.host_os not in ['Linux', 'Darwin']:
    139   config.unsupported = True
    140