1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
/* Copyright 2019 Guido Vranken
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <cstddef>
#include <cstdint>
#include <fuzzing/datasource/datasource.hpp>
#include <fuzzing/memory.hpp>
#include "FLAC++/decoder.h"
#include "FLAC++/metadata.h"
#include "common.h"
template <> FLAC__MetadataType fuzzing::datasource::Base::Get<FLAC__MetadataType>(const uint64_t id) {
(void)id;
switch ( Get<uint8_t>() ) {
case 0:
return FLAC__METADATA_TYPE_STREAMINFO;
case 1:
return FLAC__METADATA_TYPE_PADDING;
case 2:
return FLAC__METADATA_TYPE_APPLICATION;
case 3:
return FLAC__METADATA_TYPE_SEEKTABLE;
case 4:
return FLAC__METADATA_TYPE_VORBIS_COMMENT;
case 5:
return FLAC__METADATA_TYPE_CUESHEET;
case 6:
return FLAC__METADATA_TYPE_PICTURE;
case 7:
return FLAC__METADATA_TYPE_UNDEFINED;
case 8:
return FLAC__MAX_METADATA_TYPE;
default:
return FLAC__METADATA_TYPE_STREAMINFO;
}
}
namespace FLAC {
namespace Decoder {
class FuzzerStream : public Stream {
private:
fuzzing::datasource::Datasource& ds;
public:
FuzzerStream(fuzzing::datasource::Datasource& dsrc) :
Stream(), ds(dsrc) { }
::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes) override {
try {
const size_t maxCopySize = *bytes;
if ( maxCopySize > 0 ) {
/* memset just to test if this overwrites anything, and triggers ASAN */
memset(buffer, 0, maxCopySize);
}
const auto data = ds.GetData(0);
const auto dataSize = data.size();
const auto copySize = std::min(maxCopySize, dataSize);
if ( copySize > 0 ) {
memcpy(buffer, data.data(), copySize);
}
*bytes = copySize;
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
} catch ( ... ) {
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
}
}
::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]) override {
{
fuzzing::memory::memory_test(&(frame->header), sizeof(frame->header));
fuzzing::memory::memory_test(&(frame->footer), sizeof(frame->footer));
}
{
const auto numChannels = get_channels();
const size_t bytesPerChannel = frame->header.blocksize * sizeof(FLAC__int32);
for (size_t i = 0; i < numChannels; i++) {
fuzzing::memory::memory_test(buffer[i], bytesPerChannel);
}
}
try {
if ( ds.Get<bool>() == true ) {
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
} catch ( ... ) { }
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
void error_callback(::FLAC__StreamDecoderErrorStatus status) override {
fuzzing::memory::memory_test(status);
}
void metadata_callback(const ::FLAC__StreamMetadata *metadata) override {
Metadata::Prototype * cloned_object = nullptr;
fuzzing::memory::memory_test(metadata->type);
fuzzing::memory::memory_test(metadata->is_last);
fuzzing::memory::memory_test(metadata->length);
fuzzing::memory::memory_test(metadata->data);
if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
cloned_object = new Metadata::StreamInfo(metadata);
else if (metadata->type == FLAC__METADATA_TYPE_PADDING)
cloned_object = new Metadata::Padding(metadata);
else if (metadata->type == FLAC__METADATA_TYPE_APPLICATION)
cloned_object = new Metadata::Application(metadata);
else if (metadata->type == FLAC__METADATA_TYPE_SEEKTABLE)
cloned_object = new Metadata::SeekTable(metadata);
else if (metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
cloned_object = new Metadata::VorbisComment(metadata);
else if (metadata->type == FLAC__METADATA_TYPE_CUESHEET)
cloned_object = new Metadata::CueSheet(metadata);
else if (metadata->type == FLAC__METADATA_TYPE_PICTURE)
cloned_object = new Metadata::Picture(metadata);
else
return;
if (0 != cloned_object && *cloned_object == *metadata && cloned_object->is_valid()) {
if (cloned_object->get_type() == FLAC__METADATA_TYPE_SEEKTABLE)
dynamic_cast<Metadata::SeekTable *>(cloned_object)->is_legal();
if (cloned_object->get_type() == FLAC__METADATA_TYPE_PICTURE)
dynamic_cast<Metadata::Picture *>(cloned_object)->is_legal(NULL);
if (cloned_object->get_type() == FLAC__METADATA_TYPE_CUESHEET)
dynamic_cast<Metadata::CueSheet *>(cloned_object)->is_legal(true,NULL);
}
delete cloned_object;
}
::FLAC__StreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset) override {
fuzzing::memory::memory_test(absolute_byte_offset);
try {
if ( ds.Get<bool>() == true ) {
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
} else {
return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
}
} catch ( ... ) {
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
}
}
#if 0
::FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset) override {
fuzzing::memory::memory_test(*absolute_byte_offset);
try {
if ( ds.Get<bool>() == true ) {
return FLAC__STREAM_DECODER_TELL_STATUS_OK;
} else {
return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
}
} catch ( ... ) {
return FLAC__STREAM_DECODER_TELL_STATUS_OK;
}
}
::FLAC__StreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length) override {
fuzzing::memory::memory_test(*stream_length);
try {
if ( ds.Get<bool>() == true ) {
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
} else {
return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
}
} catch ( ... ) {
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
}
}
#endif
};
}
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
fuzzing::datasource::Datasource ds(data, size);
FLAC::Decoder::FuzzerStream decoder(ds);
bool use_ogg = true;
try {
if ( ds.Get<bool>() ) {
use_ogg = false;
}
if ( ds.Get<bool>() ) {
#ifdef FUZZER_DEBUG
printf("set_ogg_serial_number\n");
#endif
decoder.set_ogg_serial_number(ds.Get<long>());
}
if ( ds.Get<bool>() ) {
#ifdef FUZZER_DEBUG
printf("set_md5_checking\n");
#endif
decoder.set_md5_checking(ds.Get<bool>());
}
if ( ds.Get<bool>() ) {
#ifdef FUZZER_DEBUG
printf("set_metadata_respond\n");
#endif
decoder.set_metadata_respond(ds.Get<::FLAC__MetadataType>());
}
if ( ds.Get<bool>() ) {
const auto idVector = ds.GetData(0);
unsigned char id[4];
if ( idVector.size() >= sizeof(id) ) {
memcpy(id, idVector.data(), sizeof(id));
#ifdef FUZZER_DEBUG
printf("set_metadata_respond_application\n");
#endif
decoder.set_metadata_respond_application(id);
}
}
if ( ds.Get<bool>() ) {
#ifdef FUZZER_DEBUG
printf("set_metadata_respond_all\n");
#endif
decoder.set_metadata_respond_all();
}
if ( ds.Get<bool>() ) {
#ifdef FUZZER_DEBUG
printf("set_metadata_ignore\n");
#endif
decoder.set_metadata_ignore(ds.Get<::FLAC__MetadataType>());
}
if ( ds.Get<bool>() ) {
const auto idVector = ds.GetData(0);
unsigned char id[4];
if ( idVector.size() >= sizeof(id) ) {
memcpy(id, idVector.data(), sizeof(id));
#ifdef FUZZER_DEBUG
printf("set_metadata_ignore_application\n");
#endif
decoder.set_metadata_ignore_application(id);
}
}
if ( ds.Get<bool>() ) {
#ifdef FUZZER_DEBUG
printf("set_metadata_ignore_all\n");
#endif
decoder.set_metadata_ignore_all();
}
{
::FLAC__StreamDecoderInitStatus ret;
if ( !use_ogg ) {
ret = decoder.init();
} else {
ret = decoder.init_ogg();
}
if ( ret != FLAC__STREAM_DECODER_INIT_STATUS_OK ) {
goto end;
}
}
while ( ds.Get<bool>() ) {
switch ( ds.Get<uint8_t>() ) {
case 0:
{
#ifdef FUZZER_DEBUG
printf("flush\n");
#endif
const bool res = decoder.flush();
fuzzing::memory::memory_test(res);
}
break;
case 1:
{
#ifdef FUZZER_DEBUG
printf("reset\n");
#endif
const bool res = decoder.reset();
fuzzing::memory::memory_test(res);
}
break;
case 2:
{
#ifdef FUZZER_DEBUG
printf("process_single\n");
#endif
const bool res = decoder.process_single();
fuzzing::memory::memory_test(res);
}
break;
case 3:
{
#ifdef FUZZER_DEBUG
printf("process_until_end_of_metadata\n");
#endif
const bool res = decoder.process_until_end_of_metadata();
fuzzing::memory::memory_test(res);
}
break;
case 4:
{
#ifdef FUZZER_DEBUG
printf("process_until_end_of_stream\n");
#endif
const bool res = decoder.process_until_end_of_stream();
fuzzing::memory::memory_test(res);
}
break;
case 5:
{
#ifdef FUZZER_DEBUG
printf("skip_single_frame\n");
#endif
const bool res = decoder.skip_single_frame();
fuzzing::memory::memory_test(res);
}
break;
case 6:
{
#ifdef FUZZER_DEBUG
printf("seek_absolute\n");
#endif
const bool res = decoder.seek_absolute(ds.Get<uint64_t>());
fuzzing::memory::memory_test(res);
}
break;
case 7:
{
#ifdef FUZZER_DEBUG
printf("get_md5_checking\n");
#endif
const bool res = decoder.get_md5_checking();
fuzzing::memory::memory_test(res);
}
break;
case 8:
{
#ifdef FUZZER_DEBUG
printf("get_total_samples\n");
#endif
const bool res = decoder.get_total_samples();
fuzzing::memory::memory_test(res);
}
break;
case 9:
{
#ifdef FUZZER_DEBUG
printf("get_channels\n");
#endif
const bool res = decoder.get_channels();
fuzzing::memory::memory_test(res);
}
break;
case 10:
{
#ifdef FUZZER_DEBUG
printf("get_bits_per_sample\n");
#endif
const bool res = decoder.get_bits_per_sample();
fuzzing::memory::memory_test(res);
}
break;
case 11:
{
#ifdef FUZZER_DEBUG
printf("get_sample_rate\n");
#endif
const bool res = decoder.get_sample_rate();
fuzzing::memory::memory_test(res);
}
break;
case 12:
{
#ifdef FUZZER_DEBUG
printf("get_blocksize\n");
#endif
const bool res = decoder.get_blocksize();
fuzzing::memory::memory_test(res);
}
break;
}
}
} catch ( ... ) { }
end:
{
const bool res = decoder.finish();
fuzzing::memory::memory_test(res);
}
return 0;
}
|