Repository navigation
Expand file tree
/
Copy pathfuzz.cpp
More file actions
1719 lines (1532 loc) · 73.8 KB
/
Copy pathfuzz.cpp
File metadata and controls
1719 lines (1532 loc) · 73.8 KB
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
/*
serialize
Copyright © 2016 - 2026, Más Bandwidth LLC.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
libFuzzer harness for serialize.
Every input runs four passes:
1. Hostile read (FuzzRead). ReadStream is the trust boundary of this library: it must survive
arbitrary hostile bytes, failing reads by returning false, never by corrupting memory or
tripping an assert. Build with asserts enabled (Debug config) plus ASan/UBSan so all three
failure modes are caught.
2. The same hostile bytes through InitializePadded, from a source allocation with no slack.
The source is heap allocated at exactly the payload size so ASan fences the byte after it.
3. Hostile read with sentinel destinations (FuzzReadSentinel). The same hostile bytes, with every
scalar read wrapped so its refusal comes back as a value: a refused read must leave its
destination exactly as the caller left it (STANDARD.md), and failure is terminal, so every
later read refuses, consumes no bits and writes nothing. Run with two different sentinels.
4. Differential round trip (FuzzRoundTrip). Values generated from the input are written with
WriteStream, read back with ReadStream, and compared. Any write/read asymmetry traps.
MeasureStream runs the same ops and must never measure fewer bits than were written.
The first bytes of the fuzz input are an op program selecting which serialize_* calls run and
with what parameters. The remaining bytes are the hostile bitstream for pass 1 and the value
pool for pass 2. This lets coverage-guided fuzzing explore interleavings of every primitive.
*/
#include "serialize.h"
#include <stdio.h>
#include <stdlib.h>
#include <vector>
// the pass running now, so a failed check names the pass that caught it (the replay controls match on it)
static const char * fuzz_pass = "none";
// the replay driver exits with a status so ctest can match the message; libFuzzer wants the trap
#ifdef SERIALIZE_FUZZ_REPLAY
#define fuzz_trap() exit( 86 )
#else
#define fuzz_trap() __builtin_trap()
#endif
#define fuzz_check( condition ) do { if ( !(condition) ) { fprintf( stderr, "fuzz_check failed: pass %s, line %d\n", fuzz_pass, __LINE__ ); fuzz_trap(); } } while (0)
// NEGATIVE CONTROLS. Built with SERIALIZE_FUZZ_CONTROL=n, exactly one check of this harness is
// sabotaged, and the replay driver below must then trap in the pass that owns that check (CMake
// registers each as a test that passes only when the output names that pass). A harness that has
// never been seen to fail is a claim, not a measurement. 0 is the real harness.
// 1 sentinel a refused read is made to write its destination
// 2 hostile a range check is tightened below the range the reader accepts
// 3 padded InitializePadded's success is expected to be false
// 4 roundtrip a round trip value is expected to differ from what was written
// 5 canary a byte past the tight write buffer is overwritten
// 6 trailing the trailing data refusal is expected to be an accept
#ifndef SERIALIZE_FUZZ_CONTROL
#define SERIALIZE_FUZZ_CONTROL 0
#endif
// hands out bytes from the fuzz input, wrapping around when exhausted. both round trip passes
// regenerate the identical value sequence, so nothing needs to be stored between passes.
struct ValuePool
{
const uint8_t * data;
size_t size;
size_t index;
ValuePool( const uint8_t * data_in, size_t size_in ) : data( data_in ), size( size_in ), index( 0 ) {}
uint8_t NextByte()
{
if ( size == 0 )
return 0;
if ( index >= size )
index = 0;
return data[index++];
}
uint32_t NextUint32()
{
uint32_t value = 0;
for ( int i = 0; i < 4; i++ )
value = ( value << 8 ) | NextByte();
return value;
}
uint64_t NextUint64()
{
return ( uint64_t( NextUint32() ) << 32 ) | NextUint32();
}
};
// the compressed float declarations the harness drives: bands with a negative low bound, zero
// based bands, resolutions from coarse to fine, and one whose resolution is coarser than the whole
// band ({ 0, 1, 2 }: fewer than one step, which the params clamp up to one)
struct FuzzCompressedFloatDeclaration
{
float min;
float max;
float res;
};
static const FuzzCompressedFloatDeclaration fuzz_compressed_float_declarations[] =
{
{ -10.0f, +10.0f, 0.01f },
{ -1.0f, +1.0f, 0.001f },
{ 0.0f, +100.0f, 1.0f },
{ -100.0f, +100.0f, 0.5f },
{ 0.0f, +10.0f, 3.0f },
{ 0.0f, +1.0f, 0.0001f },
{ 0.0f, +1.0f, 2.0f },
};
static const int fuzz_num_compressed_float_declarations =
(int) ( sizeof( fuzz_compressed_float_declarations ) / sizeof( fuzz_compressed_float_declarations[0] ) );
// the bytes and string directional macros do not cross type check (the write form takes a const
// buffer, the read form a mutable one), so the direction selects a specialization instead
template <bool IsWriting> struct FuzzDirectionalBytes;
template <> struct FuzzDirectionalBytes<true>
{
template <typename Stream> static bool Run( Stream & stream, ValuePool & pool )
{
uint8_t expected_bytes[32];
for ( int j = 0; j < 16; j++ )
{
expected_bytes[j] = pool.NextByte();
}
write_bytes( stream, expected_bytes, 16 );
char expected_string[32];
const int string_length = pool.NextByte() % ( sizeof( expected_string ) - 1 );
for ( int j = 0; j < string_length; j++ )
{
const uint8_t c = pool.NextByte() & 0x7F;
expected_string[j] = ( c != 0 ) ? (char) c : ' ';
}
expected_string[string_length] = '\0';
write_string( stream, expected_string, (int) sizeof( expected_string ) );
return true;
}
};
template <> struct FuzzDirectionalBytes<false>
{
template <typename Stream> static bool Run( Stream & stream, ValuePool & pool )
{
uint8_t expected_bytes[32];
for ( int j = 0; j < 16; j++ )
{
expected_bytes[j] = pool.NextByte();
}
uint8_t bytes_value[32];
read_bytes( stream, bytes_value, 16 );
fuzz_check( memcmp( bytes_value, expected_bytes, 16 ) == 0 );
char expected_string[32];
const int string_length = pool.NextByte() % ( sizeof( expected_string ) - 1 );
for ( int j = 0; j < string_length; j++ )
{
const uint8_t c = pool.NextByte() & 0x7F;
expected_string[j] = ( c != 0 ) ? (char) c : ' ';
}
expected_string[string_length] = '\0';
char string_value[32];
read_string( stream, string_value, (int) sizeof( string_value ) );
fuzz_check( strcmp( string_value, expected_string ) == 0 );
return true;
}
};
// the directional read_* macros, in their intended direction, with the same in-contract checks the
// unified forms get in pass 1
template <typename Stream> bool FuzzDirectionalRead( Stream & stream )
{
int32_t int_value = 0;
read_int( stream, int_value, -100, +100 );
fuzz_check( int_value >= -100 && int_value <= +100 );
int64_t int64_value = 0;
read_int64( stream, int64_value, -1000000, +1000000 );
fuzz_check( int64_value >= -1000000 && int64_value <= +1000000 );
uint32_t bits_value = 0;
read_bits( stream, bits_value, 17 );
fuzz_check( bits_value < ( 1U << 17 ) );
uint64_t bits64_value = 0;
read_bits( stream, bits64_value, 40 );
fuzz_check( bits64_value < ( uint64_t( 1 ) << 40 ) );
uint8_t uint8_value = 0;
read_uint8( stream, uint8_value );
(void) uint8_value;
uint16_t uint16_value = 0;
read_uint16( stream, uint16_value );
(void) uint16_value;
uint32_t uint32_value = 0;
read_uint32( stream, uint32_value );
(void) uint32_value;
uint64_t uint64_value = 0;
read_uint64( stream, uint64_value );
(void) uint64_value;
int32_t fixed_value = 0;
read_fixed( stream, fixed_value, 16, 16, -1000, +1000 );
fuzz_check( fixed_value >= -1000 * 65536 && fixed_value <= 1000 * 65536 );
bool bool_value = false;
read_bool( stream, bool_value );
(void) bool_value;
float float_value = 0.0f;
read_float( stream, float_value );
uint32_t float_bits = 0;
memcpy( &float_bits, &float_value, 4 );
fuzz_check( serialize::float_bits_are_finite( float_bits ) );
double double_value = 0.0;
read_double( stream, double_value );
uint64_t double_bits = 0;
memcpy( &double_bits, &double_value, 8 );
fuzz_check( serialize::double_bits_are_finite( double_bits ) );
const serialize::int128_t bound128 = serialize::int128_t( 1 ) << 40;
serialize::int128_t int128_value = 0;
read_int128( stream, int128_value, -bound128, +bound128 );
fuzz_check( int128_value >= -bound128 && int128_value <= +bound128 );
serialize::uint128_t uint128_value = 0;
read_uint128( stream, uint128_value );
uint8_t bytes_value[32];
read_bytes( stream, bytes_value, 16 );
char string_value[32];
read_string( stream, string_value, (int) sizeof( string_value ) );
fuzz_check( strlen( string_value ) < sizeof( string_value ) );
int previous = 0;
int current = 0;
read_int_relative( stream, previous, current );
fuzz_check( current > previous );
read_align( stream );
return true;
}
// the directional read_* and write_* macros, selected by the stream direction. Stream::IsWriting is
// a compile time constant, so the unused half folds away but both halves are type checked.
template <typename Stream> bool FuzzDirectionalRoundTrip( Stream & stream, ValuePool & pool )
{
const int32_t expected_int = -100 + int32_t( pool.NextUint32() % 201 );
int32_t int_value = Stream::IsWriting ? expected_int : 0;
if ( Stream::IsWriting )
{
write_int( stream, int_value, -100, +100 );
}
else
{
read_int( stream, int_value, -100, +100 );
}
if ( Stream::IsReading )
{
fuzz_check( int_value == expected_int );
}
const int64_t expected_int64 = -1000000 + int64_t( pool.NextUint32() % 2000001 );
int64_t int64_value = Stream::IsWriting ? expected_int64 : 0;
if ( Stream::IsWriting )
{
write_int64( stream, int64_value, -1000000, +1000000 );
}
else
{
read_int64( stream, int64_value, -1000000, +1000000 );
}
if ( Stream::IsReading )
{
fuzz_check( int64_value == expected_int64 );
}
const uint32_t expected_bits = pool.NextUint32() & 0x1FFFF;
uint32_t bits_value = Stream::IsWriting ? expected_bits : 0;
if ( Stream::IsWriting )
{
write_bits( stream, bits_value, 17 );
}
else
{
read_bits( stream, bits_value, 17 );
}
if ( Stream::IsReading )
{
fuzz_check( bits_value == expected_bits );
}
const uint64_t expected_bits64 = pool.NextUint64() & 0xFFFFFFFFFFULL;
uint64_t bits64_value = Stream::IsWriting ? expected_bits64 : 0;
if ( Stream::IsWriting )
{
write_bits( stream, bits64_value, 40 );
}
else
{
read_bits( stream, bits64_value, 40 );
}
if ( Stream::IsReading )
{
fuzz_check( bits64_value == expected_bits64 );
}
const uint8_t expected_uint8 = pool.NextByte();
uint8_t uint8_value = Stream::IsWriting ? expected_uint8 : (uint8_t) 0;
if ( Stream::IsWriting )
{
write_uint8( stream, uint8_value );
}
else
{
read_uint8( stream, uint8_value );
}
if ( Stream::IsReading )
{
fuzz_check( uint8_value == expected_uint8 );
}
const uint16_t expected_uint16 = (uint16_t) pool.NextUint32();
uint16_t uint16_value = Stream::IsWriting ? expected_uint16 : (uint16_t) 0;
if ( Stream::IsWriting )
{
write_uint16( stream, uint16_value );
}
else
{
read_uint16( stream, uint16_value );
}
if ( Stream::IsReading )
{
fuzz_check( uint16_value == expected_uint16 );
}
const uint32_t expected_uint32 = pool.NextUint32();
uint32_t uint32_value = Stream::IsWriting ? expected_uint32 : 0;
if ( Stream::IsWriting )
{
write_uint32( stream, uint32_value );
}
else
{
read_uint32( stream, uint32_value );
}
if ( Stream::IsReading )
{
fuzz_check( uint32_value == expected_uint32 );
}
const uint64_t expected_uint64 = pool.NextUint64();
uint64_t uint64_value = Stream::IsWriting ? expected_uint64 : 0;
if ( Stream::IsWriting )
{
write_uint64( stream, uint64_value );
}
else
{
read_uint64( stream, uint64_value );
}
if ( Stream::IsReading )
{
fuzz_check( uint64_value == expected_uint64 );
}
const uint32_t fixed_range = 2000U * 65536;
const int32_t expected_fixed = int32_t( uint32_t( -1000 * 65536 ) + pool.NextUint32() % ( fixed_range + 1 ) );
int32_t fixed_value = Stream::IsWriting ? expected_fixed : 0;
if ( Stream::IsWriting )
{
write_fixed( stream, fixed_value, 16, 16, -1000, +1000 );
}
else
{
read_fixed( stream, fixed_value, 16, 16, -1000, +1000 );
}
if ( Stream::IsReading )
{
fuzz_check( fixed_value == expected_fixed );
}
const bool expected_bool = ( pool.NextByte() & 1 ) != 0;
bool bool_value = Stream::IsWriting ? expected_bool : false;
if ( Stream::IsWriting )
{
write_bool( stream, bool_value );
}
else
{
read_bool( stream, bool_value );
}
if ( Stream::IsReading )
{
fuzz_check( bool_value == expected_bool );
}
const uint32_t float_raw = pool.NextUint32();
const uint32_t float_expected_bits = serialize::float_bits_are_finite( float_raw ) ? float_raw : ( float_raw & 0x807FFFFF );
float float_value = 0.0f;
if ( Stream::IsWriting )
{
memcpy( &float_value, &float_expected_bits, 4 );
write_float( stream, float_value );
}
else
{
read_float( stream, float_value );
uint32_t float_bits = 0;
memcpy( &float_bits, &float_value, 4 );
fuzz_check( float_bits == float_expected_bits );
}
const uint64_t double_raw = pool.NextUint64();
const uint64_t double_expected_bits = serialize::double_bits_are_finite( double_raw ) ? double_raw : ( double_raw & 0x800FFFFFFFFFFFFFULL );
double double_value = 0.0;
if ( Stream::IsWriting )
{
memcpy( &double_value, &double_expected_bits, 8 );
write_double( stream, double_value );
}
else
{
read_double( stream, double_value );
uint64_t double_bits = 0;
memcpy( &double_bits, &double_value, 8 );
fuzz_check( double_bits == double_expected_bits );
}
const serialize::int128_t bound128 = serialize::int128_t( 1 ) << 40;
const serialize::int128_t expected_int128 = serialize::int128_t( int64_t( pool.NextUint64() & 0x1FFFFFFFFFFULL ) ) - bound128;
serialize::int128_t int128_value = Stream::IsWriting ? expected_int128 : serialize::int128_t( 0 );
if ( Stream::IsWriting )
{
write_int128( stream, int128_value, -bound128, +bound128 );
}
else
{
read_int128( stream, int128_value, -bound128, +bound128 );
}
if ( Stream::IsReading )
{
fuzz_check( int128_value == expected_int128 );
}
const serialize::uint128_t expected_uint128 = ( serialize::uint128_t( pool.NextUint64() ) << 64 ) | pool.NextUint64();
serialize::uint128_t uint128_value = Stream::IsWriting ? expected_uint128 : serialize::uint128_t( 0 );
if ( Stream::IsWriting )
{
write_uint128( stream, uint128_value );
}
else
{
read_uint128( stream, uint128_value );
}
if ( Stream::IsReading )
{
fuzz_check( uint128_value == expected_uint128 );
}
if ( !FuzzDirectionalBytes<( Stream::IsWriting != 0 )>::Run( stream, pool ) )
{
return false;
}
const int previous = 0;
const int expected_current = 1 + int( pool.NextUint32() % 1000000 );
int current = Stream::IsWriting ? expected_current : 0;
if ( Stream::IsWriting )
{
write_int_relative( stream, previous, current );
}
else
{
read_int_relative( stream, previous, current );
}
if ( Stream::IsReading )
{
fuzz_check( current == expected_current );
}
if ( Stream::IsWriting )
{
write_align( stream );
}
else
{
read_align( stream );
}
return true;
}
// pass 1: hostile read. arbitrary bytes go in, reads must fail cleanly or produce in-contract values.
template <typename Stream> bool FuzzRead( Stream & stream, const uint8_t * ops, int numOps )
{
uint8_t bytes[256];
char string[256];
for ( int i = 0; i < numOps; i++ )
{
const int select = ops[i] & 15;
const int param = ops[i] >> 4; // [0,15]
switch ( select )
{
case 0:
{
uint32_t value = 0;
const int bits = param + 1; // [1,16]
serialize_bits( stream, value, bits );
fuzz_check( value < ( 1U << bits ) );
}
break;
case 1:
{
uint32_t value = 0;
const int bits = param + 17; // [17,32]
serialize_bits( stream, value, bits );
fuzz_check( bits == 32 || value < ( 1U << bits ) );
}
break;
case 2:
{
int32_t value = 0;
serialize_int( stream, value, -100, +100 );
#if SERIALIZE_FUZZ_CONTROL == 2
fuzz_check( value >= -100 && value <= +50 ); // NEGATIVE CONTROL
#else
fuzz_check( value >= -100 && value <= +100 );
#endif
}
break;
case 3:
{
int32_t value = 0;
serialize_int( stream, value, INT32_MIN, INT32_MAX );
int64_t value64 = 0;
serialize_int64( stream, value64, INT64_MIN, INT64_MAX );
const int64_t bound = int64_t( param + 1 ) << 35; // spans of varying width exercise bits_required64
int64_t ranged64 = 0;
serialize_int64( stream, ranged64, -bound, +bound );
fuzz_check( ranged64 >= -bound && ranged64 <= +bound );
// fixed point: bounds are compile time, so the configurations are fixed. values read
// off hostile bytes must decode within the raw bounds or fail the read.
int32_t fixed32 = 0;
serialize_fixed( stream, fixed32, 16, 16, -1000, +1000 );
fuzz_check( fixed32 >= -1000 * 65536 && fixed32 <= 1000 * 65536 );
int64_t fixed64 = 0;
serialize_fixed( stream, fixed64, 48, 16, -100000000000LL, +100000000000LL );
fuzz_check( fixed64 >= -100000000000LL * 65536 && fixed64 <= 100000000000LL * 65536 );
serialize::int128_t wide = 0; // exists on every platform: native or emulated
serialize_fixed( stream, wide, 112, 16, -1152921504606846976LL, +1152921504606846976LL ); // ±2^60 units: a raw range past 64 bits
fuzz_check( wide >= serialize::int128_t( -1152921504606846976LL ) * serialize::int128_t( 65536 ) );
fuzz_check( wide <= serialize::int128_t( +1152921504606846976LL ) * serialize::int128_t( 65536 ) );
// ranged 128 bit integers, with the bound width varying by param so the run walks
// every group structure: one group, two, three and four. hostile bytes must decode
// inside the bounds or fail the read — never clamp, never wrap.
const serialize::int128_t bound128 = serialize::int128_t( 1 ) << ( 20 + ( param % 100 ) );
serialize::int128_t ranged128 = 0;
serialize_int128( stream, ranged128, -bound128, +bound128 );
fuzz_check( ranged128 >= -bound128 && ranged128 <= +bound128 );
serialize::int128_t full128 = 0;
serialize_int128( stream, full128, serialize::int128_t( serialize::uint128_t( 1 ) << 127 ), serialize::int128_t( ~( serialize::uint128_t( 1 ) << 127 ) ) );
#if defined( SERIALIZE_HAS_COMPILE_TIME_SURFACE )
int32_t compile_time_int = 0;
switch ( param & 3 )
{
case 0: serialize_int_compile_time( stream, compile_time_int, -100, +100 ); break;
case 1: serialize_int_compile_time( stream, compile_time_int, 0, 65535 ); break;
case 2: serialize_int_compile_time( stream, compile_time_int, INT32_MIN, INT32_MAX ); break;
default: serialize_int_compile_time( stream, compile_time_int, -2000000000, +2000000000 ); break;
}
int64_t compile_time_int64 = 0;
serialize_int64_compile_time( stream, compile_time_int64, -5000000000LL, +5000000000LL );
fuzz_check( compile_time_int64 >= -5000000000LL && compile_time_int64 <= +5000000000LL );
uint32_t compile_time_bits = 0;
switch ( param & 3 )
{
case 0: serialize_bits_compile_time( stream, compile_time_bits, 5 ); break;
case 1: serialize_bits_compile_time( stream, compile_time_bits, 17 ); break;
case 2: serialize_bits_compile_time( stream, compile_time_bits, 32 ); break;
default: serialize_bits_compile_time( stream, compile_time_bits, 1 ); break;
}
uint64_t compile_time_bits64 = 0;
serialize_bits64_compile_time( stream, compile_time_bits64, 40 );
bool compile_time_bool = false;
serialize_bool_compile_time( stream, compile_time_bool );
#endif
}
break;
case 4:
{
bool value = false;
serialize_bool( stream, value );
}
break;
case 5:
{
// finite values only: whatever the bytes, an accepted float is never NaN or infinite
float value = 0.0f;
serialize_float( stream, value );
uint32_t value_bits = 0;
memcpy( &value_bits, &value, 4 );
fuzz_check( serialize::float_bits_are_finite( value_bits ) );
}
break;
case 6:
{
double value = 0.0;
serialize_double( stream, value );
uint64_t value_bits = 0;
memcpy( &value_bits, &value, 8 );
fuzz_check( serialize::double_bits_are_finite( value_bits ) );
}
break;
case 7:
{
const int declaration_index = param % fuzz_num_compressed_float_declarations;
const FuzzCompressedFloatDeclaration & declaration = fuzz_compressed_float_declarations[declaration_index];
uint32_t max_integer_value = 0;
int bits = 0;
float delta = 0.0f;
serialize::serialize_compressed_float_params( declaration.min, declaration.max, declaration.res, max_integer_value, bits, delta );
float value = 0.0f;
if ( ( param & 1 ) != 0 )
{
serialize_compressed_float_precomputed( stream, value, max_integer_value, bits, delta, declaration.min );
}
else
{
serialize_compressed_float( stream, value, declaration.min, declaration.max, declaration.res );
}
uint32_t value_bits = 0;
memcpy( &value_bits, &value, 4 );
fuzz_check( serialize::float_bits_are_finite( value_bits ) );
fuzz_check( value >= declaration.min );
fuzz_check( value <= declaration.min + delta );
}
break;
case 8:
{
uint64_t value = 0;
serialize_uint64( stream, value );
serialize::uint128_t value128 = 0; // exists on every platform: native or emulated
serialize_uint128( stream, value128 );
}
break;
case 9:
{
const int numBytes = param * 16 + 1; // [1,241]
serialize_bytes( stream, bytes, numBytes );
}
break;
case 10:
{
serialize_string( stream, string, (int) sizeof( string ) );
fuzz_check( strlen( string ) < sizeof( string ) );
}
break;
case 11:
{
// the end of the object: an accepted end leaves nothing in the stream
serialize_end( stream );
fuzz_check( stream.GetBitsProcessed() % 8 == 0 );
uint32_t after = 0;
fuzz_check( stream.SerializeBits( after, 1 ) == false );
return false;
}
case 12:
{
serialize_align( stream );
}
break;
case 13:
{
int previous = param * 1000; // int_relative's domain starts at 0: a negative previous is caller error
int current = 0;
serialize_int_relative( stream, previous, current );
}
break;
case 14:
{
const int32_t max = ( param + 1 ) * 1000; // ranges of varying width exercise bits_required
int32_t value = 0;
serialize_int( stream, value, 0, max );
fuzz_check( value >= 0 && value <= max );
}
break;
case 15:
{
uint8_t value8 = 0;
serialize_uint8( stream, value8 );
uint16_t value16 = 0;
serialize_uint16( stream, value16 );
uint32_t value32 = 0;
serialize_uint32( stream, value32 );
if ( !FuzzDirectionalRead( stream ) )
{
return false;
}
}
break;
}
}
return true;
}
// pass 4: differential round trip. run once with WriteStream (and MeasureStream), then again with
// ReadStream over the bytes just written. every case must consume the same pool bytes when writing
// and reading, so the read pass regenerates the exact values the write pass produced.
template <typename Stream> bool FuzzRoundTrip( Stream & stream, const uint8_t * ops, int numOps, ValuePool & pool )
{
for ( int i = 0; i < numOps; i++ )
{
const int select = ops[i] & 15;
const int param = ops[i] >> 4; // [0,15]
switch ( select )
{
case 0:
case 1:
{
const int bits = ( select == 0 ) ? param + 1 : param + 17; // [1,32]
const uint32_t mask = ( bits == 32 ) ? 0xFFFFFFFF : ( ( 1U << bits ) - 1 );
const uint32_t expected = pool.NextUint32() & mask;
uint32_t value = Stream::IsWriting ? expected : 0;
serialize_bits( stream, value, bits );
if ( Stream::IsReading )
{
fuzz_check( value == expected );
}
}
break;
case 2:
{
const int32_t expected = -100 + int32_t( pool.NextUint32() % 201 );
int32_t value = Stream::IsWriting ? expected : 0;
serialize_int( stream, value, -100, +100 );
if ( Stream::IsReading )
{
#if SERIALIZE_FUZZ_CONTROL == 4
fuzz_check( value == expected + 1 ); // NEGATIVE CONTROL
#else
fuzz_check( value == expected );
#endif
}
}
break;
case 3:
{
const int32_t expected = (int32_t) pool.NextUint32();
int32_t value = Stream::IsWriting ? expected : 0;
serialize_int( stream, value, INT32_MIN, INT32_MAX );
if ( Stream::IsReading )
{
fuzz_check( value == expected );
}
const int64_t expected64 = (int64_t) pool.NextUint64();
int64_t value64 = Stream::IsWriting ? expected64 : 0;
serialize_int64( stream, value64, INT64_MIN, INT64_MAX );
if ( Stream::IsReading )
{
fuzz_check( value64 == expected64 );
}
const int64_t bound = int64_t( param + 1 ) << 35; // spans of varying width exercise bits_required64
const uint64_t span = uint64_t( bound ) * 2 + 1;
const int64_t expected_ranged = -bound + int64_t( pool.NextUint64() % span );
int64_t ranged64 = Stream::IsWriting ? expected_ranged : 0;
serialize_int64( stream, ranged64, -bound, +bound );
if ( Stream::IsReading )
{
fuzz_check( ranged64 == expected_ranged );
}
// fixed point: raw values generated uniformly inside the raw bounds must round trip exactly
const uint32_t fixed32_range = 2000U * 65536; // bounds [-1000,+1000] whole units in Q16.16
const int32_t expected_fixed32 = int32_t( uint32_t( -1000 * 65536 ) + pool.NextUint32() % ( fixed32_range + 1 ) );
int32_t fixed32 = Stream::IsWriting ? expected_fixed32 : 0;
serialize_fixed( stream, fixed32, 16, 16, -1000, +1000 );
if ( Stream::IsReading )
{
fuzz_check( fixed32 == expected_fixed32 );
}
const uint64_t fixed64_range = uint64_t( 200000000000ULL ) * 65536; // bounds [-1e11,+1e11] whole units in Q48.16
const int64_t expected_fixed64 = int64_t( uint64_t( -100000000000LL * 65536 ) + pool.NextUint64() % ( fixed64_range + 1 ) );
int64_t fixed64 = Stream::IsWriting ? expected_fixed64 : 0;
serialize_fixed( stream, fixed64, 48, 16, -100000000000LL, +100000000000LL );
if ( Stream::IsReading )
{
fuzz_check( fixed64 == expected_fixed64 );
}
// wide fixed point: bounds ±2^60 whole units in Q112.16 give a raw range of 2^77,
// exercising the multi group path with values that cannot fit in 64 bits. the wide
// types exist on every platform: native __int128 or the emulated pair.
const serialize::uint128_t wide_range = serialize::uint128_t( 1152921504606846976ULL ) << 17; // ( 2 * 2^60 ) << 16
const uint64_t wide_pool_high = pool.NextUint64();
const uint64_t wide_pool_low = pool.NextUint64();
const serialize::uint128_t wide_pool = ( serialize::uint128_t( wide_pool_high ) << 64 ) | wide_pool_low;
const serialize::uint128_t wide_raw_min = serialize::uint128_t( serialize::int128_t( -1152921504606846976LL ) ) << 16;
const serialize::int128_t expected_wide = serialize::int128_t( wide_raw_min + wide_pool % ( wide_range + serialize::uint128_t( 1 ) ) );
serialize::int128_t wide = Stream::IsWriting ? expected_wide : serialize::int128_t( 0 );
serialize_fixed( stream, wide, 112, 16, -1152921504606846976LL, +1152921504606846976LL );
if ( Stream::IsReading )
{
fuzz_check( wide == expected_wide );
}
#if defined( SERIALIZE_HAS_COMPILE_TIME_SURFACE )
{
int32_t expected_compile_time_int = 0;
int32_t compile_time_int = 0;
switch ( param & 3 )
{
case 0:
{
expected_compile_time_int = -100 + int32_t( pool.NextUint32() % 201 );
compile_time_int = Stream::IsWriting ? expected_compile_time_int : 0;
serialize_int_compile_time( stream, compile_time_int, -100, +100 );
}
break;
case 1:
{
expected_compile_time_int = int32_t( pool.NextUint32() % 65536 );
compile_time_int = Stream::IsWriting ? expected_compile_time_int : 0;
serialize_int_compile_time( stream, compile_time_int, 0, 65535 );
}
break;
case 2:
{
expected_compile_time_int = int32_t( pool.NextUint32() );
compile_time_int = Stream::IsWriting ? expected_compile_time_int : 0;
serialize_int_compile_time( stream, compile_time_int, INT32_MIN, INT32_MAX );
}
break;
default:
{
expected_compile_time_int = int32_t( -2000000000LL + int64_t( pool.NextUint32() % 4000000001U ) );
compile_time_int = Stream::IsWriting ? expected_compile_time_int : 0;
serialize_int_compile_time( stream, compile_time_int, -2000000000, +2000000000 );
}
break;
}
if ( Stream::IsReading )
{
fuzz_check( compile_time_int == expected_compile_time_int );
}
const int64_t expected_compile_time_int64 = -5000000000LL + int64_t( pool.NextUint64() % 10000000001ULL );
int64_t compile_time_int64 = Stream::IsWriting ? expected_compile_time_int64 : 0;
serialize_int64_compile_time( stream, compile_time_int64, -5000000000LL, +5000000000LL );
if ( Stream::IsReading )
{
fuzz_check( compile_time_int64 == expected_compile_time_int64 );
}
uint32_t expected_compile_time_bits = 0;
uint32_t compile_time_bits = 0;
switch ( param & 3 )
{
case 0:
expected_compile_time_bits = pool.NextUint32() & 0x1F;
compile_time_bits = Stream::IsWriting ? expected_compile_time_bits : 0;
serialize_bits_compile_time( stream, compile_time_bits, 5 );
break;
case 1:
expected_compile_time_bits = pool.NextUint32() & 0x1FFFF;
compile_time_bits = Stream::IsWriting ? expected_compile_time_bits : 0;
serialize_bits_compile_time( stream, compile_time_bits, 17 );
break;
case 2:
expected_compile_time_bits = pool.NextUint32();
compile_time_bits = Stream::IsWriting ? expected_compile_time_bits : 0;
serialize_bits_compile_time( stream, compile_time_bits, 32 );
break;
default:
expected_compile_time_bits = pool.NextUint32() & 0x1;
compile_time_bits = Stream::IsWriting ? expected_compile_time_bits : 0;
serialize_bits_compile_time( stream, compile_time_bits, 1 );
break;
}
if ( Stream::IsReading )
{
fuzz_check( compile_time_bits == expected_compile_time_bits );
}
const uint64_t expected_compile_time_bits64 = pool.NextUint64() & 0xFFFFFFFFFFULL;
uint64_t compile_time_bits64 = Stream::IsWriting ? expected_compile_time_bits64 : 0;
serialize_bits64_compile_time( stream, compile_time_bits64, 40 );
if ( Stream::IsReading )
{
fuzz_check( compile_time_bits64 == expected_compile_time_bits64 );
}
const bool expected_compile_time_bool = ( pool.NextByte() & 1 ) != 0;
bool compile_time_bool = Stream::IsWriting ? expected_compile_time_bool : false;
serialize_bool_compile_time( stream, compile_time_bool );
if ( Stream::IsReading )
{
fuzz_check( compile_time_bool == expected_compile_time_bool );
}
}
#endif
}
break;
case 4:
{
const bool expected = ( pool.NextByte() & 1 ) != 0;
bool value = Stream::IsWriting ? expected : false;
serialize_bool( stream, value );
if ( Stream::IsReading )
{
fuzz_check( value == expected );
}
}
break;
case 5:
{
// arbitrary FINITE bit patterns, -0 and subnormals included: floats round trip bit
// exact. NaN and infinity are non-conforming on write and assert in this harness, so
// a non-finite pattern has its exponent cleared, which keeps the sign and mantissa
// entropy as a subnormal. Hostile non-finite bytes reach the read path in pass 1
const uint32_t raw_bits = pool.NextUint32();
const uint32_t expected_bits = serialize::float_bits_are_finite( raw_bits ) ? raw_bits : ( raw_bits & 0x807FFFFF );