| 1 |
# Translation fuzzers |
| 2 |
|
| 3 |
There are 3 fuzzers related to translation: [`fuzz_translate`](fuzz_translate.c) and |
| 4 |
[`fuzz_translate_generic`](fuzz_translate_generic.c) target `lou_translateString`, |
| 5 |
[`fuzz_backtranslate`](fuzz_backtranslate.c) fuzzes `lou_backTranslateString`. |
| 6 |
|
| 7 |
`fuzz_translate` is run continuously through a Github workflow. `fuzz_translate_generic` |
| 8 |
and `fuzz_backtranslate` are run continously through OSS-fuzz. |
| 9 |
|
| 10 |
# Table fuzzer |
| 11 |
|
| 12 |
[`table_fuzzer`](table_fuzzer.cc) fuzzes the `compileFile` function. It is run continously |
| 13 |
through OSS-fuzz. |
| 14 |
|
| 15 |
# Configure Liblouis for fuzzing |
| 16 |
|
| 17 |
`./configure --with-fuzzer` will check if your are using clang and clang++ as compilers |
| 18 |
(by looking at `CC` and `CXX`) and enables generation of compilation instructions for the |
| 19 |
fuzzer targets (translation fuzzers only). The `--with-coverage` switch will add |
| 20 |
`-fprofile-instr-generate -fcoverage-mapping` to `AM_CPPFLAGS` (in |
| 21 |
[liblouis/Makefile.am](../../liblouis/Makefile.am) and |
| 22 |
[tests/fuzzing/Makefile.am](Makefile.am)). |
| 23 |
|
| 24 |
To configure and build the project with fuzzer and coverage: |
| 25 |
|
| 26 |
```sh |
| 27 |
./autogen.sh |
| 28 |
export CC=clang |
| 29 |
export CXX=clang++ |
| 30 |
./configure --with-coverage --with-fuzzer |
| 31 |
make -j8 |
| 32 |
``` |
| 33 |
|
| 34 |
Now you are able to run the fuzzers. |
| 35 |
|
| 36 |
# Run `fuzz_translate` |
| 37 |
|
| 38 |
fuzz_translate requires two parameters. With the `FUZZ_TABLE` environment variable you set |
| 39 |
the table(s) to fuzz. An input corpus can optionally be provided via the first argument. A |
| 40 |
corpus is a directory with files containing sample inputs that will be used by libfuzzer |
| 41 |
to craft the data passed to the fuzzing function. The idea is to keep corpus as minimal as |
| 42 |
possible. If you don't provide a corpus directory, libfuzzer generates random inputs. |
| 43 |
|
| 44 |
Here is an example: |
| 45 |
|
| 46 |
```sh |
| 47 |
# move to tests/fuzzing directory |
| 48 |
cd tests/fuzzing |
| 49 |
|
| 50 |
# we assume here you have added corpus files into the "CORPUS" directory |
| 51 |
export FUZZ_TABLE=../../tables/en-us-g2.ctb |
| 52 |
./fuzz_translate CORPUS/ |
| 53 |
|
| 54 |
# run the fuzzer using parallelization |
| 55 |
# you can also set more jobs than workers (stopped jobs will instantly be replaced by a new fuzzer process) |
| 56 |
./fuzz_translate CORPUS/ -workers=8 -jobs=8 |
| 57 |
|
| 58 |
# to start the fuzzer and let it generate random input (without corpus files): |
| 59 |
# here we don't parallelize the process |
| 60 |
./fuzz_translate |
| 61 |
``` |
| 62 |
|
| 63 |
After running the fuzzer multiple times with the same corpus, it might be possible that |
| 64 |
many corpus files added by the fuzzer explore the same code paths. Hopefully, libfuzzer |
| 65 |
allows you to minimize a corpus. There is a simple bash script that allows you to do that: |
| 66 |
|
| 67 |
```sh |
| 68 |
./minimize-corpus.sh CORPUS/ |
| 69 |
``` |
| 70 |
|
| 71 |
If you have added a PoC file in the corpus directory that you want to keep intact, change |
| 72 |
its extension to .txt and use the `--preserve-txt` switch: |
| 73 |
|
| 74 |
```sh |
| 75 |
./minimize-corpus.sh --preserve-txt CORPUS/ |
| 76 |
``` |
| 77 |
|
| 78 |
# Look at fuzzer coverage |
| 79 |
|
| 80 |
If you want to see what are the code parts that are explored by the fuzzer, you can use |
| 81 |
clang coverage. Configure with the `--with-overage` switch, run the fuzzer and show |
| 82 |
coverage data from the run with llvm tools. To be able to use the coverage data, you first |
| 83 |
need to compile the raw profile data file of the run. By default, this file is created |
| 84 |
after execution under the name of default.profraw but you can specify it with |
| 85 |
`LLVM_PROFILE_FILE`. Here is how to do that: |
| 86 |
|
| 87 |
``` |
| 88 |
export LLVM_PROFILE_FILE=fuzz_translate.profraw |
| 89 |
export FUZZ_TABLE=../../tables/en-us-g2.ctb |
| 90 |
./fuzz_translate CORPUS/ -workers=8 -jobs=8 |
| 91 |
|
| 92 |
# wait for a bit and press Ctrl+C |
| 93 |
|
| 94 |
# compile raw profile |
| 95 |
llvm-profdata merge -sparse fuzz_translate.profraw -o fuzz_translate.profdata |
| 96 |
|
| 97 |
# show coverage (red lines are the one wich are reached) |
| 98 |
llvm-cov show .libs/fuzz_translate -instr-profile=fuzz_translate.profdata |
| 99 |
``` |
| 100 |
|
| 101 |
# Continous integration |
| 102 |
|
| 103 |
## OSS-Fuzz |
| 104 |
|
| 105 |
OSS-Fuzz compiles and runs [`table_fuzzer`](table_fuzzer.cc), |
| 106 |
[`fuzz_translate_generic`](fuzz_translate_generic.c) and |
| 107 |
[`fuzz_backtranslate`](fuzz_backtranslate.c) using the Dockerfile and build.sh script from |
| 108 |
the [Liblouis OSS-Fuzz project |
| 109 |
folder](https://github.com/google/oss-fuzz/blob/master/projects/liblouis). The Dockerfile |
| 110 |
clones the Liblouis repository and build.sh invokes |
| 111 |
[tests/fuzzing/build.sh](test/fuzzing/build.sh). |
| 112 |
|
| 113 |
Reported issues can be found in Google's [Monorail issue |
| 114 |
tracker](https://bugs.chromium.org/p/oss-fuzz/issues/list?q=label%3AProj-liblouis). Note |
| 115 |
that some issues are only visible for the maintainers. |
| 116 |
|
| 117 |
### Reproducing OSS-Fuzz issues |
| 118 |
|
| 119 |
The [OSS-Fuzz |
| 120 |
documenation](https://google.github.io/oss-fuzz/advanced-topics/reproducing/#building-using-docker) |
| 121 |
explains how to reproduce OSS-Fuzz issues using the reproducer (or testcase) files and |
| 122 |
Docker. |
| 123 |
|
| 124 |
If you have configured Liblouis for fuzzing (with clang) you can also reproduce issues |
| 125 |
natively: |
| 126 |
|
| 127 |
```sh |
| 128 |
export CC=clang |
| 129 |
export CXX=clang++ |
| 130 |
# optional: enable 'address' and 'undefined' sanitizers in order to be able to reproduce ASAN and UBSAN crashes |
| 131 |
export CFLAGS="-fsanitize=address,undefined" |
| 132 |
./configure --with-fuzzer |
| 133 |
make |
| 134 |
tests/fuzzing/fuzz_translate_generic <path/to/testcase_file> |
| 135 |
``` |
| 136 |
|
| 137 |
## Github workflows |
| 138 |
|
| 139 |
### [`fuzzing.yml`](../../.github/workflows/fuzzing.yml) |
| 140 |
|
| 141 |
Builds and runs `fuzz_translate` for every table with extenion .utb or .ctb, and uploads |
| 142 |
possible crash PoC's as artifacts. This happens once a week. See [the list of previous |
| 143 |
runs](https://github.com/liblouis/liblouis/actions/workflows/fuzzing.yml). |
| 144 |
|
| 145 |
### [`cifuzz.yml`](../../.github/workflows/cifuzz.yml) |
| 146 |
|
| 147 |
This will do the same as OSS-Fuzz, but on Github's infrastructure. The idea is to be able |
| 148 |
to catch fuzzing issues in an earlier stage. Possible crash PoC's are uploaded as |
| 149 |
artifacts. The workflow is triggered on pull requests. See [the list of previous |
| 150 |
runs](https://github.com/liblouis/liblouis/actions/workflows/cifuzz.yml). |