Skip to content

Commit

Permalink
Merge branch 'sanitizers' into 'master'
Browse files Browse the repository at this point in the history
[cmake] Updated sanitizers logic

See merge request ogs/ogs!4785
  • Loading branch information
bilke committed Nov 13, 2023
2 parents 465f15f + 77737e6 commit 0e18d5b
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 53 deletions.
2 changes: 1 addition & 1 deletion ProcessLib/HydroMechanics/Tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (NOT OGS_USE_MPI)
OgsTest(PROJECTFILE HydroMechanics/ExcavationNiches/excavation_niches.prj RUNTIME 60)
OgsTest(PROJECTFILE HydroMechanics/StaggeredScheme/ConsolidationBenchmark/consolidation_benchmark_mono.prj RUNTIME 1)
OgsTest(PROJECTFILE HydroMechanics/StaggeredScheme/ConsolidationBenchmark/consolidation_benchmark.prj RUNTIME 2)
if(NOT OGS_ADDRESS_SANITIZER)
if(NOT ENABLE_ASAN)
OgsTest(PROJECTFILE HydroMechanics/Linear/Confined_Compression/square_1e2_linear.prj RUNTIME 2)
OgsTest(PROJECTFILE HydroMechanics/ExcavationNiches/excavation_niches2.prj RUNTIME 4)
endif()
Expand Down
2 changes: 1 addition & 1 deletion ProcessLib/RichardsFlow/Tests.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if (NOT OGS_USE_MPI)
# Comparison test for richards mechanics w/o deformations.
if(NOT OGS_ADDRESS_SANITIZER)
if(NOT ENABLE_ASAN)
OgsTest(PROJECTFILE RichardsMechanics/RichardsFlow_2d_richardsflow.prj RUNTIME 2)
endif()
OgsTest(PROJECTFILE Parabolic/Richards/flow_fully_saturated.prj)
Expand Down
2 changes: 1 addition & 1 deletion ProcessLib/RichardsMechanics/Tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if (NOT OGS_USE_MPI)
OgsTest(PROJECTFILE RichardsMechanics/bishops_effective_stress_power_law.prj)
OgsTest(PROJECTFILE RichardsMechanics/bishops_effective_stress_saturation_cutoff.prj)
OgsTest(PROJECTFILE RichardsMechanics/alternative_mass_balance_anzInterval_10.prj)
if(NOT OGS_ADDRESS_SANITIZER)
if(NOT ENABLE_ASAN)
OgsTest(PROJECTFILE RichardsMechanics/rotated_consolidation.prj RUNTIME 2)
endif()
OgsTest(PROJECTFILE RichardsMechanics/LiakopoulosHM/liakopoulos.prj RUNTIME 17)
Expand Down
4 changes: 2 additions & 2 deletions ProcessLib/SmallDeformation/Tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if (NOT OGS_USE_MPI)
)
endif()
OgsTest(PROJECTFILE Mechanics/Linear/disc_with_hole.prj)
if(NOT OGS_ADDRESS_SANITIZER)
if(NOT ENABLE_ASAN)
OgsTest(PROJECTFILE Mechanics/Linear/ElementDeactivation3D/element_deactivation_M_3D.prj RUNTIME 2)
endif()
OgsTest(PROJECTFILE Mechanics/Linear/square_1e5.prj RUNTIME 200)
Expand All @@ -31,7 +31,7 @@ if (NOT OGS_USE_MPI)
OgsTest(PROJECTFILE Mechanics/Linear/Orthotropy/cube_1e0_orthotropic_xyz.prj)
OgsTest(PROJECTFILE Mechanics/Linear/Orthotropy/cube_1e0_orthotropic_yzx.prj)
OgsTest(PROJECTFILE Mechanics/Linear/Orthotropy/cube_1e0_orthotropic_zxy.prj)
if(NOT OGS_ADDRESS_SANITIZER)
if(NOT ENABLE_ASAN)
OgsTest(PROJECTFILE Mechanics/Linear/Orthotropy/m3_3Dshearz.prj RUNTIME 2)
OgsTest(PROJECTFILE Mechanics/Linear/Orthotropy/m3_3Dshearz_rot.prj RUNTIME 2)
OgsTest(PROJECTFILE Mechanics/Ehlers/cube_1e1.prj RUNTIME 4)
Expand Down
2 changes: 1 addition & 1 deletion ProcessLib/TH2M/Tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if (NOT OGS_USE_MPI)
OgsTest(PROJECTFILE TH2M/TH/idealGasLaw/compression_gas.prj RUNTIME 1)
OgsTest(PROJECTFILE TH2M/H2M/Liakopoulos/liakopoulos_TH2M.prj RUNTIME 15)
NotebookTest(NOTEBOOKFILE TH2M/H2M/Liakopoulos/ogs-jupyter-lab-h2m-2d-liakopoulos.ipynb RUNTIME 15)
if(NOT OGS_ADDRESS_SANITIZER)
if(NOT ENABLE_ASAN)
OgsTest(PROJECTFILE TH2M/H2M/Liakopoulos/liakopoulos_newton.xml RUNTIME 5)
endif()
OgsTest(PROJECTFILE TH2M/H2M/OrthotropicSwelling/square.prj RUNTIME 1)
Expand Down
2 changes: 1 addition & 1 deletion ProcessLib/ThermoMechanics/Tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ AddTest(
EXECUTABLE_ARGS SimpleAxisymmetricCreep.prj
WRAPPER time
TESTER vtkdiff
REQUIREMENTS NOT (OGS_USE_LIS OR OGS_USE_MPI OR OGS_ADDRESS_SANITIZER)
REQUIREMENTS NOT (OGS_USE_LIS OR OGS_USE_MPI OR ENABLE_ASAN)
RUNTIME 4
DIFF_DATA
expected_SimpleAxisymmetricCreep_ts_370_t_360.000000.vtu SimpleAxisymmetricCreep_ts_370_t_360.000000.vtu displacement displacement 1e-14 1e-10
Expand Down
7 changes: 5 additions & 2 deletions scripts/ci/jobs/build-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ build linux debug with sanitizers:
-DBUILD_SHARED_LIBS=ON
-DOGS_COMPILE_FLAGS=-O3;-fno-var-tracking
-DOGS_EIGEN_DYNAMIC_SHAPE_MATRICES=OFF
-DOGS_ADDRESS_SANITIZER=ON
-DOGS_UNDEFINED_BEHAVIOR_SANITIZER=ON
-DENABLE_ASAN=ON
-DENABLE_UBSAN=ON
-DENABLE_LSAN=ON
-DOGS_CTEST_MAX_RUNTIME=5
-DOGS_USE_PIP=ON
-DOGS_USE_MFRONT=ON
-DOGS_BUILD_TFEL=ON
UBSAN_OPTIONS: "print_stacktrace=1"
LSAN_OPTIONS: "suppressions=$CI_PROJECT_DIR/scripts/test/leak_sanitizer.suppressions"
CTEST_TIMEOUT: 120
Expand Down
2 changes: 1 addition & 1 deletion scripts/ci/jobs/clang-sanitizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ clang sanitizer:
needs: [ci_images]
variables:
BUILD_DIR: "../build/sanitizer"
CMAKE_ARGS: "-DOGS_ADDRESS_SANITIZER=ON -DOGS_UNDEFINED_BEHAVIOR_SANITIZER=ON"
CMAKE_ARGS: "-DENABLE_ASAN=ON -DENABLE_USAN=ON"
UBSAN_OPTIONS: "print_stacktrace=1"
LSAN_OPTIONS: "suppressions=$CI_PROJECT_DIR/scripts/test/leak_sanitizer.suppressions"
image: $CONTAINER_CLANG_IMAGE
Expand Down
20 changes: 0 additions & 20 deletions scripts/cmake/ClangSanitizer.cmake

This file was deleted.

6 changes: 4 additions & 2 deletions scripts/cmake/CompilerSetup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ if(COMPILER_IS_GCC OR COMPILER_IS_CLANG OR COMPILER_IS_INTEL)
)
endif()
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fext-numeric-literals>)
include(GCCSanitizer)
if(CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 13.1.1
OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 13.2.1
)
Expand Down Expand Up @@ -101,7 +100,6 @@ if(COMPILER_IS_GCC OR COMPILER_IS_CLANG OR COMPILER_IS_INTEL)
)
endif()
endif()
include(ClangSanitizer)
endif()

