Debian Codegraph @

oss-fuzz/decoder.cc

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