Coverage Report

Created: 2026-01-10 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fuzzer_exo.cpp
Line
Count
Source
1
// Copyright 2020 Google Inc.
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 <assert.h>
16
#include <string>
17
18
#include "include/flac_parser.h"
19
20
#include <jni.h>
21
22
// #include <android/log.h>
23
24
#include <cassert>
25
#include <cstdlib>
26
#include <cstring>
27
28
#include "common.h"
29
30
#define LOG_TAG "FLACParser"
31
32
#define LITERAL_TO_STRING_INTERNAL(x) #x
33
#define LITERAL_TO_STRING(x) LITERAL_TO_STRING_INTERNAL(x)
34
35
8.31k
#define CHECK(x) if (!(x)) return 0;
36
37
const int endian = 1;
38
4.81k
#define isBigEndian() (*(reinterpret_cast<const char *>(&endian)) == 0)
39
40
// The FLAC parser calls our C++ static callbacks using C calling conventions,
41
// inside FLAC__stream_decoder_process_until_end_of_metadata
42
// and FLAC__stream_decoder_process_single.
43
// We immediately then call our corresponding C++ instance methods
44
// with the same parameter list, but discard redundant information.
45
46
FLAC__StreamDecoderReadStatus FLACParser::read_callback(
47
    const FLAC__StreamDecoder * /* decoder */, FLAC__byte buffer[],
48
62.3k
    size_t *bytes, void *client_data) {
49
62.3k
  return reinterpret_cast<FLACParser *>(client_data)
50
62.3k
      ->readCallback(buffer, bytes);
51
62.3k
}
52
53
FLAC__StreamDecoderSeekStatus FLACParser::seek_callback(
54
    const FLAC__StreamDecoder * /* decoder */,
55
20.0k
    FLAC__uint64 absolute_byte_offset, void *client_data) {
56
20.0k
  return reinterpret_cast<FLACParser *>(client_data)
57
20.0k
      ->seekCallback(absolute_byte_offset);
58
20.0k
}
59
60
FLAC__StreamDecoderTellStatus FLACParser::tell_callback(
61
    const FLAC__StreamDecoder * /* decoder */,
62
2.07M
    FLAC__uint64 *absolute_byte_offset, void *client_data) {
63
2.07M
  return reinterpret_cast<FLACParser *>(client_data)
64
2.07M
      ->tellCallback(absolute_byte_offset);
65
2.07M
}
66
67
FLAC__StreamDecoderLengthStatus FLACParser::length_callback(
68
    const FLAC__StreamDecoder * /* decoder */, FLAC__uint64 *stream_length,
69
0
    void *client_data) {
70
0
  return reinterpret_cast<FLACParser *>(client_data)
71
0
      ->lengthCallback(stream_length);
72
0
}
73
74
FLAC__bool FLACParser::eof_callback(const FLAC__StreamDecoder * /* decoder */,
75
62.3k
                                    void *client_data) {
76
62.3k
  return reinterpret_cast<FLACParser *>(client_data)->eofCallback();
77
62.3k
}
78
79
FLAC__StreamDecoderWriteStatus FLACParser::write_callback(
80
    const FLAC__StreamDecoder * /* decoder */, const FLAC__Frame *frame,
81
8.71k
    const FLAC__int32 *const buffer[], void *client_data) {
82
8.71k
  return reinterpret_cast<FLACParser *>(client_data)
83
8.71k
      ->writeCallback(frame, buffer);
84
8.71k
}
85
86
void FLACParser::metadata_callback(const FLAC__StreamDecoder * /* decoder */,
87
                                   const FLAC__StreamMetadata *metadata,
88
12.8k
                                   void *client_data) {
89
12.8k
  reinterpret_cast<FLACParser *>(client_data)->metadataCallback(metadata);
90
12.8k
}
91
92
void FLACParser::error_callback(const FLAC__StreamDecoder * /* decoder */,
93
                                FLAC__StreamDecoderErrorStatus status,
94
2.24M
                                void *client_data) {
95
2.24M
  reinterpret_cast<FLACParser *>(client_data)->errorCallback(status);
96
2.24M
}
97
98
// These are the corresponding callbacks with C++ calling conventions
99
100
FLAC__StreamDecoderReadStatus FLACParser::readCallback(FLAC__byte buffer[],
101
62.3k
                                                       size_t *bytes) {
102
62.3k
  size_t requested = *bytes;
103
62.3k
  ssize_t actual = mDataSource->readAt(mCurrentPos, buffer, requested);
104
62.3k
  if (0 > actual) {
105
0
    *bytes = 0;
106
0
    return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
107
62.3k
  } else if (0 == actual) {
108
25.3k
    *bytes = 0;
109
25.3k
    mEOF = true;
110
25.3k
    return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
111
36.9k
  } else {
112
36.9k
    assert(actual <= requested);
113
36.9k
    *bytes = actual;
114
36.9k
    mCurrentPos += actual;
115
36.9k
    return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
116
36.9k
  }
117
62.3k
}
118
119
FLAC__StreamDecoderSeekStatus FLACParser::seekCallback(
120
20.0k
    FLAC__uint64 absolute_byte_offset) {
121
20.0k
  mCurrentPos = absolute_byte_offset;
122
20.0k
  mEOF = false;
123
20.0k
  return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
124
20.0k
}
125
126
FLAC__StreamDecoderTellStatus FLACParser::tellCallback(
127
2.07M
    FLAC__uint64 *absolute_byte_offset) {
128
2.07M
  *absolute_byte_offset = mCurrentPos;
129
2.07M
  return FLAC__STREAM_DECODER_TELL_STATUS_OK;
130
2.07M
}
131
132
FLAC__StreamDecoderLengthStatus FLACParser::lengthCallback(
133
0
    FLAC__uint64 *stream_length) {
134
0
  return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
135
0
}
136
137
62.3k
FLAC__bool FLACParser::eofCallback() { return mEOF; }
138
139
FLAC__StreamDecoderWriteStatus FLACParser::writeCallback(
140
8.71k
    const FLAC__Frame *frame, const FLAC__int32 *const buffer[]) {
141
8.71k
  if (mWriteRequested) {
142
8.61k
    mWriteRequested = false;
143
    // FLAC parser doesn't free or realloc buffer until next frame or finish
144
8.61k
    mWriteHeader = frame->header;
145
8.61k
    mWriteBuffer = buffer;
146
8.61k
    mWriteCompleted = true;
147
8.61k
    return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
148
8.61k
  } else {
149
97
    return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
150
97
  }
151
8.71k
}
152
153
12.8k
void FLACParser::metadataCallback(const FLAC__StreamMetadata *metadata) {
154
12.8k
  switch (metadata->type) {
155
5.87k
    case FLAC__METADATA_TYPE_STREAMINFO:
156
5.87k
      if (!mStreamInfoValid) {
157
4.90k
        mStreamInfo = metadata->data.stream_info;
158
4.90k
        mStreamInfoValid = true;
159
4.90k
      } else {
160
962
        break;
161
962
      }
162
4.90k
      break;
163
4.90k
    case FLAC__METADATA_TYPE_SEEKTABLE:
164
253
      mSeekTable = &metadata->data.seek_table;
165
253
      break;
166
1.62k
    case FLAC__METADATA_TYPE_VORBIS_COMMENT:
167
1.62k
      if (!mVorbisCommentsValid) {
168
140
        FLAC__StreamMetadata_VorbisComment vorbisComment =
169
140
            metadata->data.vorbis_comment;
170
99.7k
        for (FLAC__uint32 i = 0; i < vorbisComment.num_comments; ++i) {
171
99.6k
          FLAC__StreamMetadata_VorbisComment_Entry vorbisCommentEntry =
172
99.6k
              vorbisComment.comments[i];
173
99.6k
          if (vorbisCommentEntry.entry != NULL) {
174
99.6k
            std::string comment(
175
99.6k
                reinterpret_cast<char *>(vorbisCommentEntry.entry),
176
99.6k
                vorbisCommentEntry.length);
177
99.6k
            mVorbisComments.push_back(comment);
178
99.6k
          }
179
99.6k
        }
180
140
        mVorbisCommentsValid = true;
181
1.48k
      } else {
182
1.48k
        break;
183
1.48k
      }
184
140
      break;
185
5.06k
    case FLAC__METADATA_TYPE_PICTURE: {
186
5.06k
      const FLAC__StreamMetadata_Picture *parsedPicture =
187
5.06k
          &metadata->data.picture;
188
5.06k
      FlacPicture picture;
189
5.06k
      picture.mimeType.assign(std::string(parsedPicture->mime_type));
190
5.06k
      picture.description.assign(
191
5.06k
          std::string((char *)parsedPicture->description));
192
5.06k
      picture.data.assign(parsedPicture->data,
193
5.06k
                          parsedPicture->data + parsedPicture->data_length);
194
5.06k
      picture.width = parsedPicture->width;
195
5.06k
      picture.height = parsedPicture->height;
196
5.06k
      picture.depth = parsedPicture->depth;
197
5.06k
      picture.colors = parsedPicture->colors;
198
5.06k
      picture.type = parsedPicture->type;
199
5.06k
      mPictures.push_back(picture);
200
5.06k
      mPicturesValid = true;
201
5.06k
      break;
202
1.62k
    }
203
0
    default:
204
0
      break;
205
12.8k
  }
206
12.8k
}
207
208
2.24M
void FLACParser::errorCallback(FLAC__StreamDecoderErrorStatus status) {
209
2.24M
  mErrorStatus = status;
210
2.24M
}
211
212
// Copy samples from FLAC native 32-bit non-interleaved to
213
// correct bit-depth (non-zero padded), interleaved.
214
// These are candidates for optimization if needed.
215
static void copyToByteArrayBigEndian(int8_t *dst, const int *const *src,
216
                                     unsigned bytesPerSample, unsigned nSamples,
217
0
                                     unsigned nChannels) {
218
0
  for (unsigned i = 0; i < nSamples; ++i) {
219
0
    for (unsigned c = 0; c < nChannels; ++c) {
220
      // point to the first byte of the source address
221
      // and then skip the first few bytes (most significant bytes)
222
      // depending on the bit depth
223
0
      const int8_t *byteSrc =
224
0
          reinterpret_cast<const int8_t *>(&src[c][i]) + 4 - bytesPerSample;
225
0
      memcpy(dst, byteSrc, bytesPerSample);
226
0
      dst = dst + bytesPerSample;
227
0
    }
228
0
  }
229
0
}
230
231
static void copyToByteArrayLittleEndian(int8_t *dst, const int *const *src,
232
                                        unsigned bytesPerSample,
233
8.31k
                                        unsigned nSamples, unsigned nChannels) {
234
9.82M
  for (unsigned i = 0; i < nSamples; ++i) {
235
20.6M
    for (unsigned c = 0; c < nChannels; ++c) {
236
      // with little endian, the most significant bytes will be at the end
237
      // copy the bytes in little endian will remove the most significant byte
238
      // so we are good here.
239
10.7M
      memcpy(dst, &(src[c][i]), bytesPerSample);
240
10.7M
      dst = dst + bytesPerSample;
241
10.7M
    }
242
9.82M
  }
243
8.31k
}
244
245
static void copyTrespass(int8_t * /* dst */, const int *const * /* src */,
246
                         unsigned /* bytesPerSample */, unsigned /* nSamples */,
247
0
                         unsigned /* nChannels */) {
248
0
  ;
249
0
}
250
251
// FLACParser
252
253
FLACParser::FLACParser(DataSource *source)
254
6.24k
    : mDataSource(source),
