-
Notifications
You must be signed in to change notification settings - Fork 139
/
Type.cpp
1658 lines (1481 loc) · 40.8 KB
/
Type.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
// -*- mode: C++ -*-
//
// Copyright (c) 2007, 2008, 2010, 2011 The University of Utah
// All rights reserved.
//
// This file is part of `csmith', a random generator of C programs.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * 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.
//
// 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 OWNER 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.
//
// This file was derived from a random program generator written by Bryan
// Turner. The attributions in that file was:
//
// Random Program Generator
// Bryan Turner (bryan.turner@pobox.com)
// July, 2005
//
#include "Type.h"
#include <sstream>
#include <assert.h>
#include <math.h>
#include "Common.h"
#include "CGOptions.h"
#include "random.h"
#include "Filter.h"
#include "Error.h"
#include "util.h"
#include "Bookkeeper.h"
#include "Probabilities.h"
#include "DepthSpec.h"
#include "Enumerator.h"
using namespace std;
///////////////////////////////////////////////////////////////////////////////
/*
*
*/
const Type *Type::simple_types[MAX_SIMPLE_TYPES];
Type *Type::void_type = NULL;
// ---------------------------------------------------------------------
// List of all types used in the program
static vector<Type *> AllTypes;
static vector<Type *> derived_types;
//////////////////////////////////////////////////////////////////////
class NonVoidTypeFilter : public Filter
{
public:
NonVoidTypeFilter();
virtual ~NonVoidTypeFilter();
virtual bool filter(int v) const;
Type *get_type();
private:
mutable Type *typ_;
};
NonVoidTypeFilter::NonVoidTypeFilter()
: typ_(NULL)
{
}
NonVoidTypeFilter::~NonVoidTypeFilter()
{
}
bool
NonVoidTypeFilter::filter(int v) const
{
assert(static_cast<unsigned int>(v) < AllTypes.size());
Type *type = AllTypes[v];
if (type->eType == eSimple && type->simple_type == eVoid)
return true;
if (!type->used) {
Bookkeeper::record_type_with_bitfields(type);
type->used = true;
}
typ_ = type;
if (type->eType == eSimple) {
Filter *filter = SIMPLE_TYPES_PROB_FILTER;
return filter->filter(typ_->simple_type);
}
return false;
}
Type *
NonVoidTypeFilter::get_type()
{
assert(typ_);
return typ_;
}
class NonVoidNonVolatileTypeFilter : public Filter
{
public:
NonVoidNonVolatileTypeFilter();
virtual ~NonVoidNonVolatileTypeFilter();
virtual bool filter(int v) const;
Type *get_type();
private:
mutable Type *typ_;
};
NonVoidNonVolatileTypeFilter::NonVoidNonVolatileTypeFilter()
: typ_(NULL)
{
}
NonVoidNonVolatileTypeFilter::~NonVoidNonVolatileTypeFilter()
{
}
bool
NonVoidNonVolatileTypeFilter::filter(int v) const
{
assert(static_cast<unsigned int>(v) < AllTypes.size());
Type *type = AllTypes[v];
if (type->eType == eSimple && type->simple_type == eVoid)
return true;
if (type->is_aggregate() && type->is_volatile_struct_union())
return true;
if ((type->eType == eStruct) && (!CGOptions::arg_structs())) {
return true;
}
if ((type->eType == eUnion) && (!CGOptions::arg_unions())) {
return true;
}
if (!type->used) {
Bookkeeper::record_type_with_bitfields(type);
type->used = true;
}
typ_ = type;
if (type->eType == eSimple) {
Filter *filter = SIMPLE_TYPES_PROB_FILTER;
return filter->filter(typ_->simple_type);
}
return false;
}
Type *
NonVoidNonVolatileTypeFilter::get_type()
{
assert(typ_);
return typ_;
}
class ChooseRandomTypeFilter : public Filter
{
public:
ChooseRandomTypeFilter();
virtual ~ChooseRandomTypeFilter();
virtual bool filter(int v) const;
Type *get_type();
private:
mutable Type *typ_;
};
ChooseRandomTypeFilter::ChooseRandomTypeFilter()
{
}
ChooseRandomTypeFilter::~ChooseRandomTypeFilter()
{
}
bool
ChooseRandomTypeFilter::filter(int v) const
{
assert((v >= 0) && (static_cast<unsigned int>(v) < AllTypes.size()));
typ_ = AllTypes[v];
assert(typ_);
if (typ_->eType == eSimple) {
Filter *filter = SIMPLE_TYPES_PROB_FILTER;
return filter->filter(typ_->simple_type);
}
else if ((typ_->eType == eStruct) && (!CGOptions::return_structs())) {
return true;
}
return false;
}
Type *
ChooseRandomTypeFilter::get_type()
{
assert(typ_);
return typ_;
}
///////////////////////////////////////////////////////////////////////////////
// --------------------------------------------------------------
/* constructor for simple types
********************************************************/
Type::Type(eSimpleType simple_type) :
eType(eSimple),
ptr_type(0),
simple_type(simple_type),
used(false),
printed(false),
packed_(false)
{
// Nothing else to do.
}
// --------------------------------------------------------------
/* copy constructor
*******************************************************/
#if 0
Type::Type(const Type &t) :
eType(t.eType),
ptr_type(t.ptr_type),
simple_type(t.simple_type),
dimensions(t.dimensions),
fields(t.fields),
used(t.used),
printed(t.printed)
{
// Nothing else to do.
}
#endif
// --------------------------------------------------------------
/* constructor for struct or union types
*******************************************************/
Type::Type(vector<const Type*>& struct_fields, bool isStruct, bool packed,
vector<CVQualifiers> &qfers, vector<int> &fields_length) :
ptr_type(0),
fields(struct_fields),
used(false),
printed(false),
packed_(packed),
qfers_(qfers),
bitfields_length_(fields_length)
{
static unsigned int sequence = 0;
if (isStruct)
eType = eStruct;
else
eType = eUnion;
sid = sequence++;
}
// --------------------------------------------------------------
/* constructor for pointers
*******************************************************/
Type::Type(const Type* t) :
eType(ePointer),
ptr_type(t),
used(false),
printed(false),
packed_(false)
{
// Nothing else to do.
}
// --------------------------------------------------------------
Type::~Type(void)
{
// Nothing to do.
}
// --------------------------------------------------------------
#if 0
Type &
Type::operator=(const Type& t)
{
if (this == &t) {
return *this;
}
eType = t.eType;
simple_type = t.simple_type;
dimensions = t.dimensions;
fields = t.fields;
return *this;
}
#endif
// ---------------------------------------------------------------------
const Type &
Type::get_simple_type(eSimpleType st)
{
static bool inited = false;
if (!inited) {
for (int i = 0; i < MAX_SIMPLE_TYPES; ++i) {
Type::simple_types[i] = 0;
}
inited = true;
}
if (Type::simple_types[st] == 0) {
// find if type is in the allTypes already (most likely only "eVoid" is not there)
for (size_t i=0; i<AllTypes.size(); i++) {
Type* tt = AllTypes[i];
if (tt->eType == eSimple && tt->simple_type == st) {
Type::simple_types[st] = tt;
}
}
if (Type::simple_types[st] == 0) {
Type *t = new Type(st);
Type::simple_types[st] = t;
AllTypes.push_back(t);
}
}
return *Type::simple_types[st];
}
// ---------------------------------------------------------------------
/* return the most commonly used type - integer
*************************************************************/
const Type *
get_int_type()
{
return &Type::get_simple_type(eInt);
}
Type*
Type::find_type(const Type* t)
{
for (size_t i=0; i<AllTypes.size(); i++) {
if (AllTypes[i] == t) {
return AllTypes[i];
}
}
return 0;
}
// ---------------------------------------------------------------------
/* find the pointer type to the given type in existing types,
* return 0 if not found
*************************************************************/
Type*
Type::find_pointer_type(const Type* t, bool add)
{
for (size_t i=0; i<derived_types.size(); i++) {
if (derived_types[i]->ptr_type == t) {
return derived_types[i];
}
}
if (add) {
Type* ptr_type = new Type(t);
derived_types.push_back(ptr_type);
return ptr_type;
}
return 0;
}
bool
Type::is_const_struct_union() const
{
if (!is_aggregate()) return false;
assert(fields.size() == qfers_.size());
for (size_t i = 0; i < fields.size(); ++i) {
const Type *field = fields[i];
if (field->is_const_struct_union()) {
return true;
}
const CVQualifiers& cf = qfers_[i];
if (cf.is_const()) return true;
}
return false;
}
bool
Type::is_volatile_struct_union() const
{
if (!is_aggregate()) return false;
assert(fields.size() == qfers_.size());
for (size_t i = 0; i < fields.size(); ++i) {
const Type *field = fields[i];
if (field->is_volatile_struct_union()) {
return true;
}
const CVQualifiers& cf = qfers_[i];
if (cf.is_volatile())
return true;
}
return false;
}
bool
Type::has_int_field() const
{
if (!is_aggregate()) return false;
for (size_t i=0; i<fields.size(); ++i) {
const Type* t = fields[i];
if (t->is_int()) return true;
if (t->has_int_field()) return true;
}
return false;
}
void
Type::get_all_ok_struct_union_types(vector<Type *> &ok_types, bool no_const, bool no_volatile, bool need_int_field, bool bStruct)
{
vector<Type *>::iterator i;
for(i = AllTypes.begin(); i != AllTypes.end(); ++i) {
Type* t = (*i);
if (bStruct && t->eType != eStruct) continue;
if (!bStruct && t->eType != eUnion) continue;
if ((no_const && t->is_const_struct_union()) ||
(no_volatile && t->is_volatile_struct_union()) ||
(need_int_field && (!t->has_int_field()))) {
continue;
}
ok_types.push_back(t);
}
}
const Type*
Type::choose_random_struct_union_type(vector<Type *> &ok_types)
{
size_t sz = ok_types.size();
assert(sz > 0);
int index = rnd_upto(ok_types.size());
ERROR_GUARD(0);
assert(index >= 0);
Type *rv_type = ok_types[index];
if (!rv_type->used) {
Bookkeeper::record_type_with_bitfields(rv_type);
rv_type->used = true;
}
return rv_type;
}
const Type*
Type::choose_random_pointer_type(void)
{
unsigned int index = rnd_upto(derived_types.size());
ERROR_GUARD(NULL);
return derived_types[index];
}
bool
Type::has_pointer_type(void)
{
return derived_types.size() > 0;
}
/* for exhaustive mode only */
const Type*
Type::choose_random_struct_from_type(const Type* type, bool no_volatile)
{
if (!type)
return NULL;
const Type* t = type;
vector<Type *> ok_struct_types;
get_all_ok_struct_union_types(ok_struct_types, no_volatile, false, true, true);
if (ok_struct_types.size() > 0) {
DEPTH_GUARD_BY_DEPTH_RETURN(1, NULL);
t = Type::choose_random_struct_union_type(ok_struct_types);
ERROR_GUARD(NULL);
}
return t;
}
const Type*
Type::random_type_from_type(const Type* type, bool no_volatile, bool strict_simple_type)
{
const Type* t = type;
DEPTH_GUARD_BY_TYPE_RETURN(dtRandomTypeFromType, NULL);
if (type == 0) {
t = no_volatile ? choose_random_nonvoid_nonvolatile() : choose_random_nonvoid();
ERROR_GUARD(NULL);
}
if (type->eType == eSimple && !strict_simple_type) {
t = choose_random_simple();
ERROR_GUARD(NULL);
}
if (t->eType == eSimple) {
assert(t->simple_type != eVoid);
}
return t;
}
// ---------------------------------------------------------------------
static bool
MoreTypesProbability(void)
{
// Always have at least 10 types in the program.
if (AllTypes.size() < 10)
return true;
// 60% probability for each additional type.
return rnd_flipcoin(60);
}
// ---------------------------------------------------------------------
eSimpleType
Type::choose_random_nonvoid_simple(void)
{
eSimpleType simple_type;
#if 0
vector<unsigned int> vs;
vs.push_back(eVoid);
if (!CGOptions::allow_int64()) {
vs.push_back(eLongLong);
vs.push_back(eULongLong);
}
VectorFilter filter(vs);
#endif
simple_type = (eSimpleType) rnd_upto(MAX_SIMPLE_TYPES, SIMPLE_TYPES_PROB_FILTER);
return simple_type;
}
void
Type::make_one_bitfield(vector<const Type*> &random_fields, vector<CVQualifiers> &qualifiers, vector<int> &fields_length)
{
int max_length = CGOptions::bitfields_length();
bool sign = rnd_flipcoin(BitFieldsSignedProb);
ERROR_RETURN();
const Type *type = sign ? &Type::get_simple_type(eInt) : &Type::get_simple_type(eUInt);
random_fields.push_back(type);
CVQualifiers qual = CVQualifiers::random_qualifiers(type, FieldConstProb, FieldVolatileProb);
ERROR_RETURN();
qualifiers.push_back(qual);
int length = rnd_upto(max_length);
ERROR_RETURN();
bool no_zero_len = fields_length.empty() || (fields_length.back() == 0);
// force length to be non-zero is required
if (length == 0 && no_zero_len) {
if (max_length <= 2) length = 1;
else length = rnd_upto(max_length - 1) + 1;
}
ERROR_RETURN();
fields_length.push_back(length);
}
// ---------------------------------------------------------------------
void
Type::make_full_bitfields_struct_fields(size_t field_cnt, vector<const Type*> &random_fields,
vector<CVQualifiers> &qualifiers,
vector<int> &fields_length)
{
for (size_t i=0; i<field_cnt; i++) {
bool is_non_bitfield = rnd_flipcoin(ScalarFieldInFullBitFieldsProb);
if (is_non_bitfield) {
make_one_struct_field(random_fields, qualifiers, fields_length);
}
else {
make_one_bitfield(random_fields, qualifiers, fields_length);
}
}
}
void
Type::make_one_struct_field(vector<const Type*> &random_fields,
vector<CVQualifiers> &qualifiers,
vector<int> &fields_length)
{
ChooseRandomTypeFilter f;
unsigned int i = rnd_upto(AllTypes.size(), &f);
ERROR_RETURN();
const Type* type = AllTypes[i];
random_fields.push_back(type);
CVQualifiers qual = CVQualifiers::random_qualifiers(type, FieldConstProb, FieldVolatileProb);
ERROR_RETURN();
qualifiers.push_back(qual);
fields_length.push_back(-1);
}
void
Type::make_one_union_field(vector<const Type*> &fields, vector<CVQualifiers> &qfers, vector<int> &lens)
{
bool is_bitfield = CGOptions::bitfields() && rnd_flipcoin(BitFieldInNormalStructProb);
if (is_bitfield) {
make_one_bitfield(fields, qfers, lens);
}
else {
vector<Type*> ok_types = AllTypes;
// find locations of struct types
int start_index = -1;
int end_index = -1;
for (size_t i=0; i<ok_types.size(); i++) {
if (ok_types[i]->eType == eStruct) {
if (start_index == -1) start_index = i;
end_index = i;
}
}
const Type* type = NULL;
do {
// 10% chance to be struct field
if (start_index != -1 && pure_rnd_flipcoin(10)) {
type = ok_types[start_index + pure_rnd_upto(end_index - start_index + 1)];
assert(type->eType == eStruct);
}
// 10% chance to be char*
else if (pure_rnd_flipcoin(10)) {
type = find_pointer_type(&get_simple_type(eChar), true);
}
else {
unsigned int i = pure_rnd_upto(ok_types.size());
const Type* t = ok_types[i];
// no union in union?
if (t->eType == eUnion ||
(t->eType == eSimple && SIMPLE_TYPES_PROB_FILTER->filter(t->simple_type))) {
continue;
}
type = t;
}
} while (type == NULL);
fields.push_back(type);
CVQualifiers qual = CVQualifiers::random_qualifiers(type, FieldConstProb, FieldVolatileProb);
ERROR_RETURN();
qfers.push_back(qual);
lens.push_back(-1);
}
}
void
Type::make_normal_struct_fields(size_t field_cnt, vector<const Type*> &random_fields,
vector<CVQualifiers> &qualifiers,
vector<int> &fields_length)
{
for (size_t i=0; i<field_cnt; i++)
{
bool is_bitfield = CGOptions::bitfields() && rnd_flipcoin(BitFieldInNormalStructProb);
if (is_bitfield) {
make_one_bitfield(random_fields, qualifiers, fields_length);
}
else {
make_one_struct_field(random_fields, qualifiers, fields_length);
}
}
}
#define ZERO_BITFIELD 0
#define RANDOM_BITFIELD 1
//#define MAX_BITFIELD 2
#define ENUM_BITFIELD_SIZE 2
void
Type::init_is_bitfield_enumerator(Enumerator<string> &enumerator, int bitfield_prob)
{
int field_cnt = CGOptions::max_struct_fields();
for (int i = 0; i < field_cnt; ++i) {
std::ostringstream ss;
ss << "bitfield" << i;
if (CGOptions::bitfields()) {
enumerator.add_bool_elem(ss.str(), bitfield_prob);
}
else {
enumerator.add_bool_elem(ss.str(), 0);
}
}
}
void
Type::init_fields_enumerator(Enumerator<string> &enumerator,
Enumerator<string> &bitfield_enumerator,
int type_bound, int qual_bound,
int bitfield_qual_bound)
{
int field_cnt = CGOptions::max_struct_fields();
for (int i = 0; i < field_cnt; ++i) {
std::ostringstream ss;
ss << "bitfield" << i;
bool is_bitfield = bitfield_enumerator.get_elem(ss.str());
if (is_bitfield) {
std::ostringstream ss1, ss2, ss3;
ss1 << "bitfield_sign" << i;
ss2 << "bitfield_qualifier" << i;
ss3 << "bitfield_length" << i;
enumerator.add_bool_elem_of_bool(ss1.str(), false);
enumerator.add_elem(ss2.str(), bitfield_qual_bound);
enumerator.add_elem(ss3.str(), ENUM_BITFIELD_SIZE);
}
else {
std::ostringstream ss1, ss2;
ss1 << "field" << i;
ss2 << "qualifier" << i;
enumerator.add_elem(ss1.str(), type_bound);
enumerator.add_elem(ss2.str(), qual_bound);
}
}
enumerator.add_bool_elem_of_bool("packed", CGOptions::packed_struct());
}
int
Type::get_bitfield_length(int length_flag)
{
int max_length = CGOptions::bitfields_length();
assert(max_length > 0);
int length;
switch (length_flag) {
case ZERO_BITFIELD:
length = 0;
break;
#if 0
case MAX_BITFIELD:
length = max_length;
break;
#endif
case RANDOM_BITFIELD:
length = pure_rnd_upto(max_length);
break;
default:
assert(0);
break;
}
return length;
}
bool
Type::make_one_bitfield_by_enum(Enumerator<string> &enumerator,
vector<CVQualifiers> &all_bitfield_quals,
vector<const Type*> &random_fields,
vector<CVQualifiers> &qualifiers,
vector<int> &fields_length,
int index, bool &last_is_zero)
{
std::ostringstream ss1, ss2, ss3;
ss1 << "bitfield_sign" << index;
ss2 << "bitfield_qualifier" << index;
ss3 << "bitfield_length" << index;
bool sign = enumerator.get_elem(ss1.str());
// we cannot allow too many structs,
// so randomly choose the sign of fields.
if (pure_rnd_flipcoin(50))
sign = true;
const Type *type = sign ? &Type::get_simple_type(eInt) : &Type::get_simple_type(eUInt);
random_fields.push_back(type);
int qual_index = enumerator.get_elem(ss2.str());
assert((qual_index >= 0) && ((static_cast<unsigned int>(qual_index)) < all_bitfield_quals.size()));
CVQualifiers qual = all_bitfield_quals[qual_index];
qualifiers.push_back(qual);
int length_flag = enumerator.get_elem(ss3.str());
int length = get_bitfield_length(length_flag);
if ((index==0 || last_is_zero) && (length == 0)) {
return false;
}
last_is_zero = (length == 0) ? true : false;
fields_length.push_back(length);
return true;
}
bool
Type::make_one_normal_field_by_enum(Enumerator<string> &enumerator, vector<const Type*> &all_types,
vector<CVQualifiers> &all_quals, vector<const Type*> &fields,
vector<CVQualifiers> &quals, vector<int> &fields_length, int i)
{
int types_size = all_types.size();
int quals_size = all_quals.size();
Filter *filter = SIMPLE_TYPES_PROB_FILTER;
std::ostringstream ss1, ss2;
ss1 << "field" << i;
int typ_index = enumerator.get_elem(ss1.str());
assert(typ_index >= 0 && typ_index < types_size);
Type *typ = const_cast<Type*>(all_types[typ_index]);
if (typ->eType == eSimple) {
assert(typ->simple_type != eVoid);
if (filter->filter(typ->simple_type))
return false;
}
assert(typ != NULL);
fields.push_back(typ);
ss2 << "qualifier" << i;
int qual_index = enumerator.get_elem(ss2.str());
assert(qual_index >= 0 && qual_index < quals_size);
CVQualifiers qual = all_quals[qual_index];
quals.push_back(qual);
fields_length.push_back(-1);
return true;
}
void
Type::make_all_struct_types_(Enumerator<string> &bitfields_enumerator, vector<const Type*> &accum_types,
vector<const Type*> &all_types, vector<CVQualifiers> &all_quals,
vector<CVQualifiers> &all_bitfield_quals)
{
Enumerator<string> fields_enumerator;
init_fields_enumerator(fields_enumerator, bitfields_enumerator, all_types.size(),
all_quals.size(), all_bitfield_quals.size());
Enumerator<string> *i;
for (i = fields_enumerator.begin(); i != fields_enumerator.end(); i = i->next()) {
make_all_struct_types_with_bitfields(*i, bitfields_enumerator, accum_types, all_types, all_quals, all_bitfield_quals);
}
}
void
Type::make_all_struct_types_with_bitfields(Enumerator<string> &enumerator,
Enumerator<string> &bitfields_enumerator, vector<const Type*> &accum_types,
vector<const Type*> &all_types, vector<CVQualifiers> &all_quals,
vector<CVQualifiers> &all_bitfield_quals)
{
vector<const Type*> fields;
vector<CVQualifiers> quals;
vector<int> fields_length;
int field_cnt = CGOptions::max_struct_fields();
bool last_is_zero = false;
int bitfields_cnt = 0;
int normal_fields_cnt = 0;
for (int i = 0; i < field_cnt; ++i) {
std::ostringstream ss;
ss << "bitfield" << i;
bool is_bitfield = bitfields_enumerator.get_elem(ss.str());
bool rv = false;
if (is_bitfield) {
rv = make_one_bitfield_by_enum(enumerator, all_bitfield_quals, fields, quals, fields_length, i, last_is_zero);
bitfields_cnt++;
}
else {
rv = make_one_normal_field_by_enum(enumerator, all_types, all_quals, fields, quals, fields_length, i);
last_is_zero = rv ? false : last_is_zero;
normal_fields_cnt++;
}
if (!rv)
return;
}
if ((ExhaustiveBitFieldsProb > 0) && (ExhaustiveBitFieldsProb < 100) &&
((bitfields_cnt == field_cnt) || (normal_fields_cnt == field_cnt)))
return;
bool packed = enumerator.get_elem("packed");
Type* new_type = new Type(fields, true, packed, quals, fields_length);
new_type->used = true;
accum_types.push_back(new_type);
}
/*
* level control's the nested level of struct
*/
void
Type::copy_all_fields_types(vector<const Type*> &dest_types, vector<const Type*> &src_types)
{
vector<const Type*>::const_iterator i;
for (i = src_types.begin(); i != src_types.end(); ++i)
dest_types.push_back(*i);
}
void
Type::reset_accum_types(vector<const Type*> &accum_types)
{
accum_types.clear();
vector<Type*>::const_iterator i;
for (i = AllTypes.begin(); i != AllTypes.end(); ++i)
accum_types.push_back(*i);
}
void
Type::delete_useless_structs(vector<const Type*> &all_types, vector<const Type*> &accum_types)
{
assert(all_types.size() <= accum_types.size());
for (size_t i = 0; i < all_types.size(); ++i) {
const Type *t = all_types[i];
if (t->eType == eStruct) {
const Type *t1 = accum_types[i];
delete t1;
accum_types[i] = t;
}
}
}
void
Type::make_all_struct_types(int level, vector<const Type*> &accum_types)
{
if (level > 0) {
make_all_struct_types(level - 1, accum_types);
}
vector<const Type*> all_types;
copy_all_fields_types(all_types, accum_types);
reset_accum_types(accum_types);
vector<CVQualifiers> all_quals;
CVQualifiers::get_all_qualifiers(all_quals, RegularConstProb, RegularVolatileProb);
vector<CVQualifiers> all_bitfield_quals;
CVQualifiers::get_all_qualifiers(all_bitfield_quals, FieldConstProb, FieldVolatileProb);
Enumerator<string> fields_enumerator;
init_is_bitfield_enumerator(fields_enumerator, ExhaustiveBitFieldsProb);
Enumerator<string> *i;
for (i = fields_enumerator.begin(); i != fields_enumerator.end(); i = i->next()) {
make_all_struct_types_(*i, accum_types, all_types, all_quals, all_bitfield_quals);
}
delete_useless_structs(all_types, accum_types);
}
void
Type::make_all_struct_union_types(void)
{
int level = CGOptions::max_nested_struct_level();
if (CGOptions::dfs_exhaustive()) {
vector<const Type*> accum_types;
reset_accum_types(accum_types);
make_all_struct_types(level, accum_types);
assert(accum_types.size() >= AllTypes.size());
for (size_t i = AllTypes.size(); i < accum_types.size(); ++i)
AllTypes.push_back(const_cast<Type*>(accum_types[i]));
}
}
Type*
Type::make_random_struct_type(void)
{
size_t field_cnt = 0;
size_t max_cnt = CGOptions::max_struct_fields();
if (CGOptions::fixed_struct_fields())
field_cnt = max_cnt;
else
field_cnt = rnd_upto(max_cnt) + 1;
ERROR_GUARD(NULL);
vector<const Type*> random_fields;
vector<CVQualifiers> qualifiers;
vector<int> fields_length;
bool is_bitfields = CGOptions::bitfields() && rnd_flipcoin(BitFieldsCreationProb);
ERROR_GUARD(NULL);
//if (CGOptions::bitfields())
if (is_bitfields)
make_full_bitfields_struct_fields(field_cnt, random_fields, qualifiers, fields_length);
else
make_normal_struct_fields(field_cnt, random_fields, qualifiers, fields_length);
ERROR_GUARD(NULL);
// for now, no union type
bool packed = false;
if (CGOptions::packed_struct()) {
packed = rnd_flipcoin(50);
ERROR_GUARD(NULL);