1# Fuzzer for libavc decoder and encoder 2 3This describes steps to build avc_dec_fuzzer and avc_enc_fuzzer. 4 5## Linux x86/x64 6 7### Requirements 8- cmake (3.9.1 or above) 9- make 10- clang (6.0 or above) 11 needs to support -fsanitize=fuzzer, -fsanitize=fuzzer-no-link 12 13### Steps to build 14Clone libavc repository 15``` 16$ git clone https://android.googlesource.com/platform/external/libavc 17``` 18Create a directory inside libavc and change directory 19``` 20 $ cd libavc 21 $ mkdir build 22 $ cd build 23``` 24Build fuzzer with required sanitizers (-DSANITIZE=fuzzer-no-link is mandatory 25 to enable fuzzers) 26``` 27 $ cmake .. -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \ 28 -DCMAKE_BUILD_TYPE=Debug -DSANITIZE=fuzzer-no-link,address 29 $ make 30 ``` 31 32### Steps to run 33Create a directory CORPUS_DIR and copy some elementary h264 files 34(for avc_dec_fuzzer) or yuv files (for avc_enc_fuzzer) to that directory 35 36To run the fuzzers 37``` 38$ ./avc_dec_fuzzer CORPUS_DIR 39$ ./avc_enc_fuzzer CORPUS_DIR 40``` 41 42## Android 43 44### Steps to build 45Build the fuzzers 46``` 47 $ mm -j$(nproc) avc_dec_fuzzer 48 $ mm -j$(nproc) avc_enc_fuzzer 49``` 50 51### Steps to run 52Create a directory CORPUS_DIR and copy some elementary h264 files 53(for avc_dec_fuzzer) or yuv files (for avc_enc_fuzzer) to that folder 54Push this directory to device 55 56To run avc_dec_fuzzer on device 57``` 58 $ adb sync data 59 $ adb shell /data/fuzz/arm64/avc_dec_fuzzer/avc_dec_fuzzer CORPUS_DIR 60``` 61To run avc_enc_fuzzer on device 62``` 63 $ adb sync data 64 $ adb shell /data/fuzz/arm64/avc_enc_fuzzer/avc_enc_fuzzer CORPUS_DIR 65``` 66 67To run avc_dec_fuzzer on host 68``` 69 $ $ANDROID_HOST_OUT/fuzz/x86_64/avc_dec_fuzzer/avc_dec_fuzzer CORPUS_DIR 70``` 71 72To run avc_enc_fuzzer on host 73``` 74 $ $ANDROID_HOST_OUT/fuzz/x86_64/avc_enc_fuzzer/avc_enc_fuzzer CORPUS_DIR 75``` 76 77 78# Appendix 79## libavc encoder fuzzer 80 81## Plugin Design Considerations 82The fuzzer plugin for AVC is designed based on the understanding of the 83codec and tries to achieve the following: 84 85##### Maximize code coverage 86The configuration parameters are not hardcoded, but instead selected based on 87incoming data. This ensures more code paths are reached by the fuzzer. 88 89AVC supports the following parameters: 901. Frame Width (parameter name: `u4_wd`) 912. Frame Height (parameter name: `u4_ht`) 923. Input color format (parameter name: `e_inp_color_fmt`) 934. Architecture type (parameter name: `e_arch`) 945. Rate control mode (parameter name: `e_rc_mode`) 956. Number of cores (parameter name: `u4_num_cores`) 967. Maximum B frames (parameter name: `u4_num_bframes`) 978. Encoder speed preset (parameter name: `u4_enc_speed_preset`) 989. enable constrained intra prediction (parameter name: `u4_constrained_intra_pred`) 9910. enable intra 4x4 (parameter name: `u4_enable_intra_4x4`) 10011. Qp for I frames (parameter name: `u4_i_qp`) 10112. Qp for P frames (parameter name: `u4_p_qp`) 10213. Qp for B frames (parameter name: `u4_b_qp`) 10314. Target Bitrate (parameter name: `u4_target_bitrate`) 10415. Intra refresh period in frames (parameter name: `u4_air_refresh_period`) 10516. Enable half pel ME (parameter name: `u4_enable_hpel`) 10617. Enable quarter pel ME (parameter name: `u4_enable_qpel`) 10718. ME speed preset (parameter name: `u4_me_speed_preset`) 10819. Adaptive intra refresh mode (parameter name: `e_air_mode`) 10920. Disable deblock level (parameter name: `u4_disable_deblock_level`) 11021. Max search range in X direction (parameter name: `u4_srch_rng_x`) 11122. Max search range in Y direction (parameter name: `u4_srch_rng_y`) 11223. I frame interval (parameter name: `u4_i_frm_interval`) 11324. IDR frame interval (parameter name: `u4_idr_frm_interval`) 11425. Enable mastering display color volume info (parameter name: `u1_sei_mdcv_params_present_flag`) 11526. Enable content light level info (parameter name: `u1_sei_cll_params_present_flag`) 11627. Enable ambient viewing environment info (parameter name: `u1_sei_ave_params_present_flag`) 11728. Enable content color volume info (parameter name: `u1_sei_ccv_params_present_flag`) 11829. Profile (parameter name: `e_profile`) 11930. Enable aspect_ratio info (parameter name: `u1_aspect_ratio_info_present_flag`) 12031. Enable NAL HRD parameters presence (parameter name: `u1_nal_hrd_parameters_present_flag`) 12132. Enable VCL HRD parameters presence (parameter name: `u1_vcl_hrd_parameters_present_flag`) 12233. Enable force IDR frame (parameter name: `mIsForceIdrEnabled`) 12334. Enable dynamic bitrate change (parameter name: `mIsDynamicBitRateChangeEnabled`) 12435. Enable dynamic framerate change (parameter name: `mIsDynamicFrameRateChangeEnabled`) 12536. Force IDR frame number (parameter name: `mForceIdrInterval`) 12637. Frame number for dynamic bitrate (parameter name: `mDynamicBitRateInterval`) 12738. Frame number for dynamic framerate (parameter name: `mDynamicFrameRateInterval`) 128 129| Parameter| Valid Values| Configured Value| 130|------------- |-------------| ----- | 131| `u4_wd` | In the range `0 to 10239` | All the bits of 1st and 2nd byte of data | 132| `u4_ht` | In the range `0 to 10239` | All the bits of 3rd and 4th byte of data | 133| `e_inp_color_fmt` | 0. `IV_YUV_420P` 1. `IV_YUV_420SP_UV` 2. `IV_YUV_422ILE` 3. `IV_YUV_420SP_VU` | All the bits of 5th byte of data | 134| `e_arch` | 0. `ARCH_ARM_NONEON` 1. `ARCH_NA` | bit 0 and 1 of 6th byte of data | 135| `e_rc_mode` | 0. `IVE_RC_NONE` 1. `IVE_RC_STORAGE` 2. `IVE_RC_CBR_NON_LOW_DELAY` 3. `IVE_RC_CBR_LOW_DELAY` | All the bits of 7th byte of data modulus 4 | 136| `u4_num_cores` | 0. `0` 1. `1` 2. `2` 3. `3`| bit 0 and 1 of 8th byte of data | 137| `u4_num_bframes` | In the range `0 to 7` | bit 0, 1 and 2 of 9th byte of data | 138| `u4_enc_speed_preset` | 0. `IVE_CONFIG` 1. `IVE_SLOWEST` 2. `IVE_NORMAL` 3. `IVE_FAST` 4. `IVE_HIGH_SPEED` 5. `IVE_FASTEST` | All the bits of 10th byte of data modulus 6 | 139| `u4_constrained_intra_pred` | 0. `0` 1. `1` | bit 0 of 11th byte of data | 140| `u4_enable_intra_4x4` | 0. `0` 1. `1` | bit 0 of 12th byte of data | 141| `u4_i_qp` | In the range `4 to 51` | All the bits of 13th byte of data | 142| `u4_p_qp` | In the range `4 to 51` | All the bits of 14th byte of data | 143| `u4_b_qp` | In the range `4 to 51` | All the bits of 15th byte of data | 144| `u4_target_bitrate` | In the range `0 to 500000000` | All the bits of 16th and 17th byte of data | 145| `u4_target_bitrate` | In the range `0 to 255` | All the bits of 18th byte of data | 146| `u4_air_refresh_period` | In the range `1 to 256` | All the bits of 19th byte of data | 147| `u4_air_refresh_period` | In the range `1 to 256` | All the bits of 19th byte of data | 148| `u4_enable_hpel` | 0. `0` 1. `1` | bit 0 of 20th byte of data | 149| `u4_enable_qpel` | 0. `0` 1. `1` | bit 0 of 21st byte of data | 150| `u4_me_speed_preset` | 0. `0` 1. `50` 2. `75` 3. `100` | All the bits of 22nd byte of data modulus 4 | 151| `e_air_mode` | 0. `IVE_AIR_MODE_NONE` 1. `IVE_AIR_MODE_CYCLIC` 2. `IVE_AIR_MODE_RANDOM` | All the bits of 23rd byte of data modulus 3 | 152| `u4_disable_deblock_level` | 0. `0` 1. `1` 2. `2` 3. `3` | bit 0 and 1 of 24th byte of data | 153| `u4_srch_rng_x` | In the range `0 to 255` | All the bits of 25th byte of data | 154| `u4_srch_rng_y` | In the range `0 to 255`| All the bits of 26th byte of data | 155| `u4_i_frm_interval` | In the range `1 to 256` | All the bits of 27th byte of data | 156| `u4_idr_frm_interval` | In the range `1 to 256` | All the bits of 28th byte of data | 157| `u1_sei_mdcv_params_present_flag` | 0. `0` 1. `1` | bit 0 of 29th byte of data | 158| `u1_sei_cll_params_present_flag` | 0. `0` 1. `1` | bit 0 of 30th byte of data | 159| `u1_sei_ave_params_present_flag` | 0. `0` 1. `1` | bit 0 of 31st byte of data | 160| `u1_sei_ccv_params_present_flag` | 0. `0` 1. `1` | bit 0 of 32nd byte of data | 161| `e_profile` | 0. `IV_PROFILE_BASE` 1. `IV_PROFILE_MAIN` | bit 0 and 1 of 33th byte of data modulus 2 | 162| `u1_aspect_ratio_info_present_flag` | 0. `0` 1. `1` | bit 0 of 34th byte of data | 163| `u1_nal_hrd_parameters_present_flag` | 0. `0` 1. `1` | bit 0 of 35th byte of data | 164| `u1_vcl_hrd_parameters_present_flag` | 0. `0` 1. `1` | bit 0 of 36th byte of data | 165| `mIsForceIdrEnabled` | 0. `0` 1. `1` | bit 0 of 37th byte of data | 166| `mIsDynamicBitRateChangeEnabled` | 0. `0` 1. `1` | bit 0 of 38th byte of data | 167| `mIsDynamicFrameRateChangeEnabled` | 0. `0` 1. `1` | bit 0 of 39th byte of data | 168| `mForceIdrInterval` | In the range `0 to 7` | bit 0, 1 and 2 of 40th byte of data | 169| `mDynamicBitRateInterval` | In the range `0 to 7` | bit 0, 1 and 2 of 41st byte of data | 170| `mDynamicFrameRateInterval` | In the range `0 to 7` | bit 0, 1 and 2 of 42nd byte of data | 171 172This also ensures that the plugin is always deterministic for any given input. 173 174##### Maximize utilization of input data 175The plugin feeds the entire input data to the codec using a loop. 176If the encode operation was successful, the input is advanced by the frame size. 177If the encode operation was un-successful, the input is still advanced by frame size so 178that the fuzzer can proceed to feed the next frame. 179 180This ensures that the plugin tolerates any kind of input (empty, huge, 181malformed, etc) and doesnt `exit()` on any input and thereby increasing the 182chance of identifying vulnerabilities. 183 184 185## References: 186 * http://llvm.org/docs/LibFuzzer.html 187 * https://github.com/google/oss-fuzz 188