1# -*- Python -*- 2 3import os 4 5 6def get_required_attr(config, attr_name): 7 attr_value = getattr(config, attr_name, None) 8 if attr_value is 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.py " % attr_name 13 ) 14 return attr_value 15 16 17# Setup config name. 18config.name = "UBSan-" + config.name_suffix 19 20# Setup source root. 21config.test_source_root = os.path.dirname(__file__) 22 23default_ubsan_opts = list(config.default_sanitizer_opts) 24# Choose between standalone and UBSan+ASan modes. 25ubsan_lit_test_mode = get_required_attr(config, "ubsan_lit_test_mode") 26if ubsan_lit_test_mode == "Standalone": 27 config.available_features.add("ubsan-standalone") 28 clang_ubsan_cflags = [] 29elif ubsan_lit_test_mode == "StandaloneStatic": 30 config.available_features.add("ubsan-standalone-static") 31 clang_ubsan_cflags = ["-static-libsan"] 32elif ubsan_lit_test_mode == "AddressSanitizer": 33 config.available_features.add("ubsan-asan") 34 clang_ubsan_cflags = ["-fsanitize=address"] 35 default_ubsan_opts += ["detect_leaks=0"] 36elif ubsan_lit_test_mode == "MemorySanitizer": 37 config.available_features.add("ubsan-msan") 38 clang_ubsan_cflags = ["-fsanitize=memory"] 39elif ubsan_lit_test_mode == "ThreadSanitizer": 40 config.available_features.add("ubsan-tsan") 41 clang_ubsan_cflags = ["-fsanitize=thread"] 42else: 43 lit_config.fatal("Unknown UBSan test mode: %r" % ubsan_lit_test_mode) 44 45# Platform-specific default for lit tests. 46if config.target_arch == "s390x": 47 # On SystemZ we need -mbackchain to make the fast unwinder work. 48 clang_ubsan_cflags.append("-mbackchain") 49 50default_ubsan_opts_str = ":".join(default_ubsan_opts) 51if default_ubsan_opts_str: 52 config.environment["UBSAN_OPTIONS"] = default_ubsan_opts_str 53 default_ubsan_opts_str += ":" 54# Substitution to setup UBSAN_OPTIONS in portable way. 55config.substitutions.append( 56 ("%env_ubsan_opts=", "env UBSAN_OPTIONS=" + default_ubsan_opts_str) 57) 58 59 60def build_invocation(compile_flags): 61 return " " + " ".join([config.clang] + compile_flags) + " " 62 63 64target_cflags = [get_required_attr(config, "target_cflags")] 65clang_ubsan_cflags += target_cflags 66clang_ubsan_cxxflags = config.cxx_mode_flags + clang_ubsan_cflags 67 68# Define %clang and %clangxx substitutions to use in test RUN lines. 69config.substitutions.append(("%clang ", build_invocation(clang_ubsan_cflags))) 70config.substitutions.append(("%clangxx ", build_invocation(clang_ubsan_cxxflags))) 71config.substitutions.append(("%gmlt ", " ".join(config.debug_info_flags) + " ")) 72 73# Default test suffixes. 74config.suffixes = [".c", ".cpp", ".m"] 75 76# Check that the host supports UndefinedBehaviorSanitizer tests 77if config.host_os not in [ 78 "Linux", 79 "Darwin", 80 "FreeBSD", 81 "Windows", 82 "NetBSD", 83 "SunOS", 84 "OpenBSD", 85]: 86 config.unsupported = True 87 88config.excludes = ["Inputs"] 89 90if ubsan_lit_test_mode in ["AddressSanitizer", "MemorySanitizer", "ThreadSanitizer"]: 91 if not config.parallelism_group: 92 config.parallelism_group = "shadow-memory" 93 if config.host_os == "NetBSD": 94 config.substitutions.insert(0, ("%run", config.netbsd_noaslr_prefix)) 95