name: Fuzz lou_translateString

on:
  workflow_dispatch:
  schedule:
    - cron: "0 4 * * 1"

permissions:  # added using https://github.com/step-security/secure-workflows
  contents: read

jobs:
  build:
    name: Build with ${{ matrix.ucs }}
    # Do not run this job on any forked repos
    if: github.repository == 'liblouis/liblouis'
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        ucs: [ucs2]
        # ucs4
    steps:
    - name: Harden Runner
      uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
      with:
        egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs

    - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
    - name: Install dependencies
      run: sudo apt-get update -qq && sudo apt-get install -y libyaml-dev texinfo texlive clang llvm
    - name: Autogen
      run: ./autogen.sh
    - name: Configure
      run: CC=clang CXX=clang++
           CFLAGS="-fsanitize=address,undefined -fstack-protector-strong -g -Og -fno-omit-frame-pointer"
           CXXFLAGS="-fsanitize=address,undefined -fstack-protector-strong -g -Og -fno-omit-frame-pointer"
           LDFLAGS="-fsanitize=address,undefined -lubsan"
           ./configure --with-fuzzer
      if: matrix.ucs == 'ucs2'
    - name: Configure with ucs4
      run: CC=clang CXX=clang++
           CFLAGS="-fsanitize=address,undefined -fstack-protector-strong -g -Og -fno-omit-frame-pointer"
           CXXFLAGS="-fsanitize=address,undefined -fstack-protector-strong -g -Og -fno-omit-frame-pointer"
           LDFLAGS="-fsanitize=address,undefined -lubsan"
           ./configure --with-fuzzer --enable-ucs4
      if: matrix.ucs == 'ucs4'
    - name: Compile project
      run: make -j
    - uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
      with:
        path: /home/runner/work/liblouis/liblouis
        key: build-${{ matrix.ucs }}-${{ github.sha }}

  run:
    name: Run with ${{ matrix.ucs }} for ${{ matrix.table }}
    needs: build
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        ucs: [ucs2]
        # ucs4
        table:
          - afr-za-g1.ctb
          - afr-za-g2.ctb
          - akk-borger.utb
          - akk.utb
          - ancient-languages-borger.utb
          - ancient-languages-us.utb
          - ar-ar-comp8.utb
          - ar-ar-g1.utb
          - ar-ar-g2.ctb
          - as-in-g1.utb
          - aw-in-g1.utb
          - ba.utb
          - be-in-g1.utb
          - bel-comp.utb
          - bel-detailed.utb
          - bel.utb
          - bg.ctb
          - bg.utb
          - bh.ctb
          - bo.ctb
          - boxes.ctb
          - br-in-g1.utb
          - ca-g1.ctb
          - chr-us-g1.ctb
          - ckb-g1.ctb
          - cop-eg-comp8.utb
          - cop.utb
          - cs-comp8.utb
          - cs-g1.ctb
          - cuneiform-transliterated-compact.utb
          - cuneiform-transliterated.utb
          - cy-cy-g1.utb
          - cy-cy-g2.ctb
          - da-dk-g08_1993.ctb
          - da-dk-g08.ctb
          - da-dk-g16_1993.ctb
          - da-dk-g16.ctb
          - da-dk-g16-lit_1993.ctb
          - da-dk-g18_1993.ctb
          - da-dk-g18.ctb
          - da-dk-g26_1993.ctb
          - da-dk-g26.ctb
          - da-dk-g26l_1993.ctb
          - da-dk-g26-lit_1993.ctb
          - da-dk-g26l-lit_1993.ctb
          - da-dk-g28_1993.ctb
          - da-dk-g28.ctb
          - da-dk-g28l_1993.ctb
          - de-chess.ctb
          - de-comp6.utb
          - de-de-comp8.ctb
          - de-g0-detailed.utb
          - de-g0.utb
          - de-g1.ctb
          - de-g1-detailed.ctb
          - de-g2.ctb
          - de-g2-detailed.ctb
          - dra.ctb
          - el.ctb
          - en_CA.ctb
          - en-chess.ctb
          - en-g3.ctb
          - en-gb-comp8.ctb
          - en-gb-g1.utb
          - en-GB-g2.ctb
          - en-in-g1.ctb
          - en-nabcc.utb
          - en-ueb-g1.ctb
          - en-ueb-g2.ctb
          - en-ueb-math.ctb
          - en-us-comp6.ctb
          - en-us-comp8.ctb
          - en-us-comp8-ext.utb
          - en-us-g1.ctb
          - en-us-g2.ctb
          - en-us-interline.ctb
          - en-us-mathtext.ctb
          - eo-g1.ctb
          - eo-g1-x-system.ctb
          - Es-Es-G0.utb
          - es-g1.ctb
          - es-g2.ctb
          - et.ctb
          - et-6dot.utb
          - et-g0.utb
          - ethio-g1.ctb
          - fa-ir-comp8.ctb
          - fa-ir-g1.utb
          - fi-fi-8dot.ctb
          - fil-g2.ctb
          - fi.utb
          - fr-bfu-comp6.utb
          - fr-bfu-comp8.utb
          - fr-bfu-g2.ctb
          - ga-g1.utb
          - ga-g2.ctb
          - gd.ctb
          - gon.ctb
          - grc-international-en-composed.utb
          - grc-international-en.utb
          - grc-international-es.utb
          - gu-in-g1.utb
          - haw-us-g1.ctb
          - hbo-cantillated.utb
          - hbo-slim.utb
          - hbo.utb
          - he-IL-comp8.utb
          - he-IL.utb
          - hi-in-g1.utb
          - hr-comp8.utb
          - hr-g1.ctb
          - hu-hu-comp8.ctb
          - hu-hu-g1.ctb
          - hu-hu-g2.ctb
          - hy.ctb
          - IPA.utb
          - is.ctb
          - it-it-comp6.utb
          - it-it-comp8.utb
          - iu-ca-g1.ctb
          - ja-kantenji-ucs2.utb
          - ja-kantenji.utb
          - ja-rokutenkanji.utb
          - ka-in-g1.utb
          - ka.utb
          - kh-in-g1.utb
          - kk.utb
          - km-g1.utb
          - ko-2006-g1.ctb
          - ko-2006-g2.ctb
          - ko-g1.ctb
          - ko-g2.ctb
          - kok.ctb
          - kru.ctb
          - ks-in-g1.utb
          - lg-ug-g1.utb
          - lo-g1.utb
          - lt-6dot.utb
          - lt-8dot.utb
          - lt.ctb
          - Lv-Lv-g1.utb
          - mao-nz-g1.ctb
          - mk-g1.utb
          - ml-in-g1.utb
          - mn-in-g1.utb
          - mn-MN-g1.utb
          - mn-MN-g2.ctb
          - mr-in-g1.utb
          - ms-my-g2.ctb
          - mt.ctb
          - mun.ctb
          - mwr.ctb
          - my-g1.utb
          - my-g2.ctb
          - ne.ctb
          - nl-comp8.utb
          - nl-NL-g0.utb
          - no-no-8dot-fallback-6dot-g0.utb
          - no-no-8dot.utb
          - no-no-comp8.ctb
          - no-no-g0.utb
          - no-no-g1.ctb
          - no-no-g2.ctb
          - no-no-g3.ctb
          - no-no-generic.ctb
          - np-in-g1.utb
          - nso-za-g1.utb
          - nso-za-g2.ctb
          - ny-mw.utb
          - or-in-g1.utb
          - pi.ctb
          - pl-pl-comp8.ctb
          - Pl-Pl-g1.utb
          - pt-comp6.utb
          - pt-pt-comp8.ctb
          - pt-pt-g1.utb
          - pt-pt-g2.ctb
          - pu-in-g1.utb
          - ro.ctb
          - ro-g0.utb
          - ru-comp6.utb
          - ru-comp8.utb
          - ru-litbrl.ctb
          - ru-litbrl-detailed.utb
          - ru-ru-g1.ctb
          - rw-rw-g1.utb
          - sah.utb
          - sa-in-g1.utb
          - se-se.ctb
          - si-in-g1.utb
          - sin.utb
          - sk-g1.ctb
          - sk-sk-g1.utb
          - sk-sk.utb
          - sl-si-comp8.ctb
          - sl-si-g1.utb
          - sot-za-g1.ctb
          - sot-za-g2.ctb
          - sr-Cyrl.ctb
          - sr-g1.ctb
          - sv-1989.ctb
          - sv-1996.ctb
          - sv-g0.utb
          - sv-g1.ctb
          - sv-g2.ctb
          - sw-ke-g1-2.ctb
          - sw-ke-g1-3.ctb
          - sw-ke-g1-4.ctb
          - sw-ke-g1-5.ctb
          - sw-ke-g1.utb
          - sw-ke-g2.ctb
          - syc.utb
          - ta.ctb
          - ta-ta-g1.ctb
          - te-in-g1.utb
          - th-comp8-backward.utb
          - th-g0.utb
          - th-g1.utb
          - th-g2.ctb
          - tr.ctb
          - tr-g1.ctb
          - tr-g2.ctb
          - tsn-za-g1.ctb
          - tsn-za-g2.ctb
          - tt.utb
          - uga.utb
          - uk-comp.utb
          - uk-detailed.utb
          - uk.utb
          - unicode-braille.utb
          - ur-pk-g1.utb
          - ur-pk-g2.ctb
          - uz-g1.utb
          - ve-za-g1.utb
          - ve-za-g2.ctb
          - vi-cb8.utb
          - vi-saigon-g1.ctb
          - vi-vn-g0.utb
          - vi-vn-g1.ctb
          - vi-vn-g2.ctb
          - xh-za-g1.utb
          - xh-za-g2.ctb
          - yi.utb
          - zh-chn.ctb
          - zhcn-cbs.ctb
          - zhcn-g1.ctb
          - zhcn-g2.ctb
          - zh-hk.ctb
          - zh-tw.ctb
          - zu-za-g1.utb
          - zu-za-g2.ctb
    steps:
    - name: Harden Runner
      uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
      with:
        egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs

    - uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
      with:
        path: /home/runner/work/liblouis/liblouis
        key: build-${{ matrix.ucs }}-${{ github.sha }}
    - name: Fuzz function lou_translateString()
      run: mkdir tests/fuzzing/CORPUS ; FUZZ_TABLE=tables/${{ matrix.table }} tests/fuzzing/fuzz_translate -seed=1234 -runs=100000 -max_len=10000 tests/fuzzing/CORPUS
    - name: Store the crash POC
      if: ${{ failure() }}
      uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
      with:
        name: crash-${{ matrix.ucs }}-${{ matrix.table }}.1
        path: crash-* timeout-*
    - name: Fuzz function lou_translateString() with language-specific input
      run: cp tables/${{ matrix.table }} tests/fuzzing/CORPUS/ ; FUZZ_TABLE=tables/${{ matrix.table }} tests/fuzzing/fuzz_translate -seed=1234 -runs=30000 -max_len=10000 tests/fuzzing/CORPUS
    - name: Store the crash POC
      if: ${{ failure() }}
      uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
      with:
        name: crash-${{ matrix.ucs }}-${{ matrix.table }}.2
        path: crash-* timeout-*
