Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1 | # -*- Python -*- |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | def 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. |
| 15 | config.test_source_root = os.path.dirname(__file__) |
| 16 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 17 | default_ubsan_opts = [] |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 18 | # Choose between standalone and UBSan+ASan modes. |
| 19 | ubsan_lit_test_mode = get_required_attr(config, 'ubsan_lit_test_mode') |
| 20 | if ubsan_lit_test_mode == "Standalone": |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 21 | config.name = 'UBSan-Standalone-' + config.target_arch |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 22 | config.available_features.add("ubsan-standalone") |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 23 | clang_ubsan_cflags = [] |
| 24 | elif ubsan_lit_test_mode == "AddressSanitizer": |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 25 | config.name = 'UBSan-ASan-' + config.target_arch |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 26 | config.available_features.add("ubsan-asan") |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 27 | clang_ubsan_cflags = ["-fsanitize=address"] |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 28 | default_ubsan_opts += ['detect_leaks=0'] |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 29 | elif 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"] |
| 33 | elif 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"] |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 37 | else: |
| 38 | lit_config.fatal("Unknown UBSan test mode: %r" % ubsan_lit_test_mode) |
| 39 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 40 | # Platform-specific default for lit tests. |
| 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 | default_ubsan_opts += ['abort_on_error=0'] |
| 45 | default_ubsan_opts += ['log_to_syslog=0'] |
| 46 | default_ubsan_opts_str = ':'.join(default_ubsan_opts) |
| 47 | if 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. |
| 51 | config.substitutions.append(('%env_ubsan_opts=', |
| 52 | 'env UBSAN_OPTIONS=' + default_ubsan_opts_str)) |
| 53 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 54 | def build_invocation(compile_flags): |
| 55 | return " " + " ".join([config.clang] + compile_flags) + " " |
| 56 | |
| 57 | target_cflags = [get_required_attr(config, "target_cflags")] |
| 58 | clang_ubsan_cflags += target_cflags |
| 59 | clang_ubsan_cxxflags = config.cxx_mode_flags + clang_ubsan_cflags |
| 60 | |
| 61 | # Define %clang and %clangxx substitutions to use in test RUN lines. |
| 62 | config.substitutions.append( ("%clang ", build_invocation(clang_ubsan_cflags)) ) |
| 63 | config.substitutions.append( ("%clangxx ", build_invocation(clang_ubsan_cxxflags)) ) |
| 64 | |
| 65 | # Default test suffixes. |
| 66 | config.suffixes = ['.c', '.cc', '.cpp'] |
| 67 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 68 | # Check that the host supports UndefinedBehaviorSanitizer tests |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 69 | if config.host_os not in ['Linux', 'Darwin', 'FreeBSD', 'Windows']: |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 70 | config.unsupported = True |
| 71 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 72 | if config.host_os == 'Windows': |
| 73 | # We do not currently support enough of the Microsoft ABI for UBSan to work on |
| 74 | # Windows. |
| 75 | config.available_features.remove('cxxabi') |
| 76 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 77 | # Allow tests to use REQUIRES=stable-runtime. For use when you cannot use XFAIL |
| 78 | # because the test hangs or fails on one configuration and not the other. |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 79 | if config.target_arch.startswith('arm') == False and config.target_arch != 'aarch64': |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 80 | config.available_features.add('stable-runtime') |