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