diff --git a/.github/workflows/ubsan_test.yml b/.github/workflows/ubsan_test.yml new file mode 100644 index 0000000000..a754f41d20 --- /dev/null +++ b/.github/workflows/ubsan_test.yml @@ -0,0 +1,63 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +name: Undefined behavior sanitizer tests + +on: + pull_request: + paths-ignore: + - 'site/**' + - 'conan/**' + branches: + - main + +concurrency: + group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.event.number || github.sha }} + cancel-in-progress: true + +jobs: + asan-test: + name: "ASAN with ${{ matrix.compiler }} on Ubuntu" + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + compiler: [gcc, clang] + include: + - compiler: gcc + cc: gcc + cxx: g++ + - compiler: clang + cc: clang + cxx: clang++ + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Configure and Build with UBSAN + env: + CC: ${{ matrix.cc }} + CXX: ${{ matrix.cxx }} + run: | + mkdir build && cd build + cmake .. -DCMAKE_BUILD_TYPE=Debug -DENABLE_UBSAN=ON -DBUILD_JAVA=OFF + make + - name: Run Tests + working-directory: build + env: + UBSAN_OPTIONS: print_stacktrace=1 + run: | + ctest --output-on-failure diff --git a/CMakeLists.txt b/CMakeLists.txt index eadc13eae6..b9573dad67 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,6 +89,10 @@ option(ORC_ENABLE_CLANG_TOOLS "Enable Clang tools" ON) +option(ENABLE_UBSAN + "Enable Undefined Behavior Sanitizer" + OFF) + # Make sure that a build type is selected if (NOT CMAKE_BUILD_TYPE) message(STATUS "No build type selected, default to ReleaseWithDebugInfo") @@ -171,6 +175,16 @@ if (ENABLE_ASAN) endif() endif() +# Configure Undefined Behavior Sanitizer if enabled +if (ENABLE_UBSAN) + if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-sanitize=alignment,vptr,function -fno-sanitize-recover=all") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-sanitize=alignment,vptr,function -fno-sanitize-recover=all") + message(STATUS "Undefined Behavior Sanitizer enabled") + else() + message(WARNING "Undefined Behavior Sanitizer is only supported for GCC and Clang compilers") + endif() + enable_testing() INCLUDE(GNUInstallDirs) # Put it before ThirdpartyToolchain to make CMAKE_INSTALL_LIBDIR available.