CMakeLists.txt revision 32b8991f961e585ccfe1710f5402a14b4e44cecc
18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# Testing rules for AddressSanitizer. 2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com# 38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# These are broken into two buckets. One set of tests directly interacts with 4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com# the runtime library and checks its functionality. These are the 5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com# no-instrumentation tests. 68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# 78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# Another group of tests relies upon the ability to compile the test with 88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# address sanitizer instrumentation pass. These tests form "integration" tests 98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# and have some elements of version skew -- they test the *host* compiler's 108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# instrumentation against the just-built runtime library. 11c12e1b138e736f70d698f14a3ffc74af6f3d8621tomhudson@google.com 128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.cominclude(CheckCXXCompilerFlag) 138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.cominclude(CompilerRTCompile) 14d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillipsinclude(CompilerRTUnittests) 15d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips 16d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillipsinclude_directories(..) 17d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillipsinclude_directories(../..) 18d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips 19d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillipsset(ASAN_UNITTEST_COMMON_CFLAGS 20d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips ${COMPILER_RT_GTEST_INCLUDE_CFLAGS} 21d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips -I${COMPILER_RT_SOURCE_DIR}/include 22d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips -I${COMPILER_RT_SOURCE_DIR}/lib 23d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips -I${COMPILER_RT_SOURCE_DIR}/lib/asan 24d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips -Wall 25d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips -Wno-format 26d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips -Werror 27d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips -g 28d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips -O2 29d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips) 30d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips 31d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillipsif(SUPPORTS_NO_VARIADIC_MACROS_FLAG) 32d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -Wno-variadic-macros) 33d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillipsendif() 34d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips 35d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips# Use -D instead of definitions to please custom compile command. 368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comif(ANDROID) 378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com list(APPEND ASAN_UNITTEST_COMMON_CFLAGS 388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com -DASAN_LOW_MEMORY=1 398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com -DASAN_HAS_BLACKLIST=1 408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com -DASAN_HAS_EXCEPTIONS=1 418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com -DASAN_NEEDS_SEGV=0 426f8f292aa768869a9e85c314b124875f57504f2creed@google.com -DASAN_UAR=0 43ac753098e8af4a17e5df97b3a4dd0ce123f8d70creed@android.com -fPIE 44ac753098e8af4a17e5df97b3a4dd0ce123f8d70creed@android.com ) 45ac753098e8af4a17e5df97b3a4dd0ce123f8d70creed@android.comelse() 46ac753098e8af4a17e5df97b3a4dd0ce123f8d70creed@android.com list(APPEND ASAN_UNITTEST_COMMON_CFLAGS 47ac753098e8af4a17e5df97b3a4dd0ce123f8d70creed@android.com -DASAN_HAS_BLACKLIST=1 488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com -DASAN_HAS_EXCEPTIONS=1 496f8f292aa768869a9e85c314b124875f57504f2creed@google.com -DASAN_NEEDS_SEGV=1 506f8f292aa768869a9e85c314b124875f57504f2creed@google.com -DASAN_UAR=0 516f8f292aa768869a9e85c314b124875f57504f2creed@google.com ) 526f8f292aa768869a9e85c314b124875f57504f2creed@google.comendif() 536f8f292aa768869a9e85c314b124875f57504f2creed@google.com 546f8f292aa768869a9e85c314b124875f57504f2creed@google.comset(ASAN_LINK_FLAGS) 556f8f292aa768869a9e85c314b124875f57504f2creed@google.comif(ANDROID) 566f8f292aa768869a9e85c314b124875f57504f2creed@google.com # On Android, we link with ASan runtime manually 576f8f292aa768869a9e85c314b124875f57504f2creed@google.com list(APPEND ASAN_LINK_FLAGS -pie) 586f8f292aa768869a9e85c314b124875f57504f2creed@google.comelse() 596f8f292aa768869a9e85c314b124875f57504f2creed@google.com # On other platforms, we depend on Clang driver behavior, 606f8f292aa768869a9e85c314b124875f57504f2creed@google.com # passing -fsanitize=address flag. 616f8f292aa768869a9e85c314b124875f57504f2creed@google.com list(APPEND ASAN_LINK_FLAGS -fsanitize=address) 626f8f292aa768869a9e85c314b124875f57504f2creed@google.comendif() 636f8f292aa768869a9e85c314b124875f57504f2creed@google.com# Unit tests on Mac depend on Foundation. 648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comif(APPLE) 658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com list(APPEND ASAN_LINK_FLAGS -framework Foundation) 668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comendif() 678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# Unit tests require libstdc++. 688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comlist(APPEND ASAN_LINK_FLAGS -lstdc++) 698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comset(ASAN_BLACKLIST_FILE "${CMAKE_CURRENT_SOURCE_DIR}/asan_test.ignore") 718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comset(ASAN_UNITTEST_INSTRUMENTED_CFLAGS 738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ${ASAN_UNITTEST_COMMON_CFLAGS} 748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com -fsanitize=address 758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com -mllvm "-asan-blacklist=${ASAN_BLACKLIST_FILE}" 768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com -mllvm -asan-stack=1 778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com -mllvm -asan-globals=1 788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com -mllvm -asan-mapping-scale=0 # default will be used 798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com -mllvm -asan-mapping-offset-log=-1 # default will be used 808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com -mllvm -asan-use-after-return=0 818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com) 828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# Compile source for the given architecture, using compiler 848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# options in ${ARGN}, and add it to the object list. 858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.commacro(asan_compile obj_list source arch) 866f8f292aa768869a9e85c314b124875f57504f2creed@google.com get_filename_component(basename ${source} NAME) 878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com set(output_obj "${basename}.${arch}.o") 888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com get_target_flags_for_arch(${arch} TARGET_CFLAGS) 898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com clang_compile(${output_obj} ${source} 906f8f292aa768869a9e85c314b124875f57504f2creed@google.com CFLAGS ${ARGN} ${TARGET_CFLAGS} 918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com DEPS gtest ${ASAN_RUNTIME_LIBRARIES} 928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ${ASAN_BLACKLIST_FILE}) 938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com list(APPEND ${obj_list} ${output_obj}) 948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comendmacro() 958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# Link ASan unit test for a given architecture from a set 978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# of objects in ${ARGN}. 988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.commacro(add_asan_test test_suite test_name arch) 998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS) 1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com add_compiler_rt_test(${test_suite} ${test_name} 1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com OBJECTS ${ARGN} 1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com DEPS ${ASAN_RUNTIME_LIBRARIES} ${ARGN} 1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com LINK_FLAGS ${ASAN_LINK_FLAGS} 1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ${TARGET_LINK_FLAGS}) 1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comendmacro() 1066f8f292aa768869a9e85c314b124875f57504f2creed@google.com 1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# Main AddressSanitizer unit tests. 1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comadd_custom_target(AsanUnitTests) 1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comset_target_properties(AsanUnitTests PROPERTIES FOLDER "ASan unit tests") 1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# ASan benchmarks (not actively used now). 1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comadd_custom_target(AsanBenchmarks) 1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comset_target_properties(AsanBenchmarks PROPERTIES FOLDER "Asan benchmarks") 1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# Adds ASan unit tests and benchmarks for architecture. 1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.commacro(add_asan_tests_for_arch arch) 1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com # Build gtest instrumented with ASan. 1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com set(ASAN_INST_GTEST) 1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com asan_compile(ASAN_INST_GTEST ${COMPILER_RT_GTEST_SOURCE} ${arch} 1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS}) 1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com # Instrumented tests. 1216f8f292aa768869a9e85c314b124875f57504f2creed@google.com set(ASAN_INST_TEST_OBJECTS) 1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com asan_compile(ASAN_INST_TEST_OBJECTS asan_globals_test.cc ${arch} 1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS}) 1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com asan_compile(ASAN_INST_TEST_OBJECTS asan_test.cc ${arch} 1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS}) 1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (APPLE) 1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com asan_compile(ASAN_INST_TEST_OBJECTS asan_mac_test.mm ${arch} 1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS} -ObjC) 1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com endif() 1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com # Uninstrumented tests. 1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com set(ASAN_NOINST_TEST_OBJECTS) 1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com asan_compile(ASAN_NOINST_TEST_OBJECTS asan_noinst_test.cc ${arch} 1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ${ASAN_UNITTEST_COMMON_CFLAGS}) 1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com asan_compile(ASAN_NOINST_TEST_OBJECTS asan_test_main.cc ${arch} 1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ${ASAN_UNITTEST_COMMON_CFLAGS}) 1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com # Link everything together. 1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com add_asan_test(AsanUnitTests "Asan-${arch}-Test" ${arch} 1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ${ASAN_NOINST_TEST_OBJECTS} 1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ${ASAN_INST_TEST_OBJECTS} ${ASAN_INST_GTEST}) 1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com # Instrumented benchmarks. 1426f8f292aa768869a9e85c314b124875f57504f2creed@google.com set(ASAN_BENCHMARKS_OBJECTS) 1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com asan_compile(ASAN_BENCHMARKS_OBJECTS asan_benchmarks_test.cc ${arch} 1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS}) 1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com # Link benchmarks. 1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com add_asan_test(AsanBenchmarks "Asan-${arch}-Benchmark" ${arch} 1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ${ASAN_BENCHMARKS_OBJECTS} ${ASAN_INST_GTEST}) 1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comendmacro() 1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# We only support building instrumented tests when we're not cross compiling 1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# and targeting a unix-like system where we can predict viable compilation and 1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# linking strategies. 1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# We use a different approach to build these tests for Android. See below. 1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comif("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID) 1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if(CAN_TARGET_X86_64) 1567ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org add_asan_tests_for_arch(x86_64) 1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com endif() 1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if(CAN_TARGET_I386) 159ac753098e8af4a17e5df97b3a4dd0ce123f8d70creed@android.com add_asan_tests_for_arch(i386) 160ac753098e8af4a17e5df97b3a4dd0ce123f8d70creed@android.com endif() 161ac753098e8af4a17e5df97b3a4dd0ce123f8d70creed@android.comendif() 162ac753098e8af4a17e5df97b3a4dd0ce123f8d70creed@android.com 163ac753098e8af4a17e5df97b3a4dd0ce123f8d70creed@android.comif(ANDROID) 1646f8f292aa768869a9e85c314b124875f57504f2creed@google.com # We assume that unit tests on Android are built in a build 165b7d956df4665e06f3ae98cb696cef7c04452ab8emike@reedtribe.org # tree with fresh Clang as a host compiler. 166b7d956df4665e06f3ae98cb696cef7c04452ab8emike@reedtribe.org set(ASAN_NOINST_TEST_SOURCES asan_noinst_test.cc asan_test_main.cc) 167b7d956df4665e06f3ae98cb696cef7c04452ab8emike@reedtribe.org set(ASAN_INST_TEST_SOURCES asan_globals_test.cc asan_test.cc) 168398b1bcb7d30f9e55504b6de37d31ccc1a26c876mike@reedtribe.org add_library(asan_noinst_test OBJECT ${ASAN_NOINST_TEST_SOURCES}) 169398b1bcb7d30f9e55504b6de37d31ccc1a26c876mike@reedtribe.org set_target_compile_flags(asan_noinst_test ${ASAN_UNITTEST_COMMON_CFLAGS}) 170398b1bcb7d30f9e55504b6de37d31ccc1a26c876mike@reedtribe.org add_library(asan_inst_test OBJECT 171398b1bcb7d30f9e55504b6de37d31ccc1a26c876mike@reedtribe.org ${ASAN_INST_TEST_SOURCES} ${COMPILER_RT_GTEST_SOURCE}) 172398b1bcb7d30f9e55504b6de37d31ccc1a26c876mike@reedtribe.org set_target_compile_flags(asan_inst_test ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS}) 1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com add_executable(AsanTest 1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com $<TARGET_OBJECTS:asan_noinst_test> 1756f8f292aa768869a9e85c314b124875f57504f2creed@google.com $<TARGET_OBJECTS:asan_inst_test> 1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ) 1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com # Setup correct output directory and link flags. 1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com get_unittest_directory(OUTPUT_DIR) 1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com set_target_properties(AsanTest PROPERTIES 1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIR}) 1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com set_target_link_flags(AsanTest ${ASAN_LINK_FLAGS}) 1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com target_link_libraries(AsanTest clang_rt.asan-arm-android) 1836f8f292aa768869a9e85c314b124875f57504f2creed@google.com # Add unit test to test suite. 1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com add_dependencies(AsanUnitTests AsanTest) 1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comendif() 1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com