Skip to content

Commit 7025d75

Browse files
sethmlarsonwebknjazhugovk
authored
[3.13] gh-143572: Run 'python3-libraries' fuzzer in CI using CIFuzz (… (#143915)
Co-authored-by: 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко) <578543+webknjaz@users.noreply.github.com> Co-authored-by: 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко) <wk.cvs.github@sydorenko.org.ua> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent 6fdb9f1 commit 7025d75

File tree

4 files changed

+161
-46
lines changed

4 files changed

+161
-46
lines changed

.github/workflows/build.yml

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -543,45 +543,45 @@ jobs:
543543
sanitizer: ${{ matrix.sanitizer }}
544544
free-threading: ${{ matrix.free-threading }}
545545

546-
# CIFuzz job based on https://google.github.io/oss-fuzz/getting-started/continuous-integration/
547546
cifuzz:
548-
name: CIFuzz
549-
runs-on: ubuntu-latest
550-
timeout-minutes: 60
547+
# ${{ '' } is a hack to nest jobs under the same sidebar category.
548+
name: CIFuzz${{ '' }} # zizmor: ignore[obfuscation]
551549
needs: build-context
552-
if: needs.build-context.outputs.run-ci-fuzz == 'true'
550+
if: >-
551+
needs.build-context.outputs.run-ci-fuzz == 'true'
552+
|| needs.build-context.outputs.run-ci-fuzz-stdlib == 'true'
553553
permissions:
554554
security-events: write
555555
strategy:
556556
fail-fast: false
557557
matrix:
558-
sanitizer: [address, undefined, memory]
559-
steps:
560-
- name: Build fuzzers (${{ matrix.sanitizer }})
561-
id: build
562-
uses: google/oss-fuzz/infra/cifuzz/actions/build_fuzzers@master
563-
with:
564-
oss-fuzz-project-name: cpython3
565-
sanitizer: ${{ matrix.sanitizer }}
566-
- name: Run fuzzers (${{ matrix.sanitizer }})
567-
uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master
568-
with:
569-
fuzz-seconds: 600
570-
oss-fuzz-project-name: cpython3
571-
output-sarif: true
572-
sanitizer: ${{ matrix.sanitizer }}
573-
- name: Upload crash
574-
if: failure() && steps.build.outcome == 'success'
575-
uses: actions/upload-artifact@v6
576-
with:
577-
name: ${{ matrix.sanitizer }}-artifacts
578-
path: ./out/artifacts
579-
- name: Upload SARIF
580-
if: always() && steps.build.outcome == 'success'
581-
uses: github/codeql-action/upload-sarif@v3
582-
with:
583-
sarif_file: cifuzz-sarif/results.sarif
584-
checkout_path: cifuzz-sarif
558+
sanitizer:
559+
- address
560+
- undefined
561+
- memory
562+
oss-fuzz-project-name:
563+
- cpython3
564+
- python3-libraries
565+
exclude:
566+
# Note that the 'no-exclude' sentinel below is to prevent
567+
# an empty string value from excluding all jobs and causing
568+
# GHA to create a 'default' matrix entry with all empty values.
569+
- oss-fuzz-project-name: >-
570+
${{
571+
needs.build-context.outputs.run-ci-fuzz == 'true'
572+
&& 'no-exclude'
573+
|| 'cpython3'
574+
}}
575+
- oss-fuzz-project-name: >-
576+
${{
577+
needs.build-context.outputs.run-ci-fuzz-stdlib == 'true'
578+
&& 'no-exclude'
579+
|| 'python3-libraries'
580+
}}
581+
uses: ./.github/workflows/reusable-cifuzz.yml
582+
with:
583+
oss-fuzz-project-name: ${{ matrix.oss-fuzz-project-name }}
584+
sanitizer: ${{ matrix.sanitizer }}
585585

