xref: /llvm-project/compiler-rt/test/lsan/lit.common.cfg.py (revision 0af6c304e48e0484672b53be49a15f411d173e59)
1# -*- Python -*-
2
3# Common configuration for running leak detection tests under LSan/ASan.
4
5import os
6import re
7
8import lit.util
9
10
11def get_required_attr(config, attr_name):
12    attr_value = getattr(config, attr_name, None)
13    if attr_value is None:
14        lit_config.fatal(
15            "No attribute %r in test configuration! You may need to run "
16            "tests from your build directory or add this attribute "
17            "to lit.site.cfg.py " % attr_name
18        )
19    return attr_value
20
21
22# Setup source root.
23config.test_source_root = os.path.dirname(__file__)
24
25# Choose between standalone and LSan+(ASan|HWAsan) modes.
26lsan_lit_test_mode = get_required_attr(config, "lsan_lit_test_mode")
27target_arch = getattr(config, "target_arch", None)
28
29if lsan_lit_test_mode == "Standalone":
30    config.name = "LeakSanitizer-Standalone"
31    lsan_cflags = ["-fsanitize=leak"]
32    config.available_features.add("lsan-standalone")
33elif lsan_lit_test_mode == "AddressSanitizer":
34    config.name = "LeakSanitizer-AddressSanitizer"
35    lsan_cflags = ["-fsanitize=address"]
36    config.available_features.add("asan")
37    if config.host_os == "NetBSD":
38        config.substitutions.insert(0, ("%run", config.netbsd_noaslr_prefix))
39elif lsan_lit_test_mode == "HWAddressSanitizer":
40    config.name = "LeakSanitizer-HWAddressSanitizer"
41    lsan_cflags = ["-fsanitize=hwaddress", "-fuse-ld=lld"]
42    if target_arch == "x86_64":
43        lsan_cflags = lsan_cflags + ["-fsanitize-hwaddress-experimental-aliasing"]
44    config.available_features.add("hwasan")
45    if config.host_os == "NetBSD":
46        config.substitutions.insert(0, ("%run", config.netbsd_noaslr_prefix))
47else:
48    lit_config.fatal("Unknown LSan test mode: %r" % lsan_lit_test_mode)
49config.name += config.name_suffix
50
51# Platform-specific default LSAN_OPTIONS for lit tests.
52default_common_opts_str = ":".join(list(config.default_sanitizer_opts))
53default_lsan_opts = default_common_opts_str + ":detect_leaks=1"
54if config.host_os == "Darwin":
55    # On Darwin, we default to `abort_on_error=1`, which would make tests run
56    # much slower. Let's override this and run lit tests with 'abort_on_error=0'.
57    # Also, make sure we do not overwhelm the syslog while testing.
58    default_lsan_opts += ":abort_on_error=0"
59    default_lsan_opts += ":log_to_syslog=0"
60
61if default_lsan_opts:
62    config.environment["LSAN_OPTIONS"] = default_lsan_opts
63    default_lsan_opts += ":"
64config.substitutions.append(
65    ("%env_lsan_opts=", "env LSAN_OPTIONS=" + default_lsan_opts)
66)
67
68if lit.util.which("strace"):
69    config.available_features.add("strace")
70
71clang_cflags = ["-O0", config.target_cflags] + config.debug_info_flags
72if config.android:
73    clang_cflags = clang_cflags + ["-fno-emulated-tls"]
74clang_cxxflags = config.cxx_mode_flags + clang_cflags
75lsan_incdir = config.test_source_root + "/../"
76clang_lsan_cflags = clang_cflags + lsan_cflags + ["-I%s" % lsan_incdir]
77clang_lsan_cxxflags = clang_cxxflags + lsan_cflags + ["-I%s" % lsan_incdir]
78
79config.clang_cflags = clang_cflags
80config.clang_cxxflags = clang_cxxflags
81
82
83def build_invocation(compile_flags):
84    return " " + " ".join([config.clang] + compile_flags) + " "
85
86
87config.substitutions.append(("%clang ", build_invocation(clang_cflags)))
88config.substitutions.append(("%clangxx ", build_invocation(clang_cxxflags)))
89config.substitutions.append(("%clang_lsan ", build_invocation(clang_lsan_cflags)))
90config.substitutions.append(("%clangxx_lsan ", build_invocation(clang_lsan_cxxflags)))
91config.substitutions.append(("%clang_hwasan ", build_invocation(clang_lsan_cflags)))
92config.substitutions.append(("%clangxx_hwasan ", build_invocation(clang_lsan_cxxflags)))
93
94
95# LeakSanitizer tests are currently supported on
96# Android{aarch64, x86, x86_64}, x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux, loongarch64 Linux and x86_64 Darwin.
97supported_android = (
98    config.android
99    and config.target_arch in ["x86_64", "i386", "aarch64"]
100    and "android-thread-properties-api" in config.available_features
101)
102supported_linux = (
103    (not config.android)
104    and config.host_os == "Linux"
105    and config.host_arch
106    in [
107        "aarch64",
108        "x86_64",
109        "ppc64",
110        "ppc64le",
111        "mips64",
112        "riscv64",
113        "arm",
114        "armhf",
115        "armv7l",
116        "s390x",
117        "loongarch64",
118    ]
119)
120supported_darwin = config.host_os == "Darwin" and config.target_arch in ["x86_64"]
121supported_netbsd = config.host_os == "NetBSD" and config.target_arch in [
122    "x86_64",
123    "i386",
124]
125if not (supported_android or supported_linux or supported_darwin or supported_netbsd):
126    config.unsupported = True
127
128# Don't support Thumb due to broken fast unwinder
129if re.search("mthumb", config.target_cflags) is not None:
130    config.unsupported = True
131
132# HWASAN tests require lld because without D65857, ld.bfd and ld.gold would
133# generate a corrupted binary. Mark them unsupported if lld is not available.
134if "hwasan" in config.available_features and not config.has_lld:
135    config.unsupported = True
136
137config.suffixes = [".c", ".cpp", ".mm"]
138