forked from NationalSecurityAgency/ghidra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslgh_compile.cc
3889 lines (3528 loc) · 132 KB
/
slgh_compile.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.
*/
#include "slgh_compile.hh"
#include "filemanage.hh"
#include <csignal>
extern FILE *sleighin; // Global pointer to file for lexer
extern int sleighlex_destroy(void);
namespace ghidra {
SleighCompile *slgh; // Global pointer to sleigh object for use with parser
#ifdef YYDEBUG
extern int sleighdebug; // Global debugging state for parser
#endif
extern int sleighparse(void);
/// This must be constructed with the \e main section of p-code, which can contain no p-code
/// \param rtl is the \e main section of p-code
/// \param scope is the symbol scope associated with the section
SectionVector::SectionVector(ConstructTpl *rtl,SymbolScope *scope)
{
nextindex = -1;
main.section = rtl;
main.scope = scope;
}
/// Associate the new section with \b nextindex, established prior to parsing
/// \param rtl is the \e named section of p-code
/// \param scope is the associated symbol scope
void SectionVector::append(ConstructTpl *rtl,SymbolScope *scope)
{
while(named.size() <= nextindex)
named.emplace_back();
named[ nextindex ] = RtlPair(rtl,scope);
}
/// Construct with the default qualities for an address space, which
/// can then be overridden with further parsing.
/// \param nm is the name of the address space
SpaceQuality::SpaceQuality(const string &nm)
{
name = nm;
type = ramtype;
size = 0;
wordsize = 1;
isdefault = false;
}
/// Establish default qualities for the field, which can then be overridden
/// by further parsing. A name and bit range must always be explicitly given.
/// \param nm is the parsed name for the field
/// \param l is the parsed lower bound of the bit range
/// \param h is the parse upper bound of the bit range
FieldQuality::FieldQuality(string *nm,uintb *l,uintb *h)
{
name = *nm;
low = *l;
high = *h;
signext = false;
flow = true;
hex = true;
delete nm;
delete l;
delete h;
}
/// Establish each component of the \b with block header
/// \param s is the subtable (or null)
/// \param pq is the pattern to prepend (or null)
/// \param cvec is the set of context changes (or null)
void WithBlock::set(SubtableSymbol *s, PatternEquation *pq, vector<ContextChange *> *cvec)
{
ss = s;
pateq = pq;
if (pateq != (PatternEquation *)0)
pateq->layClaim();
if (cvec != (vector<ContextChange *> *)0) {
for(int4 i=0;i<cvec->size();++i)
contvec.push_back((*cvec)[i]); // Lay claim to -cvec-s pointers, we don't clone
delete cvec;
}
}
WithBlock::~WithBlock(void)
{
if (pateq != (PatternEquation *)0)
PatternEquation::release(pateq);
for(int4 i=0;i<contvec.size();++i) {
delete contvec[i];
}
}
/// \brief Build a complete pattern equation from any surrounding \b with blocks
///
/// Given the pattern equation parsed locally from a Constructor and the stack of
/// surrounding \b with blocks, create the final pattern equation for the Constructor.
/// Each \b with block pattern is preprended to the local pattern.
/// \param stack is the stack of \b with blocks currently active at the Constructor
/// \param pateq is the pattern equation parsed from the local Constructor statement
/// \return the final pattern equation
PatternEquation *WithBlock::collectAndPrependPattern(const list<WithBlock> &stack, PatternEquation *pateq)
{
list<WithBlock>::const_iterator iter;
for(iter=stack.begin();iter!=stack.end();++iter) {
PatternEquation *witheq = (*iter).pateq;
if (witheq != (PatternEquation *)0)
pateq = new EquationAnd(witheq, pateq);
}
return pateq;
}
/// \brief Build a complete array of context changes from any surrounding \b with blocks
///
/// Given a list of ContextChanges parsed locally from a Constructor and the stack of
/// surrounding \b with blocks, make a new list of ContextChanges, prepending everything from
/// the stack to the local vector. Return the new list and delete the old.
/// \param stack is the current \b with block stack
/// \param contvec is the local list of ContextChanges (or null)
/// \return the new list of ContextChanges
vector<ContextChange *> *WithBlock::collectAndPrependContext(const list<WithBlock> &stack, vector<ContextChange *> *contvec)
{
vector<ContextChange *> *res = (vector<ContextChange *> *)0;
list<WithBlock>::const_iterator iter;
for(iter=stack.begin();iter!=stack.end();++iter) {
const vector<ContextChange *> &changelist( (*iter).contvec );
if (changelist.size() == 0) continue;
if (res == (vector<ContextChange *> *)0)
res = new vector<ContextChange *>();
for(int4 i=0;i<changelist.size();++i) {
res->push_back(changelist[i]->clone());
}
}
if (contvec != (vector<ContextChange *> *)0) {
if (contvec->size() != 0) {
if (res == (vector<ContextChange *> *)0)
res = new vector<ContextChange *>();
for(int4 i=0;i<contvec->size();++i)
res->push_back((*contvec)[i]); // lay claim to contvecs pointer
}
delete contvec;
}
return res;
}
/// \brief Get the active subtable from the stack of currently active \b with blocks
///
/// Find the subtable associated with the innermost \b with block and return it.
/// \param stack is the stack of currently active \b with blocks
/// \return the innermost subtable (or null)
SubtableSymbol *WithBlock::getCurrentSubtable(const list<WithBlock> &stack)
{
list<WithBlock>::const_iterator iter;
for(iter=stack.begin();iter!=stack.end();++iter) {
if ((*iter).ss != (SubtableSymbol *)0)
return (*iter).ss;
}
return (SubtableSymbol *)0;
}
/// \brief Construct the consistency checker and optimizer
///
/// \param sleigh is the parsed SLEIGH spec
/// \param rt is the root subtable of the SLEIGH spec
/// \param un is \b true to request "Unnecessary extension" warnings
/// \param warndead is \b true to request warnings for written but not read temporaries
/// \param warnlargetemp is \b true to request warnings for temporaries that are too large
ConsistencyChecker::ConsistencyChecker(SleighCompile *sleigh,SubtableSymbol *rt,bool un,bool warndead, bool warnlargetemp)
{
compiler = sleigh;
root_symbol = rt;
unnecessarypcode = 0;
readnowrite = 0;
writenoread = 0;
largetemp = 0; ///<Number of constructors using at least one temporary varnode larger than SleighBase::MAX_UNIQUE_SIZE
printextwarning = un;
printdeadwarning = warndead;
printlargetempwarning = warnlargetemp; ///< If true, prints a warning about each constructor using a temporary varnode larger than SleighBase::MAX_UNIQUE_SIZE
}
/// \brief Recover a specific value for the size associated with a Varnode template
///
/// This method is passed a ConstTpl that is assumed to be the \e size attribute of
/// a VarnodeTpl (as returned by getSize()). This method recovers the specific
/// integer value for this constant template or throws an exception.
/// The integer value can either be immediately available from parsing, derived
/// from a Constructor operand symbol whose size is known, or taken from
/// the calculated export size of a subtable symbol.
/// \param sizeconst is the Varnode size template
/// \param ct is the Constructor containing the Varnode
/// \return the integer value
int4 ConsistencyChecker::recoverSize(const ConstTpl &sizeconst,Constructor *ct)
{
int4 size,handindex;
OperandSymbol *opsym;
SubtableSymbol *tabsym;
map<SubtableSymbol *,int4>::const_iterator iter;
switch(sizeconst.getType()) {
case ConstTpl::real:
size = (int4) sizeconst.getReal();
break;
case ConstTpl::handle:
handindex = sizeconst.getHandleIndex();
opsym = ct->getOperand(handindex);
size = opsym->getSize();
if (size == -1) {
tabsym = dynamic_cast<SubtableSymbol *>(opsym->getDefiningSymbol());
if (tabsym == (SubtableSymbol *)0)
throw SleighError("Could not recover varnode template size");
iter = sizemap.find(tabsym);
if (iter == sizemap.end())
throw SleighError("Subtable out of order");
size = (*iter).second;
}
break;
default:
throw SleighError("Bad constant type as varnode template size");
}
return size;
}
/// \brief Convert an unnecessary CPUI_INT_ZEXT and CPUI_INT_SEXT into a COPY
///
/// SLEIGH allows \b zext and \b sext notation even if the input and output
/// Varnodes are ultimately the same size. In this case, a warning may be
/// issued and the operator is converted to a CPUI_COPY.
/// \param op is the given CPUI_INT_ZEXT or CPUI_INT_SEXT operator to check
/// \param ct is the Constructor containing the operator
void ConsistencyChecker::dealWithUnnecessaryExt(OpTpl *op,Constructor *ct)
{
if (printextwarning) {
ostringstream msg;
msg << "Unnecessary ";
printOpName(msg,op);
compiler->reportWarning(compiler->getLocation(ct), msg.str());
}
op->setOpcode(CPUI_COPY); // Equivalent to copy
unnecessarypcode += 1;
}
/// \brief Convert an unnecessary CPUI_SUBPIECE into a COPY
///
/// SLEIGH allows truncation notation even if the input and output Varnodes are
/// ultimately the same size. In this case, a warning may be issued and the operator
/// is converted to a CPUI_COPY.
/// \param op is the given CPUI_SUBPIECE operator
/// \param ct is the containing Constructor
void ConsistencyChecker::dealWithUnnecessaryTrunc(OpTpl *op,Constructor *ct)
{
if (printextwarning) {
ostringstream msg;
msg << "Unnecessary ";
printOpName(msg,op);
compiler->reportWarning(compiler->getLocation(ct), msg.str());
}
op->setOpcode(CPUI_COPY); // Equivalent to copy
op->removeInput(1);
unnecessarypcode += 1;
}
/// \brief Check for misuse of the given operator and print a warning
///
/// This method currently checks for:
/// - Unsigned less-than comparison with zero
///
/// \param op is the given operator
/// \param ct is the Constructor owning the operator
/// \return \b false if the operator is fatally misused
bool ConsistencyChecker::checkOpMisuse(OpTpl *op,Constructor *ct)
{
switch(op->getOpcode()) {
case CPUI_INT_LESS:
{
VarnodeTpl *vn = op->getIn(1);
if (vn->getSpace().isConstSpace() && vn->getOffset().isZero()) {
compiler->reportWarning(compiler->getLocation(ct), "Unsigned comparison with zero is always false");
}
}
break;
default:
break;
}
return true;
}
/// \brief Make sure the given operator meets size restrictions
///
/// Many SLEIGH operators require that inputs and/or outputs are the
/// same size, or they have other specific size requirement.
/// Print an error and return \b false for any violations.
/// \param op is the given p-code operator
/// \param ct is the Constructor owning the operator
/// \return \b true if there are no size restriction violations
bool ConsistencyChecker::sizeRestriction(OpTpl *op,Constructor *ct)
{ // Make sure op template meets size restrictions
// Return false and any info about mismatched sizes
int4 vnout,vn0,vn1;
AddrSpace *spc;
switch(op->getOpcode()) {
case CPUI_COPY: // Instructions where all inputs and output are same size
case CPUI_INT_2COMP:
case CPUI_INT_NEGATE:
case CPUI_FLOAT_NEG:
case CPUI_FLOAT_ABS:
case CPUI_FLOAT_SQRT:
case CPUI_FLOAT_CEIL:
case CPUI_FLOAT_FLOOR:
case CPUI_FLOAT_ROUND:
vnout = recoverSize(op->getOut()->getSize(),ct);
if (vnout == -1) {
printOpError(op,ct,-1,-1,"Using subtable with exports in expression");
return false;
}
vn0 = recoverSize(op->getIn(0)->getSize(),ct);
if (vn0 == -1) {
printOpError(op,ct,0,0,"Using subtable with exports in expression");
return false;
}
if (vnout == vn0) return true;
if ((vnout==0)||(vn0==0)) return true;
printOpError(op,ct,-1,0,"Input and output sizes must match");
return false;
case CPUI_INT_ADD:
case CPUI_INT_SUB:
case CPUI_INT_XOR:
case CPUI_INT_AND:
case CPUI_INT_OR:
case CPUI_INT_MULT:
case CPUI_INT_DIV:
case CPUI_INT_SDIV:
case CPUI_INT_REM:
case CPUI_INT_SREM:
case CPUI_FLOAT_ADD:
case CPUI_FLOAT_DIV:
case CPUI_FLOAT_MULT:
case CPUI_FLOAT_SUB:
vnout = recoverSize(op->getOut()->getSize(),ct);
if (vnout == -1) {
printOpError(op,ct,-1,-1,"Using subtable with exports in expression");
return false;
}
vn0 = recoverSize(op->getIn(0)->getSize(),ct);
if (vn0 == -1) {
printOpError(op,ct,0,0,"Using subtable with exports in expression");
return false;
}
vn1 = recoverSize(op->getIn(1)->getSize(),ct);
if (vn1 == -1) {
printOpError(op,ct,1,1,"Using subtable with exports in expression");
return false;
}
if ((vnout!=0)&&(vn0!=0)&&(vnout!=vn0)) {
printOpError(op,ct,-1,0,"The output and all input sizes must match");
return false;
}
if ((vnout!=0)&&(vn1!=0)&&(vnout!=vn1)) {
printOpError(op,ct,-1,1,"The output and all input sizes must match");
return false;
}
if ((vn0!=0)&&(vn1!=0)&&(vn0!=vn1)) {
printOpError(op,ct,0,1,"The output and all input sizes must match");
return false;
}
return true;
case CPUI_FLOAT_NAN:
vnout = recoverSize(op->getOut()->getSize(),ct);
if (vnout == -1) {
printOpError(op,ct,-1,-1,"Using subtable with exports in expression");
return false;
}
if (vnout != 1) {
printOpError(op,ct,-1,-1,"Output must be a boolean (size 1)");
return false;
}
break;
case CPUI_INT_EQUAL: // Instructions with bool output, all inputs equal size
case CPUI_INT_NOTEQUAL:
case CPUI_INT_SLESS:
case CPUI_INT_SLESSEQUAL:
case CPUI_INT_LESS:
case CPUI_INT_LESSEQUAL:
case CPUI_INT_CARRY:
case CPUI_INT_SCARRY:
case CPUI_INT_SBORROW:
case CPUI_FLOAT_EQUAL:
case CPUI_FLOAT_NOTEQUAL:
case CPUI_FLOAT_LESS:
case CPUI_FLOAT_LESSEQUAL:
vnout = recoverSize(op->getOut()->getSize(),ct);
if (vnout == -1) {
printOpError(op,ct,-1,-1,"Using subtable with exports in expression");
return false;
}
if (vnout != 1) {
printOpError(op,ct,-1,-1,"Output must be a boolean (size 1)");
return false;
}
vn0 = recoverSize(op->getIn(0)->getSize(),ct);
if (vn0 == -1) {
printOpError(op,ct,0,0,"Using subtable with exports in expression");
return false;
}
vn1 = recoverSize(op->getIn(1)->getSize(),ct);
if (vn1 == -1) {
printOpError(op,ct,1,1,"Using subtable with exports in expression");
return false;
}
if ((vn0==0)||(vn1==0)) return true;
if (vn0 != vn1) {
printOpError(op,ct,0,1,"Inputs must be the same size");
return false;
}
return true;
case CPUI_BOOL_XOR:
case CPUI_BOOL_AND:
case CPUI_BOOL_OR:
vnout = recoverSize(op->getOut()->getSize(),ct);
if (vnout == -1) {
printOpError(op,ct,-1,-1,"Using subtable with exports in expression");
return false;
}
if (vnout != 1) {
printOpError(op,ct,-1,-1,"Output must be a boolean (size 1)");
return false;
}
vn0 = recoverSize(op->getIn(0)->getSize(),ct);
if (vn0 == -1) {
printOpError(op,ct,0,0,"Using subtable with exports in expression");
return false;
}
if (vn0 != 1) {
printOpError(op,ct,0,0,"Input must be a boolean (size 1)");
return false;
}
return true;
case CPUI_BOOL_NEGATE:
vnout = recoverSize(op->getOut()->getSize(),ct);
if (vnout == -1) {
printOpError(op,ct,-1,-1,"Using subtable with exports in expression");
return false;
}
if (vnout != 1) {
printOpError(op,ct,-1,-1,"Output must be a boolean (size 1)");
return false;
}
vn0 = recoverSize(op->getIn(0)->getSize(),ct);
if (vn0 == -1) {
printOpError(op,ct,0,0,"Using subtable with exports in expression");
return false;
}
if (vn0 != 1) {
printOpError(op,ct,0,0,"Input must be a boolean (size 1)");
return false;
}
return true;
// The shift amount does not necessarily have to be the same size
// But the output and first parameter must be same size
case CPUI_INT_LEFT:
case CPUI_INT_RIGHT:
case CPUI_INT_SRIGHT:
vnout = recoverSize(op->getOut()->getSize(),ct);
if (vnout == -1) {
printOpError(op,ct,-1,-1,"Using subtable with exports in expression");
return false;
}
vn0 = recoverSize(op->getIn(0)->getSize(),ct);
if (vn0 == -1) {
printOpError(op,ct,0,0,"Using subtable with exports in expression");
return false;
}
if ((vnout==0)||(vn0==0)) return true;
if (vnout != vn0) {
printOpError(op,ct,-1,0,"Output and first input must be the same size");
return false;
}
return true;
case CPUI_INT_ZEXT:
case CPUI_INT_SEXT:
vnout = recoverSize(op->getOut()->getSize(),ct);
if (vnout == -1) {
printOpError(op,ct,-1,-1,"Using subtable with exports in expression");
return false;
}
vn0 = recoverSize(op->getIn(0)->getSize(),ct);
if (vn0 == -1) {
printOpError(op,ct,0,0,"Using subtable with exports in expression");
return false;
}
if ((vnout==0)||(vn0==0)) return true;
if (vnout == vn0) {
dealWithUnnecessaryExt(op,ct);
return true;
}
else if (vnout < vn0) {
printOpError(op,ct,-1,0,"Output size must be strictly bigger than input size");
return false;
}
return true;
case CPUI_CBRANCH:
vn1 = recoverSize(op->getIn(1)->getSize(),ct);
if (vn1 == -1) {
printOpError(op,ct,1,1,"Using subtable with exports in expression");
return false;
}
if (vn1 != 1) {
printOpError(op,ct,1,1,"Input must be a boolean (size 1)");
return false;
}
return true;
case CPUI_LOAD:
case CPUI_STORE:
if (op->getIn(0)->getOffset().getType() != ConstTpl::spaceid)
return true;
spc = op->getIn(0)->getOffset().getSpace();
vn1 = recoverSize(op->getIn(1)->getSize(),ct);
if (vn1 == -1) {
printOpError(op,ct,1,1,"Using subtable with exports in expression");
return false;
}
if ((vn1!=0)&&(vn1 != spc->getAddrSize())) {
printOpError(op,ct,1,1,"Pointer size must match size of space");
return false;
}
return true;
case CPUI_SUBPIECE:
vnout = recoverSize(op->getOut()->getSize(),ct);
if (vnout == -1) {
printOpError(op,ct,-1,-1,"Using subtable with exports in expression");
return false;
}
vn0 = recoverSize(op->getIn(0)->getSize(),ct);
if (vn0 == -1) {
printOpError(op,ct,0,0,"Using subtable with exports in expression");
return false;
}
vn1 = op->getIn(1)->getOffset().getReal();
if ((vnout==0)||(vn0==0)) return true;
if ((vnout==vn0)&&(vn1==0)) { // No actual truncation is occuring
dealWithUnnecessaryTrunc(op,ct);
return true;
}
else if (vnout>=vn0) {
printOpError(op,ct,-1,0,"Output must be strictly smaller than input");
return false;
}
if (vnout>vn0-vn1) {
printOpError(op,ct,-1,0,"Too much truncation");
return false;
}
return true;
default:
break;
}
return true;
}
/// \brief Print the name of a p-code operator (for warning and error messages)
///
/// Print the full name of the operator with its syntax token in parentheses.
/// \param s is the output stream to write to
/// \param op is the operator to print
void ConsistencyChecker::printOpName(ostream &s,OpTpl *op)
{
switch(op->getOpcode()) {
case CPUI_COPY:
s << "Copy(=)";
break;
case CPUI_LOAD:
s << "Load(*)";
break;
case CPUI_STORE:
s << "Store(*)";
break;
case CPUI_BRANCH:
s << "Branch(goto)";
break;
case CPUI_CBRANCH:
s << "Conditional branch(if)";
break;
case CPUI_BRANCHIND:
s << "Indirect branch(goto[])";
break;
case CPUI_CALL:
s << "Call";
break;
case CPUI_CALLIND:
s << "Indirect Call";
break;
case CPUI_CALLOTHER:
s << "User defined";
break;
case CPUI_RETURN:
s << "Return";
break;
case CPUI_INT_EQUAL:
s << "Equality(==)";
break;
case CPUI_INT_NOTEQUAL:
s << "Notequal(!=)";
break;
case CPUI_INT_SLESS:
s << "Signed less than(s<)";
break;
case CPUI_INT_SLESSEQUAL:
s << "Signed less than or equal(s<=)";
break;
case CPUI_INT_LESS:
s << "Less than(<)";
break;
case CPUI_INT_LESSEQUAL:
s << "Less than or equal(<=)";
break;
case CPUI_INT_ZEXT:
s << "Zero extension(zext)";
break;
case CPUI_INT_SEXT:
s << "Signed extension(sext)";
break;
case CPUI_INT_ADD:
s << "Addition(+)";
break;
case CPUI_INT_SUB:
s << "Subtraction(-)";
break;
case CPUI_INT_CARRY:
s << "Carry";
break;
case CPUI_INT_SCARRY:
s << "Signed carry";
break;
case CPUI_INT_SBORROW:
s << "Signed borrow";
break;
case CPUI_INT_2COMP:
s << "Twos complement(-)";
break;
case CPUI_INT_NEGATE:
s << "Negate(~)";
break;
case CPUI_INT_XOR:
s << "Exclusive or(^)";
break;
case CPUI_INT_AND:
s << "And(&)";
break;
case CPUI_INT_OR:
s << "Or(|)";
break;
case CPUI_INT_LEFT:
s << "Left shift(<<)";
break;
case CPUI_INT_RIGHT:
s << "Right shift(>>)";
break;
case CPUI_INT_SRIGHT:
s << "Signed right shift(s>>)";
break;
case CPUI_INT_MULT:
s << "Multiplication(*)";
break;
case CPUI_INT_DIV:
s << "Division(/)";
break;
case CPUI_INT_SDIV:
s << "Signed division(s/)";
break;
case CPUI_INT_REM:
s << "Remainder(%)";
break;
case CPUI_INT_SREM:
s << "Signed remainder(s%)";
break;
case CPUI_BOOL_NEGATE:
s << "Boolean negate(!)";
break;
case CPUI_BOOL_XOR:
s << "Boolean xor(^^)";
break;
case CPUI_BOOL_AND:
s << "Boolean and(&&)";
break;
case CPUI_BOOL_OR:
s << "Boolean or(||)";
break;
case CPUI_FLOAT_EQUAL:
s << "Float equal(f==)";
break;
case CPUI_FLOAT_NOTEQUAL:
s << "Float notequal(f!=)";
break;
case CPUI_FLOAT_LESS:
s << "Float less than(f<)";
break;
case CPUI_FLOAT_LESSEQUAL:
s << "Float less than or equal(f<=)";
break;
case CPUI_FLOAT_NAN:
s << "Not a number(nan)";
break;
case CPUI_FLOAT_ADD:
s << "Float addition(f+)";
break;
case CPUI_FLOAT_DIV:
s << "Float division(f/)";
break;
case CPUI_FLOAT_MULT:
s << "Float multiplication(f*)";
break;
case CPUI_FLOAT_SUB:
s << "Float subtractions(f-)";
break;
case CPUI_FLOAT_NEG:
s << "Float minus(f-)";
break;
case CPUI_FLOAT_ABS:
s << "Absolute value(abs)";
break;
case CPUI_FLOAT_SQRT:
s << "Square root";
break;
case CPUI_FLOAT_INT2FLOAT:
s << "Integer to float conversion(int2float)";
break;
case CPUI_FLOAT_FLOAT2FLOAT:
s << "Float to float conversion(float2float)";
break;
case CPUI_FLOAT_TRUNC:
s << "Float truncation(trunc)";
break;
case CPUI_FLOAT_CEIL:
s << "Ceiling(ceil)";
break;
case CPUI_FLOAT_FLOOR:
s << "Floor";
break;
case CPUI_FLOAT_ROUND:
s << "Round";
break;
case CPUI_MULTIEQUAL:
s << "Build";
break;
case CPUI_INDIRECT:
s << "Delay";
break;
case CPUI_SUBPIECE:
s << "Truncation(:)";
break;
case CPUI_SEGMENTOP:
s << "Segment table(segment)";
break;
case CPUI_CPOOLREF:
s << "Constant Pool(cpool)";
break;
case CPUI_NEW:
s << "New object(newobject)";
break;
case CPUI_POPCOUNT:
s << "Count bits(popcount)";
break;
case CPUI_LZCOUNT:
s << "Count leading zero bits(lzcount)";
break;
default:
break;
}
}
/// \brief Get the OperandSymbol associated with an input/output Varnode of the given p-code operator
///
/// Find the Constructor operand associated with a specified Varnode, if it exists.
/// The Varnode is specified by the p-code operator using it and the input \e slot index, with -1
/// indicating the operator's output Varnode. Not all Varnode's are associated with a
/// Constructor operand, in which case \e null is returned.
/// \param slot is the input \e slot index, or -1 for an output Varnode
/// \param op is the p-code operator using the Varnode
/// \param ct is the Constructor containing the p-code and operands
/// \return the associated operand or null
OperandSymbol *ConsistencyChecker::getOperandSymbol(int4 slot,OpTpl *op,Constructor *ct)
{
VarnodeTpl *vn;
OperandSymbol *opsym;
int4 handindex;
if (slot == -1)
vn = op->getOut();
else
vn = op->getIn(slot);
switch(vn->getSize().getType()) {
case ConstTpl::handle:
handindex = vn->getSize().getHandleIndex();
opsym = ct->getOperand(handindex);
break;
default:
opsym = (OperandSymbol *)0;
break;
}
return opsym;
}
/// \brief Print an error message describing a size restriction violation
///
/// The given p-code operator is assumed to violate the Varnode size rules for its opcode.
/// If the violation is for two Varnodes that should be the same size, each Varnode is indicated
/// as an input \e slot index, where -1 indicates the operator's output Varnode.
/// If the violation is for a single Varnode, its \e slot index is passed in twice.
/// \param op is the given p-code operator
/// \param ct is the containing Constructor
/// \param err1 is the slot of the first violating Varnode
/// \param err2 is the slot of the second violating Varnode (or equal to \b err1)
/// \param msg is additional description that is appended to the error message
void ConsistencyChecker::printOpError(OpTpl *op,Constructor *ct,int4 err1,int4 err2,const string &msg)
{
SubtableSymbol *sym = ct->getParent();
OperandSymbol *op1,*op2;
op1 = getOperandSymbol(err1,op,ct);
if (err2 != err1)
op2 = getOperandSymbol(err2,op,ct);
else
op2 = (OperandSymbol *)0;
ostringstream msgBuilder;
msgBuilder << "Size restriction error in table '" << sym->getName() << "'" << endl;
if ((op1 != (OperandSymbol *)0)&&(op2 != (OperandSymbol *)0))
msgBuilder << " Problem with operands '" << op1->getName() << "' and '" << op2->getName() << "'";
else if (op1 != (OperandSymbol *)0)
msgBuilder << " Problem with operand 1 '" << op1->getName() << "'";
else if (op2 != (OperandSymbol *)0)
msgBuilder << " Problem with operand 2 '" << op2->getName() << "'";
else
msgBuilder << " Problem";
msgBuilder << " in ";
printOpName(msgBuilder,op);
msgBuilder << " operator" << endl << " " << msg;
compiler->reportError(compiler->getLocation(ct), msgBuilder.str());
}
/// \brief Check all p-code operators within a given Constructor section for misuse and size consistency
///
/// Each operator within the section is checked in turn, and warning and error messages are emitted
/// if necessary. The method returns \b false if there is a fatal error associated with any
/// operator.
/// \param ct is the Constructor to check
/// \param cttpl is the specific p-code section to check
/// \return \b true if there are no fatal errors in the section
bool ConsistencyChecker::checkConstructorSection(Constructor *ct,ConstructTpl *cttpl)
{
if (cttpl == (ConstructTpl *)0)
return true; // Nothing to check
vector<OpTpl *>::const_iterator iter;
const vector<OpTpl *> &ops(cttpl->getOpvec());
bool testresult = true;
for(iter=ops.begin();iter!=ops.end();++iter) {
if (!sizeRestriction(*iter,ct))
testresult = false;
if (!checkOpMisuse(*iter,ct))
testresult = false;
}
return testresult;
}
/// \brief Check the given p-code operator for too large temporary registers
///
/// Return \b true if the output or one of the inputs to the operator
/// is in the \e unique space and larger than SleighBase::MAX_UNIQUE_SIZE
/// \param op is the given operator
/// \return \b true if the operator has a too large temporary parameter
bool ConsistencyChecker::hasLargeTemporary(OpTpl *op)
{
VarnodeTpl *out = op->getOut();
if ((out != (VarnodeTpl*)0x0) && isTemporaryAndTooBig(out)) {
return true;
}
for(int4 i = 0;i < op->numInput();++i) {
VarnodeTpl *in = op->getIn(i);
if (isTemporaryAndTooBig(in)) {
return true;
}
}
return false;
}
/// \brief Check if the given Varnode is a too large temporary register
///
/// Return \b true precisely when the Varnode is in the \e unique space and
/// has size larger than SleighBase::MAX_UNIQUE_SIZE
/// \param vn is the given Varnode
/// \return \b true if the Varnode is a too large temporary register
bool ConsistencyChecker::isTemporaryAndTooBig(VarnodeTpl *vn)
{
return vn->getSpace().isUniqueSpace() && (vn->getSize().getReal() > SleighBase::MAX_UNIQUE_SIZE);
}
/// \brief Resolve the offset of the given \b truncated Varnode
///
/// SLEIGH allows a Varnode to be derived from another larger Varnode using
/// truncation or bit range notation. The final offset of the truncated Varnode may not
/// be calculable immediately during parsing, especially if the address space is big endian
/// and the size of the containing Varnode is not immediately known.
/// This method recovers the final offset of the truncated Varnode now that all sizes are
/// known and otherwise checks that the truncation expression is valid.
/// \param ct is the Constructor containing the Varnode
/// \param slot is the \e slot index of the truncated Varnode (for error messages)
/// \param op is the operator using the truncated Varnode (for error messages)
/// \param vn is the given truncated Varnode
/// \param isbigendian is \b true if the Varnode is in a big endian address space
/// \return \b true if the truncation expression was valid
bool ConsistencyChecker::checkVarnodeTruncation(Constructor *ct,int4 slot,
OpTpl *op,VarnodeTpl *vn,bool isbigendian)
{
const ConstTpl &off( vn->getOffset() );
if (off.getType() != ConstTpl::handle) return true;
if (off.getSelect() != ConstTpl::v_offset_plus) return true;
ConstTpl::const_type sztype = vn->getSize().getType();
if ((sztype != ConstTpl::real)&&(sztype != ConstTpl::handle)) {
printOpError(op,ct,slot,slot,"Bad truncation expression");
return false;
}
int4 sz = recoverSize(off,ct); // Recover the size of the original operand
if (sz <= 0) {
printOpError(op,ct,slot,slot,"Could not recover size");
return false;
}
bool res = vn->adjustTruncation(sz,isbigendian);
if (!res) {
printOpError(op,ct,slot,slot,"Truncation operator out of bounds");
return false;
}
return true;
}
/// \brief Check and adjust truncated Varnodes in the given Constructor p-code section
///
/// Run through all Varnodes looking for offset templates marked as ConstTpl::v_offset_plus,
/// which indicates they were constructed using truncation notation. These truncation expressions
/// are checked for validity and adjusted depending on the endianess of the address space.
/// \param ct is the Constructor
/// \param cttpl is the given p-code section
/// \param isbigendian is set to \b true if the SLEIGH specification is big endian
/// \return \b true if all truncation expressions were valid
bool ConsistencyChecker::checkSectionTruncations(Constructor *ct,ConstructTpl *cttpl,bool isbigendian)
{
vector<OpTpl *>::const_iterator iter;
const vector<OpTpl *> &ops(cttpl->getOpvec());
bool testresult = true;
for(iter=ops.begin();iter!=ops.end();++iter) {
OpTpl *op = *iter;
VarnodeTpl *outvn = op->getOut();
if (outvn != (VarnodeTpl *)0) {
if (!checkVarnodeTruncation(ct,-1,op,outvn,isbigendian))
testresult = false;
}
for(int4 i=0;i<op->numInput();++i) {
if (!checkVarnodeTruncation(ct,i,op,op->getIn(i),isbigendian))
testresult = false;
}
}
return testresult;