-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmg-symengine.cpp
1602 lines (1443 loc) · 56.4 KB
/
mg-symengine.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
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <sstream>
using namespace std;
#include "core.hpp"
#include "mg-symengine.hpp"
enum ValueTy {SYMBOL, CONCRETE, HYBRID, UNKNOWN};
enum OperTy {ADD, MOV, SHL, XOR, SHR};
typedef pair<int,int> BitRange;
// A symbolic or concrete value in a formula
struct Value {
int id; // a unique id for each value
ValueTy valty;
Operation *opr;
string conval; // concrete value
bitset<32> bsconval; // concrete set stored as bitset
BitRange brange;
map<BitRange, Value*> childs; // a list of child values in a hybrid value
int len; // length of the value
static int idseed;
Value(ValueTy vty);
Value(ValueTy vty, int l);
Value(ValueTy vty, string con); // constructor for concrete value
Value(ValueTy vty, string con, int l);
Value(ValueTy vty, Operation *oper);
Value(ValueTy vty, Operation *oper, int l);
Value(ValueTy vty, bitset<32> bs);
bool isSymbol();
bool isConcrete();
bool isHybrid();
};
int Value::idseed = 0;
Value::Value(ValueTy vty) : opr(NULL)
{
id = ++idseed;
valty = vty;
len = 32;
}
Value::Value(ValueTy vty, int l) : opr(NULL)
{
id = ++idseed;
valty = vty;
len = l;
}
Value::Value(ValueTy vty, string con) : opr(NULL),bsconval(stoul(con, 0, 16))
{
id = ++idseed;
valty = vty;
conval = con;
brange.first = 0;
brange.second = 31;
len = 32;
}
Value::Value(ValueTy vty, string con, int l) : opr(NULL)
{
id = ++idseed;
valty = vty;
conval = con;
len = l;
}
Value::Value(ValueTy vty, bitset<32> bs) : opr(NULL)
{
id = ++idseed;
valty = vty;
bsconval = bs;
len = 32;
}
Value::Value(ValueTy vty, Operation *oper)
{
id = ++idseed;
valty = vty;
opr = oper;
len = 32;
}
Value::Value(ValueTy vty, Operation *oper, int l)
{
id = ++idseed;
valty = vty;
opr = oper;
len = l;
}
bool Value::isSymbol()
{
if (this->valty == SYMBOL)
return true;
else
return false;
}
bool Value::isConcrete()
{
if (this->valty == CONCRETE)
return true;
else
return false;
}
bool Value::isHybrid()
{
if (this->valty == HYBRID)
return true;
else
return false;
}
string getValueName(Value *v)
{
if (v->valty == SYMBOL)
return "sym" + to_string(v->id);
else
return v->conval;
}
// An operation taking several values to calculate a result value
struct Operation {
string opty;
Value *val[3];
Operation(string opt, Value *v1);
Operation(string opt, Value *v1, Value *v2);
Operation(string opt, Value *v1, Value *v2, Value *v3);
};
Operation::Operation(string opt, Value *v1)
{
opty = opt;
val[0] = v1;
val[1] = NULL;
val[2] = NULL;
}
Operation::Operation(string opt, Value *v1, Value *v2)
{
opty = opt;
val[0] = v1;
val[1] = v2;
val[2] = NULL;
}
Operation::Operation(string opt, Value *v1, Value *v2, Value *v3)
{
opty = opt;
val[0] = v1;
val[1] = v2;
val[2] = v3;
}
Value *buildop1(string opty, Value *v1)
{
Operation *oper = new Operation(opty, v1);
Value *result;
if (v1->isSymbol())
result = new Value(SYMBOL, oper);
else
result = new Value(CONCRETE, oper);
return result;
}
Value *buildop2(string opty, Value *v1, Value *v2)
{
Operation *oper = new Operation(opty, v1, v2);
Value *result;
if (v1->isSymbol() || v2->isSymbol())
result = new Value(SYMBOL, oper);
else
result = new Value(CONCRETE, oper);
return result;
}
// Currently there is no 3-operand operation,
// it is reserved for future.
Value *buildop3(string opty, Value *v1, Value *v2, Value *v3)
{
Operation *oper = new Operation(opty, v1, v2, v3);
Value *result;
if (v1->isSymbol() || v2->isSymbol() || v3->isSymbol())
result = new Value(SYMBOL, oper);
else
result = new Value(CONCRETE, oper);
return result;
}
// ********************************
// Class SEEngine Implementation
// ********************************
// return the concrete value in a register
ADDR32 SEEngine::getRegConVal(string reg)
{
if (reg == "eax")
return ip->ctxreg[0];
else if (reg == "ebx")
return ip->ctxreg[1];
else if (reg == "ecx")
return ip->ctxreg[2];
else if (reg == "edx")
return ip->ctxreg[3];
else if (reg == "esi")
return ip->ctxreg[4];
else if (reg == "edi")
return ip->ctxreg[5];
else if (reg == "esp")
return ip->ctxreg[6];
else if (reg == "ebp")
return ip->ctxreg[7];
else {
cerr << "now only get 32 bit register's concrete value." << endl;
return 0;
}
}
// Calculate the address in a memory operand
ADDR32 SEEngine::calcAddr(Operand *opr)
{
ADDR32 r1, r2, c; // addr = r1 + r2*n + c
int32_t n;
switch (opr->tag)
{
case 7: // addr7 = r1 + r2*n + c
r1 = getRegConVal(opr->field[0]);
r2 = getRegConVal(opr->field[1]);
n = stoi(opr->field[2]);
c = stoul(opr->field[4], 0, 16);
if (opr->field[3] == "+")
return r1 + r2*n + c;
else if (opr->field[3] == "-")
return r1 + r2*n - c;
else {
cerr << "unrecognized addr: tag 7" << endl;
return 0;
}
case 4: // addr4 = r1 + c
r1 = getRegConVal(opr->field[0]);
c = stoul(opr->field[2], 0, 16);
if (opr->field[1] == "+")
return r1 + c;
else if (opr->field[1] == "-")
return r1 - c;
else {
cerr << "unrecognized addr: tag 4" << endl;
return 0;
}
case 5: // addr5 = r1 + r2*n
r1 = getRegConVal(opr->field[0]);
r2 = getRegConVal(opr->field[1]);
n = stoi(opr->field[2]);
return r1 + r2*n;
case 6: // addr6 = r2*n + c
r2 = getRegConVal(opr->field[0]);
n = stoi(opr->field[1]);
c = stoul(opr->field[3], 0, 16);
if (opr->field[2] == "+")
return r2*n + c;
else if (opr->field[2] == "-")
return r2*n - c;
else {
cerr << "unrecognized addr: tag 6" << endl;
return 0;
}
case 3: // addr3 = r2*n
r2 = getRegConVal(opr->field[0]);
n = stoi(opr->field[1]);
return r2*n;
case 1: // addr1 = c
c = stoul(opr->field[0], 0, 16);
return c;
case 2: // addr2 = r1
r1 = getRegConVal(opr->field[0]);
return r1;
default:
cerr << "unrecognized addr tag" << endl;
return 0;
}
}
bool hasVal(Value *v, int start, int end)
{
BitRange br(start,end);
map<BitRange, Value*>::iterator i = v->childs.find(br);
if (i == v->childs.end())
return false;
else
return true;
}
Value *readVal(Value *v, int start, int end)
{
BitRange br(start,end);
map<BitRange, Value*>::iterator i = v->childs.find(br);
if (i == v->childs.end())
return NULL;
else
return i->second;
}
string bs2str(bitset<32> bs, BitRange br){
int start = br.first, end = br.second;
unsigned int ui = 0, step = 1;
for (int i = start; i <= end; ++i, step *= 2) {
ui += bs[i] * step;
}
stringstream strs;
strs << "0x" << hex << ui; // the number of bits that should shift right
string res(strs.str());
return res;
}
Value *writeVal(Value *from, Value *to, int start, int end)
{
Value *res = new Value(HYBRID);
BitRange brfrom(start, end);
if (to->isHybrid()) {
map<BitRange, Value*>::iterator i = to->childs.find(brfrom);
if (i != to->childs.end()) {
i->second = from;
return to;
} else {
cerr << "writeVal: no child in to match the from!" << endl;
return NULL;
}
} else if (from->isSymbol() && to->isConcrete()) {
int s1 = to->brange.first;
int e1 = to->brange.second;
Value *v1 = new Value(CONCRETE, to->bsconval);
Value *v2 = new Value(CONCRETE, to->bsconval);
v1->brange.first = s1;
v1->brange.second = start-1;
v1->conval = bs2str(v1->bsconval, v1->brange);
v2->brange.first = end+1;
v2->brange.second = e1;
v2->conval = bs2str(v2->bsconval, v2->brange);
res->childs.insert(pair<BitRange, Value*>(v1->brange, v1));
res->childs.insert(pair<BitRange, Value*>(brfrom, from));
res->childs.insert(pair<BitRange, Value*>(v2->brange, v2));
return res;
} else {
cerr << "writeVal: the case is missing!" << endl;
return NULL;
}
}
Value* SEEngine::readReg(string &s)
{
Value *res;
if (s == "eax" || s == "ebx" || s == "ecx" || s == "edx" ||
s == "esi" || s == "edi" || s == "esp" || s == "ebp") { // 32 bit reg
return ctx[s];
} else if (s == "ax" || s == "bx" || s == "cx" || s == "dx" ||
s == "si" || s == "di" || s == "bp") { // 16 bit reg
if (hasVal(ctx["e"+s], 0, 15)) { // already has a 16 bit value in the register
res = readVal(ctx["e"+s], 0, 15);
return res;
} else { // generate a 16-bit value from the 32-bit value in the register
Value *v0 = ctx["e"+s];
Value *v1 = new Value(CONCRETE, "0x0000ffff");
res = buildop2("and", v0, v1);
return res;
}
} else if (s == "al" || s == "bl" || s == "cl" || s == "dl") {
string rname = 'e' + s.substr(0,1) + 'x';
if (hasVal(ctx[rname], 0, 7)) {
res = readVal(ctx[rname], 0, 7);
return res;
} else {
Value *v0 = ctx[rname];
Value *v1 = new Value(CONCRETE, "0x000000ff");
res = buildop2("and", v0, v1);
return res;
}
} else if (s == "ah" || s == "bh" || s == "ch" || s == "dh") {
string rname = 'e' + s.substr(0,1) + 'x';
if (hasVal(ctx[rname], 8, 15)) {
res = readVal(ctx[rname], 8, 15);
return res;
} else {
Value *v0 = ctx[rname];
Value *v1 = new Value(CONCRETE, "0x0000ff00");
Value *v2 = buildop2("and", v0, v1);
Value *v3 = new Value(CONCRETE, "0x8"); // shift the value to the
res = buildop2("shr", v2, v3); // most right position
return res;
}
} else {
cerr << "unknown reg name!" << endl;
return NULL;
}
}
void SEEngine::writeReg(string &s, Value *v)
{
Value *res;
if (s == "eax" || s == "ebx" || s == "ecx" || s == "edx" ||
s == "esi" || s == "edi" || s == "esp" || s == "ebp") { // 32 bit reg
ctx[s] = v;
} else if (s == "ax" || s == "bx" || s == "cx" || s == "dx" ||
s == "si" || s == "di" || s == "bp") { // 16 bit reg
Value *v0 = new Value(CONCRETE, "0xffff0000"); // mask the low 16 bits
Value *v1 = ctx["e"+s];
Value *v2 = buildop2("and", v1, v0);
res = buildop2("or", v2, v);
ctx["e"+s] = res;
} else if (s == "al" || s == "bl" || s == "cl" || s == "dl") {
string rname = 'e' + s.substr(0,1) + 'x';
Value *v0 = new Value(CONCRETE, "0xffffff00"); // mask the low 8 bits
Value *v1 = ctx[rname];
Value *v2 = buildop2("and", v1, v0);
res = buildop2("or", v2, v);
ctx[rname] = res;
} else if (s == "ah" || s == "bh" || s == "ch" || s == "dh") {
string rname = 'e' + s.substr(0,1) + 'x';
if (ctx[rname]->isConcrete() && v->isSymbol()) {
ctx[rname] = writeVal(v, ctx[rname], 8, 15);
} else {
Value *v0 = new Value(CONCRETE, "0x8"); // shift the written value
Value *v1 = buildop2("shl", v, v0); // to the correct position
Value *v2 = new Value(CONCRETE, "0xffff00ff"); // mask the high 8 bits
Value *v3 = ctx[rname];
Value *v4 = buildop2("and", v3, v2);
res = buildop2("or", v4, v1);
ctx[rname] = res;
}
} else {
cerr << "unknown reg name!" << endl;
}
}
// find whether ar is a subset of the AddrRanges in mem. If true, set
// the AddrRange of the superset
bool SEEngine::issubset(AddrRange ar, AddrRange *superset)
{
for (map<AddrRange,Value*>::iterator it = mem.begin(); it != mem.end(); ++it) {
AddrRange curar = it->first;
if (curar.first <= ar.first && curar.second >= ar.second) {
superset->first = curar.first;
superset->second = curar.second;
return true;
}
}
return false;
}
// find whether ar is a superset of the AddrRanges in mem. If true, set
// the AddrRange of the subset
bool SEEngine::issuperset(AddrRange ar, AddrRange *subset)
{
for (map<AddrRange,Value*>::iterator it = mem.begin(); it != mem.end(); ++it) {
AddrRange curar = it->first;
if (curar.first >= ar.first && curar.second <= ar.second) {
subset->first = curar.first;
subset->second = curar.second;
return true;
}
}
return false;
}
// check whether the address range exists in current memory. Return true if it is new
bool SEEngine::isnew(AddrRange ar)
{
for (map<AddrRange,Value*>::iterator it = mem.begin(); it != mem.end(); ++it) {
AddrRange curar = it->first;
if ((curar.first <= ar.first && curar.second >= ar.first) ||
(curar.first <= ar.second && curar.second >= ar.second)) {
return false;
}
}
return true;
}
Value* SEEngine::readMem(ADDR32 addr, int nbyte)
{
ADDR32 end = addr + nbyte - 1;
AddrRange ar(addr, end), res;
if (memfind(ar)) return mem[ar];
if (isnew(ar)) {
Value *v = new Value(SYMBOL, nbyte);
mem[ar] = v;
meminput[v] = ar;
return v;
} else if (issubset(ar, &res)) {
ADDR32 b1 = ar.first, e1 = ar.second;
ADDR32 b2 = res.first, e2 = res.second;
string mask = "0x";
for (ADDR32 i = e2; i >= b2; --i) { // little endion: read from the most
if (i >= b1 && i <= e1) // significant position
mask += "ff";
else
mask += "00";
}
stringstream strs;
strs << "0x" << hex << (b1-b2) * 8; // the number of bits that should shift right
string low0(strs.str());
Value *v0 = mem[res];
Value *v1 = new Value(CONCRETE, mask);
Value *v2 = buildop2("and", v0, v1);
Value *v3 = new Value(CONCRETE, low0);
Value *v4 = buildop2("shr", v2, v3); // shift right
return v4;
} else {
cerr << "readMem: Partial overlapping symbolic memory access is not implemented yet!" << endl;
return NULL;
}
}
void SEEngine::writeMem(ADDR32 addr, int nbyte, Value *v)
{
ADDR32 end = addr + nbyte -1;
AddrRange ar(addr, end), res;
if (memfind(ar) || isnew(ar)) { // The old value is destroyed when the new AddrRange
mem[ar] = v; // is equivalent to the old one, or is a new AddrRange
return;
} else if (issuperset(ar, &res)) { // or lager
mem.erase(res);
mem[ar] = v;
return;
} else if (issubset(ar, &res)) {
ADDR32 b1 = ar.first, e1 = ar.second;
ADDR32 b2 = res.first, e2 = res.second;
string mask = "0x"; // mask used to set the bits to be writen in the old value as 0
for (ADDR32 i = e2; i >= b2; --i) {
if (i >= b1 && i <= e1)
mask += "00";
else
mask += "ff";
}
stringstream strs;
strs << "0x" << hex << (b1-b2) * 8;
string low0(strs.str()); // the number of bits that should shift left
Value *v0 = mem[res];
Value *v1 = new Value(CONCRETE, mask);
Value *v2 = buildop2("and", v0, v1);
Value *v3 = new Value(CONCRETE, low0);
Value *v4 = buildop2("shl", v, v3); // shift left the new value to the correct location
Value *v5 = buildop2("or", v2, v4);
mem[res] = v5;
return;
} else {
cerr << "writeMem: Partial overlapping symbolic memory access is not implemented yet!" << endl;
}
}
void SEEngine::init(Value *v1, Value *v2, Value *v3, Value *v4,
Value *v5, Value *v6, Value *v7, Value *v8,
list<Inst>::iterator it1,
list<Inst>::iterator it2)
{
ctx["eax"] = v1;
ctx["ebx"] = v2;
ctx["ecx"] = v3;
ctx["edx"] = v4;
ctx["esi"] = v5;
ctx["edi"] = v6;
ctx["esp"] = v7;
ctx["ebp"] = v8;
reginput[v1] = "eax";
reginput[v2] = "ebx";
reginput[v3] = "ecx";
reginput[v4] = "edx";
reginput[v5] = "esi";
reginput[v6] = "edi";
reginput[v7] = "esp";
reginput[v8] = "ebp";
this->start = it1;
this->end = it2;
}
void SEEngine::init(list<Inst>::iterator it1,
list<Inst>::iterator it2)
{
this->start = it1;
this->end = it2;
}
void SEEngine::initAllRegSymol(list<Inst>::iterator it1,
list<Inst>::iterator it2)
{
Value *v1 = new Value(SYMBOL);
Value *v2 = new Value(SYMBOL);
Value *v3 = new Value(SYMBOL);
Value *v4 = new Value(SYMBOL);
Value *v5 = new Value(SYMBOL);
Value *v6 = new Value(SYMBOL);
Value *v7 = new Value(SYMBOL);
Value *v8 = new Value(SYMBOL);
ctx["eax"] = v1;
ctx["ebx"] = v2;
ctx["ecx"] = v3;
ctx["edx"] = v4;
ctx["esi"] = v5;
ctx["edi"] = v6;
ctx["esp"] = v7;
ctx["ebp"] = v8;
reginput[v1] = "eax";
reginput[v2] = "ebx";
reginput[v3] = "ecx";
reginput[v4] = "edx";
reginput[v5] = "esi";
reginput[v6] = "edi";
reginput[v7] = "esp";
reginput[v8] = "ebp";
start = it1;
end = it2;
}
// instructions which have no effect in symbolic execution
set<string> noeffectinst = {"test","jmp","jz","jbe","jo","jno","js","jns","je","jne",
"jnz","jb","jnae","jc","jnb","jae",
"jnc","jna","ja","jnbe","jl",
"jnge","jge","jnl","jle","jng","jg",
"jnle","jp","jpe","jnp","jpo","jcxz",
"jecxz", "ret", "cmp", "call"};
int SEEngine::symexec()
{
for (list<Inst>::iterator it = start; it != end; ++it) {
// cout << hex << it->addrn << ": ";
// cout << it->opcstr << '\n';
// skip no effect instructions
ip = it;
if (noeffectinst.find(it->opcstr) != noeffectinst.end()) continue;
switch (it->oprnum) {
case 0:
break;
case 1:
{
Operand *op0 = it->oprd[0];
Value *v0, *res, *temp;
int nbyte;
if (it->opcstr == "push") {
if (op0->ty == Operand::IMM) {
v0 = new Value(CONCRETE, op0->field[0]);
writeMem(it->waddr, 4, v0);
} else if (op0->ty == Operand::REG) {
nbyte = op0->bit / 8;
temp = readReg(op0->field[0]);
writeMem(it->waddr, nbyte, temp);
} else if (op0->ty == Operand::MEM) {
// The memaddr in the trace is the read address
// We need to compute the write address
// ADDR32 espval = it->ctxreg[6];
// *******************************
// New trace include the read/write memory addresses,
// so no need to get the esp value.
nbyte = op0->bit / 8;
v0 = readMem(it->raddr, nbyte);
writeMem(it->waddr, nbyte, v0);
} else {
cout << "push error: the operand is not Imm, Reg or Mem!" << endl;
return 1;
}
} else if (it->opcstr == "pop") {
if (op0->ty == Operand::REG) {
nbyte = op0->bit / 8;
temp = readMem(it->raddr, nbyte);
writeReg(op0->field[0], temp);
} else if (op0->ty == Operand::MEM) {
nbyte = op0->bit / 8;
temp = readMem(it->raddr, nbyte);
// ADDR32 waddr = calcAddr(op0);
writeMem(it->waddr, nbyte, temp);
} else {
cout << "pop error: the operand is not Reg!" << endl;
return 1;
}
} else { // handle other one operand instructions
if (op0->ty == Operand::REG) {
v0 = readReg(op0->field[0]);
res = buildop1(it->opcstr, v0);
writeReg(op0->field[0], res);
} else if (op0->ty == Operand::MEM) {
nbyte = op0->bit / 8;
v0 = readMem(it->raddr, nbyte);
res = buildop1(it->opcstr, v0);
writeMem(it->waddr, nbyte, res);
} else {
cout << "[Error] Line " << it->id << ": Unknown 1 op instruction!" << endl;
return 1;
}
}
break;
}
case 2:
{
Operand *op0 = it->oprd[0];
Operand *op1 = it->oprd[1];
Value *v0, *v1, *res, *temp;
int nbyte;
if (it->opcstr == "mov") { // handle mov instruction
if (op0->ty == Operand::REG) {
if (op1->ty == Operand::IMM) { // mov reg, 0x1111
v1 = new Value(CONCRETE, op1->field[0]);
writeReg(op0->field[0], v1);
} else if (op1->ty == Operand::REG) { // mov reg, reg
temp = readReg(op1->field[0]);
writeReg(op0->field[0], temp);
} else if (op1->ty == Operand::MEM) { // mov reg, dword ptr [ebp+0x1]
/* 1. Get mem address
2. check whether the mem address has been accessed
3. if not, create a new value
4. else load the value in that memory
*/
nbyte = op1->bit / 8;
v1 = readMem(it->raddr, nbyte);
writeReg(op0->field[0], v1);
} else {
cerr << "op1 is not ImmValue, Reg or Mem" << endl;
return 1;
}
} else if (op0->ty == Operand::MEM) {
if (op1->ty == Operand::IMM) { // mov dword ptr [ebp+0x1], 0x1111
temp = new Value(CONCRETE, op1->field[0]);
nbyte = op0->bit / 8;
writeMem(it->waddr, nbyte, temp);
} else if (op1->ty == Operand::REG) { // mov dword ptr [ebp+0x1], reg
temp = readReg(op1->field[0]);
nbyte = op0->bit / 8;
writeMem(it->waddr, nbyte, temp);
}
} else {
cerr << "Error: The first operand in MOV is not Reg or Mem!" << endl;
}
} else if (it->opcstr == "lea") { // handle lea instruction
/* lea reg, ptr [edx+eax*1]
interpret lea instruction based on different address type
1. op0 must be reg
2. op1 must be addr
*/
if (op0->ty != Operand::REG || op1->ty != Operand::MEM) {
cerr << "lea format error!" << endl;
}
switch (op1->tag) {
case 5:
{
Value *f0, *f1, *f2; // corresponding field[0-2] in operand
f0 = readReg(op1->field[0]);
f1 = readReg(op1->field[1]);
f2 = new Value(CONCRETE, op1->field[2]);
res = buildop2("imul", f1, f2);
res = buildop2("add",f0, res);
writeReg(op0->field[0], res);
break;
}
default:
cerr << "Other tags in addr is not ready for lea!" << endl;
break;
}
} else if (it->opcstr == "xchg") {
if (op1->ty == Operand::REG) {
v1 = readReg(op1->field[0]);
if (op0->ty == Operand::REG) {
v0 = readReg(op0->field[0]);
writeReg(op1->field[0], v0);
writeReg(op0->field[0], v1);
} else if (op0->ty == Operand::MEM) {
nbyte = op0->bit / 8;
v0 = readMem(it->raddr, nbyte);
writeReg(op1->field[0], v0);
writeMem(it->waddr, nbyte, v1);
} else {
cerr << "xchg error: 1" << endl;
}
} else if (op1->ty == Operand::MEM) {
nbyte = op1->bit / 8;
v1 = readMem(it->raddr, nbyte);
if (op0->ty == Operand::REG) {
v0 = readReg(op0->field[0]);
writeReg(op0->field[0], v1);
writeMem(it->waddr, nbyte, v0);
} else {
cerr << "xchg error 3" << endl;
}
} else {
cerr << "xchg error: 2" << endl;
}
} else if (it->opcstr == "shl") {
if (op0->ty == Operand::REG && op1->ty == Operand::IMM && readReg(op0->field[0])->isHybrid()) {
v0 = readReg(op0->field[0]);
int offset = (int)stoul(op1->field[0], 0, 16);
map<BitRange, Value*> newchilds;
for (map<BitRange, Value*>::iterator i = v0->childs.begin(); i != v0->childs.end(); ++i) {
Value *v = i->second;
if (v->valty == SYMBOL) {
if (i->first.first != 0)
v->brange.first = i->first.first + offset;
v->brange.second = i->first.second + offset;
if (v->brange.second > 31)
v->brange.second = 31;
} else if (v->valty == CONCRETE) {
v->bsconval <<= offset;
if (v->brange.first != 0)
v->brange.first += offset;
v->brange.second += offset;
if (v->brange.second > 31)
v->brange.second = 31;
v->conval = bs2str(v->bsconval, v->brange);
} else {
cout << "shl: unknown value type" << endl;
}
newchilds.insert(pair<BitRange, Value*>(v->brange, v));
}
v0->childs = newchilds;
} else { // other situations in shl
if (op1->ty == Operand::IMM) {
v1 = new Value(CONCRETE, op1->field[0]);
} else if (op1->ty == Operand::REG) {
v1 = readReg(op1->field[0]);
} else if (op1->ty == Operand::MEM) {
nbyte = op1->bit / 8;
v1 = readMem(it->raddr, nbyte);
} else {
cerr << "other instructions: op1 is not ImmValue, Reg, or Mem!" << endl;
return 1;
}
if (op0->ty == Operand::REG) { // dest op is reg
v0 = readReg(op0->field[0]);
res = buildop2(it->opcstr, v0, v1);
writeReg(op0->field[0], res);
} else if (op0->ty == Operand::MEM) { // dest op is mem
nbyte = op0->bit / 8;
v0 = readMem(it->raddr, nbyte);
res = buildop2(it->opcstr, v0, v1);
writeMem(it->waddr, nbyte, res);
} else {
cout << "other instructions: op2 is not ImmValue, Reg, or Mem!" << endl;
return 1;
}
}
} else if (it->opcstr == "shr") {
if (op0->ty == Operand::REG && op1->ty == Operand::IMM && readReg(op0->field[0])->isHybrid()) {
v0 = readReg(op0->field[0]);
int offset = (int)stoul(op1->field[0], 0, 16);
for (map<BitRange, Value*>::iterator i = v0->childs.begin(); i != v0->childs.end(); ++i) {
Value *v = i->second;
if (v->valty == SYMBOL) {
if (v->brange.second != 31)
v->brange.second -= offset;
v->brange.first -= offset;
if (v->brange.first < 0) v->brange.first = 0;
} else if (v->valty == CONCRETE) {
v->bsconval >>= offset;
if (v->brange.second != 31)
v->brange.second -= offset;
v->brange.first -= offset;
if (v->brange.first < 0) v->brange.first = 0;
v->conval = bs2str(v->bsconval, v->brange);
} else {
cout << "shr: unknown value type" << endl;
}
}
} else { // other situations in shr
if (op1->ty == Operand::IMM) {
v1 = new Value(CONCRETE, op1->field[0]);
} else if (op1->ty == Operand::REG) {
v1 = readReg(op1->field[0]);
} else if (op1->ty == Operand::MEM) {
nbyte = op1->bit / 8;
v1 = readMem(it->raddr, nbyte);
} else {
cerr << "other instructions: op1 is not ImmValue, Reg, or Mem!" << endl;
return 1;
}
if (op0->ty == Operand::REG) { // dest op is reg
v0 = readReg(op0->field[0]);
res = buildop2(it->opcstr, v0, v1);
writeReg(op0->field[0], res);
} else if (op0->ty == Operand::MEM) { // dest op is mem
nbyte = op0->bit / 8;
v0 = readMem(it->raddr, nbyte);
res = buildop2(it->opcstr, v0, v1);
writeMem(it->waddr, nbyte, res);
} else {
cout << "other instructions: op2 is not ImmValue, Reg, or Mem!" << endl;
return 1;
}
}
} else if (it->opcstr == "and") {
if (op0->ty == Operand::REG && op1->ty == Operand::IMM && readReg(op0->field[0])->isHybrid()) {
v0 = readReg(op0->field[0]);
v1 = new Value(CONCRETE, op1->field[0]);
bool status = true;
for (map<BitRange, Value*>::iterator it = v0->childs.begin(); it != v0->childs.end(); ++it) {
if (it->second->isSymbol()) {
for (int i = it->first.first; i <= it->first.second; ++i) {
if (v1->bsconval[i] == 1) status = false;
}
}
}
if (status == true) {
Value *res = new Value(CONCRETE);
res->brange.first = 0;
res->brange.second = 31;
for (map<BitRange, Value*>::iterator it = v0->childs.begin(); it != v0->childs.end(); ++it) {
if (it->second->isSymbol()) {
for (int i = it->first.first; i <= it->first.second; ++i) {
res->bsconval[i] = 0;
}
} else if (it->second->isConcrete()) {
for (int i = it->first.first; i <= it->first.second; ++i) {
res->bsconval[i] = it->second->bsconval[i] & v1->bsconval[i];
}
} else {
cout << "unkown val type" << endl;
}
}
res->conval = bs2str(res->bsconval, res->brange);
writeReg(op0->field[0], res);
}
} else {
if (op1->ty == Operand::IMM) {
v1 = new Value(CONCRETE, op1->field[0]);
} else if (op1->ty == Operand::REG) {
v1 = readReg(op1->field[0]);
} else if (op1->ty == Operand::MEM) {
nbyte = op1->bit / 8;
v1 = readMem(it->raddr, nbyte);
} else {
cerr << "other instructions: op1 is not ImmValue, Reg, or Mem!" << endl;
return 1;
}
if (op0->ty == Operand::REG) { // dest op is reg
v0 = readReg(op0->field[0]);
res = buildop2(it->opcstr, v0, v1);
writeReg(op0->field[0], res);
} else if (op0->ty == Operand::MEM) { // dest op is mem
nbyte = op0->bit / 8;
v0 = readMem(it->raddr, nbyte);
res = buildop2(it->opcstr, v0, v1);
writeMem(it->waddr, nbyte, res);
} else {
cout << "other instructions: op2 is not ImmValue, Reg, or Mem!" << endl;
return 1;
}
}
} if (it->opcstr == "or") {
if (op0->ty == Operand::REG && op1->ty == Operand::IMM && readReg(op0->field[0])->isHybrid()) {
v0 = readReg(op0->field[0]);
v1 = new Value(CONCRETE, op1->field[0]);