if(COMPILER_IS_INTEL)
Expand Down Expand Up @@ -182,6 +180,10 @@ if(MSVC)
endif()
endif()

if(PROJECT_IS_TOP_LEVEL)
include(Sanitizers)
endif()

add_compile_options(
${OGS_CXX_FLAGS} # user-given, CMake-option
${CPU_FLAGS}
Expand Down
20 changes: 19 additions & 1 deletion scripts/cmake/DependenciesExternalProject.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,32 @@ if(OGS_USE_MFRONT)
"TFEL Python bindings disabled as Boosts Python library was not found."
)
endif()

# Only one flag supported, prefer ASAN
if(ENABLE_ASAN)
set(_sanitize_flag -fsanitize=address)
endif()
if(ENABLE_UBSAN AND NOT DEFINED _sanitize_flag)
set(_sanitize_flag -fsanitize=undefined)
elseif(ENABLE_UBSAN AND DEFINED _sanitize_flag)
message(STATUS "MFront: ASAN enabled only! UBSAN is off.")
endif()
if(DEFINED _sanitize_flag)
foreach(var CXX EXE_LINKER SHARED_LINKER MODULE_LINKER)
list(APPEND _tfel_cmake_args
"-DCMAKE_${var}_FLAGS_INIT=${_sanitize_flag}"
)
endforeach()
endif()

