diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ae79fbcb..c815f27d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -282,6 +282,9 @@ mark_as_advanced(ENABLE_SANDBOX) if(NOT WIN32) option(ENABLE_COVERAGE "Enable Code Coverage using gcov(only GCC/Debug build)" OFF) endif() +if(NOT WIN32) + option(ENABLE_ASAN "Enable Address Sanatizer (Debug build)" OFF) +endif() #------------------------------------ # Check memkind @@ -657,8 +660,7 @@ if(NOT WIN32) cmake_print_variables(ENABLE_COVERAGE) if(ENABLE_COVERAGE) if(NOT (${CMAKE_C_COMPILER_ID} MATCHES "GNU")) - message(WARNING "Coverage is only supported for GNU/Linux GCC Debug build") - message(" Code Coverage is disabled.") + message(FATAL_ERROR "Coverage is only supported for GNU/Linux GCC Debug build") set(ENABLE_COVERAGE OFF) endif() if(NOT(ENABLE_DEBUG STREQUAL "noopt")) @@ -671,11 +673,24 @@ if(NOT WIN32) if(ENABLE_COVERAGE) message(" Code Coverage is enabled.") else() - cmake_print_variables(ENABLE_COVERAGE) message(" Code Coverage is disabled.") endif() endif() +if(NOT WIN32) + cmake_print_variables(ENABLE_ASAN) + if(ENABLE_ASAN) + if(NOT (${CMAKE_C_COMPILER_ID} MATCHES "Clang")) + message(FATAL_ERROR "ASAN is supported only for Clang/Linux" ) + endif() + endif() + if(ENABLE_ASAN) + message(" Address Sanatizer is enabled.") + else() + message(" Address Sanatizer is disabled.") + endif() +endif() + # Initialize threading model, using the corresponding cache variable. set(THREADING_MODEL ${ENABLE_THREADING}) @@ -852,7 +867,16 @@ endif() # Code-coverage flags #-------------------------------------------- if(ENABLE_COVERAGE AND (NOT WIN32)) - set(CMAKE_C_FLAGS "-fprofile-arcs -ftest-coverage") + set(COVERAGE_FLAGS "-fprofile-arcs -ftest-coverage") + list(APPEND CMAKE_C_FLAGS ${COVERAGE_FLAGS}) +endif() + +#-------------------------------------------- +# Address Sanatizer flags +#-------------------------------------------- +if(ENABLE_ASAN AND (NOT WIN32)) + set(ASAN_FLAGS "-g -fsanitize=address") + list(APPEND CMAKE_C_FLAGS ${ASAN_FLAGS}) endif() #-------------------------------------------- diff --git a/build/cmake/config_print.py b/build/cmake/config_print.py index fb1f7bdeb..c6e8cbc2c 100644 --- a/build/cmake/config_print.py +++ b/build/cmake/config_print.py @@ -285,6 +285,12 @@ def main(): print( " report in html format. Code coverage support is provided" ) print( " only on LINUX with GCC compiler." ) print( " " ) + print( " -DENABLE_ASAN=ON or -DENABLE_ASAN=OFF" ) + print( " " ) + print( " Enable (disabled by default) Address Sanitizer to find " ) + print( " memory access error. Address Sanitizer support is provided" ) + print( " only on LINUX with Clang compiler" ) + print( " " ) print( " Additional CMake Variables:" ) print( " " ) print( " CMAKE_C_COMPILER Specifies the C compiler to use." ) diff --git a/gtestsuite/CMakeLists.txt b/gtestsuite/CMakeLists.txt index 2a3ce74ba..8cfdc31d8 100644 --- a/gtestsuite/CMakeLists.txt +++ b/gtestsuite/CMakeLists.txt @@ -279,12 +279,13 @@ if(LINUX) add_compile_options(-g -Wall -Wno-unused-function -Wfatal-errors -fPIC ) if(ENABLE_ASAN) - add_compile_options(-fsanitize=address) - add_definitions(-DENABLE_ASAN) + set(ASAN_FLAGS "-fsanitize=address") + list(APPEND CMAKE_C_FLAGS ${ASAN_FLAGS}) endif() if(ENABLE_COVERAGE) - set(CMAKE_CXX_FLAGS "-O0 --coverage") + set(COVERAGE_FLAGS "-O0 --coverage") + list(APPEND CMAKE_C_FLAGS ${COVERAGE_FLAGS}) endif() endif() @@ -307,20 +308,23 @@ if(BLIS_LINKING_TYPE STREQUAL "static") if(ENABLE_THREADING STREQUAL "openmp") try_run(RUNRESULT COMPILERESULT "${CMAKE_BINARY_DIR}/temp" SOURCES ${CMAKE_SOURCE_DIR}/cmake/config_ukr_tests.cpp COMPILE_DEFINITIONS -I${BLIS_PATH}/include/ -I${BLIS_PATH}/include/blis - LINK_LIBRARIES ${BLIS_LIBRARY} ${COMMON_LIBS} OpenMP::OpenMP_CXX + LINK_LIBRARIES ${BLIS_LIBRARY} ${COMMON_LIBS} OpenMP::OpenMP_CXX ${ASAN_FLAGS} ${COVERAGE_FLAGS} RUN_OUTPUT_VARIABLE UKR_CONFIG COMPILE_OUTPUT_VARIABLE COMP_VAR ) else() try_run(RUNRESULT COMPILERESULT "${CMAKE_BINARY_DIR}/temp" SOURCES ${CMAKE_SOURCE_DIR}/cmake/config_ukr_tests.cpp COMPILE_DEFINITIONS -I${BLIS_PATH}/include/ -I${BLIS_PATH}/include/blis - LINK_LIBRARIES ${BLIS_LIBRARY} ${COMMON_LIBS} + LINK_LIBRARIES ${BLIS_LIBRARY} ${COMMON_LIBS} ${ASAN_FLAGS} ${COVERAGE_FLAGS} RUN_OUTPUT_VARIABLE UKR_CONFIG COMPILE_OUTPUT_VARIABLE COMP_VAR ) endif() # Uncomment this to debug this snippet above, if necessary. - #message("Build output: ${COMP_VAR}") + message("Build output: ${COMP_VAR}") + if(NOT COMPILERESULT) + message(FATAL_ERROR "Compiling config_ukr_tests.cpp failed with the following error ${COMP_VAR}.") + endif() # Remove all empty items from the list. list(REMOVE_ITEM UKR_CONFIG "") # We iterate through the list returned from the snippet above. diff --git a/gtestsuite/README.md b/gtestsuite/README.md index d3b3def0b..f033add02 100644 --- a/gtestsuite/README.md +++ b/gtestsuite/README.md @@ -86,7 +86,13 @@ For threaded MKL the following OpenMP runtimes are used: * For testing a 64-bit integer BLIS library, use `-DINT_SIZE=64`. ## Address Sanitizer (Linux Only) * To build using address sanitizer, configure using `-DENABLE_ASAN=ON`. [**OFF by default**] -* An installation to BLIS which was build with ASAN flags[CFLAGS="-O0 -g -fsanitize=address"] needs to be provided. +* An installation to BLIS which was build with ASAN flags needs to be provided. +* Set -DENABLE_ASAN=ON when building BLIS with CMake, or set CFLAGS="-O0 -g -fsanitize=address" when building with make. +* By default redzone size is 16 bytes and can redzone size can be increase to 2048 bytes. +```console +$ ASAN_OPTIONS=redzone=2048 +``` + ## Code Coverage (Only GCC Compiler) * BLIS : Configure BLIS Library with code coverage flags[CFLAGS="-O0 -fprofile-arcs -ftest-coverage"], compile and install. * Gtestsuite : To build for code coverage, configure cmake with `-DENABLE_COVERAGE=ON`. [**OFF by default**] and then compile and run the executable. diff --git a/gtestsuite/testsuite/CMakeLists.txt b/gtestsuite/testsuite/CMakeLists.txt index 76e4e2e34..db0d58e49 100644 --- a/gtestsuite/testsuite/CMakeLists.txt +++ b/gtestsuite/testsuite/CMakeLists.txt @@ -93,12 +93,8 @@ foreach(dir ${DIRS}) if( (ENABLE_THREADING STREQUAL "openmp") OR (MKL_ENABLE_THREADING STREQUAL "openmp")) target_link_libraries(${target_name}.${dir}.${subdir} OpenMP::OpenMP_CXX) endif() - if(ENABLE_ASAN) - target_link_libraries(${target_name}.${dir}.${subdir} -fsanitize=address) - endif() - if(ENABLE_COVERAGE) - target_link_libraries(${target_name}.${dir}.${subdir} "--coverage") - endif() + target_link_libraries(${target_name}.${dir}.${subdir} ${ASAN_FLAGS}) + target_link_libraries(${target_name}.${dir}.${subdir} ${COVERAGE_FLAGS}) if(TEST_INTERFACE STREQUAL "BLAS") target_compile_definitions(${target_name}.${dir}.${subdir} PUBLIC TEST_BLAS) elseif(TEST_INTERFACE STREQUAL "CBLAS") diff --git a/testsuite/CMakeLists.txt b/testsuite/CMakeLists.txt index 4b1686f4a..5b794b597 100644 --- a/testsuite/CMakeLists.txt +++ b/testsuite/CMakeLists.txt @@ -25,7 +25,10 @@ file(GLOB testsuite_sources LIST_DIRECTORIES false ${CMAKE_CURRENT_SOURCE_DIR}/s set(CINFLAGS ${INC_PATH}) if((NOT WIN32) AND ENABLE_COVERAGE) include(coverage.cmake) - set(LDFLAGS "${LDFLAGS} -ftest-coverage") + list(APPEND LDFLAGS ${COVERAGE_FLAGS}) +endif() +if((NOT WIN32) AND ENABLE_ASAN) + list(APPEND LDFLAGS ${ASAN_FLAGS}) endif() # Create an executable using the sources above.