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
13# Unittest sources and build flags.
14set(MSAN_UNITTEST_SOURCES msan_test.cc msan_test_main.cc)
15set(MSAN_LOADABLE_SOURCE msan_loadable.cc)
16set(MSAN_UNITTEST_HEADERS
17  msan_test_config.h
18  ../../../include/sanitizer/msan_interface.h
19)
20set(MSAN_UNITTEST_COMMON_CFLAGS
21  -nostdinc++
22  -isystem ${COMPILER_RT_LIBCXX_PATH}/include
23  ${COMPILER_RT_UNITTEST_CFLAGS}
24  ${COMPILER_RT_GTEST_CFLAGS}
25  -I${COMPILER_RT_SOURCE_DIR}/include
26  -I${COMPILER_RT_SOURCE_DIR}/lib
27  -I${COMPILER_RT_SOURCE_DIR}/lib/msan
28  -g
29  -O2
30  -fno-exceptions
31  -fno-omit-frame-pointer
32  -mno-omit-leaf-frame-pointer
33  -Wno-deprecated-declarations
34  -Wno-unused-variable
35  -Wno-zero-length-array
36  -Wno-uninitialized
37  -Werror=sign-compare
38)
39set(MSAN_UNITTEST_INSTRUMENTED_CFLAGS
40  ${MSAN_UNITTEST_COMMON_CFLAGS}
41  -fsanitize=memory
42  -fsanitize-memory-track-origins
43  -mllvm -msan-keep-going=1
44)
45set(MSAN_UNITTEST_LINK_FLAGS
46  -fsanitize=memory
47  # Don't need -stdlib=libc++ because we explicitly list libc++.so in the linker
48  # inputs.
49  # FIXME: we build libcxx without cxxabi and need libstdc++ to provide it.
50  -lstdc++
51)
52
53append_list_if(COMPILER_RT_HAS_LIBDL -ldl MSAN_UNITTEST_LINK_FLAGS)
54
55# Compile source for the given architecture, using compiler
56# options in ${ARGN}, and add it to the object list.
57macro(msan_compile obj_list source arch kind)
58  get_filename_component(basename ${source} NAME)
59  set(output_obj "${basename}.${arch}${kind}.o")
60  get_target_flags_for_arch(${arch} TARGET_CFLAGS)
61  set(COMPILE_DEPS ${MSAN_UNITTEST_HEADERS})
62  if(NOT COMPILER_RT_STANDALONE_BUILD)
63    list(APPEND COMPILE_DEPS gtest msan)
64  endif()
65  clang_compile(${output_obj} ${source}
66                CFLAGS ${ARGN} ${TARGET_CFLAGS}
67                DEPS ${COMPILE_DEPS})
68  list(APPEND ${obj_list} ${output_obj})
69endmacro()
70
71macro(msan_link_shared so_list so_name arch kind)
72  cmake_parse_arguments(SOURCE "" "" "OBJECTS;LINKFLAGS;DEPS" ${ARGN})
73  set(output_so "${CMAKE_CURRENT_BINARY_DIR}/${so_name}.${arch}${kind}.so")
74  get_target_flags_for_arch(${arch} TARGET_LINKFLAGS)
75  if(NOT COMPILER_RT_STANDALONE_BUILD)
76    list(APPEND SOURCE_DEPS msan)
77  endif()
78  clang_link_shared(${output_so}
79                OBJECTS ${SOURCE_OBJECTS}
80                LINKFLAGS ${TARGET_LINKFLAGS} ${SOURCE_LINKFLAGS}
81                DEPS ${SOURCE_DEPS})
82  list(APPEND ${so_list} ${output_so})
83endmacro()
84
85# Main MemorySanitizer unit tests.
86add_custom_target(MsanUnitTests)
87set_target_properties(MsanUnitTests PROPERTIES FOLDER "MSan unit tests")
88
89# Adds MSan unit tests and benchmarks for architecture.
90macro(add_msan_tests_for_arch arch kind)
91  # Build gtest instrumented with MSan.
92  set(MSAN_INST_GTEST)
93  msan_compile(MSAN_INST_GTEST ${COMPILER_RT_GTEST_SOURCE} ${arch} "${kind}"
94                               ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS} ${ARGN})
95
96  # Instrumented tests.
97  set(MSAN_INST_TEST_OBJECTS)
98  foreach (SOURCE ${MSAN_UNITTEST_SOURCES})
99    msan_compile(MSAN_INST_TEST_OBJECTS ${SOURCE} ${arch} "${kind}"
100                 ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS} ${ARGN})
101  endforeach(SOURCE)
102
103  # Instrumented loadable module objects.
104  set(MSAN_INST_LOADABLE_OBJECTS)
105  msan_compile(MSAN_INST_LOADABLE_OBJECTS ${MSAN_LOADABLE_SOURCE} ${arch} "${kind}"
106               ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS} "-fPIC" ${ARGN})
107
108  # Instrumented loadable library tests.
109  set(MSAN_LOADABLE_SO)
110  msan_link_shared(MSAN_LOADABLE_SO "libmsan_loadable" ${arch} "${kind}"
111                   OBJECTS ${MSAN_INST_LOADABLE_OBJECTS}
112                   DEPS ${MSAN_INST_LOADABLE_OBJECTS})
113
114  set(MSAN_TEST_OBJECTS ${MSAN_INST_TEST_OBJECTS} ${MSAN_INST_GTEST})
115  set(MSAN_TEST_DEPS ${MSAN_TEST_OBJECTS} libcxx_msan_${arch}
116                     ${MSAN_LOADABLE_SO})
117  if(NOT COMPILER_RT_STANDALONE_BUILD)
118    list(APPEND MSAN_TEST_DEPS msan)
119  endif()
120  get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
121  add_compiler_rt_test(MsanUnitTests "Msan-${arch}${kind}-Test" ${arch}
122    OBJECTS ${MSAN_TEST_OBJECTS} ${MSAN_LIBCXX_SO}
123    DEPS ${MSAN_TEST_DEPS}
124    LINK_FLAGS ${MSAN_UNITTEST_LINK_FLAGS}
125               ${TARGET_LINK_FLAGS}
126               "-Wl,-rpath=${CMAKE_CURRENT_BINARY_DIR}"
127               "-Wl,-rpath=${LIBCXX_PREFIX}/lib")
128endmacro()
129
130# We should only build MSan unit tests if we can build instrumented libcxx.
131if(COMPILER_RT_CAN_EXECUTE_TESTS AND COMPILER_RT_HAS_LIBCXX_SOURCES)
132  foreach(arch ${MSAN_SUPPORTED_ARCH})
133    get_target_flags_for_arch(${arch} TARGET_CFLAGS)
134    set(LIBCXX_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/../libcxx_msan_${arch})
135    add_custom_libcxx(libcxx_msan_${arch} ${LIBCXX_PREFIX}
136      DEPS ${MSAN_RUNTIME_LIBRARIES}
137      CFLAGS ${MSAN_LIBCXX_CFLAGS} ${TARGET_CFLAGS})
138    set(MSAN_LIBCXX_SO ${LIBCXX_PREFIX}/lib/libc++.so)
139
140    add_msan_tests_for_arch(${arch} "")
141    add_msan_tests_for_arch(${arch} "-with-call"
142                            -mllvm -msan-instrumentation-with-call-threshold=0)
143  endforeach()
144endif()
145