-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathasmCallibration.c
854 lines (677 loc) · 18.4 KB
/
asmCallibration.c
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
#include "asmCallibration.h"
#include "allocation.h"
int asm_callibrations_initialized=0;
Stream ** codestream=0;
unsigned char codebuffer[MAX_INSN_SIZE];
#define ASM_CALLIBRATIONS_INIT()\
if(asm_callibrations_initialized==0)\
x86_init(opt_none, NULL, NULL);\
asm_callibrations_initialized=1
#define ASM_CALLIBRATIONS_CLEANUP()\
if(asm_callibrations_initialized!=0)\
x86_cleanup();\
asm_callibrations_initialized=0
void asm_init(Stream ** processstream)
{
ASM_CALLIBRATIONS_INIT();
codestream=processstream;
return;
}
void asm_cleanup()
{
ASM_CALLIBRATIONS_CLEANUP();
return;
}
insn_mask * create_insn_mask(unsigned int opcount)
{
insn_mask * toreturn;
unsigned int i;
op_mask * newop;
toreturn=create(insn_mask);
memset(toreturn,0,sizeof(insn_mask));
toreturn->opcount=opcount;
for(i=0;i<opcount;i++)
{
newop=create_op_mask();
newop->next=toreturn->ops;
toreturn->ops=newop;
}
return toreturn;
}
void mask_in(void * pfield, unsigned int fieldsize)
{
memset(pfield, 0xFF, fieldsize);
return;
}
insn_mask * insn_by_type(insn_mask * maskin, enum x86_insn_type _type)
{
if(maskin==0)
maskin=create_insn_mask(0);
maskin->insn.type=_type;
MASK_INSN(maskin, type, _type);
return maskin;
}
insn_mask * insn_by_size(insn_mask * maskin, unsigned int _size)
{
if(maskin==0)
maskin=create_insn_mask(0);
maskin->insn.size=_size;
MASK_INSN(maskin, size, _size);
return maskin;
}
insn_mask * insn_by_opcount(insn_mask * maskin, unsigned int _opcount)
{
if(maskin==0)
maskin=create_insn_mask(0);
maskin->insn.operand_count=_opcount;
MASK_INSN(maskin, operand_count, _opcount);
return maskin;
}
void _recursive_delete_opmasks(op_mask * todelete)
{
if(todelete->next)
_recursive_delete_opmasks(todelete->next);
clean(todelete);
return;
}
insn_mask * insn_set_opcount(insn_mask * maskin, unsigned int opcount)
{
unsigned int i;
op_mask * newmask, * lastmask;
if(maskin==0)
maskin=create_insn_mask(opcount);
else if(maskin->opcount>opcount)
{
if(opcount>0)
{
lastmask=insn_op_mask(maskin, opcount-1);
_recursive_delete_opmasks(lastmask->next);
lastmask->next=0;
}
else
{
_recursive_delete_opmasks(maskin->ops);
maskin->ops=0;
}
maskin->opcount=opcount;
}
else if(maskin->opcount<opcount)
{
if(maskin->opcount>0)
lastmask=insn_op_mask(maskin, maskin->opcount-1);
else
lastmask=0;
for(i=maskin->opcount;i<opcount;i++)
{
newmask=create_op_mask();
if(lastmask)
lastmask->next=newmask;
else
maskin->ops=newmask;
lastmask=newmask;
}
maskin->opcount=opcount;
}
return maskin;
}
op_mask * insn_op_mask(insn_mask * maskin, unsigned int op_number)
{
unsigned int i;
op_mask * toreturn;
if(maskin==0)
maskin=create_insn_mask(op_number);//we need at least op_number of ops; it's bad practice to call this funtion with maskin==0 though
toreturn=0;
if(op_number<maskin->opcount)
{
toreturn=maskin->ops;
for(i=0;i<op_number;i++)
toreturn=toreturn->next;
}
return toreturn;
}
op_mask * create_op_mask()
{
op_mask * toreturn;
toreturn=create(op_mask);
memset(toreturn,0,sizeof(op_mask));
return toreturn;
}
op_mask * op_by_type(op_mask * maskin, enum x86_op_type _type)
{
if(maskin==0)
maskin=create_op_mask();
maskin->op.type=_type;
MASK_OP(maskin, type, _type);
return maskin;
}
op_mask * op_by_datatype(op_mask * maskin, enum x86_op_datatype _datatype)
{
if(maskin==0)
maskin=create_op_mask();
maskin->op.datatype=_datatype;
MASK_OP(maskin, datatype, _datatype);
return maskin;
}
op_mask * op_by_data(op_mask * maskin, x86_op_datatype_t _data)
{
if(maskin==0)
maskin=create_op_mask();
maskin->op.data=_data;
MASK_OP(maskin, data, _data);
return maskin;
}
void recursive_delete_insn_mask(insn_mask * todelete, BinaryTree * deleted)
{
if(BT_find(deleted, (void *)todelete))
return;
BT_insert(deleted, (void *)todelete, (void *)todelete);
if(todelete->set_next)
recursive_delete_insn_mask(todelete->set_next, deleted);
if(todelete->seq_next)
recursive_delete_insn_mask(todelete->seq_next, deleted);
if(todelete->opcount)
_recursive_delete_opmasks(todelete->ops);
clean(todelete);
return;
}
int _unsigned_int_compare(unsigned int a, unsigned int b)
{
if(a<b)
return +1;
else if(a>b)
return -1;
else
return 0;
}
void delete_insn_mask(insn_mask * todelete)
{
BinaryTree * deleted;
deleted=BT_create((BTCompare)_unsigned_int_compare);
recursive_delete_insn_mask(todelete, deleted);
BT_delete(deleted);
return;
}
void delete_op_mask(op_mask * todelete)
{
//_recursive_delete_opmasks(todelete);
clean(todelete);
return;
}
insn_mask * insn_set(insn_mask * maskin, insn_mask * option)
{
insn_mask * cur;
if(!maskin)
maskin=option;
else
{
cur=maskin;
while(cur->set_next)
cur=cur->set_next;
cur->set_next=option;
}
return maskin;
}
insn_mask * insn_seq(insn_mask * maskin, insn_mask * nextseq)
{
insn_mask * cur;
if(!maskin)
maskin=nextseq;
else
{
cur=maskin;
while(cur->seq_next)
cur=cur->seq_next;
cur->seq_next=nextseq;
}
return maskin;
}
int uint_compare(unsigned int * a, unsigned int * b)
{
if(a<b)
return +1;
else if(a>b)
return -1;
else
return 0;
}
x86_insn_t * disasm_insn_at(unsigned int address)
{
x86_insn_t * curinsn;
unsigned int curaddr;
unsigned int insnsize;
//calculate address
curaddr=address;
//new instruction
curinsn=create(x86_insn_t);
//disassemble first instruction
SSetPos((Stream **)codestream, curaddr);
SRead(codestream, codebuffer, MAX_INSN_SIZE);
insnsize=x86_disasm(codebuffer, MAX_INSN_SIZE, (unsigned int)curaddr, 0, curinsn);
if(insnsize>0)
{
SSetPos((Stream **)codestream, curaddr+insnsize);
return curinsn;
}
else
{
clean(curinsn);
return 0;
}
}
x86_insn_t * disasm_insn_next()
{
x86_insn_t * curinsn;
unsigned int curaddr;
unsigned int insnsize;
//get address address
curaddr=SGetPos(codestream);
//new instruction
curinsn=create(x86_insn_t);
//disassemble first instruction
SRead(codestream, codebuffer, MAX_INSN_SIZE);
insnsize=x86_disasm(codebuffer, MAX_INSN_SIZE, (unsigned int)curaddr, 0, curinsn);
if(insnsize>0)
{
SSetPos((Stream **)codestream, curaddr+insnsize);
return curinsn;
}
else
{
clean(curinsn);
return 0;
}
}
asm_function * disasm_chunk(unsigned int address)
{
//ASM_CALLIBRATIONS_INIT();
x86_insn_t * curinsn;
asm_function * toreturn;
toreturn=create(asm_function);
toreturn->backwards=0;
toreturn->entrypoint_address=address;
toreturn->instructions=BT_create((BTCompare)uint_compare);
//disassemble first instruction
curinsn=disasm_insn_at(address);
while(curinsn)
{
//insert instruction
BT_insert(toreturn->instructions,(void *)curinsn->addr, (void *)curinsn);
//if instruction is a jmp(, jcc) or retn then we are done
//if((curinsn->type==x86_insn_jmp)||(curinsn->type==x86_insn_jcc)||(curinsn->type==x86_insn_return))
if((curinsn->type==insn_jmp)||(curinsn->type==insn_return))
break;
//disassemble next
curinsn=disasm_insn_next();
}
toreturn->insn_enum=BT_newenum(toreturn->instructions);
return toreturn;
}
void delete_asm_function(asm_function * todelete)
{
x86_insn_t * curinsn;
//delete enumerator used to step through the instruction tree address-per-address
BT_enumdelete(todelete->insn_enum);
//delete all instructions from instruction tree
while(todelete->instructions->itemcount>0)
{
curinsn=(x86_insn_t *)BT_remove(todelete->instructions, todelete->instructions->root->key);
clean(curinsn);
}
//delete instruction tree
BT_delete(todelete->instructions);
//delete asm_function structure
clean(todelete);
return;
}
asm_function * disasm_function(unsigned int address)
{
//ASM_CALLIBRATIONS_INIT();
x86_insn_t * curinsn;
asm_function * toreturn;
unsigned int curaddr;
unsigned int targetaddr;
LinkedList * disasm_queue;//queue of addresses to disasm
if(address==0)
return 0;
//setup new disassembled function
toreturn=create(asm_function);
toreturn->backwards=0;
toreturn->entrypoint_address=address;
toreturn->instructions=BT_create((BTCompare)uint_compare);
//create queue and tree
disasm_queue=LL_create();
//insert start address
LL_push(disasm_queue, (void *)address);
while(curaddr=(unsigned int)LL_dequeue(disasm_queue))
{
if(BT_find(toreturn->instructions,(void *)curaddr)==0)
{
//disassemble first instruction
curinsn=disasm_insn_at(curaddr);
while(curinsn)
{
//insert instruction
BT_insert(toreturn->instructions,(void *)curinsn->addr, (void *)curinsn);
//if instruction is a jmp, jcc or retn then we are done
if(curinsn->type==insn_jcc)
{
//queue target chunk's address, but don't break since the chunk continues after this instruction
if((targetaddr=(unsigned int)insn_get_target(curinsn,0))!=0)
LL_push(disasm_queue,(void *)targetaddr);
}
else if(curinsn->type==insn_jmp)
{
if(!((curinsn->operands->op.type==op_expression)&&(curinsn->operands->op.data.expression.scale!=0)))//skip
{
//queue target chunk's address
if((targetaddr=(unsigned int)insn_get_target(curinsn,0))!=0)
{
LL_push(disasm_queue,(void *)targetaddr);
}
}
//and break : a non-conditional jmp to another chunk means this chunk is finished (jmp targets are treated as seperate chunks)
break;
}
else if(curinsn->type==insn_return)
break;//just break, chunk is finished
if(BT_find(toreturn->instructions,(void *)((unsigned int)(curinsn->addr+curinsn->size)))!=0)
break;//next instruction is already in the tree, so we are done with this chunk
//disassemble next
curinsn=disasm_insn_next();
}
}
}
toreturn->insn_enum=BT_newenum(toreturn->instructions);
LL_delete(disasm_queue);
return toreturn;
}
x86_insn_t * asm_next(asm_function * onfunction)
{
return (x86_insn_t *)BT_next(onfunction->insn_enum);
}
x86_insn_t * asm_previous(asm_function * onfunction)
{
return (x86_insn_t *)BT_previous(onfunction->insn_enum);
}
x86_insn_t * asm_lowest(asm_function * onfunction)
{
return (x86_insn_t *)BT_reset(onfunction->insn_enum);
}
x86_insn_t * asm_highest(asm_function * onfunction)
{
return (x86_insn_t *)BT_end(onfunction->insn_enum);
}
x86_insn_t * asm_entrypoint(asm_function * onfunction)
{
x86_insn_t * curinsn;
asm_lowest(onfunction);
while((curinsn=asm_next(onfunction))&&(curinsn->addr<((unsigned int)onfunction->entrypoint_address)))
;
return curinsn;
}
x86_insn_t * asm_jump(asm_function * onfunction, unsigned int address)
{
x86_insn_t * curinsn;
asm_lowest(onfunction);
while((curinsn=asm_next(onfunction))&&(curinsn->addr<((unsigned int)address)))
;
return curinsn;
}
asm_function * _clone_function(asm_function * toclone)
{
asm_function * toreturn;
toreturn=create(asm_function);
toreturn->entrypoint_address=toclone->entrypoint_address;
toreturn->instructions=toclone->instructions;
toreturn->insn_enum=BT_newenum(toreturn->instructions);
toreturn->insn_enum->curnode=toclone->insn_enum->curnode;
return toreturn;
}
void _delete_asm_function_clone(asm_function * clone)
{
BT_enumdelete(clone->insn_enum);
clean(clone);
return;
}
int mask_check(unsigned char * tocheck, unsigned char * maskdata, unsigned char * mask, unsigned int size)
{
unsigned int i;
for(i=0;i<size;i++)
{
if((mask[i]!=0)&&(tocheck[i]!=maskdata[i]))
return 0;
}
return 1;
}
//does not handle sequences, but does handle options
int check_insn_mask(x86_insn_t * instruction, insn_mask ** mask)
{
op_mask * curopmask;
x86_oplist_t * curop;
int ok;
//check instruction
ok=mask_check((unsigned char *)instruction, (unsigned char *)&((*mask)->insn), (unsigned char *)&((*mask)->mask), sizeof(x86_insn_t));
//check operands if any
if(ok&&(*mask)->opcount)
{
curopmask=(*mask)->ops;
curop=instruction->operands;
while(curopmask&&curop&&ok)
{
ok&=mask_check((unsigned char *)&(curop->op), (unsigned char *)&(curopmask->op), (unsigned char *)&(curopmask->mask), sizeof(x86_op_t));
curopmask=curopmask->next;
curop=curop->next;
}
}
//if this didn't match, but there is another option, check the other option
if((!ok)&&((*mask)->set_next!=0))
{
(*mask)=(*mask)->set_next;
ok=check_insn_mask(instruction, mask);
}
return ok;
}
x86_insn_t * _asm_continue(asm_function * disassembled)
{
if(disassembled->backwards==0)
return asm_next(disassembled);
else
return asm_previous(disassembled);
}
x86_insn_t * _find_insn_linear(asm_function * disassembled, insn_mask ** tofind)
{
x86_insn_t * curinsn, * nextinsn;
int ok;
insn_mask * curmask, * nextmask;
BinaryTreeNode * _backup;
//loop the chunk
while(curinsn=_asm_continue(disassembled))
{
curmask=(*tofind);
ok=check_insn_mask(curinsn, &curmask);
if(ok)
{
_backup=disassembled->insn_enum->curnode;
nextmask=curmask;
while(ok&&(nextmask)&&(nextmask->seq_next)&&(nextinsn=asm_next(disassembled)))
{
nextmask=nextmask->seq_next;
ok&=check_insn_mask(nextinsn,&nextmask);
}
disassembled->insn_enum->curnode=_backup;
}
if(ok)
{
(*tofind)=curmask;
return curinsn;
}
}
return 0;
}
x86_insn_t * _find_insn_nonlinear(asm_function * disassembled, insn_mask ** tofind)
{
x86_insn_t * curinsn, * nextinsn;
int ok;
insn_mask * curmask, * nextmask;
unsigned int curaddr;
BinaryTreeNode * _backup;
//queue and tree to keep track of what is to be searched
LinkedList * todo;
BinaryTree * done;
todo=LL_create();
done=BT_create((BTCompare)uint_compare);
//start at current address
curaddr=0;
/*if(disassembled->insn_enum->curnode)
LL_push(todo, disassembled->insn_enum->curnode->key);
else
LL_push(todo, disassembled->entrypoint_address);*/
//while there are chunks to check
do
{
if((curaddr==0)||(BT_find(done, (void *)curaddr)==0))
{
if(curaddr!=0)
curinsn=asm_jump(disassembled, curaddr);
else
curinsn=asm_next(disassembled);
while(curinsn)
{
curmask=(*tofind);
ok=check_insn_mask(curinsn, &curmask);
if(ok&&(curmask->seq_next))
{
_backup=disassembled->insn_enum->curnode;
nextmask=curmask;
while(ok&&(nextmask)&&(nextmask->seq_next)&&(nextinsn=asm_next(disassembled)))
{
nextmask=nextmask->seq_next;
ok&=check_insn_mask(nextinsn,&nextmask);
}
disassembled->insn_enum->curnode=_backup;
}
if(ok)
{
(*tofind)=curmask;
LL_delete(todo);
BT_delete(done);
return curinsn;
}
if(curinsn->type==insn_return)
break;//chunk done
else if(curinsn->type==insn_jmp)
{
LL_push(todo, (void *)insn_get_target(curinsn, 0));
break;
}
else if(curinsn->type==insn_jcc)
{
//queue jcc target
LL_push(todo, (void *)insn_get_target(curinsn, 0));
}
curinsn=asm_next(disassembled);
}
//chunk done:
BT_insert(done, (void *)curaddr, (void *)curaddr);
}
} while(curaddr=(unsigned int)LL_dequeue(todo));
LL_delete(todo);
BT_delete(done);
return 0;
}
x86_insn_t * find_insn(asm_function * disassembled, insn_mask * tofind, insn_mask ** set_out, int allchunks)
{
insn_mask ** result;
insn_mask * dummy;
if(set_out)
result=set_out;
else
result=&dummy;
(*result)=tofind;
if(allchunks)
return _find_insn_nonlinear(disassembled, result);
else
return _find_insn_linear(disassembled, result);
}
x86_op_datatype_t insn_op_data(x86_insn_t * curinsn, unsigned int op_number, enum x86_op_datatype * datatype)
{
x86_oplist_t * curop;
unsigned int i;
curop=curinsn->operands;
for(i=0;i<op_number;i++)
curop=curop->next;
if(datatype)
(*datatype)=curop->op.datatype;
return curop->op.data;
}
unsigned int op_data_to_unsigned_int(x86_op_datatype_t data, enum x86_op_datatype type)
{
switch(type)
{
case op_byte:
return (unsigned int)data.byte;
case op_word:
return (unsigned int)data.word;
case op_dword:
return (unsigned int)data.dword;
case op_qword:
return (unsigned int)data.qword;
default:
return 0;
}
}
//originally intended just to get a target address from an operand, calculating it if this is a relative operand
// however since targets stored as immediate values means returning the immediate value, it works just fine to
// read immediate operands too
unsigned int insn_get_target(x86_insn_t * curinsn, unsigned int op_number)
{
x86_oplist_t * curop;
unsigned int i;
curop=curinsn->operands;
for(i=0;i<op_number;i++)
curop=curop->next;
if(curop->op.type==op_immediate)
return op_data_to_unsigned_int(curop->op.data, curop->op.datatype);
else if(curop->op.type==op_relative_near)
return (unsigned int)((int)curinsn->addr+curinsn->size+curop->op.data.relative_near);
else if(curop->op.type==op_relative_far)
return (unsigned int)((int)curinsn->addr+curinsn->size+curop->op.data.relative_far);
else if(curop->op.type==op_absolute)
return (unsigned int)curop->op.data.absolute.offset.off32;
else if(curop->op.type==op_expression)
return (unsigned int)curop->op.data.expression.disp;
else if(curop->op.type==op_offset)
return (unsigned int)curop->op.data.offset;
return 0;
}
unsigned int insn_address(x86_insn_t * curinsn)
{
return (unsigned int)curinsn->addr;
}
x86_insn_t * find_loop(asm_function * disassembled)
{
x86_insn_t * curinsn;
unsigned int startaddr, targetaddr;
insn_mask * jmp_mask, * jcc_mask, * jump_mask, * pmask;
jmp_mask=insn_by_type(0, insn_jmp);
jcc_mask=insn_by_type(0, insn_jcc);
jump_mask=insn_set(jmp_mask, jcc_mask);
curinsn=asm_next(disassembled);
startaddr=(unsigned int)curinsn->addr;
while(curinsn)
{
pmask=jump_mask;
if(check_insn_mask(curinsn, &pmask))
{
targetaddr=insn_get_target(curinsn, 0);
if((targetaddr>=startaddr)&&(targetaddr<((unsigned int)curinsn->addr)))
{
delete_insn_mask(jump_mask);
return curinsn;
}
}
curinsn=asm_next(disassembled);
}
delete_insn_mask(jump_mask);
return 0;
}