forked from cseagle/blc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emulateutil.cc
393 lines (318 loc) · 11.6 KB
/
emulateutil.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
/* ###
* 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 "architecture.hh"
#include "emulateutil.hh"
/// \param g is the Architecture providing the LoadImage
EmulatePcodeOp::EmulatePcodeOp(Architecture *g)
{
glb = g;
currentOp = (PcodeOp *)0;
lastOp = (PcodeOp *)0;
}
uintb EmulatePcodeOp::getLoadImageValue(AddrSpace *spc,uintb off,int4 sz) const
{
LoadImage *loadimage = glb->loader;
uintb res;
loadimage->loadFill((uint1 *)&res,sizeof(uintb),Address(spc,off));
if ((HOST_ENDIAN==1) != spc->isBigEndian())
res = byte_swap(res,sizeof(uintb));
if (spc->isBigEndian() && (sz < sizeof(uintb)))
res >>= (sizeof(uintb)-sz)*8;
else
res &= calc_mask(sz);
return res;
}
void EmulatePcodeOp::executeUnary(void)
{
uintb in1 = getVarnodeValue(currentOp->getIn(0));
uintb out = currentBehave->evaluateUnary(currentOp->getOut()->getSize(),
currentOp->getIn(0)->getSize(),in1);
setVarnodeValue(currentOp->getOut(), out);
}
void EmulatePcodeOp::executeBinary(void)
{
uintb in1 = getVarnodeValue(currentOp->getIn(0));
uintb in2 = getVarnodeValue(currentOp->getIn(1));
uintb out = currentBehave->evaluateBinary(currentOp->getOut()->getSize(),
currentOp->getIn(0)->getSize(),in1,in2);
setVarnodeValue(currentOp->getOut(), out);
}
void EmulatePcodeOp::executeLoad(void)
{
// op will be null, use current_op
uintb off = getVarnodeValue(currentOp->getIn(1));
AddrSpace *spc = Address::getSpaceFromConst(currentOp->getIn(0)->getAddr());
off = AddrSpace::addressToByte(off,spc->getWordSize());
int4 sz = currentOp->getOut()->getSize();
uintb res = getLoadImageValue(spc,off,sz);
setVarnodeValue(currentOp->getOut(),res);
}
void EmulatePcodeOp::executeStore(void)
{
// There is currently nowhere to store anything since the memstate is null
// uintb val = getVarnodeValue(current_op->getIn(2)); // Value being stored
// uintb off = getVarnodeValue(current_op->getIn(1));
// AddrSpace *spc = Address::getSpaceFromConst(current_op->getIn(0)->getAddr());
}
bool EmulatePcodeOp::executeCbranch(void)
{
// op will be null, use current_op
uintb cond = getVarnodeValue(currentOp->getIn(1));
// We must take into account the booleanflip bit with pcode from the syntax tree
return ((cond != 0) != currentOp->isBooleanFlip());
}
void EmulatePcodeOp::executeMultiequal(void)
{
// op will be null, use current_op
int4 i;
FlowBlock *bl = currentOp->getParent();
FlowBlock *last_bl = lastOp->getParent();
for(i=0;i<bl->sizeIn();++i)
if (bl->getIn(i) == last_bl) break;
if (i == bl->sizeIn())
throw LowlevelError("Could not execute MULTIEQUAL");
uintb val = getVarnodeValue(currentOp->getIn(i));
setVarnodeValue( currentOp->getOut(), val );
}
void EmulatePcodeOp::executeIndirect(void)
{
// We could probably safely ignore this in the
// context we are using it (jumptable recovery)
// But we go ahead and assume it is equivalent to copy
uintb val = getVarnodeValue(currentOp->getIn(0));
setVarnodeValue( currentOp->getOut(), val);
}
void EmulatePcodeOp::executeSegmentOp(void)
{
SegmentOp *segdef = glb->userops.getSegmentOp(Address::getSpaceFromConst(currentOp->getIn(0)->getAddr())->getIndex());
if (segdef == (SegmentOp *)0)
throw LowlevelError("Segment operand missing definition");
uintb in1 = getVarnodeValue(currentOp->getIn(1));
uintb in2 = getVarnodeValue(currentOp->getIn(2));
vector<uintb> bindlist;
bindlist.push_back(in2);
bindlist.push_back(in1);
uintb res = segdef->execute(bindlist);
setVarnodeValue(currentOp->getOut(), res);
}
void EmulatePcodeOp::executeCpoolRef(void)
{
// Ignore references to constant pool
}
void EmulatePcodeOp::executeNew(void)
{
// Ignore new operations
}
uintb EmulateSnippet::getLoadImageValue(AddrSpace *spc,uintb off,int4 sz) const
{
LoadImage *loadimage = glb->loader;
uintb res;
loadimage->loadFill((uint1 *)&res,sizeof(uintb),Address(spc,off));
if ((HOST_ENDIAN==1) != spc->isBigEndian())
res = byte_swap(res,sizeof(uintb));
if (spc->isBigEndian() && (sz < sizeof(uintb)))
res >>= (sizeof(uintb)-sz)*8;
else
res &= calc_mask(sz);
return res;
}
void EmulateSnippet::executeUnary(void)
{
uintb in1 = getVarnodeValue(currentOp->getInput(0));
uintb out = currentBehave->evaluateUnary(currentOp->getOutput()->size,
currentOp->getInput(0)->size,in1);
setVarnodeValue(currentOp->getOutput()->offset, out);
}
void EmulateSnippet::executeBinary(void)
{
uintb in1 = getVarnodeValue(currentOp->getInput(0));
uintb in2 = getVarnodeValue(currentOp->getInput(1));
uintb out = currentBehave->evaluateBinary(currentOp->getOutput()->size,
currentOp->getInput(0)->size,in1,in2);
setVarnodeValue(currentOp->getOutput()->offset, out);
}
void EmulateSnippet::executeLoad(void)
{
// op will be null, use current_op
uintb off = getVarnodeValue(currentOp->getInput(1));
AddrSpace *spc = Address::getSpaceFromConst(currentOp->getInput(0)->getAddr());
off = AddrSpace::addressToByte(off,spc->getWordSize());
int4 sz = currentOp->getOutput()->size;
uintb res = getLoadImageValue(spc,off,sz);
setVarnodeValue(currentOp->getOutput()->offset,res);
}
void EmulateSnippet::executeStore(void)
{
throw LowlevelError("Illegal p-code operation in snippet: "+ (string)get_opname(currentOp->getOpcode()));
}
void EmulateSnippet::executeBranch(void)
{
VarnodeData *vn = currentOp->getInput(0);
if (vn->space->getType() != IPTR_CONSTANT)
throw LowlevelError("Tried to emulate absolute branch in snippet code");
int4 rel = (int4)vn->offset;
pos += rel;
if ((pos < 0)||(pos>opList.size()))
throw LowlevelError("Relative branch out of bounds in snippet code");
if (pos == opList.size()) {
emu_halted = true;
return;
}
setCurrentOp(pos);
}
bool EmulateSnippet::executeCbranch(void)
{
// op will be null, use current_op
uintb cond = getVarnodeValue(currentOp->getInput(1));
// We must take into account the booleanflip bit with pcode from the syntax tree
return (cond != 0);
}
void EmulateSnippet::executeBranchind(void)
{
throw LowlevelError("Illegal p-code operation in snippet: "+ (string)get_opname(currentOp->getOpcode()));
}
void EmulateSnippet::executeCall(void)
{
throw LowlevelError("Illegal p-code operation in snippet: "+ (string)get_opname(currentOp->getOpcode()));
}
void EmulateSnippet::executeCallind(void)
{
throw LowlevelError("Illegal p-code operation in snippet: "+ (string)get_opname(currentOp->getOpcode()));
}
void EmulateSnippet::executeCallother(void)
{
throw LowlevelError("Illegal p-code operation in snippet: "+ (string)get_opname(currentOp->getOpcode()));
}
void EmulateSnippet::executeMultiequal(void)
{
throw LowlevelError("Illegal p-code operation in snippet: "+ (string)get_opname(currentOp->getOpcode()));
}
void EmulateSnippet::executeIndirect(void)
{
throw LowlevelError("Illegal p-code operation in snippet: "+ (string)get_opname(currentOp->getOpcode()));
}
void EmulateSnippet::executeSegmentOp(void)
{
throw LowlevelError("Illegal p-code operation in snippet: "+ (string)get_opname(currentOp->getOpcode()));
}
void EmulateSnippet::executeCpoolRef(void)
{
throw LowlevelError("Illegal p-code operation in snippet: "+ (string)get_opname(currentOp->getOpcode()));
}
void EmulateSnippet::executeNew(void)
{
throw LowlevelError("Illegal p-code operation in snippet: "+ (string)get_opname(currentOp->getOpcode()));
}
void EmulateSnippet::fallthruOp(void)
{
pos += 1;
if (pos == opList.size()) {
emu_halted = true;
return;
}
setCurrentOp(pos);
}
EmulateSnippet::~EmulateSnippet(void)
{
for(int4 i=0;i<opList.size();++i)
delete opList[i];
for(int4 i=0;i<varList.size();++i)
delete varList[i];
}
/// \brief Provide the caller with an emitter for building the p-code snippet
///
/// Any p-code produced by the PcodeEmit, when triggered by the caller, becomes
/// part of the \e snippet that will get emulated by \b this. The caller should
/// free the PcodeEmit object immediately after use.
/// \param inst is the \e opcode to \e behavior map the emitter will use
/// \param uniqReserve is the starting offset within the \e unique address space for any temporary registers
/// \return the newly constructed emitter
PcodeEmit *EmulateSnippet::buildEmitter(const vector<OpBehavior *> &inst,uintb uniqReserve)
{
return new PcodeEmitCache(opList,varList,inst,uniqReserve);
}
/// \brief Check for p-code that is deemed illegal for a \e snippet
///
/// This method facilitates enforcement of the formal rules for snippet code.
/// - Branches must use p-code relative addressing.
/// - Snippets can only read/write from temporary registers
/// - Snippets cannot use BRANCHIND, CALL, CALLIND, CALLOTHER, STORE, SEGMENTOP, CPOOLREF,
/// NEW, MULTIEQUAL, or INDIRECT
///
/// \return \b true if the current snippet is legal
bool EmulateSnippet::checkForLegalCode(void) const
{
for(int4 i=0;i<opList.size();++i) {
PcodeOpRaw *op = opList[i];
VarnodeData *vn;
OpCode opc = op->getOpcode();
if (opc == CPUI_BRANCHIND || opc == CPUI_CALL || opc == CPUI_CALLIND || opc == CPUI_CALLOTHER ||
opc == CPUI_STORE || opc == CPUI_SEGMENTOP || opc == CPUI_CPOOLREF ||
opc == CPUI_NEW || opc == CPUI_MULTIEQUAL || opc == CPUI_INDIRECT)
return false;
if (opc == CPUI_BRANCH) {
vn = op->getInput(0);
if (vn->space->getType() != IPTR_CONSTANT) // Only relative branching allowed
return false;
}
vn = op->getOutput();
if (vn != (VarnodeData *)0) {
if (vn->space->getType() != IPTR_INTERNAL)
return false; // Can only write to temporaries
}
for(int4 j=0;j<op->numInput();++j) {
vn = op->getInput(j);
if (vn->space->getType() == IPTR_PROCESSOR)
return false; // Cannot read from normal registers
}
}
return true;
}
/// \brief Retrieve the value of a Varnode from the current machine state
///
/// If the Varnode is a temporary registers, the storage offset is used to look up
/// the value from the machine state cache. If the Varnode represents a RAM location,
/// the value is pulled directly out of the load-image.
/// If the value does not exist, a "Read before write" exception is thrown.
/// \param vn is the Varnode to read
/// \return the retrieved value
uintb EmulateSnippet::getVarnodeValue(VarnodeData *vn) const
{
AddrSpace *spc = vn->space;
if (spc->getType() == IPTR_CONSTANT)
return vn->offset;
if (spc->getType() == IPTR_INTERNAL) {
map<uintb,uintb>::const_iterator iter;
iter = tempValues.find(vn->offset);
if (iter != tempValues.end())
return (*iter).second; // We have seen this varnode before
throw LowlevelError("Read before write in snippet emulation");
}
return getLoadImageValue(vn->space,vn->offset,vn->size);
}
/// \brief Retrieve a temporary register value directly
///
/// This allows the user to obtain the final value of the snippet calculation, without
/// having to have the Varnode object in hand.
/// \param offset is the offset of the temporary register to retrieve
/// \return the calculated value or 0 if the register was never written
uintb EmulateSnippet::getTempValue(uintb offset) const
{
map<uintb,uintb>::const_iterator iter = tempValues.find(offset);
if (iter == tempValues.end())
return 0;
return (*iter).second;
}