1# -*- Python -*-
2
3import os
4import platform
5
6import lit.formats
7
8def get_required_attr(config, attr_name):
9  attr_value = getattr(config, attr_name, None)
10  if attr_value == None:
11    lit_config.fatal(
12      "No attribute %r in test configuration! You may need to run "
13      "tests from your build directory or add this attribute "
14      "to lit.site.cfg " % attr_name)
15  return attr_value
16
17def push_dynamic_library_lookup_path(config, new_path):
18  if platform.system() == 'Windows':
19    dynamic_library_lookup_var = 'PATH'
20  elif platform.system() == 'Darwin':
21    dynamic_library_lookup_var = 'DYLD_LIBRARY_PATH'
22  else:
23    dynamic_library_lookup_var = 'LD_LIBRARY_PATH'
24
25  new_ld_library_path = os.path.pathsep.join(
26    (new_path, config.environment.get(dynamic_library_lookup_var, '')))
27  config.environment[dynamic_library_lookup_var] = new_ld_library_path
28
29# Setup config name.
30config.name = 'AddressSanitizer' + config.name_suffix
31
32# testFormat: The test format to use to interpret tests.
33external_bash = (not sys.platform in ['win32'])
34config.test_format = lit.formats.ShTest(external_bash)
35
36# Setup source root.
37config.test_source_root = os.path.dirname(__file__)
38
39# There is no libdl on FreeBSD.
40if config.host_os != 'FreeBSD':
41  libdl_flag = "-ldl"
42else:
43  libdl_flag = ""
44
45# GCC-ASan doesn't link in all the necessary libraries automatically, so
46# we have to do it ourselves.
47if config.compiler_id == 'GNU':
48  extra_linkflags = ["-pthread", "-lstdc++", libdl_flag]
49else:
50  extra_linkflags = []
51
52# Setup default compiler flags used with -fsanitize=address option.
53# FIXME: Review the set of required flags and check if it can be reduced.
54target_cflags = [get_required_attr(config, "target_cflags")] + extra_linkflags
55target_cxxflags = config.cxx_mode_flags + target_cflags
56clang_asan_static_cflags = (["-fsanitize=address",
57                            "-mno-omit-leaf-frame-pointer",
58                            "-fno-omit-frame-pointer",
59                            "-fno-optimize-sibling-calls"] +
60                            config.debug_info_flags + target_cflags)
61clang_asan_static_cxxflags = config.cxx_mode_flags + clang_asan_static_cflags
62
63if config.asan_dynamic:
64  clang_asan_cflags = clang_asan_static_cflags + ['-shared-libasan']
65  clang_asan_cxxflags = clang_asan_static_cxxflags + ['-shared-libasan']
66  config.available_features.add("asan-dynamic-runtime")
67else:
68  clang_asan_cflags = clang_asan_static_cflags
69  clang_asan_cxxflags = clang_asan_static_cxxflags
70  config.available_features.add("asan-static-runtime")
71
72asan_lit_source_dir = get_required_attr(config, "asan_lit_source_dir")
73if config.android == "1":
74  config.available_features.add('android')
75  clang_wrapper = os.path.join(asan_lit_source_dir,
76                               "android_commands", "android_compile.py") + " "
77else:
78  config.available_features.add('not-android')
79  clang_wrapper = ""
80
81def build_invocation(compile_flags):
82  return " " + " ".join([clang_wrapper, config.clang] + compile_flags) + " "
83
84config.substitutions.append( ("%clang ", build_invocation(target_cflags)) )
85config.substitutions.append( ("%clangxx ", build_invocation(target_cxxflags)) )
86config.substitutions.append( ("%clang_asan ", build_invocation(clang_asan_cflags)) )
87config.substitutions.append( ("%clangxx_asan ", build_invocation(clang_asan_cxxflags)) )
88config.substitutions.append( ("%shared_libasan", "libclang_rt.asan-%s.so" % config.target_arch))
89if config.asan_dynamic:
90  config.substitutions.append( ("%clang_asan_static ", build_invocation(clang_asan_static_cflags)) )
91  config.substitutions.append( ("%clangxx_asan_static ", build_invocation(clang_asan_static_cxxflags)) )
92
93# Windows-specific tests might also use the clang-cl.exe driver.
94if platform.system() == 'Windows':
95  clang_cl_asan_cxxflags = ["-fsanitize=address",
96                            "-Wno-deprecated-declarations",
97                            "-WX",
98                            "-D_HAS_EXCEPTIONS=0",
99                            "-Zi"] + target_cflags
100  if config.asan_dynamic:
101    clang_cl_asan_cxxflags.append("-MD")
102  clang_invocation = build_invocation(clang_cl_asan_cxxflags)
103  clang_cl_invocation = clang_invocation.replace("clang.exe","clang-cl.exe")
104  config.substitutions.append( ("%clang_cl_asan ", clang_cl_invocation) )
105  config.substitutions.append( ("%asan_dll_thunk",
106                               os.path.join(config.compiler_rt_libdir, "clang_rt.asan_dll_thunk-i386.lib")))
107
108# FIXME: De-hardcode this path.
109asan_source_dir = os.path.join(
110  get_required_attr(config, "compiler_rt_src_root"), "lib", "asan")
111# Setup path to asan_symbolize.py script.
112asan_symbolize = os.path.join(asan_source_dir, "scripts", "asan_symbolize.py")
113if not os.path.exists(asan_symbolize):
114  lit_config.fatal("Can't find script on path %r" % asan_symbolize)
115python_exec = get_required_attr(config, "python_executable")
116config.substitutions.append( ("%asan_symbolize", python_exec + " " + asan_symbolize + " ") )
117# Setup path to sancov.py script.
118sanitizer_common_source_dir = os.path.join(
119  get_required_attr(config, "compiler_rt_src_root"), "lib", "sanitizer_common")
120sancov = os.path.join(sanitizer_common_source_dir, "scripts", "sancov.py")
121if not os.path.exists(sancov):
122  lit_config.fatal("Can't find script on path %r" % sancov)
123python_exec = get_required_attr(config, "python_executable")
124config.substitutions.append( ("%sancov", python_exec + " " + sancov + " ") )
125
126# Determine kernel bitness
127if config.host_arch.find('64') != -1 and config.android != "1":
128  kernel_bits = '64'
129else:
130  kernel_bits = '32'
131
132config.substitutions.append( ('CHECK-%kernel_bits', ("CHECK-kernel-" + kernel_bits + "-bits")))
133
134config.substitutions.append( ("%libdl", libdl_flag) )
135
136config.available_features.add("asan-" + config.bits + "-bits")
137
138if config.host_os == 'Darwin':
139  config.substitutions.append( ("%ld_flags_rpath_exe", '-Wl,-rpath,@executable_path/ %dynamiclib') )
140  config.substitutions.append( ("%ld_flags_rpath_so", '-install_name @rpath/`basename %dynamiclib`') )
141elif config.host_os in ['Linux', 'FreeBSD']:
142  config.substitutions.append( ("%ld_flags_rpath_exe", "-Wl,-rpath,\$ORIGIN -L%T -l%xdynamiclib_namespec") )
143  config.substitutions.append( ("%ld_flags_rpath_so", '') )
144
145# Must be defined after the substitutions that use %dynamiclib.
146config.substitutions.append( ("%dynamiclib", '%T/lib%xdynamiclib_namespec.so') )
147config.substitutions.append( ("%xdynamiclib_namespec", '$(basename %t).dynamic') )
148
149# Allow tests to use REQUIRES=stable-runtime.  For use when you cannot use XFAIL
150# because the test hangs.
151if config.target_arch != 'arm':
152  config.available_features.add('stable-runtime')
153
154# Turn on leak detection on 64-bit Linux.
155if config.host_os == 'Linux' and config.target_arch == 'x86_64':
156  config.available_features.add('leak-detection')
157
158# Set LD_LIBRARY_PATH to pick dynamic runtime up properly.
159push_dynamic_library_lookup_path(config, config.compiler_rt_libdir)
160
161# GCC-ASan uses dynamic runtime by default.
162if config.compiler_id == 'GNU':
163  gcc_dir = os.path.dirname(config.clang)
164  libasan_dir = os.path.join(gcc_dir, "..", "lib" + config.bits)
165  push_dynamic_library_lookup_path(config, libasan_dir)
166
167# Default test suffixes.
168config.suffixes = ['.c', '.cc', '.cpp']
169
170if config.host_os == 'Darwin':
171  config.suffixes.append('.mm')
172
173# AddressSanitizer tests are currently supported on Linux, Darwin and
174# FreeBSD only.
175if config.host_os not in ['Linux', 'Darwin', 'FreeBSD']:
176  config.unsupported = True
177