Skip to content

Commit 50db26b

Browse files
author
lei.liu
committed
feat: add asan and ubsan support to cmake
1 parent cb44bdc commit 50db26b

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

.github/lsan-suppressions.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
#
18+
# Add specific leak suppressions here if needed
19+
# Format:
20+
# leak:SymbolName
21+
# leak:source_file.cc

.github/ubsan-suppressions.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
#
18+
# Add specific undefined suppressions here if needed
19+
# Format:
20+
# signed-integer-overflow:source_file.cc

.github/workflows/asan_test.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
name: Address / Undefined Sanitizer Tests
19+
20+
on:
21+
push:
22+
branches:
23+
- '**'
24+
- '!dependabot/**'
25+
tags:
26+
- '**'
27+
pull_request:
28+
29+
concurrency:
30+
group: ${{ github.repository }}-${{ github.head_ref || github.sha }}-${{ github.workflow }}
31+
cancel-in-progress: true
32+
33+
jobs:
34+
asan-test:
35+
name: "ASAN & UBSAN Tests"
36+
runs-on: ubuntu-24.04
37+
strategy:
38+
fail-fast: false
39+
steps:
40+
- name: Checkout iceberg-cpp
41+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
42+
- name: Configure and Build with ASAN & UBSAN
43+
env:
44+
CC: ${{ matrix.cc }}
45+
CXX: ${{ matrix.cxx }}
46+
run: |
47+
mkdir build && cd build
48+
cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DICEBERG_BUILD_ENABLE_ASAN=ON -DICEBERG_BUILD_ENABLE_UBSAN=ON
49+
cmake --build . --verbose
50+
- name: Run Tests
51+
working-directory: build
52+
env:
53+
ASAN_OPTIONS: log_path=out.log:detect_leaks=1:symbolize=1:strict_string_checks=1:halt_on_error=0:detect_container_overflow=0
54+
LSAN_OPTIONS: suppressions=${{ github.workspace }}/.github/lsan-suppressions.txt
55+
UBSAN_OPTIONS: log_path=out.log:print_stacktrace=1:suppressions=${{ github.workspace }}/.github/ubsan-suppressions.txt
56+
run: |
57+
ctest --output-on-failure

CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ option(ICEBERG_BUILD_STATIC "Build static library" ON)
3737
option(ICEBERG_BUILD_SHARED "Build shared library" OFF)
3838
option(ICEBERG_BUILD_TESTS "Build tests" ON)
3939
option(ICEBERG_BUILD_BUNDLE "Build the battery included library" ON)
40+
option(ICEBERG_BUILD_ENABLE_ASAN "Enable Address Sanitizer" OFF)
41+
option(ICEBERG_BUILD_ENABLE_UBSAN "Enable Undefined Behavior sanitizer" OFF)
4042

4143
include(GNUInstallDirs)
4244
include(FetchContent)
@@ -58,6 +60,27 @@ include(IcebergBuildUtils)
5860
include(IcebergThirdpartyToolchain)
5961
include(GenerateExportHeader)
6062

63+
if(ICEBERG_BUILD_ENABLE_ASAN)
64+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
65+
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
66+
add_link_options(-fsanitize=address)
67+
message(STATUS "Address Sanitizer enabled")
68+
else()
69+
message(WARNING "Address Sanitizer is only supported for GCC and Clang compilers")
70+
endif()
71+
endif()
72+
73+
if(ICEBERG_BUILD_ENABLE_UBSAN)
74+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
75+
add_compile_options(-fsanitize=undefined -fno-omit-frame-pointer)
76+
add_link_options(-fsanitize=undefined)
77+
message(STATUS "Undefined Behavior Sanitizer enabled")
78+
else()
79+
message(WARNING "Undefined Behavior Sanitizer is only supported for GCC and Clang compilers"
80+
)
81+
endif()
82+
endif()
83+
6184
add_subdirectory(src)
6285

6386
if(ICEBERG_BUILD_TESTS)

0 commit comments

Comments
 (0)