xref: /aosp_15_r20/external/libgsm/fuzzer/README.md (revision 8ec969cea971fe25ff2d3933a5a9f8504f8e86c9)
1*8ec969ceSTreehugger Robot# Fuzzer for libgsm codec
2*8ec969ceSTreehugger Robot
3*8ec969ceSTreehugger Robot## Plugin Design Considerations
4*8ec969ceSTreehugger RobotThe fuzzer plugin for GSM is designed based on the understanding of the
5*8ec969ceSTreehugger Robotcodec and tries to achieve the following:
6*8ec969ceSTreehugger Robot
7*8ec969ceSTreehugger Robot##### Maximize code coverage
8*8ec969ceSTreehugger RobotThe configuration parameters are not hardcoded, but instead selected based on
9*8ec969ceSTreehugger Robotincoming data. This ensures more code paths are reached by the fuzzer.
10*8ec969ceSTreehugger Robot
11*8ec969ceSTreehugger RobotGSM supports the following options:
12*8ec969ceSTreehugger Robot1. Verbosity (parameter name: `GSM_VERBOSE`)
13*8ec969ceSTreehugger Robot2. Fastness (parameter name: `GSM_OPT_FAST`)
14*8ec969ceSTreehugger Robot3. WAV49 Format (parameter name: `GSM_OPT_WAV49`)
15*8ec969ceSTreehugger Robot4. LTP Cut speed-up (encoder only) (parameter name: `GSM_OPT_LTP_CUT`)
16*8ec969ceSTreehugger Robot
17*8ec969ceSTreehugger Robot| Parameter| Valid Values| Configured Value|
18*8ec969ceSTreehugger Robot|------------- |-------------| ----- |
19*8ec969ceSTreehugger Robot| `GSM_VERBOSE` | 0. `Verbosity disabled` 1. `Verbosity enabled` | Bit 0 (LSB) of 1st byte of data. |
20*8ec969ceSTreehugger Robot| `GSM_OPT_FAST`   | 0. `Regular algorithm will be used` 1. `Faster version of the algorithm will be used`  | Bit 1 (LSB) of 1st byte of data. |
21*8ec969ceSTreehugger Robot| `GSM_OPT_LTP_CUT`   | 0. `Disable LTP cut-off optimization` 1. `Enable LTP cut-off optimization`  | Bit 2 (LSB) of 1st byte of data. |
22*8ec969ceSTreehugger Robot| `GSM_OPT_WAV49`   | 0. `Disable WAV49 output in the encoder.` 1. `Enable WAV49 format in the encoder.`  | Bit 3 (LSB) of 1st byte of data. |
23*8ec969ceSTreehugger Robot
24*8ec969ceSTreehugger RobotThis also ensures that the plugin is always deterministic for any given input.
25*8ec969ceSTreehugger Robot
26*8ec969ceSTreehugger Robot##### Maximize utilization of input data
27*8ec969ceSTreehugger RobotThe plugin feeds the entire input data to the codec using a loop.
28*8ec969ceSTreehugger Robot###### For the Decoder:
29*8ec969ceSTreehugger Robot * If the size of media file is less than 65 bytes, (65 - size) bytes are padded and then sent to
30*8ec969ceSTreehugger Robot the decoder so that decode happens for all the input files, regardless of their size.
31*8ec969ceSTreehugger Robot GSM decoder consumes alternating frames of 33 and 32 bytes, so 65 bytes of input are needed.
32*8ec969ceSTreehugger Robot * If the decode operation was successful, the input is advanced by the frame size
33*8ec969ceSTreehugger Robot * If the decode operation was un-successful, the input is still advanced by frame size so
34*8ec969ceSTreehugger Robotthat the fuzzer can proceed to feed the next frame.
35*8ec969ceSTreehugger Robot###### For the Encoder:
36*8ec969ceSTreehugger Robot * If the size of media file is less than 320 bytes(160 signed 16-bit words), bytes are padded and then sent to
37*8ec969ceSTreehugger Robot the encoder so that encode happens for all the input files, regardless of their size.
38*8ec969ceSTreehugger Robot GSM encoder consumes 160 signed 16-bit words, so 320 bytes of input are needed.
39*8ec969ceSTreehugger Robot * If the encode operation was successful, the input is advanced by the frame size
40*8ec969ceSTreehugger Robot * If the encode operation was un-successful, the input is still advanced by frame size so
41*8ec969ceSTreehugger Robotthat the fuzzer can proceed to feed the next frame.
42*8ec969ceSTreehugger Robot
43*8ec969ceSTreehugger RobotThis ensures that the plugin tolerates any kind of input (empty, huge,
44*8ec969ceSTreehugger Robotmalformed, etc) and doesnt `exit()` on any input and thereby increasing the
45*8ec969ceSTreehugger Robotchance of identifying vulnerabilities.
46*8ec969ceSTreehugger Robot
47*8ec969ceSTreehugger Robot## Build
48*8ec969ceSTreehugger Robot
49*8ec969ceSTreehugger RobotThis describes steps to build gsm_dec_fuzzer and gsm_enc_fuzzer binaries.
50*8ec969ceSTreehugger Robot
51*8ec969ceSTreehugger Robot### Android
52*8ec969ceSTreehugger Robot
53*8ec969ceSTreehugger Robot#### Steps to build
54*8ec969ceSTreehugger RobotBuild the fuzzer
55*8ec969ceSTreehugger Robot```
56*8ec969ceSTreehugger Robot  $ mm -j$(nproc) gsm_dec_fuzzer
57*8ec969ceSTreehugger Robot  $ mm -j$(nproc) gsm_enc_fuzzer
58*8ec969ceSTreehugger Robot```
59*8ec969ceSTreehugger Robot
60*8ec969ceSTreehugger Robot#### Steps to run
61*8ec969ceSTreehugger RobotCreate a directory CORPUS_DIR and copy some gsm files (decoder)/raw files (encoder) to that folder
62*8ec969ceSTreehugger RobotPush this directory to device.
63*8ec969ceSTreehugger Robot
64*8ec969ceSTreehugger RobotTo run on device
65*8ec969ceSTreehugger Robot```
66*8ec969ceSTreehugger Robot  $ adb sync data
67*8ec969ceSTreehugger Robot  $ adb shell /data/fuzz/arm64/gsm_dec_fuzzer/gsm_dec_fuzzer CORPUS_DIR
68*8ec969ceSTreehugger Robot  $ adb shell /data/fuzz/arm64/gsm_enc_fuzzer/gsm_enc_fuzzer CORPUS_DIR
69*8ec969ceSTreehugger Robot```
70*8ec969ceSTreehugger RobotTo run on host
71*8ec969ceSTreehugger Robot```
72*8ec969ceSTreehugger Robot  $ $ANDROID_HOST_OUT/fuzz/x86_64/gsm_dec_fuzzer/gsm_dec_fuzzer CORPUS_DIR
73*8ec969ceSTreehugger Robot  $ $ANDROID_HOST_OUT/fuzz/x86_64/gsm_enc_fuzzer/gsm_enc_fuzzer CORPUS_DIR
74*8ec969ceSTreehugger Robot```
75*8ec969ceSTreehugger Robot
76*8ec969ceSTreehugger Robot## References:
77*8ec969ceSTreehugger Robot * http://llvm.org/docs/LibFuzzer.html
78*8ec969ceSTreehugger Robot * https://github.com/google/oss-fuzz
79