llvm-project/compiler-rt/test/asan/lit.cfg

253 lines
11 KiB
INI
Raw Normal View History

# -*- Python -*-
import os
import platform
import lit.formats
def get_required_attr(config, attr_name):
attr_value = getattr(config, attr_name, None)
if attr_value == None:
lit_config.fatal(
"No attribute %r in test configuration! You may need to run "
"tests from your build directory or add this attribute "
"to lit.site.cfg " % attr_name)
return attr_value
def push_dynamic_library_lookup_path(config, new_path):
if platform.system() == 'Windows':
dynamic_library_lookup_var = 'PATH'
elif platform.system() == 'Darwin':
dynamic_library_lookup_var = 'DYLD_LIBRARY_PATH'
else:
dynamic_library_lookup_var = 'LD_LIBRARY_PATH'
new_ld_library_path = os.path.pathsep.join(
(new_path, config.environment.get(dynamic_library_lookup_var, '')))
config.environment[dynamic_library_lookup_var] = new_ld_library_path
# Setup config name.
config.name = 'AddressSanitizer' + config.name_suffix
# Platform-specific default ASAN_OPTIONS for lit tests.
default_asan_opts = ''
if config.host_os == 'Darwin':
# On Darwin, we default to `abort_on_error=1`, which would make tests run
# much slower. Let's override this and run lit tests with 'abort_on_error=0'.
# Also, make sure we do not overwhelm the syslog while testing.
default_asan_opts = 'abort_on_error=0'
default_asan_opts += ':log_to_syslog=0'
elif config.android:
# The same as on Darwin, we default to "abort_on_error=1" which slows down
# testing. Also, all existing tests are using "not" instead of "not --crash"
# which does not work for abort()-terminated programs.
default_asan_opts = 'abort_on_error=0'
if default_asan_opts:
config.environment['ASAN_OPTIONS'] = default_asan_opts
default_asan_opts += ':'
config.substitutions.append(('%env_asan_opts=',
'env ASAN_OPTIONS=' + default_asan_opts))
# Setup source root.
config.test_source_root = os.path.dirname(__file__)
# There is no libdl on FreeBSD.
if config.host_os != 'FreeBSD':
libdl_flag = "-ldl"
else:
libdl_flag = ""
# GCC-ASan doesn't link in all the necessary libraries automatically, so
# we have to do it ourselves.
if config.compiler_id == 'GNU':
extra_link_flags = ["-pthread", "-lstdc++", libdl_flag]
else:
extra_link_flags = []
# BFD linker in 64-bit android toolchains fails to find libm.so, which is a
# transitive shared library dependency (via asan runtime).
if config.android:
extra_link_flags += ["-lm"]
# Setup default compiler flags used with -fsanitize=address option.
# FIXME: Review the set of required flags and check if it can be reduced.
target_cflags = [get_required_attr(config, "target_cflags")] + extra_link_flags
target_cxxflags = config.cxx_mode_flags + target_cflags
clang_asan_static_cflags = (["-fsanitize=address",
"-mno-omit-leaf-frame-pointer",
"-fno-omit-frame-pointer",
"-fno-optimize-sibling-calls"] +
config.debug_info_flags + target_cflags)
if config.target_arch == 's390x':
clang_asan_static_cflags.append("-mbackchain")
clang_asan_static_cxxflags = config.cxx_mode_flags + clang_asan_static_cflags
asan_dynamic_flags = []
if config.asan_dynamic:
asan_dynamic_flags = ["-shared-libasan"]
# On Windows, we need to simulate "clang-cl /MD" on the clang driver side.
if platform.system() == 'Windows':
asan_dynamic_flags += ["-D_MT", "-D_DLL", "-Wl,-nodefaultlib:libcmt,-defaultlib:msvcrt,-defaultlib:oldnames"]
config.available_features.add("asan-dynamic-runtime")
else:
config.available_features.add("asan-static-runtime")
clang_asan_cflags = clang_asan_static_cflags + asan_dynamic_flags
clang_asan_cxxflags = clang_asan_static_cxxflags + asan_dynamic_flags
# Add win32-(static|dynamic)-asan features to mark tests as passing or failing
# in those modes. lit doesn't support logical feature test combinations.
if platform.system() == 'Windows':
if config.asan_dynamic:
win_runtime_feature = "win32-dynamic-asan"
else:
win_runtime_feature = "win32-static-asan"
config.available_features.add(win_runtime_feature)
asan_lit_source_dir = get_required_attr(config, "asan_lit_source_dir")
if config.android == "1":
config.available_features.add('android')
clang_wrapper = os.path.join(asan_lit_source_dir,
"android_commands", "android_compile.py") + " "
else:
config.available_features.add('not-android')
clang_wrapper = ""
def build_invocation(compile_flags):
return " " + " ".join([clang_wrapper, config.clang] + compile_flags) + " "
[compiler-rt] Fix a broken asan 64-bit test using ld_preload Summary: The 'asan_preload_test-1.cc' is not working with the i686 architecture. To repro the error, run on a linux 64-bit: ``` ninja check-asan-dynamic ``` The following error occurs: ``` -- Exit Code: 1 Command Output (stderr): -- /home/llvm/llvm/projects/compiler-rt/test/asan/TestCases/Linux/asan_preload_test-1.cc:18:12: error: expected string not found in input // CHECK: AddressSanitizer: heap-buffer-overflow ^ <stdin>:1:1: note: scanning from here ERROR: ld.so: object 'libclang_rt.asan-i686.so' from LD_PRELOAD cannot be preloaded (wrong ELF class: ELFCLASS32): ignored. ^ <stdin>:2:10: note: possible intended match here ==25982==AddressSanitizer CHECK failed: /home/llvm/llvm/projects/compiler-rt/lib/asan/asan_interceptors.cc:736 "((__interception::real_memcpy)) != (0)" (0x0, 0x0) ``` The unittest is running (where %shared_libasan is replaced by libclang_rt.asan-i686.so): ``` // RUN: env LD_PRELOAD=%shared_libasan not %run %t 2>&1 | FileCheck %s ``` But the executable also has a dependancy on libclang_rt.asan-i386.so (added by the clang driver): ``` linux-gate.so.1 => (0xf77cc000) libclang_rt.asan-i386.so => not found libstdc++.so.6 => /usr/lib/i386-linux-gnu/libstdc++.so.6 (0xf76ba000) libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0xf7673000) libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0xf7656000) libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xf74a7000) ``` By looking to the clang driver (tools.cpp) we can see that every x86 architecture are mapped to 'i386'. ``` StringRef MyArch; switch (getToolChain().getArch()) { case llvm::Triple::arm: MyArch = "arm"; break; case llvm::Triple::x86: MyArch = "i386"; break; case llvm::Triple::x86_64: MyArch = "amd64"; break; default: llvm_unreachable("Unsupported architecture"); } ``` This patch is implementing the same mapping but in the compiler-rt unittest. Reviewers: rnk, vitalybuka Subscribers: aemerson, kubabrecka, dberris, llvm-commits, chrisha Differential Revision: https://reviews.llvm.org/D24838 llvm-svn: 282263
2016-09-24 01:40:31 +08:00
# Clang driver link 'x86' (i686) architecture to 'i386'.
target_arch = config.target_arch
if (target_arch == "i686"):
target_arch = "i386"
config.substitutions.append( ("%clang ", build_invocation(target_cflags)) )
config.substitutions.append( ("%clangxx ", build_invocation(target_cxxflags)) )
config.substitutions.append( ("%clang_asan ", build_invocation(clang_asan_cflags)) )
config.substitutions.append( ("%clangxx_asan ", build_invocation(clang_asan_cxxflags)) )
[compiler-rt] Fix a broken asan 64-bit test using ld_preload Summary: The 'asan_preload_test-1.cc' is not working with the i686 architecture. To repro the error, run on a linux 64-bit: ``` ninja check-asan-dynamic ``` The following error occurs: ``` -- Exit Code: 1 Command Output (stderr): -- /home/llvm/llvm/projects/compiler-rt/test/asan/TestCases/Linux/asan_preload_test-1.cc:18:12: error: expected string not found in input // CHECK: AddressSanitizer: heap-buffer-overflow ^ <stdin>:1:1: note: scanning from here ERROR: ld.so: object 'libclang_rt.asan-i686.so' from LD_PRELOAD cannot be preloaded (wrong ELF class: ELFCLASS32): ignored. ^ <stdin>:2:10: note: possible intended match here ==25982==AddressSanitizer CHECK failed: /home/llvm/llvm/projects/compiler-rt/lib/asan/asan_interceptors.cc:736 "((__interception::real_memcpy)) != (0)" (0x0, 0x0) ``` The unittest is running (where %shared_libasan is replaced by libclang_rt.asan-i686.so): ``` // RUN: env LD_PRELOAD=%shared_libasan not %run %t 2>&1 | FileCheck %s ``` But the executable also has a dependancy on libclang_rt.asan-i386.so (added by the clang driver): ``` linux-gate.so.1 => (0xf77cc000) libclang_rt.asan-i386.so => not found libstdc++.so.6 => /usr/lib/i386-linux-gnu/libstdc++.so.6 (0xf76ba000) libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0xf7673000) libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0xf7656000) libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xf74a7000) ``` By looking to the clang driver (tools.cpp) we can see that every x86 architecture are mapped to 'i386'. ``` StringRef MyArch; switch (getToolChain().getArch()) { case llvm::Triple::arm: MyArch = "arm"; break; case llvm::Triple::x86: MyArch = "i386"; break; case llvm::Triple::x86_64: MyArch = "amd64"; break; default: llvm_unreachable("Unsupported architecture"); } ``` This patch is implementing the same mapping but in the compiler-rt unittest. Reviewers: rnk, vitalybuka Subscribers: aemerson, kubabrecka, dberris, llvm-commits, chrisha Differential Revision: https://reviews.llvm.org/D24838 llvm-svn: 282263
2016-09-24 01:40:31 +08:00
config.substitutions.append( ("%shared_libasan", "libclang_rt.asan-%s.so" % target_arch))
if config.asan_dynamic:
config.substitutions.append( ("%clang_asan_static ", build_invocation(clang_asan_static_cflags)) )
config.substitutions.append( ("%clangxx_asan_static ", build_invocation(clang_asan_static_cxxflags)) )
# Windows-specific tests might also use the clang-cl.exe driver.
if platform.system() == 'Windows':
clang_cl_cxxflags = ["-Wno-deprecated-declarations",
"-WX",
"-D_HAS_EXCEPTIONS=0",
"-Zi"] + target_cflags
clang_cl_asan_cxxflags = ["-fsanitize=address"] + clang_cl_cxxflags
if config.asan_dynamic:
clang_cl_asan_cxxflags.append("-MD")
clang_cl_invocation = build_invocation(clang_cl_cxxflags)
clang_cl_invocation = clang_cl_invocation.replace("clang.exe","clang-cl.exe")
config.substitutions.append( ("%clang_cl ", clang_cl_invocation) )
clang_cl_asan_invocation = build_invocation(clang_cl_asan_cxxflags)
clang_cl_asan_invocation = clang_cl_asan_invocation.replace("clang.exe","clang-cl.exe")
config.substitutions.append( ("%clang_cl_asan ", clang_cl_asan_invocation) )
base_lib = os.path.join(config.compiler_rt_libdir, "clang_rt.asan%%s-%s.lib" % config.target_arch)
config.substitutions.append( ("%asan_lib", base_lib % "") )
config.substitutions.append( ("%asan_cxx_lib", base_lib % "_cxx") )
config.substitutions.append( ("%asan_dll_thunk", base_lib % "_dll_thunk") )
if platform.system() == 'Windows':
# Don't use -std=c++11 on Windows, as the driver will detect the appropriate
# default needed to use with the STL.
config.substitutions.append(("%stdcxx11 ", ""))
else:
# Some tests uses C++11 features such as lambdas and need to pass -std=c++11.
config.substitutions.append(("%stdcxx11 ", "-std=c++11 "))
# FIXME: De-hardcode this path.
asan_source_dir = os.path.join(
get_required_attr(config, "compiler_rt_src_root"), "lib", "asan")
# Setup path to asan_symbolize.py script.
asan_symbolize = os.path.join(asan_source_dir, "scripts", "asan_symbolize.py")
if not os.path.exists(asan_symbolize):
lit_config.fatal("Can't find script on path %r" % asan_symbolize)
python_exec = get_required_attr(config, "python_executable")
config.substitutions.append( ("%asan_symbolize", python_exec + " " + asan_symbolize + " ") )
# Setup path to sancov.py script.
sanitizer_common_source_dir = os.path.join(
get_required_attr(config, "compiler_rt_src_root"), "lib", "sanitizer_common")
sancov = os.path.join(sanitizer_common_source_dir, "scripts", "sancov.py")
if not os.path.exists(sancov):
lit_config.fatal("Can't find script on path %r" % sancov)
python_exec = get_required_attr(config, "python_executable")
config.substitutions.append( ("%sancov ", python_exec + " " + sancov + " ") )
# Determine kernel bitness
if config.host_arch.find('64') != -1 and config.android != "1":
kernel_bits = '64'
else:
kernel_bits = '32'
config.substitutions.append( ('CHECK-%kernel_bits', ("CHECK-kernel-" + kernel_bits + "-bits")))
config.substitutions.append( ("%libdl", libdl_flag) )
config.available_features.add("asan-" + config.bits + "-bits")
if config.host_os == 'Darwin':
config.substitutions.append( ("%ld_flags_rpath_exe", '-Wl,-rpath,@executable_path/ %dynamiclib') )
config.substitutions.append( ("%ld_flags_rpath_so", '-install_name @rpath/`basename %dynamiclib`') )
elif config.host_os == 'FreeBSD':
config.substitutions.append( ("%ld_flags_rpath_exe", "-Wl,-z,origin -Wl,-rpath,\$ORIGIN -L%T -l%xdynamiclib_namespec") )
config.substitutions.append( ("%ld_flags_rpath_so", '') )
elif config.host_os == 'Linux':
config.substitutions.append( ("%ld_flags_rpath_exe", "-Wl,-rpath,\$ORIGIN -L%T -l%xdynamiclib_namespec") )
config.substitutions.append( ("%ld_flags_rpath_so", '') )
# Must be defined after the substitutions that use %dynamiclib.
config.substitutions.append( ("%dynamiclib", '%T/lib%xdynamiclib_namespec.so') )
config.substitutions.append( ("%xdynamiclib_namespec", '$(basename %t).dynamic') )
# Allow tests to use REQUIRES=stable-runtime. For use when you cannot use XFAIL
# because the test hangs. Adding armhf as we now have two modes.
if config.target_arch != 'arm' and config.target_arch != 'armhf' and config.target_arch != 'aarch64':
config.available_features.add('stable-runtime')
# Turn on leak detection on 64-bit Linux.
if config.host_os == 'Linux' and (config.target_arch == 'x86_64' or config.target_arch == 'i386'):
config.available_features.add('leak-detection')
# Set LD_LIBRARY_PATH to pick dynamic runtime up properly.
push_dynamic_library_lookup_path(config, config.compiler_rt_libdir)
# GCC-ASan uses dynamic runtime by default.
if config.compiler_id == 'GNU':
gcc_dir = os.path.dirname(config.clang)
libasan_dir = os.path.join(gcc_dir, "..", "lib" + config.bits)
push_dynamic_library_lookup_path(config, libasan_dir)
# Add the RT libdir to PATH directly so that we can successfully run the gtest
# binary to list its tests.
if config.host_os == 'Windows' and config.asan_dynamic:
os.environ['PATH'] = os.path.pathsep.join([config.compiler_rt_libdir,
os.environ.get('PATH', '')])
# Default test suffixes.
config.suffixes = ['.c', '.cc', '.cpp']
if config.host_os == 'Darwin':
config.suffixes.append('.mm')
if config.host_os == 'Windows':
config.substitutions.append(('%fPIC', ''))
config.substitutions.append(('%fPIE', ''))
config.substitutions.append(('%pie', ''))
else:
config.substitutions.append(('%fPIC', '-fPIC'))
config.substitutions.append(('%fPIE', '-fPIE'))
config.substitutions.append(('%pie', '-pie'))
# Only run the tests on supported OSs.
if config.host_os not in ['Linux', 'Darwin', 'FreeBSD', 'Windows']:
config.unsupported = True
if config.host_os == 'Darwin' and config.target_arch in ["x86_64", "x86_64h"]:
config.parallelism_group = "darwin-64bit-sanitizer"