3 # Common configuration for running leak detection tests under LSan/ASan.
10 def get_required_attr(config
, attr_name
):
11 attr_value
= getattr(config
, attr_name
, None)
12 if attr_value
== None:
14 "No attribute %r in test configuration! You may need to run "
15 "tests from your build directory or add this attribute "
16 "to lit.site.cfg.py " % attr_name
)
20 config
.test_source_root
= os
.path
.dirname(__file__
)
22 # Choose between standalone and LSan+ASan modes.
23 lsan_lit_test_mode
= get_required_attr(config
, 'lsan_lit_test_mode')
25 if lsan_lit_test_mode
== "Standalone":
26 config
.name
= "LeakSanitizer-Standalone"
27 lsan_cflags
= ["-fsanitize=leak"]
28 elif lsan_lit_test_mode
== "AddressSanitizer":
29 config
.name
= "LeakSanitizer-AddressSanitizer"
30 lsan_cflags
= ["-fsanitize=address"]
31 config
.available_features
.add('asan')
32 if config
.host_os
== 'NetBSD':
33 config
.substitutions
.insert(0, ('%run', config
.netbsd_noaslr_prefix
))
35 lit_config
.fatal("Unknown LSan test mode: %r" % lsan_lit_test_mode
)
36 config
.name
+= config
.name_suffix
38 # Platform-specific default LSAN_OPTIONS for lit tests.
39 default_common_opts_str
= ':'.join(list(config
.default_sanitizer_opts
))
40 default_lsan_opts
= default_common_opts_str
+ ':detect_leaks=1'
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 # Also, make sure we do not overwhelm the syslog while testing.
45 default_lsan_opts
+= ':abort_on_error=0'
46 default_lsan_opts
+= ':log_to_syslog=0'
49 config
.environment
['LSAN_OPTIONS'] = default_lsan_opts
50 default_lsan_opts
+= ':'
51 config
.substitutions
.append(('%env_lsan_opts=',
52 'env LSAN_OPTIONS=' + default_lsan_opts
))
54 if lit
.util
.which('strace'):
55 config
.available_features
.add('strace')
57 clang_cflags
= ["-O0", config
.target_cflags
] + config
.debug_info_flags
59 clang_cflags
= clang_cflags
+ ["-fno-emulated-tls"]
60 clang_cxxflags
= config
.cxx_mode_flags
+ clang_cflags
61 lsan_incdir
= config
.test_source_root
+ "/../"
62 clang_lsan_cflags
= clang_cflags
+ lsan_cflags
+ ["-I%s" % lsan_incdir
]
63 clang_lsan_cxxflags
= clang_cxxflags
+ lsan_cflags
+ ["-I%s" % lsan_incdir
]
65 config
.clang_cflags
= clang_cflags
66 config
.clang_cxxflags
= clang_cxxflags
68 def build_invocation(compile_flags
):
69 return " " + " ".join([config
.clang
] + compile_flags
) + " "
71 config
.substitutions
.append( ("%clang ", build_invocation(clang_cflags
)) )
72 config
.substitutions
.append( ("%clangxx ", build_invocation(clang_cxxflags
)) )
73 config
.substitutions
.append( ("%clang_lsan ", build_invocation(clang_lsan_cflags
)) )
74 config
.substitutions
.append( ("%clangxx_lsan ", build_invocation(clang_lsan_cxxflags
)) )
76 # LeakSanitizer tests are currently supported on
77 # Android{aarch64, x86, x86_64}, x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux and x86_64 Darwin.
78 supported_android
= config
.android
and config
.target_arch
in ['x86_64', 'i386', 'aarch64'] and 'android-thread-properties-api' in config
.available_features
79 supported_linux
= (not config
.android
) and config
.host_os
== 'Linux' and config
.host_arch
in ['aarch64', 'x86_64', 'ppc64', 'ppc64le', 'mips64', 'riscv64', 'arm', 'armhf', 'armv7l', 's390x']
80 supported_darwin
= config
.host_os
== 'Darwin' and config
.target_arch
in ['x86_64']
81 supported_netbsd
= config
.host_os
== 'NetBSD' and config
.target_arch
in ['x86_64', 'i386']
82 if not (supported_android
or supported_linux
or supported_darwin
or supported_netbsd
):
83 config
.unsupported
= True
85 # Don't support Thumb due to broken fast unwinder
86 if re
.search('mthumb', config
.target_cflags
) is not None:
87 config
.unsupported
= True
89 config
.suffixes
= ['.c', '.cpp', '.mm']