• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1include(CheckCXXCompilerFlag)
2include(CompilerRTCompile)
3include(CompilerRTLink)
4
5include_directories(..)
6include_directories(../..)
7
8set(MSAN_LIBCXX_CFLAGS
9  -fsanitize=memory
10  -fsanitize-memory-track-origins
11  -Wno-pedantic
12  -Xclang -fdepfile-entry=${COMPILER_RT_OUTPUT_DIR}/share/msan_blacklist.txt
13  )
14
15# Unittest sources and build flags.
16set(MSAN_UNITTEST_SOURCES
17  msan_test.cpp
18  msan_test_main.cpp
19  )
20set(MSAN_LOADABLE_SOURCE
21  msan_loadable.cpp
22  )
23set(MSAN_UNITTEST_HEADERS
24  msan_test_config.h
25  ../../../include/sanitizer/msan_interface.h
26  )
27set(MSAN_UNITTEST_COMMON_CFLAGS
28  -nostdinc++
29  ${COMPILER_RT_UNITTEST_CFLAGS}
30  ${COMPILER_RT_GTEST_CFLAGS}
31  -I${COMPILER_RT_SOURCE_DIR}/include
32  -I${COMPILER_RT_SOURCE_DIR}/lib
33  -I${COMPILER_RT_SOURCE_DIR}/lib/msan
34  -g
35  -O2
36  -fno-omit-frame-pointer
37  -mno-omit-leaf-frame-pointer
38  -Wno-deprecated-declarations
39  -Wno-unused-variable
40  -Wno-zero-length-array
41  -Wno-uninitialized
42  -Werror=sign-compare
43  -Wno-gnu-zero-variadic-macro-arguments
44)
45# Remove -stdlib= which is unused when passing -nostdinc++.
46string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
47
48set(MSAN_UNITTEST_INSTRUMENTED_CFLAGS
49  ${MSAN_UNITTEST_COMMON_CFLAGS}
50  -fsanitize=memory
51  -fsanitize-memory-track-origins
52  -mllvm -msan-keep-going=1
53)
54set(MSAN_UNITTEST_LINK_FLAGS
55  ${COMPILER_RT_UNITTEST_LINK_FLAGS}
56  -fsanitize=memory
57  # Don't need -stdlib=libc++ because we explicitly list libc++.a in the linker
58  # inputs.
59)
60
61append_list_if(COMPILER_RT_HAS_LIBDL -ldl MSAN_UNITTEST_LINK_FLAGS)
62
63macro(msan_compile obj_list source arch kind cflags)
64  sanitizer_test_compile(
65    ${obj_list} ${source} ${arch}
66    KIND ${kind}
67    COMPILE_DEPS ${MSAN_UNITTEST_HEADERS}
68    DEPS gtest msan
69    CFLAGS -isystem ${CMAKE_CURRENT_BINARY_DIR}/../libcxx_msan_${arch}/include/c++/v1
70           ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS} ${cflags}
71  )
72endmacro()
73
74macro(msan_link_shared so_list so_name arch kind)
75  cmake_parse_arguments(SOURCE "" "" "OBJECTS;LINK_FLAGS;DEPS" ${ARGN})
76  set(output_so "${CMAKE_CURRENT_BINARY_DIR}/${so_name}.${arch}${kind}.so")
77  get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
78  if(NOT COMPILER_RT_STANDALONE_BUILD)
79    list(APPEND SOURCE_DEPS msan)
80  endif()
81  clang_link_shared(${output_so}
82                OBJECTS ${SOURCE_OBJECTS}
83                LINK_FLAGS ${COMPILER_RT_UNITTEST_LINK_FLAGS} ${TARGET_LINK_FLAGS} ${SOURCE_LINK_FLAGS}
84                DEPS ${SOURCE_DEPS})
85  list(APPEND ${so_list} ${output_so})
86endmacro()
87
88# Main MemorySanitizer unit tests.
89add_custom_target(MsanUnitTests)
90set_target_properties(MsanUnitTests PROPERTIES FOLDER "MSan unit tests")
91
92# Adds MSan unit tests and benchmarks for architecture.
93macro(add_msan_tests_for_arch arch kind cflags)
94  # Build gtest instrumented with MSan.
95  set(MSAN_INST_GTEST)
96  msan_compile(MSAN_INST_GTEST ${COMPILER_RT_GTEST_SOURCE} ${arch} "${kind}"
97                               "${cflags}")
98
99  # Instrumented tests.
100  set(MSAN_INST_TEST_OBJECTS)
101  foreach (SOURCE ${MSAN_UNITTEST_SOURCES})
102    msan_compile(MSAN_INST_TEST_OBJECTS ${SOURCE} ${arch} "${kind}" "${cflags}")
103  endforeach(SOURCE)
104
105  # Instrumented loadable module objects.
106  set(MSAN_INST_LOADABLE_OBJECTS)
107  msan_compile(MSAN_INST_LOADABLE_OBJECTS ${MSAN_LOADABLE_SOURCE} ${arch} "${kind}"
108               "-fPIC;${cflags}")
109
110  # Instrumented loadable library tests.
111  set(MSAN_LOADABLE_SO)
112  msan_link_shared(MSAN_LOADABLE_SO "libmsan_loadable" ${arch} "${kind}"
113                   OBJECTS ${MSAN_INST_LOADABLE_OBJECTS}
114                   DEPS ${MSAN_INST_LOADABLE_OBJECTS})
115
116  set(MSAN_TEST_OBJECTS ${MSAN_INST_TEST_OBJECTS} ${MSAN_INST_GTEST})
117  set(MSAN_TEST_DEPS ${MSAN_TEST_OBJECTS} libcxx_msan_${arch}-build
118                     ${MSAN_LOADABLE_SO}
119		     "${MSAN_LIBCXX_DIR}/libc++.a" "${MSAN_LIBCXX_DIR}/libc++abi.a")
120  if(NOT COMPILER_RT_STANDALONE_BUILD)
121    list(APPEND MSAN_TEST_DEPS msan)
122  endif()
123  get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
124  add_compiler_rt_test(MsanUnitTests "Msan-${arch}${kind}-Test" ${arch}
125    OBJECTS ${MSAN_TEST_OBJECTS} "${MSAN_LIBCXX_DIR}/libc++.a" "${MSAN_LIBCXX_DIR}/libc++abi.a"
126    DEPS ${MSAN_TEST_DEPS}
127    LINK_FLAGS ${MSAN_UNITTEST_LINK_FLAGS}
128               ${TARGET_LINK_FLAGS})
129endmacro()
130
131# We should only build MSan unit tests if we can build instrumented libcxx.
132if(COMPILER_RT_CAN_EXECUTE_TESTS AND
133   COMPILER_RT_LIBCXX_PATH AND
134   COMPILER_RT_LIBCXXABI_PATH)
135  foreach(arch ${MSAN_SUPPORTED_ARCH})
136    get_target_flags_for_arch(${arch} TARGET_CFLAGS)
137    set(LIBCXX_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/../libcxx_msan_${arch})
138    add_custom_libcxx(libcxx_msan_${arch} ${LIBCXX_PREFIX}
139      DEPS ${MSAN_RUNTIME_LIBRARIES}
140      CFLAGS ${MSAN_LIBCXX_CFLAGS} ${TARGET_CFLAGS}
141      USE_TOOLCHAIN)
142    set(MSAN_LIBCXX_DIR ${LIBCXX_PREFIX}/lib/)
143
144    add_msan_tests_for_arch(${arch} "" "")
145    add_msan_tests_for_arch(${arch} "-with-call"
146                            "-mllvm;-msan-instrumentation-with-call-threshold=0")
147  endforeach()
148endif()
149