-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgai-js
178 lines (144 loc) · 4.15 KB
/
gai-js
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
#!/usr/bin/env node
'use strict';
var fs = require('fs')
, path = require('path')
, opcodes = require('./opcodes')
, file = process.argv[2]
if (!file) {
console.error('Usage: gai-js <filename>')
process.exit(1)
}
file = path.resolve(file)
var data = [];
process.stdin
.on('data', ondata)
.on('end', onend)
.on('error', console.error)
function ondata(d) {
data.push(d);
}
function inspect(obj, depth) {
console.error(require('util').inspect(obj, false, depth || 5, true));
}
function onend() {
var lines = Buffer.concat(data)
.toString()
.replace(/^Z/g, '')
.replace(/\t/g, ' ')
.split('\n')
processLines(lines);
}
function collectOpCodes(results) {
function opcode(x) {
var inst = x.instruction
var m = inst.match(/^([a-f0-9]{2} )+/);
if (!m) throw new Error('Couldn\'t find opcode for ' + inst);
return { op: m[0], addr: parseInt(x.address, 16) }
}
function oponly(x) {
return x.op
}
function byaddress(x1, x2) {
return x1.addr - x2.addr
}
var sorted = results
.map(opcode)
.sort(byaddress)
.map(oponly)
return sorted
}
function diffRegs(prev, regs) {
if (!prev) return {};
function collectDiff(acc, k) {
var p = prev[k], c = regs[k];
if (!p || p.hex === c.hex) return acc;
acc[k] = { prev: p, curr: c };
return acc;
}
return Object.keys(regs).reduce(collectDiff, {});
}
function processLines(lines) {
var line, res = {}, results = [], prevRegs
// find test start
for (line = lines.shift(); typeof line !== 'undefined'; line = lines.shift()) {
var match = /^=>.+?\.gai_s\+0/.test(line)
if (match) break;
}
if (!line) throw new Error('Cannot find ".gai_s:" label to mark test start');
// first process all lines until the end of the file
while (line) {
res = processFrame(line, lines)
line = res.nextLine
delete res.nextLine;
results.push(res);
}
var initialRegs = results[0].regs;
// should be same as eip at that point
var entryPoint = results[0].address.split(' ')[0]
// We see the results AFTER we step, so the reg values we want
// are in the next step
for (var i = 0; i < results.length - 1; i++) {
results[i].diff = diffRegs(results[i].regs, results[i + 1].regs)
results[i].regs = results[i + 1].regs;
var end = /^=>.+?\.gai_e\+\d+/.test(results[i + 1].line)
if (end) {
results.length = i + 1;
break;
}
}
var json = JSON.stringify({
steps : results
, initialState : {
entryPoint: entryPoint
, regs : initialRegs
}
, opcodes : opcodes(file, results, entryPoint)
}, null, 2);
process.stdout.write(json);
}
function processFrame(line, lines) {
var lineArg = line;
var parts = line.split('>:')
var address = parts[0].slice(3) + '>';
var inst = parts[1].trim()
var regs = {}, regName, hexVal, decVal, flags, flagsString, testEnd;
// line points to **next** instruction while regs point to current
for (line = lines.shift(); typeof line !== 'undefined'; line = lines.shift()) {
var frame = /^=>.+?<.+\+\d+>/.test(line)
if (frame) break;
parts = line.split(/ +/);
if (parts.length < 3) continue;
regName = parts[0]
// ignore a bunch of unexpected lines that may occur
if (!/^(e[abcd]x|e[sbi]p|e[ds]i|eflags)/.test(regName)) continue;
hexVal = parts[1]
// eflags
if (/^eflags/.test(line)) {
flagsString = parts.slice(2).join(' ');
regs[regName] = { hex: hexVal, flagsString: flagsString, flags: processFlags(flagsString) }
continue;
}
// all registers except eflag
decVal = regName === 'esp'
|| regName === 'ebp'
|| regName === 'eip' ? undefined : parts[2]
regs[regName] = { hex: hexVal, decimal: decVal }
}
return {
line : lineArg
, address : address
, instruction : inst
, diff : undefined
, regs : regs
, nextLine : line
, testEnd : testEnd
}
}
function processFlags(flagsString) {
function setFlag(acc, k) { acc[k.trim()] = true; return acc; }
return flagsString
.replace(/\[ /, '')
.replace(/ \]/, '')
.split(' ')
.reduce(setFlag, {});
}