Coverage Report

Created: 2023-06-29 06:17

/src/flac/oss-fuzz/encoder_v2.cc
Line
Count
Source (jump to first uncovered line)
1
/* fuzzer_encoder_v2
2
 * Copyright (C) 2022-2023  Xiph.Org Foundation
3
 *
4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions
6
 * are met:
7
 *
8
 * - Redistributions of source code must retain the above copyright
9
 * notice, this list of conditions and the following disclaimer.
10
 *
11
 * - Redistributions in binary form must reproduce the above copyright
12
 * notice, this list of conditions and the following disclaimer in the
13
 * documentation and/or other materials provided with the distribution.
14
 *
15
 * - Neither the name of the Xiph.org Foundation nor the names of its
16
 * contributors may be used to endorse or promote products derived from
17
 * this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
 */
31
32
#include <cstdlib>
33
#include <cstring> /* for memcpy */
34
#include "FLAC/stream_encoder.h"
35
#include "FLAC/metadata.h"
36
extern "C" {
37
#include "share/private.h"
38
}
39
#include "common.h"
40
41
/* This C++ fuzzer uses the FLAC and not FLAC++ because the latter lacks a few
42
 * hidden functions like FLAC__stream_encoder_disable_constant_subframes. It
43
 * is still processed by a C++ compiler because that's what oss-fuzz expects */
