Coverage Report

Created: 2026-02-26 06:40

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
9.66k
#define CHECK(x) if (!(x)) return 0;
36
37
const int endian = 1;
38
4.52k
#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
69.4k
    size_t *bytes, void *client_data) {
49
69.4k
  return reinterpret_cast<FLACParser *>(client_data)
50
69.4k
      ->readCallback(buffer, bytes);
51
69.4k
}
52
53
FLAC__StreamDecoderSeekStatus FLACParser::seek_callback(
54
    const FLAC__StreamDecoder * /* decoder */,
55
22.4k
    FLAC__uint64 absolute_byte_offset, void *client_data) {
56
22.4k
  return reinterpret_cast<FLACParser *>(client_data)
57
22.4k
      ->seekCallback(absolute_byte_offset);
58
22.4k
}
59
60
FLAC__StreamDecoderTellStatus FLACParser::tell_callback(
61
    const FLAC__StreamDecoder * /* decoder */,
62
2.55M
    FLAC__uint64 *absolute_byte_offset, void *client_data) {
63
2.55M
  return reinterpret_cast<FLACParser *>(client_data)
64
2.55M
      ->tellCallback(absolute_byte_offset);
65
2.55M
}
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
69.4k
                                    void *client_data) {
76
69.4k
  return reinterpret_cast<FLACParser *>(client_data)->eofCallback();
77
69.4k
}
78
79
FLAC__StreamDecoderWriteStatus FLACParser::write_callback(
80
    const FLAC__StreamDecoder * /* decoder */, const FLAC__Frame *frame,
81
10.0k
    const FLAC__int32 *const buffer[], void *client_data) {
82
10.0k
  return reinterpret_cast<FLACParser *>(client_data)
83
10.0k
      ->writeCallback(frame, buffer);
84
10.0k
}
85
86
void FLACParser::metadata_callback(const FLAC__StreamDecoder * /* decoder */,
87
                                   const FLAC__StreamMetadata *metadata,
88
11.9k
                                   void *client_data) {
89
11.9k
  reinterpret_cast<FLACParser *>(client_data)->metadataCallback(metadata);
90
11.9k
}
91
92
void FLACParser::error_callback(const FLAC__StreamDecoder * /* decoder */,
93
                                FLAC__StreamDecoderErrorStatus status,
94
2.84M
                                void *client_data) {
95
2.84M
  reinterpret_cast<FLACParser *>(client_data)->errorCallback(status);
96
2.84M
}
97
98
// These are the corresponding callbacks with C++ calling conventions
99
100
FLAC__StreamDecoderReadStatus FLACParser::readCallback(FLAC__byte buffer[],
101
69.4k
                                                       size_t *bytes) {
102
69.4k
  size_t requested = *bytes;
103
69.4k
  ssize_t actual = mDataSource->readAt(mCurrentPos, buffer, requested);
104
69.4k
  if (0 > actual) {
105
0
    *bytes = 0;
106
0
    return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
107
69.4k
  } else if (0 == actual) {
108
27.2k
    *bytes = 0;
109
27.2k
    mEOF = true;
110
27.2k
    return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
111
42.2k
  } else {
112
42.2k
    assert(actual <= requested);
113
42.2k
    *bytes = actual;
114
42.2k
    mCurrentPos += actual;
115
42.2k
    return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
116
42.2k
  }
117
69.4k
}
118
119
FLAC__StreamDecoderSeekStatus FLACParser::seekCallback(
120
22.4k
    FLAC__uint64 absolute_byte_offset) {
121
22.4k
  mCurrentPos = absolute_byte_offset;
122
22.4k
  mEOF = false;
123
22.4k
  return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
124
22.4k
}
125
126
FLAC__StreamDecoderTellStatus FLACParser::tellCallback(
127
2.55M
    FLAC__uint64 *absolute_byte_offset) {
128
2.55M
  *absolute_byte_offset = mCurrentPos;
129
2.55M
  return FLAC__STREAM_DECODER_TELL_STATUS_OK;
130
2.55M
}
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
69.4k
FLAC__bool FLACParser::eofCallback() { return mEOF; }
138
139
FLAC__StreamDecoderWriteStatus FLACParser::writeCallback(
140
10.0k
    const FLAC__Frame *frame, const FLAC__int32 *const buffer[]) {
141
10.0k
  if (mWriteRequested) {
142
9.96k
    mWriteRequested = false;
143
    // FLAC parser doesn't free or realloc buffer until next frame or finish
144
9.96k
    mWriteHeader = frame->header;
145
9.96k
    mWriteBuffer = buffer;
146
9.96k
    mWriteCompleted = true;
147
9.96k
    return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
148
9.96k
  } else {
149
89
    return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
150
89
  }
151
10.0k
}
152
153
11.9k
void FLACParser::metadataCallback(const FLAC__StreamMetadata *metadata) {
154
11.9k
  switch (metadata->type) {
155
5.23k
    case FLAC__METADATA_TYPE_STREAMINFO:
156
5.23k
      if (!mStreamInfoValid) {
157
4.61k
        mStreamInfo = metadata->data.stream_info;
158
4.61k
        mStreamInfoValid = true;
159
4.61k
      } else {
160
617
        break;
161
617
      }
162
4.61k
      break;
163
4.61k
    case FLAC__METADATA_TYPE_SEEKTABLE:
164
221
      mSeekTable = &metadata->data.seek_table;
165
221
      break;
166
2.24k
    case FLAC__METADATA_TYPE_VORBIS_COMMENT:
167
2.24k
      if (!mVorbisCommentsValid) {
168
140
        FLAC__StreamMetadata_VorbisComment vorbisComment =
169
140
            metadata->data.vorbis_comment;
170
121k
        for (FLAC__uint32 i = 0; i < vorbisComment.num_comments; ++i) {
171
120k
          FLAC__StreamMetadata_VorbisComment_Entry vorbisCommentEntry =
172
120k
              vorbisComment.comments[i];
173
120k
          if (vorbisCommentEntry.entry != NULL) {
174
120k
            std::string comment(
175
120k
                reinterpret_cast<char *>(vorbisCommentEntry.entry),
176
120k
                vorbisCommentEntry.length);
177
120k
            mVorbisComments.push_back(comment);
178
120k
          }
179
120k
        }
180
140
        mVorbisCommentsValid = true;
181
2.10k
      } else {
182
2.10k
        break;
183
2.10k
      }
184
140
      break;
185
4.28k
    case FLAC__METADATA_TYPE_PICTURE: {
186
4.28k
      const FLAC__StreamMetadata_Picture *parsedPicture =
187
4.28k
          &metadata->data.picture;
188
4.28k
      FlacPicture picture;
189
4.28k
      picture.mimeType.assign(std::string(parsedPicture->mime_type));
190
4.28k
      picture.description.assign(
191
4.28k
          std::string((char *)parsedPicture->description));
192
4.28k
      picture.data.assign(parsedPicture->data,
193
4.28k
                          parsedPicture->data + parsedPicture->data_length);
194
4.28k
      picture.width = parsedPicture->width;
195
4.28k
      picture.height = parsedPicture->height;
196
4.28k
      picture.depth = parsedPicture->depth;
197
4.28k
      picture.colors = parsedPicture->colors;
198
4.28k
      picture.type = parsedPicture->type;
199
4.28k
      mPictures.push_back(picture);
200
4.28k
      mPicturesValid = true;
201
4.28k
      break;
202
2.24k
    }
203
0
    default:
204
0
      break;
205
11.9k
  }
206
11.9k
}
207
208
2.84M
void FLACParser::errorCallback(FLAC__StreamDecoderErrorStatus status) {
209
2.84M
  mErrorStatus = status;
210
2.84M
}
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
9.66k
                                        unsigned nSamples, unsigned nChannels) {
234
11.0M
  for (unsigned i = 0; i < nSamples; ++i) {
235
23.3M
    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
12.2M
      memcpy(dst, &(src[c][i]), bytesPerSample);
240
12.2M
      dst = dst + bytesPerSample;
241
12.2M
    }
242
11.0M
  }
