name: Fuzz # go test runs only each fuzz target's seeds; this fuzzes every target for # five minutes each night, starting from the inputs its earlier nights found # (its corpus, which a cache keeps). The list is read from the tests, so a # new target is fuzzed without editing this file. The targets that fail go in # one open issue, which closes once a night's targets all pass: a failing # input is kept in its target's corpus, so it fails again each night until # the code is fixed. The fuzz jobs have only read access; the issue job, # which can write issues, runs no code of the repo's. on: schedule: - cron: "17 3 * * *" workflow_dispatch: permissions: contents: read concurrency: group: fuzz cancel-in-progress: false jobs: targets: runs-on: ubuntu-latest timeout-minutes: 5 outputs: matrix: ${{ steps.list.outputs.matrix }} count: ${{ steps.list.outputs.count }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false # Each target as {"pkg","name"}, the proxy's first. A name is a # target's alone, as its artifacts and its line in the issue are # named by it. - id: list run: | set -euo pipefail lines=$(grep -rHo --include='*_test.go' '^func Fuzz[A-Za-z0-9_]*' internal \ | sed -E 's#^(.*)/[^/]*:func (Fuzz[A-Za-z0-9_]*)$#\1 \2#' \ | sort -u | awk '{print ($1 ~ /^internal\/proxy$/ ? 0 : 1), $0}' | sort -k1,1n -k2 | cut -d' ' -f2-) test -n "$lines" dup=$(printf '%s\n' "$lines" | cut -d' ' -f2 | sort | uniq -d) if [[ -n $dup ]]; then echo "fuzz targets of one name in two packages: $dup" >&2 exit 1 fi matrix=$(printf '%s\n' "$lines" | awk 'BEGIN{printf "["} {if (NR>1) printf ","; printf "{\"pkg\":\"./%s\",\"name\":\"%s\"}", $1, $2} END{printf "]"}') echo "matrix=$matrix" >> "$GITHUB_OUTPUT" echo "count=$(printf '%s\n' "$lines" | wc -l)" >> "$GITHUB_OUTPUT" fuzz: needs: targets runs-on: ubuntu-latest timeout-minutes: 20 strategy: fail-fast: false max-parallel: 8 matrix: target: ${{ fromJSON(needs.targets.outputs.matrix) }} name: ${{ matrix.target.name }} env: NAME: ${{ matrix.target.name }} PKG: ${{ matrix.target.pkg }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false # Only Go, cached apart from the other workflows' tools. - uses: jdx/mise-action@924e7c586e33d7d4bae9c14e26c5d22e2beb6e78 # v5.3.0 with: version: 2026.11.14 install_args: go cache_key_prefix: mise-v1-fuzz # go test keeps a target's corpus in GOCACHE's fuzz folder, under the # package's import path and the target's name. - id: corpus run: | set -euo pipefail dir="$(go env GOCACHE)/fuzz/$(go list "$PKG")/$NAME" echo "dir=$dir" >> "$GITHUB_OUTPUT" echo "key=fuzz-${PKG#./}-$NAME" >> "$GITHUB_OUTPUT" - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: ${{ steps.corpus.outputs.dir }} key: ${{ steps.corpus.outputs.key }}-${{ github.run_id }}-${{ github.run_attempt }} restore-keys: ${{ steps.corpus.outputs.key }}- # Each new input is minimized in at most 1,000 runs: by time, a # target whose runs write files, such as FuzzUnpack, spends its five # minutes minimizing. - id: fuzz run: go test -run '^$' -fuzz "^${NAME}\$" -fuzztime 5m -fuzzminimizetime 1000x "$PKG" # A failing input lands in the package's testdata/fuzz//; # committed there, go test runs it as a seed. It goes in the corpus # too, so the next night starts with it and fails until it's fixed. - if: failure() && steps.fuzz.outcome == 'failure' env: DIR: ${{ steps.corpus.outputs.dir }} run: | set -euo pipefail shopt -s nullglob mkdir -p "$DIR" for f in "$PKG/testdata/fuzz/$NAME"/*; do cp "$f" "$DIR/" done - if: failure() && steps.fuzz.outcome == 'failure' uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: crash-${{ matrix.target.name }} path: ${{ matrix.target.pkg }}/testdata/fuzz/${{ matrix.target.name }}/ if-no-files-found: ignore # Saved whatever the night found, a failure included. - if: always() && steps.corpus.outputs.dir != '' uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: ${{ steps.corpus.outputs.dir }} key: ${{ steps.corpus.outputs.key }}-${{ github.run_id }}-${{ github.run_attempt }} # The target's line for the issue job: its package, its name and how # its run ended. - if: always() env: OUTCOME: ${{ steps.fuzz.outcome }} run: | set -euo pipefail mkdir -p "$RUNNER_TEMP/result" echo "$PKG $NAME $OUTCOME" > "$RUNNER_TEMP/result/$NAME.txt" - if: always() uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: result-${{ matrix.target.name }} path: ${{ runner.temp }}/result/ if-no-files-found: error retention-days: 7 # Only main's run reports, so a run started on another branch never # closes the issue as main's. issue: needs: [targets, fuzz] if: always() && github.ref == 'refs/heads/main' && needs.targets.result == 'success' runs-on: ubuntu-latest timeout-minutes: 5 permissions: issues: write # to open, update and close the one issue of the failing targets steps: - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 with: pattern: result-* merge-multiple: true path: ${{ runner.temp }}/results - name: Open, update or close the issue env: GH_TOKEN: ${{ github.token }} GH_REPO: ${{ github.repository }} COUNT: ${{ needs.targets.outputs.count }} RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} run: | set -euo pipefail shopt -s nullglob title="Fuzzing found failing inputs" # Each target's line, with only the characters a package path, a # target's name and an outcome have. files=("$RUNNER_TEMP"/results/*.txt) cat ${files[@]+"${files[@]}"} /dev/null | LC_ALL=C tr -cd '0-9A-Za-z._/ \n-' > "$RUNNER_TEMP/all.txt" awk '$3 != "success" { print "- `" $2 "` in `" $1 "`: " $3 }' "$RUNNER_TEMP/all.txt" | sort > "$RUNNER_TEMP/failed.txt" # A target whose job reported nothing failed too. reported=$(wc -l < "$RUNNER_TEMP/all.txt") if (( reported < COUNT )); then echo "- $((COUNT - reported)) targets didn't report" >> "$RUNNER_TEMP/failed.txt" fi number=$(gh issue list --state open --search "in:title \"$title\"" --json number,title \ --jq "map(select(.title == \"$title\")) | .[0].number // empty") if [[ ! -s $RUNNER_TEMP/failed.txt ]]; then if [[ -n $number ]]; then gh issue close "$number" --comment "Every fuzz target passed tonight, its failing inputs included: $RUN_URL" fi exit 0 fi { echo "The nightly fuzzing found inputs that fail these targets:" echo cat "$RUNNER_TEMP/failed.txt" echo echo "The run, with each failing input as an artifact named \`crash-\`: $RUN_URL" echo echo "Put each input in its package's \`testdata/fuzz//\` folder, fix the code, and commit both." echo "The input stays in the target's corpus, so it fails each night until the fix is on main," echo "and this issue closes on the first night every target passes:" echo "see [Fuzzing](https://next.devvm.dev/docs/contributing/testing/#fuzzing)." } > "$RUNNER_TEMP/body.md" if [[ -z $number ]]; then gh issue create --title "$title" --label bug --body-file "$RUNNER_TEMP/body.md" exit 0 fi # A night's new failures get a comment, which notifies; the same # targets failing again only update the run's link. gh issue view "$number" --json body --jq .body | tr -d '\r' > "$RUNNER_TEMP/old.md" if ! grep '^- ' "$RUNNER_TEMP/old.md" | cmp -s - "$RUNNER_TEMP/failed.txt"; then gh issue comment "$number" --body "The failing targets changed: the description lists tonight's." fi gh issue edit "$number" --body-file "$RUNNER_TEMP/body.md"