44
45
46
static FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, uint32_t samples, uint32_t current_frame, void *client_data)
47
65.9k
{
48
65.9k
  (void)encoder, (void)buffer, (void)bytes, (void)samples, (void)current_frame, (void)client_data;
49
65.9k
  return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
50
65.9k
}
51
52
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
53
11.2k
{
54
11.2k
  FLAC__bool encoder_valid = true;
55
11.2k
  FLAC__StreamEncoder *encoder = 0;
56
11.2k
  FLAC__StreamEncoderState state;
57
11.2k
  FLAC__StreamMetadata *metadata[16] = {NULL};
58
11.2k
  unsigned num_metadata = 0;
59
11.2k
  FLAC__StreamMetadata_VorbisComment_Entry VorbisCommentField;
60
61
11.2k
  unsigned sample_rate, channels, bps;
62
11.2k
  uint64_t samples_estimate, samples_in_input;
63
11.2k
  unsigned compression_level, input_data_width, blocksize, max_lpc_order, qlp_coeff_precision, min_residual_partition_order, max_residual_partition_order, metadata_mask, instruction_set_disable_mask;
64
11.2k
  FLAC__bool ogg, write_to_file, interleaved;
65
66
11.2k
  FLAC__bool data_bools[24];
67
68
  /* Set alloc threshold. This check was added later and no spare config
69
   * bytes were left, so we're reusing the sample rate as that of little
70
   * consequence to the encoder and decoder except reading the frame header */
71
72
11.2k
  if(size < 3)
73
2
    return 0;
74
11.2k
  alloc_check_threshold = data[2];
75
11.2k
  alloc_check_counter = 0;
76
77
  /* allocate the encoder */
78
11.2k
  if((encoder = FLAC__stream_encoder_new()) == NULL) {
79
0
    fprintf(stderr, "ERROR: allocating encoder\n");
80
0
    return 1;
81
0
  }
82
83
  /* Use first 20 byte for configuration */
84
11.2k
  if(size < 20){
85
9
    FLAC__stream_encoder_delete(encoder);
86
9
    return 0;
87
9
  }
88
89
  /* First 3 byte for sample rate, 4th byte for channels, 5th byte for bps */
90
11.2k
  sample_rate = ((unsigned)data[0] << 16) + ((unsigned)data[1] << 8) + data[2];
91
11.2k
  channels = data[3];
92
11.2k
  bps = data[4];
93
94
  /* Number of samples estimate, format accepts 36-bit max */
95
11.2k
  samples_estimate = ((uint64_t)data[5] << 32) + ((unsigned)data[6] << 24) + ((unsigned)data[7] << 16) + ((unsigned)data[8] << 8) + data[9];
96
97
11.2k
  compression_level = data[10]&0b1111;
98
11.2k
  input_data_width = 1 + (data[10]>>4)%4;
99
11.2k
  samples_in_input = (size-20)/input_data_width;
100
11.2k
  blocksize = ((unsigned)data[11] << 8) + (unsigned)data[12];
101
11.2k
  max_lpc_order = data[13];
102
11.2k
  qlp_coeff_precision = data[14];
103
11.2k
  min_residual_partition_order = data[15] & 0b1111;
104
11.2k
  max_residual_partition_order = data[15] & 0b11110000;
105
11.2k
  metadata_mask = data[16];
106
11.2k
  instruction_set_disable_mask = data[17];
107
108
  /* Get array of bools from configuration */
109
191k
  for(int i = 0; i < 16; i++)
110
180k
    data_bools[i] = data[18+i/8] & (1 << (i % 8));
111
112
11.2k
  ogg = data_bools[0];
113
11.2k
  interleaved = data_bools[1];
114
11.2k
  write_to_file = data_bools[13];
115
116
  /* Set input and process parameters */
117
11.2k
  encoder_valid &= FLAC__stream_encoder_set_verify(encoder, data_bools[2]);
118
11.2k
  encoder_valid &= FLAC__stream_encoder_set_channels(encoder, channels);
119
11.2k
  encoder_valid &= FLAC__stream_encoder_set_bits_per_sample(encoder, bps);
120
11.2k
  encoder_valid &= FLAC__stream_encoder_set_sample_rate(encoder, sample_rate);
121
11.2k
  encoder_valid &= FLAC__stream_encoder_set_total_samples_estimate(encoder, samples_estimate);
122
11.2k
  encoder_valid &= FLAC__stream_encoder_disable_instruction_set(encoder, instruction_set_disable_mask);
123
11.2k
  encoder_valid &= FLAC__stream_encoder_set_limit_min_bitrate(encoder, data_bools[15]);
124
125
  /* Set compression related parameters */
126
11.2k
  encoder_valid &= FLAC__stream_encoder_set_compression_level(encoder, compression_level);
127
11.2k
  if(data_bools[3]){
128
    /* Bias towards regular compression levels */
129
8.60k
    encoder_valid &= FLAC__stream_encoder_set_blocksize(encoder, blocksize);
130
8.60k
    encoder_valid &= FLAC__stream_encoder_set_max_lpc_order(encoder, max_lpc_order);
131
8.60k
    encoder_valid &= FLAC__stream_encoder_set_qlp_coeff_precision(encoder, qlp_coeff_precision);
132
8.60k
    encoder_valid &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, min_residual_partition_order);
133
134
    /* With large inputs and expensive options enabled, the fuzzer can get *really* slow.
135
     * Some combinations can make the fuzzer timeout (>60 seconds). However, while combining
136
     * options makes the fuzzer slower, most options do not expose new code when combined.
137
     * Therefore, combining slow options is disabled for large inputs. Any input containing
138
     * more than 65536 * 2 samples (max blocksize, stereo) is considered large
139
     */
140
8.60k
    if(samples_in_input < (2*65536)) {
141
8.48k
      encoder_valid &= FLAC__stream_encoder_set_streamable_subset(encoder, data_bools[4]);
142
8.48k
      encoder_valid &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, data_bools[5]);
143
8.48k
      encoder_valid &= FLAC__stream_encoder_set_do_escape_coding(encoder, data_bools[6]);
144
8.48k
      encoder_valid &= FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, data_bools[7]);
145
      /* Combining model search, precision search and a high residual partition order is especially
146
       * expensive, so limit that even further. This high partition order can only be set on
147
       * large blocksize and with streamable subset disabled */
148
8.48k
      if(samples_in_input < (2 * 4609) || data_bools[4] || !data_bools[7] || !data_bools[5] || max_residual_partition_order < 9 || blocksize < 4609)
149
8.35k
        encoder_valid &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, max_residual_partition_order);
150
8.48k
    }
