Skip to content

Commit b386060

Browse files
Merge pull request #9191 from ThomasWaldmann/backport-9139-to-1.4-maint
[1.4-maint] Backport: CI dynamic code analysis (#9139)
2 parents 2a263ec + ea6450b commit b386060

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

.github/workflows/ci.yml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,76 @@ jobs:
4343
- uses: chartboost/ruff-action@v1
4444

4545

46+
asan_ubsan:
47+
48+
runs-on: ubuntu-24.04
49+
timeout-minutes: 25
50+
needs: [lint]
51+
52+
steps:
53+
- uses: actions/checkout@v4
54+
with:
55+
# Just fetching one commit is not enough for setuptools-scm, so we fetch all.
56+
fetch-depth: 0
57+
fetch-tags: true
58+
59+
- name: Set up Python
60+
uses: actions/setup-python@v5
61+
with:
62+
python-version: '3.12'
63+
64+
- name: Install system packages
65+
run: |
66+
sudo apt-get update
67+
sudo apt-get install -y pkg-config build-essential
68+
sudo apt-get install -y libssl-dev libacl1-dev libxxhash-dev liblz4-dev libzstd-dev
69+
70+
- name: Install Python dependencies
71+
run: |
72+
python -m pip install --upgrade pip
73+
pip install -r requirements.d/development.txt
74+
75+
- name: Build Borg with ASan/UBSan
76+
# Build the C/Cython extensions with AddressSanitizer and UndefinedBehaviorSanitizer enabled.
77+
# How this works:
78+
# - The -fsanitize=address,undefined flags inject runtime checks into our native code. If a bug is hit
79+
# (e.g., buffer overflow, use-after-free, out-of-bounds, or undefined behavior), the sanitizer prints
80+
# a detailed error report to stderr, including a stack trace, and forces the process to exit with
81+
# non-zero status. In CI, this will fail the step/job so you will notice.
82+
# - ASAN_OPTIONS/UBSAN_OPTIONS configure the sanitizers' runtime behavior (see below for meanings).
83+
env:
84+
CFLAGS: "-O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined"
85+
CXXFLAGS: "-O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined"
86+
LDFLAGS: "-fsanitize=address,undefined"
87+
# ASAN_OPTIONS controls AddressSanitizer runtime tweaks:
88+
# - detect_leaks=0: Disable LeakSanitizer to avoid false positives with CPython/pymalloc in short-lived tests.
89+
# - strict_string_checks=1: Make invalid string operations (e.g., over-reads) more likely to be detected.
90+
# - check_initialization_order=1: Catch uses that depend on static initialization order (C++).
91+
# - detect_stack_use_after_return=1: Detect stack-use-after-return via stack poisoning (may increase overhead).
92+
ASAN_OPTIONS: "detect_leaks=0:strict_string_checks=1:check_initialization_order=1:detect_stack_use_after_return=1"
93+
# UBSAN_OPTIONS controls UndefinedBehaviorSanitizer runtime:
94+
# - print_stacktrace=1: Include a stack trace for UB reports to ease debugging.
95+
# Note: UBSan is recoverable by default (process may continue after reporting). If you want CI to
96+
# abort immediately and fail on the first UB, add `halt_on_error=1` (e.g., UBSAN_OPTIONS="print_stacktrace=1:halt_on_error=1").
97+
UBSAN_OPTIONS: "print_stacktrace=1"
98+
# PYTHONDEVMODE enables additional Python runtime checks and warnings.
99+
PYTHONDEVMODE: "1"
100+
run: pip install -e .
101+
102+
- name: Run tests under sanitizers
103+
env:
104+
ASAN_OPTIONS: "detect_leaks=0:strict_string_checks=1:check_initialization_order=1:detect_stack_use_after_return=1"
105+
UBSAN_OPTIONS: "print_stacktrace=1"
106+
PYTHONDEVMODE: "1"
107+
# Ensure the ASan runtime is loaded first to avoid "ASan runtime does not come first" warnings.
108+
# We discover libasan/libubsan paths via gcc and preload them for the Python test process.
109+
# the remote tests are slow and likely won't find anything useful
110+
run: |
111+
set -euo pipefail
112+
export LD_PRELOAD="$(gcc -print-file-name=libasan.so):$(gcc -print-file-name=libubsan.so)"
113+
echo "Using LD_PRELOAD=$LD_PRELOAD"
114+
pytest -v --benchmark-skip -k "not remote"
115+
46116
posix_tests:
47117

48118
needs: [lint]

src/borg/testsuite/patterns.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ def test_exclude_patterns_from_file(tmpdir, lines, expected):
275275

276276
def evaluate(filename):
277277
patterns = []
278-
load_exclude_file(open(filename), patterns)
278+
with open(filename) as fh:
279+
load_exclude_file(fh, patterns)
279280
matcher = PatternMatcher(fallback=True)
280281
matcher.add_inclexcl(patterns)
281282
return [path for path in files if matcher.match(path)]
@@ -306,7 +307,8 @@ def test_load_patterns_from_file(tmpdir, lines, expected_roots, expected_numpatt
306307
def evaluate(filename):
307308
roots = []
308309
inclexclpatterns = []
309-
load_pattern_file(open(filename), roots, inclexclpatterns)
310+
with open(filename) as fh:
311+
load_pattern_file(fh, roots, inclexclpatterns)
310312
return roots, len(inclexclpatterns)
311313
patternfile = tmpdir.join("patterns.txt")
312314

@@ -356,7 +358,8 @@ def test_load_invalid_patterns_from_file(tmpdir, lines):
356358
with pytest.raises(argparse.ArgumentTypeError):
357359
roots = []
358360
inclexclpatterns = []
359-
load_pattern_file(open(filename), roots, inclexclpatterns)
361+
with open(filename) as fh:
362+
load_pattern_file(fh, roots, inclexclpatterns)
360363

361364

362365
@pytest.mark.parametrize("lines, expected", [
@@ -400,7 +403,8 @@ def evaluate(filename):
400403
matcher = PatternMatcher(fallback=True)
401404
roots = []
402405
inclexclpatterns = []
403-
load_pattern_file(open(filename), roots, inclexclpatterns)
406+
with open(filename) as fh:
407+
load_pattern_file(fh, roots, inclexclpatterns)
404408
matcher.add_inclexcl(inclexclpatterns)
405409
return [path for path in files if matcher.match(path)]
406410

0 commit comments

Comments
 (0)