1include(CheckCXXCompilerFlag)
2include(CompilerRTCompile)
3include(CompilerRTLink)
4
5include_directories(..)
6include_directories(../..)
7
8# Instrumented libcxx sources and build flags.
9file(GLOB MSAN_LIBCXX_SOURCES ${MSAN_LIBCXX_PATH}/src/*.cpp)
10set(MSAN_LIBCXX_CFLAGS
11  -I${MSAN_LIBCXX_PATH}/include
12  -fsanitize=memory
13  -fsanitize-memory-track-origins
14  -fPIC
15  -Wno-\#warnings
16  -g
17  -O2
18  -std=c++0x
19  -fstrict-aliasing
20  -fno-exceptions
21  -nostdinc++
22  -fno-omit-frame-pointer
23  -mno-omit-leaf-frame-pointer)
24set(MSAN_LIBCXX_LINK_FLAGS
25  -nodefaultlibs
26  -lpthread
27  -lrt
28  -lc
29  -lstdc++
30  -fsanitize=memory)
31
32# Unittest sources and build flags.
33set(MSAN_UNITTEST_SOURCES msan_test.cc msan_test_main.cc)
34set(MSAN_LOADABLE_SOURCE msan_loadable.cc)
35set(MSAN_UNITTEST_HEADERS
36  msan_test_config.h
37  msandr_test_so.h
38  ../../../include/sanitizer/msan_interface.h
39)
40set(MSANDR_UNITTEST_SOURCE msandr_test_so.cc)
41set(MSAN_UNITTEST_COMMON_CFLAGS
42  -I${MSAN_LIBCXX_PATH}/include
43  ${COMPILER_RT_GTEST_INCLUDE_CFLAGS}
44  -I${COMPILER_RT_SOURCE_DIR}/include
45  -I${COMPILER_RT_SOURCE_DIR}/lib
46  -I${COMPILER_RT_SOURCE_DIR}/lib/msan
47  -std=c++0x
48  -stdlib=libc++
49  -g
50  -O2
51  -fno-exceptions
52  -fno-omit-frame-pointer
53  -mno-omit-leaf-frame-pointer
54)
55set(MSAN_UNITTEST_INSTRUMENTED_CFLAGS
56  ${MSAN_UNITTEST_COMMON_CFLAGS}
57  -fsanitize=memory
58  -fsanitize-memory-track-origins
59  -mllvm -msan-keep-going=1
60)
61set(MSAN_UNITTEST_LINK_FLAGS
62  -fsanitize=memory
63  -ldl
64  # FIXME: we build libcxx without cxxabi and need libstdc++ to provide it.
65  -lstdc++
66)
67set(MSAN_LOADABLE_LINK_FLAGS
68  -fsanitize=memory
69  -shared
70)
71
72# Compile source for the given architecture, using compiler
73# options in ${ARGN}, and add it to the object list.
74macro(msan_compile obj_list source arch)
75  get_filename_component(basename ${source} NAME)
76  set(output_obj "${basename}.${arch}.o")
77  get_target_flags_for_arch(${arch} TARGET_CFLAGS)
78  clang_compile(${output_obj} ${source}
79                CFLAGS ${ARGN} ${TARGET_CFLAGS}
80                DEPS gtest ${MSAN_RUNTIME_LIBRARIES} ${MSAN_UNITTEST_HEADERS})
81  list(APPEND ${obj_list} ${output_obj})
82endmacro()
83
84macro(msan_link_shared so_list so_name arch)
85  parse_arguments(SOURCE "OBJECTS;LINKFLAGS;DEPS" "" ${ARGN})
86  set(output_so "${CMAKE_CURRENT_BINARY_DIR}/${so_name}.${arch}.so")
87  get_target_flags_for_arch(${arch} TARGET_LINKFLAGS)
88  clang_link_shared(${output_so}
89                OBJECTS ${SOURCE_OBJECTS}
90                LINKFLAGS ${TARGET_LINKFLAGS} ${SOURCE_LINKFLAGS}
91                DEPS ${SOURCE_DEPS})
92  list(APPEND ${so_list} ${output_so})
93endmacro()
94
95# Link MSan unit test for a given architecture from a set
96# of objects in ${ARGN}.
97macro(add_msan_test test_suite test_name arch)
98  get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
99  add_compiler_rt_test(${test_suite} ${test_name}
100                       OBJECTS ${ARGN}
101                       DEPS ${MSAN_RUNTIME_LIBRARIES} ${ARGN}
102                            ${MSAN_LOADABLE_SO}
103                       LINK_FLAGS ${MSAN_UNITTEST_LINK_FLAGS}
104                                  ${TARGET_LINK_FLAGS}
105                                  "-Wl,-rpath=${CMAKE_CURRENT_BINARY_DIR}")
106endmacro()
107
108# Main MemorySanitizer unit tests.
109add_custom_target(MsanUnitTests)
110set_target_properties(MsanUnitTests PROPERTIES FOLDER "MSan unit tests")
111
112# Adds MSan unit tests and benchmarks for architecture.
113macro(add_msan_tests_for_arch arch)
114  # Build gtest instrumented with MSan.
115  set(MSAN_INST_GTEST)
116  msan_compile(MSAN_INST_GTEST ${COMPILER_RT_GTEST_SOURCE} ${arch} 
117                               ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS})
118
119  # Build libcxx instrumented with MSan.
120  set(MSAN_INST_LIBCXX_OBJECTS)
121  foreach(SOURCE ${MSAN_LIBCXX_SOURCES})
122    msan_compile(MSAN_INST_LIBCXX_OBJECTS ${SOURCE} ${arch} 
123                 ${MSAN_LIBCXX_CFLAGS})
124  endforeach(SOURCE)
125
126  set(MSAN_INST_LIBCXX)
127  msan_link_shared(MSAN_INST_LIBCXX "libcxx" ${arch}
128                   OBJECTS ${MSAN_INST_LIBCXX_OBJECTS}
129                   LINKFLAGS ${MSAN_LIBCXX_LINK_FLAGS}
130                   DEPS ${MSAN_INST_LIBCXX_OBJECTS} ${MSAN_RUNTIME_LIBRARIES})
131
132  # Instrumented tests.
133  set(MSAN_INST_TEST_OBJECTS)
134  foreach (SOURCE ${MSAN_UNITTEST_SOURCES})
135    msan_compile(MSAN_INST_TEST_OBJECTS ${SOURCE} ${arch}
136                 ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS})
137  endforeach(SOURCE)
138
139  # Instrumented loadable module objects.
140  set(MSAN_INST_LOADABLE_OBJECTS)
141  msan_compile(MSAN_INST_LOADABLE_OBJECTS ${MSAN_LOADABLE_SOURCE} ${arch}
142               ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS})
143
144  # Uninstrumented shared object for MSanDR tests.
145  set(MSANDR_TEST_OBJECTS)
146  msan_compile(MSANDR_TEST_OBJECTS ${MSANDR_UNITTEST_SOURCE} ${arch}
147               ${MSAN_UNITTEST_COMMON_CFLAGS})
148
149  # Instrumented loadable library tests.
150  set(MSAN_LOADABLE_SO)
151  msan_link_shared(MSAN_LOADABLE_SO "libmsan_loadable" ${arch}
152                   OBJECTS ${MSAN_INST_LOADABLE_OBJECTS}
153                   DEPS ${MSAN_INST_LOADABLE_OBJECTS} ${MSAN_RUNTIME_LIBRARIES})
154
155  # Uninstrumented shared library tests.
156  set(MSANDR_TEST_SO)
157  msan_link_shared(MSANDR_TEST_SO "libmsandr_test" ${arch}
158                   OBJECTS ${MSANDR_TEST_OBJECTS}
159                   DEPS ${MSANDR_TEST_OBJECTS} ${MSAN_RUNTIME_LIBRARIES})
160
161  # Link everything together.
162  add_msan_test(MsanUnitTests "Msan-${arch}-Test" ${arch}
163                ${MSAN_INST_TEST_OBJECTS} ${MSAN_INST_GTEST}
164                ${MSAN_INST_LIBCXX} ${MSANDR_TEST_SO})
165endmacro()
166
167if(COMPILER_RT_CAN_EXECUTE_TESTS AND MSAN_CAN_INSTRUMENT_LIBCXX)
168  if(CAN_TARGET_x86_64)
169    add_msan_tests_for_arch(x86_64)
170  endif()
171endif()
172