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