2023-04-30 07:49:03 +00:00
|
|
|
# Fuzzing
|
|
|
|
Surrealdb maintains a set of fuzz testing harnesses that are managed by
|
|
|
|
[cargo-fuzz](https://github.com/rust-fuzz/cargo-fuzz).
|
|
|
|
|
|
|
|
To build and run the fuzzer we will need to;
|
2024-05-28 15:31:10 +00:00
|
|
|
- Install the nightly compiler
|
2023-04-30 07:49:03 +00:00
|
|
|
- Install cargo fuzz
|
|
|
|
- Build a fuzz friendly version of surrealdb with our harnesses
|
|
|
|
|
|
|
|
## Installing nightly
|
|
|
|
One of the key requirements for high-performance fuzzing is the ability
|
|
|
|
to collect code-coverage feedback at runtime. With the current stable
|
|
|
|
version of rustc we can't instrument our fuzz-harnesses with coverage feedback.
|
|
|
|
Because of this we need to use some of the more bleeding edge features
|
|
|
|
available in the nightly release.
|
|
|
|
|
|
|
|
## Installing cargo-fuzz
|
|
|
|
Full details on the different install options are available, in the
|
|
|
|
[cargo-fuzz book](https://rust-fuzz.github.io/book/cargo-fuzz/setup.html).
|
|
|
|
but for the sake of brevity you can just install the basics with the
|
|
|
|
command below.
|
|
|
|
|
2024-05-28 15:31:10 +00:00
|
|
|
`cargo +nightly install cargo-fuzz`
|
2023-04-30 07:49:03 +00:00
|
|
|
|
|
|
|
## Building the fuzzers
|
|
|
|
Now that we've install cargo-fuzz we can go ahead and build our fuzzers.
|
|
|
|
```
|
|
|
|
cd lib
|
|
|
|
# -O: Optimised build
|
|
|
|
# --debug-assertions: Catch common bugs, e.g. integer overflow.
|
2024-05-28 15:31:10 +00:00
|
|
|
cargo +nightly fuzz build -O --debug-assertions
|
2023-04-30 07:49:03 +00:00
|
|
|
````
|
|
|
|
|
|
|
|
## Running the fuzzer
|
|
|
|
Now that the fuzzer has successfully built we can actually run them. To
|
|
|
|
list the available fuzz harnesses we can use the command.
|
|
|
|
```
|
2024-05-28 15:31:10 +00:00
|
|
|
cargo +nightly fuzz list
|
2023-04-30 07:49:03 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Once we know what fuzzer (in this case fuzz_executor) we want to run we
|
|
|
|
can it using the command;
|
|
|
|
```
|
2024-05-28 15:31:10 +00:00
|
|
|
cargo +nightly fuzz run -O --debug-assertions fuzz_executor
|
2023-04-30 07:49:03 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
The previous command will run the fuzzer in libfuzzer's default mode,
|
|
|
|
which means as a single thread. If you would like to speed fuzzing
|
|
|
|
up we can make use of all cores, and use a dictionary file. e.g.
|
|
|
|
```
|
2023-09-28 09:17:29 +00:00
|
|
|
# -fork: Run N separate process fuzzing in parallel in this case we
|
2023-04-30 07:49:03 +00:00
|
|
|
# use nproc to match the number of processors on our local
|
|
|
|
# machine.
|
|
|
|
# -dict: Make use the fuzzer specific dictionary file.
|
2024-05-28 15:31:10 +00:00
|
|
|
cargo +nightly fuzz run -O --debug-assertions \
|
2023-04-30 07:49:03 +00:00
|
|
|
fuzz_executor -- -fork=$(nproc) \
|
|
|
|
-dict=fuzz/fuzz_targets/fuzz_executor.dict
|
2024-05-28 15:31:10 +00:00
|
|
|
```
|