-
Notifications
You must be signed in to change notification settings - Fork 1
/
prompt_diff.patch
2592 lines (2484 loc) · 99.4 KB
/
prompt_diff.patch
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
diff --git a/include/klee/Constraints.h b/include/klee/Constraints.h
index 87069f8..eb35d03 100755
--- a/include/klee/Constraints.h
+++ b/include/klee/Constraints.h
@@ -44,6 +44,8 @@ public:
ref<Expr> simplifyExpr(ref<Expr> e) const;
void addConstraint(ref<Expr> e);
+ //muqi
+ void dump_constraints();
bool empty() const {
return constraints.empty();
diff --git a/include/klee/Expr.h b/include/klee/Expr.h
index 212053b..ac0c534 100755
--- a/include/klee/Expr.h
+++ b/include/klee/Expr.h
@@ -24,6 +24,10 @@
#include <vector>
#include <map>
+#include <string>
+#include <mutex>
+
+
namespace llvm {
class Type;
class raw_ostream;
@@ -38,6 +42,9 @@ class ObjectState;
template<class T> class ref;
+//muqi
+extern std::string klee_muqi_compare_file_path;
+extern std::mutex mtx;
/// Class representing symbolic expressions.
/**
@@ -1001,9 +1008,11 @@ public:
private:
llvm::APInt value;
- ConstantExpr(const llvm::APInt &v) : value(v) {}
+ //ConstantExpr(const llvm::APInt &v) : value(v) {}
public:
+ //muqi
+ ConstantExpr(const llvm::APInt &v) : value(v) {}
~ConstantExpr() {}
Width getWidth() const { return value.getBitWidth(); }
diff --git a/include/klee/Internal/Support/ErrorHandling.h b/include/klee/Internal/Support/ErrorHandling.h
index 2945169..3d8588a 100755
--- a/include/klee/Internal/Support/ErrorHandling.h
+++ b/include/klee/Internal/Support/ErrorHandling.h
@@ -16,6 +16,17 @@
#endif
#endif
+//muqi
+#include "klee/Internal/Support/ErrorHandling.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/IR/Instruction.h"
+#include "llvm/IR/Function.h"
+#include "llvm/ADT/Twine.h"
+#include "llvm/Support/raw_ostream.h"
+
+
#include <stdio.h>
namespace klee {
@@ -23,6 +34,11 @@ namespace klee {
extern FILE *klee_warning_file;
extern FILE *klee_message_file;
+//muqi
+extern FILE *klee_muqi_file;
+extern FILE *klee_muqi_compare_file;
+extern FILE *klee_muqi_compare_file_test;
+
/// Print "KLEE: ERROR: " followed by the msg in printf format and a
/// newline on stderr and to warnings.txt, then exit with an error.
void klee_error(const char *msg, ...)
@@ -32,6 +48,15 @@ void klee_error(const char *msg, ...)
/// newline on stderr and to messages.txt.
void klee_message(const char *msg, ...) __attribute__((format(printf, 1, 2)));
+
+//muqi
+void klee_muqi(const char *msg, ...) __attribute__((format(printf, 1, 2)));
+
+void klee_muqi_compare(const char *msg, ...) __attribute__((format(printf, 1, 2)));
+
+void klee_muqi_compare_test(const char *function_name, const char *msg, ...);
+
+
/// Print "KLEE: " followed by the msg in printf format and a
/// newline to messages.txt.
void klee_message_to_file(const char *msg, ...)
diff --git a/lib/Core/AddressSpace.cpp b/lib/Core/AddressSpace.cpp
index 62f1fa7..2efced4 100755
--- a/lib/Core/AddressSpace.cpp
+++ b/lib/Core/AddressSpace.cpp
@@ -12,6 +12,8 @@
#include "Memory.h"
#include "TimingSolver.h"
+#include "klee/Internal/Support/ErrorHandling.h"
+
#include "klee/Expr.h"
#include "klee/TimerStatIncrementer.h"
diff --git a/lib/Core/ExecutionState.cpp b/lib/Core/ExecutionState.cpp
index c48e1a5..50a0c8f 100755
--- a/lib/Core/ExecutionState.cpp
+++ b/lib/Core/ExecutionState.cpp
@@ -22,6 +22,10 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
+//muqi
+#include "llvm/ADT/APInt.h"
+#include "klee/Internal/Support/ErrorHandling.h"
+
#include <iomanip>
#include <sstream>
#include <cassert>
@@ -244,6 +248,24 @@ ExecutionState::~ExecutionState() {
cur_mergehandler->removeOpenState(this);
}
+ //muqi
+ klee_muqi("Inside ExecutionState::~ExecutionState() \n");
+ int temp = 0;
+ klee_muqi("the address of stack will be poped are: \n");
+ for (std::vector<StackFrame>::iterator it = stack.begin() ; it != stack.end(); ++it){
+ //(*it).locals->value.get()->dump();
+ for (std::vector<const MemoryObject *>::iterator temp_it = (*it).allocas.begin(); temp_it != (*it).allocas.end(); ++ temp_it){
+ auto temp_address = reinterpret_cast<std::uint8_t*>((*temp_it)->address);
+ llvm::APInt outputtemp1 = llvm::APInt(Context::get().getPointerWidth(), (*temp_it)->address);
+ ConstantExpr *output = new ConstantExpr(outputtemp1);
+ //output->dump();
+ klee_muqi("address of mo memoryobject is %u\n",temp_address);
+ }
+ temp ++;
+ }
+ klee_muqi("the number of stackframe will be poped is %d\n",temp);
+ klee_muqi("double check by stack.size, the number of stackframe is %d", stack.size());
+ klee_muqi("\n End of ExecutionState::~ExecutionState() printing \n");
while (!stack.empty()) popFrame();
}
@@ -834,8 +856,9 @@ void ExecutionState::updateLCMState() {
}
else if (seq->getType() == identifier) {
if (curr != 0 ) {
- Identifier *id = dynamic_cast<Identifier*>(seq);
- assert(id);
+ // Identifier *id = dynamic_cast<Identifier*>(seq);
+ Identifier *id = static_cast<Identifier*>(seq);
+ assert(id);
llvm::outs() << " next step " << id->getValue() << " to execute " << "\n";
Function *func = moduleHandle->getFunction(id->getValue());
//initiateAsync(func);
diff --git a/lib/Core/Executor.cpp b/lib/Core/Executor.cpp
index 54e659c..50a8c63 100755
--- a/lib/Core/Executor.cpp
+++ b/lib/Core/Executor.cpp
@@ -82,6 +82,17 @@
#include "klee/Internal/Support/CompressionStream.h"
#endif
+//muqi
+#include "llvm/ADT/APInt.h"
+#include "llvm/IR/Metadata.h"
+#include "llvm/IR/DebugInfoMetadata.h"
+#ifdef ENABLE_Z3
+#include "./Z3Builder.h"
+#endif
+#define Muqi_DEBUG;
+#define Muqi_external_call;
+#define Muqi_out_of_bound_pointer;
+
#include <cassert>
#include <algorithm>
#include <iomanip>
@@ -1806,7 +1817,13 @@ bool isAllocTypeLazyInit(Type *t, bool &single, int &count) {
}
}
if (t->getPrimitiveSizeInBits()) {
- count = primArraySize;
+ //count = primArraySize;
+
+ //muqi 256&8 we want the size of array be 256 bytes
+ count == 256*8/t->getPrimitiveSizeInBits();
+ if (count == 0)
+ count = 1;
+
single = false;
return true;
}
@@ -1849,7 +1866,14 @@ void collectEmbeddedPointerTypes(Type *t, std::vector<std::string> &lazyTypes, s
else if (t->getPrimitiveSizeInBits()) {
if (pointerType) {
lazyTypes.push_back(rso.str());
- lazyInitNumInstances[rso.str()] = primArraySize;
+ //lazyInitNumInstances[rso.str()] = primArraySize;
+
+ //muqi 256&8 we want the size of array be 256 bytes
+ int temp_count = 256*8/t->getPrimitiveSizeInBits();
+ if (temp_count == 0)
+ temp_count = 1;
+
+ lazyInitNumInstances[rso.str()] = temp_count;
}
}
@@ -2590,9 +2614,14 @@ void Executor::branch(ExecutionState &state,
addConstraint(*result[i], conditions[i]);
}
+//muqi
Executor::StatePair
Executor::fork(ExecutionState ¤t, ref<Expr> condition, bool isInternal) {
+ KInstruction *currentInstruction = current.prevPC;
+ Instruction *i = currentInstruction->inst;
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Executor::StatePair Executor::fork");
+ klee_muqi("in the fork of Executor::\n");
Solver::Validity res;
std::map< ExecutionState*, std::vector<SeedInfo> >::iterator it =
@@ -2621,6 +2650,10 @@ Executor::fork(ExecutionState ¤t, ref<Expr> condition, bool isInternal) {
bool success = solver->getValue(current, condition, value);
assert(success && "FIXME: Unhandled solver failure");
(void) success;
+ //start of symbolic execution 151515
+ klee_muqi("in if condition of !isSeeding && !isa<ConstantExpr>(condition) && \n");
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nIn if condition of !isSeeding && !isa<ConstantExpr>(condition) &&");
+
addConstraint(current, EqExpr::create(value, condition));
current.lastConstraint = EqExpr::create(value, condition);
condition = value;
@@ -2653,10 +2686,14 @@ Executor::fork(ExecutionState ¤t, ref<Expr> condition, bool isInternal) {
// add constraints
if(branch) {
res = Solver::True;
+ klee_muqi("in if condition of isSeeding, branch is true \n");
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside fork, First branch");
addConstraint(current, condition);
current.lastConstraint = condition;
} else {
res = Solver::False;
+ klee_muqi("in if condition of isSeeding, branch is false \n");
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside fork, Second branch");
addConstraint(current, Expr::createIsZero(condition));
current.lastConstraint = Expr::createIsZero(condition);
}
@@ -2680,10 +2717,16 @@ Executor::fork(ExecutionState ¤t, ref<Expr> condition, bool isInternal) {
TimerStatIncrementer timer(stats::forkTime);
if (theRNG.getBool()) {
+ klee_muqi("in if condition of not isSeeding, branch is true \n");
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside fork, First branch");
+
addConstraint(current, condition);
res = Solver::True;
current.lastConstraint = condition;
} else {
+ klee_muqi("in if condition of not isSeeding, branch is false \n");
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside fork, Second branch");
+
addConstraint(current, Expr::createIsZero(condition));
res = Solver::False;
current.lastConstraint = Expr::createIsZero(condition);
@@ -2718,6 +2761,9 @@ Executor::fork(ExecutionState ¤t, ref<Expr> condition, bool isInternal) {
assert(trueSeed || falseSeed);
res = trueSeed ? Solver::True : Solver::False;
+ klee_muqi("in Fix branch in only-replay-seed mode, if we don't have both true \n");
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside fork, Fix branch in only-replay-seed mode");
+
addConstraint(current, trueSeed ? condition : Expr::createIsZero(condition));
if (trueSeed)
current.lastConstraint = condition;
@@ -2816,8 +2862,15 @@ Executor::fork(ExecutionState ¤t, ref<Expr> condition, bool isInternal) {
}
}
+ klee_muqi("Add constraint for trueStatement final\n");
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nAdd constraint for trueStatement ");
+
addConstraint(*trueState, condition);
trueState->lastConstraint = condition;
+
+ klee_muqi("Add constraint for falseStatement final\n");
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nAdd constraint for falseStatement ");
+
addConstraint(*falseState, Expr::createIsZero(condition));
falseState->lastConstraint = Expr::createIsZero(condition);
// Kinda gross, do we even really still want this option?
@@ -3128,9 +3181,11 @@ void Executor::executeCall(ExecutionState &state,
klee_error("unknown intrinsic: %s", f->getName().data());
}
- if (InvokeInst *ii = dyn_cast<InvokeInst>(i))
+ if (InvokeInst *ii = dyn_cast<InvokeInst>(i)){
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nfunction declared, executeCall named: %s, transferToBasicBlock ",f->getName().data());
transferToBasicBlock(ii->getNormalDest(), i->getParent(), state);
successorsPaths->insert(&state);
+ }
} else {
/* SYSREL extension */
@@ -3174,9 +3229,14 @@ void Executor::executeCall(ExecutionState &state,
// instead of the actual instruction, since we can't make a KInstIterator
// from just an instruction (unlike LLVM).
KFunction *kf = kmodule->functionMap[f];
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nfunction not declared, executeCall named: %s, state.pc ++ ", f->getName().data());
+ klee_muqi("Inside Executor::executeCall, state.pc before is %d", state.pc);
+
state.pushFrame(state.prevPC, kf);
state.pc = kf->instructions;
-
+
+ klee_muqi("Inside Executor::executeCall, state.pc after is %d", state.pc);
+
if (recordStackTraces)
recordStackTrace(state);
@@ -3384,11 +3444,96 @@ static inline const llvm::fltSemantics * fpWidthToSemantics(unsigned width) {
}
}
+//muqi
+void muqi_transfertoblock_info(BasicBlock *BB_src,BasicBlock *BB_dest,Instruction *inst_src, Instruction *inst_dest){
+ //muqi: add for identifing the block will be executed
+ Function *Func;
+ if (BB_src){
+ Func = BB_src->getFirstNonPHI()->getFunction();
+ int temp_BB = 0;
+ int BB_src_num = -1;
+ int BB_dest_num = -1;
+ for (Function::iterator BB_temp = Func->begin(), E = Func->end(); BB_temp != E; ++BB_temp) {
+ if (BB_src == (&*BB_temp)){
+ BB_src_num = temp_BB;
+ }
+ if (BB_dest == (&*BB_temp)){
+ BB_dest_num = temp_BB;
+ }
+ temp_BB ++;
+ }
+ klee_muqi_compare_test(Func->getName().data(),"\nTransferTobasic (used for lifter.ll), BB from %d to %d ", BB_src_num, BB_dest_num );
+ //muqi: add end
+ //muqi: have the metadata of line number
+ DILocation *Loc;
+ unsigned Line_src = 2147483647;
+ unsigned Line_des = 2147483647;
+ //find the line info for source of BB
+ if (Loc = BB_src->getFirstNonPHI()->getDebugLoc()) {
+ Line_src = Loc->getLine();
+ }
+ //if first line of BB has some errors, find the instruction line being executed.
+ if (Line_src == 2147483647 && inst_src) {
+ if (Loc = inst_src->getDebugLoc()) {
+ Line_src = Loc->getLine();
+ }
+ }
+
+ //find the line info for destination of BB
+ if(BB_dest == NULL) {
+ Line_des = 2147483647;
+ }else{
+ if (Loc = BB_dest->getFirstNonPHI()->getDebugLoc()){
+ Line_des = Loc->getLine();
+ }
+ }
+
+ if (Line_des == 2147483647 && inst_dest) {
+ if (Loc = inst_dest->getDebugLoc()) {
+ Line_des = Loc->getLine();
+ }
+ }
+ klee_muqi_compare_test(Func->getName().data(),"\nTransferTobasic (used for generatedc.ll), Linenumber of instruction in c file from %d to %d ", Line_src, Line_des );
+ klee_muqi("\nTransferTobasic (used for lifter.ll), BB from %d to %d ", BB_src_num, BB_dest_num );
+ klee_muqi("\nTransferTobasic (used for generatedc.ll), Linenumber of instruction in c file from %d to %d ", Line_src, Line_des );
+ //muqi: add end
+ }else {
+ Func = NULL;
+ klee_muqi_compare_test("unknown","\nTransferTobasic (used for lifter.ll), BB from -1 to -1 ");
+ klee_muqi_compare_test("unknown","\nTransferTobasic (used for generatedc.ll), Linenumber of instruction in c file from 2147483647 to 2147483647 ");
+ klee_muqi("\nTransferTobasic (used for lifter.ll), BB from -1 to -1 ");
+ klee_muqi("\nTransferTobasic (used for generatedc.ll), Linenumber of instruction in c file from 2147483647 to 2147483647 ");
+
+ }
+
+
+
+}
+
+//muqi
+std::vector<Instruction *> vector_i;
+std::vector<ExecutionState *>vector_state;
+std::vector<KInstruction *>vector_ki;
+std::vector<ref<Expr> >vector_constantexpr;
+
+
+
void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
state.stackFrameBeforeExecInst = state.stack.size() - 1;
Instruction *i = ki->inst;
+
+ //muqi
+ std::string temp_out = i->getOpcodeName();
+ klee_muqi("current instruction for this instruction is %d", state.prevPC);
+ klee_muqi("next instruction for this instruction is %d", state.pc);
+ klee_muqi("current Function name is: %s", i->getFunction()->getName());
+
+ klee_muqi("Instruction is: %s; ", temp_out.c_str());
+ const InstructionInfo *temp_info = ki->info;
+ klee_muqi("corresponding assembly line is %d \n", temp_info->assemblyLine);
+
switch (i->getOpcode()) {
// Control flow
case Instruction::Ret: {
@@ -3402,7 +3547,7 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
/* SYSREL EXTENSION */
state.checkAndSetPMCallbackCompleted(ri->getParent()->getParent()->getName());
/* SYSREL EXTENSION */
-
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Instruction::Ret ");
llvm::outs() << "handling return for function " << ri->getParent()->getParent()->getName() << "\n";
if (!isVoidReturn) {
result = eval(ki, 0, state).value;
@@ -3424,6 +3569,12 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
generateValuedInstruction(state, ki);
if (state.stack.size() <= 1) {
+ muqi_transfertoblock_info(i->getParent(),NULL,i,NULL);
+ klee_muqi("\nInside Ret, state.stack.size() is 1\n");
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Ret state.pc ++");
+ klee_muqi("\nInside Ret, state.pc ++ , state.pc before is %d", state.pc);
+
+
assert(!caller && "caller set on initial stack frame");
/* SYSREL extension */
@@ -3503,6 +3654,7 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
}
/* SYSREL extension */
} else {
+ klee_muqi("\nInside Ret, state.stack.size() is more than one\n");
state.stackFrameBeforeExecInst = state.stack.size() - 1;
state.popFrame();
@@ -3511,13 +3663,26 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
if (InvokeInst *ii = dyn_cast<InvokeInst>(caller)) {
llvm::outs() << "returning to basic block of " << (*caller) << "\n";
+ //muqi
+ muqi_transfertoblock_info(caller->getParent(),ii->getNormalDest(),i,caller);
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Instruction::Ret transferToBasicBlock ");
+ klee_muqi("\nInside Instruction::Ret transferToBasicBlock ");
+
transferToBasicBlock(ii->getNormalDest(), caller->getParent(), state);
} else {
+ //muqi
+ muqi_transfertoblock_info(i->getParent(),caller->getParent(),i,caller);
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Ret state.pc ++");
+ klee_muqi("\nInside Ret, state.pc ++ , state.pc before is %d", state.pc);
+
state.pc = kcaller;
++state.pc;
+
+ klee_muqi("\nInside Ret, state.pc ++ , state.pc after is %d", state.pc);
+
llvm::outs() << "returning to " << (*state.pc->inst) << "\n";
}
-
+ klee_muqi("In ret, Function name is: %s", caller->getFunction()->getName());
if (!isVoidReturn) {
Type *t = caller->getType();
if (t != Type::getVoidTy(i->getContext())) {
@@ -3544,28 +3709,80 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
// We check that the return value has no users instead of
// checking the type, since C defaults to returning int for
// undeclared functions.
- /* SYSREL extension */
- llvm::errs() << "Warning: return void when caller expected a result\n";
+ /* SYSREL extension */
+ //muqi
+ result = ConstantExpr::alloc(0, Expr::Int64);
+ bindLocal(kcaller, state, result);
+ klee_warning("Injected fake return for function: %s",
+ ri->getFunction()->getName().str().c_str());
+
+ //llvm::errs() << "Warning: return void when caller expected a result\n";
/* SYSREL extension */
}
- }
+ }
+
+//muqi
+ if (result.isNull() == true ){
+ result = ConstantExpr::alloc(0, Expr::Int64);
+ }
+ result->dump();
+ #ifdef ENABLE_Z3
+ Z3Builder temp_build = Z3Builder(false,NULL);
+ //Z3ASTHandle result_z3 = temp_build.construct(result);
+ int *width = (int *)malloc(sizeof(int));
+ *width = 0;
+ Z3ASTHandle result_z3 = temp_build.construct_muqi(result,width);
+ llvm::errs()<<"after result dumped, *width is:" << *width<<"\n";
+ if (*width == 1){
+
+ *width = 64;
+ result = SelectExpr::create(result,ConstantExpr::create(1,64),ConstantExpr::create(0,64));
+ result_z3 = temp_build.construct_muqi(result,width);
+ }
+ while (*width < 64 && *width != 1){
+ //concat the result to 64bits
+ result = ConcatExpr::create(ConstantExpr::create(0,8),result);
+ result_z3 = temp_build.construct_muqi(result,width);
+ llvm::errs()<<"after result dumped, *width is:" << *width<<"\n";
+ }
+ result_z3.dump();
+
+ #endif
+
break;
}
case Instruction::Br: {
BranchInst *bi = cast<BranchInst>(i);
if (bi->isUnconditional()) {
+ //muqi
+ klee_muqi("br in Unconditional \n");
+ klee_muqi("br is %d \n", bi);
+ klee_muqi("br transfer from %d to %d\n", bi->getParent(), bi->getSuccessor(0));
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nbr in Unconditional ");
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Instruction::Br uncondition transferToBasicBlock ");
+ //muqi: add for identifing the block will be executed
+ muqi_transfertoblock_info(i->getParent(),bi->getSuccessor(0),i,NULL);
+
transferToBasicBlock(bi->getSuccessor(0), bi->getParent(), state);
successorsPaths->insert(&state);
} else {
// FIXME: Find a way that we don't have this hidden dependency.
+ klee_muqi("br in conditional \n");
+ klee_muqi("br is %d \n", bi);
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nbr in conditional ");
assert(bi->getCondition() == bi->getOperand(0) &&
"Wrong operand index!");
ref<Expr> cond = eval(ki, 0, state).value;
+
+ Expr *temp = cond.get();
+ // cond = optimizer.optimizeExpr(cond, false);
Executor::StatePair branches = fork(state, cond, false);
-
- if (branches.first)
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nFork finished");
+ klee_muqi("Fork finished\n");
+ if (branches.first){
branches.first->lastBranchInstConstraint = branches.first->lastConstraint;
-
+
+ }
if (branches.second)
branches.second->lastBranchInstConstraint = branches.second->lastConstraint;
@@ -3661,10 +3878,20 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
}
else {
if (branches.first) {
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Instruction::Br condition first transferToBasicBlock ");
+ klee_muqi("\nInside Instruction::Br condition first transferToBasicBlock ");
+ //muqi: add for identifing the block will be executed
+ muqi_transfertoblock_info(i->getParent(),bi->getSuccessor(0),i,NULL);
+
transferToBasicBlock(bi->getSuccessor(0), bi->getParent(), *branches.first);
successorsPaths->insert(branches.first);
}
if (branches.second) {
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Instruction::Br condition second transferToBasicBlock ");
+ klee_muqi("\nInside Instruction::Br condition second transferToBasicBlock ");
+ //muqi: add for identifing the block will be executed
+ muqi_transfertoblock_info(i->getParent(),bi->getSuccessor(1),i,NULL);
+
transferToBasicBlock(bi->getSuccessor(1), bi->getParent(), *branches.second);
successorsPaths->insert(branches.second);
}
@@ -3693,13 +3920,20 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
}
case Instruction::IndirectBr: {
// implements indirect branch to a label within the current function
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Instruction::IndirectBr");
+
const auto bi = cast<IndirectBrInst>(i);
auto address = eval(ki, 0, state).value;
address = toUnique(state, address);
// concrete address
if (const auto CE = dyn_cast<ConstantExpr>(address.get())) {
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Instruction::IndirectBr first transferToBasicBlock ");
+
const auto bb_address = (BasicBlock *) CE->getZExtValue(Context::get().getPointerWidth());
+
+ //muqi: add for identifing the block will be executed
+ muqi_transfertoblock_info(i->getParent(),bb_address,i,NULL);
transferToBasicBlock(bb_address, bi->getParent(), state);
successorsPaths->insert(&state);
break;
@@ -3759,6 +3993,10 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
assert(targets.size() == branches.size());
for (std::vector<ExecutionState *>::size_type k = 0; k < branches.size(); ++k) {
if (branches[k]) {
+
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Instruction::IndirectBr for loop transferToBasicBlock ");
+ //muqi: add for identifing the block will be executed
+ muqi_transfertoblock_info(i->getParent(),targets[k],i,NULL);
transferToBasicBlock(targets[k], bi->getParent(), *branches[k]);
successorsPaths->insert(branches[k]);
}
@@ -3767,6 +4005,8 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
break;
}
case Instruction::Switch: {
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Instruction::Switch");
+
SwitchInst *si = cast<SwitchInst>(i);
ref<Expr> cond = eval(ki, 0, state).value;
BasicBlock *bb = si->getParent();
@@ -3778,6 +4018,13 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
llvm::IntegerType *Ty = cast<IntegerType>(si->getCondition()->getType());
ConstantInt *ci = ConstantInt::get(Ty, CE->getZExtValue());
unsigned index = si->findCaseValue(ci).getSuccessorIndex();
+
+
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Instruction::Switch first transferToBasicBlock ");
+
+ //muqi: add for identifing the block will be executed
+ muqi_transfertoblock_info(si->getParent(),si->getSuccessor(index),i,NULL);
+
transferToBasicBlock(si->getSuccessor(index), si->getParent(), state);
successorsPaths->insert(&state);
} else {
@@ -3877,6 +4124,11 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
it != ie; ++it) {
ExecutionState *es = *bit;
if (es) {
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Instruction::Switch second transferToBasicBlock ");
+ //muqi: add for identifing the block will be executed
+ muqi_transfertoblock_info(bb,*it,i,NULL);
+
+
transferToBasicBlock(*it, bb, *es);
successorsPaths->insert(es);
}
@@ -3885,13 +4137,33 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
}
break;
}
- case Instruction::Unreachable:
+ case Instruction::Unreachable:{
// Note that this is not necessarily an internal bug, llvm will
// generate unreachable instructions in cases where it knows the
// program will crash. So it is effectively a SEGV or internal
// error.
- terminateStateOnExecError(state, "reached \"unreachable\" instruction");
+ //original
+ //terminateStateOnExecError(state, "reached \"unreachable\" instruction");
+ //muqi add for abort()
+ terminateStateEarly(state, "Instruction::Unreachable: termiantes state\n");
+
+ //muqi, we give a fake return value
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Instruction::Ret");
+ //klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Instruction::Br uncondition transferToBasicBlock ");
+ muqi_transfertoblock_info(i->getParent(),NULL,i,NULL);
+ klee_muqi_compare_test(i->getFunction()->getName().data(),"\nInside Ret state.pc ++");
+ ref<Expr> fake_result =NULL;
+ fake_result = ConstantExpr::alloc(0, Expr::Int64);
+ fake_result->dump();
+ Z3Builder temp_build = Z3Builder(false,NULL);
+ int *width = (int *)malloc(sizeof(int));
+ *width = 64;
+ #ifdef ENABLE_Z3
+ Z3ASTHandle fake_result_z3 = temp_build.construct_muqi(fake_result,width);
+ fake_result_z3.dump();
+ #endif
break;
+ }
case Instruction::Invoke:
case Instruction::Call: {
@@ -4283,17 +4555,57 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
// Memory instructions...
case Instruction::Alloca: {
- AllocaInst *ai = cast<AllocaInst>(i);
- unsigned elementSize =
- kmodule->targetData->getTypeStoreSize(ai->getAllocatedType());
- ref<Expr> size = Expr::createPointer(elementSize);
- if (ai->isArrayAllocation()) {
- ref<Expr> count = eval(ki, 0, state).value;
- count = Expr::createZExtToPointerWidth(count);
- size = MulExpr::create(size, count);
+ klee_muqi("Inside Allocate case\n");
+
+
+ //muqi change here!!!
+///*
+ Instruction *temp_next = state.pc->inst;
+ //for here, we use a vector to store all alloca instructions at the begining of function, which can be first in first out.
+ //when the last alloca instruction is executed, we allocate all instructions in vector.
+ if (temp_next){
+ unsigned temp_out_next = temp_next->getOpcode();
+ klee_muqi("Next Instruction num is: %u; ", temp_out_next); //alloc == 29 for llvm 3.8, alloc == 31 for llvm 10
+ if (temp_out_next == Instruction::Alloca){
+ klee_muqi(" Here we push to vector\n");
+ vector_i.push_back(i);
+ vector_ki.push_back(ki);
+ vector_state.push_back(&state);
+
+ }else{
+ vector_i.push_back(i);
+ vector_ki.push_back(ki);
+ vector_state.push_back(&state);
+
+ klee_muqi(" Here we allocate all\n");
+ int count = 0;
+ while(!vector_i.empty()){
+ klee_muqi(" count is %u\n",count);
+ count ++;
+ Instruction *temp_i = vector_i.back();
+ vector_i.pop_back();
+ KInstruction *temp_ki = vector_ki.back();
+ vector_ki.pop_back();
+ ExecutionState *temp_state = vector_state.back();
+ vector_state.pop_back();
+
+ AllocaInst *ai = cast<AllocaInst>(temp_i);
+ unsigned elementSize =
+ kmodule->targetData->getTypeStoreSize(ai->getAllocatedType());
+ ref<Expr> size = Expr::createPointer(elementSize);
+ if (ai->isArrayAllocation()) {
+ ref<Expr> count = eval(temp_ki, 0, *temp_state).value;
+ count = Expr::createZExtToPointerWidth(count);
+ size = MulExpr::create(size, count);
+ }
+ executeAlloc(*temp_state, size, true, temp_ki);
+ }
+ }
}
- executeAlloc(state, size, true, ki);
- break;
+ else{
+ klee_muqi("SOME HORRIBLE THING WRONG IN ALLOCATE\n");
+ }
+ break;
}
case Instruction::Load: {
@@ -4304,6 +4616,15 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
case Instruction::Store: {
ref<Expr> base = eval(ki, 1, state).value;
ref<Expr> value = eval(ki, 0, state).value;
+ /*
+ klee_muqi("Inside instruction:store ");
+ klee_muqi("value is ");
+ value.get()->print(errs());
+ errs()<<"\n";
+ klee_muqi("base is ");
+ base.get()->print(errs());
+ errs()<<"\n";
+ */
executeMemoryOperation(state, true, base, value, 0);
break;
}
@@ -4972,6 +5293,8 @@ void Executor::doDumpStates() {
}
+int muqi_external_call_function_once = 0;
+
void Executor::run(ExecutionState &initialState) {
bindModuleConstants();
@@ -5136,11 +5459,79 @@ void Executor::run(ExecutionState &initialState) {
else {
/* SYSREL */
KInstruction *ki = state.pc;
+ const InstructionInfo *temp_info = ki->info;
+
stepInstruction(state);
singleSuccessor = true;
llvm::errs() << "\nnext instruction: " << (*ki->inst) << "\n";
ExprPPrinter::printConstraints(llvm::errs(), state.constraints);
- executeInstruction(state, ki);
+
+ // executeInstruction(state, ki);
+ //for here, when we face the call instruction in main, skip it and assign 0 as the return result.
+ #if defined(Muqi_external_call)
+ Instruction *i = ki->inst;
+ Type *t = i->getType();
+ if (i->getOpcode() == Instruction::Call){
+ Function *calledFunction = cast<CallInst>(i)->getCalledFunction();
+ if (calledFunction == NULL && !t->isVoidTy()){
+ //indirect call and non-void return
+ Expr::Width to = getWidthForLLVMType(t);
+ ref<Expr> result_temp = ConstantExpr::create(0,to);
+ bindLocal(ki, state, result_temp);
+ }else if (strncmp(calledFunction->getName().str().c_str(),"SEXT",4)== 0
+ || strncmp(calledFunction->getName().str().c_str(),"CONCAT",6)== 0
+ || strncmp(calledFunction->getName().str().c_str(),"ZEXT",4)== 0
+ || strncmp(calledFunction->getName().str().c_str(),"SUB",3)== 0){
+ executeInstruction(state, ki);
+ }else if (calledFunction == NULL && t->isVoidTy()){
+ //indirect call and void return, just skip it
+ }else if (strcmp(calledFunction->getName().str().c_str(),EntryPoint.c_str())!= 0 && !t->isVoidTy()){
+ //direct call and non-void return and name is not same as current function
+ Expr::Width to = getWidthForLLVMType(t);
+ ref<Expr> result_temp = ConstantExpr::create(0,to);
+ bindLocal(ki, state, result_temp);
+ }else if (strcmp(calledFunction->getName().str().c_str(),EntryPoint.c_str())!= 0 && t->isVoidTy()){
+ //direct call and void return and name is not same as current function
+ }else if (strcmp(calledFunction->getName().str().c_str(),EntryPoint.c_str())== 0 && !t->isVoidTy()){
+ //direct call and void return and name is same as current function
+ Expr::Width to = getWidthForLLVMType(t);
+ ref<Expr> result_temp = ConstantExpr::create(0,to);
+ bindLocal(ki, state, result_temp);
+ }else if (strcmp(calledFunction->getName().str().c_str(),EntryPoint.c_str())!= 0 && !t->isVoidTy()){
+ //direct call and void return and name is same as current function
+ }else{
+
+ }
+ }else{
+ executeInstruction(state, ki);
+ }
+ /*
+ if (i->getOpcode() == Instruction::Call && strcmp(cast<CallInst>(i)->getCalledFunction()->getName().str().c_str(),EntryPoint.c_str())!= 0 && !t->isVoidTy() ) {
+ Expr::Width to = getWidthForLLVMType(t);
+ ref<Expr> result_temp = ConstantExpr::create(0,to);
+ bindLocal(ki, state, result_temp);
+ }else {
+ if (i->getOpcode() == Instruction::Call && strcmp(cast<CallInst>(i)->getCalledFunction()->getName().str().c_str(),EntryPoint.c_str())== 0 && muqi_external_call_function_once == 0 && t->isVoidTy()){
+ muqi_external_call_function_once += 1;
+ //executeInstruction(state, ki);
+ }else if (i->getOpcode() == Instruction::Call && strcmp(cast<CallInst>(i)->getCalledFunction()->getName().str().c_str(),EntryPoint.c_str())== 0 && muqi_external_call_function_once != 0 && t->isVoidTy()){
+ muqi_external_call_function_once += 1;
+ }else if (i->getOpcode() == Instruction::Call && strcmp(cast<CallInst>(i)->getCalledFunction()->getName().str().c_str(),EntryPoint.c_str())!= 0 && t->isVoidTy()){
+ }else if (i->getOpcode() == Instruction::Call && strcmp(cast<CallInst>(i)->getCalledFunction()->getName().str().c_str(),EntryPoint.c_str())== 0 && muqi_external_call_function_once == 0 && !t->isVoidTy()){
+ muqi_external_call_function_once += 1;
+ Expr::Width to = getWidthForLLVMType(t);
+ ref<Expr> result_temp = ConstantExpr::create(0,to);
+ bindLocal(ki, state, result_temp);
+ }else if (i->getOpcode() == Instruction::Call && strcmp(cast<CallInst>(i)->getCalledFunction()->getName().str().c_str(),EntryPoint.c_str())== 0 && muqi_external_call_function_once != 0 && !t->isVoidTy()){
+ muqi_external_call_function_once += 1;
+ }
+ else {
+ executeInstruction(state, ki);
+ }
+ //executeInstruction(state, ki);
+ }*/
+ #endif
+
if (RecordValueFlow)
generateValuedInstruction(state, ki);
/* SYSREL extension */
@@ -5976,7 +6367,11 @@ void Executor::executeAlloc(ExecutionState &state,
KInstruction *target,
bool zeroMemory,
bool record,
- const ObjectState *reallocFrom) {
+ const ObjectState *reallocFrom
+ //muqi change here!!!
+ ,bool is_instruction_alloc_last,
+ ref<ConstantExpr> for_last,
+ bool is_instruction_alloc) {
llvm::outs() << "Alloc'ing...\n";
size = toUnique(state, size);
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(size)) {
@@ -5986,7 +6381,7 @@ void Executor::executeAlloc(ExecutionState &state,
LLVMContext &ctx = moduleHandle->getContext();
MemoryObject *mo =
memory->allocate(CE->getZExtValue(), isLocal, /*isGlobal=*/false,
- allocSite, allocationAlignment, CE->getZExtValue(), Type::getInt8Ty(ctx));
+ allocSite, allocationAlignment, CE->getZExtValue(), Type::getInt8Ty(ctx),is_instruction_alloc_last,is_instruction_alloc);
recordMemObj(state, mo);
/* SYSREL extension */
@@ -6327,6 +6722,7 @@ bool Executor::executeCheckAddressInBound(ExecutionState &state,
memoryerror = false;
}
else {
+ //muqi we do not handle this out of bound pointer
llvm::errs() << "Offending address " << address << "\n";
std::string s;
llvm::raw_string_ostream ors(s);
@@ -6337,6 +6733,7 @@ bool Executor::executeCheckAddressInBound(ExecutionState &state,
//terminateStateOnError(*unbound, "memory error: out of bound pointer", Ptr,
//NULL, getAddressInfo(*unbound, address));
memoryerror = true;
+
}
}
return false;
@@ -6439,6 +6836,8 @@ ref<Expr> Executor::performLazyInit(ExecutionState &state,
Type *t = li->getPointerOperand()->getType();
int count = 0;
lazyInitTemp = isLazyInit(t, singleInstance, count);
+ //added by muqi
+ lazyInitTemp = false;
if (!lazyInitTemp && ForwardTypeInference) {
// let's check if the loaded value might be converted to a pointer soon
Type *ptype = checkPossiblePtrCast(target->inst);
@@ -6767,7 +7166,7 @@ bool Executor::executeMemoryOperation(ExecutionState &state,
const MemoryObject *emo = op.first;
//llvm::errs() << "unresolved address=" << address << "\n";
- int unboundResolLimit = 3;
+ int unboundResolLimit = 0;
int resC = 0;
llvm::errs() << "resolution size=" << rl.size() << "\n";
for (ResolutionList::iterator i = rl.begin(), ie = rl.end(); resC < unboundResolLimit && i != ie; ++i, resC++) {
@@ -6838,8 +7237,60 @@ bool Executor::executeMemoryOperation(ExecutionState &state,
state.dumpStack(ors);
llvm::outs() << ors.str() ;
ExprPPrinter::printConstraints(llvm::outs(), state.constraints);
+
+
+ //when we find a out of bound pointer, we
+ // create a local variable and
+ // allocate a constant 100 byte size to it
+ // set zeroMemory as false
+ #if defined(Muqi_external_call)
+ //bool zeroMemory = false;
+ bool zeroMemory = true;
+ ref<Expr> size ;
+ ref<Expr> value ;
+ if (target){
+ Instruction *target_i = target->inst;
+ Type *t_external = target_i->getType();
+ Expr::Width to = getWidthForLLVMType(t_external);
+ size = ConstantExpr::create(to,64);
+ value = ConstantExpr::create(0,to);
+ }else{
+ size = ConstantExpr::create(32,64);
+ value = ConstantExpr::create(0,32);
+ }
+
+
+ ConstantExpr *CE = dyn_cast<ConstantExpr>(size);
+ const llvm::Value *allocSite = state.prevPC->inst;
+ size_t allocationAlignment = 8;
+
+ if (allocationAlignment == 0) {
+ allocationAlignment = getAllocationAlignment(allocSite);
+ }
+
+ MemoryObject *mo =
+ memory->allocate(CE->getZExtValue(), /*isLocal=*/true, /*isGlobal=*/false,
+ allocSite, allocationAlignment,false,false);
+
+ if (!mo) {
+ //bindLocal(target, state,size);
+ } else {
+ ObjectState *os = bindObjectInState(state, mo, /*isLocal=*/true);
+ if (zeroMemory) {
+ os->initializeToZero();
+ } else {
+ os->initializeToRandom();
+ }
+ if (target){
+ bindLocal(target, state, value);
+ }
+ }
+ #endif
+
+ #if !defined(Muqi_external_call)
terminateStateOnError(*unbound, "memory error: out of bound pointer", Ptr,
NULL, getAddressInfo(*unbound, address));
+ #endif
}
}
return false;
@@ -7045,6 +7496,7 @@ void Executor::initArgsAsSymbolic(ExecutionState &state, Function *entryFunc, bo
std::string type_str;
llvm::raw_string_ostream rso(type_str);
at->print(rso);
+ std::string name_of_args = std::string("prompt_args_") + std::to_string(ind) +std::string("_args_end");
if (at->isPointerTy()) {
llvm::outs() << "arg " << ind << " type " << rso.str() << "\n";
at = at->getPointerElementType();
@@ -7080,14 +7532,20 @@ void Executor::initArgsAsSymbolic(ExecutionState &state, Function *entryFunc, bo
//mo = memory->allocateForLazyInit(state, state.prevPC->inst, at, singleInstance, count, laddr);
llvm::outs() << "is arg " << ind << " type " << rso.str() << " single instance? " << singleInstance << "\n";