README.md
1# Fuzzer for libavc decoder
2
3This describes steps to build avc_dec_fuzzer binary.
4
5## Linux x86/x64
6
7### Requirements
8- cmake (3.5 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 libavc using cmake
25```
26 $ CC=clang CXX=clang++ cmake ../ \
27 -DSANITIZE=fuzzer-no-link,address,signed-integer-overflow
28 $ make
29 ```
30Build the fuzzer
31```
32 $ clang++ -std=c++11 -fsanitize=fuzzer,address -I. -I../ -I../common \
33 -I../decoder -Wl,--start-group ../fuzzer/avc_dec_fuzzer.cpp \
34 -o ./avc_dec_fuzzer ./libavcdec.a -Wl,--end-group
35```
36
37### Steps to run
38Create a directory CORPUS_DIR and copy some elementary h264 files to that folder
39To run the fuzzer
40```
41$ ./avc_dec_fuzzer CORPUS_DIR
42```
43
44## Android
45
46### Steps to build
47Build the fuzzer
48```
49 $ SANITIZE_TARGET=address SANITIZE_HOST=address mmma -j$(nproc) \
50 external/libavc/fuzzer
51```
52
53### Steps to run
54Create a directory CORPUS_DIR and copy some elementary h264 files to that folder
55Push this directory to device.
56
57To run on device
58```
59 $ adb sync data
60 $ adb shell /data/fuzz/avc_dec_fuzzer CORPUS_DIR
61```
62To run on host
63```
64 $ $ANDROID_HOST_OUT/fuzz/avc_dec_fuzzer CORPUS_DIR
65```
66
67
68# Fuzzer for libavc encoder
69
70## Plugin Design Considerations
71The fuzzer plugin for AVC is designed based on the understanding of the
72codec and tries to achieve the following:
73
74##### Maximize code coverage
75The configuration parameters are not hardcoded, but instead selected based on
76incoming data. This ensures more code paths are reached by the fuzzer.
77
78AVC supports the following parameters:
791. Frame Width (parameter name: `u4_wd`)
802. Frame Height (parameter name: `u4_ht`)
813. Input color format (parameter name: `e_inp_color_fmt`)
824. Architecture type (parameter name: `e_arch`)
835. Rate control mode (parameter name: `e_rc_mode`)
846. Number of cores (parameter name: `u4_num_cores`)
857. Maximum B frames (parameter name: `u4_num_bframes`)
868. Encoder speed preset (parameter name: `u4_enc_speed_preset`)
879. enable constrained intra prediction (parameter name: `u4_constrained_intra_pred`)
8810. enable intra 4x4 (parameter name: `u4_enable_intra_4x4`)
8911. Qp for I frames (parameter name: `u4_i_qp`)
9012. Qp for P frames (parameter name: `u4_p_qp`)
9113. Qp for B frames (parameter name: `u4_b_qp`)
9214. Target Bitrate (parameter name: `u4_target_bitrate`)
9315. Intra refresh period in frames (parameter name: `u4_air_refresh_period`)
9416. Enable half pel ME (parameter name: `u4_enable_hpel`)
9517. Enable quarter pel ME (parameter name: `u4_enable_qpel`)
9618. ME speed preset (parameter name: `u4_me_speed_preset`)
9719. Adaptive intra refresh mode (parameter name: `e_air_mode`)
9820. Disable deblock level (parameter name: `u4_disable_deblock_level`)
9921. Max search range in X direction (parameter name: `u4_srch_rng_x`)
10022. Max search range in Y direction (parameter name: `u4_srch_rng_y`)
10123. I frame interval (parameter name: `u4_i_frm_interval`)
10224. IDR frame interval (parameter name: `u4_idr_frm_interval`)
10325. Enable mastering display color volume info (parameter name: `u1_sei_mdcv_params_present_flag`)
10426. Enable content light level info (parameter name: `u1_sei_cll_params_present_flag`)
10527. Enable ambient viewing environment info (parameter name: `u1_sei_ave_params_present_flag`)
10628. Enable content color volume info (parameter name: `u1_sei_ccv_params_present_flag`)
10729. Profile (parameter name: `e_profile`)
10830. Enable aspect_ratio info (parameter name: `u1_aspect_ratio_info_present_flag`)
10931. Enable NAL HRD parameters presence (parameter name: `u1_nal_hrd_parameters_present_flag`)
11032. Enable VCL HRD parameters presence (parameter name: `u1_vcl_hrd_parameters_present_flag`)
11133. Enable force IDR frame (parameter name: `mIsForceIdrEnabled`)
11234. Enable dynamic bitrate change (parameter name: `mIsDynamicBitRateChangeEnabled`)
11335. Enable dynamic framerate change (parameter name: `mIsDynamicFrameRateChangeEnabled`)
11436. Force IDR frame number (parameter name: `mForceIdrInterval`)
11537. Frame number for dynamic bitrate (parameter name: `mDynamicBitRateInterval`)
11638. Frame number for dynamic framerate (parameter name: `mDynamicFrameRateInterval`)
117
118| Parameter| Valid Values| Configured Value|
119|------------- |-------------| ----- |
120| `u4_wd` | In the range `0 to 10239` | All the bits of 1st and 2nd byte of data |
121| `u4_ht` | In the range `0 to 10239` | All the bits of 3rd and 4th byte of data |
122| `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 |
123| `e_arch` | 0. `ARCH_ARM_NONEON` 1. `ARCH_NA` | bit 0 and 1 of 6th byte of data |
124| `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 |
125| `u4_num_cores` | 0. `0` 1. `1` 2. `2` 3. `3`| bit 0 and 1 of 8th byte of data |
126| `u4_num_bframes` | In the range `0 to 7` | bit 0, 1 and 2 of 9th byte of data |
127| `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 |
128| `u4_constrained_intra_pred` | 0. `0` 1. `1` | bit 0 of 11th byte of data |
129| `u4_enable_intra_4x4` | 0. `0` 1. `1` | bit 0 of 12th byte of data |
130| `u4_i_qp` | In the range `4 to 51` | All the bits of 13th byte of data |
131| `u4_p_qp` | In the range `4 to 51` | All the bits of 14th byte of data |
132| `u4_b_qp` | In the range `4 to 51` | All the bits of 15th byte of data |
133| `u4_target_bitrate` | In the range `0 to 500000000` | All the bits of 16th and 17th byte of data |
134| `u4_target_bitrate` | In the range `0 to 255` | All the bits of 18th byte of data |
135| `u4_air_refresh_period` | In the range `1 to 256` | All the bits of 19th byte of data |
136| `u4_air_refresh_period` | In the range `1 to 256` | All the bits of 19th byte of data |
137| `u4_enable_hpel` | 0. `0` 1. `1` | bit 0 of 20th byte of data |
138| `u4_enable_qpel` | 0. `0` 1. `1` | bit 0 of 21st byte of data |
139| `u4_me_speed_preset` | 0. `0` 1. `50` 2. `75` 3. `100` | All the bits of 22nd byte of data modulus 4 |
140| `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 |
141| `u4_disable_deblock_level` | 0. `0` 1. `1` 2. `2` 3. `3` | bit 0 and 1 of 24th byte of data |
142| `u4_srch_rng_x` | In the range `0 to 255` | All the bits of 25th byte of data |
143| `u4_srch_rng_y` | In the range `0 to 255`| All the bits of 26th byte of data |
144| `u4_i_frm_interval` | In the range `1 to 256` | All the bits of 27th byte of data |
145| `u4_idr_frm_interval` | In the range `1 to 256` | All the bits of 28th byte of data |
146| `u1_sei_mdcv_params_present_flag` | 0. `0` 1. `1` | bit 0 of 29th byte of data |
147| `u1_sei_cll_params_present_flag` | 0. `0` 1. `1` | bit 0 of 30th byte of data |
148| `u1_sei_ave_params_present_flag` | 0. `0` 1. `1` | bit 0 of 31st byte of data |
149| `u1_sei_ccv_params_present_flag` | 0. `0` 1. `1` | bit 0 of 32nd byte of data |
150| `e_profile` | 0. `IV_PROFILE_BASE` 1. `IV_PROFILE_MAIN` | bit 0 and 1 of 33th byte of data modulus 2 |
151| `u1_aspect_ratio_info_present_flag` | 0. `0` 1. `1` | bit 0 of 34th byte of data |
152| `u1_nal_hrd_parameters_present_flag` | 0. `0` 1. `1` | bit 0 of 35th byte of data |
153| `u1_vcl_hrd_parameters_present_flag` | 0. `0` 1. `1` | bit 0 of 36th byte of data |
154| `mIsForceIdrEnabled` | 0. `0` 1. `1` | bit 0 of 37th byte of data |
155| `mIsDynamicBitRateChangeEnabled` | 0. `0` 1. `1` | bit 0 of 38th byte of data |
156| `mIsDynamicFrameRateChangeEnabled` | 0. `0` 1. `1` | bit 0 of 39th byte of data |
157| `mForceIdrInterval` | In the range `0 to 7` | bit 0, 1 and 2 of 40th byte of data |
158| `mDynamicBitRateInterval` | In the range `0 to 7` | bit 0, 1 and 2 of 41st byte of data |
159| `mDynamicFrameRateInterval` | In the range `0 to 7` | bit 0, 1 and 2 of 42nd byte of data |
160
161This also ensures that the plugin is always deterministic for any given input.
162
163##### Maximize utilization of input data
164The plugin feeds the entire input data to the codec using a loop.
165If the encode operation was successful, the input is advanced by the frame size.
166If the encode operation was un-successful, the input is still advanced by frame size so
167that the fuzzer can proceed to feed the next frame.
168
169This ensures that the plugin tolerates any kind of input (empty, huge,
170malformed, etc) and doesnt `exit()` on any input and thereby increasing the
171chance of identifying vulnerabilities.
172
173## Build
174
175This describes steps to build avc_enc_fuzzer binary.
176
177### Android
178
179#### Steps to build
180Build the fuzzer
181```
182 $ mm -j$(nproc) avc_enc_fuzzer
183```
184
185#### Steps to run
186Create a directory CORPUS_DIR and copy some yuv files to that folder
187Push this directory to device.
188
189To run on device
190```
191 $ adb sync data
192 $ adb shell /data/fuzz/arm64/avc_enc_fuzzer/avc_enc_fuzzer CORPUS_DIR
193```
194To run on host
195```
196 $ $ANDROID_HOST_OUT/fuzz/x86_64/avc_enc_fuzzer/avc_enc_fuzzer CORPUS_DIR
197```
198
199## References:
200 * http://llvm.org/docs/LibFuzzer.html
201 * https://github.com/google/oss-fuzz
202