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