Coverage Report

Created: 2026-12-01 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/flac/oss-fuzz/decoder.cc
Line
Count
Source
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
27
#include <fuzzing/datasource/datasource.hpp>
28
#include <fuzzing/memory.hpp>
29
30
#include "FLAC++/decoder.h"
31
#include "FLAC++/metadata.h"
32
#include "common.h"
33
34
134
template <> FLAC__MetadataType fuzzing::datasource::Base::Get<FLAC__MetadataType>(const uint64_t id) {
35
134
    (void)id;
36
134
    switch ( Get<uint8_t>() ) {
37
28
        case 0:
38
28
            return FLAC__METADATA_TYPE_STREAMINFO;
39
10
        case 1:
40
10
            return FLAC__METADATA_TYPE_PADDING;
41
3
        case 2:
42
3
            return FLAC__METADATA_TYPE_APPLICATION;
43
1
        case 3:
44
1
            return FLAC__METADATA_TYPE_SEEKTABLE;
45
2
        case 4:
46
2
            return FLAC__METADATA_TYPE_VORBIS_COMMENT;
47
1
        case 5:
48
1
            return FLAC__METADATA_TYPE_CUESHEET;
49
0
        case 6:
50
0
            return FLAC__METADATA_TYPE_PICTURE;
51
6
        case 7:
52
6
            return FLAC__METADATA_TYPE_UNDEFINED;
53
7
        case 8:
54
7
            return FLAC__MAX_METADATA_TYPE;
55
74
        default:
56
74
            return FLAC__METADATA_TYPE_STREAMINFO;
57
134
    }
58
134
}
59
60
namespace FLAC {
61
  namespace Decoder {
62
        class FuzzerStream : public Stream {
63
            private:
64
                fuzzing::datasource::Datasource& ds;
65
            public:
66
                FuzzerStream(fuzzing::datasource::Datasource& dsrc) :
67
9.59k
                    Stream(), ds(dsrc) { }
68
69
270k
                ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes)  override {
70
270k
                    try {
71
270k
                        const size_t maxCopySize = *bytes;
72
73
270k
                        if ( maxCopySize > 0 ) {
74
                            /* memset just to test if this overwrites anything, and triggers ASAN */
75
270k
                            memset(buffer, 0, maxCopySize);
76
270k
                        }
77
78
270k
                        const auto data = ds.GetData(0);
79
270k
                        const auto dataSize = data.size();
80
270k
                        const auto copySize = std::min(maxCopySize, dataSize);
81
82
270k
                        if ( copySize > 0 ) {
83
99.9k
                            memcpy(buffer, data.data(), copySize);
84
99.9k
                        }
85
86
270k
                        *bytes = copySize;
87
88
270k
                        return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
89
270k
                    } catch ( ... ) {
90
59.9k
                      return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
91
59.9k
                    }
92
270k
                }
93
94
81.6k
                ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])  override {
95
81.6k
                    {
96
81.6k
                        fuzzing::memory::memory_test(&(frame->header), sizeof(frame->header));
97
81.6k
                        fuzzing::memory::memory_test(&(frame->footer), sizeof(frame->footer));
98
81.6k
                    }
99
100
81.6k
                    {
101
81.6k
                        const auto numChannels = get_channels();
102
81.6k
                        const size_t bytesPerChannel = frame->header.blocksize * sizeof(FLAC__int32);
103
226k
                        for (size_t i = 0; i < numChannels; i++) {
104
145k
                            fuzzing::memory::memory_test(buffer[i], bytesPerChannel);
105
145k
                        }
106
81.6k
                    }
107
108
81.6k
                    try {
109
81.6k
                        if ( ds.Get<bool>() == true ) {
110
853
                          return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
111
853
                        }
112
81.6k
                    } catch ( ... ) { }
113
80.8k
                    return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
114
81.6k
                }
115
116
270k
                void error_callback(::FLAC__StreamDecoderErrorStatus status)  override {
117
270k
                    fuzzing::memory::memory_test(status);
118
270k
                }
