1# -*- Python -*-
2
3import os
4
5def get_required_attr(config, attr_name):
6  attr_value = getattr(config, attr_name, None)
7  if attr_value == None:
8    lit_config.fatal(
9      "No attribute %r in test configuration! You may need to run "
10      "tests from your build directory or add this attribute "
11      "to lit.site.cfg " % attr_name)
12  return attr_value
13
14# Setup source root.
15config.test_source_root = os.path.dirname(__file__)
16
17default_ubsan_opts = []
18# Choose between standalone and UBSan+ASan modes.
19ubsan_lit_test_mode = get_required_attr(config, 'ubsan_lit_test_mode')
20if ubsan_lit_test_mode == "Standalone":
21  config.name = 'UBSan-Standalone-' + config.target_arch
22  config.available_features.add("ubsan-standalone")
23  clang_ubsan_cflags = []
24elif ubsan_lit_test_mode == "AddressSanitizer":
25  config.name = 'UBSan-ASan-' + config.target_arch
26  config.available_features.add("ubsan-asan")
27  clang_ubsan_cflags = ["-fsanitize=address"]
28  default_ubsan_opts += ['detect_leaks=0']
29elif ubsan_lit_test_mode == "MemorySanitizer":
30  config.name = 'UBSan-MSan-' + config.target_arch
31  config.available_features.add("ubsan-msan")
32  clang_ubsan_cflags = ["-fsanitize=memory"]
33elif ubsan_lit_test_mode == "ThreadSanitizer":
34  config.name = 'UBSan-TSan-' + config.target_arch
35  config.available_features.add("ubsan-tsan")
36  clang_ubsan_cflags = ["-fsanitize=thread"]
37else:
38  lit_config.fatal("Unknown UBSan test mode: %r" % ubsan_lit_test_mode)
39
40# Platform-specific default for lit tests.
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  default_ubsan_opts += ['abort_on_error=0']
45  default_ubsan_opts += ['log_to_syslog=0']
46default_ubsan_opts_str = ':'.join(default_ubsan_opts)
47if default_ubsan_opts_str:
48  config.environment['UBSAN_OPTIONS'] = default_ubsan_opts_str
49  default_ubsan_opts_str += ':'
50# Substitution to setup UBSAN_OPTIONS in portable way.
51config.substitutions.append(('%env_ubsan_opts=',
52                             'env UBSAN_OPTIONS=' + default_ubsan_opts_str))
53
54def build_invocation(compile_flags):
55  return " " + " ".join([config.clang] + compile_flags) + " "
56
57target_cflags = [get_required_attr(config, "target_cflags")]
58clang_ubsan_cflags += target_cflags
59clang_ubsan_cxxflags = config.cxx_mode_flags + clang_ubsan_cflags
60
61# Define %clang and %clangxx substitutions to use in test RUN lines.
62config.substitutions.append( ("%clang ", build_invocation(clang_ubsan_cflags)) )
63config.substitutions.append( ("%clangxx ", build_invocation(clang_ubsan_cxxflags)) )
64
65# Default test suffixes.
66config.suffixes = ['.c', '.cc', '.cpp']
67
68# Check that the host supports UndefinedBehaviorSanitizer tests
69if config.host_os not in ['Linux', 'Darwin', 'FreeBSD', 'Windows']:
70  config.unsupported = True
71
72# Allow tests to use REQUIRES=stable-runtime.  For use when you cannot use XFAIL
73# because the test hangs or fails on one configuration and not the other.
74if config.target_arch.startswith('arm') == False and config.target_arch != 'aarch64':
75  config.available_features.add('stable-runtime')
76