Home | History | Annotate | Download | only in lit_tests
      1 # -*- Python -*-
      2 
      3 import os
      4 
      5 def get_required_attr(config, attr_name):
      6   attr_value = getattr(config, attr_name, None)
      7   if not attr_value:
      8     lit.fatal("No attribute %r in test configuration! You may need to run "
      9               "tests from your build directory or add this attribute "
     10               "to lit.site.cfg " % attr_name)
     11   return attr_value
     12 
     13 # Setup config name.
     14 config.name = 'AddressSanitizer' + config.bits
     15 
     16 # Setup source root.
     17 config.test_source_root = os.path.dirname(__file__)
     18 
     19 def DisplayNoConfigMessage():
     20   lit.fatal("No site specific configuration available! " +
     21             "Try running your test from the build tree or running " +
     22             "make check-asan")
     23 
     24 # Figure out LLVM source root.
     25 llvm_src_root = getattr(config, 'llvm_src_root', None)
     26 if llvm_src_root is None:
     27   # We probably haven't loaded the site-specific configuration: the user
     28   # is likely trying to run a test file directly, and the site configuration
     29   # wasn't created by the build system.
     30   asan_site_cfg = lit.params.get('asan_site_config', None)
     31   if (asan_site_cfg) and (os.path.exists(asan_site_cfg)):
     32     lit.load_config(config, asan_site_cfg)
     33     raise SystemExit
     34 
     35   # Try to guess the location of site-specific configuration using llvm-config
     36   # util that can point where the build tree is.
     37   llvm_config = lit.util.which("llvm-config", config.environment["PATH"])
     38   if not llvm_config:
     39     DisplayNoConfigMessage()
     40 
     41   # Find out the presumed location of generated site config.
     42   llvm_obj_root = lit.util.capture(["llvm-config", "--obj-root"]).strip()
     43   asan_site_cfg = os.path.join(llvm_obj_root, "projects", "compiler-rt",
     44                                "lib", "asan", "lit_tests", "lit.site.cfg")
     45   if (not asan_site_cfg) or (not os.path.exists(asan_site_cfg)):
     46     DisplayNoConfigMessage()
     47 
     48   lit.load_config(config, asan_site_cfg)
     49   raise SystemExit
     50 
     51 # Setup default compiler flags used with -fsanitize=address option.
     52 # FIXME: Review the set of required flags and check if it can be reduced.
     53 bits_cflag = " -m" + config.bits
     54 clang_asan_cflags = (" -fsanitize=address"
     55                    + " -mno-omit-leaf-frame-pointer"
     56                    + " -fno-omit-frame-pointer"
     57                    + " -fno-optimize-sibling-calls"
     58                    + " -g"
     59                    + bits_cflag)
     60 clang_asan_cxxflags = " --driver-mode=g++" + clang_asan_cflags
     61 config.substitutions.append( ("%clang ", " " + config.clang + bits_cflag + " "))
     62 config.substitutions.append( ("%clangxx ", (" " + config.clang +
     63                                             " --driver-mode=g++" +
     64                                             bits_cflag + " ")) )
     65 config.substitutions.append( ("%clang_asan ", (" " + config.clang + " " +
     66                                               clang_asan_cflags + " ")) )
     67 config.substitutions.append( ("%clangxx_asan ", (" " + config.clang + " " +
     68                                                 clang_asan_cxxflags + " ")) )
     69 
     70 # Setup path to external LLVM symbolizer to run AddressSanitizer output tests.
     71 config.environment['ASAN_SYMBOLIZER_PATH'] = config.llvm_symbolizer_path
     72 
     73 # Setup path to asan_symbolize.py script.
     74 asan_source_dir = get_required_attr(config, "asan_source_dir")
     75 asan_symbolize = os.path.join(asan_source_dir, "scripts", "asan_symbolize.py")
     76 if not os.path.exists(asan_symbolize):
     77   lit.fatal("Can't find script on path %r" % asan_symbolize)
     78 config.substitutions.append( ("%asan_symbolize", " " + asan_symbolize + " ") )
     79 
     80 # Define CHECK-%os to check for OS-dependent output.
     81 config.substitutions.append( ('CHECK-%os', ("CHECK-" + config.host_os)))
     82 
     83 config.available_features.add("asan-" + config.bits + "-bits")
     84 
     85 # Default test suffixes.
     86 config.suffixes = ['.c', '.cc', '.cpp']
     87 
     88 # AddressSanitizer tests are currently supported on Linux and Darwin only.
     89 if config.host_os not in ['Linux', 'Darwin']:
     90   config.unsupported = True
     91