151 lines · plain
1include(CheckCXXCompilerFlag)2include(CompilerRTCompile)3include(CompilerRTLink)4 5include_directories(..)6include_directories(../..)7 8set(MSAN_LIBCXX_CFLAGS9 -fsanitize=memory10 -fsanitize-memory-track-origins11 -fno-sanitize-memory-param-retval # unittests test mostly this mode.12 -Wno-pedantic13 # Do not instrument __gxx_personality_v0 (part of libcxxabi)14 -fsanitize-ignorelist=${COMPILER_RT_OUTPUT_DIR}/share/msan_ignorelist.txt15 )16 17# Unittest sources and build flags.18set(MSAN_UNITTEST_SOURCES19 msan_test.cpp20 msan_test_main.cpp21 )22set(MSAN_LOADABLE_SOURCE23 msan_loadable.cpp24 )25set(MSAN_UNITTEST_HEADERS26 msan_test_config.h27 ../../../include/sanitizer/msan_interface.h28 )29set(MSAN_UNITTEST_COMMON_CFLAGS30 -nostdinc++31 ${COMPILER_RT_UNITTEST_CFLAGS}32 ${COMPILER_RT_GTEST_CFLAGS}33 -I${COMPILER_RT_SOURCE_DIR}/include34 -I${COMPILER_RT_SOURCE_DIR}/lib35 -I${COMPILER_RT_SOURCE_DIR}/lib/msan36 -g37 -O238 -fno-omit-frame-pointer39 -mno-omit-leaf-frame-pointer40 -Wno-deprecated-declarations41 -Wno-unused-variable42 -Wno-zero-length-array43 -Wno-uninitialized44 -Werror=sign-compare45 -Wno-gnu-zero-variadic-macro-arguments46 -fno-sanitize-memory-param-retval # unittests test mostly this mode.47)48# Remove -stdlib= which is unused when passing -nostdinc++.49string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})50 51set(MSAN_UNITTEST_INSTRUMENTED_CFLAGS52 ${MSAN_UNITTEST_COMMON_CFLAGS}53 -fsanitize=memory54 -fsanitize-memory-track-origins55 -mllvm -msan-keep-going=156)57set(MSAN_UNITTEST_LINK_FLAGS58 -nostdlib++59 ${COMPILER_RT_UNITTEST_LINK_FLAGS}60 ${COMPILER_RT_UNWINDER_LINK_LIBS}61 -fsanitize=memory62 # Don't need -stdlib=libc++ because we explicitly list libc++.a in the linker63 # inputs.64)65 66append_list_if(COMPILER_RT_HAS_LIBDL -ldl MSAN_UNITTEST_LINK_FLAGS)67 68macro(msan_compile obj_list source arch kind cflags)69 sanitizer_test_compile(70 ${obj_list} ${source} ${arch}71 KIND ${kind}72 COMPILE_DEPS ${MSAN_UNITTEST_HEADERS} libcxx_msan_${arch}-install-cmake326-workaround73 DEPS msan74 CFLAGS -isystem ${MSAN_LIBCXX_DIR}/../include/c++/v175 ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS} ${cflags}76 )77endmacro()78 79macro(msan_link_shared so_list so_name arch kind)80 cmake_parse_arguments(SOURCE "" "" "OBJECTS;LINK_FLAGS;DEPS" ${ARGN})81 set(output_so "${CMAKE_CURRENT_BINARY_DIR}/${so_name}.${arch}${kind}.so")82 get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)83 list(APPEND SOURCE_DEPS msan)84 clang_link_shared(${output_so}85 OBJECTS ${SOURCE_OBJECTS}86 LINK_FLAGS ${COMPILER_RT_UNITTEST_LINK_FLAGS} ${TARGET_LINK_FLAGS} ${SOURCE_LINK_FLAGS}87 DEPS ${SOURCE_DEPS})88 list(APPEND ${so_list} ${output_so})89endmacro()90 91# Main MemorySanitizer unit tests.92add_custom_target(MsanUnitTests)93set_target_properties(MsanUnitTests PROPERTIES FOLDER "MSan unit tests")94 95# Adds MSan unit tests and benchmarks for architecture.96macro(add_msan_tests_for_arch arch kind cflags)97 # Build gtest instrumented with MSan.98 set(MSAN_INST_GTEST)99 msan_compile(MSAN_INST_GTEST ${COMPILER_RT_GTEST_SOURCE} ${arch} "${kind}"100 "${cflags}")101 102 # Instrumented tests.103 set(MSAN_INST_TEST_OBJECTS)104 foreach (SOURCE ${MSAN_UNITTEST_SOURCES})105 msan_compile(MSAN_INST_TEST_OBJECTS ${SOURCE} ${arch} "${kind}" "${cflags}")106 endforeach(SOURCE)107 108 # Instrumented loadable module objects.109 set(MSAN_INST_LOADABLE_OBJECTS)110 msan_compile(MSAN_INST_LOADABLE_OBJECTS ${MSAN_LOADABLE_SOURCE} ${arch} "${kind}"111 "-fPIC;${cflags}")112 113 # Instrumented loadable library tests.114 set(MSAN_LOADABLE_SO)115 msan_link_shared(MSAN_LOADABLE_SO "libmsan_loadable" ${arch} "${kind}"116 OBJECTS ${MSAN_INST_LOADABLE_OBJECTS}117 DEPS ${MSAN_INST_LOADABLE_OBJECTS})118 119 set(MSAN_TEST_OBJECTS ${MSAN_INST_TEST_OBJECTS} ${MSAN_INST_GTEST})120 set(MSAN_TEST_DEPS ${MSAN_TEST_OBJECTS} libcxx_msan_${arch}-install-cmake326-workaround121 ${MSAN_LOADABLE_SO}122 "${MSAN_LIBCXX_DIR}/libc++.a" "${MSAN_LIBCXX_DIR}/libc++abi.a")123 list(APPEND MSAN_TEST_DEPS msan libcxx_msan_${arch}-install-cmake326-workaround)124 get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)125 add_compiler_rt_test(MsanUnitTests "Msan-${arch}${kind}-Test" ${arch}126 OBJECTS ${MSAN_TEST_OBJECTS} "${MSAN_LIBCXX_DIR}/libc++.a" "${MSAN_LIBCXX_DIR}/libc++abi.a"127 DEPS ${MSAN_TEST_DEPS}128 LINK_FLAGS ${MSAN_UNITTEST_LINK_FLAGS}129 ${TARGET_LINK_FLAGS})130endmacro()131 132# We should only build MSan unit tests if we can build instrumented libcxx.133if(COMPILER_RT_CAN_EXECUTE_TESTS AND134 COMPILER_RT_LIBCXX_PATH AND135 COMPILER_RT_LIBCXXABI_PATH)136 foreach(arch ${MSAN_SUPPORTED_ARCH})137 get_target_flags_for_arch(${arch} TARGET_CFLAGS)138 set(LIBCXX_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/../libcxx_msan_${arch})139 add_custom_libcxx(libcxx_msan_${arch} ${LIBCXX_PREFIX}140 DEPS ${MSAN_RUNTIME_LIBRARIES}141 CFLAGS ${MSAN_LIBCXX_CFLAGS} ${TARGET_CFLAGS}142 CMAKE_ARGS -DRUNTIMES_EXECUTE_ONLY_CODE=${RUNTIMES_EXECUTE_ONLY_CODE}143 USE_TOOLCHAIN)144 set(MSAN_LIBCXX_DIR ${LIBCXX_PREFIX}/lib/)145 146 add_msan_tests_for_arch(${arch} "" "")147 add_msan_tests_for_arch(${arch} "-with-call"148 "-mllvm;-msan-instrumentation-with-call-threshold=0")149 endforeach()150endif()151