Skip to content

Commit 908530e

Browse files
committed
cmake: Add SANITIZERS option
1 parent 8bb0e85 commit 908530e

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

CMakeLists.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,41 @@ target_link_libraries(core_interface INTERFACE
263263
Threads::Threads
264264
)
265265

266+
add_library(sanitize_interface INTERFACE)
267+
target_link_libraries(core_interface INTERFACE sanitize_interface)
268+
if(SANITIZERS)
269+
# First check if the compiler accepts flags. If an incompatible pair like
270+
# -fsanitize=address,thread is used here, this check will fail. This will also
271+
# fail if a bad argument is passed, e.g. -fsanitize=undfeined
272+
try_append_cxx_flags("-fsanitize=${SANITIZERS}" TARGET sanitize_interface
273+
RESULT_VAR cxx_supports_sanitizers
274+
SKIP_LINK
275+
)
276+
if(NOT cxx_supports_sanitizers)
277+
message(FATAL_ERROR "Compiler did not accept requested flags.")
278+
endif()
279+
280+
# Some compilers (e.g. GCC) require additional libraries like libasan,
281+
# libtsan, libubsan, etc. Make sure linking still works with the sanitize
282+
# flag. This is a separate check so we can give a better error message when
283+
# the sanitize flags are supported by the compiler but the actual sanitizer
284+
# libs are missing.
285+
try_append_linker_flag("-fsanitize=${SANITIZERS}" VAR SANITIZER_LDFLAGS
286+
SOURCE "
287+
#include <cstdint>
288+
#include <cstddef>
289+
extern \"C\" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { return 0; }
290+
__attribute__((weak)) // allow for libFuzzer linking
291+
int main() { return 0; }
292+
"
293+
RESULT_VAR linker_supports_sanitizers
294+
)
295+
if(NOT linker_supports_sanitizers)
296+
message(FATAL_ERROR "Linker did not accept requested flags, you are missing required libraries.")
297+
endif()
298+
endif()
299+
target_link_options(sanitize_interface INTERFACE ${SANITIZER_LDFLAGS})
300+
266301
include(AddBoostIfNeeded)
267302
add_boost_if_needed()
268303

src/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ set(SECP256K1_BUILD_BENCHMARK OFF CACHE BOOL "" FORCE)
4444
set(SECP256K1_BUILD_TESTS ${BUILD_TESTS} CACHE BOOL "" FORCE)
4545
set(SECP256K1_BUILD_EXHAUSTIVE_TESTS ${BUILD_TESTS} CACHE BOOL "" FORCE)
4646
set(SECP256K1_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
47+
include(GetTargetInterface)
48+
# -fsanitize and related flags apply to both C++ and C,
49+
# so we can pass them down to libsecp256k1 as CFLAGS.
50+
get_target_interface(core_sanitizer_cxx_flags "" sanitize_interface COMPILE_OPTIONS)
51+
set(SECP256K1_LATE_CFLAGS ${core_sanitizer_cxx_flags} CACHE STRING "" FORCE)
52+
unset(core_sanitizer_cxx_flags)
4753
# We want to build libsecp256k1 with the most tested RelWithDebInfo configuration.
4854
enable_language(C)
4955
foreach(config IN LISTS CMAKE_BUILD_TYPE CMAKE_CONFIGURATION_TYPES)

0 commit comments

Comments
 (0)