-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
Copy pathrulecompile.cc
1013 lines (877 loc) · 24.9 KB
/
rulecompile.cc
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
/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef CPUI_RULECOMPILE
#include "rulecompile.hh"
#include "ruleparse.hh"
namespace ghidra {
RuleCompile *rulecompile;
extern int4 ruleparsedebug;
extern int4 ruleparseparse(void);
class MyLoadImage : public LoadImage { // Dummy loadimage
public:
MyLoadImage(void) : LoadImage("nofile") {}
virtual void loadFill(uint1 *ptr,int4 size,const Address &addr) { for(int4 i=0;i<size;++i) ptr[i] = 0; }
virtual string getArchType(void) const { return "myload"; }
virtual void adjustVma(long adjust) { }
};
int4 RuleLexer::identlist[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0,
0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0, 0, 5,
0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int4 RuleLexer::scanIdentifier(void)
{
int4 i=0;
identifier[i] = (char)getNextChar(); // Scan at least the first character
i += 1;
do {
if ((identlist[next(0)]&1) != 0) {
identifier[i] = (char) getNextChar();
i += 1;
}
else
break;
} while(i<255);
if ((i==255)||(i==0))
return -1; // Identifier is too long
identifier[i] = '\0';
identlength = i;
if ((identlist[(int4)identifier[0]]&2) != 0) // First number is digit
return scanNumber();
switch(identifier[0]) {
case 'o':
return buildString(OP_IDENTIFIER);
case 'v':
return buildString(VAR_IDENTIFIER);
case '#':
return buildString(CONST_IDENTIFIER);
case 'O':
return buildString(OP_NEW_IDENTIFIER);
case 'V':
return buildString(VAR_NEW_IDENTIFIER);
case '.':
return buildString(DOT_IDENTIFIER);
default:
return otherIdentifiers();
}
}
int4 RuleLexer::scanNumber(void)
{
istringstream s(identifier);
s.unsetf(ios::dec | ios::hex | ios::oct);
uint8 val;
s >> val;
if (!s)
return BADINTEGER;
ruleparselval.big = new int8(val);
return INTB;
}
int4 RuleLexer::buildString(int4 tokentype)
{
if (identlength <= 1) return -1;
for(int4 i=1;i<identlength;++i) {
if ((identlist[(int4)identifier[i]]&4)==0) return -1;
}
if (identifier[0] == '.') {
ruleparselval.str = new string(identifier+1);
return tokentype;
}
if (identifier[0] == '#')
identifier[0] = 'c';
ruleparselval.str = new string(identifier);
return tokentype;
}
int4 RuleLexer::otherIdentifiers(void)
{
map<string,int4>::const_iterator iter;
iter = keywordmap.find(string(identifier));
if (iter != keywordmap.end())
return (*iter).second;
return -1;
}
void RuleLexer::initKeywords(void)
{
keywordmap["COPY"] = OP_COPY;
keywordmap["ZEXT"] = OP_INT_ZEXT;
keywordmap["CARRY"] = OP_INT_CARRY;
keywordmap["SCARRY"] = OP_INT_SCARRY;
keywordmap["SEXT"] = OP_INT_SEXT;
keywordmap["SBORROW"] = OP_INT_SBORROW;
keywordmap["NAN"] = OP_FLOAT_NAN;
keywordmap["ABS"] = OP_FLOAT_ABS;
keywordmap["SQRT"] = OP_FLOAT_SQRT;
keywordmap["CEIL"] = OP_FLOAT_CEIL;
keywordmap["FLOOR"] = OP_FLOAT_FLOOR;
keywordmap["ROUND"] = OP_FLOAT_ROUND;
keywordmap["INT2FLOAT"] = OP_FLOAT_INT2FLOAT;
keywordmap["FLOAT2FLOAT"] = OP_FLOAT_FLOAT2FLOAT;
keywordmap["TRUNC"] = OP_FLOAT_TRUNC;
keywordmap["GOTO"] = OP_BRANCH;
keywordmap["GOTOIND"] = OP_BRANCHIND;
keywordmap["CALL"] = OP_CALL;
keywordmap["CALLIND"] = OP_CALLIND;
keywordmap["RETURN"] = OP_RETURN;
keywordmap["CBRANCH"] = OP_CBRANCH;
keywordmap["USEROP"] = OP_CALLOTHER;
keywordmap["LOAD"] = OP_LOAD;
keywordmap["STORE"] = OP_STORE;
keywordmap["CONCAT"] = OP_PIECE;
keywordmap["SUBPIECE"] = OP_SUBPIECE;
keywordmap["before"] = BEFORE_KEYWORD;
keywordmap["after"] = AFTER_KEYWORD;
keywordmap["remove"] = REMOVE_KEYWORD;
keywordmap["set"] = SET_KEYWORD;
keywordmap["istrue"] = ISTRUE_KEYWORD;
keywordmap["isfalse"] = ISFALSE_KEYWORD;
}
int4 RuleLexer::nextToken(void)
{
for(;;) {
int4 mychar = next(0);
switch(mychar) {
case '(':
case ')':
case ',':
case '[':
case ']':
case ';':
case '{':
case '}':
case ':':
getNextChar();
ruleparselval.ch = (char)mychar;
return mychar;
case '\r':
case ' ':
case '\t':
case '\v':
getNextChar();
break;
case '\n':
getNextChar();
lineno += 1;
break;
case '-':
getNextChar();
if (next(0) == '>') {
getNextChar();
return RIGHT_ARROW;
}
else if (next(0) == '-') {
getNextChar();
if (next(0) == '>') {
getNextChar();
return DOUBLE_RIGHT_ARROW;
}
return ACTION_TICK;
}
return OP_INT_SUB;
case '<':
getNextChar();
if (next(0) == '-') {
getNextChar();
if (next(0) == '-') {
getNextChar();
return DOUBLE_LEFT_ARROW;
}
return LEFT_ARROW;
}
else if (next(0) == '<') {
getNextChar();
return OP_INT_LEFT;
}
else if (next(0) == '=') {
getNextChar();
return OP_INT_LESSEQUAL;
}
return OP_INT_LESS;
case '|':
getNextChar();
if (next(0) == '|') {
getNextChar();
return OP_BOOL_OR;
}
return OP_INT_OR;
case '&':
getNextChar();
if (next(0) == '&') {
getNextChar();
return OP_BOOL_AND;
}
return OP_INT_AND;
case '^':
getNextChar();
if (next(0) == '^') {
getNextChar();
return OP_BOOL_XOR;
}
return OP_INT_XOR;
case '>':
if (next(1) == '>') {
getNextChar();
getNextChar();
return OP_INT_RIGHT;
}
return -1;
case '=':
getNextChar();
if (next(0) == '=') {
getNextChar();
return OP_INT_EQUAL;
}
ruleparselval.ch = (char)mychar;
return mychar;
case '!':
getNextChar();
if (next(0) == '=') {
getNextChar();
return OP_INT_NOTEQUAL;
}
return OP_BOOL_NEGATE;
case 's':
if (next(1) == '/') {
getNextChar();
getNextChar();
return OP_INT_SDIV;
}
else if (next(1) == '%') {
getNextChar();
getNextChar();
return OP_INT_SREM;
}
else if ((next(1)=='>')&&(next(2)=='>')) {
getNextChar();
getNextChar();
getNextChar();
return OP_INT_SRIGHT;
}
else if (next(1)=='<') {
getNextChar();
getNextChar();
if (next(0) == '=') {
getNextChar();
return OP_INT_SLESSEQUAL;
}
return OP_INT_SLESS;
}
return scanIdentifier();
case 'f':
if (next(1) == '+') {
getNextChar();
getNextChar();
return OP_FLOAT_ADD;
}
else if (next(1) == '-') {
getNextChar();
getNextChar();
return OP_FLOAT_SUB;
}
else if (next(1) == '*') {
getNextChar();
getNextChar();
return OP_FLOAT_MULT;
}
else if (next(1) == '/') {
getNextChar();
getNextChar();
return OP_FLOAT_DIV;
}
else if ((next(1) == '=')&&(next(2) == '=')) {
getNextChar();
getNextChar();
getNextChar();
return OP_FLOAT_EQUAL;
}
else if ((next(1) == '!')&&(next(2) == '=')) {
getNextChar();
getNextChar();
getNextChar();
return OP_FLOAT_NOTEQUAL;
}
else if (next(1) == '<') {
getNextChar();
getNextChar();
if (next(0) == '=') {
getNextChar();
return OP_FLOAT_LESSEQUAL;
}
return OP_FLOAT_LESS;
}
return -1;
case '+':
getNextChar();
return OP_INT_ADD;
case '*':
getNextChar();
return OP_INT_MULT;
case '/':
getNextChar();
return OP_INT_DIV;
case '%':
getNextChar();
return OP_INT_REM;
case '~':
getNextChar();
return OP_INT_NEGATE;
case '#':
if ((identlist[next(1)]&6)==4)
return scanIdentifier();
getNextChar();
ruleparselval.ch = (char)mychar; // Return '#' as single token
return mychar;
default:
return scanIdentifier();
}
}
return -1;
}
RuleLexer::RuleLexer(void)
{
initKeywords();
}
void RuleLexer::initialize(istream &t)
{
s = &t;
pos = 0;
endofstream = false;
lineno = 1;
getNextChar();
getNextChar();
getNextChar();
getNextChar(); // Fill lookahead buffer
}
RuleCompile::RuleCompile(void)
{
DummyTranslate dummy;
error_stream = (ostream *)0;
errors = 0;
finalrule = (ConstraintGroup *)0;
OpBehavior::registerInstructions(inst,&dummy);
}
RuleCompile::~RuleCompile(void)
{
if (finalrule != (ConstraintGroup *)0)
delete finalrule;
for(int4 i=0;i<inst.size();++i) {
OpBehavior *t_op = inst[i];
if (t_op != (OpBehavior *)0)
delete t_op;
}
}
void RuleCompile::ruleError(const char *s)
{
if (error_stream != (ostream *)0) {
*error_stream << "Error at line " << dec << lexer.getLineNo() << endl;
*error_stream << " " << s << endl;
}
errors += 1;
}
int4 RuleCompile::findIdentifier(string *nm)
{
int4 resid;
map<string,int4>::const_iterator iter;
iter = namemap.find(*nm);
if (iter == namemap.end()) {
resid = namemap.size();
namemap[*nm] = resid;
}
else
resid = (*iter).second;
delete nm;
return resid;
}
ConstraintGroup *RuleCompile::newOp(int4 id)
{
ConstraintGroup *res = new ConstraintGroup();
res->addConstraint(new DummyOpConstraint(id));
return res;
}
ConstraintGroup *RuleCompile::newVarnode(int4 id)
{
ConstraintGroup *res = new ConstraintGroup();
res->addConstraint(new DummyVarnodeConstraint(id));
return res;
}
ConstraintGroup *RuleCompile::newConst(int4 id)
{
ConstraintGroup *res = new ConstraintGroup();
res->addConstraint(new DummyConstConstraint(id));
return res;
}
ConstraintGroup *RuleCompile::opCopy(ConstraintGroup *base,int4 opid)
{
int4 opindex = base->getBaseIndex();
UnifyConstraint *newconstraint = new ConstraintOpCopy(opindex,opid);
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::opInput(ConstraintGroup *base,int8 *slot,int4 varid)
{
int4 ourslot = (int4) *slot;
delete slot;
int4 opindex = base->getBaseIndex();
UnifyConstraint *newconstraint = new ConstraintOpInput(opindex,varid,ourslot);
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::opInputAny(ConstraintGroup *base,int4 varid)
{
int4 opindex = base->getBaseIndex();
UnifyConstraint *newconstraint = new ConstraintOpInputAny(opindex,varid);
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::opInputConstVal(ConstraintGroup *base,int8 *slot,RHSConstant *val)
{
int4 ourslot = (int4) *slot;
delete slot;
int4 opindex = base->getBaseIndex();
UnifyConstraint *newconstraint;
ConstantAbsolute *myconst = dynamic_cast<ConstantAbsolute *>(val);
if (myconst != (ConstantAbsolute *)0) {
newconstraint = new ConstraintParamConstVal(opindex,ourslot,myconst->getVal());
}
else {
ConstantNamed *mynamed = dynamic_cast<ConstantNamed *>(val);
if (mynamed != (ConstantNamed *)0) {
newconstraint = new ConstraintParamConst(opindex,ourslot,mynamed->getId());
}
else {
ruleError("Can only use absolute constant here");
newconstraint = new ConstraintParamConstVal(opindex,ourslot,0);
}
}
delete val;
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::opOutput(ConstraintGroup *base,int4 varid)
{
int4 opindex = base->getBaseIndex();
UnifyConstraint *newconstraint = new ConstraintOpOutput(opindex,varid);
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::varCopy(ConstraintGroup *base,int4 varid)
{
int4 varindex = base->getBaseIndex();
UnifyConstraint *newconstraint = new ConstraintVarnodeCopy(varid,varindex);
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::varConst(ConstraintGroup *base,RHSConstant *ex,RHSConstant *sz)
{
int4 varindex = base->getBaseIndex();
UnifyConstraint *newconstraint = new ConstraintVarConst(varindex,ex,sz);
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::varDef(ConstraintGroup *base,int4 opid)
{
int4 varindex = base->getBaseIndex();
UnifyConstraint *newconstraint = new ConstraintDef(opid,varindex);
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::varDescend(ConstraintGroup *base,int4 opid)
{
int4 varindex = base->getBaseIndex();
UnifyConstraint *newconstraint = new ConstraintDescend(opid,varindex);
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::varUniqueDescend(ConstraintGroup *base,int4 opid)
{
int4 varindex = base->getBaseIndex();
UnifyConstraint *newconstraint = new ConstraintLoneDescend(opid,varindex);
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::opCodeConstraint(ConstraintGroup *base,vector<OpCode> *oplist)
{
if (oplist->size() != 1)
throw LowlevelError("Not currently supporting multiple opcode constraints");
int4 opindex = base->getBaseIndex();
UnifyConstraint *newconstraint = new ConstraintOpcode(opindex,*oplist);
delete oplist;
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::opCompareConstraint(ConstraintGroup *base,int4 opid,OpCode opc)
{
int4 op1index = base->getBaseIndex();
UnifyConstraint *newconstraint = new ConstraintOpCompare(op1index,opid,(opc==CPUI_INT_EQUAL));
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::varCompareConstraint(ConstraintGroup *base,int4 varid,OpCode opc)
{
int4 var1index = base->getBaseIndex();
UnifyConstraint *newconstraint = new ConstraintVarCompare(var1index,varid,(opc==CPUI_INT_EQUAL));
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::constCompareConstraint(ConstraintGroup *base,int4 constid,OpCode opc)
{
int4 const1index = base->getBaseIndex();
UnifyConstraint *newconstraint = new ConstraintConstCompare(const1index,constid,opc);
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::constNamedExpression(int4 id,RHSConstant *expr)
{
ConstraintGroup *res = new ConstraintGroup();
res->addConstraint(new ConstraintNamedExpression(id,expr));
return res;
}
ConstraintGroup *RuleCompile::emptyGroup(void)
{
return new ConstraintGroup();
}
ConstraintGroup *RuleCompile::emptyOrGroup(void)
{
return new ConstraintOr();
}
ConstraintGroup *RuleCompile::mergeGroups(ConstraintGroup *a,ConstraintGroup *b)
{
a->mergeIn(b);
return a;
}
ConstraintGroup *RuleCompile::addOr(ConstraintGroup *base,ConstraintGroup *newor)
{
base->addConstraint(newor);
return base;
}
ConstraintGroup *RuleCompile::opCreation(int4 newid,OpCode oc,bool iafter,int4 oldid)
{
OpBehavior *behave = inst[oc];
int4 numparms = behave->isUnary() ? 1 : 2;
UnifyConstraint *newconstraint = new ConstraintNewOp(newid,oldid,oc,iafter,numparms);
ConstraintGroup *res = new ConstraintGroup();
res->addConstraint(newconstraint);
return res;
}
ConstraintGroup *RuleCompile::newUniqueOut(ConstraintGroup *base,int4 varid,int4 sz)
{
UnifyConstraint *newconstraint = new ConstraintNewUniqueOut(base->getBaseIndex(),varid,sz);
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::newSetInput(ConstraintGroup *base,RHSConstant *slot,int4 varid)
{
UnifyConstraint *newconstraint = new ConstraintSetInput(base->getBaseIndex(),slot,varid);
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::newSetInputConstVal(ConstraintGroup *base,RHSConstant *slot,RHSConstant *val,RHSConstant *sz)
{
UnifyConstraint *newconstraint = new ConstraintSetInputConstVal(base->getBaseIndex(),slot,val,sz);
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::removeInput(ConstraintGroup *base,RHSConstant *slot)
{
UnifyConstraint *newconstraint = new ConstraintRemoveInput(base->getBaseIndex(),slot);
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::newSetOpcode(ConstraintGroup *base,OpCode opc)
{
int4 opid = base->getBaseIndex();
UnifyConstraint *newconstraint = new ConstraintSetOpcode(opid,opc);
base->addConstraint(newconstraint);
return base;
}
ConstraintGroup *RuleCompile::booleanConstraint(bool ist,RHSConstant *expr)
{
ConstraintGroup *base = new ConstraintGroup();
UnifyConstraint *newconstraint = new ConstraintBoolean(ist,expr);
base->addConstraint(newconstraint);
return base;
}
RHSConstant *RuleCompile::constNamed(int4 id)
{
RHSConstant *res = new ConstantNamed(id);
return res;
}
RHSConstant *RuleCompile::constAbsolute(int8 *val)
{
RHSConstant *res = new ConstantAbsolute(*val);
delete val;
return res;
}
RHSConstant *RuleCompile::constBinaryExpression(RHSConstant *ex1,OpCode opc,RHSConstant *ex2)
{
RHSConstant *res = new ConstantExpression( ex1, ex2, opc );
return res;
}
RHSConstant *RuleCompile::constVarnodeSize(int4 varindex)
{
RHSConstant *res = new ConstantVarnodeSize(varindex);
return res;
}
RHSConstant *RuleCompile::dotIdentifier(int4 id,string *str)
{
RHSConstant *res;
if ((*str) == "offset")
res = new ConstantOffset(id);
else if ((*str) == "size")
res = new ConstantVarnodeSize(id);
else if ((*str) == "isconstant")
res = new ConstantIsConstant(id);
else if ((*str) == "heritageknown")
res = new ConstantHeritageKnown(id);
else if ((*str) == "consume")
res = new ConstantConsumed(id);
else if ((*str) == "nzmask")
res = new ConstantNZMask(id);
else {
string errmsg = "Unknown variable attribute: " + *str;
ruleError(errmsg.c_str());
res = new ConstantAbsolute(0);
}
delete str;
return res;
}
void RuleCompile::run(istream &s,bool debug)
{
#ifdef YYDEBUG
ruleparsedebug = debug ? 1 : 0;
#endif
if (!s) {
if (error_stream != (ostream *)0)
*error_stream << "Bad input stream to rule compiler" << endl;
return;
}
errors = 0;
if (finalrule != (ConstraintGroup *)0) {
delete finalrule;
finalrule = (ConstraintGroup *)0;
}
lexer.initialize(s);
rulecompile = this; // Setup the global pointer
int4 parseres = ruleparseparse(); // Try to parse
if (parseres!=0) {
errors += 1;
if (error_stream != (ostream *)0)
*error_stream << "Parsing error" << endl;
}
if (errors!=0) {
if (error_stream != (ostream *)0)
*error_stream << "Parsing incomplete" << endl;
}
}
void RuleCompile::postProcess(void)
{
int4 id = 0;
finalrule->removeDummy();
finalrule->setId(id); // Set id for everybody
}
int4 RuleCompile::postProcessRule(vector<OpCode> &opcodelist)
{ // Do normal post processing but also remove initial opcode check
finalrule->removeDummy();
if (finalrule->numConstraints() == 0)
throw LowlevelError("Cannot postprocess empty rule");
ConstraintOpcode *subconst = dynamic_cast<ConstraintOpcode *>(finalrule->getConstraint(0));
if (subconst == (ConstraintOpcode *)0)
throw LowlevelError("Rule does not start with opcode constraint");
opcodelist = subconst->getOpCodes();
int4 opinit = subconst->getMaxNum();
finalrule->deleteConstraint(0);
int4 id = 0;
finalrule->setId(id);
return opinit;
}
ConstraintGroup *RuleCompile::buildUnifyer(const string &rule,const vector<string> &idlist,
vector<int4> &res)
{
RuleCompile ruler;
istringstream s(rule);
ruler.run(s,false);
if (ruler.numErrors() != 0)
throw LowlevelError("Could not build rule");
ConstraintGroup *resconst = ruler.releaseRule();
for(int4 i=0;i<idlist.size();++i) {
char initc;
int4 id = -1;
map<string,int4>::const_iterator iter;
if (idlist[i].size() != 0) {
initc = idlist[i][0];
if ((initc == 'o')||(initc == 'O')||(initc == 'v')||(initc == 'V')||(initc == '#')) {
iter = ruler.namemap.find(idlist[i]);
if (iter != ruler.namemap.end())
id = (*iter).second;
}
}
if (id == -1)
throw LowlevelError("Bad initializer name: "+idlist[i]);
res.push_back(id);
}
return resconst;
}
RuleGeneric::RuleGeneric(const string &g,const string &nm,const vector<OpCode> &sops,int4 opi,ConstraintGroup *c)
: Rule(g,0,nm), state(c)
{
starterops = sops;
opinit = opi;
constraint = c;
}
void RuleGeneric::getOpList(vector<uint4> &oplist) const
{
for(int4 i=0;i<starterops.size();++i)
oplist.push_back((uint4)starterops[i]);
}
int4 RuleGeneric::applyOp(PcodeOp *op,Funcdata &data)
{
state.setFunction(&data);
state.initialize(opinit,op);
constraint->initialize(state);
return constraint->step(state);
}
RuleGeneric *RuleGeneric::build(const string &nm,const string &gp,const string &content)
{
RuleCompile compiler;
istringstream s(content);
compiler.run(s,false);
if (compiler.numErrors() != 0)
throw LowlevelError("Unable to parse dynamic rule: "+nm);
vector<OpCode> opcodelist;
int4 opinit = compiler.postProcessRule(opcodelist);
RuleGeneric *res = new RuleGeneric(gp,nm,opcodelist,opinit,compiler.releaseRule());
return res;
}
} // End namespace ghidra
#endif
/*
Here is the original flex parser
%{
#include "rulecompile.hh"
#include "ruleparse.hh"
#define ruleparsewrap() 1
#define YY_SKIP_YYWRAP
extern RuleCompile *rulecompile;
int4 scan_number(char *numtext,YYSTYPE *lval)
{
istringstream s(numtext);
s.unsetf(ios::dec | ios::hex | ios::oct);
uintb val;
s >> val;
if (!s)
return BADINTEGER;
lval->big = new intb(val);
return INTB;
}
int4 find_op_identifier(void)
{
string ident(yytext);
ruleparselval.id = rulecompile->findOpIdentifier(ident);
return OP_IDENTIFIER;
}
int4 find_var_identifier(void)
{
string ident(yytext);
ruleparselval.id = rulecompile->findVarIdentifier(ident);
return VAR_IDENTIFIER;
}
int4 find_const_identifier(void)
{
string ident(yytext);
ruleparselval.id = rulecompile->findConstIdentifier(ident);
return CONST_IDENTIFIER;
}
%}
%%
[(),\[\];\{\}\#] { ruleparselval.ch = yytext[0]; return yytext[0]; }
[0-9]+ { return scan_number(yytext,&ruleparselval); }
0x[0-9a-fA-F]+ { return scan_number(yytext,&ruleparselval); }
[\r\ \t\v]+
\n { rulecompile->nextLine(); }
\-\> { return RIGHT_ARROW; }
\<\- { return LEFT_ARROW; }
\|\| { return OP_BOOL_OR; }
\&\& { return OP_BOOL_AND; }
\^\^ { return OP_BOOL_XOR; }
\>\> { return OP_INT_RIGHT; }
\<\< { return OP_INT_LEFT; }
\=\= { return OP_INT_EQUAL; }
\!\= { return OP_INT_NOTEQUAL; }
\<\= { return OP_INT_LESSEQUAL; }
s\/ { return OP_INT_SDIV; }
s\% { return OP_INT_SREM; }
s\>\> { return OP_INT_SRIGHT; }
s\< { return OP_INT_SLESS; }
s\<\= { return OP_INT_SLESSEQUAL; }
f\+ { return OP_FLOAT_ADD; }
f\- { return OP_FLOAT_SUB; }
f\* { return OP_FLOAT_MULT; }
f\/ { return OP_FLOAT_DIV; }
f\=\= { return OP_FLOAT_EQUAL; }
f\!\= { return OP_FLOAT_NOTEQUAL; }
f\< { return OP_FLOAT_LESS; }
f\<\= { return OP_FLOAT_LESSEQUAL; }
ZEXT { return OP_INT_ZEXT; }
CARRY { return OP_INT_CARRY; }
SEXT { return OP_INT_SEXT; }
SCARRY { return OP_INT_SCARRY; }
SBORROW { return OP_INT_SBORROW; }
NAN { return OP_FLOAT_NAN; }
ABS { return OP_FLOAT_ABS; }
SQRT { return OP_FLOAT_SQRT; }
CEIL { return OP_FLOAT_CEIL; }
FLOOR { return OP_FLOAT_FLOOR; }
ROUND { return OP_FLOAT_ROUND; }
INT2FLOAT { return OP_FLOAT_INT2FLOAT; }
FLOAT2FLOAT { return OP_FLOAT_FLOAT2FLOAT; }
TRUNC { return OP_FLOAT_TRUNC; }
GOTO { return OP_BRANCH; }
GOTOIND { return OP_BRANCHIND; }
CALL { return OP_CALL; }
CALLIND { return OP_CALLIND; }
RETURN { return OP_RETURN; }
CBRRANCH { return OP_CBRANCH; }
USEROP { return OP_CALLOTHER; }
LOAD { return OP_LOAD; }
STORE { return OP_STORE; }
CONCAT { return OP_PIECE; }
SUBPIECE { return OP_SUBPIECE; }
\+ { return OP_INT_ADD; }
\- { return OP_INT_SUB; }
\! { return OP_BOOL_NEGATE; }