---
name: Sanitizers
on:
  pull_request:
    branches:
      - main
    paths-ignore:
      - 'docs/**'
  push:
    branches:
      - main
    paths-ignore:
      - 'docs/**'
permissions:
  contents: read
jobs:
  # Adapted from https://github.com/beberlei/hdrhistogram-php
  sanitizers:
    name: ${{ matrix.sanitizer }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        sanitizer: ['asan', 'msan']
    env:
      CFLAGS: -g -O0 -fsanitize=${{ matrix.sanitizer == 'asan' && 'address' || 'memory' }} -DZEND_TRACK_ARENA_ALLOC
      LDFLAGS: -fsanitize=${{ matrix.sanitizer == 'asan' && 'address' || 'memory' }}
      CC: clang
      CXX: clang++
      USE_ZEND_ALLOC: 0
      LIBRARY_PATH: ${{ github.workspace }}/php/target/lib:${{ github.workspace }}/watcher/target/lib
      LD_LIBRARY_PATH: ${{ github.workspace }}/php/target/lib
    steps:
      -
        name: Remove local PHP
        run: sudo apt-get remove --purge --autoremove 'php*' 'libmemcached*'
      -
        uses: actions/checkout@v4
      -
        uses: actions/setup-go@v5
        with:
          go-version: '1.22'
          cache-dependency-path: |
            go.sum 
            caddy/go.sum
      -
        name: Determine PHP version
        id: determine-php-version
        run: |
          curl -fsSL 'https://www.php.net/releases/index.php?json&max=1&version=8.3' -o version.json
          echo version="$(jq -r 'keys[0]' version.json)" >> "$GITHUB_OUTPUT"
          echo archive="$(jq -r '.[] .source[] | select(.filename |endswith(".xz")) | "https://www.php.net/distributions/" + .filename' version.json)" >> "$GITHUB_OUTPUT"
      -
        name: Cache PHP
        id: cache-php
        uses: actions/cache@v4
        with:
          path: php/target
          key: php-sanitizers-${{ matrix.sanitizer }}-${{ runner.arch }}-${{ steps.determine-php-version.outputs.version }}
      -
        if: steps.cache-php.outputs.cache-hit != 'true'
        name: Compile PHP
        run: |
          mkdir php/
          curl -fsSL "${{ steps.determine-php-version.outputs.archive }}" | tar -Jx -C php --strip-components=1
          cd php/
          ./configure \
            CFLAGS="$CFLAGS" \
            LDFLAGS="$LDFLAGS" \
            --enable-debug \
            --enable-embed \
            --enable-zts \
            --enable-option-checking=fatal \
            --disable-zend-signals \
            --without-sqlite3 \
            --without-pdo-sqlite \
            --without-libxml \
            --disable-dom \
            --disable-simplexml \
            --disable-xml \
            --disable-xmlreader \
            --disable-xmlwriter \
            --without-pcre-jit \
            --disable-opcache-jit \
            --disable-cli \
            --disable-cgi \
            --disable-phpdbg \
            --without-pear \
            --disable-mbregex \
            --enable-werror \
            ${{ matrix.sanitizer == 'msan' && '--enable-memory-sanitizer' || '' }} \
            --prefix="$(pwd)/target/"
          make -j"$(getconf _NPROCESSORS_ONLN)"
          make install
      -
        name: Add PHP to the PATH
        run: echo "$(pwd)/php/target/bin" >> "$GITHUB_PATH"
      -
        name: Install e-dant/watcher
        uses: ./.github/actions/watcher
      -
        name: Set Set CGO flags
        run: |
          {
            echo "CGO_CFLAGS=$CFLAGS -I${PWD}/watcher/target/include $(php-config --includes)"
            echo "CGO_LDFLAGS=$LDFLAGS $(php-config --ldflags) $(php-config --libs)"
          } >> "$GITHUB_ENV"
      -
        name: Compile tests
        run: go test  ${{ matrix.sanitizer == 'msan' && '-tags=nowatcher' || '' }} -${{ matrix.sanitizer }} -v -x -c
      -
        name: Run tests
        run: ./frankenphp.test -test.v