151
121
    else {
152
121
      if(!data_bools[4])
153
76
        encoder_valid &= FLAC__stream_encoder_set_streamable_subset(encoder, false);
154
45
      else if(data_bools[6])
155
23
        encoder_valid &= FLAC__stream_encoder_set_do_escape_coding(encoder, true);
156
22
      else if(data_bools[7])
157
20
        encoder_valid &= FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, true);
158
2
      else if(data_bools[5])
159
1
        encoder_valid &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, true);
160
121
    }
161
8.60k
    encoder_valid &= FLAC__stream_encoder_set_do_mid_side_stereo(encoder, data_bools[8]);
162
8.60k
    encoder_valid &= FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, data_bools[9]);
163
164
8.60k
    encoder_valid &= FLAC__stream_encoder_disable_constant_subframes(encoder, data_bools[10]);
165
8.60k
    encoder_valid &= FLAC__stream_encoder_disable_fixed_subframes(encoder, data_bools[11]);
166
8.60k
    encoder_valid &= FLAC__stream_encoder_disable_verbatim_subframes(encoder, data_bools[12]);
167
8.60k
  }
168
169
  /* Disable alloc check if requested */
170
11.2k
  if(encoder_valid && data_bools[14])
171
6.34k
    alloc_check_threshold = INT32_MAX;
172
173
  /* add metadata */
174
11.2k
  if(encoder_valid && (metadata_mask & 1)) {
175
104
    if((metadata[num_metadata] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_STREAMINFO)) == NULL)
176
0
      encoder_valid = false;
177
104
    else
178
104
      num_metadata++;
179
104
  }
180
11.2k
  if(encoder_valid && (metadata_mask & 2) && size > 21){
181
5.11k
    if((metadata[num_metadata] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL)
182
0
      encoder_valid = false;
183
5.11k
    else {
184
5.11k
      metadata[num_metadata++]->length = (((unsigned)data[20]) << 8) + (unsigned)(data[21]);
185
5.11k
    }
186
5.11k
  }
187
11.2k
  if(encoder_valid && (metadata_mask & 4) && size > 20){
188
5.11k
    FLAC__byte * application_data = (FLAC__byte *)malloc(size-20);
189
5.11k
    if(0 != application_data && ((metadata[num_metadata] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION)) == NULL))
190
0
      encoder_valid = false;
191
5.11k
    else {
192
5.11k
      memcpy(application_data,data+20,size-20);
193
5.11k
      FLAC__metadata_object_application_set_data(metadata[num_metadata++], application_data, size-20, 0);
194
5.11k
    }
195
5.11k
  }
196
11.2k
  if(encoder_valid && (metadata_mask & 8) && size > 25){
197
5.67k
    if((metadata[num_metadata] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE)) == NULL)
198
0
      encoder_valid = false;
199
5.67k
    else {
200
5.67k
      unsigned seekpoint_spacing = ((unsigned)data[22] << 8) + data[23];
201
5.67k
      unsigned total_samples_for_seekpoints = ((unsigned)data[24] << 8) + data[25];
202
5.67k
      FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(metadata[num_metadata++], seekpoint_spacing, total_samples_for_seekpoints);
203
5.67k
    }
204
5.67k
  }
205
11.2k
  if(encoder_valid && (metadata_mask & 16)){
206
4.16k
    if((metadata[num_metadata] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) != NULL) {
207
3.70k
      bool vorbiscomment_valid = true;
208
      /* Append a vorbis comment */
209
3.70k
      if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&VorbisCommentField, "COMMENTARY", "Nothing to 🤔 report"))
210
45
        vorbiscomment_valid = false;
211
3.66k
      else {
212
3.66k
        if(FLAC__metadata_object_vorbiscomment_append_comment(metadata[num_metadata], VorbisCommentField, false)) {
213
214
          /* Insert a vorbis comment at the first index */
215
3.56k
          if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&VorbisCommentField, "COMMENTARY", "Still nothing to report 🤔🤣"))
216
5
            vorbiscomment_valid = false;
