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