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 # Do not instrument __gxx_personality_v0 (part of libcxxabi)
14 -fsanitize-ignorelist=${COMPILER_RT_OUTPUT_DIR}/share/msan_ignorelist.txt
17 # Unittest sources and build flags.
18 set(MSAN_UNITTEST_SOURCES
22 set(MSAN_LOADABLE_SOURCE
25 set(MSAN_UNITTEST_HEADERS
27 ../../../include/sanitizer/msan_interface.h
29 set(MSAN_UNITTEST_COMMON_CFLAGS
31 ${COMPILER_RT_UNITTEST_CFLAGS}
32 ${COMPILER_RT_GTEST_CFLAGS}
33 -I${COMPILER_RT_SOURCE_DIR}/include
34 -I${COMPILER_RT_SOURCE_DIR}/lib
35 -I${COMPILER_RT_SOURCE_DIR}/lib/msan
38 -fno-omit-frame-pointer
39 -mno-omit-leaf-frame-pointer
40 -Wno-deprecated-declarations
42 -Wno-zero-length-array
45 -Wno-gnu-zero-variadic-macro-arguments
46 -fno-sanitize-memory-param-retval # unittests test mostly this mode.
48 # Remove -stdlib= which is unused when passing -nostdinc++.
49 string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
51 set(MSAN_UNITTEST_INSTRUMENTED_CFLAGS
52 ${MSAN_UNITTEST_COMMON_CFLAGS}
54 -fsanitize-memory-track-origins
55 -mllvm -msan-keep-going=1
57 set(MSAN_UNITTEST_LINK_FLAGS
59 ${COMPILER_RT_UNITTEST_LINK_FLAGS}
60 ${COMPILER_RT_UNWINDER_LINK_LIBS}
62 # Don't need -stdlib=libc++ because we explicitly list libc++.a in the linker
66 append_list_if(COMPILER_RT_HAS_LIBDL -ldl MSAN_UNITTEST_LINK_FLAGS)
68 macro(msan_compile obj_list source arch kind cflags)
69 sanitizer_test_compile(
70 ${obj_list} ${source} ${arch}
72 COMPILE_DEPS ${MSAN_UNITTEST_HEADERS} libcxx_msan_${arch}-install-cmake326-workaround
74 CFLAGS -isystem ${MSAN_LIBCXX_DIR}/../include/c++/v1
75 ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS} ${cflags}
79 macro(msan_link_shared so_list so_name arch kind)
80 cmake_parse_arguments(SOURCE "" "" "OBJECTS;LINK_FLAGS;DEPS" ${ARGN})
81 set(output_so "${CMAKE_CURRENT_BINARY_DIR}/${so_name}.${arch}${kind}.so")
82 get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
83 list(APPEND SOURCE_DEPS msan)
84 clang_link_shared(${output_so}
85 OBJECTS ${SOURCE_OBJECTS}
86 LINK_FLAGS ${COMPILER_RT_UNITTEST_LINK_FLAGS} ${TARGET_LINK_FLAGS} ${SOURCE_LINK_FLAGS}
88 list(APPEND ${so_list} ${output_so})
91 # Main MemorySanitizer unit tests.
92 add_custom_target(MsanUnitTests)
93 set_target_properties(MsanUnitTests PROPERTIES FOLDER "MSan unit tests")
95 # Adds MSan unit tests and benchmarks for architecture.
96 macro(add_msan_tests_for_arch arch kind cflags)
97 # Build gtest instrumented with MSan.
99 msan_compile(MSAN_INST_GTEST ${COMPILER_RT_GTEST_SOURCE} ${arch} "${kind}"
102 # Instrumented tests.
103 set(MSAN_INST_TEST_OBJECTS)
104 foreach (SOURCE ${MSAN_UNITTEST_SOURCES})
105 msan_compile(MSAN_INST_TEST_OBJECTS ${SOURCE} ${arch} "${kind}" "${cflags}")
108 # Instrumented loadable module objects.
109 set(MSAN_INST_LOADABLE_OBJECTS)
110 msan_compile(MSAN_INST_LOADABLE_OBJECTS ${MSAN_LOADABLE_SOURCE} ${arch} "${kind}"
113 # Instrumented loadable library tests.
114 set(MSAN_LOADABLE_SO)
115 msan_link_shared(MSAN_LOADABLE_SO "libmsan_loadable" ${arch} "${kind}"
116 OBJECTS ${MSAN_INST_LOADABLE_OBJECTS}
117 DEPS ${MSAN_INST_LOADABLE_OBJECTS})
119 set(MSAN_TEST_OBJECTS ${MSAN_INST_TEST_OBJECTS} ${MSAN_INST_GTEST})
120 set(MSAN_TEST_DEPS ${MSAN_TEST_OBJECTS} libcxx_msan_${arch}-install-cmake326-workaround
122 "${MSAN_LIBCXX_DIR}/libc++.a" "${MSAN_LIBCXX_DIR}/libc++abi.a")
123 list(APPEND MSAN_TEST_DEPS msan libcxx_msan_${arch}-install-cmake326-workaround)
124 get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
125 add_compiler_rt_test(MsanUnitTests "Msan-${arch}${kind}-Test" ${arch}
126 OBJECTS ${MSAN_TEST_OBJECTS} "${MSAN_LIBCXX_DIR}/libc++.a" "${MSAN_LIBCXX_DIR}/libc++abi.a"
127 DEPS ${MSAN_TEST_DEPS}
128 LINK_FLAGS ${MSAN_UNITTEST_LINK_FLAGS}
129 ${TARGET_LINK_FLAGS})
132 # We should only build MSan unit tests if we can build instrumented libcxx.
133 if(COMPILER_RT_CAN_EXECUTE_TESTS AND
134 COMPILER_RT_LIBCXX_PATH AND
135 COMPILER_RT_LIBCXXABI_PATH)
136 foreach(arch ${MSAN_SUPPORTED_ARCH})
137 get_target_flags_for_arch(${arch} TARGET_CFLAGS)
138 set(LIBCXX_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/../libcxx_msan_${arch})
139 add_custom_libcxx(libcxx_msan_${arch} ${LIBCXX_PREFIX}
140 DEPS ${MSAN_RUNTIME_LIBRARIES}
141 CFLAGS ${MSAN_LIBCXX_CFLAGS} ${TARGET_CFLAGS}
143 set(MSAN_LIBCXX_DIR ${LIBCXX_PREFIX}/lib/)
145 add_msan_tests_for_arch(${arch} "" "")
146 add_msan_tests_for_arch(${arch} "-with-call"
147 "-mllvm;-msan-instrumentation-with-call-threshold=0")