119
120
25.1k
                void metadata_callback(const ::FLAC__StreamMetadata *metadata) override {
121
25.1k
                    Metadata::Prototype * cloned_object = nullptr;
122
25.1k
                    fuzzing::memory::memory_test(metadata->type);
123
25.1k
                    fuzzing::memory::memory_test(metadata->is_last);
124
25.1k
                    fuzzing::memory::memory_test(metadata->length);
125
25.1k
                    fuzzing::memory::memory_test(metadata->data);
126
25.1k
                    if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
127
1.67k
                        cloned_object = new Metadata::StreamInfo(metadata);
128
23.4k
                    else if (metadata->type == FLAC__METADATA_TYPE_PADDING)
129
893
                        cloned_object = new Metadata::Padding(metadata);
130
22.5k
                    else if (metadata->type == FLAC__METADATA_TYPE_APPLICATION)
131
2.66k
                        cloned_object = new Metadata::Application(metadata);
132
19.9k
                    else if (metadata->type == FLAC__METADATA_TYPE_SEEKTABLE)
133
999
                        cloned_object = new Metadata::SeekTable(metadata);
134
18.9k
                    else if (metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
135
2.21k
                        cloned_object = new Metadata::VorbisComment(metadata);
136
16.7k
                    else if (metadata->type == FLAC__METADATA_TYPE_CUESHEET)
137
3.47k
                        cloned_object = new Metadata::CueSheet(metadata);
138
13.2k
                    else if (metadata->type == FLAC__METADATA_TYPE_PICTURE)
139
9.70k
                        cloned_object = new Metadata::Picture(metadata);
140
3.52k
                    else
141
3.52k
                        return;
142
21.6k
                    if (0 != cloned_object && *cloned_object == *metadata && cloned_object->is_valid()) {
143
21.4k
                        if (cloned_object->get_type() == FLAC__METADATA_TYPE_SEEKTABLE)
144
999
                            dynamic_cast<Metadata::SeekTable *>(cloned_object)->is_legal();
145
21.4k
                        if (cloned_object->get_type() == FLAC__METADATA_TYPE_PICTURE)
146
9.51k
                            dynamic_cast<Metadata::Picture *>(cloned_object)->is_legal(NULL);
147
21.4k
                        if (cloned_object->get_type() == FLAC__METADATA_TYPE_CUESHEET)
148
3.47k
                            dynamic_cast<Metadata::CueSheet *>(cloned_object)->is_legal(true,NULL);
149
21.4k
                    }
150
21.6k
                    delete cloned_object;
151
21.6k
                }
152
153
66.1k
                ::FLAC__StreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset) override {
154
66.1k
                    fuzzing::memory::memory_test(absolute_byte_offset);
155
156
66.1k
                    try {
157
66.1k
                        if ( ds.Get<bool>() == true ) {
158
62.2k
                            return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
159
62.2k
                        } else {
160
3.88k
                            return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
161
3.88k
                        }
162
66.1k
                    } catch ( ... ) {
163
37
                        return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
164
37
                    }
165
66.1k
                }
166
#if 0
167
                ::FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset) override {
168
                    fuzzing::memory::memory_test(*absolute_byte_offset);
169
170
                    try {
171
                        if ( ds.Get<bool>() == true ) {
172
                            return FLAC__STREAM_DECODER_TELL_STATUS_OK;
173
                        } else {
174
                            return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
175
                        }
176
                    } catch ( ... ) {
177
                        return FLAC__STREAM_DECODER_TELL_STATUS_OK;
178
                    }
179
                }
180
181
                ::FLAC__StreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length) override {
182
                    fuzzing::memory::memory_test(*stream_length);
183
184
                    try {
185
                        if ( ds.Get<bool>() == true ) {
186
                            return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
187
                        } else {
188
                            return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
189
                        }
190
                    } catch ( ... ) {
191
                        return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
192
                    }
193
                }
194
#endif
195
        };
196
    }
197
}
198
199
9.59k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
200
9.59k
    fuzzing::datasource::Datasource ds(data, size);
201
9.59k
    FLAC::Decoder::FuzzerStream decoder(ds);
202
9.59k
    bool use_ogg = true;
