-
Notifications
You must be signed in to change notification settings - Fork 16
/
esil.ts
502 lines (491 loc) · 12.7 KB
/
esil.ts
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
import { R2Pipe } from "./r2pipe.js";
declare var console: any;
declare var r2: R2Pipe;
// ("this is just a comment"), -- comments are also part of the runtime
/*
=("//", {
=(obj, {}())
=([obj, comment], 32)
if(eq([obj,comment], 32),
ret()
)
ret(obj)
})
*/
export class EsilToken {
label: string = "";
comment: string = "";
text: string = "";
addr: string = "0"; // for ut64 we use strings for numbers :<
position: number = 0;
constructor(text: string = "", position: number = 0) {
this.text = text;
this.position = position;
}
toString() : string {
return this.text;
}
}
export type EsilNodeType = "number" | "flag" | "register" | "operation" | "none" | "block" | "goto" | "label";
export class EsilNode {
lhs: EsilNode | undefined;
rhs: EsilNode | undefined;
children: EsilNode[];
token: EsilToken;
type: EsilNodeType = "none";
constructor (token: EsilToken = new EsilToken(), type: EsilNodeType = "none") {
this.token = token;
this.children = [];
}
setSides(lhs: EsilNode, rhs: EsilNode): void {
this.lhs = lhs;
this.rhs = rhs;
}
addChildren(ths: EsilNode, fhs: EsilNode): void {
if (ths !== undefined) {
this.children.push(ths);
}
if (fhs !== undefined) {
this.children.push(fhs);
}
}
toEsil() : string {
if (this.lhs !== undefined && this.rhs !== undefined) {
// XXX handle ?{ }{ }
let left = this.lhs.toEsil();
if (left !== "") {
left += ",";
}
let right = this.rhs.toEsil();
return `${right},${left}${this.token}`;
}
return ''; // this.token.text;
}
toString() : string {
let str = "";
if (this.token.label !== "") {
str += this.token.label + ":\n";
}
if (this.token.addr !== "0") {
// str += "// @ " + this.token.addr + "\n";
}
if (this.token.comment !== "") {
str += "/*" + this.token.comment + "*/\n";
}
if (this.token.toString() === "GOTO") {
if (this.children.length > 0) {
const children = this.children[0];
str += "goto label_" + children.token.position + ";\n";
} else {
// console.log(JSON.stringify(this,null, 2));
let pos = 0;
str += `goto label_${pos};\n`;
}
}
if (this.children.length > 0) {
str += ` (if (${this.rhs})\n`;
for (let children of this.children) {
if (children !== null) {
const x = children.toString();
if (x != "") {
str += ` ${x}\n`;
}
}
}
str += " )\n";
}
if (this.lhs !== undefined && this.rhs !== undefined) {
return str + ` ( ${this.lhs} ${this.token} ${this.rhs} )`;
// return str + `${this.lhs} ${this.token} ${this.rhs}`;
}
return str + this.token.toString();
}
}
export class EsilParser {
r2: R2Pipe;
stack: EsilNode[]; // must be a stack or a list.. to parse sub expressions we must reset
nodes: EsilNode[];
root: EsilNode;
tokens: EsilToken[];
cur: number = 0;
constructor (r2: R2Pipe) {
this.r2 = r2;
this.cur = 0;
this.stack = [];
this.nodes = [];
this.tokens = [];
this.root = new EsilNode (new EsilToken("function", 0), "block");
}
toJSON() : string {
if (this.stack.length > 0) {
// return JSON.stringify (this.stack, null, 2);
throw new Error("The ESIL stack is not empty");
}
return JSON.stringify (this.root, null, 2);
}
toEsil() : string {
return this.nodes
.map( (x) => x.toEsil())
.join(',');
}
private optimizeFlags(node: EsilNode) {
if (node.rhs !== undefined) {
this.optimizeFlags(node.rhs);
}
if (node.lhs !== undefined) {
this.optimizeFlags(node.lhs);
}
for (let i = 0; i < node.children.length;i++) {
this.optimizeFlags(node.children[i]);
}
const addr : string = node.toString();
if (+addr > 4096) {
const fname = r2.cmd(`fd.@ ${addr}`).trim().split("\n")[0].trim();
if (fname != "" && fname.indexOf("+") === -1) {
node.token.text = fname;
}
}
}
optimize(options: string) : void {
if (options.indexOf("flag") != -1) {
this.optimizeFlags(this.root);
}
}
toString() : string {
return this.root.children
.map( (x) => x.toString())
.join(';\n');
}
reset() : void {
this.nodes = [];
this.stack = [];
this.tokens = [];
this.cur = 0;
this.root = new EsilNode (new EsilToken("function", 0), "block");
}
parseRange(from: number, to: number) {
let pos = from;
while (pos < this.tokens.length && pos < to) {
const token = this.peek(pos);
if (!token) {
// console.log("BREAK");
break;
}
// console.log(pos, token);
this.cur = pos;
this.pushToken(token);
pos = this.cur;
pos++;
}
// console.log("done");
}
parseFunction(addr?: string) : void {
var ep = this;
function parseAmount(n:number) : void {
// console.log("PDQ "+n);
const lines = r2.cmd("pie " + n + " @e:scr.color=0").trim().split("\n");
for (const line of lines) {
if (line.length === 0) {
console.log("Empty");
continue;
}
// console.log("parse", r2.cmd("?v:$$"));
const kv = line.split(' ');
if (kv.length > 1) { // line != "") {
// console.log("// @ " + kv[0]);
//ep.reset ();
r2.cmd(`s ${kv[0]}`);
ep.parse(kv[1], kv[0]);
ep.optimize("flags,labels");
//console.log(ep.toString());
}
}
// console.log(ep.toString());
}
const oaddr = r2.cmd("?v $$").trim();
// const func = r2.cmdj("pdrj"); // XXX this command changes the current seek
if (addr === undefined) {
addr = oaddr;
}
const bbs = r2.cmdj(`afbj@${addr}`); // XXX this command changes the current seek
for (let bb of bbs) {
// console.log("bb_" + bb.addr + ":");
r2.cmd(`s ${bb.addr}`);
parseAmount (bb.ninstr);
}
r2.cmd(`s ${oaddr}`);
}
parse(expr: string, addr?: string) : void | never {
const tokens = expr.trim().split(',').map( (x) => x.trim() );
const from = this.tokens.length;
for (let tok of tokens) {
const token = new EsilToken(tok, this.tokens.length);
if (addr !== undefined) {
token.addr = addr;
}
this.tokens.push(token);
}
const to = this.tokens.length;
this.parseRange (from, to);
}
peek (a:number): EsilToken | undefined {
return this.tokens[a];
}
pushToken(tok: EsilToken) : void | never {
if (this.isNumber (tok)) {
const node = new EsilNode(tok, "number");
this.stack.push(node);
this.nodes.push(node);
} else if (this.isInternal (tok)) {
const node = new EsilNode(tok, "flag");
this.stack.push(node);
this.nodes.push(node);
} else if (this.isOperation (tok)) {
// run the operation login
} else {
// assume it's a register, so just push the string
const node = new EsilNode(tok, "register");
this.stack.push(node);
this.nodes.push(node);
}
// we need a list of register names to do this check properly
// throw new Error ("Unknown token");
}
private isNumber(expr: EsilToken) : boolean {
if (expr.toString().startsWith("0")) {
return true;
}
return +expr > 0;
}
private isInternal(expr: EsilToken) : boolean {
const text = expr.toString();
return text.startsWith("$") && text.length > 1;
}
private parseUntil(start: number) : EsilNode | null {
const from = start + 1;
let pos = from;
const origStack : any[] = [];
const this_nodes_length = this.nodes.length;
this.stack.forEach((x) => origStack.push(x));
while (pos < this.tokens.length) {
const token = this.peek(pos);
if (!token) {
break;
}
if (token.toString() === '}') {
break;
}
if (token.toString() === '}{') {
// return token;
break;
}
// console.log("peek ", this.tokens[pos]);
pos++;
}
this.stack = origStack;
const to = pos;
this.parseRange(from, to);
const same = this.nodes.length == this_nodes_length;
// console.log("BLOCK ("+ ep.toString());
if (same) {
return null;
}
return this.nodes[this.nodes.length - 1]; // this.tokens.length - 1];
}
private getNodeFor(index:number): null | EsilNode {
const tok = this.peek(index);
if (tok === undefined) {
return null;
}
for (let node of this.nodes) {
if (node.token.position === index) {
return node;
}
}
this.nodes.push(new EsilNode(new EsilToken("label", index), "label"));
return null;
}
private findNodeFor(index:number): null | EsilNode {
for (let node of this.nodes) {
if (node.token.position === index) {
return node;
}
}
return null;
}
private isOperation(expr: EsilToken) : never | boolean {
switch(expr.toString()) {
// 1pop1push
case "[1]":
case "[2]":
case "[4]":
case "[8]":
if (this.stack.length >= 1) {
const i1 = this.stack.pop()!;
// TODO: MemoryReferenceNode(i1));
const mn = new EsilNode(i1.token, "operation"); // expr.toString());
this.stack.push(i1); // mn);
} else {
throw new Error("Stack needs more items");
}
return true;
// 1pop1push
case "!":
if (this.stack.length >= 1) {
const i0 = new EsilNode(new EsilToken("", expr.position), "none");
const i1 = this.stack.pop()!;
const nn = new EsilNode(expr, "operation");
nn.setSides(i0, i1);
this.stack.push(nn);
} else {
throw new Error("Stack needs more items");
}
return true;
case "":
case "}":
case "}{":
// no pops or nothing, just does nothing
return true;
case "DUP":
if (true) {
if (this.stack.length < 1) {
throw new Error("goto cant pop");
}
const destNode = this.stack.pop()!;
this.stack.push(destNode);
this.stack.push(destNode);
}
return true;
case "GOTO":
// take previous statement which should be const and add a label
const prev = this.peek(expr.position - 1);
if (prev !== null) {
// TODO: check stack
if (this.stack.length < 1) {
throw new Error("goto cant pop");
}
const destNode = this.stack.pop()!;
if (destNode !== null) {
const value : number = 0 | +destNode.toString();
if (value > 0) {
const destToken = this.peek(value);
if (destToken !== undefined) {
destToken.label = "label_" + value;
destToken.comment = "hehe";
const nn = new EsilNode(expr, "goto");
const gn = this.getNodeFor(destToken.position);
if (gn != null) {
nn.children.push(gn);
}
this.root.children.push(nn);
} else {
console.error("Cannot find goto node");
}
} else {
console.error("Cannot find dest node for goto");
}
}
}
return true;
// controlflow
case "?{": // ESIL_TOKEN_IF
if (this.stack.length >= 1) {
const i0 = new EsilNode(new EsilToken("if", expr.position), "none");
const i1 = this.stack.pop()!;
const nn = new EsilNode(expr, "operation");
nn.setSides(i0, i1); // left side can be ignored for now.. but we can express this somehow
let trueBlock = this.parseUntil(expr.position);
let falseBlock = null;
// nn.addChildren(trueBlock, falseBlock);
if (trueBlock !== null) {
nn.children.push(trueBlock);
this.nodes.push(trueBlock);
falseBlock = this.parseUntil(trueBlock.token.position + 1);
if (falseBlock !== null) {
nn.children.push(falseBlock);
this.nodes.push(falseBlock);
}
}
// console.log("true", trueBlock);
// console.log("false", falseBlock);
// this.stack.push(nn);
this.nodes.push(nn);
this.root.children.push(nn);
if (falseBlock !== null) {
this.cur = falseBlock.token.position;
}
} else {
throw new Error("Stack needs more items");
}
return true;
case "-":
if (this.stack.length >= 2) {
const i0 = this.stack.pop()!;
const i1 = this.stack.pop()!;
const nn = new EsilNode(expr, "operation");
nn.setSides(i0, i1);
if (this.stack.length === 0) {
// this.root.children.push(nn);
}
this.stack.push(nn);
this.nodes.push(nn);
} else {
throw new Error("Stack needs more items");
}
return true;
// 2pop1push
case "<":
case ">":
case "^":
case "&":
case "|":
case "+":
case "*":
case "/":
case ">>=":
case "<<=":
case ">>>=":
case "<<<=":
case ">>>>=":
case "<<<<=":
if (this.stack.length >= 2) {
const i0 = this.stack.pop()!;
const i1 = this.stack.pop()!;
const nn = new EsilNode(expr, "operation");
nn.setSides(i0, i1);
if (this.stack.length === 0) {
// this.root.children.push(nn);
}
this.stack.push(nn);
this.nodes.push(nn);
} else {
throw new Error("Stack needs more items");
}
return true;
// 2pop0push
case "=":
case ":=":
case "-=":
case "+=":
case "==":
case "=[1]":
case "=[2]":
case "=[4]":
case "=[8]":
if (this.stack.length >= 2) {
const i0 = this.stack.pop()!;
const i1 = this.stack.pop()!;
const nn = new EsilNode(expr, "operation");
nn.setSides(i0, i1);
if (this.stack.length === 0) {
this.root.children.push(nn);
}
this.nodes.push(nn);
} else {
throw new Error("Stack needs more items");
}
return true;
}
return false;
}
}