Browse Source

MDEV-36156: MSAN Compile and Link flags needed for compile/run checks

When MSAN adds the -fsantize=memory this significantly affects compile
and link tests. Whether this is a compile/run/or looking for a symbol
in a library these cmake tests require the same flags be set.

Ideally the minimum invocation of cmake to create a MSAN build as
investigated in MDBF-793 should be:

  -DWITH_MSAN=ON \
  -DCMAKE_{EXE,MODULE}_LINKER_FLAGS="-L${MSAN_LIBDIR} -Wl,-rpath=${MSAN_LIBDIR}"

On the assumption that the compiler supports msan and the instrumented
libraries are in MSAN_LIBDIR (maybe later can be made a cmake option).

Without cmake policy below, the checking of everything from libc++ to
libfmt will not have the supplied linker flags or the compile options
that WITH_MSAN=ON invokes. Many try_compile and CMake functions that
wrapped this and headers failed to be recognised due to missing msan symbols
when linking. Also without the -L path, they where applying a link test
to the default path libraries rather than the MSAN instrumented ones.

The CMake policy enabled is CMP0056, added CMake 3.2, applies
CMAKE_EXE_LINKER_FLAGS to try_compile.

With this change the MY_CHECK_AND_SET_COMPILER_FLAG
remove explict build types resulting in just CMAKE_{C,CXX}_FLAGS being
set rather than CMAKE_{C,CXX}_FLAGS_{DEBUG,RELWITHDEBINFO}. These
are needed for the default CMP0066 policy to be correctly applied and
the msan flags of -fsanitizer=memory are applied to all compile checks.

Likewise with MY_CHECK_AND_SET_LINKER_FLAG for CMAKE_{EXE,MODULE,SHARED}_LINKER_FLAGS
for those check that involve full linking and CHECK_CXX_SOURCE_RUNS for
example.
bb-10.6-merge
Daniel Black 7 months ago
committed by Sergei Golubchik
parent
commit
e95a8f84de
  1. 6
      CMakeLists.txt

6
CMakeLists.txt

@ -31,7 +31,7 @@ ENDIF()
# in RPM's:
#set(CPACK_RPM_SPEC_MORE_DEFINE "%define __spec_install_post /bin/true")
FOREACH(p CMP0022 CMP0046 CMP0040 CMP0048 CMP0054 CMP0067 CMP0074 CMP0075 CMP0069 CMP0135)
FOREACH(p CMP0022 CMP0046 CMP0040 CMP0048 CMP0054 CMP0056 CMP0067 CMP0074 CMP0075 CMP0069 CMP0135)
IF(POLICY ${p})
CMAKE_POLICY(SET ${p} NEW)
ENDIF()
@ -245,7 +245,7 @@ ENDIF()
OPTION(WITH_MSAN "Enable memory sanitizer" OFF)
IF (WITH_MSAN)
MY_CHECK_AND_SET_COMPILER_FLAG("-fsanitize=memory -fsanitize-memory-track-origins -U_FORTIFY_SOURCE" DEBUG RELWITHDEBINFO)
MY_CHECK_AND_SET_COMPILER_FLAG("-fsanitize=memory -fsanitize-memory-track-origins -U_FORTIFY_SOURCE")
IF(NOT (have_C__fsanitize_memory__fsanitize_memory_track_origins__U_FORTIFY_SOURCE
AND have_CXX__fsanitize_memory__fsanitize_memory_track_origins__U_FORTIFY_SOURCE))
MESSAGE(FATAL_ERROR "Compiler doesn't support -fsanitize=memory flags")
@ -255,7 +255,7 @@ IF (WITH_MSAN)
MESSAGE(FATAL_ERROR "C++ Compiler requires support for -stdlib=libc++")
ENDIF()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
MY_CHECK_AND_SET_LINKER_FLAG("-fsanitize=memory" DEBUG RELWITHDEBINFO)
MY_CHECK_AND_SET_LINKER_FLAG("-fsanitize=memory")
IF(NOT HAVE_LINK_FLAG__fsanitize_memory)
MESSAGE(FATAL_ERROR "Linker doesn't support -fsanitize=memory flags")
ENDIF()

Loading…
Cancel
Save