# Copyright 2023-2026, Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause

name: Sanitizers

permissions: read-all

on:
  pull_request:
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  linux-ispc-llvm-asan:
    runs-on: ubuntu-22.04

    steps:
    - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
      with:
        submodules: true

    - name: Install dependencies
      run: |
        .github/workflows/scripts/install-build-deps.sh

    # We will use system gcc until we rebuild LLVM with compiler-rt.
    - name: Check environment
      run: |
        which -a gcc
        cat /proc/cpuinfo

    - name: Build ISPC with ASAN
      run: |
          cmake \
            -B build \
            -DCMAKE_C_COMPILER=gcc \
            -DCMAKE_CXX_COMPILER=g++ \
            -DCMAKE_C_FLAGS="-fsanitize=address -O0 -g3" \
            -DCMAKE_CXX_FLAGS="-fsanitize=address -O0 -g3"
          cmake --build build --target ispc ispc-opt -j4

    # Address sanitizer includes leak sanitizer but we don't want it to report
    # all leaks at the moment because there are quite a lot if them.
    # So, we disable leaks detection to have clean report.
    - name: Build examples with ASAN
      run: |
          ASAN_OPTIONS=detect_leaks=1 cmake --build build --target ispc_cpu_examples -j4

    - name: Build LIT tests with ASAN
      run: |
          ASAN_OPTIONS=detect_leaks=1 cmake --build build --target check-all

    - name: Build runtime tests with ASAN
      run: |
          ASAN_OPTIONS=detect_leaks=1 PATH=$(pwd)/build/bin:$PATH ./scripts/run_tests.py --target=avx2-i32x8 --arch=x86-64
