aboutsummaryrefslogtreecommitdiff
path: root/lib/asan/tests/CMakeLists.txt
diff options
context:
space:
mode:
Diffstat (limited to 'lib/asan/tests/CMakeLists.txt')
-rw-r--r--lib/asan/tests/CMakeLists.txt118
1 files changed, 118 insertions, 0 deletions
diff --git a/lib/asan/tests/CMakeLists.txt b/lib/asan/tests/CMakeLists.txt
new file mode 100644
index 000000000000..d409d50b995e
--- /dev/null
+++ b/lib/asan/tests/CMakeLists.txt
@@ -0,0 +1,118 @@
+# 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)
+
+include_directories(..)
+include_directories(../..)
+
+set(ASAN_UNITTEST_COMMON_CFLAGS
+ -Wall
+ -Wno-format
+ -fvisibility=hidden
+)
+# Support 64-bit and 32-bit builds.
+if(LLVM_BUILD_32_BITS)
+ list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -m32)
+else()
+ list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -m64)
+endif()
+
+set(ASAN_GTEST_INCLUDE_CFLAGS
+ -I${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include
+ -I${LLVM_MAIN_SRC_DIR}/include
+ -I${LLVM_BINARY_DIR}/include
+ -D__STDC_CONSTANT_MACROS
+ -D__STDC_LIMIT_MACROS
+)
+
+set(ASAN_UNITTEST_INSTRUMENTED_CFLAGS
+ ${ASAN_UNITTEST_COMMON_CFLAGS}
+ ${ASAN_GTEST_INCLUDE_CFLAGS}
+ -faddress-sanitizer
+ -O2
+ -g
+ -mllvm "-asan-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/asan_test.ignore"
+ -DASAN_HAS_BLACKLIST=1
+ -DASAN_HAS_EXCEPTIONS=1
+ -DASAN_NEEDS_SEGV=1
+ -DASAN_UAR=0
+)
+
+add_custom_target(AsanTests)
+set_target_properties(AsanTests PROPERTIES FOLDER "ASan tests")
+function(add_asan_test testname)
+ add_unittest(AsanTests ${testname} ${ARGN})
+ if(LLVM_BUILD_32_BITS)
+ target_link_libraries(${testname} clang_rt.asan-i386)
+ else()
+ target_link_libraries(${testname} clang_rt.asan-x86_64)
+ endif()
+ if (APPLE)
+ # Darwin-specific linker flags.
+ set_property(TARGET ${testname} APPEND PROPERTY
+ LINK_FLAGS "-framework Foundation")
+ elseif (UNIX)
+ # Linux-specific linker flags.
+ set_property(TARGET ${testname} APPEND PROPERTY
+ LINK_FLAGS "-lpthread -ldl -export-dynamic")
+ endif()
+ set(add_compile_flags "")
+ get_property(compile_flags TARGET ${testname} PROPERTY COMPILE_FLAGS)
+ foreach(arg ${ASAN_UNITTEST_COMMON_CFLAGS})
+ set(add_compile_flags "${add_compile_flags} ${arg}")
+ endforeach(arg ${ASAN_UNITTEST_COMMON_CFLAGS})
+ set_property(TARGET ${testname} PROPERTY COMPILE_FLAGS
+ "${compile_flags} ${add_compile_flags}")
+endfunction()
+
+set(ASAN_NOINST_TEST_SOURCES
+ asan_noinst_test.cc
+ asan_break_optimization.cc
+)
+
+set(ASAN_INST_TEST_OBJECTS)
+
+# We only support building instrumented tests when we're not cross compiling
+# and targeting a unix-like system where we can predict viable compilation and
+# linking strategies.
+if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX)
+
+ # This function is a custom routine to manage manually compiling source files
+ # for unit tests with the just-built Clang binary, using the ASan
+ # instrumentation, and linking them into a test executable.
+ function(add_asan_compile_command source extra_cflags)
+ set(output_obj "${source}.asan.o")
+ add_custom_command(
+ OUTPUT ${output_obj}
+ COMMAND clang
+ ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS}
+ ${extra_cflags}
+ -c -o "${output_obj}"
+ ${CMAKE_CURRENT_SOURCE_DIR}/${source}
+ MAIN_DEPENDENCY ${source}
+ DEPENDS clang clang_rt.asan-i386 clang_rt.asan-x86_64 ${ARGN}
+ )
+ endfunction()
+
+ add_asan_compile_command(asan_globals_test.cc "")
+ add_asan_compile_command(asan_test.cc "")
+ list(APPEND ASAN_INST_TEST_OBJECTS asan_globals_test.cc.asan.o
+ asan_test.cc.asan.o)
+ if (APPLE)
+ add_asan_compile_command(asan_mac_test.mm "-ObjC")
+ list(APPEND ASAN_INST_TEST_OBJECTS asan_mac_test.mm.asan.o)
+ endif()
+
+endif()
+
+add_asan_test(AsanTest ${ASAN_NOINST_TEST_SOURCES}
+ ${ASAN_INST_TEST_OBJECTS})