Skip to content

Commit 0e18d5b

Browse files
committed
Merge branch 'sanitizers' into 'master'
[cmake] Updated sanitizers logic See merge request ogs/ogs!4785
2 parents 465f15f + 77737e6 commit 0e18d5b

File tree

13 files changed

+96
-53
lines changed

13 files changed

+96
-53
lines changed

ProcessLib/HydroMechanics/Tests.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if (NOT OGS_USE_MPI)
88
OgsTest(PROJECTFILE HydroMechanics/ExcavationNiches/excavation_niches.prj RUNTIME 60)
99
OgsTest(PROJECTFILE HydroMechanics/StaggeredScheme/ConsolidationBenchmark/consolidation_benchmark_mono.prj RUNTIME 1)
1010
OgsTest(PROJECTFILE HydroMechanics/StaggeredScheme/ConsolidationBenchmark/consolidation_benchmark.prj RUNTIME 2)
11-
if(NOT OGS_ADDRESS_SANITIZER)
11+
if(NOT ENABLE_ASAN)
1212
OgsTest(PROJECTFILE HydroMechanics/Linear/Confined_Compression/square_1e2_linear.prj RUNTIME 2)
1313
OgsTest(PROJECTFILE HydroMechanics/ExcavationNiches/excavation_niches2.prj RUNTIME 4)
1414
endif()

ProcessLib/RichardsFlow/Tests.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
if (NOT OGS_USE_MPI)
22
# Comparison test for richards mechanics w/o deformations.
3-
if(NOT OGS_ADDRESS_SANITIZER)
3+
if(NOT ENABLE_ASAN)
44
OgsTest(PROJECTFILE RichardsMechanics/RichardsFlow_2d_richardsflow.prj RUNTIME 2)
55
endif()
66
OgsTest(PROJECTFILE Parabolic/Richards/flow_fully_saturated.prj)

ProcessLib/RichardsMechanics/Tests.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ if (NOT OGS_USE_MPI)
1818
OgsTest(PROJECTFILE RichardsMechanics/bishops_effective_stress_power_law.prj)
1919
OgsTest(PROJECTFILE RichardsMechanics/bishops_effective_stress_saturation_cutoff.prj)
2020
OgsTest(PROJECTFILE RichardsMechanics/alternative_mass_balance_anzInterval_10.prj)
21-
if(NOT OGS_ADDRESS_SANITIZER)
21+
if(NOT ENABLE_ASAN)
2222
OgsTest(PROJECTFILE RichardsMechanics/rotated_consolidation.prj RUNTIME 2)
2323
endif()
2424
OgsTest(PROJECTFILE RichardsMechanics/LiakopoulosHM/liakopoulos.prj RUNTIME 17)

ProcessLib/SmallDeformation/Tests.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ if (NOT OGS_USE_MPI)
1414
)
1515
endif()
1616
OgsTest(PROJECTFILE Mechanics/Linear/disc_with_hole.prj)
17-
if(NOT OGS_ADDRESS_SANITIZER)
17+
if(NOT ENABLE_ASAN)
1818
OgsTest(PROJECTFILE Mechanics/Linear/ElementDeactivation3D/element_deactivation_M_3D.prj RUNTIME 2)
1919
endif()
2020
OgsTest(PROJECTFILE Mechanics/Linear/square_1e5.prj RUNTIME 200)
@@ -31,7 +31,7 @@ if (NOT OGS_USE_MPI)
3131
OgsTest(PROJECTFILE Mechanics/Linear/Orthotropy/cube_1e0_orthotropic_xyz.prj)
3232
OgsTest(PROJECTFILE Mechanics/Linear/Orthotropy/cube_1e0_orthotropic_yzx.prj)
3333
OgsTest(PROJECTFILE Mechanics/Linear/Orthotropy/cube_1e0_orthotropic_zxy.prj)
34-
if(NOT OGS_ADDRESS_SANITIZER)
34+
if(NOT ENABLE_ASAN)
3535
OgsTest(PROJECTFILE Mechanics/Linear/Orthotropy/m3_3Dshearz.prj RUNTIME 2)
3636
OgsTest(PROJECTFILE Mechanics/Linear/Orthotropy/m3_3Dshearz_rot.prj RUNTIME 2)
3737
OgsTest(PROJECTFILE Mechanics/Ehlers/cube_1e1.prj RUNTIME 4)

ProcessLib/TH2M/Tests.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ if (NOT OGS_USE_MPI)
1414
OgsTest(PROJECTFILE TH2M/TH/idealGasLaw/compression_gas.prj RUNTIME 1)
1515
OgsTest(PROJECTFILE TH2M/H2M/Liakopoulos/liakopoulos_TH2M.prj RUNTIME 15)
1616
NotebookTest(NOTEBOOKFILE TH2M/H2M/Liakopoulos/ogs-jupyter-lab-h2m-2d-liakopoulos.ipynb RUNTIME 15)
17-
if(NOT OGS_ADDRESS_SANITIZER)
17+
if(NOT ENABLE_ASAN)
1818
OgsTest(PROJECTFILE TH2M/H2M/Liakopoulos/liakopoulos_newton.xml RUNTIME 5)
1919
endif()
2020
OgsTest(PROJECTFILE TH2M/H2M/OrthotropicSwelling/square.prj RUNTIME 1)

