-
Notifications
You must be signed in to change notification settings - Fork 0
/
FuzzSMT.java
5232 lines (4918 loc) · 202 KB
/
FuzzSMT.java
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
/* FuzzSMT: Fuzzing tool for Satisfiablity Modulo Theories (SMT) benchmarks.
* Copyright (C) 2009 Robert Daniel Brummayer
*
* This file is part of FuzzSMT.
*
* FuzzSMT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FuzzSMT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.util.*;
import java.math.*;
public class FuzzSMT {
/*----------------------------------------------------------------------------*/
/* Auxillary */
/*----------------------------------------------------------------------------*/
private enum BVDivMode {
OFF,
GUARD,
FULL;
}
private enum RelCompMode {
OFF,
EQ,
FULL;
}
private static int selectRandValRange (Random r, int min, int max){
int result;
assert (r != null);
assert (min >= 0);
assert (max >= 0);
assert (max >= min);
result = r.nextInt(max - min + 1) + min;
assert (result >= min);
assert (result <= max);
return result;
}
private static void updateStringRefs (HashMap<String, Integer> map,
String string, int minRefs){
Integer refs;
assert (map != null);
assert (string != null);
assert (minRefs > 0);
refs = map.get(string);
if (refs != null) {
refs = new Integer (refs.intValue() + 1);
if (refs.intValue() >= minRefs)
map.remove(string);
else
map.put (string, refs);
}
}
private static void updateNodeRefs (HashMap<SMTNode, Integer> map,
SMTNode node, int minRefs){
Integer refs;
assert (map != null);
assert (node != null);
assert (minRefs > 0);
refs = map.get(node);
if (refs != null) {
refs = new Integer (refs.intValue() + 1);
if (refs.intValue() >= minRefs)
map.remove(node);
else
map.put (node, refs);
}
}
private static void updateFuncRefs (HashMap<UFunc, Integer> map,
UFunc uFunc, int minRefs){
Integer refs;
assert (map != null);
assert (uFunc != null);
assert (minRefs > 0);
refs = map.get(uFunc);
if (refs != null) {
refs = new Integer (refs.intValue() + 1);
if (refs.intValue() >= minRefs)
map.remove(uFunc);
else
map.put (uFunc, refs);
}
}
private static void updatePredRefs (HashMap<UPred, Integer> map,
UPred uPred, int minRefs){
Integer refs;
assert (map != null);
assert (uPred != null);
assert (minRefs > 0);
refs = map.get(uPred);
if (refs != null) {
refs = new Integer (refs.intValue() + 1);
if (refs.intValue() >= minRefs)
map.remove(uPred);
else
map.put (uPred, refs);
}
}
private static String wrapEqualBW (Random r, SMTNode n1, SMTNode n2){
int n1bw;
int n2bw;
int ext;
StringBuilder builder;
assert (n1 != null);
assert (n2 != null);
assert (n1.getType() instanceof BVType);
assert (n2.getType() instanceof BVType);
n1bw = (((BVType) n1.getType()).width);
n2bw = (((BVType) n2.getType()).width);
builder = new StringBuilder();
if (n1bw == n2bw) {
builder.append (n1.getName());
builder.append (" ");
builder.append (n2.getName());
} else if (n1bw < n2bw){
ext = n2bw - n1bw;
if (r.nextBoolean())
builder.append ("((_ zero_extend ");
else
builder.append ("((_ sign_extend ");
builder.append (ext);
builder.append (") ");
builder.append (n1.getName());
builder.append (") ");
builder.append (n2.getName());
} else {
assert (n2bw < n1bw);
ext = n1bw - n2bw;
builder.append (n1.getName());
builder.append (" ");
if (r.nextBoolean())
builder.append ("((_ zero_extend ");
else
builder.append ("((_ sign_extend ");
builder.append (ext);
builder.append (") ");
builder.append (n2.getName());
builder.append (")");
}
return builder.toString();
}
private static String adaptBW (Random r, SMTNode n, int bw){
BVType type;
int diff, upper, lower;
StringBuilder builder;
assert (r != null);
assert (n != null);
assert (n.getType() instanceof BVType);
assert (bw > 0);
type = (BVType) n.getType();
builder = new StringBuilder();
if (type.width == bw){
builder.append (n.getName());
} else if (type.width < bw) {
diff = bw - type.width;
if (r.nextBoolean())
builder.append ("((_ zero_extend ");
else
builder.append ("((_ sign_extend ");
builder.append (diff);
builder.append (") ");
builder.append (n.getName());
builder.append (")");
} else {
assert (type.width > bw);
diff = type.width - bw;
lower = r.nextInt(diff + 1);
upper = lower + bw - 1;
assert (upper - lower + 1 == bw);
assert (upper >= 0);
assert (upper >= lower);
assert (upper < type.width);
builder.append ("((_ extract ");
builder.append (upper);
builder.append (" ");
builder.append (lower);
builder.append (") ");
builder.append (n.getName());
builder.append (")");
}
return builder.toString();
}
/*----------------------------------------------------------------------------*/
/* Input Layer */
/*----------------------------------------------------------------------------*/
private static int generateVarsOfOneType (List<SMTNode> nodes, int numVars,
SMTType type){
String name;
StringBuilder builder;
assert (nodes != null);
assert (type != null);
assert (numVars >= 0);
builder = new StringBuilder();
for (int i = 0; i < numVars; i++) {
name = "v" + SMTNode.getNodeCtr();
builder.append ("(declare-fun ");
builder.append (name);
builder.append (" () ");
builder.append (type.toString());
builder.append (")\n");
SMTNode node = new SMTNode (type, name);
nodes.add(node);
}
System.out.print (builder.toString());
return numVars;
}
private static int generateBVVars (Random r, List<SMTNode> nodes, int numVars,
int minBW, int maxBW) {
int bw;
String name;
SMTNode node;
StringBuilder builder;
assert (r != null);
assert (nodes != null);
assert (numVars >= 0);
assert (minBW > 0);
assert (maxBW > 0);
assert (maxBW >= minBW);
builder = new StringBuilder();
for (int i = 0; i < numVars; i++) {
bw = selectRandValRange (r, minBW, maxBW);
assert (bw >= minBW && bw <= maxBW);
name = "v" + SMTNode.getNodeCtr();
builder.append ("(declare-fun ");
builder.append (name);
builder.append (" () (_ BitVec ");
builder.append (bw);
builder.append ("))\n");
node = new SMTNode (new BVType (bw), name);
nodes.add (node);
}
System.out.print (builder.toString());
return numVars;
}
private static int generateBVConsts (Random r, List<SMTNode> nodes,
int numConsts, int minBW, int maxBW) {
int bw;
String name;
SMTNode node;
BigInteger bi;
StringBuilder builder;
assert (r != null);
assert (nodes != null);
assert (numConsts >= 0);
assert (minBW > 0);
assert (maxBW > 0);
assert (maxBW >= minBW);
builder = new StringBuilder();
for (int i = 0; i < numConsts; i++) {
bw = selectRandValRange (r, minBW, maxBW);
assert (bw >= minBW && bw <= maxBW);
name = "e" + SMTNode.getNodeCtr();
bi = new BigInteger(bw, r);
builder.append ("(let ((");
builder.append (name);
builder.append ("(_ bv");
builder.append (bi.toString());
builder.append (" ");
builder.append (bw);
builder.append (")))\n");
node = new SMTNode (new BVType (bw), name);
nodes.add (node);
}
System.out.print (builder.toString());
return numConsts;
}
private static int generateBVArrayVars (Random r, List<SMTNode> nodes,
int numArrays, int minBW, int maxBW) {
int indexWidth, valWidth;
String name;
SMTNode node;
StringBuilder builder;
assert (r != null);
assert (nodes != null);
assert (numArrays > 0);
assert (minBW > 0);
assert (maxBW > 0);
assert (maxBW >= minBW);
builder = new StringBuilder();
for (int i = 0; i < numArrays; i++) {
indexWidth = selectRandValRange (r, minBW, maxBW);
assert (indexWidth >= minBW && indexWidth <= maxBW);
valWidth = selectRandValRange (r, minBW, maxBW);
assert (valWidth >= minBW && valWidth <= maxBW);
name = "a" + SMTNode.getNodeCtr();
builder.append ("(declare-fun ");
builder.append (name);
builder.append (" () (Array ");
builder.append (" (_ BitVec " + indexWidth + ") ");
builder.append (" (_ BitVec " + valWidth + ")");
builder.append ("))\n");
node = new SMTNode (new BVArrayType (indexWidth, valWidth), name);
nodes.add (node);
}
System.out.print (builder.toString());
return numArrays;
}
private static int generateIntVars (List<SMTNode> nodes, int numVars){
assert (nodes != null);
assert (numVars >= 0);
return generateVarsOfOneType (nodes, numVars, IntType.intType);
}
private static int generateIntConsts (Random r, List<SMTNode> nodes,
int numConsts, int maxBW){
String name;
BigInteger bi;
int bw;
StringBuilder builder;
assert (nodes != null);
assert (r != null);
assert (nodes != null);
assert (numConsts >= 0);
assert (maxBW > 0);
builder = new StringBuilder();
for (int i = 0; i < numConsts; i++) {
name = "e" + SMTNode.getNodeCtr();
bw = r.nextInt (maxBW) + 1;
bi = new BigInteger(bw, r);
builder.append ("(let ((");
builder.append (name);
builder.append (" ");
builder.append (bi.toString());
builder.append ("))\n");
nodes.add (new SMTNode (IntType.intType, name));
}
System.out.print (builder.toString());
return numConsts;
}
private static int generateBoolVars (List<SMTNode> nodes, int numVars){
assert (nodes != null);
assert (numVars >= 0);
return generateVarsOfOneType (nodes, numVars, BoolType.boolType);
}
private static int generateRealVars (List<SMTNode> nodes, int numVars){
assert (nodes != null);
assert (numVars >= 0);
return generateVarsOfOneType (nodes, numVars, RealType.realType);
}
/* generates non empty list of int constants.
* at least one constant is not mapped to zero */
private static int generateIntConstsNotFilledZero (Random r,
List<SMTNode> nodes,
Set<SMTNode> zeroConsts,
int numConsts, int maxBW){
String name;
BigInteger bi;
int bw;
SMTNode node;
StringBuilder builder;
assert (r != null);
assert (nodes != null);
assert (zeroConsts != null);
assert (numConsts > 0);
assert (maxBW > 0);
do {
bw = r.nextInt (maxBW) + 1;
bi = new BigInteger(bw, r);
} while (bi.equals(BigInteger.ZERO));
builder = new StringBuilder();
for (int i = 0; i < numConsts; i++) {
name = "e" + SMTNode.getNodeCtr();
builder.append ("(let ((");
builder.append (name);
builder.append (" ");
builder.append (bi.toString());
builder.append ("))\n");
node = new SMTNode (IntType.intType, name);
nodes.add (node);
if (bi.equals(BigInteger.ZERO))
zeroConsts.add(node);
bw = r.nextInt (maxBW) + 1;
bi = new BigInteger(bw, r);
}
System.out.print (builder.toString());
return numConsts;
}
/* generates non empty list of real constants.
* at least one constant is not mapped to zero */
private static int generateRealConstsNotFilledZero (Random r,
List<SMTNode> nodes,
Set<SMTNode> zeroConsts,
int numConsts, int maxBW,
boolean printAsReal){
String name;
BigInteger bi;
int bw;
SMTNode node;
StringBuilder builder;
assert (r != null);
assert (nodes != null);
assert (zeroConsts != null);
assert (numConsts > 0);
assert (maxBW > 0);
do {
bw = r.nextInt (maxBW) + 1;
bi = new BigInteger(bw, r);
} while (bi.equals(BigInteger.ZERO));
builder = new StringBuilder();
for (int i = 0; i < numConsts; i++) {
name = "e" + SMTNode.getNodeCtr();
builder.append ("(let ((");
builder.append (name);
builder.append (" ");
builder.append (bi.toString());
if (printAsReal)
builder.append (".0");
builder.append ("))\n");
node = new SMTNode (RealType.realType, name);
nodes.add (node);
if (bi.equals(BigInteger.ZERO))
zeroConsts.add(node);
bw = r.nextInt (maxBW) + 1;
bi = new BigInteger(bw, r);
}
System.out.print (builder.toString());
return numConsts;
}
private static int generateUTypes (List<SMTType> types, int numUTypes){
String name;
StringBuilder builder;
assert (types != null);
assert (numUTypes > 0);
builder = new StringBuilder();
for (int i = 0; i < numUTypes; i++) {
name = "S" + i;
types.add (new UType (name));
builder.append ("(declare-sort ");
builder.append (name);
builder.append (" 0)\n");
}
System.out.print (builder.toString());
return numUTypes;
}
private static int generateUVars (List<SMTType> sorts, List<SMTNode> nodes,
int numVars) {
int generated = 0;
int sizeSorts;
assert (sorts != null);
assert (sorts.size() > 0);
assert (nodes != null);
sizeSorts = sorts.size();
for (int i = 0; i < sizeSorts; i++)
generated += generateVarsOfOneType (nodes, numVars, sorts.get(i));
assert (generated == sizeSorts * numVars);
return generated;
}
private static int generateUFuncs (Random r, List<SMTType> sorts,
List<UFunc> funcs, int minNumFuncs,
int minArgs, int maxArgs) {
int generated = 0;
int numArgs, sizeSorts;
Signature sig;
ArrayList<SMTType> operandTypes;
SMTType resultType, cur;
HashSet<SMTType> todoResult, todoArg;
String name;
StringBuilder builder;
assert (r != null);
assert (sorts != null);
assert (sorts.size() > 0);
assert (funcs != null);
assert (minNumFuncs > 0);
assert (minArgs >= 1);
assert (maxArgs >= minArgs);
sizeSorts = sorts.size();
/* each sort must be used as return type at least once */
todoResult = new HashSet<SMTType>();
for (int i = 0; i < sizeSorts; i++)
todoResult.add (sorts.get(i));
/* each sort must be used as argument type at least once */
todoArg = new HashSet<SMTType>();
for (int i = 0; i < sizeSorts; i++)
todoArg.add (sorts.get(i));
builder = new StringBuilder();
while (!todoResult.isEmpty() || !todoArg.isEmpty() ||
generated < minNumFuncs){
name = "f" + UFunc.getFuncsCtr();
numArgs = selectRandValRange (r, minArgs, maxArgs);
assert (numArgs >= minArgs);
assert (numArgs <= maxArgs);
operandTypes = new ArrayList<SMTType>(numArgs);
for (int i = 0; i < numArgs; i++) {
cur = sorts.get(r.nextInt(sizeSorts));
operandTypes.add (cur);
if (todoArg.contains (cur))
todoArg.remove(cur);
}
resultType = sorts.get(r.nextInt(sizeSorts));
if (todoResult.contains (resultType))
todoResult.remove (resultType);
sig = new Signature (operandTypes, resultType);
funcs.add (new UFunc (name, sig));
builder.append ("(declare-fun ");
builder.append (name + " (");
for (int i = 0; i < numArgs; i++) {
builder.append (" ");
builder.append (operandTypes.get(i).toString());
}
builder.append (") ");
builder.append (resultType.toString());
builder.append (")\n");
generated++;
}
System.out.print (builder.toString());
assert (generated > 0);
return generated;
}
private static int generateUPreds (Random r, List<SMTType> sorts,
List<UPred> preds, int minNumPreds,
int minArgs, int maxArgs) {
int generated = 0;
int numArgs, sizeSorts;
Signature sig;
ArrayList<SMTType> operandTypes;
SMTType cur;
HashSet<SMTType> todo;
String name;
StringBuilder builder;
assert (r != null);
assert (sorts != null);
assert (sorts.size() > 0);
assert (preds != null);
assert (minNumPreds > 0);
assert (minArgs >= 1);
assert (maxArgs >= minArgs);
/* each sort must be used as argument type at least once */
sizeSorts = sorts.size();
todo = new HashSet<SMTType>();
for (int i = 0; i < sizeSorts; i++)
todo.add (sorts.get(i));
builder = new StringBuilder();
while (!todo.isEmpty() || generated < minNumPreds){
name = "p" + UPred.getPredsCtr();
numArgs = selectRandValRange (r, minArgs, maxArgs);
assert (numArgs >= minArgs);
assert (numArgs <= maxArgs);
operandTypes = new ArrayList<SMTType>(numArgs);
for (int i = 0; i < numArgs; i++) {
cur = sorts.get(r.nextInt(sizeSorts));
if (todo.contains (cur))
todo.remove (cur);
operandTypes.add (cur);
}
sig = new Signature (operandTypes, BoolType.boolType);
preds.add (new UPred (name, sig));
builder.append ("(declare-fun ");
builder.append (name);
builder.append (" (");
for (int i = 0; i < numArgs; i++) {
builder.append (" ");
builder.append (operandTypes.get(i).toString());
}
builder.append (") Bool)\n");
generated++;
}
System.out.print (builder.toString());
assert (generated > 0);
return generated;
}
private static int generateUFuncsBV (Random r, List<UFunc> funcs,
int numFuncs, int minArgs, int maxArgs,
int minBW, int maxBW) {
int numArgs, bw;
Signature sig;
ArrayList<SMTType> operandTypes;
SMTType resultType;
String name;
StringBuilder builder;
assert (r != null);
assert (funcs != null);
assert (numFuncs >= 0);
assert (minArgs >= 1);
assert (maxArgs >= minArgs);
assert (minBW > 0);
assert (maxBW >= minBW);
builder = new StringBuilder();
for (int i = 0; i < numFuncs; i++) {
name = "f" + UFunc.getFuncsCtr();
numArgs = selectRandValRange (r, minArgs, maxArgs);
assert (numArgs >= minArgs);
assert (numArgs <= maxArgs);
operandTypes = new ArrayList<SMTType>(numArgs);
for (int j = 0; j < numArgs; j++) {
bw = selectRandValRange (r, minBW, maxBW);
assert (bw >= minBW);
assert (bw <= maxBW);
operandTypes.add (new BVType (bw));
}
bw = selectRandValRange (r, minBW, maxBW);
assert (bw >= minBW);
assert (bw <= maxBW);
resultType = new BVType (bw);
sig = new Signature (operandTypes, resultType);
funcs.add (new UFunc (name, sig));
builder.append ("(declare-fun ");
builder.append (name + " (");
for (int j = 0; j < numArgs; j++) {
builder.append (" ");
builder.append (operandTypes.get(j).toString());
}
builder.append (") ");
builder.append (resultType.toString());
builder.append (")\n");
}
System.out.print (builder.toString());
return numFuncs;
}
private static int generateUPredsBV (Random r, List<UPred> preds,
int numPreds, int minArgs, int maxArgs,
int minBW, int maxBW) {
int numArgs, bw;
Signature sig;
ArrayList<SMTType> operandTypes;
HashSet<SMTType> todo;
String name;
StringBuilder builder;
assert (r != null);
assert (preds != null);
assert (numPreds >= 0);
assert (minArgs >= 1);
assert (maxArgs >= minArgs);
assert (minBW > 0);
assert (maxBW >= minBW);
builder = new StringBuilder();
for (int i = 0; i < numPreds; i++){
name = "p" + UPred.getPredsCtr();
numArgs = selectRandValRange (r, minArgs, maxArgs);
assert (numArgs >= minArgs);
assert (numArgs <= maxArgs);
operandTypes = new ArrayList<SMTType>(numArgs);
for (int j = 0; j < numArgs; j++) {
bw = selectRandValRange(r, minBW, maxBW);
assert (bw >= minBW);
assert (bw <= maxBW);
operandTypes.add (new BVType(bw));
}
sig = new Signature (operandTypes, BoolType.boolType);
preds.add (new UPred (name, sig));
builder.append ("(declare-fun ");
builder.append (name);
builder.append (" (");
for (int j = 0; j < numArgs; j++) {
builder.append (" ");
builder.append (operandTypes.get(j).toString());
}
builder.append (") Bool)\n");
}
System.out.print (builder.toString());
return numPreds;
}
/*----------------------------------------------------------------------------*/
/* Main layer */
/*----------------------------------------------------------------------------*/
private static int generateBVLayer (Random r, List<SMTNode> nodes,
int minRefs, int minBW, int maxBW,
BVDivMode divMode,
HashMap<SMTNode, SMTNodeKind> guards,
boolean noBlowup, List<UFunc> uFuncs,
List<UPred> uPreds){
int oldSize, upper, lower, maxRep, rep, ext, rotate, pos, tmp;
int sizeOpTypes, n1BW, n2BW, n3BW, resBW = 0;
int sizeUFuncs, sizeUPreds;
SMTNodeKind kind;
EnumSet<SMTNodeKind> kindSet;
SMTNodeKind []kinds;
SMTNode []todoNodesArray;
UFunc []todoUFuncsArray;
UPred []todoUPredsArray;
String name;
UFunc uFunc;
UPred uPred;
Signature sig;
HashMap<SMTNode, Integer> todoNodes;
HashMap<UFunc, Integer> todoUFuncs;
HashMap<UPred, Integer> todoUPreds;
SMTNode n1, n2, n3, tmpNode;
BVType curType;
List<SMTType> operandTypes;
StringBuilder builder;
assert (r != null);
assert (nodes != null);
assert (minRefs > 0);
assert (!nodes.isEmpty());
assert (minBW > 0);
assert (maxBW > 0);
assert (maxBW >= minBW);
assert (divMode != BVDivMode.GUARD || (guards != null));
assert (uFuncs != null);
assert (uPreds != null);
assert (SMTNodeKind.BVNOT.ordinal() < SMTNodeKind.CONCAT.ordinal());
kindSet = EnumSet.range (SMTNodeKind.BVNOT, SMTNodeKind.CONCAT);
kindSet.add (SMTNodeKind.EQ);
kindSet.add (SMTNodeKind.DISTINCT);
kindSet.add (SMTNodeKind.ITE);
if (divMode == BVDivMode.OFF) {
kindSet.remove (SMTNodeKind.BVUDIV);
kindSet.remove (SMTNodeKind.BVSDIV);
kindSet.remove (SMTNodeKind.BVUREM);
kindSet.remove (SMTNodeKind.BVSREM);
kindSet.remove (SMTNodeKind.BVSMOD);
}
kindSet.remove (SMTNodeKind.BVSMOD); /* Semantics changed; SMOD disabled for now */
if (!uFuncs.isEmpty())
kindSet.add (SMTNodeKind.UFUNC);
if (!uPreds.isEmpty())
kindSet.add (SMTNodeKind.UPRED);
kinds = kindSet.toArray(new SMTNodeKind[0]);
oldSize = nodes.size();
todoNodes = new HashMap<SMTNode, Integer>();
for (int i = 0; i < oldSize; i++)
todoNodes.put (nodes.get(i), new Integer(0));
todoUFuncs = new HashMap<UFunc, Integer>();
sizeUFuncs = uFuncs.size();
for (int i = 0; i < sizeUFuncs; i++)
todoUFuncs.put (uFuncs.get(i), new Integer(0));
todoUPreds = new HashMap<UPred, Integer>();
sizeUPreds = uPreds.size();
for (int i = 0; i < sizeUPreds; i++)
todoUPreds.put (uPreds.get(i), new Integer(0));
builder = new StringBuilder();
while (!todoNodes.isEmpty() || !todoUFuncs.isEmpty() ||
!todoUPreds.isEmpty()){
name = "e" + SMTNode.getNodeCtr();
builder.append ("(let ((");
builder.append (name);
builder.append (" (");
/* increase probability that ufunc or upred is selected
* if todo list is not empty */
if (!todoUFuncs.isEmpty() && r.nextBoolean())
kind = SMTNodeKind.UFUNC;
else if (!todoUPreds.isEmpty() && r.nextBoolean())
kind = SMTNodeKind.UPRED;
else
kind = kinds[r.nextInt (kinds.length)];
if (noBlowup && r.nextBoolean() && !todoNodes.isEmpty()) {
todoNodesArray = todoNodes.keySet().toArray(new SMTNode[0]);
n1 = todoNodesArray[r.nextInt(todoNodesArray.length)];
} else {
n1 = nodes.get(r.nextInt(nodes.size()));
}
assert (n1.getType() instanceof BVType);
n1BW = ((BVType) n1.getType()).width;
switch (kind.arity) {
case 1:
builder.append (kind.getString());
switch (kind) {
case BVNOT:
case BVNEG:
builder.append (" ");
resBW = n1BW;
break;
case EXTRACT:
upper = r.nextInt(n1BW);
lower = r.nextInt (upper + 1);
builder.append (" ");
builder.append (upper);
builder.append (" ");
builder.append (lower);
builder.append (") ");
resBW = upper - lower + 1;
break;
case ROTATE_LEFT:
case ROTATE_RIGHT:
rotate = r.nextInt(n1BW);
builder.append (" ");
builder.append (rotate);
builder.append (") ");
resBW = n1BW;
break;
case ZERO_EXTEND:
case SIGN_EXTEND:
ext = r.nextInt(maxBW - n1BW + 1);
builder.append (" ");
builder.append (ext);
builder.append (") ");
resBW = n1BW + ext;
break;
default:
assert (kind == SMTNodeKind.REPEAT);
maxRep = maxBW / n1BW;
rep = r.nextInt(maxRep) + 1;
builder.append (" ");
builder.append (rep);
builder.append (") ");
resBW = n1BW * rep;
break;
}
builder.append (n1.getName());
updateNodeRefs (todoNodes, n1, minRefs);
break;
case 2:
n2 = nodes.get(r.nextInt(nodes.size()));
assert (n2.getType() instanceof BVType);
n2BW = ((BVType) n2.getType()).width;
/* choose another binary operator if concat
* exceeds maximum bit-width */
if (kind == SMTNodeKind.CONCAT && n1BW + n2BW > maxBW) {
do {
kind = kinds[r.nextInt (kinds.length)];
} while (kind.arity != 2 || kind == SMTNodeKind.CONCAT);
}
switch (kind) {
case BVULT:
case BVULE:
case BVUGT:
case BVUGE:
case BVSLT:
case BVSLE:
case BVSGT:
case BVSGE:
case EQ:
/* encode boolean results into bit-vector */
builder.append ("ite (");
builder.append (kind.getString());
builder.append (" ");
builder.append (wrapEqualBW (r, n1, n2));
builder.append (") (_ bv1 1) (_ bv0 1)");
resBW = 1;
break;
case CONCAT:
builder.append (kind.getString());
builder.append (" ");
builder.append (n1.getName());
builder.append (" ");
builder.append (n2.getName());
resBW = n1BW + n2BW;
break;
case BVUDIV:
case BVSDIV:
case BVUREM:
case BVSREM:
case BVSMOD:
if (divMode == BVDivMode.GUARD) {
/* assumption
* wrapEqualBW tries to extend the first node before
* it tries to extend the second node
*/
/* we swap the nodes if the seond node would be extended */
if (n1BW > n2BW) {
tmp = n1BW;
n1BW = n2BW;
n2BW = tmp;
tmpNode = n1;
n1 = n2;
n2 = tmpNode;
}
/* update guards if necessary */
if (!guards.containsKey(n2) ||
guards.get(n2) == SMTNodeKind.BVUDIV ||
guards.get(n2) == SMTNodeKind.BVUREM)
guards.put(n2, kind);
/* fall through by intention */
}
default:
builder.append (kind.getString());
builder.append (" ");
builder.append (wrapEqualBW (r, n1, n2));
if (kind == SMTNodeKind.BVCOMP) {
resBW = 1;
} else {
if (n1BW < n2BW)
resBW = n2BW;
else
resBW = n1BW;
}
break;
}
updateNodeRefs (todoNodes, n1, minRefs);
updateNodeRefs (todoNodes, n2, minRefs);
break;
case 3:
assert (kind == SMTNodeKind.ITE);
n2 = nodes.get(r.nextInt(nodes.size()));
assert (n2.getType() instanceof BVType);
n2BW = ((BVType) n2.getType()).width;
n3 = nodes.get(r.nextInt(nodes.size()));
assert (n3.getType() instanceof BVType);
n3BW = ((BVType) n3.getType()).width;
pos = r.nextInt(n1BW);
builder.append (kind.getString());
/* ite condition: is bit at random bit position set to 1? */
builder.append (" (= (_ bv1 1) ((_ extract ");
builder.append (pos);
builder.append (" ");
builder.append (pos);
builder.append (") ");
builder.append (n1.getName());
builder.append (")) ");
builder.append (wrapEqualBW(r, n2, n3));
if (n2BW < n3BW)
resBW = n3BW;
else
resBW = n2BW;
updateNodeRefs (todoNodes, n1, minRefs);
updateNodeRefs (todoNodes, n2, minRefs);
updateNodeRefs (todoNodes, n3, minRefs);
break;
default:
assert (kind.arity == -1);