Coverage Report

Created: 2025-07-18 06:27

/src/flac/oss-fuzz/encoder.cc
Line
Count
Source (jump to first uncovered line)
1
/* Copyright 2019 Guido Vranken
2
 *
3
 * Permission is hereby granted, free of charge, to any person obtaining
4
 * a copy of this software and associated documentation files (the
5
 * "Software"), to deal in the Software without restriction, including
6
 * without limitation the rights to use, copy, modify, merge, publish,
7
 * distribute, sublicense, and/or sell copies of the Software, and to
8
 * permit persons to whom the Software is furnished to do so, subject
9
 * to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be
12
 * included in all copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
 * SOFTWARE.
22
 */
23
24
#include <cstddef>
25
#include <cstdint>
26
#include <limits>
27
28
#include <fuzzing/datasource/datasource.hpp>
29
#include <fuzzing/memory.hpp>
30
31
#include "FLAC++/encoder.h"
32
#include "common.h"
33
34
namespace FLAC {
35
  namespace Encoder {
36
        class FuzzerStream : public Stream {
37
            private:
38
                // fuzzing::datasource::Datasource& ds;
39
            public:
40
                FuzzerStream(fuzzing::datasource::Datasource&) :
41
12.1k
                    Stream() { }
42
43
147k
                ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], size_t bytes, uint32_t /* samples */, uint32_t /* current_frame */) override {
44
147k
                    fuzzing::memory::memory_test(buffer, bytes);
45
#if 0
46
                    try {
47
                        if ( ds.Get<bool>() == true ) {
48
                          return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
49
                        }
50
                    } catch ( ... ) { }
51
#endif
52
147k
                    return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
53
147k
                }
54
        };
55
    }
56
}
57
58
12.1k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
59
12.1k
    fuzzing::datasource::Datasource ds(data, size);
60
12.1k
    FLAC::Encoder::FuzzerStream encoder(ds);