ProcessLib/ThermoMechanics/Tests.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ AddTest(
263263
EXECUTABLE_ARGS SimpleAxisymmetricCreep.prj
264264
WRAPPER time
265265
TESTER vtkdiff
266-
REQUIREMENTS NOT (OGS_USE_LIS OR OGS_USE_MPI OR OGS_ADDRESS_SANITIZER)
266+
REQUIREMENTS NOT (OGS_USE_LIS OR OGS_USE_MPI OR ENABLE_ASAN)
267267
RUNTIME 4
268268
DIFF_DATA
269269
expected_SimpleAxisymmetricCreep_ts_370_t_360.000000.vtu SimpleAxisymmetricCreep_ts_370_t_360.000000.vtu displacement displacement 1e-14 1e-10

scripts/ci/jobs/build-linux.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ build linux debug with sanitizers:
6565
-DBUILD_SHARED_LIBS=ON
6666
-DOGS_COMPILE_FLAGS=-O3;-fno-var-tracking
6767
-DOGS_EIGEN_DYNAMIC_SHAPE_MATRICES=OFF
68-
-DOGS_ADDRESS_SANITIZER=ON
69-
-DOGS_UNDEFINED_BEHAVIOR_SANITIZER=ON
68+
-DENABLE_ASAN=ON
69+
-DENABLE_UBSAN=ON
70+
-DENABLE_LSAN=ON
7071
-DOGS_CTEST_MAX_RUNTIME=5
7172
-DOGS_USE_PIP=ON
73+
-DOGS_USE_MFRONT=ON
74+
-DOGS_BUILD_TFEL=ON
7275
UBSAN_OPTIONS: "print_stacktrace=1"
7376
LSAN_OPTIONS: "suppressions=$CI_PROJECT_DIR/scripts/test/leak_sanitizer.suppressions"
7477
CTEST_TIMEOUT: 120

scripts/ci/jobs/clang-sanitizer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ clang sanitizer:
55
needs: [ci_images]
66
variables:
77
BUILD_DIR: "../build/sanitizer"
8-
CMAKE_ARGS: "-DOGS_ADDRESS_SANITIZER=ON -DOGS_UNDEFINED_BEHAVIOR_SANITIZER=ON"
8+
CMAKE_ARGS: "-DENABLE_ASAN=ON -DENABLE_USAN=ON"
99
UBSAN_OPTIONS: "print_stacktrace=1"
1010
LSAN_OPTIONS: "suppressions=$CI_PROJECT_DIR/scripts/test/leak_sanitizer.suppressions"
1111
image: $CONTAINER_CLANG_IMAGE

scripts/cmake/ClangSanitizer.cmake

Lines changed: 0 additions & 20 deletions
This file was deleted.

scripts/cmake/CompilerSetup.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ if(COMPILER_IS_GCC OR COMPILER_IS_CLANG OR COMPILER_IS_INTEL)
6666
)
6767
endif()
6868
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fext-numeric-literals>)
69-
include(GCCSanitizer)
7069
if(CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 13.1.1
7170
OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 13.2.1
7271
)
@@ -101,7 +100,6 @@ if(COMPILER_IS_GCC OR COMPILER_IS_CLANG OR COMPILER_IS_INTEL)
101100
)
102101
endif()
103102
endif()
104-
include(ClangSanitizer)
105103
endif()
106104

107105
if(COMPILER_IS_INTEL)
@@ -182,6 +180,10 @@ if(MSVC)
182180
endif()
183181
endif()
184182

