/src/opus/tests/opus_projection_decoder_fuzzer.cc
Line | Count | Source |
1 | | // Copyright 2021 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <fuzzer/FuzzedDataProvider.h> |
16 | | #include <limits.h> |
17 | | #include <stddef.h> |
18 | | |
19 | | #include <array> |
20 | | #include <cmath> |
21 | | #include <memory> |
22 | | |
23 | | #include "../celt/mathops.h" |
24 | | #include "../celt/os_support.h" |
25 | | #include "opus.h" |
26 | | #include "opus_defines.h" |
27 | | #include "opus_projection.h" |
28 | | #include "opus_types.h" |
29 | | |
30 | | #include "opus_ossfuzz_utils.h" |
31 | | |
32 | | // Having a huge-size vastly reduces the fuzzer's speed |
33 | 56.2k | #define MAX_MATRIX_SIZE 4096 |
34 | | |
35 | 56.2k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
36 | 56.2k | FuzzedDataProvider fdp(data, size); |
37 | | |
38 | 56.2k | const int frame_size_ms_x2 = |
39 | 56.2k | fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240}); |
40 | 56.2k | const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48 |
41 | 56.2k | ARG_QEXT(96)}) * 1000; |
42 | 56.2k | const int frame_size = frame_size_ms_x2 * frequency / 2000; |
43 | | |
44 | | // The allowed number of channels is either n^2 or n^2 + 2 |
45 | 56.2k | opus_int32 nb_channels = pow(fdp.ConsumeIntegralInRange(0, 15), 2); |
46 | 56.2k | if (fdp.ConsumeBool()) { |
47 | 2.30k | nb_channels += 2; |
48 | 2.30k | } |
49 | 56.2k | const int streams = fdp.ConsumeIntegralInRange(0, 255); |
50 | 56.2k | const int coupled_streams = fdp.ConsumeIntegralInRange(0, streams); |
51 | | |
52 | 56.2k | const opus_int32 matrix_size = fdp.ConsumeIntegralInRange(1, MAX_MATRIX_SIZE); |
53 | 56.2k | unsigned char *matrix = (unsigned char *)opus_alloc(matrix_size); |
54 | 56.2k | if (matrix == NULL) { |
55 | 0 | return 0; |
56 | 0 | } |
57 | | |
58 | 56.2k | fdp.ConsumeData(matrix, matrix_size); |
59 | | |
60 | 56.2k | int err = OPUS_OK; |
61 | 56.2k | OpusProjectionDecoder *const decoder = opus_projection_decoder_create( |
62 | 56.2k | frequency, nb_channels, streams, coupled_streams, matrix, matrix_size, |
63 | 56.2k | &err); |
64 | 56.2k | opus_free(matrix); |
65 | 56.2k | if (decoder == nullptr || err != OPUS_OK) { |
66 | 976 | return 0; |
67 | 976 | } |
68 | 55.2k | bool use_float = fdp.ConsumeBool(); |
69 | 55.2k | bool use_fec = fdp.ConsumeBool(); |
70 | 55.2k | auto payload = fdp.ConsumeRemainingBytes<unsigned char>(); |
71 | 55.2k | const opus_int16 payload_size = |
72 | 55.2k | std::min((unsigned long)SHRT_MAX, payload.size()); |
73 | | |
74 | 55.2k | if (use_float) { |
75 | 19.7k | float *pcm = |
76 | 19.7k | (float *)opus_alloc(sizeof(float) * frame_size * nb_channels); |
77 | 19.7k | if (pcm == NULL) { |
78 | 0 | goto end; |
79 | 0 | } |
80 | | |
81 | 19.7k | const int foo = opus_projection_decode_float( |
82 | 19.7k | decoder, payload.data(), payload_size, pcm, frame_size, use_fec); |
83 | 19.7k | (void)foo; |
84 | 19.7k | opus_free(pcm); |
85 | 35.4k | } else { |
86 | 35.4k | opus_int16 *pcm = |
87 | 35.4k | (opus_int16 *)opus_alloc(sizeof(opus_int16) * frame_size * nb_channels); |
88 | 35.4k | if (pcm == NULL) { |
89 | 0 | goto end; |
90 | 0 | } |
91 | | |
92 | 35.4k | const int foo = opus_projection_decode( |
93 | 35.4k | decoder, payload.data(), payload_size, pcm, frame_size, use_fec); |
94 | 35.4k | (void)foo; |
95 | 35.4k | opus_free(pcm); |
96 | 35.4k | } |
97 | 55.2k | end: |
98 | 55.2k | opus_projection_decoder_destroy(decoder); |
99 | | |
100 | 55.2k | return 0; |
101 | 55.2k | } Line | Count | Source | 35 | 28.1k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { | 36 | 28.1k | FuzzedDataProvider fdp(data, size); | 37 | | | 38 | 28.1k | const int frame_size_ms_x2 = | 39 | 28.1k | fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240}); | 40 | 28.1k | const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48 | 41 | 28.1k | ARG_QEXT(96)}) * 1000; | 42 | 28.1k | const int frame_size = frame_size_ms_x2 * frequency / 2000; | 43 | | | 44 | | // The allowed number of channels is either n^2 or n^2 + 2 | 45 | 28.1k | opus_int32 nb_channels = pow(fdp.ConsumeIntegralInRange(0, 15), 2); | 46 | 28.1k | if (fdp.ConsumeBool()) { | 47 | 1.15k | nb_channels += 2; | 48 | 1.15k | } | 49 | 28.1k | const int streams = fdp.ConsumeIntegralInRange(0, 255); | 50 | 28.1k | const int coupled_streams = fdp.ConsumeIntegralInRange(0, streams); | 51 | | | 52 | 28.1k | const opus_int32 matrix_size = fdp.ConsumeIntegralInRange(1, MAX_MATRIX_SIZE); | 53 | 28.1k | unsigned char *matrix = (unsigned char *)opus_alloc(matrix_size); | 54 | 28.1k | if (matrix == NULL) { | 55 | 0 | return 0; | 56 | 0 | } | 57 | | | 58 | 28.1k | fdp.ConsumeData(matrix, matrix_size); | 59 | | | 60 | 28.1k | int err = OPUS_OK; | 61 | 28.1k | OpusProjectionDecoder *const decoder = opus_projection_decoder_create( | 62 | 28.1k | frequency, nb_channels, streams, coupled_streams, matrix, matrix_size, | 63 | 28.1k | &err); | 64 | 28.1k | opus_free(matrix); | 65 | 28.1k | if (decoder == nullptr || err != OPUS_OK) { | 66 | 488 | return 0; | 67 | 488 | } | 68 | 27.6k | bool use_float = fdp.ConsumeBool(); | 69 | 27.6k | bool use_fec = fdp.ConsumeBool(); | 70 | 27.6k | auto payload = fdp.ConsumeRemainingBytes<unsigned char>(); | 71 | 27.6k | const opus_int16 payload_size = | 72 | 27.6k | std::min((unsigned long)SHRT_MAX, payload.size()); | 73 | | | 74 | 27.6k | if (use_float) { | 75 | 9.89k | float *pcm = | 76 | 9.89k | (float *)opus_alloc(sizeof(float) * frame_size * nb_channels); | 77 | 9.89k | if (pcm == NULL) { | 78 | 0 | goto end; | 79 | 0 | } | 80 | | | 81 | 9.89k | const int foo = opus_projection_decode_float( | 82 | 9.89k | decoder, payload.data(), payload_size, pcm, frame_size, use_fec); | 83 | 9.89k | (void)foo; | 84 | 9.89k | opus_free(pcm); | 85 | 17.7k | } else { | 86 | 17.7k | opus_int16 *pcm = | 87 | 17.7k | (opus_int16 *)opus_alloc(sizeof(opus_int16) * frame_size * nb_channels); | 88 | 17.7k | if (pcm == NULL) { | 89 | 0 | goto end; | 90 | 0 | } | 91 | | | 92 | 17.7k | const int foo = opus_projection_decode( | 93 | 17.7k | decoder, payload.data(), payload_size, pcm, frame_size, use_fec); | 94 | 17.7k | (void)foo; | 95 | 17.7k | opus_free(pcm); | 96 | 17.7k | } | 97 | 27.6k | end: | 98 | 27.6k | opus_projection_decoder_destroy(decoder); | 99 | | | 100 | 27.6k | return 0; | 101 | 27.6k | } |
Line | Count | Source | 35 | 28.1k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { | 36 | 28.1k | FuzzedDataProvider fdp(data, size); | 37 | | | 38 | 28.1k | const int frame_size_ms_x2 = | 39 | 28.1k | fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240}); | 40 | 28.1k | const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48 | 41 | 28.1k | ARG_QEXT(96)}) * 1000; | 42 | 28.1k | const int frame_size = frame_size_ms_x2 * frequency / 2000; | 43 | | | 44 | | // The allowed number of channels is either n^2 or n^2 + 2 | 45 | 28.1k | opus_int32 nb_channels = pow(fdp.ConsumeIntegralInRange(0, 15), 2); | 46 | 28.1k | if (fdp.ConsumeBool()) { | 47 | 1.15k | nb_channels += 2; | 48 | 1.15k | } | 49 | 28.1k | const int streams = fdp.ConsumeIntegralInRange(0, 255); | 50 | 28.1k | const int coupled_streams = fdp.ConsumeIntegralInRange(0, streams); | 51 | | | 52 | 28.1k | const opus_int32 matrix_size = fdp.ConsumeIntegralInRange(1, MAX_MATRIX_SIZE); | 53 | 28.1k | unsigned char *matrix = (unsigned char *)opus_alloc(matrix_size); | 54 | 28.1k | if (matrix == NULL) { | 55 | 0 | return 0; | 56 | 0 | } | 57 | | | 58 | 28.1k | fdp.ConsumeData(matrix, matrix_size); | 59 | | | 60 | 28.1k | int err = OPUS_OK; | 61 | 28.1k | OpusProjectionDecoder *const decoder = opus_projection_decoder_create( | 62 | 28.1k | frequency, nb_channels, streams, coupled_streams, matrix, matrix_size, | 63 | 28.1k | &err); | 64 | 28.1k | opus_free(matrix); | 65 | 28.1k | if (decoder == nullptr || err != OPUS_OK) { | 66 | 488 | return 0; | 67 | 488 | } | 68 | 27.6k | bool use_float = fdp.ConsumeBool(); | 69 | 27.6k | bool use_fec = fdp.ConsumeBool(); | 70 | 27.6k | auto payload = fdp.ConsumeRemainingBytes<unsigned char>(); | 71 | 27.6k | const opus_int16 payload_size = | 72 | 27.6k | std::min((unsigned long)SHRT_MAX, payload.size()); | 73 | | | 74 | 27.6k | if (use_float) { | 75 | 9.89k | float *pcm = | 76 | 9.89k | (float *)opus_alloc(sizeof(float) * frame_size * nb_channels); | 77 | 9.89k | if (pcm == NULL) { | 78 | 0 | goto end; | 79 | 0 | } | 80 | | | 81 | 9.89k | const int foo = opus_projection_decode_float( | 82 | 9.89k | decoder, payload.data(), payload_size, pcm, frame_size, use_fec); | 83 | 9.89k | (void)foo; | 84 | 9.89k | opus_free(pcm); | 85 | 17.7k | } else { | 86 | 17.7k | opus_int16 *pcm = | 87 | 17.7k | (opus_int16 *)opus_alloc(sizeof(opus_int16) * frame_size * nb_channels); | 88 | 17.7k | if (pcm == NULL) { | 89 | 0 | goto end; | 90 | 0 | } | 91 | | | 92 | 17.7k | const int foo = opus_projection_decode( | 93 | 17.7k | decoder, payload.data(), payload_size, pcm, frame_size, use_fec); | 94 | 17.7k | (void)foo; | 95 | 17.7k | opus_free(pcm); | 96 | 17.7k | } | 97 | 27.6k | end: | 98 | 27.6k | opus_projection_decoder_destroy(decoder); | 99 | | | 100 | 27.6k | return 0; | 101 | 27.6k | } |
|