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 # Instrumented libcxx sources and build flags.
      9 set(MSAN_LIBCXX_PATH ${LLVM_MAIN_SRC_DIR}/projects/libcxx)
     10 file(GLOB MSAN_LIBCXX_SOURCES ${MSAN_LIBCXX_PATH}/src/*.cpp)
     11 set(MSAN_LIBCXX_CFLAGS
     12   -I${MSAN_LIBCXX_PATH}/include
     13   -fsanitize=memory
     14   -fsanitize-memory-track-origins
     15   -fPIC
     16   -Wno-\#warnings
     17   -g
     18   -O2
     19   -std=c++0x
     20   -fstrict-aliasing
     21   -fno-exceptions
     22   -nostdinc++
     23   -fno-omit-frame-pointer
     24   -mno-omit-leaf-frame-pointer)
     25 set(MSAN_LIBCXX_LINK_FLAGS
     26   -nodefaultlibs
     27   -lpthread
     28   -lrt
     29   -lc
     30   -lstdc++
     31   -fsanitize=memory)
     32 
     33 # Unittest sources and build flags.
     34 set(MSAN_UNITTEST_SOURCE msan_test.cc)
     35 set(MSAN_LOADABLE_SOURCE msan_loadable.cc)
     36 set(MSAN_UNITTEST_HEADERS
     37   msandr_test_so.h
     38   ../../../include/sanitizer/msan_interface.h
     39 )
     40 set(MSANDR_UNITTEST_SOURCE msandr_test_so.cc)
     41 set(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   -fPIE
     50   -g
     51   -O2
     52   -fno-exceptions
     53   -fno-omit-frame-pointer
     54   -mno-omit-leaf-frame-pointer
     55 )
     56 set(MSAN_UNITTEST_INSTRUMENTED_CFLAGS
     57   ${MSAN_UNITTEST_COMMON_CFLAGS}
     58   -fsanitize=memory
     59   -fsanitize-memory-track-origins
     60   -mllvm -msan-keep-going=1
     61 )
     62 set(MSAN_UNITTEST_LINK_FLAGS
     63   -fsanitize=memory
     64   -pie
     65   -ldl
     66   # FIXME: we build libcxx without cxxabi and need libstdc++ to provide it.
     67   -lstdc++
     68 )
     69 set(MSAN_LOADABLE_LINK_FLAGS
     70   -fsanitize=memory
     71   -shared
     72 )
     73 
     74 # Compile source for the given architecture, using compiler
     75 # options in ${ARGN}, and add it to the object list.
     76 macro(msan_compile obj_list source arch)
     77   get_filename_component(basename ${source} NAME)
     78   set(output_obj "${basename}.${arch}.o")
     79   get_target_flags_for_arch(${arch} TARGET_CFLAGS)
     80   clang_compile(${output_obj} ${source}
     81                 CFLAGS ${ARGN} ${TARGET_CFLAGS}
     82                 DEPS gtest ${MSAN_RUNTIME_LIBRARIES} ${MSAN_UNITTEST_HEADERS})
     83   list(APPEND ${obj_list} ${output_obj})
     84 endmacro()
     85 
     86 macro(msan_link_shared so_list so_name arch)
     87   parse_arguments(SOURCE "OBJECTS;LINKFLAGS;DEPS" "" ${ARGN})
     88   set(output_so "${CMAKE_CURRENT_BINARY_DIR}/${so_name}.${arch}.so")
     89   get_target_flags_for_arch(${arch} TARGET_LINKFLAGS)
     90   clang_link_shared(${output_so}
     91                 OBJECTS ${SOURCE_OBJECTS}
     92                 LINKFLAGS ${TARGET_LINKFLAGS} ${SOURCE_LINKFLAGS}
     93                 DEPS ${SOURCE_DEPS})
     94   list(APPEND ${so_list} ${output_so})
     95 endmacro()
     96 
     97 # Link MSan unit test for a given architecture from a set
     98 # of objects in ${ARGN}.
     99 macro(add_msan_test test_suite test_name arch)
    100   get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
    101   add_compiler_rt_test(${test_suite} ${test_name}
    102                        OBJECTS ${ARGN}
    103                        DEPS ${MSAN_RUNTIME_LIBRARIES} ${ARGN}
    104                             ${MSAN_LOADABLE_SO}
    105                        LINK_FLAGS ${MSAN_UNITTEST_LINK_FLAGS}
    106                                   ${TARGET_LINK_FLAGS}
    107                                   "-Wl,-rpath=${CMAKE_CURRENT_BINARY_DIR}")
    108 endmacro()
    109 
    110 # Main MemorySanitizer unit tests.
    111 add_custom_target(MsanUnitTests)
    112 set_target_properties(MsanUnitTests PROPERTIES FOLDER "MSan unit tests")
    113 
    114 # Adds MSan unit tests and benchmarks for architecture.
    115 macro(add_msan_tests_for_arch arch)
    116   # Build gtest instrumented with MSan.
    117   set(MSAN_INST_GTEST)
    118   msan_compile(MSAN_INST_GTEST ${COMPILER_RT_GTEST_SOURCE} ${arch} 
    119                                ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS})
    120 
    121   # Build libcxx instrumented with MSan.
    122   set(MSAN_INST_LIBCXX_OBJECTS)
    123   foreach(SOURCE ${MSAN_LIBCXX_SOURCES})
    124     msan_compile(MSAN_INST_LIBCXX_OBJECTS ${SOURCE} ${arch} 
    125                  ${MSAN_LIBCXX_CFLAGS})
    126   endforeach(SOURCE)
    127 
    128   set(MSAN_INST_LIBCXX)
    129   msan_link_shared(MSAN_INST_LIBCXX "libcxx" ${arch}
    130                    OBJECTS ${MSAN_INST_LIBCXX_OBJECTS}
    131                    LINKFLAGS ${MSAN_LIBCXX_LINK_FLAGS}
    132                    DEPS ${MSAN_INST_LIBCXX_OBJECTS} ${MSAN_RUNTIME_LIBRARIES})
    133 
    134   # Instrumented tests.
    135   set(MSAN_INST_TEST_OBJECTS)
    136   msan_compile(MSAN_INST_TEST_OBJECTS ${MSAN_UNITTEST_SOURCE} ${arch}
    137                ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS})
    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})
    165 endmacro()
    166 
    167 if(COMPILER_RT_CAN_EXECUTE_TESTS AND EXISTS ${MSAN_LIBCXX_PATH}/)
    168   if(CAN_TARGET_x86_64)
    169     add_msan_tests_for_arch(x86_64)
    170   endif()
    171 endif()
    172