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