Max Moroz | 0266b68 | 2019-07-29 17:30:30 | [diff] [blame] | 1 | # Getting started with fuzzing in Chromium |
aizatsky | a6f8629 | 2016-03-18 00:22:24 | [diff] [blame] | 2 | |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 3 | This document walks through how to get started adding fuzz tests to Chromium. |
| 4 | |
| 5 | It guides you how to use our latest fuzzing technology, called [FuzzTest]. This |
| 6 | replaces earlier technology called [libfuzzer]. See the section at the end |
| 7 | for reasons why you might sometimes still want to use libfuzzer. |
aizatsky | a6f8629 | 2016-03-18 00:22:24 | [diff] [blame] | 8 | |
Max Moroz | 0266b68 | 2019-07-29 17:30:30 | [diff] [blame] | 9 | [TOC] |
aizatsky | a6f8629 | 2016-03-18 00:22:24 | [diff] [blame] | 10 | |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 11 | ## What to fuzz |
aizatsky | a6f8629 | 2016-03-18 00:22:24 | [diff] [blame] | 12 | |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 13 | You should fuzz any function which takes input from any |
| 14 | untrusted source, such as the internet. If the code parses, decodes, or |
| 15 | otherwise manipulates that input, it definitely should be fuzzed! |
aizatsky | a6f8629 | 2016-03-18 00:22:24 | [diff] [blame] | 16 | |
Adrian Taylor | e3fbcd46 | 2024-02-27 11:18:28 | [diff] [blame] | 17 | To decide how best to fuzz it, you should decide which of these two situations |
| 18 | best matches your input: |
| 19 | |
| 20 | * *Binary data*: the input is a single buffer of contiguous bytes, for example |
| 21 | an image or some binary format which your code decodes. |
| 22 | * *Function arguments*: the input is multiple different chunks of data, for |
| 23 | example the arguments to a function. |
| 24 | |
| 25 | In the latter case, go ahead and read this guide - it will show you how to use |
| 26 | our latest fuzzing technology, FuzzTest. |
| 27 | |
| 28 | If however your input more closely matches the former description - just a |
| 29 | single binary blob of data - then instead use our older fuzzing technology |
| 30 | [libfuzzer] - click that link for a separate getting started guide. (libfuzzer |
| 31 | will work a little better in these cases because if the fuzzer finds a problem, |
| 32 | the test case will exactly match the binary format.) |
| 33 | |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 34 | ## How to fuzz |
mmoroz | db4e66a1 | 2016-11-04 22:14:24 | [diff] [blame] | 35 | |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 36 | 1. Find your existing unit test target. Create a new similar target |
| 37 | alongside. (In the future, you'll be able to add them right into your |
| 38 | unit test code directly.) |
Adrian Taylor | 82c71d0 | 2023-10-30 19:13:43 | [diff] [blame] | 39 | 2. Add a gn target definition a lot like a normal unit test, but with |
Adrian Taylor | a20bc5a | 2024-01-29 23:22:26 | [diff] [blame] | 40 | `fuzztests = [ list-of-fuzztests ]`. See below for details. Create a `.cc` file. |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 41 | 3. In the unit tests code, `#include "third_party/fuzztest/src/fuzztest/fuzztest.h"` |
| 42 | 4. Add a `FUZZ_TEST` macro, which might be as simple as `FUZZ_TEST(MyApiTest, ExistingFunctionWhichTakesUntrustedInput)` |
| 43 | (though you may wish to structure things differently, see below) |
| 44 | 5. Run the unit tests and ensure they pass. |
| 45 | 6. Land the CL. |
Bo Majewski | 3bd1cb8 | 2023-06-13 05:30:12 | [diff] [blame] | 46 | |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 47 | That's it! |
Bo Majewski | 3bd1cb8 | 2023-06-13 05:30:12 | [diff] [blame] | 48 | |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 49 | This fuzzer will be built automatically, using various [sanitizers], and run |
| 50 | on our distributed fuzzing infrastructure [ClusterFuzz]. If it finds bugs, |
| 51 | they'll be reported back to you. |
Bo Majewski | 3bd1cb8 | 2023-06-13 05:30:12 | [diff] [blame] | 52 | |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 53 | More detail in all the following sections. |
| 54 | |
Adrian Taylor | 82c71d0 | 2023-10-30 19:13:43 | [diff] [blame] | 55 | ## Creating a new `FUZZ_TEST` target |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 56 | |
aizatsky | a6f8629 | 2016-03-18 00:22:24 | [diff] [blame] | 57 | ``` |
Adrian Taylor | 598c72c | 2023-11-02 09:10:54 | [diff] [blame] | 58 | import("//testing/test.gni") |
Adrian Taylor | 82c71d0 | 2023-10-30 19:13:43 | [diff] [blame] | 59 | |
Adrian Taylor | 714aaa4 | 2024-03-11 13:32:16 | [diff] [blame] | 60 | test("hypothetical_fuzztests") { |
| 61 | sources = [ "hypothetical_fuzztests.cc" ] |
aizatsky | a6f8629 | 2016-03-18 00:22:24 | [diff] [blame] | 62 | |
Andrew Paseltiner | b55d15f | 2024-03-12 15:00:16 | [diff] [blame] | 63 | fuzztests = ["MyApiTest.MyApiCanSuccessfullyParseAnyString"] |
Juan Jose Lopez Jaimez | c43c7c1 | 2022-07-26 22:33:05 | [diff] [blame] | 64 | |
Adrian Taylor | 714aaa4 | 2024-03-11 13:32:16 | [diff] [blame] | 65 | deps = [ |
| 66 | ":hypothetical_component", |
| 67 | "//third_party/fuzztest:fuzztest_gtest_main", |
| 68 | ] |
Max Moroz | eae4cef | 2018-11-06 00:26:28 | [diff] [blame] | 69 | } |
robert.bradford | 160c598 | 2016-08-30 17:04:30 | [diff] [blame] | 70 | ``` |
| 71 | |
Hubert Chao | 839f32aa | 2024-01-18 19:37:05 | [diff] [blame] | 72 | You may also need to add `third_party/fuzztest` to your DEPS file. |
| 73 | |
Adrian Taylor | 82c71d0 | 2023-10-30 19:13:43 | [diff] [blame] | 74 | ## Adding `FUZZ_TEST` support to a target |
| 75 | |
Adrian Taylor | c96daf2 | 2024-12-06 14:47:21 | [diff] [blame] | 76 | We also support adding `FUZZ_TEST`s alongside existing |
Adrian Taylor | a20bc5a | 2024-01-29 23:22:26 | [diff] [blame] | 77 | unit tests, even in the same .cc file. |
Adrian Taylor | 82c71d0 | 2023-10-30 19:13:43 | [diff] [blame] | 78 | |
| 79 | ``` |
Adrian Taylor | 5294aad | 2024-04-18 12:25:39 | [diff] [blame] | 80 | test("existing_unit_tests") { |
| 81 | sources = [ "existing_unit_tests.cc" ] # add FUZZ_TESTs here |
Adrian Taylor | 82c71d0 | 2023-10-30 19:13:43 | [diff] [blame] | 82 | |
Adrian Taylor | 5294aad | 2024-04-18 12:25:39 | [diff] [blame] | 83 | fuzztests = ["MyApiTest.ApiWorksAlways"] |
| 84 | # Add this! |
Adrian Taylor | 82c71d0 | 2023-10-30 19:13:43 | [diff] [blame] | 85 | |
Adrian Taylor | 5294aad | 2024-04-18 12:25:39 | [diff] [blame] | 86 | deps = [ |
| 87 | ":existing_component", |
| 88 | # Other stuff |
| 89 | ] |
Adrian Taylor | 82c71d0 | 2023-10-30 19:13:43 | [diff] [blame] | 90 | } |
| 91 | ``` |
Max Moroz | 9b37075 | 2018-03-20 22:05:32 | [diff] [blame] | 92 | |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 93 | This will: |
| 94 | * add a dependency on the appropriate fuzztest libraries; |
| 95 | * cause the target to be built on all our [fuzzer builders] |
| 96 | * construct metadata so that [ClusterFuzz] knows how to run the resulting |
| 97 | binary. |
Max Moroz | 9b37075 | 2018-03-20 22:05:32 | [diff] [blame] | 98 | |
Adrian Taylor | 82c71d0 | 2023-10-30 19:13:43 | [diff] [blame] | 99 | This relies on something, somewhere, calling `base::LaunchUnitTests` within |
| 100 | your executable to initialize FuzzTest. This should be the case already. |
| 101 | |
Adrian Taylor | f4c9616 | 2023-10-30 09:25:41 | [diff] [blame] | 102 | (If you have other code targets, such as `source_set`s, contributing to your |
| 103 | unit test target they may need to explicitly depend upon `//third_party/fuzztest` |
| 104 | too.) |
| 105 | |
Adrian Taylor | 82c71d0 | 2023-10-30 19:13:43 | [diff] [blame] | 106 | *** note |
Adrian Taylor | c96daf2 | 2024-12-06 14:47:21 | [diff] [blame] | 107 | **Note:** This may not yet work reliably in `browser_test`s. We're working on |
| 108 | it. |
Adrian Taylor | 82c71d0 | 2023-10-30 19:13:43 | [diff] [blame] | 109 | *** |
| 110 | |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 111 | ## Adding `FUZZ_TEST`s in the code |
Max Moroz | 9b37075 | 2018-03-20 22:05:32 | [diff] [blame] | 112 | |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 113 | First, `#include "third_party/fuzztest/src/fuzztest/fuzztest.h"`. |
Max Moroz | 9b37075 | 2018-03-20 22:05:32 | [diff] [blame] | 114 | |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 115 | Then, it's normal to create a function named after the thing you're trying to |
| 116 | prove, with assertions to prove it. |
Max Moroz | 9b37075 | 2018-03-20 22:05:32 | [diff] [blame] | 117 | |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 118 | For instance, |
Max Moroz | 9b37075 | 2018-03-20 22:05:32 | [diff] [blame] | 119 | |
| 120 | ``` |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 121 | void MyApiCanSuccessfullyParseAnyString(std::string input) { |
| 122 | bool success = MyApi(input); |
| 123 | EXPECT_TRUE(success); |
| 124 | } |
| 125 | ``` |
Max Moroz | 9b37075 | 2018-03-20 22:05:32 | [diff] [blame] | 126 | |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 127 | Then, declare the `FUZZ_TEST` macro: |
Max Moroz | 9b37075 | 2018-03-20 22:05:32 | [diff] [blame] | 128 | |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 129 | ``` |
| 130 | FUZZ_TEST(MyApiTest, MyApiCanSuccessfullyParseAnyString); |
| 131 | ``` |
| 132 | |
| 133 | Our fuzzing infrastructure will generate all possible strings and prove it works. |
| 134 | Obviously, that takes infinite time, so instead our fuzzing infrastructure will |
| 135 | carefully craft strings to explore more and more branches within `MyApi`, |
| 136 | mutating the input according to code coverage, so there's a good chance bugs |
| 137 | will be found quickly. |
| 138 | |
| 139 | Fuzzing should always be alongside traditional unit testing - never rely on it |
| 140 | to find all the bugs! It should be a backstop to prevent unexpected security |
| 141 | flaws sneaking past your regular testing. |
| 142 | |
| 143 | In more complex cases, you'll need to tell FuzzTest about the expected domains |
| 144 | of valid input. For example: |
| 145 | |
| 146 | ``` |
| 147 | void MyApiAlwaysSucceedsOnPositiveIntegers(int i) { |
| 148 | bool success = MyApi(i); |
| 149 | EXPECT_TRUE(success); |
| 150 | } |
| 151 | FUZZ_TEST(MyApiTest, MyApiAlwaysSucceedsOnPositiveIntegers) |
| 152 | .WithDomains(/*i:*/fuzztest::Positive<int>()); |
| 153 | ``` |
| 154 | |
| 155 | See the [FuzzTest reference] for all your options here. |
| 156 | |
| 157 | ## Running this locally |
| 158 | |
| 159 | Simply build and run your unit tests as normal. `FUZZ_TEST`s are supported only |
| 160 | on some platforms. If you're on such a platform, you'll see your fuzz test |
| 161 | run for one second: |
| 162 | |
| 163 | ``` |
| 164 | [==========] Running 1 test from 1 test suite. |
| 165 | [----------] Global test environment set-up. |
| 166 | [----------] 1 test from ScaleFuzz |
| 167 | [ RUN ] ApiTest.MyApiCanSuccessfullyParseAnyString |
| 168 | [ OK ] ApiTest.MyApiCanSuccessfullyParseAnyString (1000 ms) |
| 169 | [----------] 1 test from ScaleFuzz (1000 ms total) |
| 170 | |
| 171 | [----------] Global test environment tear-down |
| 172 | [==========] 1 test from 1 test suite ran. (1000 ms total) |
| 173 | [ PASSED ] 1 test. |
| 174 | ``` |
| 175 | |
| 176 | On other platforms, the test will be ignored. |
| 177 | |
Hubert Chao | 839f32aa | 2024-01-18 19:37:05 | [diff] [blame] | 178 | If you want to try actually fuzzing with FuzzTest, modify your gn arguments to |
| 179 | contain: |
| 180 | |
| 181 | ``` |
| 182 | enable_fuzztest_fuzz=true |
| 183 | is_component_build=false |
| 184 | ``` |
| 185 | |
| 186 | You can then run your unit test with the extra command line argument `--fuzz=`, |
| 187 | optionally specifying a test name. You'll see lots of output as it explores your |
| 188 | code: |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 189 | |
| 190 | ``` |
| 191 | [*] Corpus size: 1 | Edges covered: 73 | Fuzzing time: 1.60482ms | Total runs: 1.00e+00 | Runs/secs: 623 | Max stack usage: 0 |
| 192 | [*] Corpus size: 2 | Edges covered: 103 | Fuzzing time: 1.844ms | Total runs: 2.00e+00 | Runs/secs: 1084 | Max stack usage: 0 |
| 193 | [*] Corpus size: 3 | Edges covered: 111 | Fuzzing time: 2.747931ms | Total runs: 3.00e+00 | Runs/secs: 1091 | Max stack usage: 0 |
| 194 | [*] Corpus size: 4 | Edges covered: 135 | Fuzzing time: 2.92305ms | Total runs: 4.00e+00 | Runs/secs: 1368 | Max stack usage: 0 |
| 195 | [*] Corpus size: 5 | Edges covered: 173 | Fuzzing time: 3.35237ms | Total runs: 5.00e+00 | Runs/secs: 1491 | Max stack usage: 0 |
| 196 | [*] Corpus size: 6 | Edges covered: 178 | Fuzzing time: 4.15666ms | Total runs: 6.00e+00 | Runs/secs: 1443 | Max stack usage: 0 |
| 197 | ``` |
| 198 | |
| 199 | ("Edges covered") is how many different code blocks have been explored (that is, |
| 200 | sections between branches). Over time, you'll see it explore more and more until |
| 201 | it runs out of new edges to explore. |
| 202 | |
| 203 | ## Landing the CL |
| 204 | |
| 205 | Nothing special is required here! |
| 206 | |
| 207 | After a day or two, we should see [ClusterFuzz] starting to run your new fuzzer, |
| 208 | and it should be visible on [ClusterFuzz Fuzzer Stats]. Look for fuzzers starting |
Adrian Taylor | c96daf2 | 2024-12-06 14:47:21 | [diff] [blame] | 209 | with `centipede_` or 'libfuzzer_' and your test target's name. |
Adrian Taylor | 598c72c | 2023-11-02 09:10:54 | [diff] [blame] | 210 | |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 211 | Thanks very much for doing your part in making Chromium more secure! |
| 212 | |
| 213 | ## Unusual cases |
| 214 | |
| 215 | There are some situations where FuzzTests may not work. For example: |
| 216 | |
| 217 | * You need to run on platforms not currently supported by FuzzTest |
| 218 | * You need more structured input |
| 219 | * You need to mutate the input in a more precise way |
Adrian Taylor | e3fbcd46 | 2024-02-27 11:18:28 | [diff] [blame] | 220 | * Your fuzzer input is a single binary blob |
Adrian Taylor | 6a886ec6 | 2023-10-25 23:45:27 | [diff] [blame] | 221 | |
| 222 | In these cases, you may be best off creating a standalone fuzzer using our |
| 223 | older fuzzing technology, [libfuzzer]. There are further options beyond |
| 224 | that, e.g. uploading "black box" fuzzers to ClusterFuzz, or even running |
| 225 | fuzzers outside of ClusterFuzz which then upload results to ClusterFuzz |
| 226 | for triage and diagnosis. To explore any of those options, please discuss |
| 227 | with the fuzzing team (email [email protected] if you're outside Google). |
| 228 | |
| 229 | |
| 230 | [FuzzTest]: https://github.com/google/fuzztest#how-do-i-use-it |
| 231 | [libfuzzer]: getting_started_with_libfuzzer.md |
| 232 | [`test` template]: https://source.chromium.org/chromium/chromium/src/+/main:testing/test.gni?q=test.gni |
| 233 | [fuzzer builders]: https://ci.chromium.org/p/chromium/g/chromium.fuzz/console |
| 234 | [ClusterFuzz]: https://clusterfuzz.com/ |
| 235 | [FuzzTest reference]: https://github.com/google/fuzztest#how-do-i-use-it |
| 236 | [ClusterFuzz Fuzzer Stats]: https://clusterfuzz.com/fuzzer-stats/by-fuzzer/fuzzer/libFuzzer/job/libfuzzer_chrome_asan |