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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <ostream>
#include <set>
#include <vector>
#include "base/command_line.h"
#include "base/pickle.h"
#include "base/strings/string_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_message_utils.h"
#include "ipc/ipc_switches.h"
#include "ipc/ipc_sync_channel.h"
#include "ipc/ipc_sync_message.h"
#include "tools/ipc_fuzzer/message_lib/message_file.h"
#include "tools/ipc_fuzzer/mutate/rand_util.h"
#if defined(OS_POSIX)
#include <unistd.h>
#endif
// First include of message files to provide basic type.
#include "tools/ipc_fuzzer/message_lib/all_messages.h"
#include "ipc/ipc_message_null_macros.h"
namespace IPC {
class Message;
} // namespace IPC
namespace {
// For breaking deep recursion.
int g_depth = 0;
} // namespace
namespace ipc_fuzzer {
// Interface implemented by those who generate basic types. The types all
// correspond to the types which a pickle from base/pickle.h can pickle,
// plus the floating point types.
class Generator {
public:
virtual void GenerateBool(bool* value) = 0;
virtual void GenerateInt(int* value) = 0;
virtual void GenerateLong(long* value) = 0;
virtual void GenerateSize(size_t* value) = 0;
virtual void GenerateUChar(unsigned char *value) = 0;
virtual void GenerateUInt16(uint16* value) = 0;
virtual void GenerateUInt32(uint32* value) = 0;
virtual void GenerateInt64(int64* value) = 0;
virtual void GenerateUInt64(uint64* value) = 0;
virtual void GenerateFloat(float *value) = 0;
virtual void GenerateDouble(double *value) = 0;
virtual void GenerateString(std::string* value) = 0;
virtual void GenerateString16(base::string16* value) = 0;
virtual void GenerateData(char* data, int length) = 0;
virtual void GenerateBytes(void* data, int data_len) = 0;
};
template <typename T>
void GenerateIntegralType(T* value) {
switch (RandInRange(16)) {
case 0:
*value = 0;
break;
case 1:
*value = 1;
break;
case 2:
*value = -1;
break;
case 3:
*value = 2;
break;
default:
*value = static_cast<T>(RandU64());
break;
}
}
template <typename T>
void GenerateFloatingType(T* value) {
*value = RandDouble();
}
template <typename T>
void GenerateStringType(T* value) {
T temp_string;
size_t length = RandInRange(300);
for (size_t i = 0; i < length; ++i)
temp_string += RandInRange(256);
*value = temp_string;
}
class GeneratorImpl : public Generator {
public:
GeneratorImpl() {}
virtual ~GeneratorImpl() {}
virtual void GenerateBool(bool* value) OVERRIDE {
*value = RandInRange(2);
}
virtual void GenerateInt(int* value) OVERRIDE {
GenerateIntegralType<int>(value);
}
virtual void GenerateLong(long* value) OVERRIDE {
GenerateIntegralType<long>(value);
}
virtual void GenerateSize(size_t* value) OVERRIDE {
GenerateIntegralType<size_t>(value);
}
virtual void GenerateUChar(unsigned char* value) OVERRIDE {
GenerateIntegralType<unsigned char>(value);
}
virtual void GenerateUInt16(uint16* value) OVERRIDE {
GenerateIntegralType<uint16>(value);
}
virtual void GenerateUInt32(uint32* value) OVERRIDE {
GenerateIntegralType<uint32>(value);
}
virtual void GenerateInt64(int64* value) OVERRIDE {
GenerateIntegralType<int64>(value);
}
virtual void GenerateUInt64(uint64* value) OVERRIDE {
GenerateIntegralType<uint64>(value);
}
virtual void GenerateFloat(float* value) OVERRIDE {
GenerateFloatingType<float>(value);
}
virtual void GenerateDouble(double* value) OVERRIDE {
GenerateFloatingType<double>(value);
}
virtual void GenerateString(std::string* value) OVERRIDE {
GenerateStringType<std::string>(value);
}
virtual void GenerateString16(base::string16* value) OVERRIDE {
GenerateStringType<base::string16>(value);
}
virtual void GenerateData(char* data, int length) OVERRIDE {
for (int i = 0; i < length; ++i) {
GenerateIntegralType<char>(&data[i]);
}
}
virtual void GenerateBytes(void* data, int data_len) OVERRIDE {
GenerateData(static_cast<char*>(data), data_len);
}
};
// Partially-specialized class that knows how to generate a given type.
template <class P>
struct GenerateTraits {
static bool Generate(P* p, Generator *generator) {
// This is the catch-all for types we don't have enough information
// to generate.
std::cerr << "Can't handle " << __PRETTY_FUNCTION__ << "\n";
return false;
}
};
// Template function to invoke partially-specialized class method.
template <class P>
static bool GenerateParam(P* p, Generator* generator) {
return GenerateTraits<P>::Generate(p, generator);
}
// Specializations to generate primitive types.
template <>
struct GenerateTraits<bool> {
static bool Generate(bool* p, Generator* generator) {
generator->GenerateBool(p);
return true;
}
};
template <>
struct GenerateTraits<int> {
static bool Generate(int* p, Generator* generator) {
generator->GenerateInt(p);
return true;
}
};
template <>
struct GenerateTraits<unsigned int> {
static bool Generate(unsigned int* p, Generator* generator) {
generator->GenerateInt(reinterpret_cast<int*>(p));
return true;
}
};
template <>
struct GenerateTraits<long> {
static bool Generate(long* p, Generator* generator) {
generator->GenerateLong(p);
return true;
}
};
template <>
struct GenerateTraits<unsigned long> {
static bool Generate(unsigned long* p, Generator* generator) {
generator->GenerateLong(reinterpret_cast<long*>(p));
return true;
}
};
template <>
struct GenerateTraits<long long> {
static bool Generate(long long* p, Generator* generator) {
generator->GenerateInt64(reinterpret_cast<int64*>(p));
return true;
}
};
template <>
struct GenerateTraits<unsigned long long> {
static bool Generate(unsigned long long* p, Generator* generator) {
generator->GenerateInt64(reinterpret_cast<int64*>(p));
return true;
}
};
template <>
struct GenerateTraits<short> {
static bool Generate(short* p, Generator* generator) {
generator->GenerateUInt16(reinterpret_cast<uint16*>(p));
return true;
}
};
template <>
struct GenerateTraits<unsigned short> {
static bool Generate(unsigned short* p, Generator* generator) {
generator->GenerateUInt16(reinterpret_cast<uint16*>(p));
return true;
}
};
template <>
struct GenerateTraits<char> {
static bool Generate(char* p, Generator* generator) {
generator->GenerateUChar(reinterpret_cast<unsigned char*>(p));
return true;
}
};
template <>
struct GenerateTraits<unsigned char> {
static bool Generate(unsigned char* p, Generator* generator) {
generator->GenerateUChar(p);
return true;
}
};
template <>
struct GenerateTraits<float> {
static bool Generate(float* p, Generator* generator) {
generator->GenerateFloat(p);
return true;
}
};
template <>
struct GenerateTraits<double> {
static bool Generate(double* p, Generator* generator) {
generator->GenerateDouble(p);
return true;
}
};
template <>
struct GenerateTraits<std::string> {
static bool Generate(std::string* p, Generator* generator) {
generator->GenerateString(p);
return true;
}
};
template <>
struct GenerateTraits<base::string16> {
static bool Generate(base::string16* p, Generator* generator) {
generator->GenerateString16(p);
return true;
}
};
// Specializations to generate tuples.
template <>
struct GenerateTraits<Tuple0> {
static bool Generate(Tuple0* p, Generator* generator) {
return true;
}
};
template <class A>
struct GenerateTraits<Tuple1<A> > {
static bool Generate(Tuple1<A>* p, Generator* generator) {
return GenerateParam(&p->a, generator);
}
};
template <class A, class B>
struct GenerateTraits<Tuple2<A, B> > {
static bool Generate(Tuple2<A, B>* p, Generator* generator) {
return
GenerateParam(&p->a, generator) &&
GenerateParam(&p->b, generator);
}
};
template <class A, class B, class C>
struct GenerateTraits<Tuple3<A, B, C> > {
static bool Generate(Tuple3<A, B, C>* p, Generator* generator) {
return
GenerateParam(&p->a, generator) &&
GenerateParam(&p->b, generator) &&
GenerateParam(&p->c, generator);
}
};
template <class A, class B, class C, class D>
struct GenerateTraits<Tuple4<A, B, C, D> > {
static bool Generate(Tuple4<A, B, C, D>* p, Generator* generator) {
return
GenerateParam(&p->a, generator) &&
GenerateParam(&p->b, generator) &&
GenerateParam(&p->c, generator) &&
GenerateParam(&p->d, generator);
}
};
template <class A, class B, class C, class D, class E>
struct GenerateTraits<Tuple5<A, B, C, D, E> > {
static bool Generate(Tuple5<A, B, C, D, E>* p, Generator* generator) {
return
GenerateParam(&p->a, generator) &&
GenerateParam(&p->b, generator) &&
GenerateParam(&p->c, generator) &&
GenerateParam(&p->d, generator) &&
GenerateParam(&p->e, generator);
}
};
// Specializations to generate containers.
template <class A>
struct GenerateTraits<std::vector<A> > {
static bool Generate(std::vector<A>* p, Generator* generator) {
size_t count = ++g_depth > 3 ? 0 : RandInRange(20);
p->resize(count);
for (size_t i = 0; i < count; ++i) {
if (!GenerateParam(&p->at(i), generator)) {
--g_depth;
return false;
}
}
--g_depth;
return true;
}
};
template <class A>
struct GenerateTraits<std::set<A> > {
static bool Generate(std::set<A>* p, Generator* generator) {
static int g_depth = 0;
size_t count = ++g_depth > 3 ? 0 : RandInRange(20);
A a;
for (size_t i = 0; i < count; ++i) {
if (!GenerateParam(&a, generator)) {
--g_depth;
return false;
}
p->insert(a);
}
--g_depth;
return true;
}
};
template <class A, class B>
struct GenerateTraits<std::map<A, B> > {
static bool Generate(std::map<A, B>* p, Generator* generator) {
static int g_depth = 0;
size_t count = ++g_depth > 3 ? 0 : RandInRange(20);
std::pair<A, B> place_holder;
for (size_t i = 0; i < count; ++i) {
if (!GenerateParam(&place_holder, generator)) {
--g_depth;
return false;
}
p->insert(place_holder);
}
--g_depth;
return true;
}
};
template <class A, class B>
struct GenerateTraits<std::pair<A, B> > {
static bool Generate(std::pair<A, B>* p, Generator* generator) {
return
GenerateParam(&p->first, generator) &&
GenerateParam(&p->second, generator);
}
};
// Specializations to generate hand-coded tyoes
template <>
struct GenerateTraits<base::NullableString16> {
static bool Generate(base::NullableString16* p, Generator* generator) {
*p = base::NullableString16();
return true;
}
};
template <>
struct GenerateTraits<base::FileDescriptor> {
static bool Generate(base::FileDescriptor* p, Generator* generator) {
// I don't think we can generate real ones due to check on construct.
p->fd = -1;
return true;
}
};
template <>
struct GenerateTraits<base::FilePath> {
static bool Generate(base::FilePath* p, Generator* generator) {
const char path_chars[] = "ACz0/.~:";
size_t count = RandInRange(60);
std::string random_path;
for (size_t i = 0; i < count; ++i)
random_path += path_chars[RandInRange(sizeof(path_chars) - 1)];
*p = base::FilePath(random_path);
return true;
}
};
template <>
struct GenerateTraits<base::File::Error> {
static bool Generate(base::File::Error* p, Generator* generator) {
int temporary;
if (!GenerateParam(&temporary, generator))
return false;
*p = static_cast<base::File::Error>(temporary);
return true;
}
};
template <>
struct GenerateTraits<base::File::Info> {
static bool Generate(base::File::Info* p, Generator* generator) {
double last_modified;
double last_accessed;
double creation_time;
if (!GenerateParam(&p->size, generator))
return false;
if (!GenerateParam(&p->is_directory, generator))
return false;
if (!GenerateParam(&last_modified, generator))
return false;
if (GenerateParam(&last_accessed, generator))
return false;
if (GenerateParam(&creation_time, generator))
return false;
p->last_modified = base::Time::FromDoubleT(last_modified);
p->last_accessed = base::Time::FromDoubleT(last_accessed);
p->creation_time = base::Time::FromDoubleT(creation_time);
return true;
}
};
template <>
struct GenerateTraits<base::Time> {
static bool Generate(base::Time* p, Generator* generator) {
*p = base::Time::FromInternalValue(RandU64());
return true;
}
};
template <>
struct GenerateTraits<base::TimeDelta> {
static bool Generate(base::TimeDelta* p, Generator* generator) {
*p = base::TimeDelta::FromInternalValue(RandU64());
return true;
}
};
template <>
struct GenerateTraits<base::TimeTicks> {
static bool Generate(base::TimeTicks* p, Generator* generator) {
*p = base::TimeTicks::FromInternalValue(RandU64());
return true;
}
};
template <>
struct GenerateTraits<base::ListValue> {
static bool Generate(base::ListValue* p, Generator* generator) {
++g_depth;
size_t list_length = g_depth > 3 ? 0 : RandInRange(8);
for (size_t index = 0; index < list_length; ++index) {
switch (RandInRange(8))
{
case base::Value::TYPE_BOOLEAN: {
bool tmp;
generator->GenerateBool(&tmp);
p->Set(index, new base::FundamentalValue(tmp));
break;
}
case base::Value::TYPE_INTEGER: {
int tmp;
generator->GenerateInt(&tmp);
p->Set(index, new base::FundamentalValue(tmp));
break;
}
case base::Value::TYPE_DOUBLE: {
double tmp;
generator->GenerateDouble(&tmp);
p->Set(index, new base::FundamentalValue(tmp));
break;
}
case base::Value::TYPE_STRING: {
std::string tmp;
generator->GenerateString(&tmp);
p->Set(index, new base::StringValue(tmp));
break;
}
case base::Value::TYPE_BINARY: {
char tmp[200];
size_t bin_length = RandInRange(sizeof(tmp));
generator->GenerateData(tmp, bin_length);
p->Set(index,
base::BinaryValue::CreateWithCopiedBuffer(tmp, bin_length));
break;
}
case base::Value::TYPE_DICTIONARY: {
base::DictionaryValue* tmp = new base::DictionaryValue();
GenerateParam(tmp, generator);
p->Set(index, tmp);
break;
}
case base::Value::TYPE_LIST: {
base::ListValue* tmp = new base::ListValue();
GenerateParam(tmp, generator);
p->Set(index, tmp);
break;
}
case base::Value::TYPE_NULL:
default:
break;
}
}
--g_depth;
return true;
}
};
template <>
struct GenerateTraits<base::DictionaryValue> {
static bool Generate(base::DictionaryValue* p, Generator* generator) {
++g_depth;
size_t dict_length = g_depth > 3 ? 0 : RandInRange(8);
for (size_t index = 0; index < dict_length; ++index) {
std::string property;
generator->GenerateString(&property);
switch (RandInRange(8))
{
case base::Value::TYPE_BOOLEAN: {
bool tmp;
generator->GenerateBool(&tmp);
p->SetWithoutPathExpansion(property, new base::FundamentalValue(tmp));
break;
}
case base::Value::TYPE_INTEGER: {
int tmp;
generator->GenerateInt(&tmp);
p->SetWithoutPathExpansion(property, new base::FundamentalValue(tmp));
break;
}
case base::Value::TYPE_DOUBLE: {
double tmp;
generator->GenerateDouble(&tmp);
p->SetWithoutPathExpansion(property, new base::FundamentalValue(tmp));
break;
}
case base::Value::TYPE_STRING: {
std::string tmp;
generator->GenerateString(&tmp);
p->SetWithoutPathExpansion(property, new base::StringValue(tmp));
break;
}
case base::Value::TYPE_BINARY: {
char tmp[200];
size_t bin_length = RandInRange(sizeof(tmp));
generator->GenerateData(tmp, bin_length);
p->SetWithoutPathExpansion(
property,
base::BinaryValue::CreateWithCopiedBuffer(tmp, bin_length));
break;
}
case base::Value::TYPE_DICTIONARY: {
base::DictionaryValue* tmp = new base::DictionaryValue();
GenerateParam(tmp, generator);
p->SetWithoutPathExpansion(property, tmp);
break;
}
case base::Value::TYPE_LIST: {
base::ListValue* tmp = new base::ListValue();
GenerateParam(tmp, generator);
p->SetWithoutPathExpansion(property, tmp);
break;
}
case base::Value::TYPE_NULL:
default:
break;
}
}
--g_depth;
return true;
}
};
template <>
struct GenerateTraits<GURL> {
static bool Generate(GURL *p, Generator* generator) {
const char url_chars[] = "Ahtp0:/.?+\%&#";
size_t count = RandInRange(100);
std::string random_url;
for (size_t i = 0; i < count; ++i)
random_url += url_chars[RandInRange(sizeof(url_chars) - 1)];
int selector = RandInRange(10);
if (selector == 0)
random_url = std::string("http://") + random_url;
else if (selector == 1)
random_url = std::string("file://") + random_url;
else if (selector == 2)
random_url = std::string("javascript:") + random_url;
else if (selector == 2)
random_url = std::string("data:") + random_url;
*p = GURL(random_url);
return true;
}
};
// FIXME: Actually generate something.
template <>
struct GenerateTraits<SkBitmap> {
static bool Generate(SkBitmap* p, Generator* generator) {
*p = SkBitmap();
return true;
}
};
template <>
struct GenerateTraits<IPC::ChannelHandle> {
static bool Generate(IPC::ChannelHandle* p, Generator* generator) {
return
GenerateParam(&p->name, generator) &&
GenerateParam(&p->socket, generator);
}
};
template <>
struct GenerateTraits<cc::CompositorFrame> {
// FIXME: this should actually generate something
static bool Generate(cc::CompositorFrame* p, Generator* generator) {
return true;
}
};
template <>
struct GenerateTraits<cc::CompositorFrameAck> {
// FIXME: this should actually generate something
static bool Generate(cc::CompositorFrameAck* p, Generator* generator) {
return true;
}
};
template <>
struct GenerateTraits<content::IndexedDBKey> {
static bool Generate(content::IndexedDBKey* p, Generator* generator) {
++g_depth;
blink::WebIDBKeyType web_type =
static_cast<blink::WebIDBKeyType>(RandInRange(7));
switch (web_type)
{
case blink::WebIDBKeyTypeArray: {
size_t length = g_depth > 3 ? 0 : RandInRange(4);
std::vector<content::IndexedDBKey> array;
array.resize(length);
for (size_t i = 0; i < length; ++i) {
if (!GenerateParam(&array[i], generator))
return false;
}
*p = content::IndexedDBKey(array);
return true;
}
case blink::WebIDBKeyTypeBinary: {
std::string binary;
if (!GenerateParam(&binary, generator))
return false;
*p = content::IndexedDBKey(binary);
return true;
}
case blink::WebIDBKeyTypeString: {
base::string16 string;
if (!GenerateParam(&string, generator))
return false;
*p = content::IndexedDBKey(string);
return true;
}
case blink::WebIDBKeyTypeDate:
case blink::WebIDBKeyTypeNumber: {
double number;
if (!GenerateParam(&number, generator))
return false;
*p = content::IndexedDBKey(number, web_type);
return true;
}
case blink::WebIDBKeyTypeInvalid:
case blink::WebIDBKeyTypeNull: {
*p = content::IndexedDBKey(web_type);
return true;
}
default:
NOTREACHED();
return false;
}
--g_depth;
return true;
}
};
template <>
struct GenerateTraits<content::IndexedDBKeyRange> {
static bool Generate(content::IndexedDBKeyRange *p, Generator* generator) {
content::IndexedDBKey lower;
content::IndexedDBKey upper;
bool lower_open;
bool upper_open;
if (!GenerateParam(&lower, generator))
return false;
if (!GenerateParam(&upper, generator))
return false;
if (!GenerateParam(&lower_open, generator))
return false;
if (!GenerateParam(&upper_open, generator))
return false;
*p = content::IndexedDBKeyRange(lower, upper, lower_open, upper_open);
return true;
}
};
template <>
struct GenerateTraits<content::IndexedDBKeyPath> {
static bool Generate(content::IndexedDBKeyPath *p, Generator* generator) {
switch (RandInRange(3)) {
case 0: {
std::vector<base::string16> array;
if (!GenerateParam(&array, generator))
return false;
*p = content::IndexedDBKeyPath(array);
break;
}
case 1: {
base::string16 string;
if (!GenerateParam(&string, generator))
return false;
*p = content::IndexedDBKeyPath(string);
break;
}
case 2: {
*p = content::IndexedDBKeyPath();
break;
}
}
return true;
}
};
template <>
struct GenerateTraits<content::PageState> {
static bool Generate(content::PageState *p, Generator* generator) {
std::string junk;
if (!GenerateParam(&junk, generator))
return false;
*p = content::PageState::CreateFromEncodedData(junk);
return true;
}
};
template <>
struct GenerateTraits<gpu::Mailbox> {
static bool Generate(gpu::Mailbox *p, Generator* generator) {
generator->GenerateBytes(p->name, sizeof(p->name));
return true;
}
};
template <>
struct GenerateTraits<media::AudioParameters> {
static bool Generate(media::AudioParameters *p, Generator* generator) {
int format;
int channel_layout;
int sample_rate;
int bits_per_sample;
int frames_per_buffer;
int channels;
int effects;
if (!GenerateParam(&format, generator))
return false;
if (!GenerateParam(&channel_layout, generator))
return false;
if (!GenerateParam(&sample_rate, generator))
return false;
if (!GenerateParam(&bits_per_sample, generator))
return false;
if (!GenerateParam(&frames_per_buffer, generator))
return false;
if (!GenerateParam(&channels, generator))
return false;
if (!GenerateParam(&effects, generator))
return false;
media::AudioParameters params(
static_cast<media::AudioParameters::Format>(format),
static_cast<media::ChannelLayout>(channel_layout),
channels,
sample_rate,
bits_per_sample,
frames_per_buffer,
effects);
*p = params;
return true;
}
};
template <>
struct GenerateTraits<media::VideoCaptureFormat> {
static bool Generate(media::VideoCaptureFormat *p, Generator* generator) {
int frame_size_width;
int frame_size_height;
int pixel_format;
if (!GenerateParam(&frame_size_height, generator))
return false;
if (!GenerateParam(&frame_size_width, generator))
return false;
if (!GenerateParam(&pixel_format, generator))
return false;
if (!GenerateParam(&p->frame_rate, generator))
return false;
p->frame_size.SetSize(frame_size_width, frame_size_height);
p->pixel_format = static_cast<media::VideoPixelFormat>(pixel_format);
return true;
}
};
template <>
struct GenerateTraits<net::LoadTimingInfo> {
static bool Generate(net::LoadTimingInfo *p, Generator* generator) {
return
GenerateParam(&p->socket_log_id, generator) &&
GenerateParam(&p->socket_reused, generator) &&
GenerateParam(&p->request_start_time, generator) &&
GenerateParam(&p->request_start, generator) &&
GenerateParam(&p->proxy_resolve_start, generator) &&
GenerateParam(&p->proxy_resolve_end, generator) &&
GenerateParam(&p->connect_timing.dns_start, generator) &&
GenerateParam(&p->connect_timing.dns_end, generator) &&
GenerateParam(&p->connect_timing.connect_start, generator) &&
GenerateParam(&p->connect_timing.connect_end, generator) &&
GenerateParam(&p->connect_timing.ssl_start, generator) &&
GenerateParam(&p->connect_timing.ssl_end, generator) &&
GenerateParam(&p->send_start, generator) &&
GenerateParam(&p->send_end, generator) &&
GenerateParam(&p->receive_headers_end, generator);
}
};
template <>
struct GenerateTraits<net::HostPortPair> {
static bool Generate(net::HostPortPair *p, Generator* generator) {
std::string host;
uint16 port;
if (!GenerateParam(&host, generator))
return false;
if (!GenerateParam(&port, generator))
return false;
p->set_host(host);
p->set_port(port);
return true;
}
};
template <>
struct GenerateTraits<net::IPEndPoint> {
static bool Generate(net::IPEndPoint *p, Generator* generator) {
net::IPAddressNumber address;
int port;
if (!GenerateParam(&address, generator))
return false;
if (!GenerateParam(&port, generator))
return false;
net::IPEndPoint ip_endpoint(address, port);
*p = ip_endpoint;
return true;
}
};
template <>
struct GenerateTraits<gfx::Point> {
static bool Generate(gfx::Point *p, Generator* generator) {
int x;
int y;
if (!GenerateParam(&x, generator))
return false;
if (!GenerateParam(&y, generator))
return false;
p->SetPoint(x, y);
return true;
}
};
template <>
struct GenerateTraits<gfx::PointF> {
static bool Generate(gfx::PointF *p, Generator* generator) {
float x;
float y;
if (!GenerateParam(&x, generator))
return false;
if (!GenerateParam(&y, generator))
return false;
p->SetPoint(x, y);
return true;
}
};
template <>
struct GenerateTraits<gfx::Size> {
static bool Generate(gfx::Size *p, Generator* generator) {
int w;
int h;
if (!GenerateParam(&w, generator))
return false;
if (!GenerateParam(&h, generator))
return false;
p->SetSize(w, h);
return true;
}
};
template <>
struct GenerateTraits<gfx::SizeF> {
static bool Generate(gfx::SizeF *p, Generator* generator) {
float w;
float h;
if (!GenerateParam(&w, generator))
return false;
if (!GenerateParam(&h, generator))
return false;
p->SetSize(w, h);
return true;
}
};
template <>
struct GenerateTraits<gfx::Rect> {
static bool Generate(gfx::Rect *p, Generator* generator) {
gfx::Point origin;
gfx::Size size;
if (!GenerateParam(&origin, generator))
return false;
if (!GenerateParam(&size, generator))
return false;
p->set_origin(origin);
p->set_size(size);
return true;
}
};
template <>
struct GenerateTraits<gfx::RectF> {
static bool Generate(gfx::RectF *p, Generator* generator) {
gfx::PointF origin;
gfx::SizeF size;
if (!GenerateParam(&origin, generator))
return false;
if (!GenerateParam(&size, generator))
return false;
p->set_origin(origin);
p->set_size(size);
return true;
}
};
template <>
struct GenerateTraits<gfx::Range> {
static bool Generate(gfx::Range *p, Generator* generator) {
size_t start;
size_t end;
if (!GenerateParam(&start, generator))
return false;
if (!GenerateParam(&end, generator))
return false;
*p = gfx::Range(start, end);
return true;
}
};
template <>
struct GenerateTraits<gfx::Vector2d> {
static bool Generate(gfx::Vector2d *p, Generator* generator) {
int x;
int y;
if (!GenerateParam(&x, generator))
return false;
if (!GenerateParam(&y, generator))
return false;
*p = gfx::Vector2d(x, y);
return true;
}
};
template <>
struct GenerateTraits<gfx::Vector2dF> {
static bool Generate(gfx::Vector2dF *p, Generator* generator) {
float x;
float y;
if (!GenerateParam(&x, generator))
return false;
if (!GenerateParam(&y, generator))
return false;
*p = gfx::Vector2dF(x, y);
return true;
}
};
// PP_ traits.
template <>
struct GenerateTraits<PP_Bool> {
static bool Generate(PP_Bool *p, Generator* generator) {
bool tmp;
if (!GenerateParam(&tmp, generator))
return false;
*p = PP_FromBool(tmp);
return true;
}
};
template <>
struct GenerateTraits<PP_NetAddress_Private> {
static bool Generate(PP_NetAddress_Private *p, Generator* generator) {
p->size = RandInRange(sizeof(p->data) + 1);
generator->GenerateBytes(&p->data, p->size);
return true;
}
};
template <>
struct GenerateTraits<ppapi::HostResource> {
static bool Generate(ppapi::HostResource *p, Generator* generator) {
PP_Instance instance;
PP_Resource resource;
if (!GenerateParam(&instance, generator))
return false;
if (!GenerateParam(&resource, generator))
return false;
p->SetHostResource(instance, resource);
return true;
}
};
template <>
struct GenerateTraits<ppapi::PepperFilePath> {
static bool Generate(ppapi::PepperFilePath *p, Generator* generator) {
unsigned domain = RandInRange(ppapi::PepperFilePath::DOMAIN_MAX_VALID+1);
base::FilePath path;
if (!GenerateParam(&path, generator))
return false;
*p = ppapi::PepperFilePath(
static_cast<ppapi::PepperFilePath::Domain>(domain), path);
return true;
}
};
template <>
struct GenerateTraits<ppapi::PpapiPermissions> {
static bool Generate(ppapi::PpapiPermissions *p, Generator* generator) {
uint32_t bits;
if (!GenerateParam(&bits, generator))
return false;
*p = ppapi::PpapiPermissions(bits);
return true;
}
};
template <>
struct GenerateTraits<ppapi::SocketOptionData> {
static bool Generate(ppapi::SocketOptionData *p, Generator* generator) {
// FIXME: we can do better here.
int32 temp;
if (!GenerateParam(&temp, generator))
return false;
p->SetInt32(temp);
return true;
}
};
// Redefine macros to generate generating from traits declarations.
// STRUCT declarations cause corresponding STRUCT_TRAITS declarations to occur.
#undef IPC_STRUCT_BEGIN
#undef IPC_STRUCT_BEGIN_WITH_PARENT
#undef IPC_STRUCT_MEMBER
#undef IPC_STRUCT_END
#define IPC_STRUCT_BEGIN_WITH_PARENT(struct_name, parent) \
IPC_STRUCT_BEGIN(struct_name)
#define IPC_STRUCT_BEGIN(struct_name) IPC_STRUCT_TRAITS_BEGIN(struct_name)
#define IPC_STRUCT_MEMBER(type, name, ...) IPC_STRUCT_TRAITS_MEMBER(name)
#define IPC_STRUCT_END() IPC_STRUCT_TRAITS_END()
// Set up so next include will generate generate trait classes.
#undef IPC_STRUCT_TRAITS_BEGIN
#undef IPC_STRUCT_TRAITS_MEMBER
#undef IPC_STRUCT_TRAITS_PARENT
#undef IPC_STRUCT_TRAITS_END
#define IPC_STRUCT_TRAITS_BEGIN(struct_name) \
template <> \
struct GenerateTraits<struct_name> { \
static bool Generate(struct_name *p, Generator* generator) {
#define IPC_STRUCT_TRAITS_MEMBER(name) \
if (!GenerateParam(&p->name, generator)) \
return false;
#define IPC_STRUCT_TRAITS_PARENT(type) \
if (!GenerateParam(static_cast<type*>(p), generator)) \
return false;
#define IPC_STRUCT_TRAITS_END() \
return true; \
} \
};
// If |condition| isn't met, the messsge will fail to serialize. Try
// increasingly smaller ranges until we find one that happens to meet
// the condition, or fail trying.
#undef IPC_ENUM_TRAITS_VALIDATE
#define IPC_ENUM_TRAITS_VALIDATE(enum_name, condition) \
template <> \
struct GenerateTraits<enum_name> { \
static bool Generate(enum_name* p, Generator* generator) { \
for (int shift = 30; shift; --shift) { \
for (int tries = 0; tries < 2; ++tries) { \
int value = RandInRange(1 << shift); \
if (condition) { \
*reinterpret_cast<int*>(p) = value; \
return true; \
} \
} \
} \
std::cerr << "failed to satisfy " << #condition << "\n"; \
return false; \
} \
};
// Bring them into existence.
#include "tools/ipc_fuzzer/message_lib/all_messages.h"
#include "ipc/ipc_message_null_macros.h"
// Redefine macros to generate generating funtions
#undef IPC_MESSAGE_DECL
#define IPC_MESSAGE_DECL(kind, type, name, in, out, ilist, olist) \
IPC_##kind##_##type##_GENERATE(name, in, out, ilist, olist)
#define IPC_EMPTY_CONTROL_GENERATE(name, in, out, ilist, olist) \
IPC::Message* generator_for_##name(Generator* generator) { \
return new name(); \
}
#define IPC_EMPTY_ROUTED_GENERATE(name, in, out, ilist, olist) \
IPC::Message* generator_for_##name(Generator* generator) { \
return new name(RandInRange(MAX_FAKE_ROUTING_ID)); \
}
#define IPC_ASYNC_CONTROL_GENERATE(name, in, out, ilist, olist) \
IPC::Message* generator_for_##name(Generator* generator) { \
IPC_TUPLE_IN_##in ilist p; \
if (GenerateParam(&p, generator)) { \
return new name(IPC_MEMBERS_IN_##in(p)); \
} \
std::cerr << "Don't know how to generate " << #name << "\n"; \
return 0; \
}
#define IPC_ASYNC_ROUTED_GENERATE(name, in, out, ilist, olist) \
IPC::Message* generator_for_##name(Generator* generator) { \
IPC_TUPLE_IN_##in ilist p; \
if (GenerateParam(&p, generator)) { \
return new name(RandInRange(MAX_FAKE_ROUTING_ID) \
IPC_COMMA_##in \
IPC_MEMBERS_IN_##in(p)); \
} \
std::cerr << "Don't know how to generate " << #name << "\n"; \
return 0; \
}
#define IPC_SYNC_CONTROL_GENERATE(name, in, out, ilist, olist) \
IPC::Message* generator_for_##name(Generator* generator) { \
IPC_TUPLE_IN_##in ilist p; \
if (GenerateParam(&p, generator)) { \
return new name(IPC_MEMBERS_IN_##in(p) \
IPC_COMMA_AND_##out(IPC_COMMA_##in) \
IPC_MEMBERS_OUT_##out()); \
} \
std::cerr << "Don't know how to generate " << #name << "\n"; \
return 0; \
}
#define IPC_SYNC_ROUTED_GENERATE(name, in, out, ilist, olist) \
IPC::Message* generator_for_##name(Generator* generator) { \
IPC_TUPLE_IN_##in ilist p; \
if (GenerateParam(&p, generator)) { \
return new name(RandInRange(MAX_FAKE_ROUTING_ID) \
IPC_COMMA_OR_##out(IPC_COMMA_##in) \
IPC_MEMBERS_IN_##in(p) \
IPC_COMMA_AND_##out(IPC_COMMA_##in) \
IPC_MEMBERS_OUT_##out()); \
} \
std::cerr << "Don't know how to generate " << #name << "\n"; \
return 0; \
}
#define MAX_FAKE_ROUTING_ID 15
#define IPC_MEMBERS_IN_0(p)
#define IPC_MEMBERS_IN_1(p) p.a
#define IPC_MEMBERS_IN_2(p) p.a, p.b
#define IPC_MEMBERS_IN_3(p) p.a, p.b, p.c
#define IPC_MEMBERS_IN_4(p) p.a, p.b, p.c, p.d
#define IPC_MEMBERS_IN_5(p) p.a, p.b, p.c, p.d, p.e
#define IPC_MEMBERS_OUT_0()
#define IPC_MEMBERS_OUT_1() NULL
#define IPC_MEMBERS_OUT_2() NULL, NULL
#define IPC_MEMBERS_OUT_3() NULL, NULL, NULL
#define IPC_MEMBERS_OUT_4() NULL, NULL, NULL, NULL
#define IPC_MEMBERS_OUT_5() NULL, NULL, NULL, NULL, NULL
#include "tools/ipc_fuzzer/message_lib/all_messages.h"
#include "ipc/ipc_message_null_macros.h"
typedef IPC::Message* (*GeneratorFunction)(Generator*);
typedef std::vector<GeneratorFunction> GeneratorFunctionVector;
void PopulateGeneratorFunctionVector(
GeneratorFunctionVector *function_vector) {
#undef IPC_MESSAGE_DECL
#define IPC_MESSAGE_DECL(kind, type, name, in, out, ilist, olist) \
function_vector->push_back(generator_for_##name);
#include "tools/ipc_fuzzer/message_lib/all_messages.h"
}
static const char kCountSwitch[] = "count";
static const char kHelpSwitch[] = "help";
int GenerateMain(int argc, char** argv) {
CommandLine::Init(argc, argv);
CommandLine* cmd = CommandLine::ForCurrentProcess();
CommandLine::StringVector args = cmd->GetArgs();
if (args.size() != 1 || cmd->HasSwitch(kHelpSwitch)) {
std::cerr << "Usage: ipc_fuzzer_generate [--help] [--count=n] outfile\n";
return EXIT_FAILURE;
}
std::string output_file_name = args[0];
int message_count = 1000;
if (cmd->HasSwitch(kCountSwitch))
message_count = atoi(cmd->GetSwitchValueASCII(kCountSwitch).c_str());
InitRand();
GeneratorFunctionVector function_vector;
PopulateGeneratorFunctionVector(&function_vector);
std::cerr << "Counted " << function_vector.size()
<< " distinct messages present in chrome.\n";
Generator* generator = new GeneratorImpl();
MessageVector message_vector;
int bad_count = 0;
if (message_count < 0) {
// Enumerate them all.
for (size_t i = 0; i < function_vector.size(); ++i) {
if (IPC::Message* new_message = (*function_vector[i])(generator))
message_vector.push_back(new_message);
else
bad_count += 1;
}
} else {
// Generate a random batch.
for (int i = 0; i < message_count; ++i) {
size_t index = RandInRange(function_vector.size());
if (IPC::Message* new_message = (*function_vector[index])(generator))
message_vector.push_back(new_message);
else
bad_count += 1;
}
}
std::cerr << "Failed to generate " << bad_count << " messages.\n";
if (!MessageFile::Write(base::FilePath(output_file_name), message_vector))
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
} // namespace ipc_fuzzer
int main(int argc, char** argv) {
return ipc_fuzzer::GenerateMain(argc, argv);
}
|