Skip to content

ASan workflow fix #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions .github/workflows/basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
pool_tracking: ['ON', 'OFF']
shared_library: ['OFF']
os_provider: ['ON']
sanitizers: [{asan: 'OFF', ubsan: 'OFF', tsan: 'OFF'}]
sanitizers: [{asan: OFF, ubsan: OFF, tsan: OFF}]
include:
- os: 'ubuntu-20.04'
build_type: Release
Expand Down Expand Up @@ -53,13 +53,25 @@ jobs:
pool_tracking: 'OFF'
shared_library: 'OFF'
# TSAN is mutually exclusive with other sanitizers
sanitizers: [{asan: 'ON', ubsan: 'ON', tsan: 'OFF'}, {asan: 'OFF', ubsan: 'OFF', tsan: 'ON'}]
sanitizers: {asan: ON, ubsan: ON, tsan: OFF}
- os: 'ubuntu-22.04'
build_type: Debug
compiler: {c: clang, cxx: clang++}
pool_tracking: 'OFF'
shared_library: 'OFF'
sanitizers: {asan: OFF, ubsan: OFF, tsan: ON}
- os: 'ubuntu-22.04'
build_type: Debug
compiler: {c: gcc, cxx: g++}
pool_tracking: 'OFF'
shared_library: 'OFF'
sanitizers: [{asan: 'ON', ubsan: 'ON', tsan: 'OFF'}, {asan: 'OFF', ubsan: 'OFF', tsan: 'ON'}]
sanitizers: {asan: ON, ubsan: ON, tsan: OFF}
- os: 'ubuntu-22.04'
build_type: Debug
compiler: {c: gcc, cxx: g++}
pool_tracking: 'OFF'
shared_library: 'OFF'
sanitizers: {asan: OFF, ubsan: OFF, tsan: ON}
runs-on: ${{matrix.os}}

steps:
Expand Down Expand Up @@ -123,8 +135,6 @@ jobs:
compiler: [{c: cl, cxx: cl}]
pool_tracking: ['ON', 'OFF']
shared_library: ['OFF']
# ASAN is the only available sanitizer on Windows
sanitizers: [{asan: 'OFF'}]
include:
- os: 'windows-2022'
build_type: Release
Expand All @@ -136,18 +146,6 @@ jobs:
compiler: {c: cl, cxx: cl}
pool_tracking: 'ON'
shared_library: 'ON'
- os: 'windows-2022'
build_type: Debug
compiler: {c: clang-cl, cxx: clang-cl}
pool_tracking: 'OFF'
shared_library: 'OFF'
sanitizers: [{asan: 'ON'}]
- os: 'windows-2022'
build_type: Debug
compiler: {c: cl, cxx: cl}
pool_tracking: 'OFF'
shared_library: 'OFF'
sanitizers: [{asan: 'ON'}]

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

Expand All @@ -166,7 +164,6 @@ jobs:
-DUMF_FORMAT_CODE_STYLE=OFF
-DUMF_DEVELOPER_MODE=ON
-DUMF_BUILD_LIBUMF_POOL_DISJOINT=ON
-DUSE_ASAN=${{matrix.sanitizers.asan}}
- name: Build UMF
run: cmake --build ${{env.BUILD_DIR}} --config ${{matrix.build_type}} -j $Env:NUMBER_OF_PROCESSORS
Expand Down
52 changes: 32 additions & 20 deletions cmake/helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,17 @@ endfunction()

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

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

if(${flag} STREQUAL "address")
set(check_name "HAS_ASAN")
Expand All @@ -138,18 +136,32 @@ macro(add_sanitizer_flag flag)
# Check C and CXX compilers for given sanitizer flag.
check_c_compiler_flag("${SANITIZER_FLAG}" "C_${check_name}")
check_cxx_compiler_flag("${SANITIZER_FLAG}" "CXX_${check_name}")
if (${C_${check_name}} OR ${CXX_${check_name}})
# Set appropriate linker flags for building executables.
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${SANITIZER_FLAG} ${SANITIZER_ARGS}")
if (${C_${check_name}})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SANITIZER_FLAG} ${SANITIZER_ARGS}")
endif()
if (${CXX_${check_name}})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SANITIZER_FLAG} ${SANITIZER_ARGS}")
if (NOT ${C_${check_name}} OR NOT ${CXX_${check_name}})
message(FATAL_ERROR "${flag} sanitizer is not supported (either by C or CXX compiler)")
endif()

# Check C and CXX compilers for sanitizer arguments.
if (${SANITIZER_ARGS})
check_c_compiler_flag("${SANITIZER_ARGS}" "C_HAS_SAN_ARGS")
check_cxx_compiler_flag("${SANITIZER_ARGS}" "CXX_HAS_SAN_ARGS")

if (NOT ${C_HAS_SAN_ARGS} OR NOT ${CXX_HAS_SAN_ARGS})
message(FATAL_ERROR "sanitizer argument ${SANITIZER_ARGS} is not supported (either by C or CXX compiler)")
endif()

set(SANITIZER_OPTION "${SANITIZER_FLAG} ${SANITIZER_ARGS}")
else()
message(FATAL_ERROR "${flag} sanitizer is not supported (neither by C nor CXX compiler)")
# No sanitizer argument was set. For now, that's the case for MSVC.
set(SANITIZER_OPTION "${SANITIZER_FLAG}")
endif()

add_compile_options("${SANITIZER_OPTION}")

# Clang/gcc needs the flag added to the linker. The Microsoft LINK linker doesn't recognize
# sanitizer flags and will give a LNK4044 warning.
if (NOT MSVC)
add_link_options("${SANITIZER_OPTION}")
endif()

set(CMAKE_REQUIRED_LIBRARIES ${SAVED_CMAKE_REQUIRED_LIBRARIES})
set(CMAKE_REQUIRED_FLAGS ${SAVED_CMAKE_REQUIRED_FLAGS})
endmacro()