586586
all-required-green: # This job does nothing and is only used for the branch protection
587587
name: All required checks pass
@@ -625,7 +625,12 @@ jobs:
625625
|| ''
626626
}}
627627
${{ !fromJSON(needs.build-context.outputs.run-windows-tests) && 'build-windows,' || '' }}
628-
${{ !fromJSON(needs.build-context.outputs.run-ci-fuzz) && 'cifuzz,' || '' }}
628+
${{
629+
!fromJSON(needs.build-context.outputs.run-ci-fuzz)
630+
&& !fromJSON(needs.build-context.outputs.run-ci-fuzz-stdlib)
631+
&& 'cifuzz,' ||
632+
''
633+
}}
629634
${{ !fromJSON(needs.build-context.outputs.run-macos) && 'build-macos,' || '' }}
630635
${{
631636
!fromJSON(needs.build-context.outputs.run-ubuntu)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# CIFuzz job based on https://google.github.io/oss-fuzz/getting-started/continuous-integration/
2+
name: Reusable CIFuzz
3+
4+
on:
5+
workflow_call:
6+
inputs:
7+
oss-fuzz-project-name:
8+
description: OSS-Fuzz project name
9+
required: true
10+
type: string
11+
sanitizer:
12+
description: OSS-Fuzz sanitizer
13+
required: true
14+
type: string
15+
16+
jobs:
17+
cifuzz:
18+
name: ${{ inputs.oss-fuzz-project-name }} (${{ inputs.sanitizer }})
19+
runs-on: ubuntu-latest
20+
timeout-minutes: 60
21+
steps:
22+
- name: Build fuzzers (${{ inputs.sanitizer }})
23+
id: build
24+
uses: google/oss-fuzz/infra/cifuzz/actions/build_fuzzers@master
25+
with:
26+
oss-fuzz-project-name: ${{ inputs.oss-fuzz-project-name }}
27+
sanitizer: ${{ inputs.sanitizer }}
28+
- name: Run fuzzers (${{ inputs.sanitizer }})
29+
uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master
30+
with:
31+
fuzz-seconds: 600
32+
oss-fuzz-project-name: ${{ inputs.oss-fuzz-project-name }}
33+
output-sarif: true
34+
sanitizer: ${{ inputs.sanitizer }}
35+
- name: Upload crash
36+
if: failure() && steps.build.outcome == 'success'
37+
uses: actions/upload-artifact@v6
38+
with:
39+
name: ${{ inputs.sanitizer }}-artifacts
40+
path: ./out/artifacts
41+
- name: Upload SARIF
42+
if: always() && steps.build.outcome == 'success'
43+
uses: github/codeql-action/upload-sarif@v4
44+
with:
45+
sarif_file: cifuzz-sarif/results.sarif
46+
checkout_path: cifuzz-sarif

.github/workflows/reusable-context.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ on: # yamllint disable-line rule:truthy
2121
description: Whether to run the Android tests
2222
value: ${{ jobs.compute-changes.outputs.run-android }} # bool
2323
run-ci-fuzz:
24-
description: Whether to run the CIFuzz job
24+
description: Whether to run the CIFuzz job for 'cpython' fuzzer
2525
value: ${{ jobs.compute-changes.outputs.run-ci-fuzz }} # bool
26+
run-ci-fuzz-stdlib:
27+
description: Whether to run the CIFuzz job for 'python3-libraries' fuzzer
28+
value: ${{ jobs.compute-changes.outputs.run-ci-fuzz-stdlib }} # bool
2629
run-docs:
2730
description: Whether to build the docs
2831
value: ${{ jobs.compute-changes.outputs.run-docs }} # bool
@@ -53,6 +56,7 @@ jobs:
5356
outputs:
5457
run-android: ${{ steps.changes.outputs.run-android }}
5558
run-ci-fuzz: ${{ steps.changes.outputs.run-ci-fuzz }}
59+
run-ci-fuzz-stdlib: ${{ steps.changes.outputs.run-ci-fuzz-stdlib }}
5660
run-docs: ${{ steps.changes.outputs.run-docs }}
5761
run-macos: ${{ steps.changes.outputs.run-macos }}
5862
run-tests: ${{ steps.changes.outputs.run-tests }}

Tools/build/compute-changes.py

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import os
1313
import subprocess
14-
from dataclasses import dataclass
14+
from dataclasses import dataclass, fields
1515
from pathlib import Path
1616

1717
TYPE_CHECKING = False
@@ -51,11 +51,59 @@
5151
MACOS_DIRS = frozenset({"Mac"})
5252
WASI_DIRS = frozenset({Path("Tools", "wasm")})
5353

54+
LIBRARY_FUZZER_PATHS = frozenset({
55+
# All C/CPP fuzzers.
56+
Path("configure"),
57+
Path(".github/workflows/reusable-cifuzz.yml"),
58+
# ast
59+
Path("Lib/ast.py"),
60+
Path("Python/ast.c"),
61+
# configparser
62+
Path("Lib/configparser.py"),
63+
# csv
64+
Path("Lib/csv.py"),
65+
Path("Modules/_csv.c"),
66+
# decode
67+
Path("Lib/encodings/"),
68+
Path("Modules/_codecsmodule.c"),
69+
Path("Modules/cjkcodecs/"),
70+
Path("Modules/unicodedata*"),
71+
# difflib
72+
Path("Lib/difflib.py"),
73+
# email
74+
Path("Lib/email/"),
75+
# html
76+
Path("Lib/html/"),
77+
Path("Lib/_markupbase.py"),
78+
# http.client
79+
Path("Lib/http/client.py"),
80+
# json
81+
Path("Lib/json/"),
82+
Path("Modules/_json.c"),
83+
# plist
84+
Path("Lib/plistlib.py"),
85+
# re
86+
Path("Lib/re/"),
87+
Path("Modules/_sre/"),
88+
# tarfile
89+
Path("Lib/tarfile.py"),
90+
# tomllib
91+
Path("Modules/tomllib/"),
92+
# xml
93+
Path("Lib/xml/"),
94+
Path("Lib/_markupbase.py"),
95+
Path("Modules/expat/"),
96+
Path("Modules/pyexpat.c"),
97+
# zipfile
98+
Path("Lib/zipfile/"),
99+
})
100+
54101

55102
@dataclass(kw_only=True, slots=True)
56103
class Outputs:
57104
run_android: bool = False
58105
run_ci_fuzz: bool = False
106+
run_ci_fuzz_stdlib: bool = False
59107
run_docs: bool = False
60108
run_macos: bool = False
61109
run_tests: bool = False
@@ -93,6 +141,11 @@ def compute_changes() -> None:
93141
else:
94142
print("Branch too old for CIFuzz tests; or no C files were changed")
95143

144+
if outputs.run_ci_fuzz_stdlib:
145+
print("Run CIFuzz tests for stdlib")
146+
else:
147+
print("Branch too old for CIFuzz tests; or no stdlib files were changed")
148+
96149
if outputs.run_docs:
97150
print("Build documentation")
98151

@@ -141,9 +194,18 @@ def get_file_platform(file: Path) -> str | None:
141194
return None
142195

143196

197+
def is_fuzzable_library_file(file: Path) -> bool:
198+
return any(
199+
(file.is_relative_to(needs_fuzz) and needs_fuzz.is_dir())
200+
or (file == needs_fuzz and file.is_file())
201+
for needs_fuzz in LIBRARY_FUZZER_PATHS
202+
)
203+
204+
144205
def process_changed_files(changed_files: Set[Path]) -> Outputs:
145206
run_tests = False
146207
run_ci_fuzz = False
208+
run_ci_fuzz_stdlib = False
147209
run_docs = False
148210
run_windows_tests = False
149211
run_windows_msi = False
@@ -157,8 +219,8 @@ def process_changed_files(changed_files: Set[Path]) -> Outputs:
157219
doc_file = file.suffix in SUFFIXES_DOCUMENTATION or doc_or_misc
158220

159221
if file.parent == GITHUB_WORKFLOWS_PATH:
160-
if file.name == "build.yml":
161-
run_tests = run_ci_fuzz = True
222+
if file.name in ("build.yml", "reusable-cifuzz.yml"):
223+
run_tests = run_ci_fuzz = run_ci_fuzz_stdlib = True
162224
has_platform_specific_change = False
163225
if file.name == "reusable-docs.yml":
164226
run_docs = True
@@ -189,6 +251,8 @@ def process_changed_files(changed_files: Set[Path]) -> Outputs:
189251
("Modules", "_xxtestfuzz"),
190252
}:
191253
run_ci_fuzz = True
254+
if not run_ci_fuzz_stdlib and is_fuzzable_library_file(file):
255+
run_ci_fuzz_stdlib = True
192256

193257
# Check for changed documentation-related files
194258
if doc_file:
@@ -219,6 +283,7 @@ def process_changed_files(changed_files: Set[Path]) -> Outputs:
219283
return Outputs(
220284
run_android=run_android,
221285
run_ci_fuzz=run_ci_fuzz,
286+
run_ci_fuzz_stdlib=run_ci_fuzz_stdlib,
222287
run_docs=run_docs,
223288
run_macos=run_macos,
224289
run_tests=run_tests,
@@ -252,15 +317,10 @@ def write_github_output(outputs: Outputs) -> None:
252317
return
253318

254319
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as f:
255-
f.write(f"run-android={bool_lower(outputs.run_android)}\n")
256-
f.write(f"run-ci-fuzz={bool_lower(outputs.run_ci_fuzz)}\n")
257-
f.write(f"run-docs={bool_lower(outputs.run_docs)}\n")
258-
f.write(f"run-macos={bool_lower(outputs.run_macos)}\n")
259-
f.write(f"run-tests={bool_lower(outputs.run_tests)}\n")
260-
f.write(f"run-ubuntu={bool_lower(outputs.run_ubuntu)}\n")
261-
f.write(f"run-wasi={bool_lower(outputs.run_wasi)}\n")
262-
f.write(f"run-windows-msi={bool_lower(outputs.run_windows_msi)}\n")
263-
f.write(f"run-windows-tests={bool_lower(outputs.run_windows_tests)}\n")
320+
for field in fields(outputs):
321+
name = field.name.replace("_", "-")
322+
val = bool_lower(getattr(outputs, field.name))
323+
f.write(f"{name}={val}\n")
264324

265325

266326
def bool_lower(value: bool, /) -> str:

0 commit comments

Comments
 (0)