243
9.66k
}
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
5.91k
    : mDataSource(source),
255
5.91k
      mCopy(copyTrespass),
256
5.91k
      mDecoder(NULL),
257
5.91k
      mCurrentPos(0LL),
258
5.91k
      mEOF(false),
259
5.91k
      mStreamInfoValid(false),
260
5.91k
      mSeekTable(NULL),
261
5.91k
      firstFrameOffset(0LL),
262
5.91k
      mVorbisCommentsValid(false),
263
5.91k
      mPicturesValid(false),
264
5.91k
      mWriteRequested(false),
265
5.91k
      mWriteCompleted(false),
266
5.91k
      mWriteBuffer(NULL),
267
5.91k
      mErrorStatus((FLAC__StreamDecoderErrorStatus)-1) {
268
5.91k
  memset(&mStreamInfo, 0, sizeof(mStreamInfo));
269
5.91k
  memset(&mWriteHeader, 0, sizeof(mWriteHeader));
270
5.91k
}
271
272
5.91k
FLACParser::~FLACParser() {
273
5.91k
  if (mDecoder != NULL) {
274
5.91k
    FLAC__stream_decoder_delete(mDecoder);
275
5.91k
    mDecoder = NULL;
276
5.91k
  }
277
5.91k
}
278
279
5.91k
bool FLACParser::init() {
280
  // setup libFLAC parser
281
5.91k
  mDecoder = FLAC__stream_decoder_new();
282
5.91k
  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
5.91k
  FLAC__stream_decoder_set_md5_checking(mDecoder, false);
289
5.91k
  FLAC__stream_decoder_set_metadata_ignore_all(mDecoder);
290
5.91k
  FLAC__stream_decoder_set_metadata_respond(mDecoder,
291
5.91k
                                            FLAC__METADATA_TYPE_STREAMINFO);
292
5.91k
  FLAC__stream_decoder_set_metadata_respond(mDecoder,
293
5.91k
                                            FLAC__METADATA_TYPE_SEEKTABLE);
294
5.91k
  FLAC__stream_decoder_set_metadata_respond(mDecoder,
295
5.91k
                                            FLAC__METADATA_TYPE_VORBIS_COMMENT);
296
5.91k
  FLAC__stream_decoder_set_metadata_respond(mDecoder,
297
5.91k
                                            FLAC__METADATA_TYPE_PICTURE);
298
5.91k
  FLAC__StreamDecoderInitStatus initStatus;
299
5.91k
  initStatus = FLAC__stream_decoder_init_stream(
300
5.91k
      mDecoder, read_callback, seek_callback, tell_callback, length_callback,
301
5.91k
      eof_callback, write_callback, metadata_callback, error_callback,
302
5.91k
      reinterpret_cast<void *>(this));
303
5.91k
  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
5.91k
  return true;
309
5.91k
}
310
311
5.91k
bool FLACParser::decodeMetadata() {
312
  // parse all metadata
313
5.91k
  if (!FLAC__stream_decoder_process_until_end_of_metadata(mDecoder)) {
314
1.28k
    return false;
315
1.28k
  }
316
  // store first frame offset
317
4.63k
  FLAC__stream_decoder_get_decode_position(mDecoder, &firstFrameOffset);
318
319
4.63k
  if (mStreamInfoValid) {
320
    // check channel count
321
4.58k
    if (getChannels() == 0 || getChannels() > 8) {
322
0
      return false;
323
0
    }
324
    // check bit depth
325
4.58k
    switch (getBitsPerSample()) {
326
1.01k
      case 8:
327
1.91k
      case 16:
328
2.13k
      case 24:
329
4.52k
      case 32:
330
4.52k
        break;
331
54
      default:
332
54
        return false;
333
4.58k
    }
334
    // configure the appropriate copy function based on device endianness.
335
4.52k
    if (isBigEndian()) {
336
0
      mCopy = copyToByteArrayBigEndian;
337
4.52k
    } else {
338
4.52k
      mCopy = copyToByteArrayLittleEndian;
339
4.52k
    }
340
4.52k
  } else {
341
54
    return false;
342
54
  }
343
4.52k
  return true;
344
4.63k
}
345
346
14.1k
size_t FLACParser::readBuffer(void *output, size_t output_size) {
347
14.1k
  mWriteRequested = true;
348
14.1k
  mWriteCompleted = false;
349
350
14.1k
  if (!FLAC__stream_decoder_process_single(mDecoder)) {
351
798
    return -1;
352
798
  }
353
13.3k
  if (!mWriteCompleted) {
354
3.51k
    if (FLAC__stream_decoder_get_state(mDecoder) !=
355
3.51k
        FLAC__STREAM_DECODER_END_OF_STREAM) {
356
0
    }
357
3.51k
    return -1;
358
3.51k
  }
359
360
  // verify that block header keeps the promises made by STREAMINFO
361
9.87k
  unsigned blocksize = mWriteHeader.blocksize;
362
9.87k
  if (blocksize == 0 || blocksize > getMaxBlockSize()) {
363
39
    return -1;
364
39
  }
365
9.83k
  if (mWriteHeader.sample_rate != getSampleRate() ||
366
9.72k
      mWriteHeader.channels != getChannels() ||
367
9.68k
      mWriteHeader.bits_per_sample != getBitsPerSample()) {
368
172
    return -1;
369
172
  }
370
371
9.66k
  unsigned bytesPerSample = getBitsPerSample() >> 3;
372
9.66k
  size_t bufferSize = blocksize * getChannels() * bytesPerSample;
373
9.66k
  if (bufferSize > output_size) {
374
3
    return -1;
375
3
  }
376
377
  // copy PCM from FLAC write buffer to our media buffer, with interleaving.
378
9.66k
  (*mCopy)(reinterpret_cast<int8_t *>(output), mWriteBuffer, bytesPerSample,
379
9.66k
           blocksize, getChannels());
380
381
  // fill in buffer metadata
382
9.66k
  CHECK(mWriteHeader.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
383
384
9.66k
  return bufferSize;
385
9.66k
}
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
5.91k
    FuzzDataSource(const uint8_t *data, size_t size) {
438
5.91k
      data_ = data;
439
5.91k
      size_ = size;
440
5.91k
    }
441
442
69.4k
    ssize_t readAt(off64_t offset, void *const data, size_t size) {
443
69.4k
      if (offset > size_)
444
0
        return -1;
445
69.4k
      size_t remaining = size_ - offset;
446
69.4k
      if (remaining < size)
447
55.0k
        size = remaining;
448
69.4k
      memcpy(data, data_ + offset, size);
449
69.4k
      return size;
450
69.4k
    }
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
5.91k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
458
5.91k
  FuzzDataSource source(data, size);
459
5.91k
  FLACParser parser(&source);
460
461
  // Early parsing
462
5.91k
  if (!parser.init() || !parser.decodeMetadata())
463
1.39k
    return 0;
464
465
4.52k
  auto streamInfo = parser.getStreamInfo();
466
467
  // Similar implementation than ExoPlayer
468
4.52k
  int buffer_size = streamInfo.max_blocksize * streamInfo.channels * 2;
469
4.52k
  assert(buffer_size >= 0);  // Not expected
470
4.52k
  auto buffer = new uint8_t[buffer_size];
471
472
14.1k
  while (parser.readBuffer(buffer, buffer_size) < ((size_t)-1));
473
4.52k
  delete[] buffer;
474
475
4.52k
  return 0;
476
4.52k
}