217
3.56k
          else
218
3.56k
            if(!FLAC__metadata_object_vorbiscomment_insert_comment(metadata[num_metadata], 0, VorbisCommentField, false)) {
219
43
              free(VorbisCommentField.entry);
220
43
              vorbiscomment_valid = false;
221
43
            }
222
3.56k
        }
223
95
        else {
224
95
          free(VorbisCommentField.entry);
225
95
          vorbiscomment_valid = false;
226
95
        }
227
3.66k
      }
228
3.70k
      if(!vorbiscomment_valid) {
229
188
        FLAC__metadata_object_delete(metadata[num_metadata]);
230
188
        metadata[num_metadata] = 0;
231
188
      }
232
3.51k
      else
233
3.51k
        num_metadata++;
234
3.70k
    }
235
4.16k
  }
236
11.2k
  if(encoder_valid && (metadata_mask & 32)){
237
4.42k
    if((metadata[num_metadata] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET)) != NULL) {
238
4.42k
      if(!FLAC__metadata_object_cuesheet_insert_blank_track(metadata[num_metadata],0)) {
239
190
        FLAC__metadata_object_delete(metadata[num_metadata]);
240
190
        metadata[num_metadata] = 0;
241
190
      }
242
4.23k
      else {
243
4.23k
        if(!FLAC__metadata_object_cuesheet_track_insert_blank_index(metadata[num_metadata],0,0)) {
244
31
          FLAC__metadata_object_delete(metadata[num_metadata]);
245
31
          metadata[num_metadata] = 0;
246
31
        }
247
4.20k
        else {
248
4.20k
          metadata[num_metadata]->data.cue_sheet.tracks[0].number = 1;
249
4.20k
          num_metadata++;
250
4.20k
        }
251
4.23k
      }
252
4.42k
    }
253
4.42k
  }
254
11.2k
  if(encoder_valid && (metadata_mask & 64)){
255
3.82k
    if((metadata[num_metadata] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PICTURE)) != NULL) {
256
3.82k
      num_metadata++;
257
3.82k
    }
258
3.82k
  }
259
11.2k
  if(encoder_valid && (metadata_mask & 128)){
260
3.29k
    if((metadata[num_metadata] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_UNDEFINED)) != NULL) {
261
3.29k
      metadata[num_metadata]->length = 24;
262
3.29k
      metadata[num_metadata]->data.unknown.data = (FLAC__byte *)calloc(24, 1);
263
3.29k
      num_metadata++;
264
3.29k
    }
265
3.29k
  }
266
267
11.2k
  if(num_metadata && encoder_valid)
268
8.18k
      encoder_valid = FLAC__stream_encoder_set_metadata(encoder, metadata, num_metadata);
269
270
  /* initialize encoder */
271
11.2k
  if(encoder_valid) {
272
11.2k
    FLAC__StreamEncoderInitStatus init_status;
273
11.2k
    if(ogg)
274
4.24k
      if(write_to_file)
275
2.56k
        init_status = FLAC__stream_encoder_init_ogg_file(encoder, "/tmp/tmp.flac", NULL, NULL);
276
1.68k
      else
277
1.68k
        init_status = FLAC__stream_encoder_init_ogg_stream(encoder, NULL, write_callback, NULL, NULL, NULL, NULL);
278
7.02k
    else
279
7.02k
      if(write_to_file)
280
3.10k
        init_status = FLAC__stream_encoder_init_file(encoder, "/tmp/tmp.flac", NULL, NULL);
281
3.92k
      else
282
3.92k
        init_status = FLAC__stream_encoder_init_stream(encoder, write_callback, NULL, NULL, NULL, NULL);
283
11.2k
    if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
284
268
      encoder_valid = false;
285
268
    }
286
11.2k
  }
287
288
289
  /* send samples to encoder */
