6 def get_required_attr(config
, attr_name
):
7 attr_value
= getattr(config
, attr_name
, None)
10 "No attribute %r in test configuration! You may need to run "
11 "tests from your build directory or add this attribute "
12 "to lit.site.cfg.py " % attr_name
18 config
.name
= "UBSan-" + config
.name_suffix
21 config
.test_source_root
= os
.path
.dirname(__file__
)
23 default_ubsan_opts
= list(config
.default_sanitizer_opts
)
24 # Choose between standalone and UBSan+ASan modes.
25 ubsan_lit_test_mode
= get_required_attr(config
, "ubsan_lit_test_mode")
26 if ubsan_lit_test_mode
== "Standalone":
27 config
.available_features
.add("ubsan-standalone")
28 clang_ubsan_cflags
= []
29 elif ubsan_lit_test_mode
== "StandaloneStatic":
30 config
.available_features
.add("ubsan-standalone-static")
31 clang_ubsan_cflags
= ["-static-libsan"]
32 elif ubsan_lit_test_mode
== "AddressSanitizer":
33 config
.available_features
.add("ubsan-asan")
34 clang_ubsan_cflags
= ["-fsanitize=address"]
35 default_ubsan_opts
+= ["detect_leaks=0"]
36 elif ubsan_lit_test_mode
== "MemorySanitizer":
37 config
.available_features
.add("ubsan-msan")
38 clang_ubsan_cflags
= ["-fsanitize=memory"]
39 elif ubsan_lit_test_mode
== "ThreadSanitizer":
40 config
.available_features
.add("ubsan-tsan")
41 clang_ubsan_cflags
= ["-fsanitize=thread"]
43 lit_config
.fatal("Unknown UBSan test mode: %r" % ubsan_lit_test_mode
)
45 # Platform-specific default for lit tests.
46 if config
.target_arch
== "s390x":
47 # On SystemZ we need -mbackchain to make the fast unwinder work.
48 clang_ubsan_cflags
.append("-mbackchain")
50 default_ubsan_opts_str
= ":".join(default_ubsan_opts
)
51 if default_ubsan_opts_str
:
52 config
.environment
["UBSAN_OPTIONS"] = default_ubsan_opts_str
53 default_ubsan_opts_str
+= ":"
54 # Substitution to setup UBSAN_OPTIONS in portable way.
55 config
.substitutions
.append(
56 ("%env_ubsan_opts=", "env UBSAN_OPTIONS=" + default_ubsan_opts_str
)
60 def build_invocation(compile_flags
):
61 return " " + " ".join([config
.clang
] + compile_flags
) + " "
64 target_cflags
= [get_required_attr(config
, "target_cflags")]
65 clang_ubsan_cflags
+= target_cflags
66 clang_ubsan_cxxflags
= config
.cxx_mode_flags
+ clang_ubsan_cflags
68 # Define %clang and %clangxx substitutions to use in test RUN lines.
69 config
.substitutions
.append(("%clang ", build_invocation(clang_ubsan_cflags
)))
70 config
.substitutions
.append(("%clangxx ", build_invocation(clang_ubsan_cxxflags
)))
71 config
.substitutions
.append(("%gmlt ", " ".join(config
.debug_info_flags
) + " "))
73 # Default test suffixes.
74 config
.suffixes
= [".c", ".cpp", ".m"]
76 # Check that the host supports UndefinedBehaviorSanitizer tests
77 if config
.host_os
not in [
86 config
.unsupported
= True
88 config
.excludes
= ["Inputs"]
90 if ubsan_lit_test_mode
in ["AddressSanitizer", "MemorySanitizer", "ThreadSanitizer"]:
91 if not config
.parallelism_group
:
92 config
.parallelism_group
= "shadow-memory"
93 if config
.host_os
== "NetBSD":
94 config
.substitutions
.insert(0, ("%run", config
.netbsd_noaslr_prefix
))