forked from NationalSecurityAgency/ghidra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslghpatexpress.hh
504 lines (461 loc) · 21.1 KB
/
slghpatexpress.hh
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
/* ###
* 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.
*/
#ifndef __SLGHPATEXPRESS_HH__
#define __SLGHPATEXPRESS_HH__
#include "slghpattern.hh"
namespace ghidra {
class TokenPattern {
Pattern *pattern;
vector<Token *> toklist;
bool leftellipsis;
bool rightellipsis;
static PatternBlock *buildSingle(int4 startbit,int4 endbit,uintm byteval);
static PatternBlock *buildBigBlock(int4 size,int4 bitstart,int4 bitend,intb value);
static PatternBlock *buildLittleBlock(int4 size,int4 bitstart,int4 bitend,intb value);
int4 resolveTokens(const TokenPattern &tokpat1,const TokenPattern &tokpat2);
TokenPattern(Pattern *pat) { pattern = pat; leftellipsis=false; rightellipsis=false; }
public:
TokenPattern(void); // TRUE pattern unassociated with a token
TokenPattern(bool tf); // TRUE or FALSE pattern unassociated with a token
TokenPattern(Token *tok); // TRUE pattern associated with token -tok-
TokenPattern(Token *tok,intb value,int4 bitstart,int4 bitend);
TokenPattern(intb value,int4 startbit,int4 endbit);
TokenPattern(const TokenPattern &tokpat);
~TokenPattern(void) { delete pattern; }
const TokenPattern &operator=(const TokenPattern &tokpat);
void setLeftEllipsis(bool val) { leftellipsis = val; }
void setRightEllipsis(bool val) { rightellipsis = val; }
bool getLeftEllipsis(void) const { return leftellipsis; }
bool getRightEllipsis(void) const { return rightellipsis; }
TokenPattern doAnd(const TokenPattern &tokpat) const;
TokenPattern doOr(const TokenPattern &tokpat) const;
TokenPattern doCat(const TokenPattern &tokpat) const;
TokenPattern commonSubPattern(const TokenPattern &tokpat) const;
Pattern *getPattern(void) const { return pattern; }
int4 getMinimumLength(void) const;
bool alwaysTrue(void) const { return pattern->alwaysTrue(); }
bool alwaysFalse(void) const { return pattern->alwaysFalse(); }
bool alwaysInstructionTrue(void) const { return pattern->alwaysInstructionTrue(); }
};
class PatternValue;
class PatternExpression {
int4 refcount; // Number of objects referencing this
// for deletion
protected:
virtual ~PatternExpression(void) {} // Only delete through release
public:
PatternExpression(void) { refcount = 0; }
virtual intb getValue(ParserWalker &walker) const=0;
virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const=0;
virtual void listValues(vector<const PatternValue *> &list) const=0;
virtual void getMinMax(vector<intb> &minlist,vector<intb> &maxlist) const=0;
virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const=0;
virtual void saveXml(ostream &s) const=0;
virtual void restoreXml(const Element *el,Translate *trans)=0;
intb getSubValue(const vector<intb> &replace) {
int4 listpos = 0;
return getSubValue(replace,listpos); }
void layClaim(void) { refcount += 1; }
static void release(PatternExpression *p);
static PatternExpression *restoreExpression(const Element *el,Translate *trans);
};
class PatternValue : public PatternExpression {
public:
virtual TokenPattern genPattern(intb val) const=0;
virtual void listValues(vector<const PatternValue *> &list) const { list.push_back(this); }
virtual void getMinMax(vector<intb> &minlist,vector<intb> &maxlist) const {
minlist.push_back(minValue()); maxlist.push_back(maxValue()); }
virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const { return replace[listpos++]; }
virtual intb minValue(void) const=0;
virtual intb maxValue(void) const=0;
};
class TokenField : public PatternValue {
Token *tok;
bool bigendian;
bool signbit;
int4 bitstart,bitend; // Bits within the token, 0 bit is LEAST significant
int4 bytestart,byteend; // Bytes to read to get value
int4 shift; // Amount to shift to align value (bitstart % 8)
public:
TokenField(void) {} // For use with restoreXml
TokenField(Token *tk,bool s,int4 bstart,int4 bend);
virtual intb getValue(ParserWalker &walker) const;
virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(tok); }
virtual TokenPattern genPattern(intb val) const;
virtual intb minValue(void) const { return 0; }
virtual intb maxValue(void) const { intb res=0; res=~res; zero_extend(res,bitend-bitstart); return res; }
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el,Translate *trans);
};
class ContextField : public PatternValue {
int4 startbit,endbit;
int4 startbyte,endbyte;
int4 shift;
bool signbit;
public:
ContextField(void) {} // For use with restoreXml
ContextField(bool s,int4 sbit,int4 ebit);
int4 getStartBit(void) const { return startbit; }
int4 getEndBit(void) const { return endbit; }
bool getSignBit(void) const { return signbit; }
virtual intb getValue(ParserWalker &walker) const;
virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(); }
virtual TokenPattern genPattern(intb val) const;
virtual intb minValue(void) const { return 0; }
virtual intb maxValue(void) const { intb res=0; res=~res; zero_extend(res,(endbit-startbit)); return res; }
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el,Translate *trans);
};
class ConstantValue : public PatternValue {
intb val;
public:
ConstantValue(void) {} // For use with restoreXml
ConstantValue(intb v) { val = v; }
virtual intb getValue(ParserWalker &walker) const { return val; }
virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(); }
virtual TokenPattern genPattern(intb v) const { return TokenPattern(val==v); }
virtual intb minValue(void) const { return val; }
virtual intb maxValue(void) const { return val; }
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el,Translate *trans);
};
class StartInstructionValue : public PatternValue {
public:
StartInstructionValue(void) {}
virtual intb getValue(ParserWalker &walker) const {
return (intb)AddrSpace::byteToAddress(walker.getAddr().getOffset(),walker.getAddr().getSpace()->getWordSize()); }
virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(); }
virtual TokenPattern genPattern(intb val) const { return TokenPattern(); }
virtual intb minValue(void) const { return (intb)0; }
virtual intb maxValue(void) const { return (intb)0; }
virtual void saveXml(ostream &s) const { s << "<start_exp/>"; }
virtual void restoreXml(const Element *el,Translate *trans) {}
};
class OperandOffsetValue : public PatternValue {
public:
OperandOffsetValue(void) {}
virtual intb getValue(ParserWalker &walker) const {
return (intb)walker.getOffset(-1);
}
virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(); }
virtual TokenPattern genPattern(intb val) const { return TokenPattern(); }
virtual intb minValue(void) const { return (intb)0; }
virtual intb maxValue(void) const { return (intb)0; }
virtual void saveXml(ostream &s) const { s << "<offset_exp/>"; }
virtual void restoreXml(const Element *el,Translate *trans) {}
};
class EndInstructionValue : public PatternValue {
public:
EndInstructionValue(void) {}
virtual intb getValue(ParserWalker &walker) const {
return (intb)AddrSpace::byteToAddress(walker.getNaddr().getOffset(),walker.getNaddr().getSpace()->getWordSize()); }
virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(); }
virtual TokenPattern genPattern(intb val) const { return TokenPattern(); }
virtual intb minValue(void) const { return (intb)0; }
virtual intb maxValue(void) const { return (intb)0; }
virtual void saveXml(ostream &s) const { s << "<end_exp/>"; }
virtual void restoreXml(const Element *el,Translate *trans) {}
};
class Next2InstructionValue : public PatternValue {
public:
Next2InstructionValue(void) {}
virtual intb getValue(ParserWalker &walker) const {
return (intb)AddrSpace::byteToAddress(walker.getN2addr().getOffset(),walker.getN2addr().getSpace()->getWordSize()); }
virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(); }
virtual TokenPattern genPattern(intb val) const { return TokenPattern(); }
virtual intb minValue(void) const { return (intb)0; }
virtual intb maxValue(void) const { return (intb)0; }
virtual void saveXml(ostream &s) const { s << "<next2_exp/>"; }
virtual void restoreXml(const Element *el,Translate *trans) {}
};
class Constructor; // Forward declaration
class OperandSymbol;
class OperandValue : public PatternValue {
int4 index; // This is the defining field of expression
Constructor *ct; // cached pointer to constructor
public:
OperandValue(void) { } // For use with restoreXml
OperandValue(int4 ind,Constructor *c) { index = ind; ct = c; }
void changeIndex(int4 newind) { index = newind; }
bool isConstructorRelative(void) const;
const string &getName(void) const;
virtual TokenPattern genPattern(intb val) const;
virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return ops[index]; }
virtual intb getValue(ParserWalker &walker) const;
virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
virtual intb minValue(void) const;
virtual intb maxValue(void) const;
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el,Translate *trans);
};
class BinaryExpression : public PatternExpression {
PatternExpression *left,*right;
protected:
virtual ~BinaryExpression(void);
public:
BinaryExpression(void) { left = (PatternExpression *)0; right = (PatternExpression *)0; } // For use with restoreXml
BinaryExpression(PatternExpression *l,PatternExpression *r);
PatternExpression *getLeft(void) const { return left; }
PatternExpression *getRight(void) const { return right; }
virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(); }
virtual void listValues(vector<const PatternValue *> &list) const {
left->listValues(list); right->listValues(list); }
virtual void getMinMax(vector<intb> &minlist,vector<intb> &maxlist) const {
left->getMinMax(minlist,maxlist); right->getMinMax(minlist,maxlist); }
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el,Translate *trans);
};
class UnaryExpression : public PatternExpression {
PatternExpression *unary;
protected:
virtual ~UnaryExpression(void);
public:
UnaryExpression(void) { unary = (PatternExpression *)0; } // For use with restoreXml
UnaryExpression(PatternExpression *u);
PatternExpression *getUnary(void) const { return unary; }
virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(); }
virtual void listValues(vector<const PatternValue *> &list) const {
unary->listValues(list); }
virtual void getMinMax(vector<intb> &minlist,vector<intb> &maxlist) const {
unary->getMinMax(minlist,maxlist);
}
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el,Translate *trans);
};
class PlusExpression : public BinaryExpression {
public:
PlusExpression(void) {} // For use by restoreXml
PlusExpression(PatternExpression *l,PatternExpression *r) : BinaryExpression(l,r) {}
virtual intb getValue(ParserWalker &walker) const;
virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
virtual void saveXml(ostream &s) const;
};
class SubExpression : public BinaryExpression {
public:
SubExpression(void) {} // For use with restoreXml
SubExpression(PatternExpression *l,PatternExpression *r) : BinaryExpression(l,r) {}
virtual intb getValue(ParserWalker &walker) const;
virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
virtual void saveXml(ostream &s) const;
};
class MultExpression : public BinaryExpression {
public:
MultExpression(void) {} // For use with restoreXml
MultExpression(PatternExpression *l,PatternExpression *r) : BinaryExpression(l,r) {}
virtual intb getValue(ParserWalker &walker) const;
virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
virtual void saveXml(ostream &s) const;
};
class LeftShiftExpression : public BinaryExpression {
public:
LeftShiftExpression(void) {}
LeftShiftExpression(PatternExpression *l,PatternExpression *r) : BinaryExpression(l,r) {}
virtual intb getValue(ParserWalker &walker) const;
virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
virtual void saveXml(ostream &s) const;
};
class RightShiftExpression : public BinaryExpression {
public:
RightShiftExpression(void) {}
RightShiftExpression(PatternExpression *l,PatternExpression *r) : BinaryExpression(l,r) {}
virtual intb getValue(ParserWalker &walker) const;
virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
virtual void saveXml(ostream &s) const;
};
class AndExpression : public BinaryExpression {
public:
AndExpression(void) {}
AndExpression(PatternExpression *l,PatternExpression *r) : BinaryExpression(l,r) {}
virtual intb getValue(ParserWalker &walker) const;
virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
virtual void saveXml(ostream &s) const;
};
class OrExpression : public BinaryExpression {
public:
OrExpression(void) {}
OrExpression(PatternExpression *l,PatternExpression *r) : BinaryExpression(l,r) {}
virtual intb getValue(ParserWalker &walker) const;
virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
virtual void saveXml(ostream &s) const;
};
class XorExpression : public BinaryExpression {
public:
XorExpression(void) {}
XorExpression(PatternExpression *l,PatternExpression *r) : BinaryExpression(l,r) {}
virtual intb getValue(ParserWalker &walker) const;
virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
virtual void saveXml(ostream &s) const;
};
class DivExpression : public BinaryExpression {
public:
DivExpression(void) {}
DivExpression(PatternExpression *l,PatternExpression *r) : BinaryExpression(l,r) {}
virtual intb getValue(ParserWalker &walker) const;
virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
virtual void saveXml(ostream &s) const;
};
class MinusExpression : public UnaryExpression {
public:
MinusExpression(void) {}
MinusExpression(PatternExpression *u) : UnaryExpression(u) {}
virtual intb getValue(ParserWalker &walker) const;
virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
virtual void saveXml(ostream &s) const;
};
class NotExpression : public UnaryExpression {
public:
NotExpression(void) {}
NotExpression(PatternExpression *u) : UnaryExpression(u) {}
virtual intb getValue(ParserWalker &walker) const;
virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
virtual void saveXml(ostream &s) const;
};
struct OperandResolve {
vector<OperandSymbol *> &operands;
OperandResolve(vector<OperandSymbol *> &ops) : operands(ops) {
base=-1; offset=0; cur_rightmost = -1; size = 0; }
int4 base; // Current base operand (as we traverse the pattern equation from left to right)
int4 offset; // Bytes we have traversed from the LEFT edge of the current base
int4 cur_rightmost; // (resulting) rightmost operand in our pattern
int4 size; // (resulting) bytes traversed from the LEFT edge of the rightmost
};
// operandOrder returns a vector of the self-defining OperandSymbols as the appear
// in left to right order in the pattern
class PatternEquation {
int4 refcount; // Number of objects referencing this
protected:
mutable TokenPattern resultpattern; // Resulting pattern generated by this equation
virtual ~PatternEquation(void) {} // Only delete through release
public:
PatternEquation(void) { refcount = 0; }
const TokenPattern &getTokenPattern(void) const { return resultpattern; }
virtual void genPattern(const vector<TokenPattern> &ops) const=0;
virtual bool resolveOperandLeft(OperandResolve &state) const=0;
virtual void operandOrder(Constructor *ct,vector<OperandSymbol *> &order) const {}
void layClaim(void) { refcount += 1; }
static void release(PatternEquation *pateq);
};
class OperandEquation : public PatternEquation { // Equation that defines operand
int4 index;
public:
OperandEquation(int4 ind) { index = ind; }
virtual void genPattern(const vector<TokenPattern> &ops) const;
virtual bool resolveOperandLeft(OperandResolve &state) const;
virtual void operandOrder(Constructor *ct,vector<OperandSymbol *> &order) const;
};
class UnconstrainedEquation : public PatternEquation { // Unconstrained equation, just get tokens
PatternExpression *patex;
protected:
virtual ~UnconstrainedEquation(void);
public:
UnconstrainedEquation(PatternExpression *p);
virtual void genPattern(const vector<TokenPattern> &ops) const;
virtual bool resolveOperandLeft(OperandResolve &state) const;
};
class ValExpressEquation : public PatternEquation {
protected:
PatternValue *lhs;
PatternExpression *rhs;
virtual ~ValExpressEquation(void);
public:
ValExpressEquation(PatternValue *l,PatternExpression *r);
virtual bool resolveOperandLeft(OperandResolve &state) const;
};
class EqualEquation : public ValExpressEquation {
public:
EqualEquation(PatternValue *l,PatternExpression *r) : ValExpressEquation(l,r) {}
virtual void genPattern(const vector<TokenPattern> &ops) const;
};
class NotEqualEquation : public ValExpressEquation {
public:
NotEqualEquation(PatternValue *l,PatternExpression *r) : ValExpressEquation(l,r) {}
virtual void genPattern(const vector<TokenPattern> &ops) const;
};
class LessEquation : public ValExpressEquation {
public:
LessEquation(PatternValue *l,PatternExpression *r) : ValExpressEquation(l,r) {}
virtual void genPattern(const vector<TokenPattern> &ops) const;
};
class LessEqualEquation : public ValExpressEquation {
public:
LessEqualEquation(PatternValue *l,PatternExpression *r) : ValExpressEquation(l,r) {}
virtual void genPattern(const vector<TokenPattern> &ops) const;
};
class GreaterEquation : public ValExpressEquation {
public:
GreaterEquation(PatternValue *l,PatternExpression *r) : ValExpressEquation(l,r) {}
virtual void genPattern(const vector<TokenPattern> &ops) const;
};
class GreaterEqualEquation : public ValExpressEquation {
public:
GreaterEqualEquation(PatternValue *l,PatternExpression *r) : ValExpressEquation(l,r) {}
virtual void genPattern(const vector<TokenPattern> &ops) const;
};
class EquationAnd : public PatternEquation { // Pattern Equations ANDed together
PatternEquation *left;
PatternEquation *right;
protected:
virtual ~EquationAnd(void);
public:
EquationAnd(PatternEquation *l,PatternEquation *r);
virtual void genPattern(const vector<TokenPattern> &ops) const;
virtual bool resolveOperandLeft(OperandResolve &state) const;
virtual void operandOrder(Constructor *ct,vector<OperandSymbol *> &order) const;
};
class EquationOr : public PatternEquation { // Pattern Equations ORed together
PatternEquation *left;
PatternEquation *right;
protected:
virtual ~EquationOr(void);
public:
EquationOr(PatternEquation *l,PatternEquation *r);
virtual void genPattern(const vector<TokenPattern> &ops) const;
virtual bool resolveOperandLeft(OperandResolve &state) const;
virtual void operandOrder(Constructor *ct,vector<OperandSymbol *> &order) const;
};
class EquationCat : public PatternEquation { // Pattern Equations concatenated
PatternEquation *left;
PatternEquation *right;
protected:
virtual ~EquationCat(void);
public:
EquationCat(PatternEquation *l,PatternEquation *r);
virtual void genPattern(const vector<TokenPattern> &ops) const;
virtual bool resolveOperandLeft(OperandResolve &state) const;
virtual void operandOrder(Constructor *ct,vector<OperandSymbol *> &order) const;
};
class EquationLeftEllipsis : public PatternEquation { // Equation preceded by ellipses
PatternEquation *eq;
protected:
virtual ~EquationLeftEllipsis(void) { PatternEquation::release(eq); }
public:
EquationLeftEllipsis(PatternEquation *e) { (eq=e)->layClaim(); }
virtual void genPattern(const vector<TokenPattern> &ops) const;
virtual bool resolveOperandLeft(OperandResolve &state) const;
virtual void operandOrder(Constructor *ct,vector<OperandSymbol *> &order) const;
};
class EquationRightEllipsis : public PatternEquation { // Equation preceded by ellipses
PatternEquation *eq;
protected:
virtual ~EquationRightEllipsis(void) { PatternEquation::release(eq); }
public:
EquationRightEllipsis(PatternEquation *e) { (eq=e)->layClaim(); }
virtual void genPattern(const vector<TokenPattern> &ops) const;
virtual bool resolveOperandLeft(OperandResolve &state) const;
virtual void operandOrder(Constructor *ct,vector<OperandSymbol *> &order) const;
};
} // End namespace ghidra
#endif