-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzz_target.cpp
1078 lines (944 loc) · 30.8 KB
/
fuzz_target.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
#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef USE_LIBPROTOBUF_MUTATOR
#include <src/libfuzzer/libfuzzer_macro.h>
#endif
#include <zlib.h>
#ifdef USE_LIBPROTOBUF_MUTATOR
#include "fuzz_target.pb.h"
#endif
#ifdef __cplusplus
#define EXTERN_C extern "C"
#else
#define EXTERN_C
#endif
/* Constants. */
#define MEM_LEVEL_DEFAULT 0
#define MEM_LEVEL8 8
#ifdef USE_LIBPROTOBUF_MUTATOR
static_assert(PB_DEFLATE_Z_NO_FLUSH == Z_NO_FLUSH);
static_assert(PB_DEFLATE_Z_PARTIAL_FLUSH == Z_PARTIAL_FLUSH);
static_assert(PB_DEFLATE_Z_SYNC_FLUSH == Z_SYNC_FLUSH);
static_assert(PB_DEFLATE_Z_FULL_FLUSH == Z_FULL_FLUSH);
static_assert(PB_DEFLATE_Z_BLOCK == Z_BLOCK);
static_assert(PB_INFLATE_Z_NO_FLUSH == Z_NO_FLUSH);
static_assert(PB_Z_NO_COMPRESSION == Z_NO_COMPRESSION);
static_assert(PB_Z_BEST_SPEED == Z_BEST_SPEED);
static_assert(PB_Z_BEST_COMPRESSION == Z_BEST_COMPRESSION);
static_assert(PB_Z_DEFAULT_COMPRESSION == Z_DEFAULT_COMPRESSION);
static_assert(PB_Z_DEFAULT_STRATEGY == Z_DEFAULT_STRATEGY);
static_assert(PB_Z_FILTERED == Z_FILTERED);
static_assert(PB_Z_HUFFMAN_ONLY == Z_HUFFMAN_ONLY);
static_assert(PB_Z_RLE == Z_RLE);
static_assert(PB_Z_FIXED == Z_FIXED);
static_assert(PB_MEM_LEVEL_DEFAULT == MEM_LEVEL_DEFAULT);
static_assert(PB_MEM_LEVEL8 == MEM_LEVEL8);
#endif
/* Window size utilities. */
static bool IsWbRaw(int WindowBits) {
return WindowBits >= -15 && WindowBits <= -9;
}
static bool IsWbZlib(int WindowBits) {
return WindowBits >= 9 && WindowBits <= 15;
}
static bool IsWbGzip(int WindowBits) {
return WindowBits >= (16 + 9) && WindowBits <= (16 + 15);
}
static int FixupWindowBits(int WindowBits) {
if (IsWbRaw(WindowBits) || IsWbZlib(WindowBits) || IsWbGzip(WindowBits))
return WindowBits;
else
return 15;
}
/* Dumping. */
static int Debug;
__attribute__((constructor)) static void Init() {
const char *Env = getenv("DEBUG");
if (Env)
Debug = atoi(Env);
}
static int Indent = 0;
static void Print(FILE *Stream, const char *Fmt, ...) {
va_list VaList;
va_start(VaList, Fmt);
fprintf(Stream, "%*s", Indent * 2, "");
vfprintf(Stream, Fmt, VaList);
va_end(VaList);
}
static void HexDump(FILE *Stream, const void *Data, size_t Size) {
for (size_t i = 0; i < Size; i++)
fprintf(Stream, "\\x%02x", ((const uint8_t *)Data)[i]);
}
static void HexDumpCStr(FILE *Stream, const void *Data, size_t Size) {
if (Size == 0) {
fprintf(Stream, "\"\"");
return;
}
const size_t ChunkSize = 16;
bool IndentIncremented = false;
for (size_t i = 0; i < Size; i += ChunkSize) {
if (i == 0)
fprintf(Stream, "\"");
else {
fprintf(Stream, "\n");
if (!IndentIncremented) {
Indent++;
IndentIncremented = true;
}
Print(Stream, "\"");
}
HexDump(Stream, ((const uint8_t *)Data) + i,
(Size - i) < ChunkSize ? (Size - i) : ChunkSize);
fprintf(Stream, "\"");
}
if (IndentIncremented)
Indent--;
}
static void Dump(FILE *Stream, const char *Name, const void *Data,
size_t Size) {
Print(Stream, "unsigned char %s[%zu]", Name, Size);
if (Debug & 2) {
FILE *File = fopen(Name, "wb");
assert(File);
assert(fwrite(Data, 1, Size, File) == Size);
assert(fclose(File) == 0);
fprintf(Stream, ";\n");
Print(Stream, "{\n");
Indent++;
Print(Stream, "FILE *File = fopen(\"%s\", \"rb\");\n", Name);
Print(Stream, "assert(File);\n");
Print(Stream, "assert(fread(%s, 1, %zu, File) == %zu);\n", Name, Size,
Size);
Print(Stream, "assert(fclose(File) == 0);\n");
Indent--;
Print(Stream, "}\n");
} else {
fprintf(Stream, " = ");
HexDumpCStr(Stream, Data, Size);
fprintf(Stream, ";\n");
}
}
static const char *StrategyStr(int Strategy) {
switch (Strategy) {
case Z_FILTERED:
return "Z_FILTERED";
case Z_HUFFMAN_ONLY:
return "Z_HUFFMAN_ONLY";
case Z_RLE:
return "Z_RLE";
case Z_FIXED:
return "Z_FIXED";
case Z_DEFAULT_STRATEGY:
return "Z_DEFAULT_STRATEGY";
default:
return "<unknown>";
}
}
static const char *FlushStr(int Flush) {
switch (Flush) {
case Z_NO_FLUSH:
return "Z_NO_FLUSH";
case Z_PARTIAL_FLUSH:
return "Z_PARTIAL_FLUSH";
case Z_SYNC_FLUSH:
return "Z_SYNC_FLUSH";
case Z_FULL_FLUSH:
return "Z_FULL_FLUSH";
case Z_FINISH:
return "Z_FINISH";
case Z_BLOCK:
return "Z_BLOCK";
case Z_TREES:
return "Z_TREES";
default:
return "<unknown>";
}
}
static const char *ErrStr(int Err) {
switch (Err) {
case Z_OK:
return "Z_OK";
break;
case Z_STREAM_END:
return "Z_STREAM_END";
break;
case Z_NEED_DICT:
return "Z_NEED_DICT";
break;
case Z_ERRNO:
return "Z_ERRNO";
break;
case Z_STREAM_ERROR:
return "Z_STREAM_ERROR";
break;
case Z_DATA_ERROR:
return "Z_DATA_ERROR";
break;
case Z_MEM_ERROR:
return "Z_MEM_ERROR";
break;
case Z_BUF_ERROR:
return "Z_BUF_ERROR";
break;
case Z_VERSION_ERROR:
return "Z_VERSION_ERROR";
break;
default:
return "<unknown>";
}
}
static int DeflateInit2(z_stream *Strm, size_t *Idx, int Level, int Method,
int WindowBits, int MemLevel, int Strategy) {
if (Debug)
Print(stderr,
"assert(deflateInit2(&Strm[%zu], %i, %i, %i, %i, %s) == ", *Idx,
Level, Method, WindowBits, MemLevel, StrategyStr(Strategy));
int Err =
deflateInit2(&Strm[*Idx], Level, Method, WindowBits, MemLevel, Strategy);
if (Debug)
fprintf(stderr, "%s);\n", ErrStr(Err));
return Err;
}
static int DeflateSetDictionary(z_stream *Strm, size_t *Idx, const Bytef *Dict,
size_t DictLen) {
if (Debug)
Print(stderr,
"assert(deflateSetDictionary(&Strm[%zu], Dict, %zu) == ", *Idx,
DictLen);
int Err = deflateSetDictionary(&Strm[*Idx], Dict, DictLen);
if (Debug)
fprintf(stderr, "%s);\n", ErrStr(Err));
return Err;
}
static int Deflate(z_stream *Strm, size_t *Idx, int Flush) {
if (Debug)
Print(stderr,
"Strm[%zu].avail_in = %u; Strm[%zu].avail_out = %u; "
"assert(deflate(&Strm[%zu], "
"%s) == ",
*Idx, Strm->avail_in, *Idx, Strm->avail_out, *Idx, FlushStr(Flush));
int Err = deflate(&Strm[*Idx], Flush);
if (Debug)
fprintf(stderr, "%s);\n", ErrStr(Err));
return Err;
}
static int DeflateParams(z_stream *Strm, size_t *Idx, int Level, int Strategy) {
if (Debug)
Print(stderr,
"Strm[%zu].avail_in = %u; Strm[%zu].avail_out = %u; "
"assert(deflateParams(&Strm[%zu], %i, %s) == ",
*Idx, Strm[*Idx].avail_in, *Idx, Strm[*Idx].avail_out, *Idx, Level,
StrategyStr(Strategy));
int Err = deflateParams(&Strm[*Idx], Level, Strategy);
if (Debug)
fprintf(stderr, "%s);\n", ErrStr(Err));
return Err;
}
static int DeflateCopy(z_stream *Strm, size_t *Idx) {
if (Debug)
Print(stderr, "assert(deflateCopy(&Strm[%zu], &Strm[%zu]) == ", 1 - *Idx,
*Idx);
int Err = deflateCopy(&Strm[1 - *Idx], &Strm[*Idx]);
if (Debug) {
fprintf(stderr, "%s);\n", ErrStr(Err));
Print(stderr, "deflateEnd(&Strm[%zu]);\n", *Idx);
}
deflateEnd(&Strm[*Idx]);
*Idx = 1 - *Idx;
return Err;
}
static int InflateSetDictionary(z_stream *Strm, size_t *Idx, const Bytef *Dict,
size_t DictLen) {
if (Debug) {
Print(stderr,
"assert(inflateSetDictionary(&Strm[%zu], Dict, %zu) == ", *Idx,
DictLen);
}
int Err = inflateSetDictionary(&Strm[*Idx], Dict, DictLen);
if (Debug)
fprintf(stderr, "%s);\n", ErrStr(Err));
return Err;
}
static int Inflate(z_stream *Strm, size_t *Idx, int Flush) {
if (Debug)
Print(stderr,
"Strm[%zu].avail_in = %u; Strm[%zu].avail_out = %u; "
"assert(inflate(&Strm[%zu], "
"%s) == ",
*Idx, Strm[*Idx].avail_in, *Idx, Strm[*Idx].avail_out, *Idx,
FlushStr(Flush));
int Err = inflate(&Strm[*Idx], Flush);
if (Debug)
fprintf(stderr, "%s);\n", ErrStr(Err));
return Err;
}
static int InflateCopy(z_stream *Strm, size_t *Idx) {
if (Debug)
Print(stderr, "assert(inflateCopy(&Strm[%zu], &Strm[%zu]) == ", 1 - *Idx,
*Idx);
int Err = inflateCopy(&Strm[1 - *Idx], &Strm[*Idx]);
if (Debug) {
fprintf(stderr, "%s);\n", ErrStr(Err));
Print(stderr, "inflateEnd(&Strm[%zu]);\n", *Idx);
}
inflateEnd(&Strm[*Idx]);
*Idx = 1 - *Idx;
return Err;
}
/* Restricting avail_in / avail_out. */
struct Avail {
z_stream *Strm;
uInt AvailIn0;
uInt AvailIn1;
uInt AvailOut0;
uInt AvailOut1;
};
void AvailInit(struct Avail *Self, z_stream *Strm, uInt MaxAvailIn,
uInt MaxAvailOut) {
Self->Strm = Strm;
Self->AvailIn0 = Strm->avail_in;
Self->AvailIn1 = Self->AvailIn0 < MaxAvailIn ? Self->AvailIn0 : MaxAvailIn;
Self->AvailOut0 = Strm->avail_out;
Self->AvailOut1 =
Self->AvailOut0 < MaxAvailOut ? Self->AvailOut0 : MaxAvailOut;
Strm->avail_in = Self->AvailIn1;
Strm->avail_out = Self->AvailOut1;
}
void AvailEnd(struct Avail *Self) {
uInt ConsumedIn = Self->AvailIn1 - Self->Strm->avail_in;
Self->Strm->avail_in = Self->AvailIn0 - ConsumedIn;
uInt ConsumedOut = Self->AvailOut1 - Self->Strm->avail_out;
Self->Strm->avail_out = Self->AvailOut0 - ConsumedOut;
}
/* libprotobuf-mutator and libFuzzer adapters. */
enum DeflateOpType {
DeflateOpTypeNone,
DeflateOpTypeDeflate,
DeflateOpTypeDeflateParams,
DeflateOpTypeDeflateCopy,
DeflateOpTypeMax,
};
enum InflateOpType {
InflateOpTypeNone,
InflateOpTypeInflate,
InflateOpTypeInflateCopy,
InflateOpTypeMax,
};
#ifdef USE_LIBPROTOBUF_MUTATOR
struct PlanExecution {
const PbPlan *Plan;
size_t DeflateDictPieceIdx;
size_t DeflateOpIdx;
size_t InflateDictPieceIdx;
size_t InflateOpIdx;
size_t FinishOpIdx;
size_t BitFlipIdx;
};
static void PlanExecutionInit(struct PlanExecution *PE, const PbPlan *Plan) {
PE->Plan = Plan;
PE->DeflateDictPieceIdx = 0;
PE->DeflateOpIdx = 0;
PE->InflateDictPieceIdx = 0;
PE->InflateOpIdx = 0;
PE->FinishOpIdx = 0;
PE->BitFlipIdx = 0;
}
static const char *GetPlainData(struct PlanExecution *PE) {
return PE->Plan->data().c_str();
}
static size_t GetPlainDataSize(struct PlanExecution *PE) {
return PE->Plan->data().size();
}
static int GetInitialLevel(struct PlanExecution *PE) {
return PE->Plan->level();
}
static int GetWindowBits(struct PlanExecution *PE) {
return PE->Plan->window_bits();
}
static int GetMemLevel(struct PlanExecution *PE) {
return PE->Plan->mem_level();
}
static int GetInitialStrategy(struct PlanExecution *PE) {
return PE->Plan->strategy();
}
static const char *GetDict(struct PlanExecution *PE) {
return PE->Plan->dict().c_str();
}
static size_t GetDictSize(struct PlanExecution *PE) {
return PE->Plan->dict().size();
}
static size_t GetDeflateDictPiecesCount(struct PlanExecution *PE) {
return PE->Plan->deflate_dict_pieces_size();
}
static uInt GetDeflateDictPiece(struct PlanExecution *PE) {
return PE->Plan->deflate_dict_pieces(PE->DeflateDictPieceIdx++);
}
static size_t GetDeflateOpCount(struct PlanExecution *PE) {
return PE->Plan->deflate_ops_size();
}
static void NextDeflateOp(struct PlanExecution *PE) { PE->DeflateOpIdx++; }
static enum DeflateOpType GetDeflateOpType(struct PlanExecution *PE) {
int op_case = PE->Plan->deflate_ops(PE->DeflateOpIdx).op_case();
switch (op_case) {
case PbDeflateOp::OP_NOT_SET:
return DeflateOpTypeNone;
case PbDeflateOp::kDeflate:
return DeflateOpTypeDeflate;
case PbDeflateOp::kDeflateParams:
return DeflateOpTypeDeflateParams;
case PbDeflateOp::kDeflateCopy:
return DeflateOpTypeDeflateCopy;
default:
fprintf(stderr, "Unexpected PbDeflateOp->op_case: %i\n", op_case);
assert(0);
}
}
static uInt GetDeflateAvailIn(struct PlanExecution *PE) {
return PE->Plan->deflate_ops(PE->DeflateOpIdx).deflate().avail_in();
}
static uInt GetDeflateAvailOut(struct PlanExecution *PE) {
return PE->Plan->deflate_ops(PE->DeflateOpIdx).deflate().avail_out();
}
static int GetDeflateFlush(struct PlanExecution *PE) {
return PE->Plan->deflate_ops(PE->DeflateOpIdx).deflate().flush();
}
static uInt GetDeflateParamsAvailIn(struct PlanExecution *PE) {
return PE->Plan->deflate_ops(PE->DeflateOpIdx).deflate_params().avail_in();
}
static uInt GetDeflateParamsAvailOut(struct PlanExecution *PE) {
return PE->Plan->deflate_ops(PE->DeflateOpIdx).deflate_params().avail_out();
}
static int GetDeflateParamsLevel(struct PlanExecution *PE) {
return PE->Plan->deflate_ops(PE->DeflateOpIdx).deflate_params().level();
}
static int GetDeflateParamsStrategy(struct PlanExecution *PE) {
return PE->Plan->deflate_ops(PE->DeflateOpIdx).deflate_params().strategy();
}
static size_t GetFinishOpCount(struct PlanExecution *PE) {
return PE->Plan->finish_avail_outs().size();
}
static void NextFinishOp(struct PlanExecution *PE) { PE->FinishOpIdx++; }
static uInt GetFinishAvailOut(struct PlanExecution *PE) {
return PE->Plan->finish_avail_outs(PE->FinishOpIdx);
}
static void ResetInflateDictPieces(struct PlanExecution *PE) {
PE->InflateDictPieceIdx = 0;
}
static size_t GetInflateDictPiecesCount(struct PlanExecution *PE) {
return PE->Plan->inflate_dict_pieces_size();
}
static uInt GetInflateDictPiece(struct PlanExecution *PE) {
return PE->Plan->inflate_dict_pieces(PE->InflateDictPieceIdx++);
}
static void ResetInflateOps(struct PlanExecution *PE) { PE->InflateOpIdx = 0; }
static size_t GetInflateOpCount(struct PlanExecution *PE) {
return PE->Plan->inflate_ops_size();
}
static void NextInflateOp(struct PlanExecution *PE) { PE->InflateOpIdx++; }
static enum InflateOpType GetInflateOpType(struct PlanExecution *PE) {
int op_case = PE->Plan->inflate_ops(PE->InflateOpIdx).op_case();
switch (op_case) {
case PbInflateOp::OP_NOT_SET:
return InflateOpTypeNone;
case PbInflateOp::kInflate:
return InflateOpTypeInflate;
case PbInflateOp::kInflateCopy:
return InflateOpTypeInflateCopy;
default:
fprintf(stderr, "Unexpected PbInflateOp->op_case: %i\n", op_case);
assert(0);
}
}
static uInt GetInflateAvailIn(struct PlanExecution *PE) {
return PE->Plan->inflate_ops(PE->InflateOpIdx).inflate().avail_in();
}
static uInt GetInflateAvailOut(struct PlanExecution *PE) {
return PE->Plan->inflate_ops(PE->InflateOpIdx).inflate().avail_out();
}
static int GetInflateFlush(struct PlanExecution *PE) {
return PE->Plan->inflate_ops(PE->InflateOpIdx).inflate().flush();
}
static int GetTailSize(struct PlanExecution *PE) {
return PE->Plan->tail_size();
}
static size_t GetBitFlipCount(struct PlanExecution *PE) {
return PE->Plan->bit_flips().size();
}
static void NextBitFlip(struct PlanExecution *PE) { PE->BitFlipIdx++; }
static uInt GetBitFlip(struct PlanExecution *PE) {
return PE->Plan->bit_flips(PE->BitFlipIdx);
}
#else
struct PlanExecution {
const uint8_t *Data;
size_t Size;
const char *PlainData;
size_t PlainDataSize;
int WindowBits;
const char *Dict;
size_t DictSize;
};
#define POP(PE, Type, Default) \
({ \
Type __Result; \
if ((PE)->Size < sizeof(__Result)) { \
__Result = (Default); \
} else { \
memcpy(&__Result, (PE)->Data, sizeof(__Result)); \
(PE)->Data += sizeof(__Result); \
(PE)->Size -= sizeof(__Result); \
} \
__Result; \
})
static int ChooseLevel(uint8_t Choice) {
if (Choice < 128)
return (Choice % 11) - 1;
else
return Z_BEST_SPEED;
}
static int ChooseMemLevel(uint8_t Choice) { return (Choice % 9) + 1; }
static int ChooseStrategy(uint8_t Choice) {
if (Choice < 43)
return Z_FILTERED;
else if (Choice < 86)
return Z_HUFFMAN_ONLY;
else if (Choice < 128)
return Z_RLE;
else if (Choice < 196)
return Z_FIXED;
else
return Z_DEFAULT_STRATEGY;
}
static enum DeflateOpType ChooseDeflateOp(uint8_t Choice) {
if (Choice < 120)
return DeflateOpTypeDeflate;
else if (Choice < 240)
return DeflateOpTypeDeflateParams;
else
return DeflateOpTypeDeflateCopy;
}
static int ChooseDeflateFlush(uint8_t Choice) {
if (Choice < 32)
return Z_PARTIAL_FLUSH;
else if (Choice < 64)
return Z_SYNC_FLUSH;
else if (Choice < 96)
return Z_FULL_FLUSH;
else if (Choice < 128)
return Z_BLOCK;
else
return Z_NO_FLUSH;
}
static void PlanExecutionInit(struct PlanExecution *PE, const uint8_t *Data,
size_t Size) {
if (Debug)
Print(stderr, "/* size == %zu; */\n", Size);
PE->Data = Data;
PE->Size = Size;
uint16_t Choice = POP(PE, uint16_t, 0);
PE->PlainData = (const char *)PE->Data;
PE->PlainDataSize = PE->Size * (Choice + 0x2000) / 0x10000;
if (PE->PlainDataSize > PE->Size)
PE->PlainDataSize = PE->Size;
PE->Data += PE->PlainDataSize;
PE->Size -= PE->PlainDataSize;
PE->WindowBits = FixupWindowBits(POP(PE, int8_t, 0));
PE->Dict = NULL;
PE->DictSize = 0;
if (!IsWbGzip(PE->WindowBits)) {
size_t DictSize = POP(PE, uint16_t, 0);
if (DictSize > 0 && DictSize < 1024) {
size_t MaxDictSize = PE->Size / 4;
if (DictSize > MaxDictSize)
DictSize = MaxDictSize;
PE->Dict = (const char *)PE->Data;
PE->DictSize = DictSize;
PE->Data += DictSize;
PE->Size -= DictSize;
}
}
}
static const char *GetPlainData(struct PlanExecution *PE) {
return PE->PlainData;
}
static size_t GetPlainDataSize(struct PlanExecution *PE) {
return PE->PlainDataSize;
}
static int GetInitialLevel(struct PlanExecution *PE) {
return ChooseLevel(POP(PE, uint8_t, 0));
}
static int GetWindowBits(struct PlanExecution *PE) { return PE->WindowBits; }
static int GetMemLevel(struct PlanExecution *PE) {
return ChooseMemLevel(POP(PE, uint8_t, 0));
}
static int GetInitialStrategy(struct PlanExecution *PE) {
return ChooseStrategy(POP(PE, uint8_t, 0));
}
static const char *GetDict(struct PlanExecution *PE) { return PE->Dict; }
static size_t GetDictSize(struct PlanExecution *PE) { return PE->DictSize; }
static size_t GetDeflateDictPiecesCount(struct PlanExecution *PE) {
return POP(PE, uint8_t, 0);
}
static uInt GetDeflateDictPiece(struct PlanExecution *PE) {
return POP(PE, uint16_t, 0);
}
static size_t GetDeflateOpCount(struct PlanExecution *PE) {
return POP(PE, uint8_t, 0);
}
static void NextDeflateOp(struct PlanExecution *PE) { (void)PE; }
static enum DeflateOpType GetDeflateOpType(struct PlanExecution *PE) {
return ChooseDeflateOp(POP(PE, uint8_t, 0));
}
static uInt GetDeflateAvailIn(struct PlanExecution *PE) {
return POP(PE, uint8_t, 0);
}
static uInt GetDeflateAvailOut(struct PlanExecution *PE) {
return POP(PE, uint8_t, 0);
}
static int GetDeflateFlush(struct PlanExecution *PE) {
return ChooseDeflateFlush(POP(PE, uint8_t, 0xff));
}
static uInt GetDeflateParamsAvailIn(struct PlanExecution *PE) {
return POP(PE, uint8_t, 0);
}
static uInt GetDeflateParamsAvailOut(struct PlanExecution *PE) {
return POP(PE, uint8_t, 0);
}
static int GetDeflateParamsLevel(struct PlanExecution *PE) {
return ChooseLevel(POP(PE, uint8_t, 0xff));
}
static int GetDeflateParamsStrategy(struct PlanExecution *PE) {
return ChooseStrategy(POP(PE, uint8_t, 0xff));
}
static size_t GetFinishOpCount(struct PlanExecution *PE) {
return POP(PE, uint8_t, 0);
}
static void NextFinishOp(struct PlanExecution *PE) { (void)PE; }
static uInt GetFinishAvailOut(struct PlanExecution *PE) {
return POP(PE, uint8_t, 0);
}
static void ResetInflateDictPieces(struct PlanExecution *PE) { (void)PE; }
static size_t GetInflateDictPiecesCount(struct PlanExecution *PE) {
return POP(PE, uint8_t, 0);
}
static uInt GetInflateDictPiece(struct PlanExecution *PE) {
return POP(PE, uint16_t, 0);
}
static void ResetInflateOps(struct PlanExecution *PE) { (void)PE; }
static size_t GetInflateOpCount(struct PlanExecution *PE) {
return POP(PE, uint8_t, 0);
}
static void NextInflateOp(struct PlanExecution *PE) { (void)PE; }
static enum InflateOpType GetInflateOpType(struct PlanExecution *PE) {
return (enum InflateOpType)(POP(PE, uint8_t, 0) % InflateOpTypeMax);
}
static uInt GetInflateAvailIn(struct PlanExecution *PE) {
return POP(PE, uint8_t, 0);
}
static uInt GetInflateAvailOut(struct PlanExecution *PE) {
return POP(PE, uint8_t, 0);
}
static int GetInflateFlush(struct PlanExecution *PE) {
(void)PE;
return Z_NO_FLUSH;
}
static int GetTailSize(struct PlanExecution *PE) { return POP(PE, uint8_t, 0); }
static size_t GetBitFlipCount(struct PlanExecution *PE) {
return POP(PE, uint8_t, 0);
}
static void NextBitFlip(struct PlanExecution *PE) { (void)PE; }
static uInt GetBitFlip(struct PlanExecution *PE) {
return POP(PE, uint16_t, 0);
}
#endif
/* Common fuzzing logic. */
static int RunDeflateOp(z_stream *Strm, size_t *Idx, struct PlanExecution *PE,
bool Check) {
switch (GetDeflateOpType(PE)) {
case DeflateOpTypeNone:
return 0;
case DeflateOpTypeDeflate: {
struct Avail Avail;
AvailInit(&Avail, &Strm[*Idx], GetDeflateAvailIn(PE),
GetDeflateAvailOut(PE));
int Err = Deflate(Strm, Idx, GetDeflateFlush(PE));
AvailEnd(&Avail);
if (Check)
assert(Err == Z_OK || Err == Z_BUF_ERROR);
return Err;
}
case DeflateOpTypeDeflateParams: {
struct Avail Avail;
AvailInit(&Avail, &Strm[*Idx], GetDeflateParamsAvailIn(PE),
GetDeflateParamsAvailOut(PE));
int Err = DeflateParams(Strm, Idx, GetDeflateParamsLevel(PE),
GetDeflateParamsStrategy(PE));
AvailEnd(&Avail);
if (Check)
assert(Err == Z_OK || Err == Z_BUF_ERROR);
return Err;
}
case DeflateOpTypeDeflateCopy: {
int Err = DeflateCopy(Strm, Idx);
if (Check)
assert(Err == Z_OK);
return Err;
}
default:
fprintf(stderr, "Unexpected DeflateOp\n");
assert(0);
}
}
static int RunInflateOp(z_stream *Strm, size_t *Idx, struct PlanExecution *PE,
bool Check) {
switch (GetInflateOpType(PE)) {
case InflateOpTypeNone:
return 0;
case InflateOpTypeInflate: {
struct Avail Avail;
AvailInit(&Avail, &Strm[*Idx], GetInflateAvailIn(PE),
GetInflateAvailOut(PE));
int Err = Inflate(Strm, Idx, GetInflateFlush(PE));
AvailEnd(&Avail);
if (Check)
assert(Err == Z_OK || Err == Z_STREAM_END || Err == Z_NEED_DICT ||
Err == Z_BUF_ERROR);
return Err;
}
case InflateOpTypeInflateCopy: {
int Err = InflateCopy(Strm, Idx);
if (Check)
assert(Err == Z_OK);
return Err;
}
default:
fprintf(stderr, "Unexpected InflateOp\n");
assert(0);
}
}
static void ExecutePlanInflate(struct PlanExecution *PE,
const uint8_t *Compressed,
uInt ActualCompressedSize, bool Check) {
ResetInflateDictPieces(PE);
ResetInflateOps(PE);
int InflateOpCount = GetInflateOpCount(PE);
z_stream Strm[2];
size_t Idx = 0;
if (Debug) {
Print(stderr, "/* n_inflate_ops == %i; */\n", InflateOpCount);
Print(stderr, "Strm[%zu].next_in = Compressed;\n", Idx);
Print(stderr, "Strm[%zu].next_out = Plain;\n", Idx);
}
memset(&Strm, 0, sizeof(Strm));
int WindowBits = FixupWindowBits(GetWindowBits(PE));
int Err = inflateInit2(&Strm[Idx], WindowBits);
if (Debug)
Print(stderr, "assert(inflateInit2(&Strm[%zu], %i) == %s);\n", Idx,
WindowBits, ErrStr(Err));
assert(Err == Z_OK);
if (GetDictSize(PE) > 0 && IsWbRaw(WindowBits)) {
size_t PiecesCount = GetInflateDictPiecesCount(PE);
size_t Offset = 0;
for (size_t i = 0; i <= PiecesCount && Offset < GetDictSize(PE); i++) {
uInt Piece;
if (i < PiecesCount) {
Piece = GetInflateDictPiece(PE);
if (Offset + Piece > GetDictSize(PE))
Piece = GetDictSize(PE) - Offset;
} else {
Piece = GetDictSize(PE) - Offset;
}
int Err = InflateSetDictionary(
Strm, &Idx, (const Bytef *)GetDict(PE) + Offset, Piece);
if (Check)
assert(Err == Z_OK);
Offset += Piece;
}
assert(Offset == GetDictSize(PE));
}
size_t TailSize = GetTailSize(PE) & 0xff;
uint8_t *Uncompressed = (uint8_t *)calloc(GetPlainDataSize(PE) + TailSize, 1);
assert(Uncompressed);
Strm[Idx].next_in = Compressed;
Strm[Idx].avail_in = ActualCompressedSize;
Strm[Idx].next_out = Uncompressed;
Strm[Idx].avail_out = GetPlainDataSize(PE) + TailSize;
for (int i = 0; i < InflateOpCount; i++, NextInflateOp(PE)) {
Err = RunInflateOp(Strm, &Idx, PE, Check);
if (Err == Z_NEED_DICT) {
if (Check)
assert(GetDictSize(PE) > 0 && IsWbZlib(WindowBits));
Err = InflateSetDictionary(Strm, &Idx, (const Bytef *)GetDict(PE),
GetDictSize(PE));
if (Check)
assert(Err == Z_OK);
}
}
if (Err != Z_STREAM_END) {
Err = Inflate(Strm, &Idx, Z_NO_FLUSH);
if (Err == Z_NEED_DICT) {
if (Check)
assert(GetDictSize(PE) > 0 && IsWbZlib(WindowBits));
Err = InflateSetDictionary(Strm, &Idx, (const Bytef *)GetDict(PE),
GetDictSize(PE));
if (Check)
assert(Err == Z_OK);
Err = Inflate(Strm, &Idx, Z_NO_FLUSH);
}
}
if (Check) {
assert(Err == Z_STREAM_END);
if (Debug)
Print(stderr, "assert(Strm[%zu].avail_in == %u);\n", Idx,
Strm[Idx].avail_in);
assert(Strm[Idx].avail_in == 0);
if (Debug)
Print(stderr, "assert(Strm[%zu].avail_out == %u);\n", Idx,
Strm[Idx].avail_out);
assert(Strm[Idx].avail_out == TailSize);
assert(memcmp(Uncompressed, GetPlainData(PE), GetPlainDataSize(PE)) == 0);
}
Err = inflateEnd(&Strm[Idx]);
if (Debug)
Print(stderr, "assert(inflateEnd(&Strm[%zu]) == %s);\n", Idx, ErrStr(Err));
assert(Err == Z_OK);
free(Uncompressed);
}
static void ExecutePlan(struct PlanExecution *PE) {
if (Debug) {
Print(stderr, "#include <assert.h>\n");
Print(stderr, "#include <stdio.h>\n");
Print(stderr, "#include <string.h>\n");
Print(stderr, "#include <zlib.h>\n");
Print(stderr, "int main(void) {\n");
Indent++;
}
size_t DeflateOpCount = GetDeflateOpCount(PE);
size_t CompressedSize = GetPlainDataSize(PE) * 2 + (DeflateOpCount + 1) * 128;
if (Debug) {
Dump(stderr, "Dict", GetDict(PE), GetDictSize(PE));
Dump(stderr, "Plain", GetPlainData(PE), GetPlainDataSize(PE));
Print(stderr, "z_stream Strm[2];\n");
Print(stderr, "/* n_deflate_ops == %zu; */\n", DeflateOpCount);
Print(stderr, "memset(&Strm, 0, sizeof(Strm));\n");
}
uint8_t *Compressed = (uint8_t *)malloc(CompressedSize);
assert(Compressed);
z_stream Strm[2];
size_t Idx = 0;
memset(&Strm, 0, sizeof(Strm));
int WindowBits = FixupWindowBits(GetWindowBits(PE));
int MemLevel = GetMemLevel(PE);
if (MemLevel == MEM_LEVEL_DEFAULT)
MemLevel = MEM_LEVEL8;
int Strategy = GetInitialStrategy(PE);
int Err = DeflateInit2(Strm, &Idx, GetInitialLevel(PE), Z_DEFLATED,
WindowBits, MemLevel, Strategy);
assert(Err == Z_OK);
int Bound = deflateBound(&Strm[Idx], GetPlainDataSize(PE));
if (Debug)
Print(stderr, "int Bound = deflateBound(&Strm[%zu], %zu);\n", Idx,
GetPlainDataSize(PE));
if (GetDictSize(PE) > 0 && !IsWbGzip(WindowBits)) {
size_t PiecesCount = GetDeflateDictPiecesCount(PE);
size_t Offset = 0;
for (size_t i = 0; i < PiecesCount && Offset < GetDictSize(PE); i++) {
uInt Piece = GetDeflateDictPiece(PE);
if (Offset + Piece > GetDictSize(PE))
Piece = GetDictSize(PE) - Offset;
Err = DeflateSetDictionary(Strm, &Idx,
(const Bytef *)GetDict(PE) + Offset, Piece);
assert(Err == Z_OK);
Offset += Piece;
}
if (Offset < GetDictSize(PE)) {
Err =
DeflateSetDictionary(Strm, &Idx, (const Bytef *)GetDict(PE) + Offset,
GetDictSize(PE) - Offset);
assert(Err == Z_OK);
}
}
Strm[Idx].next_in = (const Bytef *)GetPlainData(PE);
Strm[Idx].avail_in = GetPlainDataSize(PE);
Strm[Idx].next_out = Compressed;
Strm[Idx].avail_out = CompressedSize;
if (Debug) {