2012-06-25 20:57:43 +08:00
|
|
|
# Testing rules for AddressSanitizer.
|
|
|
|
#
|
|
|
|
# These are broken into two buckets. One set of tests directly interacts with
|
|
|
|
# the runtime library and checks its functionality. These are the
|
|
|
|
# no-instrumentation tests.
|
|
|
|
#
|
|
|
|
# Another group of tests relies upon the ability to compile the test with
|
|
|
|
# address sanitizer instrumentation pass. These tests form "integration" tests
|
|
|
|
# and have some elements of version skew -- they test the *host* compiler's
|
|
|
|
# instrumentation against the just-built runtime library.
|
|
|
|
|
|
|
|
include(CheckCXXCompilerFlag)
|
2012-12-19 20:33:39 +08:00
|
|
|
include(CompilerRTCompile)
|
2012-06-25 20:57:43 +08:00
|
|
|
|
|
|
|
include_directories(..)
|
|
|
|
include_directories(../..)
|
|
|
|
|
2013-01-21 19:36:38 +08:00
|
|
|
# Use zero-based shadow on Android.
|
|
|
|
if(ANDROID)
|
2013-01-21 18:51:18 +08:00
|
|
|
set(ASAN_TESTS_USE_ZERO_BASE_SHADOW TRUE)
|
|
|
|
else()
|
|
|
|
set(ASAN_TESTS_USE_ZERO_BASE_SHADOW FALSE)
|
|
|
|
endif()
|
|
|
|
|
2012-12-21 22:04:52 +08:00
|
|
|
set(ASAN_UNITTEST_HEADERS
|
|
|
|
asan_mac_test.h
|
|
|
|
asan_test_config.h
|
|
|
|
asan_test_utils.h)
|
|
|
|
|
2012-06-28 21:12:07 +08:00
|
|
|
set(ASAN_UNITTEST_COMMON_CFLAGS
|
2012-12-19 20:33:39 +08:00
|
|
|
${COMPILER_RT_GTEST_INCLUDE_CFLAGS}
|
|
|
|
-I${COMPILER_RT_SOURCE_DIR}/include
|
|
|
|
-I${COMPILER_RT_SOURCE_DIR}/lib
|
|
|
|
-I${COMPILER_RT_SOURCE_DIR}/lib/asan
|
2012-06-28 20:19:52 +08:00
|
|
|
-Wall
|
2012-06-28 21:12:07 +08:00
|
|
|
-Wno-format
|
2012-09-12 15:38:47 +08:00
|
|
|
-Werror
|
2012-08-28 20:38:17 +08:00
|
|
|
-g
|
|
|
|
-O2
|
2012-06-28 20:19:52 +08:00
|
|
|
)
|
2012-09-11 19:55:45 +08:00
|
|
|
|
2013-01-21 18:51:18 +08:00
|
|
|
if(ASAN_TESTS_USE_ZERO_BASE_SHADOW)
|
|
|
|
list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -fPIE)
|
|
|
|
endif()
|
2012-09-12 20:07:36 +08:00
|
|
|
if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
|
|
|
|
list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -Wno-variadic-macros)
|
|
|
|
endif()
|
|
|
|
|
2012-09-11 19:55:45 +08:00
|
|
|
# Use -D instead of definitions to please custom compile command.
|
2013-01-21 18:51:18 +08:00
|
|
|
list(APPEND ASAN_UNITTEST_COMMON_CFLAGS
|
|
|
|
-DASAN_HAS_BLACKLIST=1
|
|
|
|
-DASAN_HAS_EXCEPTIONS=1
|
|
|
|
-DASAN_UAR=0)
|
2012-09-11 19:55:45 +08:00
|
|
|
if(ANDROID)
|
|
|
|
list(APPEND ASAN_UNITTEST_COMMON_CFLAGS
|
2013-01-22 15:21:24 +08:00
|
|
|
-DASAN_FLEXIBLE_MAPPING_AND_OFFSET=0
|
2012-09-11 19:55:45 +08:00
|
|
|
-DASAN_LOW_MEMORY=1
|
2013-01-21 18:51:18 +08:00
|
|
|
-DASAN_NEEDS_SEGV=0)
|
2012-09-11 19:55:45 +08:00
|
|
|
else()
|
|
|
|
list(APPEND ASAN_UNITTEST_COMMON_CFLAGS
|
2013-01-22 15:21:24 +08:00
|
|
|
-DASAN_FLEXIBLE_MAPPING_AND_OFFSET=1
|
2013-01-21 18:51:18 +08:00
|
|
|
-DASAN_LOW_MEMORY=0
|
|
|
|
-DASAN_NEEDS_SEGV=1)
|
2012-09-11 19:55:45 +08:00
|
|
|
endif()
|
|
|
|
|
2012-12-20 14:16:50 +08:00
|
|
|
set(ASAN_LINK_FLAGS)
|
2013-01-21 18:51:18 +08:00
|
|
|
if(ASAN_TESTS_USE_ZERO_BASE_SHADOW)
|
2012-12-19 20:33:39 +08:00
|
|
|
list(APPEND ASAN_LINK_FLAGS -pie)
|
2013-01-21 18:51:18 +08:00
|
|
|
endif()
|
|
|
|
# On Android, we link with ASan runtime manually. On other platforms we depend
|
|
|
|
# on Clang driver behavior, passing -fsanitize=address flag.
|
|
|
|
if(NOT ANDROID)
|
2012-12-20 14:16:50 +08:00
|
|
|
list(APPEND ASAN_LINK_FLAGS -fsanitize=address)
|
|
|
|
endif()
|
|
|
|
# Unit tests on Mac depend on Foundation.
|
|
|
|
if(APPLE)
|
2012-12-19 20:33:39 +08:00
|
|
|
list(APPEND ASAN_LINK_FLAGS -framework Foundation)
|
|
|
|
endif()
|
|
|
|
# Unit tests require libstdc++.
|
|
|
|
list(APPEND ASAN_LINK_FLAGS -lstdc++)
|
|
|
|
|
2012-10-19 23:18:14 +08:00
|
|
|
set(ASAN_BLACKLIST_FILE "${CMAKE_CURRENT_SOURCE_DIR}/asan_test.ignore")
|
|
|
|
|
2012-06-29 18:23:31 +08:00
|
|
|
set(ASAN_UNITTEST_INSTRUMENTED_CFLAGS
|
|
|
|
${ASAN_UNITTEST_COMMON_CFLAGS}
|
2012-11-06 10:31:42 +08:00
|
|
|
-fsanitize=address
|
2012-10-19 23:18:14 +08:00
|
|
|
-mllvm "-asan-blacklist=${ASAN_BLACKLIST_FILE}"
|
2012-09-05 17:00:03 +08:00
|
|
|
-mllvm -asan-stack=1
|
|
|
|
-mllvm -asan-globals=1
|
|
|
|
-mllvm -asan-mapping-scale=0 # default will be used
|
|
|
|
-mllvm -asan-mapping-offset-log=-1 # default will be used
|
|
|
|
-mllvm -asan-use-after-return=0
|
2012-06-29 18:23:31 +08:00
|
|
|
)
|
2013-01-21 18:51:18 +08:00
|
|
|
if(ASAN_TESTS_USE_ZERO_BASE_SHADOW)
|
|
|
|
list(APPEND ASAN_UNITTEST_INSTRUMENTED_CFLAGS
|
|
|
|
-fsanitize-address-zero-base-shadow)
|
|
|
|
endif()
|
2012-06-29 18:23:31 +08:00
|
|
|
|
2012-12-19 23:17:23 +08:00
|
|
|
# Compile source for the given architecture, using compiler
|
|
|
|
# options in ${ARGN}, and add it to the object list.
|
|
|
|
macro(asan_compile obj_list source arch)
|
2012-12-19 20:33:39 +08:00
|
|
|
get_filename_component(basename ${source} NAME)
|
2012-12-19 23:17:23 +08:00
|
|
|
set(output_obj "${basename}.${arch}.o")
|
|
|
|
get_target_flags_for_arch(${arch} TARGET_CFLAGS)
|
2012-12-19 20:33:39 +08:00
|
|
|
clang_compile(${output_obj} ${source}
|
2012-12-19 23:17:23 +08:00
|
|
|
CFLAGS ${ARGN} ${TARGET_CFLAGS}
|
2012-12-19 20:33:39 +08:00
|
|
|
DEPS gtest ${ASAN_RUNTIME_LIBRARIES}
|
2012-12-21 22:04:52 +08:00
|
|
|
${ASAN_UNITTEST_HEADERS}
|
2012-12-19 20:33:39 +08:00
|
|
|
${ASAN_BLACKLIST_FILE})
|
|
|
|
list(APPEND ${obj_list} ${output_obj})
|
|
|
|
endmacro()
|
|
|
|
|
2012-12-19 23:17:23 +08:00
|
|
|
# Link ASan unit test for a given architecture from a set
|
|
|
|
# of objects in ${ARGN}.
|
|
|
|
macro(add_asan_test test_suite test_name arch)
|
|
|
|
get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
|
2012-12-19 20:33:39 +08:00
|
|
|
add_compiler_rt_test(${test_suite} ${test_name}
|
|
|
|
OBJECTS ${ARGN}
|
2012-12-21 16:56:14 +08:00
|
|
|
DEPS ${ASAN_RUNTIME_LIBRARIES} ${ARGN}
|
2012-12-19 23:17:23 +08:00
|
|
|
LINK_FLAGS ${ASAN_LINK_FLAGS}
|
|
|
|
${TARGET_LINK_FLAGS})
|
2012-12-19 20:33:39 +08:00
|
|
|
endmacro()
|
2012-06-28 20:19:52 +08:00
|
|
|
|
2012-12-19 20:33:39 +08:00
|
|
|
# Main AddressSanitizer unit tests.
|
|
|
|
add_custom_target(AsanUnitTests)
|
|
|
|
set_target_properties(AsanUnitTests PROPERTIES FOLDER "ASan unit tests")
|
|
|
|
# ASan benchmarks (not actively used now).
|
|
|
|
add_custom_target(AsanBenchmarks)
|
|
|
|
set_target_properties(AsanBenchmarks PROPERTIES FOLDER "Asan benchmarks")
|
2012-07-24 16:26:19 +08:00
|
|
|
|
2013-01-22 15:31:28 +08:00
|
|
|
set(ASAN_NOINST_TEST_SOURCES
|
|
|
|
asan_noinst_test.cc
|
|
|
|
asan_test_main.cc)
|
|
|
|
set(ASAN_INST_TEST_SOURCES
|
|
|
|
asan_globals_test.cc
|
|
|
|
asan_test.cc
|
|
|
|
asan_oob_test.cc
|
|
|
|
asan_mem_test.cc
|
|
|
|
asan_str_test.cc)
|
|
|
|
|
2012-12-19 23:17:23 +08:00
|
|
|
# Adds ASan unit tests and benchmarks for architecture.
|
|
|
|
macro(add_asan_tests_for_arch arch)
|
2012-12-19 20:33:39 +08:00
|
|
|
# Build gtest instrumented with ASan.
|
|
|
|
set(ASAN_INST_GTEST)
|
2012-12-19 23:17:23 +08:00
|
|
|
asan_compile(ASAN_INST_GTEST ${COMPILER_RT_GTEST_SOURCE} ${arch}
|
|
|
|
${ASAN_UNITTEST_INSTRUMENTED_CFLAGS})
|
2012-12-19 20:33:39 +08:00
|
|
|
# Instrumented tests.
|
|
|
|
set(ASAN_INST_TEST_OBJECTS)
|
2013-01-22 15:31:28 +08:00
|
|
|
foreach(src ${ASAN_INST_TEST_SOURCES})
|
|
|
|
asan_compile(ASAN_INST_TEST_OBJECTS ${src} ${arch}
|
|
|
|
${ASAN_UNITTEST_INSTRUMENTED_CFLAGS})
|
|
|
|
endforeach()
|
|
|
|
# Add Mac-specific tests.
|
2012-06-28 20:19:52 +08:00
|
|
|
if (APPLE)
|
2013-01-21 22:49:55 +08:00
|
|
|
asan_compile(ASAN_INST_TEST_OBJECTS asan_mac_test.cc ${arch}
|
|
|
|
${ASAN_UNITTEST_INSTRUMENTED_CFLAGS})
|
|
|
|
asan_compile(ASAN_INST_TEST_OBJECTS asan_mac_test_helpers.mm ${arch}
|
2012-12-19 20:33:39 +08:00
|
|
|
${ASAN_UNITTEST_INSTRUMENTED_CFLAGS} -ObjC)
|
2012-06-28 20:19:52 +08:00
|
|
|
endif()
|
2012-12-19 20:33:39 +08:00
|
|
|
# Uninstrumented tests.
|
|
|
|
set(ASAN_NOINST_TEST_OBJECTS)
|
2013-01-22 15:31:28 +08:00
|
|
|
foreach(src ${ASAN_NOINST_TEST_SOURCES})
|
|
|
|
asan_compile(ASAN_NOINST_TEST_OBJECTS ${src} ${arch}
|
|
|
|
${ASAN_UNITTEST_COMMON_CFLAGS})
|
|
|
|
endforeach()
|
2012-12-19 20:33:39 +08:00
|
|
|
# Link everything together.
|
2012-12-19 23:17:23 +08:00
|
|
|
add_asan_test(AsanUnitTests "Asan-${arch}-Test" ${arch}
|
|
|
|
${ASAN_NOINST_TEST_OBJECTS}
|
2012-12-19 20:33:39 +08:00
|
|
|
${ASAN_INST_TEST_OBJECTS} ${ASAN_INST_GTEST})
|
|
|
|
|
|
|
|
# Instrumented benchmarks.
|
|
|
|
set(ASAN_BENCHMARKS_OBJECTS)
|
2012-12-19 23:17:23 +08:00
|
|
|
asan_compile(ASAN_BENCHMARKS_OBJECTS asan_benchmarks_test.cc ${arch}
|
2012-12-19 20:33:39 +08:00
|
|
|
${ASAN_UNITTEST_INSTRUMENTED_CFLAGS})
|
|
|
|
# Link benchmarks.
|
2012-12-19 23:17:23 +08:00
|
|
|
add_asan_test(AsanBenchmarks "Asan-${arch}-Benchmark" ${arch}
|
|
|
|
${ASAN_BENCHMARKS_OBJECTS} ${ASAN_INST_GTEST})
|
|
|
|
endmacro()
|
|
|
|
|
2012-12-27 21:19:23 +08:00
|
|
|
if(COMPILER_RT_CAN_EXECUTE_TESTS)
|
2013-01-21 22:31:45 +08:00
|
|
|
foreach(arch ${ASAN_SUPPORTED_ARCH})
|
|
|
|
add_asan_tests_for_arch(${arch})
|
|
|
|
endforeach()
|
2012-06-27 17:01:24 +08:00
|
|
|
endif()
|
2012-06-29 18:23:31 +08:00
|
|
|
|
2012-09-11 19:55:45 +08:00
|
|
|
if(ANDROID)
|
2012-12-19 20:33:39 +08:00
|
|
|
# We assume that unit tests on Android are built in a build
|
|
|
|
# tree with fresh Clang as a host compiler.
|
|
|
|
add_library(asan_noinst_test OBJECT ${ASAN_NOINST_TEST_SOURCES})
|
|
|
|
set_target_compile_flags(asan_noinst_test ${ASAN_UNITTEST_COMMON_CFLAGS})
|
|
|
|
add_library(asan_inst_test OBJECT
|
|
|
|
${ASAN_INST_TEST_SOURCES} ${COMPILER_RT_GTEST_SOURCE})
|
|
|
|
set_target_compile_flags(asan_inst_test ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS})
|
|
|
|
add_executable(AsanTest
|
2012-09-11 19:55:45 +08:00
|
|
|
$<TARGET_OBJECTS:asan_noinst_test>
|
2012-12-19 20:33:39 +08:00
|
|
|
$<TARGET_OBJECTS:asan_inst_test>
|
|
|
|
)
|
|
|
|
# Setup correct output directory and link flags.
|
|
|
|
get_unittest_directory(OUTPUT_DIR)
|
|
|
|
set_target_properties(AsanTest PROPERTIES
|
|
|
|
RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIR})
|
|
|
|
set_target_link_flags(AsanTest ${ASAN_LINK_FLAGS})
|
2012-12-19 23:52:30 +08:00
|
|
|
target_link_libraries(AsanTest clang_rt.asan-arm-android)
|
2012-12-19 21:46:58 +08:00
|
|
|
# Add unit test to test suite.
|
2012-12-19 20:33:39 +08:00
|
|
|
add_dependencies(AsanUnitTests AsanTest)
|
2012-09-11 19:55:45 +08:00
|
|
|
endif()
|