-
Notifications
You must be signed in to change notification settings - Fork 140
/
Reducer.cpp
1343 lines (1271 loc) · 37.6 KB
/
Reducer.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, 2009, 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.
#ifdef WIN32
#pragma warning(disable : 4786) /* Disable annoying warning messages */
#endif
#include "Reducer.h"
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <assert.h>
#include "StringUtils.h"
#include "Function.h"
#include "FunctionInvocationUser.h"
#include "FunctionInvocationBinary.h"
#include "Block.h"
#include "Statement.h"
#include "StatementExpr.h"
#include "StatementIf.h"
#include "StatementFor.h"
#include "StatementGoto.h"
#include "StatementArrayOp.h"
#include "StatementReturn.h"
#include "Lhs.h"
#include "Variable.h"
#include "VariableSelector.h"
#include "ArrayVariable.h"
#include "Expression.h"
#include "ExpressionVariable.h"
#include "ExpressionFuncall.h"
#include "Lhs.h"
#include "Constant.h"
#include "CVQualifiers.h"
#include "FactMgr.h"
Reducer::Reducer(string fname)
: dump_block_entry(false),
dump_all_block_info(false),
dump_monitored_var(false),
dump_dropped_params(false),
drop_params(false),
rewrite_calls_inside(NULL),
reduce_binaries(false),
output_if_ids(false),
print_value_with_multi_lines(false),
test_command_line_option(false),
monitored_var(NULL),
monitored_func(NULL),
monitored_call_id(""),
dump_stms_in_blocks(NULL),
configured(false),
fname_(fname)
{
// nothing else to do
}
Reducer::~Reducer(void)
{
}
void
Reducer::configure(void)
{
ifstream conf(fname_.c_str());
std::string line;
// default: use the first function as mian
main = GetFirstFunction();
while(!conf.eof()) {
getline(conf, line);
if (StringUtils::empty_line(line))
continue;
if (dump_dropped_params) {
// if "dropped parameters" is not the last setting, means we are done with parameter dropping
dump_dropped_params = false;
}
// find the variable that caused diff. checksums
if (line.find("focus variable") == 0) {
getline(conf, line);
StringUtils::chop(line);
// make sure the focus var is marked as used var
const Variable* key = VariableSelector::find_var_by_name(line);
if (key == NULL) {
// it's possible an array variable that is not specifically itemized in the
// program that is the focus var. we manually itemize it here
vector<string> strs;
StringUtils::split_string(line, strs, "[]");
const Variable* ary = VariableSelector::find_var_by_name(strs[0]);
assert(ary && ary->isArray);
const ArrayVariable* av = (const ArrayVariable*)ary;
vector<int> indices;
for (size_t k=0; k<av->get_dimension(); k++) {
assert(k + 1 < strs.size());
indices.push_back(StringUtils::str2int(strs[k+1]));
}
av->itemize(indices);
}
key = VariableSelector::find_var_by_name(line);
assert(key);
monitored_var = key;
used_vars.push_back(key->get_named_var());
}
else if (line.find("keep variable") == 0) {
getline(conf, line);
StringUtils::chop(line);
// make sure the focus var is marked as used var
vector<string> vnames;
StringUtils::split_string(line, vnames, ", ");
for (size_t i=0; i<vnames.size(); i++) {
const Variable* v = VariableSelector::find_var_by_name(vnames[i]);
assert(v);
used_vars.push_back(v);
}
}
else if (line.find("drop parameters") == 0) {
getline(conf, line);
StringUtils::chop(line);
if (line == "all") {
drop_params = true;
dump_dropped_params = true;
}
}
// find the active blocks
else if (line.find("active blocks") == 0) {
getline(conf, line);
config_active_blks(line);
}
else if (line.find("print checksum with multi-lines") == 0) {
print_value_with_multi_lines = true;
}
else if (line.find("test command line option") == 0) {
test_command_line_option = true;
main_str = "func_1()";
}
else if (line.find("call chain shortcut") == 0) {
getline(conf, line);
config_call_chain_shortcut(line);
}
else if (line.find("replace statement") == 0) {
getline(conf, line);
config_stm_reduction(line);
}
else if (line.find("replace calls") == 0) {
getline(conf, line);
config_expr_reduction(line);
}
else if (line.find("replace binary") == 0) {
getline(conf, line);
config_binary_reduction(line);
}
else if (line.find("replace if") == 0) {
getline(conf, line);
config_if_reduction(line);
}
else if (line.find("replace array initializer") == 0) {
getline(conf, line);
config_var_init_reduction(line);
}
}
conf.close();
configured = true;
}
const Statement*
Reducer::find_stm_by_id(int stm_id)
{
size_t j;
map<const Block*, int>::const_iterator iter;
for(iter = map_active_blks.begin(); iter != map_active_blks.end(); ++iter) {
const Block* b = iter->first;
for (j=0; j<b->stms.size(); j++) {
if (b->stms[j]->stm_id == stm_id) {
return b->stms[j];
}
}
}
return NULL;
}
bool
Reducer::is_ptr_written_in_stm(const Statement* stm)
{
size_t i;
FactMgr* fm = get_fact_mgr_for_func(stm->func);
const vector<const Variable *>& write_vars = fm->map_stm_effect[stm].get_write_vars();
for (i=0; i<write_vars.size(); i++) {
const Variable* wvar = write_vars[i];
if (wvar->is_pointer()) {
const Block* blk = (stm->eType == eBlock) ? (const Block*)stm : stm->parent;
if (wvar->is_visible(blk) && is_var_used(wvar)) {
return true;
}
}
}
return false;
}
bool
Reducer::is_exp_replaced(const Expression* e)
{
if (e->term_type == eVariable || e->term_type == eLhs) {
return map_reduced_vars.find(e) != map_reduced_vars.end();
}
if (e->term_type == eFunction) {
const FunctionInvocation* fi = e->get_invoke();
assert(fi);
return map_reduced_invocations.find(fi) != map_reduced_invocations.end();
}
return false;
}
bool
Reducer::is_var_init_reduced(const Variable* v)
{
return map_reduced_var_inits.find(v) != map_reduced_var_inits.end();
}
bool
Reducer::is_label_used(const string l)
{
if (l == "") return false;
return std::find(used_labels.begin(), used_labels.end(), l) != used_labels.end();
}
string
Reducer::find_jump_label(const Statement* stm)
{
string label = stm->find_jump_label();
return (is_label_used(label)) ? label : "";
}
int
Reducer::find_required_labels(const Statement* stm, vector<string>& labels)
{
string label = find_jump_label(stm);
if (label != "") {
labels.push_back(label);
}
vector<const Block*> blks;
size_t i, j;
stm->get_blocks(blks);
for (i=0; i<blks.size(); i++) {
if (!is_blk_deleted(blks[i])) {
for (j=0; j<blks[i]->stms.size(); j++) {
const Statement* s = blks[i]->stms[j];
find_required_labels(s, labels);
}
}
}
return labels.size();
}
int
Reducer::find_missing_labels(const Statement* stm, const Statement* alt_stm, vector<string>& labels)
{
labels.clear();
vector<string> labels1, labels2;
stm->find_contained_labels(labels1);
if (alt_stm) {
alt_stm->find_contained_labels(labels2);
}
size_t i;
for (i=0; i<labels1.size(); i++) {
string l = labels1[i];
if (is_label_used(l)) {
if (std::find(labels2.begin(), labels2.end(), l) == labels2.end()) {
labels.push_back(l);
}
}
}
return labels.size();
}
void
Reducer::output_block_skeleton(const Block* blk, vector<const Block*>& work_blks, vector<const Function*>& work_funcs, std::ostream& out)
{
size_t i, j;
vector<const Block*> blks;
vector<const FunctionInvocationUser*> funcalls;
vector<const Function*> uniq_funcs;
out << "****** block: " << blk->stm_id << " ******" << endl;
for (i=0; i<blk->stms.size(); i++) {
const Statement* stm = blk->stms[i];
funcalls.clear();
uniq_funcs.clear();
stm->get_called_funcs(funcalls);
stm->get_blocks(blks);
out << stm->stm_id;
if (funcalls.size() > 0) {
// find the unique functions that was called
for (j=0; j<funcalls.size(); j++) {
const Function* f = funcalls[j]->get_func();
if (find_function_in_set(uniq_funcs, f) == -1) {
uniq_funcs.push_back(f);
}
}
// print the unique functions, add them to work list if necessary
out << "(";
for (j=0; j<uniq_funcs.size(); j++) {
if (j > 0) {
out << ", ";
}
const Function* f = uniq_funcs[j];
if (find_function_in_set(work_funcs, f) == -1) {
work_funcs.push_back(f);
}
out << f->name;
}
out << ")";
}
if (blks.size() > 0) {
out << "{";
for (j=0; j<blks.size(); j++) {
if (j > 0) {
out << ", ";
}
work_blks.push_back(blks[j]);
out << blks[j]->stm_id;
}
out << "}";
}
out << endl;
}
}
bool
Reducer::is_blk_deleted(const Block* b) const
{
// if active_blks is not initialized, we assume all functions are to be kept
if (map_active_blks.size() == 0) return false;
return map_active_blks.find(b) == map_active_blks.end();
}
bool
Reducer::is_stm_deleted(const Statement* stm) const
{
const Statement* s = stm;
while (s && replaced_stms.find(s) == replaced_stms.end() && !is_blk_deleted(s->parent)) {
s = s->find_container_stm();
}
// s = NULL means the statement is not reduced
return !(s == NULL);
}
bool
Reducer::is_param_dropped(const Function* f, int i)
{
// assume all parameters are not dropped during configuration
if (!drop_params || !configured) return false;
return !is_var_used(f->param[i]);
}
void
Reducer::replace_stm(const Statement* stm, const Statement* new_stm, string pre_stm)
{
replaced_stms[stm] = new_stm;
if (!pre_stm.empty()) {
map_pre_stm_assigns[stm] = pre_stm;
map_str_effects[pre_stm] = find_used_vars(pre_stm);
}
}
void
Reducer::delete_stms_after(const Statement* stm, bool include_parent_blks)
{
const Block* parent = stm->parent;
size_t i, j;
bool begin_delete = false;
const Statement* return_stm = stm->func->body->get_last_stm();
assert(return_stm);
for (i=0; i<parent->stms.size(); i++) {
const Statement* s = parent->stms[i];
vector<string> labels;
if (begin_delete) {
if (find_required_labels(s, labels)==0 && s != return_stm) {
// don't delete statements that contains add-back blocks (those has 0 count in mao_active_blks)
vector<const Block*> blks;
s->get_blocks(blks);
bool must_dump = false;
for (j=0; j<blks.size(); j++) {
if (map_active_blks[blks[j]] == 0) {
must_dump = true;
break;
}
}
if (!must_dump) {
replace_stm(s, NULL, "");
}
}
}
if (s == stm) {
begin_delete = true;
}
}
if (include_parent_blks) {
const Statement* s = stm->find_container_stm();
if (s) {
delete_stms_after(s, include_parent_blks);
}
}
}
// find local variables that have to be lifted to global, most likely because
// they are used via pointer in a callee function at the bottom of a call chain
int
Reducer::find_local_vars_to_lift(vector<const Variable*>& vars)
{
size_t i;
vars.clear();
if (monitored_func) {
for (i=0; i<used_vars.size(); i++) {
const Variable* v = used_vars[i];
if (monitored_func->feffect.is_read(v) || monitored_func->feffect.is_written(v)) {
// if there is a variable that is read by the function externally,
// but is neither a global or a parameter, this must be a local variable
// of one of the functions in the call chain
// Note this can not handle parameters not belong to monitored function,
// so taking address of parameters and passing into a function call is forbidden
// see ExpressionVariable::make_random
if (!v->is_global() && !v->is_argument()) {
vars.push_back(v);
break;
}
}
}
}
return vars.size();
}
void
Reducer::expand_used_vars(void)
{
size_t i;
for (i=0; i<used_vars.size(); i++) {
const Variable* v = used_vars[i];
const Expression* init = v->init;
if (init && init->term_type == eVariable) {
const ExpressionVariable* ev = (const ExpressionVariable*)(init);
if (ev->get_indirect_level() < 0) {
string dummy;
const Variable* addr_var = ev->get_var()->get_array(dummy);
// if not an array ...
if (addr_var == 0) {
addr_var = ev->get_var();
}
addr_var = addr_var->get_named_var();
if (!is_var_used(addr_var)) {
bool found = false;
// if find no appropriate variable in used list, use the original variable
if (!found) {
used_vars.push_back(addr_var);
}
}
}
}
}
}
bool
Reducer::is_replaced_var(const ExpressionVariable* ev, string& str_out)
{
if (map_reduced_vars.find(ev) != map_reduced_vars.end()) {
str_out = map_reduced_vars[ev];
return true;
}
return false;
}
const Expression*
Reducer::get_replaced_invocation(const FunctionInvocation* fi)
{
if (map_reduced_invocations.find(fi) != map_reduced_invocations.end()) {
return map_reduced_invocations[fi];
}
return NULL;
}
void
Reducer::get_used_vars_and_funcs_and_labels(const FunctionInvocation* fi, vector<const Variable*>& vars, vector<const Function*>& funcs, vector<string>& labels)
{
const Expression* replace = get_replaced_invocation(fi);
bool must_visit = std::find(must_use_var_invocations.begin(), must_use_var_invocations.end(), fi) != must_use_var_invocations.end();
if (replace != NULL && !must_visit) {
get_used_vars_and_funcs_and_labels(replace, vars, funcs, labels);
return;
}
size_t i;
if (fi->invoke_type == eFuncCall) {
const FunctionInvocationUser* call = (const FunctionInvocationUser*)(fi);
if (find_function_in_set(funcs, call->get_func()) == -1) {
// special case: if the body of function is never entered, means this
// function is never invoked, mostly due to a && or || shortcut, we
// replace the function call with a constant 0. In fact it's not evaluated
// anyway, so which constant we use doesn't matter
if (is_blk_deleted(call->get_func()->body)) {
map_reduced_invocations[fi] = new Constant(&fi->get_type(), "0");
return;
}
// recursively find used vars and functions
const Function* f = call->get_func();
get_used_vars_and_funcs_and_labels(f->body, vars, funcs, labels);
funcs.push_back(f);
// remember the parameters we dropped
for (i=0; i<f->param.size(); i++) {
if (is_param_dropped(f, i)) {
dropped_params.push_back(f->param[i]);
}
}
}
// only visit the parameters that are not dropped
for (i=0; i<fi->param_value.size(); i++) {
if (!is_param_dropped(call->get_func(), i)) {
get_used_vars_and_funcs_and_labels(fi->param_value[i], vars, funcs, labels);
}
}
}
else {
for (i=0; i<fi->param_value.size(); i++) {
get_used_vars_and_funcs_and_labels(fi->param_value[i], vars, funcs, labels);
}
}
}
void
Reducer::get_used_vars_and_funcs_and_labels(const Expression* e, vector<const Variable*>& vars, vector<const Function*>& funcs, vector<string>& labels)
{
string tmp;
switch (e->term_type) {
case eLhs: {
const Lhs* lhs = (const Lhs*)e;
add_variable_to_set(vars, lhs->get_var()->get_named_var());
break;
}
case eConstant: {
const Constant* cst = (const Constant*)e;
const Variable* v = find_addressed_var(cst->get_value());
if (v) {
add_variable_to_set(vars, v);
}
break;
}
// check if the variable is replaced by a constant
case eVariable: {
const ExpressionVariable* ev = (const ExpressionVariable*)e;
if (!is_replaced_var(ev, tmp) && !ev->get_var()->is_tmp_var()) {
add_variable_to_set(vars, ev->get_var()->get_named_var());
}
break;
}
// check if invocation is replaced by a variable or constant
case eFunction: {
const ExpressionFuncall* funcall = (const ExpressionFuncall*)e;
get_used_vars_and_funcs_and_labels(funcall->get_invoke(), vars, funcs, labels);
break;
}
default: break;
}
}
void
Reducer::find_called_funcs(const FunctionInvocation* fi, string id, vector<const FunctionInvocationUser*>& funcs, vector<string>& ids)
{
// analyze reduced expression instead if there is one
if (map_reduced_invocations.find(fi) != map_reduced_invocations.end()) {
const Expression* e = map_reduced_invocations[fi];
const FunctionInvocation* invoke = e->get_invoke();
if (invoke) {
find_called_funcs(invoke, id, funcs, ids);
}
return;
}
const FunctionInvocationUser* func_call = NULL;
if (fi->invoke_type == eFuncCall) {
func_call = (const FunctionInvocationUser*)fi;
funcs.push_back(func_call);
ids.push_back(id);
}
// find calls in parameters, skip dropped parameters
for (size_t i=0; i<fi->param_value.size(); i++) {
const Expression* value = fi->param_value[i];
if (func_call==NULL || !is_param_dropped(func_call->get_func(), i)) {
const FunctionInvocation* fi = value->get_invoke();
if (fi) {
find_called_funcs(fi, id + "_" + StringUtils::int2str(i), funcs, ids);
}
}
}
}
int
Reducer::reduce_const_binary_op(const FunctionInvocationBinary* fib)
{
const Expression* op1 = fib->param_value[0];
const Expression* op2 = fib->param_value[1];
if (op1->get_invoke() != NULL && map_reduced_invocations.find(op1->get_invoke()) != map_reduced_invocations.end()) {
op1 = map_reduced_invocations[op1->get_invoke()];
}
if (op2->get_invoke() != NULL && map_reduced_invocations.find(op2->get_invoke()) != map_reduced_invocations.end()) {
op2 = map_reduced_invocations[op2->get_invoke()];
}
if (op1->term_type == eConstant || map_reduced_vars.find(op1) != map_reduced_vars.end()) {
string str1 = (op1->term_type == eConstant) ? ((const Constant*)op1)->get_value() : map_reduced_vars[op1];
if (op2->term_type == eConstant || map_reduced_vars.find(op2) != map_reduced_vars.end()) {
string str2 = (op2->term_type == eConstant) ? ((const Constant*)op2)->get_value() : map_reduced_vars[op2];
INT64 result = 0;
INT64 v1 = StringUtils::str2longlong(str1);
INT64 v2 = StringUtils::str2longlong(str2);
switch (fib->get_operation()) {
case eAdd: result = v1 + v2; break;
case eSub: result = v1 - v2; break;
case eMul: result = v1 * v2; break;
case eDiv: result = v2 ? v1 / v2 : v1; break;
case eMod: result = v2 ? v1 % v2 : v1; break;
case eCmpGt: result = v1 > v2; break;
case eCmpLt: result = v1 < v2; break;
case eCmpGe: result = v1 >= v2; break;
case eCmpLe: result = v1 <= v2; break;
case eCmpEq: result = v1 == v2; break;
case eCmpNe: result = v1 != v2; break;
case eAnd: result = v1 && v2; break;
case eOr: result = v1 || v2; break;
case eBitXor: result = v1 ^ v2; break;
case eBitAnd: result = v1 & v2; break;
case eBitOr: result = v1 | v2; break;
case eRShift: result = (v2 > 0) ? v1 >> v2 : v1; break;
case eLShift: result = (v2 > 0) ? v1 << v2 : v1; break;
}
Constant* cst = new Constant(&fib->get_type(), StringUtils::longlong2str(result));
map_reduced_invocations[fib] = cst;
return 1;
}
}
return 0;
}
void
Reducer::reduce_const_binary_ops(vector<const FunctionInvocationBinary*>& ops)
{
size_t i;
size_t len = ops.size();
for (i=0; i<len; i++) {
const FunctionInvocationBinary* fi = ops[i];
if (reduce_const_binary_op(fi)) {
ops.erase(ops.begin() + i);
i--;
len--;
}
}
}
int
Reducer::find_binary_operations(const Statement* stm, vector<const FunctionInvocationBinary*>& ops, vector<int>& ids, bool no_ptr_cmp)
{
int cnt = 0;
vector<const Expression*> exprs;
stm->get_exprs(exprs);
for (size_t i=0; i<exprs.size(); i++) {
cnt += find_binary_operations(exprs[i], ops, ids, no_ptr_cmp);
}
return cnt;
}
int
Reducer::find_binary_operations(const Expression* exp, vector<const FunctionInvocationBinary*>& ops, vector<int>& ids, bool no_ptr_cmp)
{
const FunctionInvocation* fi = exp->get_invoke();
if (fi) {
// analyze reduced expression instead if there is one
if (map_reduced_invocations.find(fi) != map_reduced_invocations.end()) {
return find_binary_operations(map_reduced_invocations[fi], ops, ids, no_ptr_cmp);
}
if (fi->invoke_type == eBinaryPrim) {
const FunctionInvocationBinary* fib = (const FunctionInvocationBinary*)fi;
if (!reduce_const_binary_op(fib)) {
if (!no_ptr_cmp || !fib->ptr_cmp) {
ops.push_back(fib);
ids.push_back(exp->expr_id);
}
}
}
const FunctionInvocationUser* func_call = NULL;
if (fi->invoke_type == eFuncCall) {
func_call = (const FunctionInvocationUser*)fi;
}
// find binary operations in parameters, skip dropped parameters
for (size_t i=0; i<fi->param_value.size(); i++) {
const Expression* param = fi->param_value[i];
if (func_call==NULL || !is_param_dropped(func_call->get_func(), i)) {
find_binary_operations(param, ops, ids, no_ptr_cmp);
}
}
}
return ops.size();
}
const FunctionInvocation*
Reducer::find_invoke_by_eid(const Statement* s, int id) const
{
size_t i;
vector<const Expression*> exprs;
s->get_exprs(exprs);
for (i=0; i<exprs.size(); i++) {
const FunctionInvocation* fi = find_invoke_by_eid(exprs[i], id);
if (fi) {
return fi;
}
}
return NULL;
}
const FunctionInvocation*
Reducer::find_invoke_by_eid(const Expression* e, int id) const
{
const FunctionInvocation* fi = e->get_invoke();
if (e->expr_id == id) {
return fi;
}
if (fi) {
for (size_t i=0; i<fi->param_value.size(); i++) {
const Expression* param = fi->param_value[i];
const FunctionInvocation* tmp = find_invoke_by_eid(param, id);
if (tmp) {
return tmp;
}
}
}
return NULL;
}
void
Reducer::build_left_right_binary_trees(vector<const FunctionInvocationBinary*>& ops, vector<intvec>& left_trees, vector<intvec>& right_trees)
{
size_t i, j, k;
vector<const FunctionInvocationBinary*> sub_ops;
vector<int> dummy, covered;
left_trees.clear();
right_trees.clear();
for (i=0; i<ops.size(); i++) {
const FunctionInvocationBinary* op = ops[i];
sub_ops.clear();
covered.clear();
find_binary_operations(op->param_value[0], sub_ops, dummy, true);
for (j=0; j<sub_ops.size(); j++) {
int index = -1;
for (k=0; k<ops.size(); k++) {
if (ops[k] == sub_ops[j]) {
index = k;
covered.push_back(index);
break;
}
}
}
left_trees.push_back(covered);
sub_ops.clear();
covered.clear();
find_binary_operations(op->param_value[1], sub_ops, dummy, true);
for (j=0; j<sub_ops.size(); j++) {
int index = -1;
for (k=0; k<ops.size(); k++) {
if (ops[k] == sub_ops[j]) {
index = k;
covered.push_back(index);
break;
}
}
}
right_trees.push_back(covered);
}
}
/*
* find all the variables and functions used in this statement
*/
void
Reducer::get_used_vars_and_funcs_and_labels(const Statement* stm, vector<const Variable*>& vars, vector<const Function*>& funcs, vector<string>& labels)
{
if (stm == NULL) return;
// for if statements with both branches deleted
if (stm->eType == eIfElse) {
const StatementIf* si = (const StatementIf*)stm;
if (is_blk_deleted(si->get_true_branch()) && is_blk_deleted(si->get_false_branch())) {
replace_stm(si, NULL, "");
delete_stms_after(si, false);
}
}
// for goto statement whose target is deleted
else if (stm->eType == eGoto) {
const StatementGoto* sg = (const StatementGoto*)stm;
if (is_blk_deleted(sg->dest->parent)) {
bool keep = false;
// special case: if goto jump into a for-loop that use the monitored variable as
// induction variable, we keep the jump, for seed 355297830
const Statement* s = sg->dest->find_container_stm();
if (s && s->eType == eFor) {
const StatementFor* sf = (const StatementFor*)s;
if (sf->get_init()->get_lhs()->get_var() == this->monitored_var) {
keep = true;
if (std::find(labels.begin(), labels.end(), sg->label) == labels.end()) {
labels.push_back(sg->label);
}
}
}
if (!keep) {
replace_stm(sg, NULL, "");
}
} else {
if (std::find(labels.begin(), labels.end(), sg->label) == labels.end()) {
labels.push_back(sg->label);
}
}
}
// include used variables from pre-statement assignments
if (map_pre_stm_assigns.find(stm) != map_pre_stm_assigns.end()) {
const string& assigns = map_pre_stm_assigns[stm];
add_variables_to_set(vars, map_str_effects[assigns]);
}
// used replacement for replaced statements
bool must_visit = std::find(must_use_var_stms.begin(), must_use_var_stms.end(), stm) != must_use_var_stms.end();
if (!must_visit && replaced_stms.find(stm) != replaced_stms.end()) {
get_used_vars_and_funcs_and_labels(replaced_stms[stm], vars, funcs, labels);
return;
}
string label = stm->find_jump_label();
if (!label.empty()) {
labels.push_back(label);
}
size_t i, j;
//stm->Output(cout);
vector<const Expression*> exps;
stm->get_exprs(exps);
for (i=0; i<exps.size(); i++) {
get_used_vars_and_funcs_and_labels(exps[i], vars, funcs, labels);
}
if (stm->eType == eArrayOp) {
const StatementArrayOp* sa = (const StatementArrayOp*)stm;
for (i=0; i<sa->ctrl_vars.size(); i++) {
add_variable_to_set(vars, sa->ctrl_vars[i]->get_named_var());
}
// JYTODO: detect the case when initializer is not invoked
if (sa->init_value) {
add_variable_to_set(vars, sa->array_var->get_named_var());
}
}
vector<const Block*> blks;
stm->get_blocks(blks);
for (i=0; i<blks.size(); i++) {
if (is_blk_deleted(blks[i])) {
continue;
}
for (j=0; j<blks[i]->stms.size(); j++) {
const Statement* s = blks[i]->stms[j];
get_used_vars_and_funcs_and_labels(s, vars, funcs, labels);
}
}
}
void
Reducer::configure_diff_active_blks(string line, int first_bid)
{
vector<int> ids;
StringUtils::split_int_string(line, ids, ",()");
size_t i;
assert(!ids.empty());
const Block* one_branch = find_block_by_id(first_bid);
assert(one_branch);
const Statement* stm = one_branch->find_container_stm();
assert(stm && stm->eType == eIfElse);
for (i=0; i<ids.size(); i+=2) {
int id = ids[i];
if (1) { //id <= first_bid) {
int cnt = ids[i+1];
const Block* blk = find_block_by_id(id);
assert(blk);
map_active_blks[blk] = cnt;
}
}
const StatementIf* si = (const StatementIf*)stm;
const Block* other_branch = (one_branch == si->get_false_branch()) ? si->get_true_branch() : si->get_false_branch();
map_active_blks[other_branch] = map_active_blks[one_branch];
assert(!used_vars.empty());
// replace both branches with direct conflicting assignments
string vname = monitored_var->name;
string assign1 = vname + " = 0;";
string assign2 = vname + " = 1;";
replace_stm(one_branch, NULL, assign1);
replace_stm(other_branch, NULL, assign2);
// delete all statement following this if-else statement
delete_stms_after(si, true);
}
void
Reducer::config_active_blks(string cmd)
{
size_t i;
vector<string> tmp_strs;
if (cmd == "poll") {
dump_block_entry = true;
} else if (cmd == "blind poll") {
dump_all_block_info = true;
}
else {
tmp_strs.clear();
if (cmd.find("||") != string::npos) {
StringUtils::split_string(cmd, tmp_strs, "|");
assert(tmp_strs.size() == 2);
cmd = tmp_strs[0];
int blkid = StringUtils::str2int(tmp_strs[1]);
configure_diff_active_blks(cmd, blkid);
}
else {
StringUtils::split_string(cmd, tmp_strs, ",()");
for (i=0; i<tmp_strs.size(); i+=2) {
const Block* blk = find_block_by_id(StringUtils::str2int(tmp_strs[i]));
assert(i+1 < tmp_strs.size());
int cnt = StringUtils::str2int(tmp_strs[i+1]);
assert(blk);
map_active_blks[blk] = cnt;
// for rollback blocks, find the parant blocks and put them on active block list as well
if (cnt == 0) {
for (const Block* b = blk->parent; b != NULL; b = b->parent) {
if (map_active_blks.find(b) != map_active_blks.end()) {
break;
}
map_active_blks[b] = 0;
}
}
}
}
}
}
void
Reducer::config_call_chain_shortcut(string cmd)
{
if (cmd == "poll") {
dump_monitored_var = true;
}
else {
vector<string> strs;
StringUtils::split_string(cmd, strs, ":");
assert(strs.size() == 2 || strs.size() == 3);
if (strs.size() == 2) {
dump_monitored_var = true;
monitored_func = find_function_by_name(strs[0]);
assert(monitored_func);
monitored_call_id = strs[1];
}
else {
main = find_function_by_name(strs[0]);
assert(main);
main_str = strs[2];
}
}
}
void
Reducer::config_stm_reduction(string cmd)
{
size_t i, j;
if (cmd.find(":") == string::npos) {
size_t pos = cmd.find(" blind");
if (pos != string::npos) {
cmd = cmd.substr(0, pos);