1# -*- Python -*-
2
3# Common configuration for running leak detection tests under LSan/ASan.
4
5import os
6import re
7
8import lit.util
9
10def get_required_attr(config, attr_name):
11  attr_value = getattr(config, attr_name, None)
12  if attr_value == None:
13    lit_config.fatal(
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)
17  return attr_value
18
19# Setup source root.
20config.test_source_root = os.path.dirname(__file__)
21
22# Choose between standalone and LSan+ASan modes.
23lsan_lit_test_mode = get_required_attr(config, 'lsan_lit_test_mode')
24if lsan_lit_test_mode == "Standalone":
25  config.name = "LeakSanitizer-Standalone"
26  lsan_cflags = ["-fsanitize=leak"]
27elif lsan_lit_test_mode == "AddressSanitizer":
28  config.name = "LeakSanitizer-AddressSanitizer"
29  lsan_cflags = ["-fsanitize=address"]
30  config.available_features.add('asan')
31  if config.host_os == 'NetBSD':
32    config.substitutions.insert(0, ('%run', config.netbsd_noaslr_prefix))
33else:
34  lit_config.fatal("Unknown LSan test mode: %r" % lsan_lit_test_mode)
35config.name += config.name_suffix
36
37# Platform-specific default LSAN_OPTIONS for lit tests.
38default_lsan_opts = 'detect_leaks=1'
39if config.host_os == 'Darwin':
40  # On Darwin, we default to `abort_on_error=1`, which would make tests run
41  # much slower. Let's override this and run lit tests with 'abort_on_error=0'.
42  # Also, make sure we do not overwhelm the syslog while testing.
43  default_lsan_opts += ':abort_on_error=0'
44  default_lsan_opts += ':log_to_syslog=0'
45
46if default_lsan_opts:
47  config.environment['LSAN_OPTIONS'] = default_lsan_opts
48  default_lsan_opts += ':'
49config.substitutions.append(('%env_lsan_opts=',
50                             'env LSAN_OPTIONS=' + default_lsan_opts))
51
52if lit.util.which('strace'):
53  config.available_features.add('strace')
54
55clang_cflags = ["-O0", config.target_cflags] + config.debug_info_flags
56clang_cxxflags = config.cxx_mode_flags + clang_cflags
57lsan_incdir = config.test_source_root + "/../"
58clang_lsan_cflags = clang_cflags + lsan_cflags + ["-I%s" % lsan_incdir]
59clang_lsan_cxxflags = clang_cxxflags + lsan_cflags + ["-I%s" % lsan_incdir]
60
61config.clang_cflags = clang_cflags
62config.clang_cxxflags = clang_cxxflags
63
64def build_invocation(compile_flags):
65  return " " + " ".join([config.clang] + compile_flags) + " "
66
67config.substitutions.append( ("%clang ", build_invocation(clang_cflags)) )
68config.substitutions.append( ("%clangxx ", build_invocation(clang_cxxflags)) )
69config.substitutions.append( ("%clang_lsan ", build_invocation(clang_lsan_cflags)) )
70config.substitutions.append( ("%clangxx_lsan ", build_invocation(clang_lsan_cxxflags)) )
71
72# LeakSanitizer tests are currently supported on x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux and x86_64 Darwin.
73supported_linux = config.host_os is 'Linux' and config.host_arch in ['x86_64', 'ppc64', 'ppc64le', 'mips64', 'arm', 'armhf', 'armv7l', 's390x']
74supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64']
75supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386']
76if not (supported_linux or supported_darwin or supported_netbsd):
77  config.unsupported = True
78
79# Don't support Thumb due to broken fast unwinder
80if re.search('mthumb', config.target_cflags) is not None:
81  config.unsupported = True
82
83config.suffixes = ['.c', '.cpp', '.mm']
84