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') 24 25if lsan_lit_test_mode == "Standalone": 26 config.name = "LeakSanitizer-Standalone" 27 lsan_cflags = ["-fsanitize=leak"] 28elif 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)) 34else: 35 lit_config.fatal("Unknown LSan test mode: %r" % lsan_lit_test_mode) 36config.name += config.name_suffix 37 38# Platform-specific default LSAN_OPTIONS for lit tests. 39default_common_opts_str = ':'.join(list(config.default_sanitizer_opts)) 40default_lsan_opts = default_common_opts_str + ':detect_leaks=1' 41if 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' 47 48if default_lsan_opts: 49 config.environment['LSAN_OPTIONS'] = default_lsan_opts 50 default_lsan_opts += ':' 51config.substitutions.append(('%env_lsan_opts=', 52 'env LSAN_OPTIONS=' + default_lsan_opts)) 53 54if lit.util.which('strace'): 55 config.available_features.add('strace') 56 57clang_cflags = ["-O0", config.target_cflags] + config.debug_info_flags 58if config.android: 59 clang_cflags = clang_cflags + ["-fno-emulated-tls"] 60clang_cxxflags = config.cxx_mode_flags + clang_cflags 61lsan_incdir = config.test_source_root + "/../" 62clang_lsan_cflags = clang_cflags + lsan_cflags + ["-I%s" % lsan_incdir] 63clang_lsan_cxxflags = clang_cxxflags + lsan_cflags + ["-I%s" % lsan_incdir] 64 65config.clang_cflags = clang_cflags 66config.clang_cxxflags = clang_cxxflags 67 68def build_invocation(compile_flags): 69 return " " + " ".join([config.clang] + compile_flags) + " " 70 71config.substitutions.append( ("%clang ", build_invocation(clang_cflags)) ) 72config.substitutions.append( ("%clangxx ", build_invocation(clang_cxxflags)) ) 73config.substitutions.append( ("%clang_lsan ", build_invocation(clang_lsan_cflags)) ) 74config.substitutions.append( ("%clangxx_lsan ", build_invocation(clang_lsan_cxxflags)) ) 75 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. 78supported_android = config.android and config.target_arch in ['x86_64', 'i386', 'aarch64'] and 'android-thread-properties-api' in config.available_features 79supported_linux = (not config.android) and config.host_os == 'Linux' and config.host_arch in ['x86_64', 'ppc64', 'ppc64le', 'mips64', 'arm', 'armhf', 'armv7l', 's390x'] 80supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64'] 81supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386'] 82if not (supported_android or supported_linux or supported_darwin or supported_netbsd): 83 config.unsupported = True 84 85# Don't support Thumb due to broken fast unwinder 86if re.search('mthumb', config.target_cflags) is not None: 87 config.unsupported = True 88 89config.suffixes = ['.c', '.cpp', '.mm'] 90