Skip to content

Commit 798584d

Browse files
MDBF-1214 Add WITH_{MSAN, ASAN, UBSAN} CMake support (#313)
* MDBF-1214 Add WITH_MSAN CMake support Declare WITH_MSAN as a regular option for standalone Connector/C builds. When enabled directly in Connector/C, append MemorySanitizer compile and link flags for Debug and RelWithDebInfo builds. When Connector/C is included as a subproject, keep using the inherited WITH_MSAN value only for sanitizer-specific linker behavior. This avoids duplicating sanitizer flags already managed by parent projects. * MDBF-1214 Add ASAN and UBSAN CMake support Add standalone Connector/C options for WITH_ASAN and WITH_UBSAN. Use a shared sanitizer flag helper for Debug and RelWithDebInfo builds, covering compile flags and executable/shared/module linker flags. Keep the flag handling disabled for subproject builds so Connector/C++ and ODBC continue to own sanitizer flags for their combined builds. * MDBF-1214 Refine sanitizer CMake options Add an opt-in WITH_ASAN_SCOPE option for Clang builds so ASAN can detect stack use after scope without adding the flag to linker options. Enable MSVC ASAN stack use after return instrumentation and keep incremental linking disabled for those builds. Reject MSAN when ASAN or UBSAN is also enabled. Remove the redundant ASAN -fPIC flag and keep UBSAN alignment checks enabled.
1 parent 7564026 commit 798584d

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,78 @@ ADD_OPTION(WITH_DYNCOL "Enables support of dynamic columns" ON)
7474
ADD_OPTION(WITH_EXTERNAL_ZLIB "Enables use of external zlib" OFF)
7575
ADD_OPTION(WITH_CURL "Enables use of curl" ON)
7676
ADD_OPTION(WITH_SSL "Enables use of TLS/SSL library" ON)
77+
IF(NOT IS_SUBPROJECT)
78+
ADD_OPTION(WITH_ASAN
79+
"Enable AddressSanitizer instrumentation for Debug and RelWithDebInfo builds"
80+
OFF)
81+
ADD_OPTION(WITH_ASAN_SCOPE
82+
"Enable -fsanitize-address-use-after-scope for Clang ASAN builds"
83+
OFF)
84+
ADD_OPTION(WITH_UBSAN
85+
"Enable UndefinedBehaviorSanitizer instrumentation for Debug and RelWithDebInfo builds"
86+
OFF)
87+
ADD_OPTION(WITH_MSAN "Enable MemorySanitizer instrumentation for Debug and RelWithDebInfo builds" OFF)
88+
ENDIF()
7789

7890

7991
INCLUDE(${CC_SOURCE_DIR}/cmake/misc.cmake)
8092
INCLUDE(FindCURL)
8193

94+
IF(NOT IS_SUBPROJECT)
95+
MACRO(MARIADB_ADD_SANITIZER_FLAGS compile_flags link_flags)
96+
FOREACH(BUILD_TYPE DEBUG RELWITHDEBINFO)
97+
SET(CMAKE_C_FLAGS_${BUILD_TYPE}
98+
"${CMAKE_C_FLAGS_${BUILD_TYPE}} ${compile_flags}")
99+
FOREACH(LINKER EXE SHARED MODULE)
100+
SET(CMAKE_${LINKER}_LINKER_FLAGS_${BUILD_TYPE}
101+
"${CMAKE_${LINKER}_LINKER_FLAGS_${BUILD_TYPE}} ${link_flags}")
102+
ENDFOREACH()
103+
ENDFOREACH()
104+
ENDMACRO()
105+
106+
IF(WITH_MSAN AND (WITH_ASAN OR WITH_UBSAN))
107+
MESSAGE(FATAL_ERROR "WITH_MSAN is incompatible with WITH_ASAN or WITH_UBSAN")
108+
ENDIF()
109+
110+
IF(WITH_ASAN_SCOPE)
111+
IF(NOT WITH_ASAN)
112+
MESSAGE(FATAL_ERROR "WITH_ASAN_SCOPE requires WITH_ASAN")
113+
ENDIF()
114+
IF(NOT CMAKE_C_COMPILER_ID MATCHES "Clang")
115+
MESSAGE(FATAL_ERROR "WITH_ASAN_SCOPE is supported only with Clang")
116+
ENDIF()
117+
ADD_COMPILE_OPTIONS(
118+
"$<$<CONFIG:Debug>:-fsanitize-address-use-after-scope>"
119+
"$<$<CONFIG:RelWithDebInfo>:-fsanitize-address-use-after-scope>")
120+
ENDIF()
121+
122+
IF(WITH_ASAN)
123+
IF(MSVC)
124+
MARIADB_ADD_SANITIZER_FLAGS(
125+
"/fsanitize=address /fsanitize-address-use-after-return"
126+
"/INCREMENTAL:NO")
127+
ELSE()
128+
MARIADB_ADD_SANITIZER_FLAGS(
129+
"-U_FORTIFY_SOURCE -fsanitize=address"
130+
"-fsanitize=address")
131+
ENDIF()
132+
ENDIF()
133+
134+
IF(NOT MSVC)
135+
IF(WITH_UBSAN)
136+
MARIADB_ADD_SANITIZER_FLAGS(
137+
"-fsanitize=undefined -U_FORTIFY_SOURCE"
138+
"-fsanitize=undefined")
139+
ENDIF()
140+
141+
IF(WITH_MSAN)
142+
MARIADB_ADD_SANITIZER_FLAGS(
143+
"-fsanitize=memory -fsanitize-memory-track-origins -U_FORTIFY_SOURCE"
144+
"-fsanitize=memory -fsanitize-memory-track-origins")
145+
ENDIF()
146+
ENDIF()
147+
ENDIF()
148+
82149
IF(WITH_SIGNCODE)
83150
IF(WIN32 AND NOT SIGN_OPTIONS)
84151
SET(SIGN_OPTIONS /tr http://timestamp.digicert.com /td sha256 /fd sha256 /a)
@@ -566,6 +633,10 @@ MESSAGE1(STATUS "MariaDB Connector/c configuration:
566633
-- SSL support: ${WITH_SSL} Libs: ${SSL_LIBRARIES}
567634
-- Zlib support: ${zlib_status}
568635
-- ZStd support: ${ZSTD_FOUND}
636+
-- ASAN support: ${WITH_ASAN}
637+
-- ASAN scope support: ${WITH_ASAN_SCOPE}
638+
-- UBSAN support: ${WITH_UBSAN}
639+
-- MSAN support: ${WITH_MSAN}
569640
-- Installation layout: ${INSTALL_LAYOUT}
570641
-- Include files will be installed in ${INSTALL_INCLUDEDIR}
571642
-- Libraries will be installed in ${INSTALL_LIBDIR}

0 commit comments

Comments
 (0)