Skip to content

Commit 7872b23

Browse files
authored
Merge pull request #154 from kswiecicki/asan-workflow-fix
ASan workflow fix
2 parents cd1104f + 1a99a28 commit 7872b23

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

.github/workflows/basic.yml

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
pool_tracking: ['ON', 'OFF']
1818
shared_library: ['OFF']
1919
os_provider: ['ON']
20-
sanitizers: [{asan: 'OFF', ubsan: 'OFF', tsan: 'OFF'}]
20+
sanitizers: [{asan: OFF, ubsan: OFF, tsan: OFF}]
2121
include:
2222
- os: 'ubuntu-20.04'
2323
build_type: Release
@@ -53,13 +53,25 @@ jobs:
5353
pool_tracking: 'OFF'
5454
shared_library: 'OFF'
5555
# TSAN is mutually exclusive with other sanitizers
56-
sanitizers: [{asan: 'ON', ubsan: 'ON', tsan: 'OFF'}, {asan: 'OFF', ubsan: 'OFF', tsan: 'ON'}]
56+
sanitizers: {asan: ON, ubsan: ON, tsan: OFF}
57+
- os: 'ubuntu-22.04'
58+
build_type: Debug
59+
compiler: {c: clang, cxx: clang++}
60+
pool_tracking: 'OFF'
61+
shared_library: 'OFF'
62+
sanitizers: {asan: OFF, ubsan: OFF, tsan: ON}
5763
- os: 'ubuntu-22.04'
5864
build_type: Debug
5965
compiler: {c: gcc, cxx: g++}
6066
pool_tracking: 'OFF'
6167
shared_library: 'OFF'
62-
sanitizers: [{asan: 'ON', ubsan: 'ON', tsan: 'OFF'}, {asan: 'OFF', ubsan: 'OFF', tsan: 'ON'}]
68+
sanitizers: {asan: ON, ubsan: ON, tsan: OFF}
69+
- os: 'ubuntu-22.04'
70+
build_type: Debug
71+
compiler: {c: gcc, cxx: g++}
72+
pool_tracking: 'OFF'
73+
shared_library: 'OFF'
74+
sanitizers: {asan: OFF, ubsan: OFF, tsan: ON}
6375
runs-on: ${{matrix.os}}
6476

6577
steps:
@@ -123,8 +135,6 @@ jobs:
123135
compiler: [{c: cl, cxx: cl}]
124136
pool_tracking: ['ON', 'OFF']
125137
shared_library: ['OFF']
126-
# ASAN is the only available sanitizer on Windows
127-
sanitizers: [{asan: 'OFF'}]
128138
include:
129139
- os: 'windows-2022'
130140
build_type: Release
@@ -136,18 +146,6 @@ jobs:
136146
compiler: {c: cl, cxx: cl}
137147
pool_tracking: 'ON'
138148
shared_library: 'ON'
139-
- os: 'windows-2022'
140-
build_type: Debug
141-
compiler: {c: clang-cl, cxx: clang-cl}
142-
pool_tracking: 'OFF'
143-
shared_library: 'OFF'
144-
sanitizers: [{asan: 'ON'}]
145-
- os: 'windows-2022'
146-
build_type: Debug
147-
compiler: {c: cl, cxx: cl}
148-
pool_tracking: 'OFF'
149-
shared_library: 'OFF'
150-
sanitizers: [{asan: 'ON'}]
151149

152150
runs-on: ${{matrix.os}}
153151

@@ -166,7 +164,6 @@ jobs:
166164
-DUMF_FORMAT_CODE_STYLE=OFF
167165
-DUMF_DEVELOPER_MODE=ON
168166
-DUMF_BUILD_LIBUMF_POOL_DISJOINT=ON
169-
-DUSE_ASAN=${{matrix.sanitizers.asan}}
170167
171168
- name: Build UMF
172169
run: cmake --build ${{env.BUILD_DIR}} --config ${{matrix.build_type}} -j $Env:NUMBER_OF_PROCESSORS

cmake/helpers.cmake

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,17 @@ endfunction()
111111