203
204
9.59k
    try {
205
9.59k
        if ( ds.Get<bool>() ) {
206
8.32k
            use_ogg = false;
207
8.32k
        }
208
209
9.59k
        decoder.set_decode_chained_stream(true);
210
211
9.59k
        if ( ds.Get<bool>() ) {
212
#ifdef FUZZER_DEBUG
213
            printf("set_ogg_serial_number\n");
214
#endif
215
21
            decoder.set_ogg_serial_number(ds.Get<long>());
216
21
        }
217
9.59k
        if ( ds.Get<bool>() ) {
218
#ifdef FUZZER_DEBUG
219
            printf("set_md5_checking\n");
220
#endif
221
1.22k
            decoder.set_md5_checking(ds.Get<bool>());
222
1.22k
        }
223
9.59k
        if ( ds.Get<bool>() ) {
224
#ifdef FUZZER_DEBUG
225
            printf("set_metadata_respond\n");
226
#endif
227
79
            decoder.set_metadata_respond(ds.Get<::FLAC__MetadataType>());
228
79
        }
229
9.59k
        if ( ds.Get<bool>() ) {
230
55
            const auto idVector = ds.GetData(0);
231
55
            unsigned char id[4];
232
55
            if ( idVector.size() >= sizeof(id) ) {
233
17
                memcpy(id, idVector.data(), sizeof(id));
234
#ifdef FUZZER_DEBUG
235
                printf("set_metadata_respond_application\n");
236
#endif
237
17
                decoder.set_metadata_respond_application(id);
238
17
            }
239
55
        }
240
9.59k
        if ( ds.Get<bool>() ) {
241
#ifdef FUZZER_DEBUG
242
            printf("set_metadata_respond_all\n");
243
#endif
244
5.43k
            decoder.set_metadata_respond_all();
245
5.43k
        }
246
9.59k
        if ( ds.Get<bool>() ) {
247
#ifdef FUZZER_DEBUG
248
            printf("set_metadata_ignore\n");
249
#endif
250
55
            decoder.set_metadata_ignore(ds.Get<::FLAC__MetadataType>());
251
55
        }
252
9.59k
        if ( ds.Get<bool>() ) {
253
123
            const auto idVector = ds.GetData(0);
254
123
            unsigned char id[4];
255
123
            if ( idVector.size() >= sizeof(id) ) {
256
91
                memcpy(id, idVector.data(), sizeof(id));
257
#ifdef FUZZER_DEBUG
258
                printf("set_metadata_ignore_application\n");
259
#endif
260
91
                decoder.set_metadata_ignore_application(id);
261
91
            }
262
123
        }
263
9.59k
        if ( ds.Get<bool>() ) {
264
#ifdef FUZZER_DEBUG
265
            printf("set_metadata_ignore_all\n");
266
#endif
267
3.47k
            decoder.set_metadata_ignore_all();
268
3.47k
        }
269
9.59k
        {
270
9.59k
            ::FLAC__StreamDecoderInitStatus ret;
271
9.59k
            if ( !use_ogg ) {
272
8.24k
                ret = decoder.init();
273
8.24k
            } else {
274
1.35k
                ret = decoder.init_ogg();
275
1.35k
            }
276
277
9.59k
            if ( ret != FLAC__STREAM_DECODER_INIT_STATUS_OK ) {
278
0
                goto end;
279
0
            }
280
9.59k
        }
281
282
231k
        while ( ds.Get<bool>() ) {
283
222k
            switch ( ds.Get<uint8_t>() ) {
284
14.6k
                case    0:
285
14.6k
                    {
286
#ifdef FUZZER_DEBUG
287
                        printf("flush\n");
288
#endif
289
14.6k
                        const bool res = decoder.flush();
290
14.6k
                        fuzzing::memory::memory_test(res);
291
14.6k
                    }
292
14.6k
                    break;
293
66.3k
                case    1:
294
66.3k
                    {
295
#ifdef FUZZER_DEBUG
296
                        printf("reset\n");
297
#endif
298
66.3k
                        const bool res = decoder.reset();
299
66.3k
                        fuzzing::memory::memory_test(res);
300
66.3k
                    }
301
66.3k
                    break;
302
18.6k
                case    2:
303
18.6k
                    {
304
#ifdef FUZZER_DEBUG
305
                        printf("process_single\n");
306
#endif
307
18.6k
                        const bool res = decoder.process_single();
308
18.6k
                        fuzzing::memory::memory_test(res);
309
18.6k
                    }
310
18.6k
                    break;
311
50.1k
                case    3:
312
50.1k
                    {
313
#ifdef FUZZER_DEBUG
314
                        printf("process_until_end_of_metadata\n");
315
#endif
316
50.1k
                        const bool res = decoder.process_until_end_of_metadata();
317
50.1k
                        fuzzing::memory::memory_test(res);
318
50.1k
                    }
319
50.1k
                    break;
320
39.8k
                case    4:
321
39.8k
                    {
322
#ifdef FUZZER_DEBUG
323
                        printf("process_until_end_of_stream\n");
324
#endif
325
39.8k
                        const bool res = decoder.process_until_end_of_stream();
326
39.8k
                        fuzzing::memory::memory_test(res);
327
39.8k
                    }
328
39.8k
                    break;
329
2.23k
                case    5:
330
2.23k
                    {
331
#ifdef FUZZER_DEBUG
332
                        printf("skip_single_frame\n");
333
#endif
334
2.23k
                        const bool res = decoder.skip_single_frame();
335
2.23k
                        fuzzing::memory::memory_test(res);
336
2.23k
                    }
337
2.23k
                    break;
338
1.00k
                case    6:
339
1.00k
                    {
340
#ifdef FUZZER_DEBUG
341
                        printf("seek_absolute\n");
342
#endif
343
1.00k
                        const bool res = decoder.seek_absolute(ds.Get<uint64_t>());
344
1.00k
                        fuzzing::memory::memory_test(res);
345
1.00k
                    }
346
1.00k
                    break;
347
559
                case    7:
348
559
                    {
349
#ifdef FUZZER_DEBUG
350
                        printf("get_md5_checking\n");
351
#endif
352
559
                        const bool res = decoder.get_md5_checking();
353
559
                        fuzzing::memory::memory_test(res);
354
559
                    }
355
559
                    break;
356
447
                case    8:
357
447
                    {
358
#ifdef FUZZER_DEBUG
359
                        printf("get_total_samples\n");
360
#endif
361
447
                        const bool res = decoder.get_total_samples();
362
447
                        fuzzing::memory::memory_test(res);
363
447
                    }
364
447
                    break;
365
295
                case    9:
366
295
                    {
367
#ifdef FUZZER_DEBUG
368
                        printf("get_channels\n");
369
#endif
370
295
                        const bool res = decoder.get_channels();
371
295
                        fuzzing::memory::memory_test(res);
372
295
                    }
373
295
                    break;
374
375
                case    10:
375
375
                    {
376
#ifdef FUZZER_DEBUG
377
                        printf("get_bits_per_sample\n");
378
#endif
379
375
                        const bool res = decoder.get_bits_per_sample();
380
375
                        fuzzing::memory::memory_test(res);
381
375
                    }
382
375
                    break;
383
812
                case    11:
384
812
                    {
385
#ifdef FUZZER_DEBUG
386
                        printf("get_sample_rate\n");
387
#endif
388
812
                        const bool res = decoder.get_sample_rate();
389
812
                        fuzzing::memory::memory_test(res);
390
812
                    }
391
812
                    break;
392
245
                case    12:
393
245
                    {
394
#ifdef FUZZER_DEBUG
395
                        printf("get_blocksize\n");
396
#endif
397
245
                        const bool res = decoder.get_blocksize();
398
245
                        fuzzing::memory::memory_test(res);
399
245
                    }
400
245
                    break;
401
222k
            }
402
222k
        }
403
9.59k
    } catch ( ... ) { }
404
405
9.59k
end:
406
9.59k
    {
407
9.59k
        const bool res = decoder.finish();
408
9.59k
        fuzzing::memory::memory_test(res);
409
9.59k
    }
410
9.59k
    return 0;
411
9.59k
}