1# -*- Python -*- 2 3import os 4import platform 5 6def get_required_attr(config, attr_name): 7 attr_value = getattr(config, attr_name, None) 8 if attr_value == None: 9 lit_config.fatal( 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 " % attr_name) 13 return attr_value 14 15def push_ld_library_path(config, new_path): 16 new_ld_library_path = os.path.pathsep.join( 17 (new_path, config.environment['LD_LIBRARY_PATH'])) 18 config.environment['LD_LIBRARY_PATH'] = new_ld_library_path 19 20# Setup config name. 21config.name = 'AddressSanitizer' + config.name_suffix 22 23# Setup source root. 24config.test_source_root = os.path.dirname(__file__) 25 26# GCC-ASan doesn't link in all the necessary libraries automatically, so 27# we have to do it ourselves. 28if config.compiler_id == 'GNU': 29 extra_linkflags = ["-pthread", "-lstdc++", "-ldl"] 30else: 31 extra_linkflags = [] 32# Setup default compiler flags used with -fsanitize=address option. 33# FIXME: Review the set of required flags and check if it can be reduced. 34target_cflags = [get_required_attr(config, "target_cflags")] + extra_linkflags 35target_cxxflags = config.cxx_mode_flags + target_cflags 36clang_asan_static_cflags = ["-fsanitize=address", 37 "-mno-omit-leaf-frame-pointer", 38 "-fno-omit-frame-pointer", 39 "-fno-optimize-sibling-calls", 40 "-g"] + target_cflags 41clang_asan_static_cxxflags = config.cxx_mode_flags + clang_asan_static_cflags 42 43if config.asan_dynamic: 44 clang_asan_cflags = clang_asan_static_cflags + ['-shared-libasan'] 45 clang_asan_cxxflags = clang_asan_static_cxxflags + ['-shared-libasan'] 46 config.available_features.add("asan-dynamic-runtime") 47else: 48 clang_asan_cflags = clang_asan_static_cflags 49 clang_asan_cxxflags = clang_asan_static_cxxflags 50 config.available_features.add("asan-static-runtime") 51 52asan_lit_source_dir = get_required_attr(config, "asan_lit_source_dir") 53if config.android == "TRUE": 54 config.available_features.add('android') 55 clang_wrapper = os.path.join(asan_lit_source_dir, 56 "android_commands", "android_compile.py") + " " 57else: 58 clang_wrapper = "" 59 60def build_invocation(compile_flags): 61 return " " + " ".join([clang_wrapper, config.clang] + compile_flags) + " " 62 63config.substitutions.append( ("%clang ", build_invocation(target_cflags)) ) 64config.substitutions.append( ("%clangxx ", build_invocation(target_cxxflags)) ) 65config.substitutions.append( ("%clang_asan ", build_invocation(clang_asan_cflags)) ) 66config.substitutions.append( ("%clangxx_asan ", build_invocation(clang_asan_cxxflags)) ) 67config.substitutions.append( ("%shared_libasan", "libclang_rt.asan-%s.so" % config.target_arch)) 68if config.asan_dynamic: 69 config.substitutions.append( ("%clang_asan_static ", build_invocation(clang_asan_static_cflags)) ) 70 config.substitutions.append( ("%clangxx_asan_static ", build_invocation(clang_asan_static_cxxflags)) ) 71 72# Windows-specific tests might also use the clang-cl.exe driver. 73if platform.system() == 'Windows': 74 clang_cl_asan_cxxflags = ["-fsanitize=address", 75 "-Wno-deprecated-declarations", 76 "-WX", 77 "-D_HAS_EXCEPTIONS=0", 78 "-Zi"] + target_cflags 79 clang_invocation = build_invocation(clang_cl_asan_cxxflags) 80 clang_cl_invocation = clang_invocation.replace("clang.exe","clang-cl.exe") 81 config.substitutions.append( ("%clang_cl_asan ", clang_cl_invocation) ) 82 83# FIXME: De-hardcode this path. 84asan_source_dir = os.path.join( 85 get_required_attr(config, "compiler_rt_src_root"), "lib", "asan") 86# Setup path to asan_symbolize.py script. 87asan_symbolize = os.path.join(asan_source_dir, "scripts", "asan_symbolize.py") 88if not os.path.exists(asan_symbolize): 89 lit_config.fatal("Can't find script on path %r" % asan_symbolize) 90python_exec = get_required_attr(config, "python_executable") 91config.substitutions.append( ("%asan_symbolize", python_exec + " " + asan_symbolize + " ") ) 92# Setup path to sancov.py script. 93sanitizer_common_source_dir = os.path.join( 94 get_required_attr(config, "compiler_rt_src_root"), "lib", "sanitizer_common") 95sancov = os.path.join(sanitizer_common_source_dir, "scripts", "sancov.py") 96if not os.path.exists(sancov): 97 lit_config.fatal("Can't find script on path %r" % sancov) 98python_exec = get_required_attr(config, "python_executable") 99config.substitutions.append( ("%sancov", python_exec + " " + sancov + " ") ) 100 101# Determine kernel bitness 102if config.host_arch.find('64') != -1 and config.android != "TRUE": 103 kernel_bits = '64' 104else: 105 kernel_bits = '32' 106 107# Define CHECK-%os to check for OS-dependent output. 108config.substitutions.append( ('CHECK-%os', ("CHECK-" + config.host_os))) 109config.substitutions.append( ('CHECK-%kernel_bits', ("CHECK-kernel-" + kernel_bits + "-bits"))) 110 111config.available_features.add("asan-" + config.bits + "-bits") 112 113# Allow tests to use REQUIRES=stable-runtime. For use when you cannot use XFAIL 114# because the test hangs. 115if config.target_arch != 'arm': 116 config.available_features.add('stable-runtime') 117 118# Turn on leak detection on 64-bit Linux. 119if config.host_os == 'Linux' and config.bits == '64': 120 config.environment['ASAN_OPTIONS'] = 'detect_leaks=1' 121 122# Set LD_LIBRARY_PATH to pick dynamic runtime up properly. 123push_ld_library_path(config, config.compiler_rt_libdir) 124 125# GCC-ASan uses dynamic runtime by default. 126if config.compiler_id == 'GNU': 127 gcc_dir = os.path.dirname(config.clang) 128 libasan_dir = os.path.join(gcc_dir, "..", "lib" + config.bits) 129 push_ld_library_path(config, libasan_dir) 130 131# Default test suffixes. 132config.suffixes = ['.c', '.cc', '.cpp'] 133 134if config.host_os == 'Darwin': 135 config.suffixes.append('.mm') 136 137# AddressSanitizer tests are currently supported on Linux and Darwin only. 138if config.host_os not in ['Linux', 'Darwin']: 139 config.unsupported = True 140