-
Notifications
You must be signed in to change notification settings - Fork 4
/
fuzzer-encoder.cpp
1053 lines (776 loc) · 36 KB
/
fuzzer-encoder.cpp
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* LAME encoder fuzzer by Guido Vranken <[email protected]> */
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <fuzzing/datasource/datasource.hpp>
#include <fuzzing/memory.hpp>
#include <lame.h>
#include <limits>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
#ifdef MSAN
extern "C" {
void __msan_allocated_memory(const volatile void* data, size_t size);
}
#endif
using fuzzing::datasource::Datasource;
namespace limits {
template <size_t Min, size_t Max>
class Limit {
static_assert(Min <= Max);
public:
Limit(void) { }
size_t Test(const size_t val) const {
if ( val < Min || val > Max ) {
/* If not within bounds, default to the minimum allowed value */
return Min;
}
return val;
}
template <typename T = uint32_t>
size_t Generate(fuzzing::datasource::Datasource& ds) {
const size_t ret = ds.Get<T>();
return Test(ret);
}
};
/* Set these to acceptable min/max limits */
static Limit<1, 1024*1024> OutBufferSize;
static Limit<1, 1024> MinBitrate;
static Limit<1, 1024> MaxBitrate;
static Limit<1, 1024> VBRQ;
static Limit<1, 1024> ABRBitrate;
static Limit<1, 1024> CBRBitrate;
static Limit<100, 1000000> OutSamplerate;
static Limit<0, 9> Quality;
static Limit<0, 1000000> LowpassFrequency;
static Limit<1000, 1000000> LowpassWidth;
static Limit<1000, 1000000> HighpassFrequency;
static Limit<1000, 1000000> HighpassWidth;
static Limit<1, 100> CompressionRatio;
}
#define _(expr) Debug ? printf("%s\n", #expr) : 0; expr;
/* Unspecialized method, for all types other than
* float and double; do nothing.
*/
template <typename T> bool isNAN(const T& val) {
(void)val;
return false;
}
template <> bool isNAN<float>(const float& val) {
return std::isnan(val);
}
template <> bool isNAN<double>(const double& val) {
return std::isnan(val);
}
std::string debug_define_size_t(const std::string name, const size_t val) {
return "const size_t " + name + " = " + std::to_string(val) + ";";
}
template <typename T>
struct DebugDefineArray {
static std::string Str(const std::string name, const std::string typeName, const T* inData, const size_t inDataSize, const bool indent) {
std::stringstream ret;
if ( indent ) {
ret << "\t";
}
ret << "const " << typeName << " " << name << "[] = {\n";
if ( indent ) {
ret << "\t";
}
for (size_t i = 0; i < inDataSize; i++) {
if ( i && !(i % 16) ) {
ret << "\n";
if ( indent ) {
ret << "\t";
}
}
ret << "\t" << std::to_string(inData[i]) << ", ";
}
ret << "\n";
if ( indent ) {
ret << "\t";
}
ret << "};\n";
return ret.str();
}
};
class EncoderCoreBase {
public:
EncoderCoreBase(void) { }
virtual ~EncoderCoreBase() { }
virtual bool Run(uint8_t* outBuffer, const size_t outBufferSize, const bool mono) = 0;
};
template <typename T, bool Debug>
class EncoderCore : public EncoderCoreBase {
private:
Datasource& ds;
lame_global_flags* flags;
std::vector< std::vector<T> > inDataV;
typename std::vector< std::vector<T> >::iterator it;
const bool useInterleavingFunction;
const bool useIEEEFunction;
void getInputData(void) {
while ( ds.Get<bool>() ) {
const auto data = ds.GetData(0);
/* Round to a multiple of sizeof(T) */
const size_t copySize = data.size() - (data.size() % sizeof(T));
std::vector<T> toInsert;
toInsert.resize( data.size() / sizeof(T) );
memcpy(toInsert.data(), data.data(), copySize);
/* Correct NAN values */
for (size_t i = 0; i < toInsert.size(); i++) {
if ( isNAN(toInsert[i]) ) {
/* If NaN, set to default value (0.0) */
toInsert[i] = {};
}
}
inDataV.push_back(toInsert);
}
it = inDataV.begin();
}
template <typename _T, long Min, long Max>
struct InputCorrect {
static_assert(std::numeric_limits<_T>::lowest() <= Min);
static_assert(std::numeric_limits<_T>::max() >= Max);
static void Correct(_T* inData, const size_t inDataSize) {
if ( inData == nullptr ) {
return;
}
for (size_t i = 0; i < inDataSize; i++) {
if ( inData[i] > Max ) {
inData[i] = Max;
} else if ( inData[i] < Min ) {
inData[i] = Min;
}
}
}
};
template <typename T_, bool Debug_>
struct EncodeSingle {
static int encode(
lame_global_flags* flags,
T* inData,
const size_t inDataSize,
uint8_t* outBuffer,
const size_t outBufferSize,
bool useInterleavedFunction,
bool useIEEEFunction);
};
template<bool Debug_> struct EncodeSingle<short int, Debug_> {
static int encode(
lame_global_flags* flags,
short int* inDataL,
short int* inDataR,
const size_t inDataSize,
uint8_t* outBuffer,
const size_t outBufferSize,
bool useInterleavingFunction,
bool useIEEEFunction) {
/* Not applicable for short int */
(void)useIEEEFunction;
if ( useInterleavingFunction == false ) {
Debug ? printf("{\n") : 0;
Debug ? printf("\t%s\n", debug_define_size_t("inDataSize", inDataSize).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<short int>::Str("inDataL", "short int", inDataL, inDataSize, true).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<short int>::Str("inDataR", "short int", inDataR, inDataSize, true).c_str()) : 0;
Debug ?
printf("\tlame_encode_buffer(flags, inDataL, inDataR, inDataSize, outBuffer, outBufferSize);\n")
: 0;
const int ret = lame_encode_buffer(flags, inDataL, inDataR, inDataSize, outBuffer, outBufferSize);
Debug ? printf("\t// (returns %d)\n", ret) : 0;
Debug ? printf("}\n") : 0;
return ret;
} else {
Debug ? printf("{\n") : 0;
Debug ? printf("\t%s\n", debug_define_size_t("inDataSize", inDataSize).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<short int>::Str("inDataL", "short int", inDataL, inDataSize * 2, true).c_str()) : 0;
Debug ?
printf("\tlame_encode_buffer_interleaved(flags, inDataL, inDataSize, outBuffer, outBufferSize);\n")
: 0;
const int ret = lame_encode_buffer_interleaved(flags, inDataL, inDataSize, outBuffer, outBufferSize);
Debug ? printf("\t// (returns %d)\n", ret) : 0;
Debug ? printf("}\n") : 0;
return ret;
}
}
};
template<bool Debug_> struct EncodeSingle<int, Debug_> {
static int encode(
lame_global_flags* flags,
int* inDataL,
int* inDataR,
const size_t inDataSize,
uint8_t* outBuffer,
const size_t outBufferSize,
bool useInterleavingFunction,
bool useIEEEFunction ) {
/* Not applicable for int */
(void)useIEEEFunction;
if ( useInterleavingFunction == false ) {
Debug ? printf("{\n") : 0;
Debug ? printf("\t%s\n", debug_define_size_t("inDataSize", inDataSize).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<int>::Str("inDataL", "int", inDataL, inDataSize, true).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<int>::Str("inDataR", "int", inDataR, inDataSize, true).c_str()) : 0;
Debug ?
printf("\tlame_encode_buffer_int(flags, inDataL, inDataR, inDataSize, outBuffer, outBufferSize);\n")
: 0;
const int ret = lame_encode_buffer_int(flags, inDataL, inDataR, inDataSize, outBuffer, outBufferSize);
Debug ? printf("\t// (returns %d)\n", ret) : 0;
Debug ? printf("}\n") : 0;
return ret;
} else {
Debug ? printf("{\n") : 0;
Debug ? printf("\t%s\n", debug_define_size_t("inDataSize", inDataSize).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<int>::Str("inDataL", "int", inDataL, inDataSize * 2, true).c_str()) : 0;
Debug ?
printf("\tlame_encode_buffer_interleaved_int(flags, inDataL, inDataSize, outBuffer, outBufferSize);\n")
: 0;
const int ret = lame_encode_buffer_interleaved_int(flags, inDataL, inDataSize, outBuffer, outBufferSize);
Debug ? printf("\t// (returns %d)\n", ret) : 0;
Debug ? printf("}\n") : 0;
return ret;
}
}
};
template<bool Debug_> struct EncodeSingle<long, Debug_> {
static int encode(
lame_global_flags* flags,
long* inDataL,
long* inDataR,
const size_t inDataSize,
uint8_t* outBuffer,
const size_t outBufferSize,
bool useInterleavingFunction,
bool useIEEEFunction) {
/* Not applicable for long */
(void)useIEEEFunction;
if ( useInterleavingFunction == false ) {
InputCorrect<long, -32768, 32768>::Correct(inDataL, inDataSize);
InputCorrect<long, -32768, 32768>::Correct(inDataR, inDataSize);
Debug ? printf("{\n") : 0;
Debug ? printf("\t%s\n", debug_define_size_t("inDataSize", inDataSize).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<long>::Str("inDataL", "long", inDataL, inDataSize, true).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<long>::Str("inDataR", "long", inDataR, inDataSize, true).c_str()) : 0;
Debug ?
printf("\tlame_encode_buffer_long(flags, inDataL, inDataR, inDataSize, outBuffer, outBufferSize);\n")
: 0;
const int ret = lame_encode_buffer_long(flags, inDataL, inDataR, inDataSize, outBuffer, outBufferSize);
Debug ? printf("\t// (returns %d)\n", ret) : 0;
Debug ? printf("}\n") : 0;
return ret;
} else {
Debug ? printf("{\n") : 0;
Debug ? printf("\t%s\n", debug_define_size_t("inDataSize", inDataSize).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<long>::Str("inDataL", "long", inDataL, inDataSize, true).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<long>::Str("inDataR", "long", inDataR, inDataSize, true).c_str()) : 0;
Debug ?
printf("\tlame_encode_buffer_long2(flags, inDataL, inDataR, inDataSize, outBuffer, outBufferSize);\n")
: 0;
/* Not actually interleaved */
const int ret = lame_encode_buffer_long2(flags, inDataL, inDataR, inDataSize, outBuffer, outBufferSize);
Debug ? printf("\t// (returns %d)\n", ret) : 0;
Debug ? printf("}\n") : 0;
return ret;
}
}
};
template<bool Debug_> struct EncodeSingle<float, Debug_> {
static int encode(
lame_global_flags* flags,
float* inDataL,
float* inDataR,
const size_t inDataSize,
uint8_t* outBuffer,
const size_t outBufferSize,
bool useInterleavingFunction,
bool useIEEEFunction) {
if ( useInterleavingFunction == false ) {
if ( useIEEEFunction == false ) {
InputCorrect<float, -32768, 32768>::Correct(inDataL, inDataSize);
InputCorrect<float, -32768, 32768>::Correct(inDataR, inDataSize);
Debug ? printf("{\n") : 0;
Debug ? printf("\t%s\n", debug_define_size_t("inDataSize", inDataSize).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<float>::Str("inDataL", "float", inDataL, inDataSize, true).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<float>::Str("inDataR", "float", inDataR, inDataSize, true).c_str()) : 0;
Debug ?
printf("\tlame_encode_buffer_float(flags, inDataL, inDataR, inDataSize, outBuffer, outBufferSize);\n")
: 0;
const int ret = lame_encode_buffer_float(flags, inDataL, inDataR, inDataSize, outBuffer, outBufferSize);
Debug ? printf("\t// (returns %d)\n", ret) : 0;
Debug ? printf("}\n") : 0;
return ret;
} else {
InputCorrect<float, -1, 1>::Correct(inDataL, inDataSize);
InputCorrect<float, -1, 1>::Correct(inDataR, inDataSize);
Debug ? printf("{\n") : 0;
Debug ? printf("\t%s\n", debug_define_size_t("inDataSize", inDataSize).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<float>::Str("inDataL", "float", inDataL, inDataSize, true).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<float>::Str("inDataR", "float", inDataR, inDataSize, true).c_str()) : 0;
Debug ?
printf("\tlame_encode_buffer_ieee_float(flags, inDataL, inDataR, inDataSize, outBuffer, outBufferSize);\n")
: 0;
const int ret = lame_encode_buffer_ieee_float(flags, inDataL, inDataR, inDataSize, outBuffer, outBufferSize);
Debug ? printf("\t// (returns %d)\n", ret) : 0;
Debug ? printf("}\n") : 0;
return ret;
}
} else {
if ( useIEEEFunction == true ) {
InputCorrect<float, -1, 1>::Correct(inDataL, inDataSize * 2);
Debug ? printf("{\n") : 0;
Debug ? printf("\t%s\n", debug_define_size_t("inDataSize", inDataSize * 2).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<float>::Str("inDataL", "float", inDataL, inDataSize * 2, true).c_str()) : 0;
Debug ?
printf("\tlame_encode_buffer_interleaved_ieee_float(flags, inDataL, inDataSize, outBuffer, outBufferSize);\n")
: 0;
const int ret = lame_encode_buffer_interleaved_ieee_float(flags, inDataL, inDataSize, outBuffer, outBufferSize);
Debug ? printf("\t// (returns %d)\n", ret) : 0;
Debug ? printf("}\n") : 0;
return ret;
} else {
/* No function for interleaved float */
return -1;
}
}
}
};
template<bool Debug_> struct EncodeSingle<double, Debug_> {
int static encode(
lame_global_flags* flags,
double* inDataL,
double* inDataR,
const size_t inDataSize,
uint8_t* outBuffer,
const size_t outBufferSize,
bool useInterleavingFunction,
bool useIEEEFunction) {
if ( useInterleavingFunction == false ) {
if ( useIEEEFunction == false ) {
/* No non-IEEE function for interleaved double */
return -1;
} else {
InputCorrect<double, -1, 1>::Correct(inDataL, inDataSize);
InputCorrect<double, -1, 1>::Correct(inDataR, inDataSize);
Debug ? printf("{\n") : 0;
Debug ? printf("\t%s\n", debug_define_size_t("inDataSize", inDataSize).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<double>::Str("inDataL", "double", inDataL, inDataSize, true).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<double>::Str("inDataR", "double", inDataR, inDataSize, true).c_str()) : 0;
Debug ?
printf("\tlame_encode_buffer_ieee_double(flags, inDataL, inDataR, inDataSize, outBuffer, outBufferSize);\n")
: 0;
const int ret = lame_encode_buffer_ieee_double(flags, inDataL, inDataR, inDataSize, outBuffer, outBufferSize);
Debug ? printf("\t// (returns %d)\n", ret) : 0;
Debug ? printf("}\n") : 0;
return ret;
}
} else {
if ( useIEEEFunction == false ) {
InputCorrect<double, -1, 1>::Correct(inDataL, inDataSize * 2);
Debug ? printf("{\n") : 0;
Debug ? printf("\t%s\n", debug_define_size_t("inDataSize", inDataSize).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<double>::Str("inDataL", "double", inDataL, inDataSize * 2, true).c_str()) : 0;
Debug ?
printf("lame_encode_buffer_interleaved_ieee_double(flags, inDataL, inDataSize, outBuffer, outBufferSize);\n")
: 0;
const int ret = lame_encode_buffer_interleaved_ieee_double(flags, inDataL, inDataSize, outBuffer, outBufferSize);
Debug ? printf("\t// (returns %d)\n", ret) : 0;
Debug ? printf("}\n") : 0;
return ret;
} else {
/* No non-IEEE function for double */
return -1;
}
}
}
};
int encode(std::vector<T>& inData, uint8_t* outBuffer, const size_t outBufferSize, const bool mono) {
if ( useInterleavingFunction && mono ) {
return -1;
}
if ( mono == true ) {
return EncodeSingle<T, Debug>::encode(flags, inData.data(), nullptr, inData.size() / 2, outBuffer, outBufferSize, useInterleavingFunction, useIEEEFunction);
} else if ( useInterleavingFunction ) {
const size_t numSamples = inData.size() / 2;
std::vector<T> inDataCopy;
inDataCopy.resize(numSamples * 2);
memcpy(inDataCopy.data(), inData.data(), numSamples * 2 * sizeof(T));
return EncodeSingle<T, Debug>::encode(flags, inData.data(), nullptr, numSamples, outBuffer, outBufferSize, useInterleavingFunction, useIEEEFunction);
} else {
size_t numSamples = inData.size();
/* Round to a multiple of 2 */
if ( numSamples % 2 ) {
numSamples--;
}
/* To samples per channel */
numSamples /= 2;
/* Left, right channels */
std::vector<T> inDataL, inDataR;
inDataL.resize(numSamples);
inDataR.resize(numSamples);
/* Split inData evenly between inDataL and inDataR */
memcpy(inDataL.data(), inData.data(), numSamples * sizeof(T));
memcpy(inDataR.data(), inData.data() + numSamples, numSamples * sizeof(T));
return EncodeSingle<T, Debug>::encode(flags, inDataL.data(), inDataR.data(), numSamples, outBuffer, outBufferSize, useInterleavingFunction, useIEEEFunction);
}
}
int flush(uint8_t* outBuffer, const size_t outBufferSize) {
if ( ds.Get<bool>() ) {
/* No flush */
return 0;
}
if ( ds.Get<bool>() ) {
Debug ?
printf("lame_encode_flush(flags, outBuffer, outBufferSize);\n")
: 0;
const int ret = lame_encode_flush(flags, outBuffer, outBufferSize);
Debug ? printf("// (returns %d)\n", ret) : 0;
if ( ret > static_cast<int>(outBufferSize) ) {
/* Crashes
printf("lame_encode_flush reported more output bytes (%zu) than the buffer can hold (%zu)\n", static_cast<size_t>(ret), outBufferSize);
abort();
*/
}
return ret;
} else {
#if 0
/* XXX disabled because it prints:
* "strange error flushing buffer ..."
*/
Debug ?
printf("lame_encode_flush_nogap(flags, outBuffer, outBufferSize);\n")
: 0;
const int ret = lame_encode_flush_nogap(flags, outBuffer, outBufferSize);
Debug ? printf("// (returns %d)\n", ret) : 0;
return ret;
#else
return 0;
#endif
}
}
public:
EncoderCore(Datasource& ds, lame_global_flags* flags) :
EncoderCoreBase(),
ds(ds),
flags(flags),
it(inDataV.end()),
useInterleavingFunction( ds.Get<bool>() ),
useIEEEFunction( ds.Get<bool>() )
{
getInputData();
}
bool Run(uint8_t* outBuffer, const size_t outBufferSize, const bool mono) override {
if ( it == inDataV.end() ) {
return false;
}
auto& inData = *it;
it++;
#ifdef MSAN
/* Poison the outbuffer so if encode() puts uninitialized memory in it,
* this can be detected.
*/
__msan_allocated_memory(outBuffer, outBufferSize);
#endif
const int encodeRet = encode(inData, outBuffer, outBufferSize, mono);
if ( encodeRet < 0 ) {
return false;
}
/* static_cast is safe because outBufferSize is never anywhere near 2**31 */
if ( encodeRet > static_cast<int>(outBufferSize) ) {
printf("encode reported more output bytes than the buffer can hold\n");
abort();
}
#ifdef MSAN
/* Check for uninitialized data in the output buffer */
fuzzing::memory::memory_test_msan(outBuffer, encodeRet);
/* Poison it again */
__msan_allocated_memory(outBuffer, outBufferSize);
#endif
const int flushRet = flush(outBuffer, outBufferSize);
if ( flushRet < 0 ) {
return false;
}
fuzzing::memory::memory_test_msan(outBuffer, flushRet);
if ( encodeRet == 0 ) {
return false;
}
return true;
}
};
/* In the interest of speed, let Debug be a template parameter,
* so that in non-debug mode, all debug checks will be optimized away.
*/
template <bool Debug>
class EncoderFuzzer {
private:
Datasource& ds;
lame_global_flags* flags = nullptr;
uint8_t* outBuffer = nullptr;
const size_t outBufferSize;
bool mono = false;
void setBitrateModeVBR_RH(void) {
_(lame_set_VBR(flags, vbr_rh););
}
void setBitrateModeVBR_MTRH(void) {
_(lame_set_VBR(flags, vbr_mtrh););
}
void setBitrateModeVBR_ABR(void) {
_(lame_set_VBR(flags, vbr_abr););
const size_t ABRBitrate = limits::ABRBitrate.Generate(ds);
Debug ? printf("lame_set_VBR_mean_bitrate_kbps(flags, %zu);\n", ABRBitrate) : 0;
lame_set_VBR_mean_bitrate_kbps(flags, ABRBitrate);
}
void setVBRQ(void) {
if ( ds.Get<bool>() ) return;
const size_t vbrQ = limits::VBRQ.Generate<uint8_t>(ds);
Debug ? printf("lame_set_VBR_q(flags, %zu);\n", vbrQ) : 0;
lame_set_VBR_q(flags, vbrQ);
}
size_t setMinBitrate(void) {
if ( ds.Get<bool>() ) return 0;
const size_t minBitrate = limits::MinBitrate.Generate(ds);
Debug ? printf("lame_set_VBR_min_bitrate_kbps(flags, %zu);\n", minBitrate) : 0;
lame_set_VBR_min_bitrate_kbps(flags, minBitrate);
return minBitrate;
}
void setMaxBitrate(const size_t minBitrate) {
if ( ds.Get<bool>() ) return;
size_t maxBitrate = limits::MaxBitrate.Generate(ds);
if ( minBitrate > maxBitrate ) {
/* minBitrate should be <= maxBitrate, so if that is not the case,
* set them both to the same value.
*/
maxBitrate = minBitrate;
}
Debug ? printf("lame_set_VBR_max_bitrate_kbps(flags, %zu);\n", maxBitrate) : 0;
lame_set_VBR_max_bitrate_kbps(flags, maxBitrate);
}
void setBitrateModeVBR(void) {
const uint8_t whichVbr = ds.Get<uint8_t>() % 3;
if ( whichVbr == 0 ) {
setBitrateModeVBR_RH();
} else if ( whichVbr == 1 ) {
setBitrateModeVBR_MTRH();
} else if ( whichVbr == 2 ) {
/* Disabled due to crash */ throw std::runtime_error("");
setBitrateModeVBR_ABR();
}
setVBRQ();
size_t minBitrate = setMinBitrate();
setMaxBitrate(minBitrate);
}
void setBitrateModeCBR(void) {
_(lame_set_VBR(flags, vbr_off););
const size_t bitrate = limits::CBRBitrate.Generate(ds);
Debug ? printf("lame_set_brate(flags, %zu);\n", bitrate) : 0;
lame_set_brate(flags, bitrate);
}
void setBitrateMode(void) {
ds.Get<bool>() ? setBitrateModeVBR() : setBitrateModeCBR();
}
void setInputChannels(void) {
const int numChannels = ds.Get<bool>() ? 1 : 2;
Debug ? printf("lame_set_num_channels(flags, %d);\n", numChannels) : 0;
lame_set_num_channels(flags, numChannels);
if ( numChannels == 1 ) {
mono = true;
}
}
void setChannelMode(void) {
const uint8_t whichChannelMode = ds.Get<uint8_t>() % 3;
if ( whichChannelMode == 0 ) {
_(lame_set_mode(flags, STEREO););
} else if ( whichChannelMode == 1 ) {
_(lame_set_mode(flags, JOINT_STEREO););
} else if ( whichChannelMode == 2 ) {
_(lame_set_mode(flags, MONO););
}
}
void setQuality(void) {
const size_t quality = limits::Quality.Generate<uint8_t>(ds);
Debug ? printf("lame_set_quality(flags, %zu);\n", quality) : 0;
lame_set_quality(flags, quality);
}
void setOutSamplerate(void) {
const size_t outSamplerate = limits::OutSamplerate.Generate(ds);
Debug ? printf("lame_set_out_samplerate(flags, %zu);\n", outSamplerate) : 0;
lame_set_out_samplerate(flags, outSamplerate);
}
void setID3(void) {
/* Optionally set various ID3 fields */
if ( ds.Get<bool>() ) {
id3tag_init(flags);
if ( ds.Get<bool>() ) {
const std::string title = ds.Get<std::string>();
Debug ? printf("id3tag_set_title(flags, %s);\n", title.c_str()) : 0;
id3tag_set_title(flags, title.c_str());
}
if ( ds.Get<bool>() ) {
const std::string artist = ds.Get<std::string>();
Debug ? printf("id3tag_set_artist(flags, %s);\n", artist.c_str()) : 0;
id3tag_set_artist(flags, artist.c_str());
}
if ( ds.Get<bool>() ) {
const std::string album = ds.Get<std::string>();
Debug ? printf("id3tag_set_album(flags, %s);\n", album.c_str()) : 0;
id3tag_set_album(flags, album.c_str());
}
if ( ds.Get<bool>() ) {
const std::string year = ds.Get<std::string>();
Debug ? printf("id3tag_set_year(flags, %s);\n", year.c_str()) : 0;
id3tag_set_year(flags, year.c_str());
}
if ( ds.Get<bool>() ) {
const std::string comment = ds.Get<std::string>();
Debug ? printf("id3tag_set_comment(flags, %s);\n", comment.c_str()) : 0;
id3tag_set_comment(flags, comment.c_str());
}
if ( ds.Get<bool>() ) {
const std::string track = ds.Get<std::string>();
Debug ? printf("id3tag_set_track(flags, %s);\n", track.c_str()) : 0;
id3tag_set_track(flags, track.c_str());
}
if ( ds.Get<bool>() ) {
const std::string genre = ds.Get<std::string>();
Debug ? printf("id3tag_set_genre(flags, %s);\n", genre.c_str()) : 0;
id3tag_set_genre(flags, genre.c_str());
}
if ( ds.Get<bool>() ) {
const std::string fieldvalue = ds.Get<std::string>();
Debug ? printf("id3tag_set_fieldvalue(flags, %s);\n", fieldvalue.c_str()) : 0;
id3tag_set_fieldvalue(flags, fieldvalue.c_str());
}
if ( ds.Get<bool>() ) {
const auto albumArt = ds.GetData(0);
Debug ? printf("{\n") : 0;
Debug ? printf("\t%s\n", debug_define_size_t("albumArtSize", albumArt.size()).c_str()) : 0;
Debug ? printf("%s\n", DebugDefineArray<unsigned char>::Str("albumart", "char", albumArt.data(), albumArt.size(), true).c_str()) : 0;
Debug ?
printf("\tid3tag_set_albumart(flags, albumArt, albumArtSize);\n")
: 0;
Debug ? printf("}\n") : 0;
id3tag_set_albumart(flags, (const char*)albumArt.data(), albumArt.size());
}
}
}
void setFilters(void) {
if ( ds.Get<bool>() ) {
const size_t lowpassFreq = limits::LowpassFrequency.Generate(ds);
Debug ? printf("lame_set_lowpassfreq(flags, %zu);\n", lowpassFreq) : 0;
lame_set_lowpassfreq(flags, lowpassFreq);
}
if ( ds.Get<bool>() ) {
const size_t lowpassWidth = limits::LowpassWidth.Generate(ds);
Debug ? printf("lame_set_lowpasswidth(flags, %zu);\n", lowpassWidth) : 0;
lame_set_lowpasswidth(flags, lowpassWidth);
}
if ( ds.Get<bool>() ) {
const size_t highpassFreq = limits::HighpassFrequency.Generate(ds);
Debug ? printf("lame_set_highpassfreq(flags, %zu);\n", highpassFreq) : 0;
lame_set_highpassfreq(flags, highpassFreq);
}
if ( ds.Get<bool>() ) {
const size_t highpassWidth = limits::HighpassWidth.Generate(ds);
Debug ? printf("lame_set_highpasswidth(flags, %zu);\n", highpassWidth) : 0;
lame_set_highpasswidth(flags, highpassWidth);
}
}
void setMisc(void) {
if ( ds.Get<bool>() ) {
_(lame_set_strict_ISO(flags, MDB_STRICT_ISO););
}
if ( ds.Get<bool>() ) {
_(lame_set_bWriteVbrTag(flags, 1););
}
if ( ds.Get<bool>() ) {
_(lame_set_copyright(flags, 1););
}
if ( ds.Get<bool>() ) {
_(lame_set_original(flags, 1););
}
if ( ds.Get<bool>() ) {
_(lame_set_error_protection(flags, 1););
}
if ( ds.Get<bool>() ) {
_(lame_set_extension(flags, 1););
}
if ( ds.Get<bool>() ) {
/* Crashes */
/* _(lame_set_free_format(flags, 1);); */
}
}
public:
EncoderFuzzer(Datasource& ds) :
ds(ds), outBufferSize(limits::OutBufferSize.Generate(ds))
{
Debug ?
printf("lame_global_flags* flags = lame_init();\n")
: 0;
flags = lame_init();
Debug ?
printf("const size_t outBufferSize = %zu;\n", outBufferSize)
: 0;
Debug ?
printf("unsigned char outBuffer[outBufferSize];\n")
: 0;
outBuffer = (uint8_t*)malloc(outBufferSize + 1024 /* Add 1024 due to crash */);
}
void Run(void) {
std::unique_ptr<EncoderCoreBase> encoder = nullptr;
const uint8_t whichSampleSize = ds.Get<uint8_t>() % 5;
if ( whichSampleSize == 0 ) {
encoder = std::make_unique<EncoderCore<short int, Debug>>(ds, flags);
} else if ( whichSampleSize == 1 ) {
encoder = std::make_unique<EncoderCore<int, Debug>>(ds, flags);
} else if ( whichSampleSize == 2 ) {
encoder = std::make_unique<EncoderCore<long, Debug>>(ds, flags);
} else if ( whichSampleSize == 3 ) {
encoder = std::make_unique<EncoderCore<float, Debug>>(ds, flags);
} else if ( whichSampleSize == 4 ) {
encoder = std::make_unique<EncoderCore<double, Debug>>(ds, flags);
}
setInputChannels();
setBitrateMode();