290
11.2k
  if(encoder_valid && size > (input_data_width*channels+26)) {
291
10.7k
    unsigned samples = (size - 26)/input_data_width/channels;
292
10.7k
    const uint8_t * pcm_data = data + 26;
293
10.7k
    int32_t * data_as_int32 = (int32_t *)malloc(4*samples*channels);
294
10.7k
    if(0 != data_as_int32){
295
73.4M
      for(unsigned i = 0; i < samples*channels; i++)
296
73.4M
        if(input_data_width == 1)
297
58.2M
          data_as_int32[i] = (int32_t)pcm_data[i] - 0x80;
298
15.1M
        else if(input_data_width == 2)
299
850k
          data_as_int32[i] = (((int32_t)pcm_data[i*2] << 8) + pcm_data[i*2+1]) - 0x8000;
300
14.3M
        else if(input_data_width == 3)
301
5.25M
          data_as_int32[i] = (((int32_t)pcm_data[i*3] << 16) + ((int32_t)pcm_data[i*3+1] << 8) + pcm_data[i*3+2]) - 0x800000;
302
9.06M
        else if(input_data_width == 4)
303
9.06M
          data_as_int32[i] = (((int64_t)pcm_data[i*4] << 24) + ((int32_t)pcm_data[i*4+1] << 16) + ((int32_t)pcm_data[i*4+2] << 8) + pcm_data[i*4+3]) - 0x80000000;
304
305
      /* feed samples to encoder */
306
10.7k
      if(interleaved)
307
4.20k
        encoder_valid = FLAC__stream_encoder_process_interleaved(encoder, data_as_int32, samples);
308
6.52k
      else {
309
6.52k
        encoder_valid = FLAC__stream_encoder_process(encoder, (const int32_t*[]){data_as_int32,
310
6.52k
                                                               data_as_int32+samples,
311
6.52k
                                                               data_as_int32+samples*2,
312
6.52k
                                                               data_as_int32+samples*3,
313
6.52k
                                                               data_as_int32+samples*4, data_as_int32+samples*5, data_as_int32+samples*6, data_as_int32+samples*7}, samples);
314
6.52k
      }
315
10.7k
      free(data_as_int32);
316
10.7k
    }
317
0
    else {
318
0
      encoder_valid = false;
319
0
    }
320
10.7k
  }
321
322
11.2k
  state = FLAC__stream_encoder_get_state(encoder);
323
11.2k
  if(!(state == FLAC__STREAM_ENCODER_OK ||
324
11.2k
       state == FLAC__STREAM_ENCODER_UNINITIALIZED ||
325
11.2k
       state == FLAC__STREAM_ENCODER_CLIENT_ERROR ||
326
11.2k
       ((state == FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR ||
327
67
               state == FLAC__STREAM_ENCODER_FRAMING_ERROR ||
328
67
         (state == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR &&
329
5
          FLAC__stream_encoder_get_verify_decoder_state(encoder) == FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR)) &&
330
67
        alloc_check_threshold < INT32_MAX))) {
331
0
    fprintf(stderr,"-----\nERROR: stream encoder returned %s\n-----\n",FLAC__stream_encoder_get_resolved_state_string(encoder));
332
0
    if(state == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA) {
333
0
      uint32_t frame_number, channel, sample_number;
334
0
      FLAC__int32 expected, got;
335
0
      FLAC__stream_encoder_get_verify_decoder_error_stats(encoder, NULL, &frame_number, &channel, &sample_number, &expected, &got);
336
0
      fprintf(stderr,"Frame number %d\nChannel %d\n Sample number %d\nExpected value %d\nGot %d\n", frame_number, channel, sample_number, expected, got);
337
0
    }
338
0
    abort();
339
0
  }
340
341
11.2k
  FLAC__stream_encoder_finish(encoder);
342
343
  /* now that encoding is finished, the metadata can be freed */
344
191k
  for(unsigned i = 0; i < 16; i++)
345
180k
    if(0 != metadata[i])
346
30.8k
      FLAC__metadata_object_delete(metadata[i]);
347
348
11.2k
  FLAC__stream_encoder_delete(encoder);
349
350
11.2k
  return 0;
351
11.2k
}
352