1# -*- Python -*- 2 3import os 4 5from lit.llvm import llvm_config 6from lit.llvm.subst import ToolSubst, FindTool 7 8# Setup config name. 9config.name = "HWAddressSanitizer" + getattr(config, "name_suffix", "default") 10 11# Setup source root. 12config.test_source_root = os.path.dirname(__file__) 13 14# Setup default compiler flags used with -fsanitize=memory option. 15clang_cflags = [config.target_cflags] + config.debug_info_flags 16clang_cxxflags = config.cxx_mode_flags + clang_cflags 17clang_hwasan_common_cflags = clang_cflags + ["-fsanitize=hwaddress", "-fuse-ld=lld"] 18 19if config.target_arch == "x86_64" and config.enable_aliases != "0": 20 clang_hwasan_common_cflags += ["-fsanitize-hwaddress-experimental-aliasing"] 21else: 22 config.available_features.add("pointer-tagging") 23if config.target_arch == "x86_64": 24 # The callback instrumentation used on x86_64 has a 1/64 chance of choosing a 25 # stack tag of 0. This causes stack tests to become flaky, so we force tags 26 # to be generated via calls to __hwasan_generate_tag, which never returns 0. 27 # TODO: See if we can remove this once we use the outlined instrumentation. 28 clang_hwasan_common_cflags += ["-mllvm", "-hwasan-generate-tags-with-calls=1"] 29clang_hwasan_cflags = clang_hwasan_common_cflags + [ 30 "-mllvm", 31 "-hwasan-globals", 32 "-mllvm", 33 "-hwasan-use-short-granules", 34 "-mllvm", 35 "-hwasan-instrument-landing-pads=0", 36 "-mllvm", 37 "-hwasan-instrument-personality-functions", 38] 39clang_hwasan_oldrt_cflags = clang_hwasan_common_cflags + [ 40 "-mllvm", 41 "-hwasan-use-short-granules=0", 42 "-mllvm", 43 "-hwasan-instrument-landing-pads=1", 44 "-mllvm", 45 "-hwasan-instrument-personality-functions=0", 46] 47 48clang_hwasan_cxxflags = config.cxx_mode_flags + clang_hwasan_cflags 49clang_hwasan_oldrt_cxxflags = config.cxx_mode_flags + clang_hwasan_oldrt_cflags 50 51 52def build_invocation(compile_flags): 53 return " " + " ".join([config.clang] + compile_flags) + " " 54 55 56config.substitutions.append(("%clangxx ", build_invocation(clang_cxxflags))) 57config.substitutions.append(("%clang_hwasan ", build_invocation(clang_hwasan_cflags))) 58config.substitutions.append( 59 ("%clang_hwasan_oldrt ", build_invocation(clang_hwasan_oldrt_cflags)) 60) 61config.substitutions.append( 62 ("%clangxx_hwasan ", build_invocation(clang_hwasan_cxxflags)) 63) 64config.substitutions.append( 65 ("%clangxx_hwasan_oldrt ", build_invocation(clang_hwasan_oldrt_cxxflags)) 66) 67config.substitutions.append(("%compiler_rt_libdir", config.compiler_rt_libdir)) 68 69default_hwasan_opts_str = ":".join( 70 ["disable_allocator_tagging=1", "random_tags=0", "fail_without_syscall_abi=0"] 71 + config.default_sanitizer_opts 72) 73if default_hwasan_opts_str: 74 config.environment["HWASAN_OPTIONS"] = default_hwasan_opts_str 75 default_hwasan_opts_str += ":" 76config.substitutions.append( 77 ("%env_hwasan_opts=", "env HWASAN_OPTIONS=" + default_hwasan_opts_str) 78) 79 80# Ensure that we can use hwasan_symbolize from the expected location 81llvm_config.add_tool_substitutions( 82 [ToolSubst("hwasan_symbolize", unresolved="fatal")], 83 search_dirs=[config.compiler_rt_bindir], 84) 85 86# Default test suffixes. 87config.suffixes = [".c", ".cpp"] 88 89if config.host_os not in ["Linux", "Android"] or not config.has_lld: 90 config.unsupported = True 91