1*89a0ef05SAndroid Build Coastguard Worker## Building fuzzers for libultrahdr 2*89a0ef05SAndroid Build Coastguard Worker 3*89a0ef05SAndroid Build Coastguard Worker### Requirements 4*89a0ef05SAndroid Build Coastguard Worker 5*89a0ef05SAndroid Build Coastguard Worker- Refer [Requirements](./building.md#Requirements) 6*89a0ef05SAndroid Build Coastguard Worker 7*89a0ef05SAndroid Build Coastguard Worker- Additionally compilers are required to support options `-fsanitize=fuzzer, -fsanitize=fuzzer-no-link`. 8*89a0ef05SAndroid Build Coastguard Worker For instance, `clang 12` (or later) 9*89a0ef05SAndroid Build Coastguard Worker 10*89a0ef05SAndroid Build Coastguard Worker### Building Commands 11*89a0ef05SAndroid Build Coastguard Worker 12*89a0ef05SAndroid Build Coastguard Worker```sh 13*89a0ef05SAndroid Build Coastguard Workercmake -G Ninja ../ -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DUHDR_BUILD_FUZZERS=1 14*89a0ef05SAndroid Build Coastguard Workerninja 15*89a0ef05SAndroid Build Coastguard Worker``` 16*89a0ef05SAndroid Build Coastguard Worker 17*89a0ef05SAndroid Build Coastguard WorkerThis will generate the following files under `build_directory`: 18*89a0ef05SAndroid Build Coastguard Worker 19*89a0ef05SAndroid Build Coastguard Worker**ultrahdr_enc_fuzzer** - ultrahdr encoder fuzzer <br> 20*89a0ef05SAndroid Build Coastguard Worker**ultrahdr_dec_fuzzer** - ultrahdr decoder fuzzer <br> 21*89a0ef05SAndroid Build Coastguard Worker 22*89a0ef05SAndroid Build Coastguard WorkerAdditionally, while building fuzzers, user can enable sanitizers by providing desired 23*89a0ef05SAndroid Build Coastguard Workersanitizer option(s) through `UHDR_SANITIZE_OPTIONS`. 24*89a0ef05SAndroid Build Coastguard Worker 25*89a0ef05SAndroid Build Coastguard WorkerTo enable ASan, 26*89a0ef05SAndroid Build Coastguard Worker 27*89a0ef05SAndroid Build Coastguard Worker```sh 28*89a0ef05SAndroid Build Coastguard Workercmake -G Ninja ../ -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DUHDR_BUILD_FUZZERS=1 -DUHDR_SANITIZE_OPTIONS=address 29*89a0ef05SAndroid Build Coastguard Workerninja 30*89a0ef05SAndroid Build Coastguard Worker``` 31*89a0ef05SAndroid Build Coastguard Worker 32*89a0ef05SAndroid Build Coastguard WorkerTo enable MSan, 33*89a0ef05SAndroid Build Coastguard Worker 34*89a0ef05SAndroid Build Coastguard Worker```sh 35*89a0ef05SAndroid Build Coastguard Workercmake -G Ninja ../ -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DUHDR_BUILD_FUZZERS=1 -DUHDR_SANITIZE_OPTIONS=memory 36*89a0ef05SAndroid Build Coastguard Workerninja 37*89a0ef05SAndroid Build Coastguard Worker``` 38*89a0ef05SAndroid Build Coastguard WorkerTo enable TSan, 39*89a0ef05SAndroid Build Coastguard Worker 40*89a0ef05SAndroid Build Coastguard Worker```sh 41*89a0ef05SAndroid Build Coastguard Workercmake -G Ninja ../ -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DUHDR_BUILD_FUZZERS=1 -DUHDR_SANITIZE_OPTIONS=thread 42*89a0ef05SAndroid Build Coastguard Workerninja 43*89a0ef05SAndroid Build Coastguard Worker``` 44*89a0ef05SAndroid Build Coastguard Worker 45*89a0ef05SAndroid Build Coastguard WorkerTo enable UBSan, 46*89a0ef05SAndroid Build Coastguard Worker 47*89a0ef05SAndroid Build Coastguard Worker```sh 48*89a0ef05SAndroid Build Coastguard Workercmake -G Ninja ../ -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DUHDR_BUILD_FUZZERS=1 -DUHDR_SANITIZE_OPTIONS=undefined 49*89a0ef05SAndroid Build Coastguard Workerninja 50*89a0ef05SAndroid Build Coastguard Worker``` 51*89a0ef05SAndroid Build Coastguard Worker 52*89a0ef05SAndroid Build Coastguard WorkerUBSan can be grouped with ASan, MSan or TSan. 53*89a0ef05SAndroid Build Coastguard Worker 54*89a0ef05SAndroid Build Coastguard WorkerFor example, to enable ASan and UBSan, 55*89a0ef05SAndroid Build Coastguard Worker 56*89a0ef05SAndroid Build Coastguard Worker```sh 57*89a0ef05SAndroid Build Coastguard Workercmake -G Ninja ../ -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DUHDR_BUILD_FUZZERS=1 -DUHDR_SANITIZE_OPTIONS=address,undefined 58*89a0ef05SAndroid Build Coastguard Workerninja 59*89a0ef05SAndroid Build Coastguard Worker``` 60*89a0ef05SAndroid Build Coastguard Worker 61*89a0ef05SAndroid Build Coastguard Worker### Running 62*89a0ef05SAndroid Build Coastguard Worker 63*89a0ef05SAndroid Build Coastguard WorkerTo run the fuzzer(s), first create a corpus directory that holds the initial 64*89a0ef05SAndroid Build Coastguard Worker"seed" sample inputs. For decoder fuzzer, ultrahdr jpeg images can be used and 65*89a0ef05SAndroid Build Coastguard Workerfor encoder fuzzer, sample yuv files can be used. 66*89a0ef05SAndroid Build Coastguard Worker 67*89a0ef05SAndroid Build Coastguard WorkerThen run the fuzzers on the corpus directory. 68*89a0ef05SAndroid Build Coastguard Worker 69*89a0ef05SAndroid Build Coastguard Worker```sh 70*89a0ef05SAndroid Build Coastguard Workermkdir CORPUS_DIR 71*89a0ef05SAndroid Build Coastguard Workercp seeds/* CORPUS_DIR 72*89a0ef05SAndroid Build Coastguard Worker./ultrahdr_dec_fuzzer CORPUS_DIR 73*89a0ef05SAndroid Build Coastguard Worker./ultrahdr_enc_fuzzer CORPUS_DIR 74*89a0ef05SAndroid Build Coastguard Worker``` 75