183+
if(PROJECT_IS_TOP_LEVEL)
184+
include(Sanitizers)
185+
endif()
186+
185187
add_compile_options(
186188
${OGS_CXX_FLAGS} # user-given, CMake-option
187189
${CPU_FLAGS}

scripts/cmake/DependenciesExternalProject.cmake

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,32 @@ if(OGS_USE_MFRONT)
8080
"TFEL Python bindings disabled as Boosts Python library was not found."
8181
)
8282
endif()
83+
84+
# Only one flag supported, prefer ASAN
85+
if(ENABLE_ASAN)
86+
set(_sanitize_flag -fsanitize=address)
87+
endif()
88+
if(ENABLE_UBSAN AND NOT DEFINED _sanitize_flag)
89+
set(_sanitize_flag -fsanitize=undefined)
90+
elseif(ENABLE_UBSAN AND DEFINED _sanitize_flag)
91+
message(STATUS "MFront: ASAN enabled only! UBSAN is off.")
92+
endif()
93+
if(DEFINED _sanitize_flag)
94+
foreach(var CXX EXE_LINKER SHARED_LINKER MODULE_LINKER)
95+
list(APPEND _tfel_cmake_args
96+
"-DCMAKE_${var}_FLAGS_INIT=${_sanitize_flag}"
97+
)
98+
endforeach()
99+
endif()
100+
83101
BuildExternalProject(
84102
TFEL ${_tfel_source}
85103
CMAKE_ARGS "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
86104
"-DBUILD_SHARED_LIBS=OFF"
87105
"-DCMAKE_POSITION_INDEPENDENT_CODE=ON"
88106
"-Denable-testing=OFF"
89107
${_defaultCMakeArgs}
90-
${_tfel_cmake_args}
108+
"${_tfel_cmake_args}"
91109
)
92110
message(
93111
STATUS

scripts/cmake/GCCSanitizer.cmake

Lines changed: 0 additions & 20 deletions
This file was deleted.

scripts/cmake/Sanitizers.cmake

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Initial implementation from Professional CMake, 16th Edition, by Craig Scott
2+
option(ENABLE_ASAN "Enable AddressSanitizer" NO)
3+
if(MSVC)
4+
if(ENABLE_ASAN)
5+
string(REPLACE "/RTC1" "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
6+
string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG
7+
"${CMAKE_CXX_FLAGS_DEBUG}"
8+
)
9+
add_compile_options(
10+
/fsanitize=address /fsanitize-address-use-after-return
11+
)
12+
endif()
13+
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
14+
option(ENABLE_LSAN "Enable LeakSanitizer" NO)
15+
option(ENABLE_TSAN "Enable ThreadSanitizer" NO)
16+
option(ENABLE_UBSAN "Enable UndefinedBehaviorSanitizer" NO)
17+
if(NOT APPLE)
18+
option(ENABLE_MSAN "Enable MemorySanitizer" NO)
19+
endif()
20+
if((ENABLE_ASAN AND (ENABLE_TSAN OR ENABLE_MSAN))
21+
OR (ENABLE_LSAN AND (ENABLE_TSAN OR ENABLE_MSAN)) OR (ENABLE_TSAN
22+
AND ENABLE_MSAN)
23+
)
24+
message(
25+
FATAL_ERROR
26+
"Invalid sanitizer combination:\n"
27+
" ENABLE_ASAN: ${ENABLE_ASAN}\n"
28+
" ENABLE_LSAN: ${ENABLE_LSAN}\n"
29+
" ENABLE_TSAN: ${ENABLE_TSAN}\n"
30+
" ENABLE_MSAN: ${ENABLE_MSAN}"
31+
)
32+
endif()
33+
34+
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
35+
set(_ubsan_options
36+
"$<COMMA>unreachable$<COMMA>integer-divide-by-zero$<COMMA>vla-bound$<COMMA>bounds$<COMMA>null"
37+
)
38+
endif()
39+
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
40+
set(_ubsan_options "$<COMMA>integer")
41+
endif()
42+
add_compile_options(
43+
$<$<BOOL:${ENABLE_ASAN}>:-fsanitize=address>
44+
$<$<BOOL:${ENABLE_ASAN}>:-fno-omit-frame-pointer>
45+
$<$<BOOL:${ENABLE_LSAN}>:-fsanitize=leak>
46+
$<$<BOOL:${ENABLE_MSAN}>:-fsanitize=memory>
47+
$<$<BOOL:${ENABLE_TSAN}>:-fsanitize=thread>
48+
$<$<BOOL:${ENABLE_UBSAN}>:-fsanitize=undefined${_ubsan_options}>
49+
$<$<AND:$<BOOL:${ENABLE_UBSAN}>,$<C_COMPILER_ID:Clang>>:-fsanitize-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/scripts/test/clang_sanitizer_blacklist.txt>
50+
)
51+
add_link_options(
52+
$<$<BOOL:${ENABLE_ASAN}>:-fsanitize=address>
53+
$<$<BOOL:${ENABLE_ASAN}>:-fno-omit-frame-pointer>
54+
$<$<BOOL:${ENABLE_LSAN}>:-fsanitize=leak>
55+
$<$<BOOL:${ENABLE_MSAN}>:-fsanitize=memory>
56+
$<$<BOOL:${ENABLE_TSAN}>:-fsanitize=thread>
57+
$<$<BOOL:${ENABLE_UBSAN}>:-fsanitize=undefined${_ubsan_options}>
58+
$<$<AND:$<BOOL:${ENABLE_UBSAN}>,$<C_COMPILER_ID:Clang>>:-fsanitize-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/scripts/test/clang_sanitizer_blacklist.txt>
59+
)
60+
endif()

0 commit comments

Comments
 (0)