-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAnalyze1.cpp
More file actions
500 lines (456 loc) · 18.2 KB
/
Analyze1.cpp
File metadata and controls
500 lines (456 loc) · 18.2 KB
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
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <assert>
#include "Misc.h"
extern BYTE *Code;
extern DWORD TotalSize;
extern DWORD CodeBase;
//extern MDisasm Disasm;
extern DWORD EP;
//---------------------------------------------------------------------------
//Scan proc calls
DWORD __fastcall Idr64Manager::AnalyzeProcInitial(DWORD fromAdr)
{
BYTE op, b1, b2;
int num, instrLen, instrLen1, instrLen2, _procSize;
int fromPos;
int curPos;
DWORD curAdr;
DWORD lastAdr = 0;
DWORD Adr, Adr1, Pos, lastMovAdr = 0;
PInfoRec recN;
DISINFO _disInfo;
fromPos = Adr2Pos(fromAdr);
if (fromPos < 0) return 0;
if (IsFlagSet(cfPass0, fromPos)) return 0;
if (IsFlagSet(cfEmbedded, fromPos)) return 0;
if (IsFlagSet(cfExport, fromPos)) return 0;
SetFlag(cfProcStart | cfPass0, fromPos);
//Don't analyze imports
if (IsFlagSet(cfImport, fromPos)) return 0;
_procSize = GetProcSize(fromAdr);
curPos = fromPos; curAdr = fromAdr;
while (1)
{
if (curAdr >= CodeBase + TotalSize) break;
//For example, cfProcEnd can be set for interface procs
if (_procSize && curAdr - fromAdr >= _procSize) break;
b1 = Code[curPos];
b2 = Code[curPos + 1];
if (!b1 && !b2 && !lastAdr) break;
instrLen = GetDisasm().Disassemble(Code + curPos, (__int64)curAdr, &_disInfo, 0);
//if (!instrLen) break;
if (!instrLen)
{
curPos++; curAdr++;
continue;
}
op = GetDisasm().GetOp(_disInfo.MnemIdx);
//Code
SetFlags(cfCode, curPos, instrLen);
//Instruction begin
SetFlag(cfInstruction, curPos);
if (curAdr >= lastAdr) lastAdr = 0;
//End
if (curAdr > fromAdr && IsFlagSet(cfProcEnd, curPos))
break;
if (_disInfo.Ret && (!lastAdr || curAdr == lastAdr))
break;
if (op == OP_MOV)
lastMovAdr = _disInfo.Offset;
if (_disInfo.Call) //call sub_XXXXXXXX
{
Adr = _disInfo.Immediate;
if (IsValidCodeAdr(Adr))
{
recN = GetInfoRec(Adr);
//If @Halt0 - end of procedure
if (recN && recN->SameName("@Halt0"))
{
if (fromAdr == EP && !lastAdr) break;
}
AnalyzeProcInitial(Adr);
}
curPos += instrLen; curAdr += instrLen;
continue;
}
if (op == OP_JMP)
{
if (curAdr == fromAdr) return 0;
if (_disInfo.OpType[0] == otMEM)
{
if (Adr2Pos(_disInfo.Offset) < 0 && (!lastAdr || curAdr == lastAdr)) return 0;
}
if (_disInfo.OpType[0] == otIMM)
{
Adr = _disInfo.Immediate;
if (Adr2Pos(Adr) < 0 && (!lastAdr || curAdr == lastAdr)) return 0;
if (GetSegmentNo(Adr) != 0 && GetSegmentNo(fromAdr) != GetSegmentNo(Adr) && (!lastAdr || curAdr == lastAdr)) return 0;
if (Adr < fromAdr && (!lastAdr || curAdr == lastAdr)) return Adr;
}
curPos += instrLen; curAdr += instrLen;
continue;
}
if (_disInfo.Conditional)
{
Adr = _disInfo.Immediate;
if (IsValidCodeAdr(Adr))
{
if (Adr >= fromAdr && Adr > lastAdr) lastAdr = Adr;
}
curPos += instrLen; curAdr += instrLen;
continue;
}
curPos += instrLen; curAdr += instrLen;
}
}
//---------------------------------------------------------------------------
//Create XRefs
//Scan procedure calls (include constructors and destructors)
//Calculate size of stack for arguments
void __fastcall Idr64Manager::AnalyzeProc1(DWORD fromAdr, char xrefType, DWORD xrefAdr, int xrefOfs, bool maybeEmb)
{
BYTE op, b1, b2;
bool bpBased = false, mbemb = false;
WORD bpBase = 4;
int num, skipNum, instrLen, instrLen1, instrLen2, procSize;
DWORD b;
int fromPos, curPos, Pos, Pos1, Pos2;
DWORD curAdr, Adr, Adr1, finallyAdr, endAdr, maxAdr;
DWORD lastMovTarget = 0, lastCmpPos = 0, lastAdr = 0;
PInfoRec recN, recN1;
PXrefRec recX;
DISINFO _disInfo;
fromPos = Adr2Pos(fromAdr);
if (fromPos < 0) return;
recN = GetInfoRec(fromAdr);
//Virtual constructor - don't analyze
if (recN && recN->type.Pos("class of ") == 1) return;
if (!recN)
{
recN = new InfoRec(fromPos, ikRefine);
}
else if (recN->kind == ikUnknown || recN->kind == ikData)
{
recN->kind = ikRefine;
recN->procInfo = new InfoProcInfo;
}
//If xrefAdr != 0, add it to recN->xrefs
if (xrefAdr)
{
recN->AddXref(xrefType, xrefAdr, xrefOfs);
SetFlag(cfProcStart, Adr2Pos(xrefAdr));
}
if (maybeEmb) recN->procInfo->flags |= PF_EMBED;
//Don't analyze imports
if (IsFlagSet(cfImport, fromPos)) return;
//if (IsFlagSet(cfExport, fromPos)) return;
if (!IsFlagSet(cfPass0, fromPos))
AnalyzeProcInitial(fromAdr);
//If Pass1 was set skip analyze
if (IsFlagSet(cfPass1, fromPos)) return;
SetFlag(cfProcStart | cfPass1, fromPos);
procSize = GetProcSize(fromAdr);
curPos = fromPos; curAdr = fromAdr;
while (1)
{
if (curAdr >= CodeBase + TotalSize) break;
//Int64Comparison
skipNum = ProcessInt64Comparison(curAdr, &maxAdr);
if (skipNum > 0)
{
if (maxAdr > lastAdr) lastAdr = maxAdr;
curPos += skipNum; curAdr += skipNum;
continue;
}
//Int64ComparisonViaStack1
skipNum = ProcessInt64ComparisonViaStack1(curAdr, &maxAdr);
if (skipNum > 0)
{
if (maxAdr > lastAdr) lastAdr = maxAdr;
curPos += skipNum; curAdr += skipNum;
continue;
}
//Int64ComparisonViaStack2
skipNum = ProcessInt64ComparisonViaStack2(curAdr, &maxAdr);
if (skipNum > 0)
{
if (maxAdr > lastAdr) lastAdr = maxAdr;
curPos += skipNum; curAdr += skipNum;
continue;
}
b1 = Code[curPos];
b2 = Code[curPos + 1];
if (!b1 && !b2 && !lastAdr) break;
instrLen = GetDisasm().Disassemble(Code + curPos, (__int64)curAdr, &_disInfo, 0);
if (curAdr > fromAdr && IsFlagSet(cfProcEnd, curPos))
{
recN->procInfo->procSize = curAdr - fromAdr;
recN->procInfo->retBytes = 0;
//ret N
if (_disInfo.OpNum)
{
recN->procInfo->retBytes = _disInfo.Immediate;//num;
}
break;
}
//if (!instrLen) break;
if (!instrLen)
{
curPos++; curAdr++;
continue;
}
op = GetDisasm().GetOp(_disInfo.MnemIdx);
//Code
SetFlags(cfCode, curPos, instrLen);
//Instruction begin
SetFlag(cfInstruction, curPos);
if (curAdr >= lastAdr) lastAdr = 0;
//Frame instructions
if (curAdr == fromAdr &&
_disInfo.MnemIdx == IDX_PUSH &&
_disInfo.OpType[0] == otREG &&
_disInfo.OpRegIdx[0] == REG_RBP) //push rbp
{
SetFlag(cfFrame, curPos);
}
if (_disInfo.MnemIdx == IDX_MOV &&
_disInfo.OpType[0] == otREG &&
_disInfo.OpRegIdx[0] == REG_RBP &&
_disInfo.OpType[1] == otREG &&
_disInfo.OpRegIdx[1] == REG_RSP) //mov rbp, rsp
{
bpBased = true;
recN->procInfo->flags |= PF_BPBASED;
recN->procInfo->bpBase = bpBase;
SetFlags(cfFrame, curPos, instrLen);
curPos += instrLen; curAdr += instrLen;
continue;
}
if (_disInfo.MnemIdx == IDX_MOV &&
_disInfo.OpType[0] == otREG &&
_disInfo.OpRegIdx[0] == REG_RSP &&
_disInfo.OpType[1] == otREG &&
_disInfo.OpRegIdx[1] == REG_RBP) //mov rsp, rbp
{
SetFlags(cfFrame, curPos, instrLen);
curPos += instrLen; curAdr += instrLen;
continue;
}
if (op == OP_JMP)
{
if (curAdr == fromAdr) break;
if (_disInfo.OpType[0] == otMEM)
{
if (Adr2Pos(_disInfo.Offset) < 0 && (!lastAdr || curAdr == lastAdr)) break;
}
if (_disInfo.OpType[0] == otIMM)
{
Adr = _disInfo.Immediate; Pos = Adr2Pos(Adr);
if (Pos < 0 && (!lastAdr || curAdr == lastAdr)) break;
if (GetSegmentNo(Adr) != 0 && GetSegmentNo(fromAdr) != GetSegmentNo(Adr) && (!lastAdr || curAdr == lastAdr)) break;
SetFlag(cfLoc, Pos);
recN1 = GetInfoRec(Adr);
if (!recN1) recN1 = new InfoRec(Pos, ikUnknown);
recN1->AddXref('J', fromAdr, curAdr - fromAdr);
if (Adr < fromAdr && (!lastAdr || curAdr == lastAdr)) break;
}
}
//End of procedure
if (_disInfo.Ret)
{
if (!lastAdr || curAdr == lastAdr)
{
//Proc end
SetFlag(cfProcEnd, curPos + instrLen);
recN->procInfo->procSize = curAdr - fromAdr + instrLen;
recN->procInfo->retBytes = 0;
//ret N
if (_disInfo.OpNum)
{
recN->procInfo->retBytes = _disInfo.Immediate;//num;
}
break;
}
}
//push
if (op == OP_PUSH)
{
SetFlag(cfPush, curPos);
bpBase += 8;
}
//pop
if (op == OP_POP) SetFlag(cfPop, curPos);
//add (sub) rsp,...
if (_disInfo.OpType[0] == otREG &&
_disInfo.OpRegIdx[0] == REG_RSP &&
_disInfo.OpType[1] == otIMM)
{
if (op == OP_ADD) bpBase -= (int)_disInfo.Immediate;
if (op == OP_SUB) bpBase += (int)_disInfo.Immediate;
//skip
SetFlags(cfSkip, curPos, instrLen);
curPos += instrLen; curAdr += instrLen;
continue;
}
////fstp [esp]
//if (Disasm.GetOp(_disInfo.MnemIdx) == IDX_FSTP && _disInfo.BaseReg == REG_RSP) SetFlag(cfFush, curPos);
//skip
if (GetDisasm().GetOp(_disInfo.MnemIdx) == IDX_SAHF || GetDisasm().GetOp(_disInfo.MnemIdx) == IDX_WAIT)
{
SetFlags(cfSkip, curPos, instrLen);
curPos += instrLen; curAdr += instrLen;
continue;
}
if (op == OP_MOV) lastMovTarget = _disInfo.Offset;
if (op == OP_CMP) lastCmpPos = curPos;
//mov rcx,rbp - embedded procedure (next instruction is not "neg rcx")
if (_disInfo.MnemIdx == IDX_MOV &&
_disInfo.OpType[0] == otREG &&
_disInfo.OpRegIdx[0] == REG_RCX &&
_disInfo.OpType[1] == otREG &&
_disInfo.OpRegIdx[1] == REG_RBP)
{
curPos += instrLen; curAdr += instrLen;
instrLen1 = GetDisasm().Disassemble(Code + curPos, (__int64)curAdr, &_disInfo, 0);
if (_disInfo.MnemIdx == IDX_NEG &&
_disInfo.OpType[0] == otREG &&
_disInfo.OpRegIdx[0] == REG_RCX)
{
curPos += instrLen1; curAdr += instrLen1;
continue;
}
mbemb = true;
continue;
}
if (_disInfo.Call)
{
SetFlag(cfCall, curPos);
Adr = _disInfo.Immediate;
if (IsValidCodeAdr(Adr) && Adr2Pos(Adr) >= 0)
{
SetFlag(cfLoc, Adr2Pos(Adr));
AnalyzeProc1(Adr, 'C', fromAdr, curAdr - fromAdr, mbemb);
mbemb = false;
recN1 = GetInfoRec(Adr);
if (recN1 && recN1->procInfo)
{
if (recN1->HasName())
{
if (recN1->SameName("@Halt0"))
{
SetFlags(cfSkip, curPos, instrLen);
if (fromAdr == EP && !lastAdr)
{
SetFlag(cfProcEnd, curPos + instrLen);
recN->procInfo->procSize = curAdr - fromAdr + instrLen;
recN->SetName("EntryPoint");
recN->procInfo->retBytes = 0;
break;
}
}
int begPos, endPos;
//If called procedure is @ClassCreate, then current procedure is constructor
if (recN1->SameName("@ClassCreate"))
{
recN->kind = ikConstructor;
//Code from instruction cmp... until this call is not sufficient (mark skipped)
begPos = GetNearestUpInstruction1(curPos, fromPos, "cmp");
if (begPos != -1) SetFlags(cfSkip, begPos, curPos + instrLen - begPos);
}
else if (recN1->SameName("@AfterConstruction"))
{
begPos = GetNearestUpInstruction2(curPos, fromPos, "test", "cmp");
endPos = GetNearestDownInstruction(curPos, "jmp");
//Code from instruction cmp... until address XXX (of jmp XXX) is not sufficient (mark skipped)
GetDisasm().Disassemble(Code + endPos, (__int64)Pos2Adr(endPos), &_disInfo, 0);
endPos = Adr2Pos(_disInfo.Immediate);
if (begPos != -1 && endPos != -1) SetFlags(cfSkip, begPos, endPos - begPos);
}
else if (recN1->SameName("@BeforeDestruction"))
SetFlag(cfSkip, curPos);
//If called procedure is @ClassDestroy, then current procedure is destructor
else if (recN1->SameName("@ClassDestroy"))
{
recN->kind = ikDestructor;
begPos = GetNearestUpInstruction2(curPos, fromPos, "test", "cmp");
if (begPos != -1) SetFlags(cfSkip, begPos, curPos + instrLen - begPos);
}
}
}
}
curPos += instrLen; curAdr += instrLen;
continue;
}
if (_disInfo.Branch && instrLen == 2) //Short relative abs jmp or cond jmp
{
Adr = _disInfo.Immediate;
if (IsValidCodeAdr(Adr))
{
Pos = Adr2Pos(Adr);
if (!IsFlagSet(cfEmbedded, Pos))//Possible branch to start of Embedded proc (for ex. in proc TextToFloat))
{
SetFlag(cfLoc, Pos);
//Mark possible start of Loop
if (Adr < curAdr)
SetFlag(cfLoop, Pos);
recN1 = GetInfoRec(Adr);
if (!recN1) recN1 = new InfoRec(Pos, ikUnknown);
recN1->AddXref('C', fromAdr, curAdr - fromAdr);
if (Adr >= fromAdr && Adr > lastAdr) lastAdr = Adr;
}
}
curPos += instrLen; curAdr += instrLen;
continue;
}
if (_disInfo.Branch && instrLen == 5) //Relative abs jmp or cond jmp
{
Adr = _disInfo.Immediate;
if (IsValidCodeAdr(Adr))
{
Pos = Adr2Pos(Adr);
SetFlag(cfLoc, Pos);
//Mark possible start of Loop
if (Adr < curAdr)
SetFlag(cfLoop, Pos);
recN1 = GetInfoRec(Adr);
if (!recN1 && Adr >= fromAdr && Adr > lastAdr) lastAdr = Adr;
}
curPos += instrLen; curAdr += instrLen;
continue;
}
//Second operand - immediate and is valid address
if (_disInfo.OpType[1] == otIMM)
{
Pos = Adr2Pos(_disInfo.Immediate);
//Immediate must be valid code address outside current procedure
if (Pos >= 0 && IsValidCodeAdr(_disInfo.Immediate) && (_disInfo.Immediate < fromAdr || _disInfo.Immediate >= fromAdr + procSize))
{
//Position must be free
if (IsFlagEmpty(Pos))
{
//No Name
if (!HasInfosAt(Pos))
{
//Address must be outside current procedure
if (_disInfo.Immediate < fromAdr || _disInfo.Immediate >= fromAdr + procSize)
{
//If valid code lets user decide later
int codeValidity = IsValidCode(_disInfo.Immediate);
if (codeValidity == 1) //Code
AnalyzeProc1(_disInfo.Immediate, 'D', fromAdr, curAdr - fromAdr, false);
}
}
}
//If slot is not free (procedure is already loaded)
else if (IsFlagSet(cfProcStart, Pos))
AnalyzeProc1(_disInfo.Immediate, 'D', fromAdr, curAdr - fromAdr, false);
}
curPos += instrLen; curAdr += instrLen;
continue;
}
curPos += instrLen; curAdr += instrLen;
}
}
//---------------------------------------------------------------------------