255
6.24k
      mCopy(copyTrespass),
256
6.24k
      mDecoder(NULL),
257
6.24k
      mCurrentPos(0LL),
258
6.24k
      mEOF(false),
259
6.24k
      mStreamInfoValid(false),
260
6.24k
      mSeekTable(NULL),
261
6.24k
      firstFrameOffset(0LL),
262
6.24k
      mVorbisCommentsValid(false),
263
6.24k
      mPicturesValid(false),
264
6.24k
      mWriteRequested(false),
265
6.24k
      mWriteCompleted(false),
266
6.24k
      mWriteBuffer(NULL),
267
6.24k
      mErrorStatus((FLAC__StreamDecoderErrorStatus)-1) {
268
6.24k
  memset(&mStreamInfo, 0, sizeof(mStreamInfo));
269
6.24k
  memset(&mWriteHeader, 0, sizeof(mWriteHeader));
270
6.24k
}
271
272
6.24k
FLACParser::~FLACParser() {
273
6.24k
  if (mDecoder != NULL) {
274
6.24k
    FLAC__stream_decoder_delete(mDecoder);
275
6.24k
    mDecoder = NULL;
276
6.24k
  }
277
6.24k
}
278
279
6.24k
bool FLACParser::init() {
280
  // setup libFLAC parser
281
6.24k
  mDecoder = FLAC__stream_decoder_new();
282
6.24k
  if (mDecoder == NULL) {
283
    // The new should succeed, since probably all it does is a malloc
284
    // that always succeeds in Android.  But to avoid dependence on the
285
    // libFLAC internals, we check and log here.
286
0
    return false;
287
0
  }
288
6.24k
  FLAC__stream_decoder_set_md5_checking(mDecoder, false);
289
6.24k
  FLAC__stream_decoder_set_metadata_ignore_all(mDecoder);
290
6.24k
  FLAC__stream_decoder_set_metadata_respond(mDecoder,
291
6.24k
                                            FLAC__METADATA_TYPE_STREAMINFO);
292
6.24k
  FLAC__stream_decoder_set_metadata_respond(mDecoder,
293
6.24k
                                            FLAC__METADATA_TYPE_SEEKTABLE);
294
6.24k
  FLAC__stream_decoder_set_metadata_respond(mDecoder,
295
6.24k
                                            FLAC__METADATA_TYPE_VORBIS_COMMENT);
296
6.24k
  FLAC__stream_decoder_set_metadata_respond(mDecoder,
297
6.24k
                                            FLAC__METADATA_TYPE_PICTURE);
298
6.24k
  FLAC__StreamDecoderInitStatus initStatus;
299
6.24k
  initStatus = FLAC__stream_decoder_init_stream(
300
6.24k
      mDecoder, read_callback, seek_callback, tell_callback, length_callback,
301
6.24k
      eof_callback, write_callback, metadata_callback, error_callback,
302
6.24k
      reinterpret_cast<void *>(this));
303
6.24k
  if (initStatus != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
304
    // A failure here probably indicates a programming error and so is
305
    // unlikely to happen. But we check and log here similarly to above.
306
0
    return false;
307
0
  }
308
6.24k
  return true;
309
6.24k
}
310
311
6.24k
bool FLACParser::decodeMetadata() {
312
  // parse all metadata
313
6.24k
  if (!FLAC__stream_decoder_process_until_end_of_metadata(mDecoder)) {
314
1.30k
    return false;
315
1.30k
  }
316
  // store first frame offset
317
4.93k
  FLAC__stream_decoder_get_decode_position(mDecoder, &firstFrameOffset);
318
319
4.93k
  if (mStreamInfoValid) {
320
    // check channel count
321
4.88k
    if (getChannels() == 0 || getChannels() > 8) {
322
0
      return false;
323
0
    }
324
    // check bit depth
325
4.88k
    switch (getBitsPerSample()) {
326
1.09k
      case 8:
327
2.02k
      case 16:
328
2.28k
      case 24:
329
4.81k
      case 32:
330
4.81k
        break;
331
68
      default:
332
68
        return false;
333
4.88k
    }
334
    // configure the appropriate copy function based on device endianness.
335
4.81k
    if (isBigEndian()) {
336
0
      mCopy = copyToByteArrayBigEndian;
337
4.81k
    } else {
338
4.81k
      mCopy = copyToByteArrayLittleEndian;
339
4.81k
    }
340
4.81k
  } else {
341
56
    return false;
342
56
  }
343
4.81k
  return true;
344
4.93k
}
345
346
13.1k
size_t FLACParser::readBuffer(void *output, size_t output_size) {
347
13.1k
  mWriteRequested = true;
348
13.1k
  mWriteCompleted = false;
349
350
13.1k
  if (!FLAC__stream_decoder_process_single(mDecoder)) {
351
827
    return -1;
352
827
  }
353
12.2k
  if (!mWriteCompleted) {
354
3.77k
    if (FLAC__stream_decoder_get_state(mDecoder) !=
355
3.77k
        FLAC__STREAM_DECODER_END_OF_STREAM) {
356
0
    }
357
3.77k
    return -1;
358
3.77k
  }
359
360
  // verify that block header keeps the promises made by STREAMINFO
361
8.52k
  unsigned blocksize = mWriteHeader.blocksize;
362
8.52k
  if (blocksize == 0 || blocksize > getMaxBlockSize()) {
363
38
    return -1;
364
38
  }
365
8.48k
  if (mWriteHeader.sample_rate != getSampleRate() ||
366
8.36k
      mWriteHeader.channels != getChannels() ||
367
8.33k
      mWriteHeader.bits_per_sample != getBitsPerSample()) {
368
169
    return -1;
369
169
  }
370
371
8.31k
  unsigned bytesPerSample = getBitsPerSample() >> 3;
372
8.31k
  size_t bufferSize = blocksize * getChannels() * bytesPerSample;
373
8.31k
  if (bufferSize > output_size) {
374
1
    return -1;
375
1
  }
376
377
  // copy PCM from FLAC write buffer to our media buffer, with interleaving.
378
8.31k
  (*mCopy)(reinterpret_cast<int8_t *>(output), mWriteBuffer, bytesPerSample,
379
8.31k
           blocksize, getChannels());
380
381
  // fill in buffer metadata
382
8.31k
  CHECK(mWriteHeader.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
383
384
8.31k
  return bufferSize;
385
8.31k
}
386
387
bool FLACParser::getSeekPositions(int64_t timeUs,
388
0
                                  std::array<int64_t, 4> &result) {
389
0
  if (!mSeekTable) {
390
0
    return false;
391
0
  }
392
393
0
  unsigned sampleRate = getSampleRate();
394
0
  int64_t totalSamples = getTotalSamples();
395
0
  int64_t targetSampleNumber = (timeUs * sampleRate) / 1000000LL;
396
0
  if (targetSampleNumber >= totalSamples) {
397
0
    targetSampleNumber = totalSamples - 1;
398
0
  }
399
400
0
  FLAC__StreamMetadata_SeekPoint* points = mSeekTable->points;
401
0
  unsigned length = mSeekTable->num_points;
402
403
0
  for (unsigned i = length; i != 0; i--) {
404
0
    int64_t sampleNumber = points[i - 1].sample_number;
405
0
    if (sampleNumber == -1) {  // placeholder
406
0
      continue;
407
0
    }
408
0
    if (sampleNumber <= targetSampleNumber) {
409
0
      result[0] = (sampleNumber * 1000000LL) / sampleRate;
410
0
      result[1] = firstFrameOffset + points[i - 1].stream_offset;
411
0
      if (sampleNumber == targetSampleNumber || i >= length ||
412
0
          points[i].sample_number == -1) {  // placeholder
413
        // exact seek, or no following non-placeholder seek point
414
0
        result[2] = result[0];
415
0
        result[3] = result[1];
416
0
      } else {
417
0
        result[2] = (points[i].sample_number * 1000000LL) / sampleRate;
418
0
        result[3] = firstFrameOffset + points[i].stream_offset;
419
0
      }
420
0
      return true;
421
0
    }
422
0
  }
423
0
  result[0] = 0;
424
0
  result[1] = firstFrameOffset;
425
0
  result[2] = 0;
426
0
  result[3] = firstFrameOffset;
427
0
  return true;
428
0
}
429
430
namespace {
431
432
  class FuzzDataSource : public DataSource {
433
    const uint8_t *data_;
434
    size_t size_;
435
436
   public:
437
6.24k
    FuzzDataSource(const uint8_t *data, size_t size) {
438
6.24k
      data_ = data;
439
6.24k
      size_ = size;
440
6.24k
    }
441
442
62.3k
    ssize_t readAt(off64_t offset, void *const data, size_t size) {
443
62.3k
      if (offset > size_)
444
0
        return -1;
445
62.3k
      size_t remaining = size_ - offset;
446
62.3k
      if (remaining < size)
447
51.4k
        size = remaining;
448
62.3k
      memcpy(data, data_ + offset, size);
449
62.3k
      return size;
450
62.3k
    }
451
  };
452
453
}  // namespace
454
455
// Fuzz FLAC format and instrument the result as exoplayer JNI would:
456
// https://github.com/google/ExoPlayer/blob/release-v2/extensions/flac/src/main/jni/
457
6.24k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
458
6.24k
  FuzzDataSource source(data, size);
459
6.24k
  FLACParser parser(&source);
460
461
  // Early parsing
462
6.24k
  if (!parser.init() || !parser.decodeMetadata())
463
1.43k
    return 0;
464
465
4.81k
  auto streamInfo = parser.getStreamInfo();
466
467
  // Similar implementation than ExoPlayer
468
4.81k
  int buffer_size = streamInfo.max_blocksize * streamInfo.channels * 2;
469
4.81k
  assert(buffer_size >= 0);  // Not expected
470
4.81k
  auto buffer = new uint8_t[buffer_size];
471
472
13.1k
  while (parser.readBuffer(buffer, buffer_size) < ((size_t)-1));
473
4.81k
  delete[] buffer;
474
475
4.81k
  return 0;
476
4.81k
}