1 include(CheckCXXCompilerFlag)
2 include(CompilerRTCompile)
3 include(CompilerRTLink)
5 include_directories(..)
6 include_directories(../..)
10 -fsanitize-memory-track-origins
11 -fno-sanitize-memory-param-retval # unittests test mostly this mode.
13 -Xclang -fdepfile-entry=${COMPILER_RT_OUTPUT_DIR}/share/msan_ignorelist.txt
16 # Unittest sources and build flags.
17 set(MSAN_UNITTEST_SOURCES
21 set(MSAN_LOADABLE_SOURCE
24 set(MSAN_UNITTEST_HEADERS
26 ../../../include/sanitizer/msan_interface.h
28 set(MSAN_UNITTEST_COMMON_CFLAGS
30 ${COMPILER_RT_UNITTEST_CFLAGS}
31 ${COMPILER_RT_GTEST_CFLAGS}
32 -I${COMPILER_RT_SOURCE_DIR}/include
33 -I${COMPILER_RT_SOURCE_DIR}/lib
34 -I${COMPILER_RT_SOURCE_DIR}/lib/msan
37 -fno-omit-frame-pointer
38 -mno-omit-leaf-frame-pointer
39 -Wno-deprecated-declarations
41 -Wno-zero-length-array
44 -Wno-gnu-zero-variadic-macro-arguments
45 -fno-sanitize-memory-param-retval # unittests test mostly this mode.
47 # Remove -stdlib= which is unused when passing -nostdinc++.
48 string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
50 set(MSAN_UNITTEST_INSTRUMENTED_CFLAGS
51 ${MSAN_UNITTEST_COMMON_CFLAGS}
53 -fsanitize-memory-track-origins
54 -mllvm -msan-keep-going=1
56 set(MSAN_UNITTEST_LINK_FLAGS
58 ${COMPILER_RT_UNITTEST_LINK_FLAGS}
59 ${COMPILER_RT_UNWINDER_LINK_LIBS}
61 # Don't need -stdlib=libc++ because we explicitly list libc++.a in the linker
65 append_list_if(COMPILER_RT_HAS_LIBDL -ldl MSAN_UNITTEST_LINK_FLAGS)
67 macro(msan_compile obj_list source arch kind cflags)
68 sanitizer_test_compile(
69 ${obj_list} ${source} ${arch}
71 COMPILE_DEPS ${MSAN_UNITTEST_HEADERS}
73 CFLAGS -isystem ${CMAKE_CURRENT_BINARY_DIR}/../libcxx_msan_${arch}/include/c++/v1
74 ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS} ${cflags}
78 macro(msan_link_shared so_list so_name arch kind)
79 cmake_parse_arguments(SOURCE "" "" "OBJECTS;LINK_FLAGS;DEPS" ${ARGN})
80 set(output_so "${CMAKE_CURRENT_BINARY_DIR}/${so_name}.${arch}${kind}.so")
81 get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
82 list(APPEND SOURCE_DEPS msan)
83 clang_link_shared(${output_so}
84 OBJECTS ${SOURCE_OBJECTS}
85 LINK_FLAGS ${COMPILER_RT_UNITTEST_LINK_FLAGS} ${TARGET_LINK_FLAGS} ${SOURCE_LINK_FLAGS}
87 list(APPEND ${so_list} ${output_so})
90 # Main MemorySanitizer unit tests.
91 add_custom_target(MsanUnitTests)
92 set_target_properties(MsanUnitTests PROPERTIES FOLDER "MSan unit tests")
94 # Adds MSan unit tests and benchmarks for architecture.
95 macro(add_msan_tests_for_arch arch kind cflags)
96 # Build gtest instrumented with MSan.
98 msan_compile(MSAN_INST_GTEST ${COMPILER_RT_GTEST_SOURCE} ${arch} "${kind}"
101 # Instrumented tests.
102 set(MSAN_INST_TEST_OBJECTS)
103 foreach (SOURCE ${MSAN_UNITTEST_SOURCES})
104 msan_compile(MSAN_INST_TEST_OBJECTS ${SOURCE} ${arch} "${kind}" "${cflags}")
107 # Instrumented loadable module objects.
108 set(MSAN_INST_LOADABLE_OBJECTS)
109 msan_compile(MSAN_INST_LOADABLE_OBJECTS ${MSAN_LOADABLE_SOURCE} ${arch} "${kind}"
112 # Instrumented loadable library tests.
113 set(MSAN_LOADABLE_SO)
114 msan_link_shared(MSAN_LOADABLE_SO "libmsan_loadable" ${arch} "${kind}"
115 OBJECTS ${MSAN_INST_LOADABLE_OBJECTS}
116 DEPS ${MSAN_INST_LOADABLE_OBJECTS})
118 set(MSAN_TEST_OBJECTS ${MSAN_INST_TEST_OBJECTS} ${MSAN_INST_GTEST})
119 set(MSAN_TEST_DEPS ${MSAN_TEST_OBJECTS} libcxx_msan_${arch}-build
121 "${MSAN_LIBCXX_DIR}/libc++.a" "${MSAN_LIBCXX_DIR}/libc++abi.a")
122 if(NOT COMPILER_RT_STANDALONE_BUILD)
123 list(APPEND MSAN_TEST_DEPS msan)
125 get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
126 add_compiler_rt_test(MsanUnitTests "Msan-${arch}${kind}-Test" ${arch}
127 OBJECTS ${MSAN_TEST_OBJECTS} "${MSAN_LIBCXX_DIR}/libc++.a" "${MSAN_LIBCXX_DIR}/libc++abi.a"
128 DEPS ${MSAN_TEST_DEPS}
129 LINK_FLAGS ${MSAN_UNITTEST_LINK_FLAGS}
130 ${TARGET_LINK_FLAGS})
133 # We should only build MSan unit tests if we can build instrumented libcxx.
134 if(COMPILER_RT_CAN_EXECUTE_TESTS AND
135 COMPILER_RT_LIBCXX_PATH AND
136 COMPILER_RT_LIBCXXABI_PATH)
137 foreach(arch ${MSAN_SUPPORTED_ARCH})
138 get_target_flags_for_arch(${arch} TARGET_CFLAGS)
139 set(LIBCXX_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/../libcxx_msan_${arch})
140 add_custom_libcxx(libcxx_msan_${arch} ${LIBCXX_PREFIX}
141 DEPS ${MSAN_RUNTIME_LIBRARIES}
142 CFLAGS ${MSAN_LIBCXX_CFLAGS} ${TARGET_CFLAGS}
144 set(MSAN_LIBCXX_DIR ${LIBCXX_PREFIX}/lib/)
146 add_msan_tests_for_arch(${arch} "" "")
147 add_msan_tests_for_arch(${arch} "-with-call"
148 "-mllvm;-msan-instrumentation-with-call-threshold=0")