Skip to content

Commit 7abf979

Browse files
committed
cmake: Disable ctime_tests if build with -fsanitize=memory
Clang >= 16 has `-fsanitize-memory-param-retval` turned on by default, which is incompatible with
1 parent 1791f6f commit 7abf979

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

CMakeLists.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,17 @@ endif()
263263

264264
set(CMAKE_C_VISIBILITY_PRESET hidden)
265265

266+
set(print_msan_notice)
267+
if(SECP256K1_BUILD_CTIME_TESTS)
268+
include(CheckMemorySanitizer)
269+
check_memory_sanitizer(msan_enabled)
270+
if(msan_enabled)
271+
try_append_c_flags(-fno-sanitize-memory-param-retval)
272+
set(print_msan_notice YES)
273+
endif()
274+
unset(msan_enabled)
275+
endif()
276+
266277
# Ask CTest to create a "check" target (e.g., make check) as alias for the "test" target.
267278
# CTEST_TEST_TARGET_ALIAS is not documented but supposed to be user-facing.
268279
# See: https://gitlab.kitware.com/cmake/cmake/-/commit/816c9d1aa1f2b42d40c81a991b68c96eb12b6d2
@@ -358,7 +369,14 @@ endif()
358369
if(SECP256K1_LATE_CFLAGS)
359370
message("SECP256K1_LATE_CFLAGS ................. ${SECP256K1_LATE_CFLAGS}")
360371
endif()
361-
message("\n")
372+
message("")
373+
if(print_msan_notice)
374+
message(
375+
"Note:\n"
376+
" MemorySanitizer detected, tried to add -fno-sanitize-memory-param-retval to compile options\n"
377+
" to avoid false positives in ctime_tests. Pass -DSECP256K1_BUILD_CTIME_TESTS=OFF to avoid this.\n"
378+
)
379+
endif()
362380
if(SECP256K1_EXPERIMENTAL)
363381
message(
364382
" ******\n"

cmake/CheckMemorySanitizer.cmake

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
include_guard(GLOBAL)
2+
include(CheckCSourceCompiles)
3+
4+
function(check_memory_sanitizer output)
5+
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
6+
check_c_source_compiles("
7+
#if defined(__has_feature)
8+
# if __has_feature(memory_sanitizer)
9+
/* MemorySanitizer is enabled. */
10+
# elif
11+
# error \"MemorySanitizer is disabled.\"
12+
# endif
13+
#else
14+
# error \"__has_feature is not defined.\"
15+
#endif
16+
" HAVE_MSAN)
17+
set(${output} ${HAVE_MSAN} PARENT_SCOPE)
18+
endfunction()

0 commit comments

Comments
 (0)