BuildExternalProject(
TFEL ${_tfel_source}
CMAKE_ARGS "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
"-DBUILD_SHARED_LIBS=OFF"
"-DCMAKE_POSITION_INDEPENDENT_CODE=ON"
"-Denable-testing=OFF"
${_defaultCMakeArgs}
${_tfel_cmake_args}
"${_tfel_cmake_args}"
)
message(
STATUS
Expand Down
20 changes: 0 additions & 20 deletions scripts/cmake/GCCSanitizer.cmake

This file was deleted.

60 changes: 60 additions & 0 deletions scripts/cmake/Sanitizers.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Initial implementation from Professional CMake, 16th Edition, by Craig Scott
option(ENABLE_ASAN "Enable AddressSanitizer" NO)
if(MSVC)
if(ENABLE_ASAN)
string(REPLACE "/RTC1" "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG
"${CMAKE_CXX_FLAGS_DEBUG}"
)
add_compile_options(
/fsanitize=address /fsanitize-address-use-after-return
)
endif()
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
option(ENABLE_LSAN "Enable LeakSanitizer" NO)
option(ENABLE_TSAN "Enable ThreadSanitizer" NO)
option(ENABLE_UBSAN "Enable UndefinedBehaviorSanitizer" NO)
if(NOT APPLE)
option(ENABLE_MSAN "Enable MemorySanitizer" NO)
endif()
if((ENABLE_ASAN AND (ENABLE_TSAN OR ENABLE_MSAN))
OR (ENABLE_LSAN AND (ENABLE_TSAN OR ENABLE_MSAN)) OR (ENABLE_TSAN
AND ENABLE_MSAN)
)
message(
FATAL_ERROR
"Invalid sanitizer combination:\n"
" ENABLE_ASAN: ${ENABLE_ASAN}\n"
" ENABLE_LSAN: ${ENABLE_LSAN}\n"
" ENABLE_TSAN: ${ENABLE_TSAN}\n"
" ENABLE_MSAN: ${ENABLE_MSAN}"
)
endif()

if(CMAKE_C_COMPILER_ID MATCHES "GNU")
set(_ubsan_options
"$<COMMA>unreachable$<COMMA>integer-divide-by-zero$<COMMA>vla-bound$<COMMA>bounds$<COMMA>null"
)
endif()
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
set(_ubsan_options "$<COMMA>integer")
endif()
add_compile_options(
$<$<BOOL:${ENABLE_ASAN}>:-fsanitize=address>
$<$<BOOL:${ENABLE_ASAN}>:-fno-omit-frame-pointer>
$<$<BOOL:${ENABLE_LSAN}>:-fsanitize=leak>
$<$<BOOL:${ENABLE_MSAN}>:-fsanitize=memory>
$<$<BOOL:${ENABLE_TSAN}>:-fsanitize=thread>
$<$<BOOL:${ENABLE_UBSAN}>:-fsanitize=undefined${_ubsan_options}>
$<$<AND:$<BOOL:${ENABLE_UBSAN}>,$<C_COMPILER_ID:Clang>>:-fsanitize-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/scripts/test/clang_sanitizer_blacklist.txt>
)
add_link_options(
$<$<BOOL:${ENABLE_ASAN}>:-fsanitize=address>
$<$<BOOL:${ENABLE_ASAN}>:-fno-omit-frame-pointer>
$<$<BOOL:${ENABLE_LSAN}>:-fsanitize=leak>
$<$<BOOL:${ENABLE_MSAN}>:-fsanitize=memory>
$<$<BOOL:${ENABLE_TSAN}>:-fsanitize=thread>
$<$<BOOL:${ENABLE_UBSAN}>:-fsanitize=undefined${_ubsan_options}>
$<$<AND:$<BOOL:${ENABLE_UBSAN}>,$<C_COMPILER_ID:Clang>>:-fsanitize-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/scripts/test/clang_sanitizer_blacklist.txt>
)
endif()

0 comments on commit 0e18d5b

Please sign in to comment.