Repository navigation
Expand file tree
/
Copy pathFuzzSMT.java
More file actions
7018 lines (6535 loc) · 270 KB
/
Copy pathFuzzSMT.java
File metadata and controls
7018 lines (6535 loc) · 270 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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 {
static boolean smtlib1; // if true output in smtlib1 format.
static boolean noOverflow = false; // if true, do not emit bv overflow predicates.
static int maxNary = 3; // max arity for left-assoc bv ops (bvand/bvor/bvadd/bvmul/bvxor); 2 = binary only.
/* distinct over three or more Booleans is trivially false by pigeonhole, so
* an n-ary Boolean distinct is only emitted with probability
* 1/naryDistinctOdds; otherwise the binary form is used. Deliberately set
* very high: the n-ary form remains reachable, so the parser path is still
* covered over a long fuzzing run, but it is rare enough that it is
* effectively absent from any single batch of instances. */
private static final int naryDistinctOdds = 32000;
static boolean fpAnySort = false; // if true, draw arbitrary (eb,sb) instead of the four standard sorts.
static boolean fpWideOps = false; // if true, also emit fp.div/fp.rem/fp.fma on sorts wider than Float64.
static int maxFPExp = 15; // max exponent bits when -fp-any is given.
static int maxFPSig = 24; // max significand bits when -fp-any is given.
static String bulkPrefix =""; // Prepend to bulk output.
static boolean produceModels = false; // if true, set :produce-models and follow check-sat with get-value and get-model.
static ArrayList<SMTNode> declaredConsts = new ArrayList<SMTNode>(); // the zero-arity declarations of the current instance, which -models asks for.
/* -models re-runs the logic's layers after check-sat, inside a get-value,
* to build a term for the solver to evaluate. While replaying is set the
* declaring functions print nothing and hand back the symbols declared
* the first time round, in order, from these lists. */
static boolean replaying = false;
static int declaredConstCursor = 0;
static ArrayList<SMTType> declaredSorts = new ArrayList<SMTType>();
static int declaredSortCursor = 0;
static ArrayList<List<UFunc>> declaredFuncBatches = new ArrayList<List<UFunc>>();
static int declaredFuncCursor = 0;
static ArrayList<List<UPred>> declaredPredBatches = new ArrayList<List<UPred>>();
static int declaredPredCursor = 0;
static ArrayList<SMTType> internedTypes = new ArrayList<SMTType>();
static java.io.PrintStream output; // where output is written to.
/*----------------------------------------------------------------------------*/
/* 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 (smtlib1)
builder.append("(");
else
builder.append("((_ ");
if (r.nextBoolean())
builder.append ("zero_extend");
else
builder.append ("sign_extend");
if (smtlib1)
{
builder.append ("[");
builder.append (ext);
builder.append ("] ");
}
else
{
builder.append (" ");
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 (smtlib1)
builder.append("(");
else
builder.append("((_ ");
if (r.nextBoolean())
builder.append ("zero_extend");
else
builder.append ("sign_extend");
if (smtlib1)
{
builder.append ("[");
builder.append (ext);
builder.append ("] ");
}
else
{
builder.append (" ");
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 (smtlib1)
builder.append("(");
else
builder.append("((_ ");
if (r.nextBoolean())
builder.append ("zero_extend");
else
builder.append ("sign_extend");
if (smtlib1)
{
builder.append ("[");
builder.append (diff);
builder.append ("] ");
}
else
{
builder.append (" ");
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);
if (smtlib1)
{
builder.append ("(extract[");
builder.append (upper);
builder.append (":");
builder.append (lower);
builder.append ("] ");
builder.append (n.getName());
builder.append (")");
}
else
{
builder.append ("((_ extract ");
builder.append (upper);
builder.append (" ");
builder.append (lower);
builder.append (") ");
builder.append (n.getName());
builder.append (")");
}
}
return builder.toString();
}
/*----------------------------------------------------------------------------*/
/* Model queries */
/*----------------------------------------------------------------------------*/
/* A declared constant of the same sort as the given one, preferring one
* that is not the constant itself when the sort has more than one. */
private static SMTNode selectSameSortConst (Random r, List<SMTNode> consts,
SMTNode node){
ArrayList<SMTNode> matches = new ArrayList<SMTNode>();
assert (r != null);
assert (consts != null);
assert (node != null);
for (int i = 0; i < consts.size(); i++) {
SMTNode cur = consts.get(i);
if (cur != node && cur.getType().equals (node.getType()))
matches.add (cur);
}
if (matches.isEmpty())
return node;
return matches.get (r.nextInt (matches.size()));
}
/* The index and element sorts of any array sort the logics declare. */
private static SMTType indexSortOf (SMTType type){
assert (type instanceof ArrayType);
if (type instanceof BVArrayType)
return new BVType (((BVArrayType) type).getIndexWidth());
if (type instanceof MixedArrayType)
return ((MixedArrayType) type).getIndexType();
if (type instanceof ArrayTypeFromTo)
return ((ArrayTypeFromTo) type).getIndexType();
assert (type instanceof Array1Type || type instanceof Array2Type);
return IntType.intType;
}
private static SMTType elementSortOf (SMTType type){
assert (type instanceof ArrayType);
if (type instanceof BVArrayType)
return new BVType (((BVArrayType) type).getValWidth());
if (type instanceof MixedArrayType)
return ((MixedArrayType) type).getElementType();
if (type instanceof ArrayTypeFromTo)
return ((ArrayTypeFromTo) type).getElementType();
if (type instanceof Array2Type)
return Array1Type.array1Type;
assert (type instanceof Array1Type);
return RealType.realType;
}
/* A closed term of the given sort for use as an array index: a declared
* constant of that sort when there is one, else a zero literal. An
* uninterpreted sort has no literal, so null is returned when no constant
* of it was declared. */
private static String indexTermOfSort (Random r, SMTType type){
ArrayList<SMTNode> matches = new ArrayList<SMTNode>();
assert (r != null);
assert (type != null);
for (int i = 0; i < declaredConsts.size(); i++)
if (declaredConsts.get(i).getType().equals (type))
matches.add (declaredConsts.get(i));
if (!matches.isEmpty())
return matches.get (r.nextInt (matches.size())).getName();
if (type instanceof BVType)
return "(_ bv0 " + ((BVType) type).getWidth() + ")";
if (type instanceof FPType)
return "(_ +zero " + ((FPType) type).getExponentBits() + " " +
((FPType) type).getSignificandBits() + ")";
if (type == IntType.intType)
return "0";
if (type == RealType.realType)
return "0.0";
return null;
}
/* The term get-value asks for in place of a declared constant. A scalar
* is asked for by name. An array is read at an index, selecting again
* while the element is itself an array, because a value for a whole array
* is something not every solver will print, while the value of a select is
* what a solver has to be able to give. Arrays over an uninterpreted
* index sort with no declared index fall back to the bare name. */
private static String queryTerm (Random r, SMTNode node){
SMTType type;
String term, index;
assert (r != null);
assert (node != null);
term = node.getName();
type = node.getType();
while (type instanceof ArrayType) {
index = indexTermOfSort (r, indexSortOf (type));
if (index == null)
return node.getName();
term = "(select " + term + " " + index + ")";
type = elementSortOf (type);
}
return term;
}
/* The commands -models puts after check-sat, in random order. Each entry
* is one command, except null, which stands for a get-value whose term is
* built by running the logic's layers again (see main). The plain
* get-values ask for every declared constant once, in groups of random
* size and order, and compare a few same-sort scalar constants, whose
* Boolean has to agree with the values printed for the operands. Only
* declared constants can be asked for by name: the let-bound terms of the
* formula are out of scope once the assert closes. */
private static ArrayList<String> planModelQueries (Random r, int numTerms,
int numGetModels){
ArrayList<SMTNode> consts, paired;
ArrayList<String> queries;
SMTNode a, b;
StringBuilder builder;
int i, groupSize, numComparisons;
assert (r != null);
assert (numTerms >= 0);
assert (numGetModels >= 0);
assert (!smtlib1);
queries = new ArrayList<String>();
consts = new ArrayList<SMTNode>(declaredConsts);
Collections.shuffle (consts, r);
i = 0;
while (i < consts.size()) {
groupSize = Math.min (r.nextInt (4) + 1, consts.size() - i);
builder = new StringBuilder ("(get-value (");
for (int j = 0; j < groupSize; j++) {
if (j > 0)
builder.append (" ");
builder.append (queryTerm (r, consts.get(i + j)));
}
builder.append ("))");
queries.add (builder.toString());
i += groupSize;
}
/* compare scalar constants that share a sort with another where
* possible, so the comparison is not of a constant with itself. Whole
* arrays are left out: deciding their equality is a feature not every
* solver has, and the array-equality layer covers it where it does. */
paired = new ArrayList<SMTNode>();
for (int k = 0; k < consts.size(); k++)
if (!(consts.get(k).getType() instanceof ArrayType) &&
selectSameSortConst (r, consts, consts.get(k)) != consts.get(k))
paired.add (consts.get(k));
if (paired.isEmpty())
for (int k = 0; k < consts.size(); k++)
if (!(consts.get(k).getType() instanceof ArrayType))
paired.add (consts.get(k));
numComparisons = Math.min (4, paired.size());
for (int k = 0; k < numComparisons; k++) {
a = paired.get (r.nextInt (paired.size()));
b = selectSameSortConst (r, consts, a);
queries.add ("(get-value ((" + (r.nextBoolean() ? "= " : "distinct ") +
a.getName() + " " + b.getName() + ")))");
}
for (int k = 0; k < numGetModels; k++)
queries.add ("(get-model)");
for (int k = 0; k < numTerms; k++)
queries.add (null);
Collections.shuffle (queries, r);
return queries;
}
/* The term a replayed get-value asks for: any term built in the replay,
* or any declared constant, other than an array, since not every solver
* prints a value for a whole array. Every let the replay opened is still
* open where the term is printed, so every name is in scope. */
private static SMTNode selectQueryTerm (Random r){
ArrayList<SMTNode> candidates = new ArrayList<SMTNode>();
assert (r != null);
assert (replaying);
assert (SMTNode.recorded != null);
for (int i = 0; i < SMTNode.recorded.size(); i++)
if (!(SMTNode.recorded.get(i).getType() instanceof ArrayType))
candidates.add (SMTNode.recorded.get(i));
for (int i = 0; i < declaredConsts.size(); i++)
if (!(declaredConsts.get(i).getType() instanceof ArrayType))
candidates.add (declaredConsts.get(i));
assert (!candidates.isEmpty());
return candidates.get (r.nextInt (candidates.size()));
}
/* Bookkeeping for the replays: cleared per instance, cursors reset per
* replay. */
private static void clearDeclarations (){
replaying = false;
declaredConsts.clear();
declaredSorts.clear();
declaredFuncBatches.clear();
declaredPredBatches.clear();
internedTypes.clear();
declaredConstCursor = 0;
declaredSortCursor = 0;
declaredFuncCursor = 0;
declaredPredCursor = 0;
SMTNode.recorded = null;
}
private static void startReplay (){
replaying = true;
declaredConstCursor = 0;
declaredSortCursor = 0;
declaredFuncCursor = 0;
declaredPredCursor = 0;
SMTNode.recorded = new ArrayList<SMTNode>();
}
/* The replay counterpart of the constant declarers: the next n constants
* declared the first time round, in order. Each declarer declares a
* count fixed per instance, so the cursor stays in step. */
private static int reuseDeclaredConsts (List<SMTNode> nodes, int n){
assert (replaying);
assert (nodes != null);
assert (declaredConstCursor + n <= declaredConsts.size());
for (int i = 0; i < n; i++)
nodes.add (declaredConsts.get (declaredConstCursor++));
return n;
}
/* The function and predicate declarers draw how many they declare, so a
* replay cannot know the count: each call records what it declared as a
* batch, and the replay hands the batches back in order. */
private static void recordFuncBatch (List<UFunc> funcs, int oldSize){
assert (!replaying);
declaredFuncBatches.add (
new ArrayList<UFunc>(funcs.subList (oldSize, funcs.size())));
}
private static int reuseFuncBatch (List<UFunc> funcs){
List<UFunc> batch;
assert (replaying);
assert (declaredFuncCursor < declaredFuncBatches.size());
batch = declaredFuncBatches.get (declaredFuncCursor++);
funcs.addAll (batch);
return batch.size();
}
private static void recordPredBatch (List<UPred> preds, int oldSize){
assert (!replaying);
declaredPredBatches.add (
new ArrayList<UPred>(preds.subList (oldSize, preds.size())));
}
private static int reusePredBatch (List<UPred> preds){
List<UPred> batch;
assert (replaying);
assert (declaredPredCursor < declaredPredBatches.size());
batch = declaredPredBatches.get (declaredPredCursor++);
preds.addAll (batch);
return batch.size();
}
/* The sort objects a logic case makes for itself. The layers compare
* sorts by identity, so a replay has to use the very objects the declared
* constants carry: the first call with a given sort keeps it, and later
* calls with the same sort hand the kept one back. Sameness is by class
* and printed form rather than an equals of the sort classes: the layers
* also draw from hash maps keyed on nodes, whose order follows the
* identity hash codes, and giving the sort classes hash codes of their
* own shifts those and with them the formula a seed produces. */
private static SMTType internType (SMTType type){
SMTType kept;
assert (type != null);
for (int i = 0; i < internedTypes.size(); i++) {
kept = internedTypes.get(i);
if (kept.getClass() == type.getClass() &&
kept.toString (false).equals (type.toString (false)))
return kept;
}
assert (!replaying);
internedTypes.add (type);
return type;
}
/*----------------------------------------------------------------------------*/
/* Input Layer */
/*----------------------------------------------------------------------------*/
/* Every zero-arity declaration goes through here so that -models can ask
* for its value after check-sat. Uninterpreted functions and predicates
* with arguments are not recorded: get-value takes terms, not functions. */
private static void addDeclaredConst (List<SMTNode> nodes, SMTNode node){
assert (nodes != null);
assert (node != null);
nodes.add (node);
declaredConsts.add (node);
}
private static int generateVarsOfOneType (List<SMTNode> nodes, int numVars,
SMTType type){
if (replaying)
return reuseDeclaredConsts (nodes, numVars);
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();
if (smtlib1)
{
builder.append (":extrafuns ((");
builder.append (name);
builder.append (" ");
builder.append (type.toString(smtlib1));
builder.append ("))\n");
}
else
{
builder.append ("(declare-fun ");
builder.append (name);
builder.append (" () ");
builder.append (type.toString(smtlib1));
builder.append (")\n");
}
addDeclaredConst (nodes, new SMTNode (type, name));
}
output.print (builder.toString());
return numVars;
}
private static int generateBVVars (Random r, List<SMTNode> nodes, int numVars,
int minBW, int maxBW) {
if (replaying)
return reuseDeclaredConsts (nodes, numVars);
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();
if (smtlib1)
{
builder.append (":extrafuns ((");
builder.append (name);
builder.append (" BitVec[");
builder.append (bw);
builder.append ("]))\n");
}
else
{
builder.append ("(declare-fun ");
builder.append (name);
builder.append (" () (_ BitVec ");
builder.append (bw);
builder.append ("))\n");
}
node = new SMTNode (new BVType (bw), name);
addDeclaredConst (nodes, node);
}
output.print (builder.toString());
return numVars;
}
private static int generateBVConsts (Random r, List<SMTNode> nodes,
int numConsts, int minBW, int maxBW) {
int bw;
int size;
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();
size = nodes.size();
for (int i = 0; i < numConsts; i++) {
bw = selectRandValRange (r, minBW, maxBW);
assert (bw >= minBW && bw <= maxBW);
name = letName();
bi = new BigInteger(bw, r);
builder.append(letStart());
builder.append (name);
if (smtlib1)
{
builder.append (" bv");
builder.append (bi.toString());
builder.append ("[");
builder.append (bw);
builder.append ("]");
}
else
{
builder.append (" (_ bv" );
builder.append (bi.toString());
builder.append (" " );
builder.append (bw);
builder.append (")" );
}
builder.append (letClose());
node = new SMTNode (new BVType (bw), name);
nodes.add (node);
}
output.print (builder.toString());
return numConsts;
}
private static String letStart()
{
if (smtlib1)
return "(let (";
else
return "(let ((";
}
private static String uMinus()
{
return SMTNodeKind.UNMINUS.getString(smtlib1);
}
private static String letClose()
{
if (smtlib1)
return ")\n";
else
return "))\n";
}
private static String letName()
{
if (smtlib1)
return "?e" + SMTNode.getNodeCtr();
else
return "e" + SMTNode.getNodeCtr();
}
private static String oneBit()
{
//bv1[1] bv0[1]");
if (smtlib1)
return "bv1[1]";
else
//return "#b1";
return "(_ bv1 1)";
}
private static String zeroBit()
{
if (smtlib1)
return "bv0[1]";
else
//return "#b0";
return "(_ bv0 1)";
}
private static String fletName()
{
if (smtlib1)
return "$e" + SMTNode.getNodeCtr();
else
return "e" + SMTNode.getNodeCtr();
}
private static String fletStart()
{
if (smtlib1)
return "(flet (";
else
return "(let ((";
}
private static int generateBVArrayVars (Random r, List<SMTNode> nodes,
int numArrays, int minBW, int maxBW) {
if (replaying)
return reuseDeclaredConsts (nodes, numArrays);
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();
if (smtlib1)
{
builder.append (":extrafuns ((");
builder.append (name);
builder.append (" Array[");
builder.append (indexWidth);
builder.append (":");
builder.append (valWidth);
builder.append ("]))\n");
}
else
{
builder.append("(declare-fun ");
builder.append(name);
builder.append(" () (Array (_ BitVec ");
builder.append(indexWidth);
builder.append(") (_ BitVec ");
builder.append(valWidth);
builder.append(")))\n");
}
node = new SMTNode (new BVArrayType (indexWidth, valWidth), name);
addDeclaredConst (nodes, node);
}
output.print (builder.toString());
return numArrays;
}
/* The floating-point sorts that occur in the given nodes, each once. */
private static List<FPType> distinctFPTypes (List<SMTNode> nodes){
ArrayList<FPType> types = new ArrayList<FPType>();
assert (nodes != null);
for (int i = 0; i < nodes.size(); i++) {
SMTType cur = nodes.get(i).getType();
if (cur instanceof FPType && !types.contains (cur))
types.add ((FPType) cur);
}
return types;
}
/* A term of exactly the given floating-point sort. The fp operators need
* no such search -- they impose one operand's sort on the others -- but an
* array's index and element sorts are fixed by its declaration, and there
* is no adaptBW for floating point: converting would round. Callers
* guarantee a term exists by drawing array sorts from the sorts the
* declared variables already use. */
private static SMTNode selectFPNodeOfType (Random r, List<SMTNode> fps,
FPType type){
ArrayList<SMTNode> matches = new ArrayList<SMTNode>();
assert (r != null);
assert (fps != null);
assert (type != null);
for (int i = 0; i < fps.size(); i++)
if (fps.get(i).getType().equals (type))
matches.add (fps.get(i));
assert (!matches.isEmpty());
return matches.get (r.nextInt (matches.size()));
}
/* Array variables for the logics that mix arrays with floating point:
* each side of the array sort is drawn independently as a bit-vector or
* as one of the floating-point sorts in fpSorts, so the read and write
* layers always have terms of the exact sort to hand. A draw where both
* sides come out bit-vector keeps its BVArrayType, making those arrays
* exactly the ones the bit-vector logics generate. */
private static int generateABVFPArrayVars (Random r, List<SMTNode> nodes,
int numArrays, int minBW,
int maxBW, List<FPType> fpSorts) {
if (replaying)
return reuseDeclaredConsts (nodes, numArrays);
String name;
SMTType indexType, elementType, type;
StringBuilder builder;
assert (r != null);
assert (nodes != null);
assert (numArrays > 0);
assert (minBW > 0);
assert (maxBW >= minBW);
assert (fpSorts != null);
assert (!fpSorts.isEmpty());
assert (!smtlib1);
builder = new StringBuilder();
for (int i = 0; i < numArrays; i++) {
if (r.nextBoolean())
indexType = new BVType (selectRandValRange (r, minBW, maxBW));
else
indexType = fpSorts.get (r.nextInt (fpSorts.size()));
if (r.nextBoolean())
elementType = new BVType (selectRandValRange (r, minBW, maxBW));
else
elementType = fpSorts.get (r.nextInt (fpSorts.size()));
if (indexType instanceof BVType && elementType instanceof BVType)
type = new BVArrayType (((BVType) indexType).getWidth(),
((BVType) elementType).getWidth());
else
type = new MixedArrayType (indexType, elementType);
name = "a" + SMTNode.getNodeCtr();
builder.append ("(declare-fun ");
builder.append (name);
builder.append (" () (Array ");
builder.append (indexType.toString (false));
builder.append (" ");
builder.append (elementType.toString (false));
builder.append ("))\n");
addDeclaredConst (nodes, new SMTNode (type, name));
}
output.print (builder.toString());
return numArrays;
}
/* The index and element sorts of an array node, across both array types. */
private static SMTType arrayIndexType (SMTNode array){
SMTType type = array.getType();
if (type instanceof BVArrayType)
return new BVType (((BVArrayType) type).getIndexWidth());
return ((MixedArrayType) type).getIndexType();
}
private static SMTType arrayElementType (SMTNode array){
SMTType type = array.getType();
if (type instanceof BVArrayType)
return new BVType (((BVArrayType) type).getValWidth());
return ((MixedArrayType) type).getElementType();
}
/* A term spelled for one side of an array operation: a bit-vector side
* reuses an existing term adapted to the width, a floating-point side
* picks a term of exactly that sort. */
private static String arraySideTerm (Random r, SMTType sideType,
List<SMTNode> bvs, List<SMTNode> fps){
if (sideType instanceof FPType)
return selectFPNodeOfType (r, fps, (FPType) sideType).getName();
SMTNode node = bvs.get (r.nextInt (bvs.size()));
return adaptBW (r, node, ((BVType) sideType).getWidth());
}
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);