61
62
12.1k
    try {
63
12.1k
            const int channels = ds.Get<uint8_t>();
64
12.1k
            const int bps = ds.Get<uint8_t>();
65
12.1k
            encoder.set_channels(channels);
66
12.1k
            encoder.set_bits_per_sample(bps);
67
68
12.1k
        {
69
12.1k
            const bool res = encoder.set_streamable_subset(ds.Get<bool>());
70
12.1k
            fuzzing::memory::memory_test(res);
71
12.1k
        }
72
12.1k
        {
73
12.1k
            const bool res = encoder.set_ogg_serial_number(ds.Get<long>());
74
12.1k
            fuzzing::memory::memory_test(res);
75
12.1k
        }
76
12.1k
        {
77
12.1k
            const bool res = encoder.set_verify(ds.Get<bool>());
78
12.1k
            fuzzing::memory::memory_test(res);
79
12.1k
        }
80
12.1k
        {
81
12.1k
            const bool res = encoder.set_compression_level(ds.Get<uint8_t>());
82
12.1k
            fuzzing::memory::memory_test(res);
83
12.1k
        }
84
12.1k
        {
85
12.1k
            const bool res = encoder.set_do_exhaustive_model_search(ds.Get<bool>());
86
12.1k
            fuzzing::memory::memory_test(res);
87
12.1k
        }
88
12.1k
        {
89
12.1k
            const bool res = encoder.set_do_mid_side_stereo(ds.Get<bool>());
90
12.1k
            fuzzing::memory::memory_test(res);
91
12.1k
        }
92
12.1k
        {
93
12.1k
            const bool res = encoder.set_loose_mid_side_stereo(ds.Get<bool>());
94
12.1k
            fuzzing::memory::memory_test(res);
95
12.1k
        }
96
12.1k
        {
97
12.1k
            const auto s = ds.Get<std::string>();
98
12.1k
            const bool res = encoder.set_apodization(s.data());
99
12.1k
            fuzzing::memory::memory_test(res);
100
12.1k
        }
101
12.1k
        {
102
12.1k
            const bool res = encoder.set_max_lpc_order(ds.Get<uint8_t>());
103
12.1k
            fuzzing::memory::memory_test(res);
104
12.1k
        }
105
12.1k
        {
106
12.1k
            const bool res = encoder.set_qlp_coeff_precision(ds.Get<uint32_t>());
107
12.1k
            fuzzing::memory::memory_test(res);
108
12.1k
        }
109
12.1k
        {
110
12.1k
            const bool res = encoder.set_do_qlp_coeff_prec_search(ds.Get<bool>());
111
12.1k
            fuzzing::memory::memory_test(res);
112
12.1k
        }
113
12.1k
        {
114
12.1k
            const bool res = encoder.set_do_escape_coding(ds.Get<bool>());
115
12.1k
            fuzzing::memory::memory_test(res);
116
12.1k
        }
117
12.1k
        {
118
12.1k
            const bool res = encoder.set_min_residual_partition_order(ds.Get<uint32_t>());
119
12.1k
            fuzzing::memory::memory_test(res);
120
12.1k
        }
121
12.1k
        {
122
12.1k
            const bool res = encoder.set_max_residual_partition_order(ds.Get<uint32_t>());
123
12.1k
            fuzzing::memory::memory_test(res);
124
12.1k
        }
125
12.1k
        {
126
12.1k
            const bool res = encoder.set_rice_parameter_search_dist(ds.Get<uint32_t>());
127
12.1k
            fuzzing::memory::memory_test(res);
128
12.1k
        }
129
12.1k
        {
130
12.1k
            const bool res = encoder.set_total_samples_estimate(ds.Get<uint64_t>());
131
12.1k
            fuzzing::memory::memory_test(res);
132
12.1k
        }
133
12.1k
        {
134
12.1k
            const bool res = encoder.set_blocksize(ds.Get<uint16_t>());
135
12.1k
            fuzzing::memory::memory_test(res);
136
12.1k
        }
137
12.1k
        {
138
12.1k
            const bool res = encoder.set_limit_min_bitrate(ds.Get<bool>());
139
12.1k
            fuzzing::memory::memory_test(res);
140
12.1k
        }
141
12.1k
        {
142
12.1k
            const bool res = encoder.set_sample_rate(ds.Get<uint32_t>());
143
12.1k
            fuzzing::memory::memory_test(res);
144
12.1k
        }
145
12.1k
        {
146
12.1k
            const bool res = encoder.set_num_threads(ds.Get<uint32_t>());
147
12.1k
            fuzzing::memory::memory_test(res);
148
12.1k
        }
149
150
12.1k
        if ( size > 2 * 65535 * 4 ) {
151
            /* With large inputs and expensive options enabled, the fuzzer can get *really* slow.
152
            * Some combinations can make the fuzzer timeout (>60 seconds). However, while combining
153
            * options makes the fuzzer slower, most options do not expose new code when combined.
154
            * Therefore, combining slow options is disabled for large inputs. Any input containing
155
            * more than 65536 * 2 samples of 32 bits each (max blocksize, stereo) is considered large
156
            */
157
181
            encoder.set_do_qlp_coeff_prec_search(false);
158
181
            encoder.set_do_exhaustive_model_search(false);
159
181
        }
160
12.1k
        if ( size > 2 * 4096 * 4 + 250 ) {
161
            /* With subdivide_tukey in the mix testing apodizations can get really expensive. Therefore
162
             * this is disabled for inputs of more than one whole stereo block of 32-bit inputs plus a
163
             * bit of overhead */
164
1.38k
            encoder.set_apodization("");
165
1.38k
        }
166
167
12.1k
        {
168
12.1k
            ::FLAC__StreamEncoderInitStatus ret;
169
12.1k
            if ( ds.Get<bool>() ) {
170
6.71k
                ret = encoder.init();
171
6.71k
            } else {
172
5.46k
                ret = encoder.init_ogg();
173
5.46k
            }
174
175
12.1k
            if ( ret != FLAC__STREAM_ENCODER_INIT_STATUS_OK ) {
176
250
                goto end;
177
250
            }
178
12.1k
        }
179
180
  /* These sets must fail, because encoder is already initialized */
181
11.9k
        {
182
11.9k
            bool res = false;
183
11.9k
            res = res || encoder.set_streamable_subset(true);
184
11.9k
            res = res || encoder.set_ogg_serial_number(0);
185
11.9k
            res = res || encoder.set_verify(true);
186
11.9k
            res = res || encoder.set_compression_level(0);
187
11.9k
            res = res || encoder.set_do_exhaustive_model_search(true);
188
11.9k
            res = res || encoder.set_do_mid_side_stereo(true);
189
11.9k
            res = res || encoder.set_loose_mid_side_stereo(true);
190
11.9k
            res = res || encoder.set_apodization("test");
191
11.9k
            res = res || encoder.set_max_lpc_order(0);
192
11.9k
            res = res || encoder.set_qlp_coeff_precision(0);
193
11.9k
            res = res || encoder.set_do_qlp_coeff_prec_search(true);
194
11.9k
            res = res || encoder.set_do_escape_coding(true);
195
11.9k
            res = res || encoder.set_min_residual_partition_order(0);
196
11.9k
            res = res || encoder.set_max_residual_partition_order(0);
197
11.9k
            res = res || encoder.set_rice_parameter_search_dist(0);
198
11.9k
            res = res || encoder.set_total_samples_estimate(0);
199
11.9k
            res = res || encoder.set_channels(channels);
200
11.9k
            res = res || encoder.set_bits_per_sample(16);
201
11.9k
            res = res || encoder.set_limit_min_bitrate(true);
202
11.9k
            res = res || encoder.set_blocksize(3021);
203
11.9k
            res = res || encoder.set_sample_rate(44100);
204
11.9k
            res = res || (encoder.set_num_threads(4) == FLAC__STREAM_ENCODER_SET_NUM_THREADS_OK);
205
11.9k
            fuzzing::memory::memory_test(res);
206
11.9k
            if(res)
207
0
                abort();
208
11.9k
        }
209
210
211
11.9k
        {
212
            /* XORing values as otherwise compiler will optimize, apparently */
213
11.9k
            bool res = false;
214
11.9k
            res = res != encoder.get_streamable_subset();
215
11.9k
            res = res != encoder.get_verify();
216
11.9k
            res = res != encoder.get_do_exhaustive_model_search();
217
11.9k
            res = res != encoder.get_do_mid_side_stereo();
218
11.9k
            res = res != encoder.get_loose_mid_side_stereo();
219
11.9k
            res = res != encoder.get_max_lpc_order();
220
11.9k
            res = res != encoder.get_qlp_coeff_precision();
221
11.9k
            res = res != encoder.get_do_qlp_coeff_prec_search();
222
11.9k
            res = res != encoder.get_do_escape_coding();
223
11.9k
            res = res != encoder.get_min_residual_partition_order();
224
11.9k
            res = res != encoder.get_max_residual_partition_order();
225
11.9k
            res = res != encoder.get_rice_parameter_search_dist();
226
11.9k
            res = res != encoder.get_total_samples_estimate();
227
11.9k
            res = res != encoder.get_channels();
228
11.9k
            res = res != encoder.get_bits_per_sample();
229
11.9k
            res = res != encoder.get_limit_min_bitrate();
230
11.9k
            res = res != encoder.get_blocksize();
231
11.9k
            res = res != encoder.get_sample_rate();
232
11.9k
            res = res != encoder.get_num_threads();
233
11.9k
            fuzzing::memory::memory_test(res);
234
11.9k
        }
235
236
237
204k
        while ( ds.Get<bool>() ) {
238
192k
            {
239
192k
                auto dat = ds.GetVector<FLAC__int32>();
240
241
192k
                if( ds.Get<bool>() )
242
                    /* Mask */
243
22.1M
                    for (size_t i = 0; i < dat.size(); i++)
244
      /* If we get here, bps is 4 or larger, or init will have failed */
245
21.9M
                        dat[i] = (int32_t)(((uint32_t)(dat[i]) << (32-bps)) >> (32-bps));
246
247
192k
                const uint32_t samples = dat.size() / channels;
248
192k
                if ( samples > 0 ) {
249
164k
                    const int32_t* ptr = dat.data();
250
164k
                    const bool res = encoder.process_interleaved(ptr, samples);
251
164k
                    fuzzing::memory::memory_test(res);
252
164k
                }
253
192k
            }
254
192k
        }
255
11.9k
    } catch ( ... ) { }
256
257
12.1k
end:
258
12.1k
    {
259
12.1k
        const bool res = encoder.finish();
260
12.1k
        fuzzing::memory::memory_test(res);
261
12.1k
    }
262
12.1k
    return 0;
263
12.1k
}