Skip to content

Commit

Permalink
Add support for GCC static analyzer and ASAN
Browse files Browse the repository at this point in the history
Also enable the static analyzer in the linux build.
  • Loading branch information
imciner2 committed Jan 2, 2025
1 parent 12dbdf0 commit c287bcd
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
39 changes: 37 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,24 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
include:
- os: ubuntu-latest
# Use 24.04 explicitly to get newer GCC version
- os: ubuntu-24.04
compiler: gcc
gcc: 14
extra_c_flags: "-fdiagnostics-format=sarif-file"
coverage: ON
analysis: ON
asan: ON
- os: macos-latest
extra_c_flags: ""
coverage: OFF
analysis: OFF
asan: OFF
- os: windows-latest
extra_c_flags: ""
coverage: OFF
analysis: OFF
asan: OFF

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

Expand All @@ -34,7 +46,15 @@ jobs:
- name: Configure
shell: bash
working-directory: ${{ runner.workspace }}/build
run: cmake --warn-uninitialized -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DQDLDL_UNITTESTS=ON -DCOVERAGE=${{ matrix.coverage }} $GITHUB_WORKSPACE
run: |
cmake -S $GITHUB_WORKSPACE -B ${{ runner.workspace }}/build \
--warn-uninitialized \
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
-DQDLDL_UNITTESTS=ON \
-DQDLDL_DEV_COVERAGE=${{ matrix.coverage }} \
-DQDLDL_DEV_ANALYSIS=${{ matrix.analysis }} \
-DQDLDL_DEV_ASAN=${{ matrix.asan }} \
-DCMAKE_C_FLAGS=${{ matrix.extra_c_flags }}
- name: Build
shell: bash
Expand Down Expand Up @@ -62,6 +82,21 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: '${{ runner.workspace }}/build/coverage.info'

- name: Merge diagnostics
if: ${{ matrix.analysis == 'ON' }}
uses: microsoft/[email protected]
with:
# Command to be sent to SARIF Multitool
command: 'merge ${{ runner.workspace }}/build/*.sarif --recurse true file.sarif --output-directory=${{ runner.workspace }}/build/ --output-file=gcc.sarif'

- name: Upload diagnostics
if: ${{ matrix.analysis == 'ON' }}
uses: github/codeql-action/upload-sarif@v3
with:
# Path to SARIF file relative to the root of the repository
sarif_file: ${{ runner.workspace }}/build/gcc.sarif
category: gcc


test_configs:
strategy:
Expand Down
32 changes: 31 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(QDLDL_VERSION "${QDLDL_VERSION_MAJOR}.${QDLDL_VERSION_MINOR}.${QDLDL_VERSION
project(qdldl VERSION ${QDLDL_VERSION})

include( CMakeDependentOption )
include( CheckCXXCompilerFlag )

option( QDLDL_BUILD_STATIC_LIB "Build the static library" ON )
option( QDLDL_BUILD_SHARED_LIB "Build the shared library" ON )
Expand All @@ -24,6 +25,14 @@ cmake_dependent_option( QDLDL_UNITTESTS
OFF # Default to off
QDLDL_BUILD_STATIC_LIB OFF ) # Force off if the static library isn't built

# Dev options
option( QDLDL_DEV_COVERAGE "Include coverage information in the library" OFF )
option( QDLDL_DEV_ANALYSIS "Run the compiler static analysis checks" OFF )
option( QDLDL_DEV_ASAN "Build with ASAN" OFF )

mark_as_advanced( OSQP_DEV_COVERAGE )
mark_as_advanced( OSQP_DEV_ANALYSIS )

# Set the output folder where your program will be created
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/out)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/out)
Expand Down Expand Up @@ -62,14 +71,35 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON) # -fPIC
# Add compiler options if we are not on windows
if (NOT MSVC)

if (COVERAGE)
if (QDLDL_DEV_COVERAGE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage")

if(FORTRAN)
set(CMAKE_FORTRAN_FLAGS "${CMAKE_FORTRAN_FLAGS} --coverage")
endif(FORTRAN)
endif()

if (QDLDL_DEV_ANALYSIS)
check_cxx_compiler_flag( "-fanalyzer" COMPILER_SUPPORTS_FANALYZER )

if( COMPILER_SUPPORTS_FANALYZER )
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fanalyzer")

message( STATUS "Enabling -fanalyzer static analysis" )
endif()
endif()

if(OSQP_ASAN)
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fsanitize=address -fno-optimize-sibling-calls -fsanitize-address-use-after-scope -fno-omit-frame-pointer" )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -D_GLIBCXX_SANITIZE_VECTOR -fsanitize=address -fno-optimize-sibling-calls -fsanitize-address-use-after-scope -fno-omit-frame-pointer" )

# ASAN shouldn't be used with these options (https://github.com/google/sanitizers/wiki/AddressSanitizer#faq)
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-stack-protector -U_FORTIFY_SOURCE" )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-stack-protector -U_FORTIFY_SOURCE" )

message( STATUS "Enabling ASAN" )
endif()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -g")
endif (NOT MSVC)
Expand Down

0 comments on commit c287bcd

Please sign in to comment.