112112
# Add sanitizer ${flag}, if it is supported, for both C and C++ compiler
113113
macro(add_sanitizer_flag flag)
114-
# Save current 'CMAKE_REQUIRED_LIBRARIES' state and temporarily extend it with
115-
# '-fsanitize=${flag}'. It is required by CMake to check the compiler for
116-
# availability of provided sanitizer ${flag}.
117-
if(WINDOWS)
118-
set(SANITIZER_FLAG "/fsanitize=${flag}")
119-
set(SANITIZER_ARGS "")
120-
else()
121-
set(SANITIZER_FLAG "-fsanitize=${flag}")
114+
set(SANITIZER_FLAG "-fsanitize=${flag}")
115+
if (NOT MSVC)
116+
# Not available on MSVC.
122117
set(SANITIZER_ARGS "-fno-sanitize-recover=all")
123118
endif()
124119

125-
set(SAVED_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
126-
set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES} ${SANITIZER_FLAG}")
120+
# Save current 'SAVED_CMAKE_REQUIRED_FLAGS' state and temporarily extend it
121+
# with '-fsanitize=${flag}'. It is required by CMake to check the compiler
122+
# for availability of provided sanitizer ${flag}.
123+
set(SAVED_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
124+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${SANITIZER_FLAG}")
127125

128126
if(${flag} STREQUAL "address")
129127
set(check_name "HAS_ASAN")
@@ -138,18 +136,32 @@ macro(add_sanitizer_flag flag)
138136
# Check C and CXX compilers for given sanitizer flag.
139137
check_c_compiler_flag("${SANITIZER_FLAG}" "C_${check_name}")
140138
check_cxx_compiler_flag("${SANITIZER_FLAG}" "CXX_${check_name}")
141-
if (${C_${check_name}} OR ${CXX_${check_name}})
142-
# Set appropriate linker flags for building executables.
143-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${SANITIZER_FLAG} ${SANITIZER_ARGS}")
144-
if (${C_${check_name}})
145-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SANITIZER_FLAG} ${SANITIZER_ARGS}")
146-
endif()
147-
if (${CXX_${check_name}})
148-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SANITIZER_FLAG} ${SANITIZER_ARGS}")
139+
if (NOT ${C_${check_name}} OR NOT ${CXX_${check_name}})
140+
message(FATAL_ERROR "${flag} sanitizer is not supported (either by C or CXX compiler)")
141+
endif()
142+
143+
# Check C and CXX compilers for sanitizer arguments.
144+
if (${SANITIZER_ARGS})
145+
check_c_compiler_flag("${SANITIZER_ARGS}" "C_HAS_SAN_ARGS")
146+
check_cxx_compiler_flag("${SANITIZER_ARGS}" "CXX_HAS_SAN_ARGS")
147+
148+
if (NOT ${C_HAS_SAN_ARGS} OR NOT ${CXX_HAS_SAN_ARGS})
149+
message(FATAL_ERROR "sanitizer argument ${SANITIZER_ARGS} is not supported (either by C or CXX compiler)")
149150
endif()
151+
152+
set(SANITIZER_OPTION "${SANITIZER_FLAG} ${SANITIZER_ARGS}")
150153
else()
151-
message(FATAL_ERROR "${flag} sanitizer is not supported (neither by C nor CXX compiler)")
154+
# No sanitizer argument was set. For now, that's the case for MSVC.
155+
set(SANITIZER_OPTION "${SANITIZER_FLAG}")
156+
endif()
157+
158+
add_compile_options("${SANITIZER_OPTION}")
159+
160+
# Clang/gcc needs the flag added to the linker. The Microsoft LINK linker doesn't recognize
161+
# sanitizer flags and will give a LNK4044 warning.
162+
if (NOT MSVC)
163+
add_link_options("${SANITIZER_OPTION}")
152164
endif()
153165

154-
set(CMAKE_REQUIRED_LIBRARIES ${SAVED_CMAKE_REQUIRED_LIBRARIES})
166+
set(CMAKE_REQUIRED_FLAGS ${SAVED_CMAKE_REQUIRED_FLAGS})
155167
endmacro()

0 commit comments

Comments
 (0)