From e267e3903b08b271adb9a57e6d2ad922b46ccd93 Mon Sep 17 00:00:00 2001 From: fcostaoliveira Date: Mon, 17 Nov 2025 09:17:26 +0000 Subject: [PATCH] Add UndefinedBehaviorSanitizer (UBSan) support. --- .github/workflows/ubsan.yml | 90 +++++++++++++++++++++++++++++++++++++ README.md | 22 +++++++++ configure.ac | 10 +++++ 3 files changed, 122 insertions(+) create mode 100644 .github/workflows/ubsan.yml diff --git a/.github/workflows/ubsan.yml b/.github/workflows/ubsan.yml new file mode 100644 index 0000000..0795c55 --- /dev/null +++ b/.github/workflows/ubsan.yml @@ -0,0 +1,90 @@ +name: UBSan (UndefinedBehaviorSanitizer) + +# Undefined behavior detection using UndefinedBehaviorSanitizer +# This workflow builds memtier_benchmark with UBSan enabled and runs +# the full test suite to detect undefined behavior issues. + +on: [push, pull_request] + +jobs: + test-with-ubsan: + runs-on: ubuntu-latest + name: Undefined behavior detection (UBSan) + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install build dependencies + run: | + sudo apt-get -qq update + sudo apt-get install -y \ + build-essential \ + autoconf \ + automake \ + pkg-config \ + libevent-dev \ + zlib1g-dev \ + libssl-dev + + - name: Build with UBSan + run: | + autoreconf -ivf + ./configure --enable-ubsan + make -j + + - name: Setup Python + uses: actions/setup-python@v2 + with: + python-version: '3.10' + architecture: x64 + + - name: Install Python test dependencies + run: pip install -r ./tests/test_requirements.txt + + - name: Install Redis + run: | + curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg + echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list + sudo apt-get -qq update + sudo apt-get install redis + sudo service redis-server stop + + - name: Increase connection limit + run: | + sudo sysctl -w net.ipv4.tcp_fin_timeout=10 + sudo sysctl -w net.ipv4.tcp_tw_reuse=1 + ulimit -n 40960 + + - name: Generate TLS test certificates + run: ./tests/gen-test-certs.sh + + - name: Test OSS TCP with UBSan + timeout-minutes: 10 + run: | + UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 ./tests/run_tests.sh + + - name: Test OSS TCP TLS with UBSan + timeout-minutes: 10 + run: | + UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 TLS=1 ./tests/run_tests.sh + + - name: Test OSS TCP TLS v1.2 with UBSan + timeout-minutes: 10 + run: | + UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 TLS_PROTOCOLS='TLSv1.2' TLS=1 ./tests/run_tests.sh + + - name: Test OSS TCP TLS v1.3 with UBSan + timeout-minutes: 10 + run: | + UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 TLS_PROTOCOLS='TLSv1.3' TLS=1 ./tests/run_tests.sh + + - name: Test OSS-CLUSTER TCP with UBSan + timeout-minutes: 10 + run: | + UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 OSS_STANDALONE=0 OSS_CLUSTER=1 ./tests/run_tests.sh + + - name: Test OSS-CLUSTER TCP TLS with UBSan + timeout-minutes: 10 + run: | + UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 OSS_STANDALONE=0 OSS_CLUSTER=1 TLS=1 ./tests/run_tests.sh + diff --git a/README.md b/README.md index 7cd83d5..83497d6 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,28 @@ To verify ASAN is enabled: $ ldd ./memtier_benchmark | grep asan + +**Undefined behavior detection with UBSan** + + +memtier_benchmark supports building with UndefinedBehaviorSanitizer (UBSan) to detect undefined behavior such as integer overflows, null pointer dereferences, and alignment issues. + +To build with UBSan enabled: + + $ ./configure --enable-ubsan + $ make + +To run tests with undefined behavior detection: + + $ UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 ./tests/run_tests.sh + +UBSan can be combined with ASAN for comprehensive testing: + + $ ./configure --enable-sanitizers --enable-ubsan + $ make + +**Note:** UBSan can be used together with ASAN/LSAN, but not with ThreadSanitizer (TSAN). + ## Using Docker Use available images on Docker Hub: diff --git a/configure.ac b/configure.ac index b75efcd..fca4656 100755 --- a/configure.ac +++ b/configure.ac @@ -79,6 +79,16 @@ AS_IF([test "x$enable_sanitizers" = "xyes"], [ LDFLAGS="$LDFLAGS -fsanitize=address -fsanitize=leak" ], []) +# UndefinedBehaviorSanitizer (UBSan) is optional and can be combined with ASAN. +AC_ARG_ENABLE([ubsan], + [AS_HELP_STRING([--enable-ubsan], + [Enable UndefinedBehaviorSanitizer for undefined behavior detection])]) +AS_IF([test "x$enable_ubsan" = "xyes"], [ + AC_MSG_NOTICE([Enabling UndefinedBehaviorSanitizer]) + CXXFLAGS="$CXXFLAGS -fsanitize=undefined -fno-omit-frame-pointer" + LDFLAGS="$LDFLAGS -fsanitize=undefined" + ], []) + # clock_gettime requires -lrt on old glibc only. AC_SEARCH_LIBS([clock_gettime], [rt], , AC_MSG_ERROR([rt is required libevent.]))