This repository has been archived by the owner on Jan 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalloctrace.cpp
1244 lines (1112 loc) · 43.7 KB
/
malloctrace.cpp
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
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*BEGIN_LEGAL
Intel Open Source License
Copyright (c) 2002-2015 Intel Corporation. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. Redistributions
in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution. Neither the name of
the Intel Corporation nor the names of its contributors may be used to
endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
END_LEGAL */
#include "pin.H"
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <vector>
#include <map>
#include "typeparser.h"
#include "heapmemory.h"
#include "stackmemory.h"
#include "objdumpparser.h"
#include "typerefiner.h"
#include "callstack.h"
/* ===================================================================== */
/* Names of malloc and free */
/* ===================================================================== */
#if defined(TARGET_MAC)
#define MALLOC "_malloc"
#define FREE "_free"
#else
#define MALLOC "malloc"
#define FREE "free"
#endif
/* ===================================================================== */
/* Global Variables */
/* ===================================================================== */
std::ofstream TraceFile;
std::ofstream TraceXMLFile;
std::map<std::string, std::vector<HowardType*>*>* typeDB;
std::map<std::string, std::vector<HowardType*>*>* stackTypeDB;
ADDRINT last_size;
ADDRINT last_ret;
int event_id = 0;
// Flag to indicate that main was called and that the
// tracing should start
int start_trace = 0;
Heap *heap = new Heap();
Stack *stack = new Stack();
CallStack *callStackReturnAddr = new CallStack();
/* ===================================================================== */
/* Commandline Switches */
/* ===================================================================== */
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o",
"malloctrace.out", "specify trace file name");
KNOB<string> KnobInputFile(KNOB_MODE_WRITEONCE, "pintool", "i", "-",
"specify file name for input files");
KNOB<string> KnobPath(KNOB_MODE_WRITEONCE, "pintool", "p", "-",
"specify absolut path of project");
KNOB<string> KnobCallStackPath(KNOB_MODE_WRITEONCE, "pintool", "c", "-",
"specify absolut path of callstack file");
KNOB<string> KnobWritePath(KNOB_MODE_WRITEONCE, "pintool", "w", "-",
"specify absolut path for writing xml files. Can end with the beginning of a file name, as trace/types.xml will be simply attached to string.");
/* ===================================================================== */
/* ===================================================================== */
/* Forward declerations */
/* ===================================================================== */
/* ===================================================================== */
/* ===================================================================== */
/* Analysis routines */
/* ===================================================================== */
VOID Arg1Before(CHAR * name, ADDRINT size, ADDRINT sp, ADDRINT funcStartAddr,
ADDRINT addr) {
if (start_trace) {
std::cout << " malloc called " << name << " (" << size << ")" << endl;
TraceFile << name << "(" << size << ")" << endl;
last_size = size;
last_ret = 0;
TraceFile << "Arg1Before: address: " << std::hex << addr
<< " funcStartAddr: " << std::hex << funcStartAddr
<< " stack pointer: " << std::hex << sp << endl;
/*call_stack.push_back(addr);
std::cout << "\tstack after push: " << std::endl;
printStack();*/
} else {
TraceFile << " malloc not enabled yet " << endl;
}
}
VOID FreeArg1Before(CHAR * name, ADDRINT addr) {
TraceFile << name << "(" << std::hex << addr << ")" << endl;
event_id++;
TraceXMLFile << "<event id=\"" << std::dec << event_id << "\">" << endl;
TraceXMLFile << "<free>" << endl;
TraceXMLFile << "<sourceLocation>" << endl;
TraceXMLFile << "<file>dontcare</file>" << endl;
TraceXMLFile << "<line>0</line>" << endl;
TraceXMLFile << "<column>0</column>" << endl;
TraceXMLFile << "</sourceLocation>" << endl;
TraceXMLFile << "<argCodeFragment>dontcare</argCodeFragment>" << endl;
TraceXMLFile << "<argValue>" << std::hex << addr << "</argValue>" << endl;
TraceXMLFile << "</free>" << endl;
TraceXMLFile << "</event>" << endl;
std::cout << "Freeing heap memory chunk..." << std::endl;
std::cout << "Heap before free:" << std::endl;
heap->printHeap();
heap->removeHeapChunkByAddress(addr);
std::cout << "Heap after free:" << std::endl;
heap->printHeap();
}
VOID MallocAfter(ADDRINT ret) {
TraceFile << " returns " << std::hex << ret << endl;
event_id++;
std::string full_stack = callStackReturnAddr->print_call();
// Here one can switch between merging the call stack based on the
// last element (BUT you need to prepare the howard files accordingly)
//std::string stack = callStackReturnAddr->last_callstack_element();
std::string stack = callStackReturnAddr->print_call();
TraceFile << "checking call stack: " << stack << " full call stack: "
<< full_stack << std::endl;
std::map<std::string, std::vector<HowardType*>*>::iterator csTypeIt =
typeDB->find(stack);
std::string mallocedType = "";
if (typeDB->end() != csTypeIt) {
HowardType *foundType = (csTypeIt->second)->front();
mallocedType = foundType->getType();
if (foundType->getSize() < last_size) {
TraceFile << "size of type " << mallocedType
<< " is smaller than malloced size, expanding: type size: "
<< foundType->getSize() << " malloced size: " << last_size
<< std::endl;
std::cout << "size of type " << mallocedType
<< " is smaller than malloced size, expanding: type size: "
<< foundType->getSize() << " malloced size: " << last_size
<< std::endl;
foundType->setSizeAndEnd(last_size);
std::cout << "new size of type " << mallocedType << " is: "
<< foundType->getSize() << std::endl;
}
TraceFile << "call stack: " << stack << " has type: " << mallocedType
<< std::endl;
HeapChunk *heapChunk = new HeapChunk((unsigned long int) ret,
(unsigned long int) last_size, stack,
(csTypeIt->second)->front());
heap->addHeapChunk(heapChunk);
heap->printHeap();
TraceXMLFile << "<!-- call stack found: " << stack << " -->\n"
<< std::endl;
} else {
mallocedType = "error";
TraceFile << "call stack not found: ." << stack << "." << std::endl;
std::cout << "call stack not found: ." << stack << "." << std::endl;
TraceXMLFile << "<!-- call stack not found: ." << stack << ". -->\n"
<< std::endl;
}
// Need to simulate the return, as malloc is not tracked via callInstruction/returnInstruction
//returnInstruction();
TraceXMLFile << "<event id=\"" << std::dec << event_id << "\">" << endl;
TraceXMLFile
<< "<memory-write>\n\
<sourceLocation>\n\
<file>sll-with-slls-same-type.c</file>\n\
<line>14</line>\n\
<column>0</column>\n\
</sourceLocation>\n\
<lval>\n\
<address>"
<< "0x08" << "</address>\n\
<type>struct " << mallocedType
<< " *</type>\n\
<codeFragment>statically chosen 0x08 as address</codeFragment>\n\
</lval>\n\
<content>\n\
<content>"
<< std::hex << ret << "</content>\n\
<lvalDerefType>struct "
<< mallocedType
<< "</lvalDerefType>\n\
<rhsCodeFragment>malloc</rhsCodeFragment>\n\
</content>\n\
<memory-allocation>\n\
<malloc>\n\
<argCodeFragment>sizeof(*(*start))</argCodeFragment>\n\
<argValue>"
<< std::dec << last_size
<< "</argValue>\n\
</malloc>\n\
</memory-allocation>\n\
</memory-write>\n\
</event>"
<< endl;
}
/* ===================================================================== */
/* Instrumentation routines */
/* ===================================================================== */
unsigned long offsetInImage(ADDRINT addr) {
unsigned long lowImgAddr = callStackReturnAddr->findLowAddressForAddr(addr);
return addr - lowImgAddr;
}
VOID startTrace(ADDRINT sp, ADDRINT funcStartAddr) {
unsigned long imgOffset = offsetInImage(funcStartAddr);
std::cout << "start trace: value of SP(!): " << std::hex << sp
<< " function start addr: " << funcStartAddr << " offset in image: "
<< imgOffset << endl;
// Here it seems, that sp is already decremented.
// See other sp recording in callInstruction for offset!
callStackReturnAddr->push_fun_start(funcStartAddr, sp);
callStackReturnAddr->print();
std::cout << " startTrace: " << endl;
start_trace = 1;
}
VOID startTraceAfter() {
callStackReturnAddr->pop_fun_start();
callStackReturnAddr->print();
std::cout << "start traceAfter: " << endl;
start_trace = 0;
}
VOID Image(IMG img, VOID *v) {
callStackReturnAddr->push(img);
// Instrument the malloc() and free() functions. Print the input argument
// of each malloc() or free(), and the return value of malloc().
RTN mainRtn = RTN_FindByName(img, "main");
if (RTN_Valid(mainRtn)) {
RTN_Open(mainRtn);
ADDRINT function_address = RTN_Address(mainRtn);
std::cout << "intercepting main: start address: " << std::hex
<< function_address << endl;
// Instrument main() to print the input argument value and the return value.
RTN_InsertCall(mainRtn, IPOINT_BEFORE, (AFUNPTR) startTrace,
IARG_REG_VALUE, REG_STACK_PTR, IARG_ADDRINT, function_address,
IARG_END);
RTN_InsertCall(mainRtn, IPOINT_AFTER, (AFUNPTR) startTraceAfter,
IARG_END);
RTN_Close(mainRtn);
}
// Find the malloc() function.
RTN mallocRtn = RTN_FindByName(img, MALLOC);
if (RTN_Valid(mallocRtn)) {
RTN_Open(mallocRtn);
ADDRINT function_address = RTN_Address(mallocRtn);
// Instrument malloc() to print the input argument value and the return value.
RTN_InsertCall(mallocRtn, IPOINT_BEFORE, (AFUNPTR) Arg1Before,
IARG_ADDRINT, MALLOC, IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_REG_VALUE, REG_STACK_PTR, IARG_ADDRINT, function_address,
IARG_INST_PTR, IARG_RETURN_IP, IARG_END);
RTN_InsertCall(mallocRtn, IPOINT_AFTER, (AFUNPTR) MallocAfter,
IARG_FUNCRET_EXITPOINT_VALUE, IARG_END);
RTN_Close(mallocRtn);
}
// Find the free() function.
RTN freeRtn = RTN_FindByName(img, FREE);
if (RTN_Valid(freeRtn)) {
RTN_Open(freeRtn);
// Instrument free() to print the input argument value.
RTN_InsertCall(freeRtn, IPOINT_BEFORE, (AFUNPTR) FreeArg1Before,
IARG_ADDRINT, FREE, IARG_FUNCARG_ENTRYPOINT_VALUE, 0, IARG_END);
RTN_Close(freeRtn);
}
}
/* ===================================================================== */
VOID *last_IP = NULL;
// Print a memory read record
VOID RecordMemRead(VOID * ip, VOID * addr, UINT32 memOp) {
int readVal = *((int*) addr);
if (last_IP == ip) {
TraceFile << std::hex << ip << ": ip not changed R " << std::hex << addr
<< " value: " << std::dec << readVal << endl;
} else {
TraceFile << std::hex << ip << ": ip changed R " << std::hex << addr
<< " value: " << std::dec << readVal << endl;
}
last_IP = ip;
}
// Print a memory write record
VOID RecordMemWrite(VOID * ip, VOID * addr, UINT32 memOp)
{
if (last_IP == ip) {
TraceFile << std::hex << ip << ": ip not changed W " << std::hex << addr
<< endl;
} else {
TraceFile << std::hex << ip << ": ip changed W " << std::hex << addr
<< endl;
}
last_IP = ip;
}
VOID RecordMemWriteAfter(VOID * ip, VOID * addr, UINT32 memOp) {
int readVal = *((int*) addr);
TraceFile << std::hex << ip << ": after W " << std::hex << addr
<< " value: " << std::hex << *((int*) addr) << " dec: " << std::dec
<< readVal << endl;
}
HowardType* getTypeForAddressTarget(unsigned long offset,
std::vector<HowardType*> *foundTypes) {
// At the moment always return the innermost (most specific) struct
// => start from the back and return first compound element
std::vector<HowardType*>::reverse_iterator foundTypesIt =
foundTypes->rbegin();
for (; foundTypes->rend() != foundTypesIt; foundTypesIt++) {
HowardType *sub = (*foundTypesIt);
if (sub->getIsCompound()) {
return sub;
}
}
TraceFile << "getTypeForAddressTarget: nothing found." << endl;
return NULL;
}
HowardType* getTypeForAddressSource(unsigned long offset,
std::vector<HowardType*> *foundTypes) {
std::cout << "getTypeForAddressSource: entered." << endl;
return foundTypes->size() > 0 ? foundTypes->back() : NULL;
}
HowardType* getTypeForAddress(unsigned long addr,
HowardType*(*typeProducer)(unsigned long, std::vector<HowardType*>*)) {
std::vector<HeapChunk*>::iterator memChunkIt2 =
heap->findHeapChunkByAddress(addr);
if (heap->getHeapChunksEndIt() != memChunkIt2) {
unsigned long offset = (unsigned long) addr
- (*memChunkIt2)->getStartAddr();
cout << "Addr " << std::hex << addr
<< " is located on heap chunk with type: "
<< (*memChunkIt2)->getType()->getType() << endl;
TraceXMLFile << "<!-- " << "Addr " << std::hex << addr
<< " is located on heap chunk with type: "
<< (*memChunkIt2)->getType()->getType() << "-->\n" << std::endl;
cout << "\toffset: " << offset << std::endl;
HowardType *type = (*memChunkIt2)->getType();
std::vector<HowardType*> *foundTypes = type->findTypeForOffset(offset);
std::vector<HowardType*>::iterator foundTypesIt = foundTypes->begin();
for (; foundTypes->end() != foundTypesIt; foundTypesIt++) {
HowardType *sub = (*foundTypesIt);
cout << "\ttype:" << sub->getType() << " offset: "
<< sub->getOffset() << endl;
TraceXMLFile << "<!-- \ttype:" << sub->getType() << " offset: "
<< sub->getOffset() << "-->" << std::endl;
}
// Let the typeProducer decide which type to return here
cout << "\tcalling typeProducer:" << endl;
TraceXMLFile << "<!-- calling typeProducer: found on heap addr: "
<< std::hex << addr << "-->\n" << std::endl;
HowardType * retType = typeProducer(offset, foundTypes);
if (retType == NULL) {
TraceXMLFile << "<!-- calling typeProducer: returned NULL -->\n"
<< std::endl;
} else {
TraceXMLFile << "<!-- calling typeProducer: returned "
<< retType->getType() << " -->\n" << std::endl;
}
return retType;
} else {
TraceXMLFile << "<!-- getTypeForAddress: not found on heap addr: "
<< std::hex << addr << "-->\n" << std::endl;
cout << "Addr not found on heap: " << std::hex << addr << endl;
return NULL;
}
}
std::vector<HowardType*> * inspectStackAddress(unsigned long addr) {
StackChunk* memChunk = callStackReturnAddr->findStackChunkByAddress(addr);
std::cout << "inspectStackAddress: addr: " << std::hex << addr << std::endl;
// Addr was not found, see if this is a red zone access
if (memChunk == NULL) {
// This part should only be executed if we actually have a red zone access
// and there was no previous red zone access which already subsumes the current one
bool isInRedZone = callStackReturnAddr->addrInRedZone(addr);
if (!isInRedZone) {
std::cout << "addr not found in red zone: " << endl;
return NULL;
}
std::cout << "addr " << std::hex << addr << " is in red zone: "
<< std::endl;
std::string funcStartAddr = callStackReturnAddr->getLastFuncStartAddr();
std::map<std::string, std::vector<HowardType*>*>::iterator csTypeIt =
stackTypeDB->find(funcStartAddr);
if (stackTypeDB->end() != csTypeIt) {
std::cout << "Found types for funcStartAddr: " << funcStartAddr
<< std::endl;
callStackReturnAddr->addStackChunk(addr, 0, (*csTypeIt).second,
event_id, TraceXMLFile);
} else {
std::cout << "No entry found for funcStartAddr: " << funcStartAddr
<< endl;
callStackReturnAddr->addStackChunk(addr, 0, NULL, event_id,
TraceXMLFile);
}
}
// Try again with red zone fix
StackChunk* stackChunk = callStackReturnAddr->findStackChunkByAddress(addr);
if (stackChunk != NULL) {
std::cout << "addr: " << std::hex << addr
<< " found on live stack! stack belongs to function: "
<< stackChunk->getFuncStartAddr() << endl;
// Stack grows downward: start address >= addr
std::cout << "addr: " << std::hex << addr
<< " stackChunk->getStartAddr(): " << stackChunk->getStartAddr()
<< endl;
unsigned long offset = stackChunk->getStartAddr()
- (unsigned long) addr;
std::cout << "\toffset: " << offset << " (dec: " << std::dec << offset
<< ")" << std::endl;
std::vector<HowardType*> *foundTypes = stackChunk->findTypeForOffset(
offset);
std::cout << "findTypeForOffset done" << std::endl;
if (foundTypes != NULL) {
std::vector<HowardType*>::iterator foundTypesIt =
foundTypes->begin();
for (; foundTypes->end() != foundTypesIt; foundTypesIt++) {
HowardType *sub = (*foundTypesIt);
cout << "\ttype:" << sub->getType() << " offset: "
<< sub->getOffset() << endl;
}
return foundTypes;
} else {
std::cout << "no type found for offset: " << std::hex << offset
<< endl;
return NULL;
}
} else {
std::cout << "addr: " << std::hex << addr << " not found on live stack!"
<< endl;
}
return NULL;
}
VOID RecordMemWriteRegToMem(VOID *disassembled, CONTEXT *ctxt, VOID * ip,
VOID * addr, REG reg, ADDRINT regval) {
char *disassembledChar = (static_cast<char*>(disassembled));
if (!start_trace) {
std::cout << "RecordMemWriteRegToMem not enabled yet" << std::endl;
return;
}
TraceFile << std::hex << ip << ": W to addr: " << std::hex << addr << endl;
TraceFile << " Reg : " << REG_StringShort(reg) << " Value: " << regval
<< endl;
std::cout << std::hex << ip << ": W to addr: " << std::hex << addr << endl;
std::cout << " Reg : " << REG_StringShort(reg) << " Value: " << regval
<< endl;
cout << "Inspecting regval, which is the address of the target" << endl;
TraceXMLFile << "<!-- inspecting instruction: " << hex << ip << ": "
<< disassembledChar << "-->\n" << std::endl;
cout << "<!-- inspecting instruction: " << hex << ip << ": "
<< disassembledChar << "-->\n" << std::endl;
TraceXMLFile << "<!-- inspecting target -->\n" << std::endl;
HowardType *targetType = getTypeForAddress((unsigned long) regval,
&getTypeForAddressTarget);
cout << "Inspecting addr, which is the address of the source" << endl;
TraceXMLFile << "<!-- inspecting source -->\n" << std::endl;
HowardType *sourceType = getTypeForAddress((unsigned long) addr,
&getTypeForAddressSource);
// First try to inspect the source, if it is not found on the heap
if (sourceType == NULL) {
TraceXMLFile << "<!-- not found on heap addr: " << std::hex << addr
<< "-->\n" << std::endl;
std::cout << "inspecting sourceType: " << std::hex << addr << endl;
TraceXMLFile << "<!-- inspectStackAddress addr: " << std::hex << addr
<< "-->\n" << std::endl;
std::vector<HowardType*> *types = inspectStackAddress(
(unsigned long) addr);
TraceXMLFile << "<!-- inspectStackAddress done addr: " << std::hex
<< addr << "-->\n" << std::endl;
if (types == NULL) {
TraceXMLFile << "<!-- not found on stack addr: " << std::hex << addr
<< "-->\n" << std::endl;
std::cout << "\tno type found for: " << std::hex << addr
<< std::endl;
std::cout << "\tstopping." << std::endl;
return;
} else {
std::cout << "\ttypes found for addr " << std::hex << addr << ": "
<< types->size() << std::endl;
TraceXMLFile << "<!-- found on stack addr: " << std::hex << addr
<< "-->\n" << std::endl;
if (types->size() <= 0) {
std::cout << "\ttypes size not greater 0. skipping"
<< std::endl;
return;
}
// The innermost type should be the primitive type:
HowardType *primitiveType = types->back();
if (primitiveType->getIsCompound()) {
std::cout
<< "\terror! found a compound type instead of a primitive type: "
<< primitiveType->getType() << std::endl;
std::cout << "\tstopping." << std::endl;
TraceXMLFile << "<!-- compound found for addr: " << std::hex
<< addr << "-->\n" << std::endl;
return;
} else {
std::cout << "\tfound type for addr " << std::hex << addr
<< ": " << types->size() << std::endl;
TraceXMLFile << "<!-- found primitiveType "
<< primitiveType->getType() << " for addr: " << std::hex
<< addr << "-->\n" << std::endl;
sourceType = primitiveType;
}
TraceXMLFile << "<!-- done with stack addr: " << std::hex << addr
<< "-->\n" << std::endl;
}
}
// Now check, if the source type is a pointer. If not, skip!
if (sourceType->getType().find("VOID*") != std::string::npos ||
// At the moment treat INT64 like a pointer, as this needs to be fixed by Chen
sourceType->getType().find("INT64") != std::string::npos) {
std::cout << "\tfound pointer type for source: "
<< sourceType->getType() << std::endl;
} else {
std::cout << "\tsource is not of pointer type: "
<< sourceType->getType() << std::endl;
std::cout << "\tstopping." << std::endl;
TraceXMLFile << "<!-- skipping write as type is not a pointer: "
<< sourceType->getType() << " ip: " << std::hex << ip
<< ": W to addr: " << std::hex << addr << "-->\n" << std::endl;
return;
}
// We found a suitable source type (pointer). Now check if the target was not found on the heap.
if (targetType == NULL) {
std::cout << "inspecting targetType: " << std::hex << regval << endl;
std::vector<HowardType*> *types = inspectStackAddress(
(unsigned long) regval);
if (types == NULL) {
std::cout << "\tno type found for: " << std::hex << regval
<< std::endl;
std::cout << "\tstopping." << std::endl;
return;
} else {
std::cout << "\ttypes found for addr " << std::hex << regval << ": "
<< types->size() << std::endl;
// Offset does not matter: 0
targetType = getTypeForAddressTarget(0, types);
// Still not found
if (targetType == NULL) {
std::cout << "\ttype producer failed! stopping. " << std::endl;
return;
}
}
}
event_id++;
TraceXMLFile << "<event id=\"" << std::dec << event_id << "\">\n\
<!--"
<< std::hex << ip << ": W to addr: " << std::hex << addr
<< "-->\n\
<!--" << " Reg : " << REG_StringShort(reg)
<< " Value: " << regval
<< "-->\n\
<memory-write>\n\
<sourceLocation>\n\
<file>dummy</file>\n\
<line>0</line>\n\
<column>0</column>\n\
</sourceLocation>\n\
<lval>\n\
<address>"
<< std::hex << addr << "</address>\n";
std::stringstream typeTag;
if (targetType != NULL && sourceType != NULL) {
cout << "Source and target found" << endl;
cout << "\tsource type: " << sourceType->getType() << endl;
cout << "\ttarget type: " << targetType->getType() << endl;
// Type the source pointer with the target
if (targetType->getIsCompound()) {
typeTag << "struct ";
}
typeTag << targetType->getType() << " *";
if (sourceType->getType().find("VOID*") != std::string::npos) {
TraceXMLFile << "<type>" << typeTag.str()
<< "</type><!-- sourceType: " << sourceType->getType()
<< " -->\n";
} else {
TraceXMLFile << "<type>" << typeTag.str()
<< "</type><!-- sourceType: " << sourceType->getType()
<< " -->\n";
}
} else if (targetType != NULL && sourceType == NULL) {
typeTag << "" << "VOID *";
TraceXMLFile << "<type>" << typeTag.str() << "</type>\n";
}
TraceXMLFile
<< "<codeFragment>*start</codeFragment>\n\
</lval>\n\
<content>\n\
<content>"
<< std::hex << regval << "</content>\n";
std::string cutType = typeTag.str().substr(0, typeTag.str().size() - 1);
if (cutType.at(cutType.size() - 1) == ' ') {
cutType = cutType.substr(0, cutType.size() - 1);
}
TraceXMLFile << "<lvalDerefType>" << cutType << "</lvalDerefType>\n";
/* <lvalDerefType>struct parent</lvalDerefType>\n\ */
TraceXMLFile
<< "<rhsCodeFragment>(struct parent *)tmp</rhsCodeFragment>\n\
</content>\n\
</memory-write>\n\
</event>"
<< endl;
}
/* TR: Taken from structtrace.cpp: Check if address belongs to main executable */
bool isMain(ADDRINT ret) {
PIN_LockClient();
IMG im = IMG_FindByAddress(ret);
PIN_UnlockClient();
int inMain = IMG_Valid(im) ? IMG_IsMainExecutable(im) : 0;
return inMain;
}
VOID callInstruction(ADDRINT addr, ADDRINT retaddr, ADDRINT sp,
ADDRINT targetAddr, ADDRINT bp, ADDRINT ins_size) {
if (start_trace) {
std::cout << "Call instruction " << std::hex << addr
<< " with return address: " << std::hex << retaddr
<< " push call stack. Address of stack pointer: " << std::hex
<< sp << endl;
std::cout << "Call instruction: value of SP(!): " << std::hex << sp
<< " value of BP: " << std::hex << bp << endl;
// Stack pointer needs adjustment, as there seems to be a
// difference between RTN_InsertCall and instrumenting
// an actual call instruction in terms of the SP value.
// See other last sp saving in startTrace().
callStackReturnAddr->push(addr, (addr + ((USIZE) ins_size)), targetAddr,
sp - 0x8);
callStackReturnAddr->print();
} else {
std::cout << "Call instruction not enabled yet" << endl;
}
}
VOID returnInstruction(ADDRINT ip, ADDRINT sp) {
std::cout << "\tstack after pop: " << std::endl;
if (start_trace) {
ADDRINT retAddr = (*((ADDRINT*) sp));
bool popped = callStackReturnAddr->pop(retAddr, event_id, TraceXMLFile,
stackTypeDB);
if (popped) {
std::cout << "Return address " << std::hex << retAddr
<< " was popped." << std::endl;
} else {
std::cout << "Return address " << std::hex << retAddr
<< " was not popped." << std::endl;
}
callStackReturnAddr->print();
} else {
TraceFile << "Return instruction not enabled yet" << endl;
}
}
VOID pushRbp(ADDRINT sp) {
if (start_trace) {
std::cout << "Inspecting push of rbp: value of SP(!): " << std::hex
<< sp << endl;
} else {
std::cout << "Inspecting push of rbp: value of SP(!): not enabled yet "
<< endl;
}
}
VOID pushRbpAfter(ADDRINT sp) {
if (start_trace) {
std::cout << "Inspecting push of rbp after: value of SP(!): " << sp
<< endl;
} else {
std::cout
<< "Inspecting push of rbp after: value of SP(!): not enabled yet "
<< endl;
}
}
void stackPointerManipulation(ADDRINT sp, UINT32 size, ADDRINT bp) {
if (start_trace) {
std::cout << "Called stackPointerManipulation: SP: " << std::hex << sp
<< " BP: " << bp << " size: " << std::dec << size << "(0x"
<< std::hex << size << ")" << endl;
callStackReturnAddr->print();
std::string funcStartAddr =
callStackReturnAddr->getLastFuncStartAddrImgOffset();
std::cout << "funcStartAddr : " << funcStartAddr << std::endl;
std::map<std::string, std::vector<HowardType*>*>::iterator csTypeIt =
stackTypeDB->find(funcStartAddr);
if (stackTypeDB->end() != csTypeIt) {
callStackReturnAddr->addStackChunk(sp, size, (*csTypeIt).second,
event_id, TraceXMLFile);
} else {
std::cout << "No entry found for funcStartAddr: " << funcStartAddr
<< endl;
callStackReturnAddr->addStackChunk(sp, size, NULL, event_id,
TraceXMLFile);
}
callStackReturnAddr->print();
} else {
std::cout << "Called stackPointerManipulation: SP: not enabled yet "
<< endl;
}
}
unsigned long registerToAddressMapping(const char *reg_name) {
unsigned long baseAddr = 0x10;
int numRegisters = 64;
const char *registers[] = { "al", "ax", "bl", "bp", "bpl", "bx", "cl", "cx",
"di", "dil", "dl", "dx", "eax", "ebp", "ebx", "ecx", "edi", "edx",
"esi", "esp", "r10", "r10b", "r10d", "r10w", "r11", "r11b", "r11d",
"r11w", "r12", "r12b", "r12d", "r12w", "r13", "r13b", "r13d",
"r13w", "r14", "r14b", "r14d", "r14w", "r15", "r15b", "r15d",
"r15w", "r8", "r8b", "r8d", "r8w", "r9", "r9b", "r9d", "r9w", "rax",
"rbp", "rbx", "rcx", "rdi", "rdx", "rsi", "rsp", "si", "sil", "sp",
"spl" };
for (int i = 0; i < numRegisters; i++) {
if (strcmp(reg_name, registers[i]) == 0) {
std::cout << "register found: " << reg_name << std::endl;
return baseAddr;
}
// Eight bytes per register, which is not correct but a plain hack
baseAddr += 0x8;
}
std::cout << "Unsupported register found: " << reg_name << std::endl;
std::cout << "Stopping. " << std::endl;
exit(1);
return 0xdeadbeef;
}
/*
* Keep track of the current content of a register to filter out relevant events
*/
typedef unsigned long registerID;
enum registerState {
NOPOINTER, POINTER
};
std::map<registerID, registerState> regShadow;
registerState getCurrentStateForRegister(unsigned long regID) {
std::map<registerID, registerState>::iterator regIt = regShadow.find(regID);
if (regShadow.end() == regIt) {
std::cout << "getCurrentStateForRegister: not found regID: " << regID
<< " adding " << std::endl;
regShadow[regID] = NOPOINTER;
}
std::cout << "getCurrentStateForRegister: regID: " << regID << " -> "
<< regShadow[regID] << std::endl;
return regShadow[regID];
}
VOID RecordMemReadMemToReg(VOID *disassembled, VOID *reg_name, ADDRINT ip,
VOID *read_ea) {
UINT64 readValue = (*static_cast<UINT64*>(read_ea));
char *disassembledChar = (static_cast<char*>(disassembled));
char *regNameChar = (static_cast<char*>(reg_name));
std::cout << "RecordMemReadMemToReg: reg: " << regNameChar << " read_ea: "
<< std::hex << read_ea << " *read_ea: " << std::hex << readValue
<< std::endl;
std::cout << "\t(" << std::hex << ip << "): " << disassembledChar
<< std::endl;
if (start_trace) {
unsigned long regId = registerToAddressMapping(regNameChar);
registerState curRegState = getCurrentStateForRegister(regId);
std::vector<HeapChunk*>::iterator heapIt = heap->findHeapChunkByAddress(
readValue);
bool valueIsAddress = heap->getHeapChunksEndIt() != heapIt;
// We will stop immediately, if the register currently does not store a pointer
// and the value we are writing is not an address. => Nothing changes on the register state
if (curRegState == NOPOINTER && !valueIsAddress) {
TraceXMLFile
<< "<!-- skipping register event: NOPOINTER -> NOPOINTER: "
<< disassembledChar << " -->" << endl;
return;
}
// We are saving a pointer to the a register, which held a non pointer value
if (curRegState == NOPOINTER && valueIsAddress) {
TraceXMLFile << "<!-- register event: NOPOINTER -> POINTER: "
<< disassembledChar << " -->" << endl;
regShadow[regId] = POINTER;
}
// We are saving a non pointer value to a register, which held a pointer
if (curRegState == POINTER && !valueIsAddress) {
// Always make the readValue a null pointer write to indicate the loss of a reference to DSI
readValue = 0;
regShadow[regId] = NOPOINTER;
TraceXMLFile << "<!-- register event: POINTER -> NOPOINTER: "
<< disassembledChar << " -->" << endl;
}
// We are saving a pointer value to register holding a pointer => Nothing changes on the register state
if (curRegState == POINTER && valueIsAddress) {
TraceXMLFile << "<!-- register event: POINTER -> POINTER: "
<< disassembledChar << " -->" << endl;
}
// Now create the register write event
event_id++;
TraceXMLFile << "<event id=\"" << std::dec << event_id << "\">" << endl;
TraceXMLFile
<< "<memory-write>\n\
<sourceLocation>\n\
<file>dontcare</file>\n\
<line>0</line>\n\
<column>0</column>\n\
</sourceLocation>\n\
<lval>\n\
<address>"
<< std::hex << registerToAddressMapping(regNameChar)
<< "</address>\n\
<type>VOID *</type>\n\
<codeFragment>writing to reg: "
<< regNameChar << ": (" << std::hex << ip << "): "
<< disassembledChar
<< "</codeFragment>\n\
</lval>\n\
<content>\n\
<content>"
<< std::hex << (readValue == 0 ? "0x" : "") << readValue
<< "</content>\n\
<lvalDerefType>VOID</lvalDerefType>\n\
<rhsCodeFragment>writing to reg: "
<< regNameChar << ": (" << std::hex << ip << "): "
<< disassembledChar
<< "</rhsCodeFragment>\n\
</content>\n\
</memory-write>\n\
</event>"
<< endl;
} else {
std::cout << "\tnot enabled yet" << std::endl;
}
}
VOID RecordMemReadMemToRegWrap(VOID *disassembled, VOID *reg_name, ADDRINT ip,
ADDRINT read_reg_value) {
std::cout << "RecordMemReadMemToRegWrap: reg: "
<< (static_cast<char*>(reg_name)) << " read_reg_value: " << std::hex
<< read_reg_value << std::endl;
RecordMemReadMemToReg(disassembled, reg_name, ip, &read_reg_value);
}
VOID recordPush(ADDRINT ip, ADDRINT sp, ADDRINT reg_value) {
std::cerr << "recordPush: sp: " << std::hex << sp << " reg_value: "
<< std::hex << reg_value << std::endl;
std::vector<HeapChunk*>::iterator heapIt = heap->findHeapChunkByAddress(
reg_value);
bool valueIsAddress = heap->getHeapChunksEndIt() != heapIt;
if (valueIsAddress) {
std::cerr << "\treg_value is an address" << std::endl;
// Now create the register write event
event_id++;
TraceXMLFile << "<event id=\"" << std::dec << event_id << "\">" << endl;
TraceXMLFile
<< "<memory-write>\n\
<sourceLocation>\n\
<file>dontcare</file>\n\
<line>0</line>\n\
<column>0</column>\n\
</sourceLocation>\n\
<lval>\n\
<address>"
<< std::hex << sp
<< "</address>\n\
<type>VOID *</type>\n\
<codeFragment>pushing to stack</codeFragment>\n\
</lval>\n\
<content>\n\
<content>"
<< std::hex << (reg_value == 0 ? "0x" : "") << reg_value
<< "</content>\n\
<lvalDerefType>VOID</lvalDerefType>\n\
<rhsCodeFragment>pushing to stack</rhsCodeFragment>\n\
</content>\n\
</memory-write>\n\
</event>"
<< endl;
} else {
std::cerr << "\treg_value is not an address" << std::endl;
}
}
// Is called for every instruction and instruments reads and writes
VOID Instruction(INS ins, VOID *v) {
if (INS_IsCall(ins)) {
std::cout << "Call number of operands: " << INS_OperandCount(ins)
<< endl;
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR) callInstruction,
IARG_INST_PTR, IARG_RETURN_IP, IARG_REG_VALUE, REG_STACK_PTR,
IARG_BRANCH_TARGET_ADDR, IARG_REG_VALUE, REG_GBP, IARG_ADDRINT,
INS_Size(ins), IARG_END);
}
if (INS_IsRet(ins)) {
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR) returnInstruction,
IARG_INST_PTR, IARG_REG_VALUE, REG_STACK_PTR, IARG_END);
}
if (INS_Opcode(ins) == XED_ICLASS_PUSH) {
std::cerr << "Found push instruction: " << INS_Disassemble(ins) << endl;
if (INS_IsMemoryWrite(ins)) {
std::cerr << "\tpush instruction is treated as a memory write "
<< INS_Disassemble(ins) << endl;
}
if (INS_OperandIsReg(ins, 0)) {
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR) recordPush,
IARG_INST_PTR, IARG_REG_VALUE, REG_STACK_PTR,
IARG_REG_VALUE, REG(INS_OperandReg(ins, 0)), IARG_END);
}
INT32 count = INS_OperandCount(ins);