xref: /aosp_15_r20/external/cronet/testing/libfuzzer/reproducing.md (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker# Reproducing libFuzzer and AFL crashes
2*6777b538SAndroid Build Coastguard Worker
3*6777b538SAndroid Build Coastguard Worker*** note
4*6777b538SAndroid Build Coastguard Worker**Requirements:** For Windows, you must convert the forward slashes (/) to
5*6777b538SAndroid Build Coastguard Workerbackslashes (\\) in the commands below and use `set` command instead of `export`
6*6777b538SAndroid Build Coastguard Workerto set the environment variable (step 4). Note that these commands are intended
7*6777b538SAndroid Build Coastguard Workerto be used with cmd.exe, not PowerShell. Also, you may find [these tips] on how
8*6777b538SAndroid Build Coastguard Workerto debug an ASAN instrumented binary helpful.
9*6777b538SAndroid Build Coastguard Worker***
10*6777b538SAndroid Build Coastguard Worker
11*6777b538SAndroid Build Coastguard Worker[TOC]
12*6777b538SAndroid Build Coastguard Worker
13*6777b538SAndroid Build Coastguard Worker## Crashes reported as Reproducible
14*6777b538SAndroid Build Coastguard Worker
15*6777b538SAndroid Build Coastguard WorkerThe majority of the bugs reported by ClusterFuzz have **Reproducible** label.
16*6777b538SAndroid Build Coastguard WorkerThat means there is a testcase that can be used to reliably reproduce the crash.
17*6777b538SAndroid Build Coastguard Worker
18*6777b538SAndroid Build Coastguard Worker1. Download the testcase from ClusterFuzz. If you are CCed on an issue filed by
19*6777b538SAndroid Build Coastguard Worker   ClusterFuzz, a link to it is next to "Reproducer testcase" in the bug
20*6777b538SAndroid Build Coastguard Worker   description.
21*6777b538SAndroid Build Coastguard Worker
22*6777b538SAndroid Build Coastguard Worker   For the rest of this walkthrough, we call the path of this
23*6777b538SAndroid Build Coastguard Worker   file: `$TESTCASE_PATH` and the fuzz target you want to reproduce a
24*6777b538SAndroid Build Coastguard Worker   crash on: `$FUZZER_NAME` (provided as "Fuzz Target" in the bug
25*6777b538SAndroid Build Coastguard Worker   description).
26*6777b538SAndroid Build Coastguard Worker
27*6777b538SAndroid Build Coastguard Worker2. Generate gn build configuration:
28*6777b538SAndroid Build Coastguard Worker
29*6777b538SAndroid Build Coastguard Worker```
30*6777b538SAndroid Build Coastguard Workergn args out/fuzz
31*6777b538SAndroid Build Coastguard Worker```
32*6777b538SAndroid Build Coastguard Worker
33*6777b538SAndroid Build Coastguard Worker   This will open up an editor. Copy the gn configuration parameters from the
34*6777b538SAndroid Build Coastguard Worker   values provided in `GN Config` section in the ClusterFuzz testcase report.
35*6777b538SAndroid Build Coastguard Worker
36*6777b538SAndroid Build Coastguard Worker
37*6777b538SAndroid Build Coastguard Worker3. Build the fuzzer:
38*6777b538SAndroid Build Coastguard Worker
39*6777b538SAndroid Build Coastguard Worker```
40*6777b538SAndroid Build Coastguard Workerautoninja -C out/fuzz $FUZZER_NAME
41*6777b538SAndroid Build Coastguard Worker```
42*6777b538SAndroid Build Coastguard Worker
43*6777b538SAndroid Build Coastguard Worker4. Set the `*SAN_OPTIONS` environment variable as provided in the
44*6777b538SAndroid Build Coastguard Worker   `Crash Stacktrace` section in the testcase report.
45*6777b538SAndroid Build Coastguard Worker   Here is an example value of `ASAN_OPTIONS` that is similar to its value on
46*6777b538SAndroid Build Coastguard Worker   ClusterFuzz:
47*6777b538SAndroid Build Coastguard Worker
48*6777b538SAndroid Build Coastguard Worker```
49*6777b538SAndroid Build Coastguard Workerexport ASAN_OPTIONS=redzone=256:print_summary=1:handle_sigill=1:allocator_release_to_os_interval_ms=500:print_suppressions=0:strict_memcmp=1:allow_user_segv_handler=0:use_sigaltstack=1:handle_sigfpe=1:handle_sigbus=1:detect_stack_use_after_return=0:alloc_dealloc_mismatch=0:detect_leaks=0:print_scariness=1:allocator_may_return_null=1:handle_abort=1:check_malloc_usable_size=0:detect_container_overflow=0:quarantine_size_mb=256:detect_odr_violation=0:symbolize=1:handle_segv=1:fast_unwind_on_fatal=0
50*6777b538SAndroid Build Coastguard Worker```
51*6777b538SAndroid Build Coastguard Worker
52*6777b538SAndroid Build Coastguard Worker5. Run the fuzz target:
53*6777b538SAndroid Build Coastguard Worker
54*6777b538SAndroid Build Coastguard Worker```
55*6777b538SAndroid Build Coastguard Workerout/fuzz/$FUZZER_NAME -runs=100 $TESTCASE_PATH
56*6777b538SAndroid Build Coastguard Worker```
57*6777b538SAndroid Build Coastguard Worker
58*6777b538SAndroid Build Coastguard Worker[File a bug] if you run into any issues.
59*6777b538SAndroid Build Coastguard Worker
60*6777b538SAndroid Build Coastguard Worker## Symbolizing stack traces
61*6777b538SAndroid Build Coastguard Worker
62*6777b538SAndroid Build Coastguard WorkerStack traces from ASAN builds are not symbolized by default. However, you
63*6777b538SAndroid Build Coastguard Workercan symbolize them by piping the output into:
64*6777b538SAndroid Build Coastguard Worker
65*6777b538SAndroid Build Coastguard Worker```
66*6777b538SAndroid Build Coastguard Workersrc/tools/valgrind/asan/asan_symbolize.py
67*6777b538SAndroid Build Coastguard Worker```
68*6777b538SAndroid Build Coastguard Worker
69*6777b538SAndroid Build Coastguard Worker## Crashes reported as Unreproducible
70*6777b538SAndroid Build Coastguard Worker
71*6777b538SAndroid Build Coastguard WorkerClusterFuzz generally does not report issues that it cannot reliably reproduce,
72*6777b538SAndroid Build Coastguard Workerunless the following condition is met. If a certain crash is occurring often
73*6777b538SAndroid Build Coastguard Workerenough, such a crash might be reported with **Unreproducible** label and an
74*6777b538SAndroid Build Coastguard Workerexplicit clarification that there is no convenient way to reproduce it. There
75*6777b538SAndroid Build Coastguard Workerare two ways to work with such crashes.
76*6777b538SAndroid Build Coastguard Worker
77*6777b538SAndroid Build Coastguard Worker1. Try a speculative fix based on the stacktrace. Once the fix is landed, wait a
78*6777b538SAndroid Build Coastguard Worker   couple days and then check Crash Statistics section on the ClusterFuzz
79*6777b538SAndroid Build Coastguard Worker   testcase report page. If the fix works out, you will see that the crash is
80*6777b538SAndroid Build Coastguard Worker   not happening anymore. If the crash does not occur again for a little while,
81*6777b538SAndroid Build Coastguard Worker   ClusterFuzz will automatically close the issue as Verified.
82*6777b538SAndroid Build Coastguard Worker
83*6777b538SAndroid Build Coastguard Worker2. (libFuzzer only) Try to reproduce the whole fuzzing session. This workflow is
84*6777b538SAndroid Build Coastguard Worker   very similar to the one described above for the **Reproducible** crashes. The
85*6777b538SAndroid Build Coastguard Worker   only differences are:
86*6777b538SAndroid Build Coastguard Worker
87*6777b538SAndroid Build Coastguard Worker  * On step 1, instead of downloading a single testcase, you need to download
88*6777b538SAndroid Build Coastguard Worker    corpus backup. This can be done using the following command:
89*6777b538SAndroid Build Coastguard Worker```
90*6777b538SAndroid Build Coastguard Workergsutil cp gs://clusterfuzz-libfuzzer-backup/corpus/libfuzzer/$FUZZER_NAME/latest.zip .
91*6777b538SAndroid Build Coastguard Worker```
92*6777b538SAndroid Build Coastguard Worker
93*6777b538SAndroid Build Coastguard Worker  * Alternatively, you can navigate to the following URL in your browser and
94*6777b538SAndroid Build Coastguard Worker    download the `latest.zip` file:
95*6777b538SAndroid Build Coastguard Worker```
96*6777b538SAndroid Build Coastguard Workerhttps://pantheon.corp.google.com/storage/browser/clusterfuzz-libfuzzer-backup/corpus/libfuzzer/$FUZZER_NAME
97*6777b538SAndroid Build Coastguard Worker```
98*6777b538SAndroid Build Coastguard Worker
99*6777b538SAndroid Build Coastguard Worker  * Create an empty directory and unpack the corpus into it.
100*6777b538SAndroid Build Coastguard Worker  * Follow steps 2-4 in the **Reproducible** section above.
101*6777b538SAndroid Build Coastguard Worker  * On step 5, use the following command:
102*6777b538SAndroid Build Coastguard Worker
103*6777b538SAndroid Build Coastguard Worker```
104*6777b538SAndroid Build Coastguard Workerout/fuzz/$FUZZER_NAME -timeout=25 -rss_limit_mb=2048 -print_final_stats=1 $CORPUS_DIRECTORY_FROM_THE_PREVIOUS_STEP
105*6777b538SAndroid Build Coastguard Worker```
106*6777b538SAndroid Build Coastguard Worker
107*6777b538SAndroid Build Coastguard Worker  * Wait and hope that the fuzzer will crash.
108*6777b538SAndroid Build Coastguard Worker
109*6777b538SAndroid Build Coastguard WorkerWaiting for a crash to occur may take some time (up to 1hr), but if it happens,
110*6777b538SAndroid Build Coastguard Workeryou will be able to test the fix locally and/or somehow debug the issue.
111*6777b538SAndroid Build Coastguard Worker
112*6777b538SAndroid Build Coastguard Worker## Minimizing a crash input (optional)
113*6777b538SAndroid Build Coastguard Worker
114*6777b538SAndroid Build Coastguard WorkerClusterFuzz does crash input minimization automatically, and a typical crash
115*6777b538SAndroid Build Coastguard Workerreport has two testcases available for downloading:
116*6777b538SAndroid Build Coastguard Worker
117*6777b538SAndroid Build Coastguard Worker* An original testcase that has triggered the crash;
118*6777b538SAndroid Build Coastguard Worker* A minimized testcase that is smaller than the original but triggers the same
119*6777b538SAndroid Build Coastguard Worker  crash.
120*6777b538SAndroid Build Coastguard Worker
121*6777b538SAndroid Build Coastguard WorkerIf you would like to further minimize a testcase, run the fuzz target with the
122*6777b538SAndroid Build Coastguard Workertwo additional arguments:
123*6777b538SAndroid Build Coastguard Worker
124*6777b538SAndroid Build Coastguard Worker* `-minimize_crash=1`
125*6777b538SAndroid Build Coastguard Worker* `-exact_artifact_path=<output_filename_for_minimized_testcase>`
126*6777b538SAndroid Build Coastguard Worker
127*6777b538SAndroid Build Coastguard WorkerThe full command would be:
128*6777b538SAndroid Build Coastguard Worker
129*6777b538SAndroid Build Coastguard Worker```
130*6777b538SAndroid Build Coastguard Workerout/fuzz/$FUZZER_NAME -minimize_crash=1 -exact_artifact_path=<minimized_testcase_path> $TESTCASE_PATH
131*6777b538SAndroid Build Coastguard Worker```
132*6777b538SAndroid Build Coastguard Worker
133*6777b538SAndroid Build Coastguard WorkerThis might be useful for large testcases that make it hard to identify a root
134*6777b538SAndroid Build Coastguard Workercause of a crash. You can leave the minimization running locally for a while
135*6777b538SAndroid Build Coastguard Worker(e.g. overnight) for better results.
136*6777b538SAndroid Build Coastguard Worker
137*6777b538SAndroid Build Coastguard Worker
138*6777b538SAndroid Build Coastguard Worker[File a bug]: https://bugs.chromium.org/p/chromium/issues/entry?components=Tools%3EStability%3ElibFuzzer&comment=What%20problem%20are%20you%20seeing
139*6777b538SAndroid Build Coastguard Worker[these tips]: https://github.com/google/sanitizers/wiki/AddressSanitizerWindowsPort#debugging
140