diff --git a/.github/workflows/fuzz-corpus.yml b/.github/workflows/fuzz-corpus.yml new file mode 100644 index 0000000000..98fe931365 --- /dev/null +++ b/.github/workflows/fuzz-corpus.yml @@ -0,0 +1,179 @@ +name: Fuzz corpus (weekly) + +# Minimizes each target's cached fuzz corpus (see fuzz.yml) with +# `cargo fuzz cmin`, saves the result back to the cache, and proposes it as a +# commit to the branch in a pull request from bot/fuzz-corpus- (#387). +# Committed seed_* files are always kept: they are regression seeds. +# +# Two branches, as in fuzz.yml: main on Monday 08:00 UTC, v3 on Monday 20:00 +# UTC. Keep the cron strings here and in CORPUS_REF/run-name identical, and +# keep this file the same on main and v3. +# +# Opening the pull request needs the repository setting "Allow GitHub Actions +# to create and approve pull requests". Without it, the branch is still +# pushed and the job summary links to it. Pull requests opened with +# GITHUB_TOKEN do not start CI; they change only fuzz/corpus/. +run-name: >- + Fuzz corpus (${{ github.event_name == 'workflow_dispatch' && inputs.ref + || (github.event.schedule == '0 20 * * 1' && 'v3' || 'main') }}) + +on: + schedule: + - cron: "0 8 * * 1" # main + - cron: "0 20 * * 1" # v3 + workflow_dispatch: + inputs: + ref: + description: Branch whose corpus to minimize + required: true + default: main + +permissions: + contents: read + +env: + CORPUS_REF: ${{ github.event_name == 'workflow_dispatch' && inputs.ref || (github.event.schedule == '0 20 * * 1' && 'v3' || 'main') }} + # Larger corpus files stay in the cache only. + MAX_COMMITTED_BYTES: "65536" + +jobs: + targets: + name: List fuzz targets + runs-on: ubuntu-latest + outputs: + targets: ${{ steps.list.outputs.targets }} + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ env.CORPUS_REF }} + + - name: Read the [[bin]] names + id: list + shell: bash + run: | + targets="$(sed -n 's/^name = "\(.*\)"$/\1/p' fuzz/Cargo.toml | grep -vx minigraf-fuzz \ + | jq -R . | jq -sc .)" + echo "targets=$targets" >> "$GITHUB_OUTPUT" + + cmin: + name: Minimize ${{ matrix.target }} + needs: targets + runs-on: ubuntu-latest + timeout-minutes: 60 + strategy: + fail-fast: false + matrix: + target: ${{ fromJSON(needs.targets.outputs.targets) }} + + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ env.CORPUS_REF }} + + - name: Install Rust nightly + uses: dtolnay/rust-toolchain@nightly + + - name: Cache cargo-fuzz binary + uses: actions/cache@v4 + with: + path: ~/.cargo/bin/cargo-fuzz + key: cargo-fuzz-${{ runner.os }}-${{ hashFiles('fuzz/Cargo.toml') }} + + - name: Install cargo-fuzz + run: which cargo-fuzz || cargo install cargo-fuzz + + - name: Restore corpus + uses: actions/cache/restore@v4 + with: + path: fuzz/corpus/${{ matrix.target }} + key: fuzz-corpus-${{ env.CORPUS_REF }}-${{ matrix.target }}-cmin-${{ github.run_id }}-${{ github.run_attempt }} + restore-keys: fuzz-corpus-${{ env.CORPUS_REF }}-${{ matrix.target }}- + + - name: Minimize + shell: bash + env: + TARGET: ${{ matrix.target }} + run: | + dir="fuzz/corpus/$TARGET" + echo "Before: $(find "$dir" -type f | wc -l) files" + cargo fuzz cmin -O "$TARGET" "$dir" + # Put back every committed seed cmin dropped. + git ls-files -z "$dir" | grep -z '/seed_' | xargs -0 -r git checkout -- + echo "After: $(find "$dir" -type f | wc -l) files" + + - name: Save corpus + uses: actions/cache/save@v4 + with: + path: fuzz/corpus/${{ matrix.target }} + key: fuzz-corpus-${{ env.CORPUS_REF }}-${{ matrix.target }}-cmin-${{ github.run_id }}-${{ github.run_attempt }} + + - name: Keep committable files + shell: bash + env: + TARGET: ${{ matrix.target }} + run: | + mkdir -p "committed/$TARGET" + find "fuzz/corpus/$TARGET" -maxdepth 1 -type f -size -"$((MAX_COMMITTED_BYTES + 1))"c \ + -exec cp {} "committed/$TARGET/" \; + + - uses: actions/upload-artifact@v4 + with: + name: corpus-${{ matrix.target }} + path: committed/ + retention-days: 7 + + propose: + name: Propose the minimized corpus + needs: cmin + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ env.CORPUS_REF }} + + - uses: actions/download-artifact@v4 + with: + pattern: corpus-* + path: minimized + merge-multiple: true + + - name: Commit, push and open the pull request + shell: bash + env: + GH_TOKEN: ${{ github.token }} + run: | + branch="bot/fuzz-corpus-$CORPUS_REF" + for d in minimized/*/; do + target="$(basename "$d")" + rm -rf "fuzz/corpus/$target" + cp -r "$d" "fuzz/corpus/$target" + done + git add -A fuzz/corpus + if git diff --cached --quiet; then + echo "Corpus unchanged." | tee -a "$GITHUB_STEP_SUMMARY" + exit 0 + fi + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + git commit -q -m "fuzz: minimized corpus for $CORPUS_REF (weekly cmin)" \ + -m "Run: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" \ + -m "Refs #387" + git push -f origin "HEAD:refs/heads/$branch" + + body="Weekly \`cargo fuzz cmin\` of the cached corpus of every fuzz target on \`$CORPUS_REF\`, with all committed \`seed_*\` files kept. Files over $MAX_COMMITTED_BYTES bytes stay in the cache only. Generated by \`.github/workflows/fuzz-corpus.yml\` (#387). + + Run: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" + existing="$(gh pr list --head "$branch" --base "$CORPUS_REF" --state open --json number --jq '.[0].number')" + if [ -n "$existing" ]; then + echo "Updated pull request #$existing." | tee -a "$GITHUB_STEP_SUMMARY" + elif gh pr create --base "$CORPUS_REF" --head "$branch" \ + --title "fuzz: minimized corpus for $CORPUS_REF" --body "$body"; then + echo "Opened a pull request from $branch." | tee -a "$GITHUB_STEP_SUMMARY" + else + compare="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/compare/$CORPUS_REF...$branch?expand=1" + echo "Could not open the pull request (is \"Allow GitHub Actions to create and approve pull requests\" off?). Open it from $compare" \ + | tee -a "$GITHUB_STEP_SUMMARY" + fi diff --git a/.github/workflows/fuzz.yml b/.github/workflows/fuzz.yml index eef1af0410..50160be718 100644 --- a/.github/workflows/fuzz.yml +++ b/.github/workflows/fuzz.yml @@ -1,22 +1,75 @@ name: Fuzz (nightly) +# Two branches alternate every 12 hours: main at 04:00 UTC, v3 at 16:00 UTC. +# Scheduled runs always use this file from the default branch, so the branch +# under test is chosen from the cron string that fired (as in bench.yml). +# Keep the cron strings here and in FUZZ_REF/run-name identical, and keep this +# file the same on main and v3. +# +# Each target runs in its own job, in parallel. The corpus a run grows is saved +# in the Actions cache per branch and target, and the next run starts from it +# (#387). fuzz-corpus.yml minimizes it weekly and proposes it as a commit. +run-name: >- + Fuzz (${{ github.event_name == 'workflow_dispatch' && inputs.ref + || (github.event.schedule == '0 16 * * *' && 'v3' || 'main') }}) + on: schedule: - - cron: "0 4 * * *" + - cron: "0 4 * * *" # main + - cron: "0 16 * * *" # v3 workflow_dispatch: + inputs: + ref: + description: Branch to fuzz + required: true + default: main + seconds: + description: Seconds per target + required: true + default: "3300" permissions: contents: read - issues: write + +env: + FUZZ_REF: ${{ github.event_name == 'workflow_dispatch' && inputs.ref || (github.event.schedule == '0 16 * * *' && 'v3' || 'main') }} + FUZZ_SECONDS: ${{ github.event_name == 'workflow_dispatch' && inputs.seconds || '3300' }} jobs: + targets: + name: List fuzz targets + runs-on: ubuntu-latest + outputs: + targets: ${{ steps.list.outputs.targets }} + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ env.FUZZ_REF }} + + # Each branch fuzzes the targets its own fuzz/Cargo.toml declares. + - name: Read the [[bin]] names + id: list + shell: bash + run: | + targets="$(sed -n 's/^name = "\(.*\)"$/\1/p' fuzz/Cargo.toml | grep -vx minigraf-fuzz \ + | jq -R . | jq -sc .)" + echo "targets=$targets" >> "$GITHUB_OUTPUT" + echo "Targets on $FUZZ_REF: $targets" + fuzz: - name: Fuzz targets + name: Fuzz ${{ matrix.target }} + needs: targets runs-on: ubuntu-latest - timeout-minutes: 360 + timeout-minutes: 90 + strategy: + fail-fast: false + matrix: + target: ${{ fromJSON(needs.targets.outputs.targets) }} steps: - uses: actions/checkout@v4 + with: + ref: ${{ env.FUZZ_REF }} - name: Install Rust nightly uses: dtolnay/rust-toolchain@nightly @@ -30,102 +83,77 @@ jobs: - name: Install cargo-fuzz run: which cargo-fuzz || cargo install cargo-fuzz - - name: Fuzz wal_entry (55m) - id: wal_entry - shell: bash - run: | - set -o pipefail - cargo fuzz run wal_entry -- -max_total_time=3300 2>&1 | tee fuzz-wal_entry.log - continue-on-error: true - - - name: Fuzz file_header (55m) - id: file_header - shell: bash - run: | - set -o pipefail - cargo fuzz run file_header -- -max_total_time=3300 2>&1 | tee fuzz-file_header.log - continue-on-error: true - - - name: Fuzz fact_page (55m) - id: fact_page - shell: bash - run: | - set -o pipefail - cargo fuzz run fact_page -- -max_total_time=3300 2>&1 | tee fuzz-fact_page.log - continue-on-error: true + - name: Restore corpus + uses: actions/cache/restore@v4 + with: + path: fuzz/corpus/${{ matrix.target }} + key: fuzz-corpus-${{ env.FUZZ_REF }}-${{ matrix.target }}-${{ github.run_id }}-${{ github.run_attempt }} + restore-keys: fuzz-corpus-${{ env.FUZZ_REF }}-${{ matrix.target }}- - - name: Fuzz btree_page (55m) - id: btree_page - shell: bash - run: | - set -o pipefail - cargo fuzz run btree_page -- -max_total_time=3300 2>&1 | tee fuzz-btree_page.log - continue-on-error: true + - name: Build + run: cargo fuzz build -O ${{ matrix.target }} - - name: Fuzz datalog_parser (55m) - id: datalog_parser + # -timeout: one input running this long is a hang (libFuzzer's default + # of 1200 s would spend a third of the budget on it). + - name: Fuzz ${{ matrix.target }} shell: bash run: | set -o pipefail - cargo fuzz run datalog_parser -- -max_total_time=3300 2>&1 | tee fuzz-datalog_parser.log - continue-on-error: true + cargo fuzz run -O ${{ matrix.target }} fuzz/corpus/${{ matrix.target }} -- \ + -max_total_time="$FUZZ_SECONDS" -timeout=60 2>&1 | tee fuzz-${{ matrix.target }}.log - - name: Fuzz datalog_eval (55m) - id: datalog_eval - shell: bash - run: | - set -o pipefail - cargo fuzz run datalog_eval -- -max_total_time=3300 2>&1 | tee fuzz-datalog_eval.log - continue-on-error: true + - name: Save corpus + if: ${{ always() }} + uses: actions/cache/save@v4 + with: + path: fuzz/corpus/${{ matrix.target }} + key: fuzz-corpus-${{ env.FUZZ_REF }}-${{ matrix.target }}-${{ github.run_id }}-${{ github.run_attempt }} - - name: Upload fuzz failures - if: ${{ always() && (steps.wal_entry.outcome == 'failure' || steps.file_header.outcome == 'failure' || steps.fact_page.outcome == 'failure' || steps.btree_page.outcome == 'failure' || steps.datalog_parser.outcome == 'failure' || steps.datalog_eval.outcome == 'failure') }} + - name: Upload failure + if: ${{ failure() }} uses: actions/upload-artifact@v4 with: - name: fuzz-failures-${{ github.run_id }} + name: fuzz-failure-${{ matrix.target }} path: | - fuzz/artifacts/ - fuzz-*.log + fuzz/artifacts/${{ matrix.target }}/ + fuzz-${{ matrix.target }}.log if-no-files-found: warn retention-days: 30 - - name: Record fuzz failures - if: ${{ always() && (steps.wal_entry.outcome == 'failure' || steps.file_header.outcome == 'failure' || steps.fact_page.outcome == 'failure' || steps.btree_page.outcome == 'failure' || steps.datalog_parser.outcome == 'failure' || steps.datalog_eval.outcome == 'failure') }} + report: + name: Record fuzz failures + needs: fuzz + if: ${{ failure() }} + runs-on: ubuntu-latest + permissions: + contents: read + issues: write + steps: + - uses: actions/download-artifact@v4 + with: + pattern: fuzz-failure-* + path: failures + + - name: Open or update the issue shell: bash env: GH_TOKEN: ${{ github.token }} - ISSUE_TITLE: "Nightly fuzz failure" - FAILED_TARGETS: >- - wal_entry=${{ steps.wal_entry.outcome }}, - file_header=${{ steps.file_header.outcome }}, - fact_page=${{ steps.fact_page.outcome }}, - btree_page=${{ steps.btree_page.outcome }}, - datalog_parser=${{ steps.datalog_parser.outcome }}, - datalog_eval=${{ steps.datalog_eval.outcome }} + ISSUE_TITLE: "Nightly fuzz failure (${{ env.FUZZ_REF }})" run: | report_file="fuzz-failure.md" - artifact_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" + run_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" { - echo "The nightly fuzz workflow found one or more failures." + echo "The nightly fuzz workflow found one or more failures on \`$FUZZ_REF\`." echo - echo "- Run and artifacts: $artifact_url" - echo "- Commit: $GITHUB_SHA" - echo "- Target outcomes: $FAILED_TARGETS" - echo "- Reproduce: \`cargo +nightly fuzz run fuzz/artifacts//\`" - echo "- Full logs: see the \`fuzz-failures-$GITHUB_RUN_ID\` artifact on the run above" + echo "- Run and artifacts: $run_url" + echo "- Reproduce: check out \`$FUZZ_REF\` at the run's commit, then" + echo " \`cargo +nightly fuzz run fuzz/artifacts//\`" echo echo "## Fuzz logs (failed targets only, last 100 lines each)" echo - - IFS=',' read -ra target_outcomes <<< "$FAILED_TARGETS" - for entry in "${target_outcomes[@]}"; do - entry="$(echo "$entry" | xargs)" - target="${entry%%=*}" - outcome="${entry#*=}" - [ "$outcome" = "failure" ] || continue - log="fuzz-${target}.log" - [ -e "$log" ] || continue - echo "### $log" + shopt -s nullglob + for log in failures/fuzz-failure-*/fuzz-*.log; do + echo "### $(basename "$log")" echo echo '```text' tail -n 100 "$log" @@ -134,7 +162,7 @@ jobs: done } > "$report_file" - # Defensive hard cap: GitHub issue/comment bodies max out at 65536 chars. + # GitHub issue and comment bodies max out at 65536 characters. max_bytes=60000 if [ "$(wc -c < "$report_file")" -gt "$max_bytes" ]; then head -c "$max_bytes" "$report_file" > "${report_file}.tmp" @@ -142,7 +170,7 @@ jobs: echo echo "..." echo - echo "_Report truncated to fit GitHub's body size limit. See the run and artifact linked above for full logs._" + echo "_Report truncated to fit GitHub's body size limit. See the run linked above for full logs._" } >> "${report_file}.tmp" mv "${report_file}.tmp" "$report_file" fi @@ -153,7 +181,7 @@ jobs: --search "$ISSUE_TITLE in:title" \ --json number,title \ --limit 100 \ - --jq '.[] | select(.title == "Nightly fuzz failure") | .number' \ + --jq ".[] | select(.title == \"$ISSUE_TITLE\") | .number" \ | head -n 1)" if [ -n "$existing_issue" ]; then @@ -161,7 +189,4 @@ jobs: else gh issue create --repo "$GITHUB_REPOSITORY" --title "$ISSUE_TITLE" --body-file "$report_file" fi - - - name: Fail workflow when fuzzing finds a failure - if: ${{ always() && (steps.wal_entry.outcome == 'failure' || steps.file_header.outcome == 'failure' || steps.fact_page.outcome == 'failure' || steps.btree_page.outcome == 'failure' || steps.datalog_parser.outcome == 'failure' || steps.datalog_eval.outcome == 'failure') }} - run: exit 1 + exit 1