Home | History | Annotate | Download | only in ubsan
      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 attr_value == None:
      8     lit_config.fatal(
      9       "No attribute %r in test configuration! You may need to run "
     10       "tests from your build directory or add this attribute "
     11       "to lit.site.cfg " % attr_name)
     12   return attr_value
     13 
     14 # Setup source root.
     15 config.test_source_root = os.path.dirname(__file__)
     16 
     17 default_ubsan_opts = []
     18 # Choose between standalone and UBSan+ASan modes.
     19 ubsan_lit_test_mode = get_required_attr(config, 'ubsan_lit_test_mode')
     20 if ubsan_lit_test_mode == "Standalone":
     21   config.name = 'UBSan-Standalone-' + config.target_arch
     22   config.available_features.add("ubsan-standalone")
     23   clang_ubsan_cflags = []
     24 elif ubsan_lit_test_mode == "AddressSanitizer":
     25   config.name = 'UBSan-ASan-' + config.target_arch
     26   config.available_features.add("ubsan-asan")
     27   clang_ubsan_cflags = ["-fsanitize=address"]
     28   default_ubsan_opts += ['detect_leaks=0']
     29 elif ubsan_lit_test_mode == "MemorySanitizer":
     30   config.name = 'UBSan-MSan-' + config.target_arch
     31   config.available_features.add("ubsan-msan")
     32   clang_ubsan_cflags = ["-fsanitize=memory"]
     33 elif ubsan_lit_test_mode == "ThreadSanitizer":
     34   config.name = 'UBSan-TSan-' + config.target_arch
     35   config.available_features.add("ubsan-tsan")
     36   clang_ubsan_cflags = ["-fsanitize=thread"]
     37 else:
     38   lit_config.fatal("Unknown UBSan test mode: %r" % ubsan_lit_test_mode)
     39 
     40 # Platform-specific default for lit tests.
     41 if config.host_os == 'Darwin':
     42   # On Darwin, we default to `abort_on_error=1`, which would make tests run
     43   # much slower. Let's override this and run lit tests with 'abort_on_error=0'.
     44   default_ubsan_opts += ['abort_on_error=0']
     45   default_ubsan_opts += ['log_to_syslog=0']
     46 default_ubsan_opts_str = ':'.join(default_ubsan_opts)
     47 if default_ubsan_opts_str:
     48   config.environment['UBSAN_OPTIONS'] = default_ubsan_opts_str
     49   default_ubsan_opts_str += ':'
     50 # Substitution to setup UBSAN_OPTIONS in portable way.
     51 config.substitutions.append(('%env_ubsan_opts=',
     52                              'env UBSAN_OPTIONS=' + default_ubsan_opts_str))
     53 
     54 def build_invocation(compile_flags):
     55   return " " + " ".join([config.clang] + compile_flags) + " "
     56 
     57 target_cflags = [get_required_attr(config, "target_cflags")]
     58 clang_ubsan_cflags += target_cflags
     59 clang_ubsan_cxxflags = config.cxx_mode_flags + clang_ubsan_cflags
     60 
     61 # Define %clang and %clangxx substitutions to use in test RUN lines.
     62 config.substitutions.append( ("%clang ", build_invocation(clang_ubsan_cflags)) )
     63 config.substitutions.append( ("%clangxx ", build_invocation(clang_ubsan_cxxflags)) )
     64 
     65 # Default test suffixes.
     66 config.suffixes = ['.c', '.cc', '.cpp']
     67 
     68 # Check that the host supports UndefinedBehaviorSanitizer tests
     69 if config.host_os not in ['Linux', 'Darwin', 'FreeBSD', 'Windows']:
     70   config.unsupported = True
     71 
     72 if config.host_os == 'Windows':
     73   # We do not currently support enough of the Microsoft ABI for UBSan to work on
     74   # Windows.
     75   config.available_features.remove('cxxabi')
     76 
     77 # Allow tests to use REQUIRES=stable-runtime.  For use when you cannot use XFAIL
     78 # because the test hangs or fails on one configuration and not the other.
     79 if config.target_arch.startswith('arm') == False and config.target_arch != 'aarch64':
     80   config.available_features.add('stable-runtime')
     81