# Sanitizer testing (AddressSanitizer, MemorySanitizer, etc.)
name: Sanitizers
on:
  push:
  pull_request:

permissions:
  contents: read

concurrency:
  cancel-in-progress: true
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}

jobs:
  ubsan-x64:
    name: UndefinedBehaviorSanitizer (x64)
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

    - name: Run UBSan tests
      run: make ubsan V=1

  ubsan-x86:
    name: UndefinedBehaviorSanitizer (x86)
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

    - name: Install 32-bit dependencies
      run: |
        sudo apt-get update
        sudo apt-get install gcc-multilib lib32gcc-11-dev

    - name: Run UBSan tests (32-bit)
      run: |
        make clean
        CC=clang make V=1 usan32

  asan-x64:
    name: AddressSanitizer (x64)
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

    - name: Setup memory mapping
      run: sudo sysctl -w vm.mmap_min_addr=4096

    - name: ASan - basic check
      run: |
        make clean
        CFLAGS=-fsanitize=address LDFLAGS=-fsanitize=address make -j check V=1

    - name: ASan - frame test
      run: |
        make clean
        CFLAGS=-fsanitize=address LDFLAGS=-fsanitize=address make -j -C tests test-frametest V=1

    - name: ASan - fuzzer test
      run: |
        make clean
        CFLAGS=-fsanitize=address LDFLAGS=-fsanitize=address make -j -C tests test-fuzzer V=1

  msan-x64:
    name: MemorySanitizer (x64)
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

    - name: MSan - basic check
      run: |
        make clean
        CC=clang CFLAGS=-fsanitize=memory LDFLAGS=-fsanitize=memory make -j check V=1

    - name: MSan - frame test
      run: |
        make clean
        CC=clang CFLAGS=-fsanitize=memory LDFLAGS=-fsanitize=memory make -j -C tests test-frametest V=1

    - name: MSan - fuzzer test
      run: |
        make clean
        CC=clang CFLAGS=-fsanitize=memory LDFLAGS=-fsanitize=memory make -j -C tests test-fuzzer V=1

  tsan-x64:
    name: ThreadSanitizer (x64)
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

    - name: TSan - CLI test
      run: |
        make clean
        CC=clang CPPFLAGS="-fsanitize=thread" make -j -C tests test-lz4 V=1
