-
Notifications
You must be signed in to change notification settings - Fork 2
/
tracer.cpp
1230 lines (1050 loc) · 33.7 KB
/
tracer.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-2010 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 "tracer.h"
#include "tracerlib.h"
#include "tracer_decode.h"
#include "resultvector.h"
#include "tracebb.h"
#include "instructions.h"
#include "tracer_decode.h"
#include "shadow.h"
#include "output.h"
#include "threads.h"
#include <unistd.h>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <stdio.h>
#include <assert.h>
#include <climits>
#include <atomic>
#define STATIC_CAST(x,y) ((x) (y))
/* ===================================================================== */
/* Names of malloc and free */
/* ===================================================================== */
#if defined(TARGET_MAC)
#define MALLOC "_malloc"
#define FREE "_free"
#else
#define MALLOC "malloc"
#define FREE "free"
#endif
#define TRACE_FUNCTION ""
#define MINTHRESHOLD 0
#define MINVECTORWIDTH 100
// Shared Globals
// Status Variables
atomic<bool> inMain;
atomic<bool> inAlloc;
unsigned tracingLevel;
int traceRegionCount;
PIN_RWMUTEX traclingLevelLock; // Protects both tracingLevel and traceRegion Count
// Execution Dtata variables
atomic<unsigned> instructionCount;
unsigned vectorInstructionCountSavings;
// Shadow Memory threadsafe if THREADSAFE defined in build of shadow.o
#ifdef NOSHAODWCACHE
ShadowMemoryNoCache shadowMemory;
#else
ShadowMemory shadowMemory;
#endif
// Data on instructions built from decoding
unordered_map<ADDRINT,instructionLocationsData > instructionLocations;
const unordered_map<ADDRINT,instructionLocationsData > &constInstructionLocations = instructionLocations;
unordered_map<ADDRINT, instructionDebugData> debugData;
PIN_RWMUTEX InstructionsDataLock;
// The data from decoded basic blocks (Needs thread safety checks)
// currently protected by InstructionsDataLock
vector<BBData> basicBlocks;
const BBData empty; // Default block
size_t UBBID; // May need to be made atomic
// Output Files set in main
FILE *trace;
PIN_MUTEX traceLock;
FILE *bbl_log;
#ifdef LOOPSTACK
list<long long> loopStack;
#endif
// If not thread safe there is a single set of the data that a thread would use
#ifndef THREADSAFE
thread_data_t *tdata;
#endif
// Local Functions
VOID clearState(THREADID threadid);
// Command line arguments
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
"o", "tracer.log", "specify output file name");
KNOB<string> KnobTraceFunction(KNOB_MODE_WRITEONCE, "pintool",
"f", "main", "specify fucntion to start tracing");
KNOB<bool> KnobSummaryOn(KNOB_MODE_WRITEONCE, "pintool",
"s", "0", "Enable summary data of whole run");
KNOB<bool> KnobVectorLineSummary(KNOB_MODE_WRITEONCE, "pintool",
"l", "0", "Enable line summary");
KNOB<int> KnobMinVectorCount(KNOB_MODE_WRITEONCE, "pintool",
"n", "100", "Line summary minimum count for vectorization");
KNOB<int> KnobTraceLimit(KNOB_MODE_WRITEONCE, "pintool",
"trace-limit", "0", "Maximum number of trace regions 0 is unlimited");
KNOB<bool> KnobSkipMove(KNOB_MODE_WRITEONCE, "pintool",
"m", "1", "Try to vectorize move instructions");
KNOB<bool> KnobDebugTrace(KNOB_MODE_WRITEONCE, "pintool",
"D", "0", "Enable full debug trace");
KNOB<bool> KnobMallocPrinting(KNOB_MODE_WRITEONCE, "pintool",
"log-malloc", "0", "log malloc calls");
KNOB<bool> KnobSupressMalloc(KNOB_MODE_WRITEONCE, "pintool",
"no-malloc", "0", "Don't track malloc calls");
KNOB<bool> KnobForFrontend(KNOB_MODE_WRITEONCE, "pintool",
"frontend", "0", "format for the frontend");
KNOB<bool> KnobForVectorSummary(KNOB_MODE_WRITEONCE, "pintool",
"vector-summary", "0", "summerize vectors");
KNOB<bool> KnobForShowNoFileInfo(KNOB_MODE_WRITEONCE, "pintool",
"show-all", "0", "show instrucions with no file info");
KNOB<bool> KobForPrintBasicBlocks(KNOB_MODE_WRITEONCE, "pintool",
"print-bb", "0", "print all basic blocks traced at end of log");
KNOB<bool> KnobBBVerstion(KNOB_MODE_WRITEONCE, "pintool",
"bb", "0", "Use PIN basic block mode");
KNOB<bool> KnobBBDotLog(KNOB_MODE_WRITEONCE, "pintool",
"bb-dot", "0", "Log PIN basic blocks in dot format");
KNOB<bool> KnobBBReport(KNOB_MODE_WRITEONCE, "pintool",
"bb-report", "0", "Final report on basic blocks");
KNOB<bool> KnobBBSummary(KNOB_MODE_WRITEONCE, "pintool",
"bb-summary", "0", "Summary report on basic blocks");
// This function is called when the application exits
VOID Fini(INT32 code, VOID *v)
{
#ifdef THREADSAFE
thread_data_t *tdata = get_tls(0);
#endif
if(tracingLevel != 0)
{
fprintf(trace,"Progam ended with tracing on\n");
#ifdef THREADSAFE
PIN_MutexLock(&traceLock);
#endif
traceOutput(trace, tdata->instructionResults);
#ifdef THREADSAFE
PIN_MutexUnlock(&traceLock);
#endif
}
finalOutput(trace, bbl_log, tdata->instructionResults);
}
// Handle instrumentation of an instruction that does not read or write memory
VOID recoredBaseInst(VOID *ip, THREADID threadid)
{
#ifdef THREADSAFE
PIN_RWMutexReadLock(&traclingLevelLock);
assert(threadid <= numThreads);
thread_data_t *tdata = get_tls(threadid);
assert(tdata != nullptr);
#endif
if(tracingLevel == 0 || inAlloc)
{
#ifdef THREADSAFE
PIN_RWMutexUnlock(&traclingLevelLock);
if(tdata->tracing)
{
tdata->tracing = false;
PIN_MutexLock(&traceLock);
traceOutput(trace, tdata->instructionResults);
PIN_MutexUnlock(&traceLock);
}
#endif
return;
}
#ifdef THREADSAFE
PIN_RWMutexUnlock(&traclingLevelLock);
// cerr << "Ask Read Lock (" << threadid << ")" << endl;
PIN_RWMutexReadLock(&InstructionsDataLock);
// cerr << "Got Read Lock (" << threadid << ")" << endl;
if(tdata->tracing == false)
{
tdata->tracing = true;
tdata->instructionResults.clear();
}
#endif
instructionCount++;
long value = 0;
auto ciItr = constInstructionLocations.find((ADDRINT)ip);
assert(ciItr != constInstructionLocations.end());
const instructionLocationsData *current_instruction = &(ciItr->second);
for(unsigned int i = 0; i < current_instruction->registers_read.size(); i++)
{
value = max(tdata->registers.readReg(current_instruction->registers_read[i]),value);
}
if(value > 0 && (current_instruction->type != MOVEONLY_INS_TYPE))
{
value = value +1;
}
for(unsigned int i = 0; i < current_instruction->registers_written.size(); i++)
{
tdata->registers.writeReg(current_instruction->registers_written[i], value);
}
if(value > 0 && (!((current_instruction->type == MOVEONLY_INS_TYPE) && KnobSkipMove)))
{
tdata->instructionResults[(ADDRINT)ip].addToDepth(value);
#ifdef LOOPSTACK
current_instruction->loopid = loopStack;
#endif
}
if(KnobDebugTrace)
instructionTracing(ip,NULL,value,"Base",tdata->debuglog,shadowMemory,tdata->registers);
#ifdef THREADSAFE
// cerr << "Release(" << threadid << ")" << endl;
PIN_RWMutexUnlock(&InstructionsDataLock);
// cerr << "Released(" << threadid << ")" << endl;
#endif
}
// Handle instrumentation of an instruction that does read or write memory
VOID RecordMemReadWrite(VOID * ip, VOID * addr1, UINT32 t1, VOID *addr2, UINT32 t2, THREADID threadid, bool pred)
{
UINT32 type1 = t1;
UINT32 type2 = t2;
#ifdef THREADSAFE
PIN_RWMutexReadLock(&traclingLevelLock);
assert(threadid <= numThreads);
thread_data_t *tdata = get_tls(threadid);
assert(tdata != nullptr);
#endif
if(tracingLevel == 0 || inAlloc)
{
#ifdef THREADSAFE
PIN_RWMutexUnlock(&traclingLevelLock);
if(tdata->tracing)
{
tdata->tracing = false;
PIN_MutexLock(&traceLock);
traceOutput(trace, tdata->instructionResults);
PIN_MutexUnlock(&traceLock);
}
#endif
return;
}
#ifdef THREADSAFE
PIN_RWMutexUnlock(&traclingLevelLock);
// cerr << "Ask Read Lock (" << threadid << ")" << endl;
PIN_RWMutexReadLock(&InstructionsDataLock);
// cerr << "Got Read Lock (" << threadid << ")" << endl;
if(tdata->tracing == false)
{
tdata->tracing = true;
tdata->instructionResults.clear();
}
#endif
if(KnobBBVerstion)
{
tdata->rwContext.addrs.push_back( make_pair((ADDRINT) addr1, t1) );
tdata->rwContext.addrs.push_back( make_pair((ADDRINT) addr2, t2) );
tdata->rwContext.pred.push_back(pred);
if(KnobDebugTrace)
fprintf(trace,"%p:<%p,%u><%p,%u>\n", (void*) ip, addr1, type1, addr2, type2);
PIN_RWMutexUnlock(&InstructionsDataLock);
return;
}
if(!pred)
{
if(KnobDebugTrace)
fprintf(trace, "%p:pred not executed\n", (void*) ip);
PIN_RWMutexUnlock(&InstructionsDataLock);
return;
}
instructionCount++;
long value = 0;
auto ciItr = constInstructionLocations.find((ADDRINT)ip);
assert(ciItr != constInstructionLocations.end());
const instructionLocationsData *current_instruction = &(ciItr->second);
// instructionLocationsData *current_instruction = &(instructionLocations[(ADDRINT)ip]);
if(type1 & READ_OPERATOR_TYPE)
{
value = shadowMemory.readMem((ADDRINT)addr1, current_instruction->memReadSize);
}
if(type2 & READ_OPERATOR_TYPE)
{
value = max(shadowMemory.readMem((ADDRINT)addr2, current_instruction->memReadSize), value);
}
for(unsigned int i = 0; i < current_instruction->registers_read.size(); i++)
{
value = max(tdata->registers.readReg(current_instruction->registers_read[i]),value);
}
if(value > 0 && (current_instruction->type != MOVEONLY_INS_TYPE))
{
value = value +1;
}
for(unsigned int i = 0; i < current_instruction->registers_written.size(); i++)
{
tdata->registers.writeReg(current_instruction->registers_written[i], value);
}
long region1 = 0;
long region2 = 0;
if(type1 & WRITE_OPERATOR_TYPE)
{
if(shadowMemory.memIsArray(addr1))
region1 = 1;
else
region1 = 0;
shadowMemory.writeMem((ADDRINT)addr1, max(value,region1));
}
if(type2 & WRITE_OPERATOR_TYPE)
{
if(shadowMemory.memIsArray(addr2))
region2 = 1;
else
region2 = 0;
shadowMemory.writeMem((ADDRINT)addr2, max(value,region2));
}
value = max(max(value,region1),region2);
if(value > 0 && !(((current_instruction->type == MOVEONLY_INS_TYPE) && KnobSkipMove)))
{
tdata->instructionResults[(ADDRINT)ip].addToDepth(value);
#ifdef LOOPSTACK
current_instruction->loopid = loopStack;
#endif
}
if(KnobDebugTrace)
{
instructionTracing(ip,addr2,value,"Mem",trace, shadowMemory, tdata->registers);
fprintf(trace,"<%p,%u><%p,%u>", addr1, type1, addr2, type2);
}
#ifdef THREADSAFE
// cerr << "Release(" << threadid << ")" << endl;
PIN_RWMutexUnlock(&InstructionsDataLock);
// cerr << "Released(" << threadid << ")" << endl;
#endif
}
// Internal dispatch
void blockTracerExecuter(VOID *ip, ADDRINT id, thread_data_t *tdata)
{
#ifdef THREADSAFE
PIN_RWMutexReadLock(&InstructionsDataLock);
#endif
basicBlocks[tdata->lastBB].execute(tdata->rwContext, shadowMemory, tdata->registers, tdata->instructionResults, trace);
basicBlocks[tdata->lastBB].addSuccessors((ADDRINT) ip);
if(KnobDebugTrace)
{
fprintf(trace, "BBL %p executed\n", (void *) tdata->lastBB);
basicBlocks[tdata->lastBB].printBlock(trace);
}
#ifdef THREADSAFE
PIN_RWMutexUnlock(&InstructionsDataLock);
#endif
}
// Wrapper to dispatch instrumenation in block trace mode
VOID blockTracer(VOID *ip, ADDRINT id, THREADID threadid)
{
if(!KnobBBVerstion)
return;
#ifdef THREADSAFE
assert(threadid <= numThreads);
thread_data_t *tdata = get_tls(threadid);
assert(tdata != nullptr);
PIN_RWMutexReadLock(&traclingLevelLock);
#endif
if(tracingLevel && tdata->lastBB && !inAlloc)
{
blockTracerExecuter(ip, id, tdata);
}
else
{
#ifdef THREADSAFE
if(tdata->tracing)
{
tdata->tracing = false;
PIN_MutexLock(&traceLock);
traceOutput(trace, tdata->instructionResults);
PIN_MutexUnlock(&traceLock);
}
#endif
}
tdata->lastBB = id;
#ifdef THREADSAFE
PIN_RWMutexUnlock(&traclingLevelLock);
#endif
}
// Code for insturmenting durring excution in jit mode.
VOID Trace(TRACE pintrace, VOID *v)
{
if(!inMain)
return;
#ifdef THREADSAFE
PIN_LockClient();
// cerr << "Ask Read Lock" << endl;
PIN_RWMutexReadLock(&InstructionsDataLock);
// cerr << "Have Read Lock" << endl;
#endif
for (BBL bbl = TRACE_BblHead(pintrace); BBL_Valid(bbl); bbl = BBL_Next(bbl))
{
// Only instrument instructions in a vaid RTN that is neither malloc or free
if(RTN_Valid(INS_Rtn(BBL_InsHead(bbl))))
{
string rtn_name = RTN_Name(INS_Rtn(BBL_InsHead(bbl)));
if( (rtn_name != MALLOC) && (rtn_name != FREE))
{
if(BBL_Original(bbl))
{
UBBID++;
basicBlocks.push_back(empty);
if(KnobDebugTrace)
{
logBasicBlock(bbl, UBBID, bbl_log);
}
BBL_InsertCall(bbl, IPOINT_BEFORE, (AFUNPTR)blockTracer, IARG_INST_PTR, IARG_ADDRINT, UBBID, IARG_THREAD_ID, IARG_CALL_ORDER, CALL_ORDER_FIRST-5, IARG_END);
basicBlocks[UBBID].expected_num_ins = BBL_NumIns(bbl);
// insturment each instruction in the curent basic block
for(INS ins = BBL_InsHead(bbl); INS_Valid(ins); ins = INS_Next(ins) )
{
ADDRINT ip = INS_Address(ins);
if(INS_IsOriginal(ins))
{
instructionType insType;
auto ciItr = constInstructionLocations.find((ADDRINT)ip);
if(ciItr == constInstructionLocations.end())
{
// Will need to edit Instruction Data heree
// auto dbrtn = RTN_FindByAddress(ip);
// RTN_Open(dbrtn);
// auto img = SEC_Img(RTN_Sec(dbrtn));
// if(IMG_Valid(img))
// cerr << "IMG = " << IMG_Name(img) << endl;
// else
// cerr << "IMG = NO IMAGE" << endl;
// cerr << "RTN = " << RTN_Name(dbrtn) << endl;
// cerr << "ADDR = " << (void *) ip << endl;
// cerr << "INS = " << (void *) INS_Address(ins) << "\t" << INS_Disassemble(ins) << endl;
// cerr << "BBL Start" << endl;
// for(auto dbins = BBL_InsHead(bbl); INS_Valid(dbins); dbins = INS_Next(dbins))
// {
// cerr << (void *) INS_Address(dbins) << "\t" << INS_Disassemble(dbins) << endl;
// }
// cerr << "BBL End" << endl;
// cerr << RTN_Name(dbrtn) << " Start" << endl;
// for(auto dbins = RTN_InsHead(dbrtn); INS_Valid(dbins); dbins = INS_Next(dbins))
// {
// cerr << (void *) INS_Address(dbins) << "\t" << INS_Disassemble(dbins) << endl;
// }
// cerr << RTN_Name(dbrtn) << " End" << endl;
// RTN_Close(dbrtn);
#ifdef THREADSAFE
// cerr << "Release Read Lock" << endl;
PIN_RWMutexUnlock(&InstructionsDataLock);
// cerr << "Released Read Lock" << endl;
// cerr << "Ask Write Lock" << endl;
PIN_RWMutexWriteLock(&InstructionsDataLock);
// cerr << "Have Write Lock" << endl;
#endif
// Insert instruction
instructionType insType;
insType = decodeInstructionData(ip, instructionLocations);
debugData[ip].instruction = INS_Disassemble(ins);
instructionLocations[ip].type = insType;
instructionLocations[ip].rtn_name = rtn_name;
UINT32 memOperands = INS_MemoryOperandCount(ins);
instructionLocations[ip].memOperands = memOperands;
if(memOperands)
{
instructionLocations[ip].memReadSize = INS_MemoryReadSize(ins);
instructionLocations[ip].memWriteSize = INS_MemoryWriteSize(ins);
}
if(INS_IsAtomicUpdate(ins))
{
instructionLocations[ip+1] = instructionLocations[ip];
debugData[ip+1] = debugData[ip];
}
#ifdef THREADSAFE
// cerr << "Release Write Lock" << endl;
PIN_RWMutexUnlock(&InstructionsDataLock);
// cerr << "Released Write Lock" << endl;
// cerr << "Ask Read Lock" << endl;
PIN_RWMutexReadLock(&InstructionsDataLock);
// cerr << "Have Read Lock" << endl;
#endif
// Find the newly inserted instructions
ciItr = constInstructionLocations.find((ADDRINT)ip);
assert(ciItr != constInstructionLocations.end());
}
const instructionLocationsData *current_instruction = &(ciItr->second);
insType = current_instruction->type;
UINT32 memOperands = current_instruction->memOperands;
if(insType == IGNORED_INS_TYPE)
continue;
// Instruments memory accesses using a predicated call, i.e.
// the instrumentation is called iff the instruction will actually be executed.
//
// The IA-64 architecture has explicitly predicated instructions.
// On the IA-32 and Intel(R) 64 architectures conditional moves and REP
// prefixed instructions appear as predicated instructions in Pin.
if(memOperands == 0)
{
if(!KnobBBVerstion)
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)recoredBaseInst, IARG_INST_PTR, IARG_CALL_ORDER, CALL_ORDER_FIRST, IARG_THREAD_ID, IARG_END);
}
else if( memOperands < 3)
{
UINT32 addr1 = 0;
UINT32 type1 = NONE_OPERATOR_TYPE;
UINT32 addr2 = 0;
UINT32 type2 = NONE_OPERATOR_TYPE;
UINT32 memOp= 0;
if(INS_MemoryOperandIsRead(ins, memOp))
type1 = type1 | READ_OPERATOR_TYPE;
if(INS_MemoryOperandIsWritten(ins, memOp))
type1 = type1 | WRITE_OPERATOR_TYPE;
if(memOperands > 1)
{
memOp = 1;
if(INS_MemoryOperandIsRead(ins, memOp))
type2 = type2 | READ_OPERATOR_TYPE;
if(INS_MemoryOperandIsWritten(ins, memOp))
type2 = type2 | WRITE_OPERATOR_TYPE;
}
INS_InsertCall(
ins, IPOINT_BEFORE, (AFUNPTR)RecordMemReadWrite,
IARG_INST_PTR,
IARG_MEMORYOP_EA, addr1,
IARG_UINT32, type1,
IARG_MEMORYOP_EA, addr2,
IARG_UINT32, type2,
IARG_THREAD_ID,
IARG_CALL_ORDER, CALL_ORDER_FIRST,
IARG_EXECUTING,
IARG_END);
}
else
{
assert(false);
}
//push instruction on current basic block
basicBlocks[UBBID].pushInstruction(current_instruction);
}
else
{
fprintf(trace, "Instruction %p not original\n", (void *) ip);
}
}
}
}
}
}
#ifdef THREADSAFE
// cerr << "Release Read Lock" << endl;
PIN_RWMutexUnlock(&InstructionsDataLock);
// cerr << "Released Read Lock" << endl;
PIN_UnlockClient();
#endif
}
// utility for clearing the vector results per thread
void clearVectors(THREADID threadid)
{
#ifdef THREADSAFE
thread_data_t *tdata = get_tls(threadid);
#endif
// FIX?
for(auto it = instructionLocations.begin(); it != instructionLocations.end(); ++it)
{
it->second.logged = false;
}
tdata->instructionResults.clear();
}
// Utility to clear the state per thread
VOID clearState(THREADID threadid)
{
clearVectors(threadid);
shadowMemory.clear();
#ifdef THREADSAFE
thread_data_t *tdata = get_tls(threadid);
#endif
tdata->lastBB = 0; //this may be the wrong place
tdata->rwContext.addrs.clear();
tdata->rwContext.pred.clear();
}
// Instumentation for malloc calls before the call
VOID MallocBefore(CHAR * name, ADDRINT size, THREADID threadid)
{
// inAlloc = true;
if(KnobSupressMalloc)
return;
#ifdef THREADSAFE
assert(threadid <= numThreads);
thread_data_t* tdata = get_tls(threadid);
#endif
tdata->malloc_size = size;
}
// Instumentation for malloc calls after the call
VOID MallocAfter(ADDRINT ret, THREADID threadid)
{
// inAlloc = false;
if(KnobSupressMalloc)
return;
#ifdef THREADSAFE
assert(threadid <= numThreads);
thread_data_t* tdata = get_tls(threadid);
PIN_RWMutexReadLock(&traclingLevelLock);
#endif
bool currentlyTracing = tracingLevel;
#ifdef THREADSAFE
PIN_RWMutexUnlock(&traclingLevelLock);
#endif
//Map version
if(tdata->malloc_size > 0)
{
shadowMemory.arrayMem(ret, tdata->malloc_size, currentlyTracing);
}
if(KnobMallocPrinting)
{
fprintf(trace,"malloc returns %p of size(%p)\n",(void *)ret,(void *)tdata->malloc_size);
}
}
// Instumentation for free calls before the call
VOID FreeBefore(CHAR * name, ADDRINT start, THREADID threadid)
{
// inAlloc = true;
if(KnobSupressMalloc)
return;
shadowMemory.arrayMemClear(start);
}
// Instumentation for free calls before the call
VOID FreeAfter()
{
// inAlloc = false;
}
// Utility for staring tracing starts only if not started
VOID traceOn(CHAR * name, ADDRINT start, THREADID threadid)
{
#ifdef THREADSAFE
PIN_GetLock(&lock,threadid);
PIN_RWMutexWriteLock(&traclingLevelLock);
thread_data_t* tdata = get_tls(threadid);
#endif
if(traceRegionCount > KnobTraceLimit.Value())
{
#ifdef THREADSAFE
PIN_RWMutexUnlock(&traclingLevelLock);
PIN_ReleaseLock(&lock);
#endif
return;
}
if(tracingLevel == 0 && (KnobTraceLimit.Value() != 0))
{
traceRegionCount++;
}
if(tracingLevel == 0)
{
#ifdef THREADSAFE
PIN_RWMutexWriteLock(&InstructionsDataLock);
#endif
clearState(threadid);
#ifdef THREADSAFE
PIN_RWMutexUnlock(&InstructionsDataLock);
#endif
}
tracingLevel++;
tdata->tracing = true;
if(!KnobForFrontend)
{
fprintf(trace,"tracing turned on\n");
}
#ifdef THREADSAFE
PIN_RWMutexUnlock(&traclingLevelLock);
PIN_ReleaseLock(&lock);
#endif
}
// Utility for stopping tracing stops after matching number of starts
VOID traceOff(CHAR * name, ADDRINT start, THREADID threadid)
{
#ifdef THREADSAFE
PIN_GetLock(&lock, threadid);
PIN_RWMutexWriteLock(&traclingLevelLock);
assert(threadid <= numThreads);
thread_data_t *tdata = get_tls(threadid);
assert(tdata != nullptr);
#endif
if(traceRegionCount > KnobTraceLimit.Value())
{
#ifdef THREADSAFE
PIN_RWMutexUnlock(&traclingLevelLock);
PIN_ReleaseLock(&lock);
#endif
return;
}
if(!KnobForFrontend)
fprintf(trace,"tracing turned off\n");
if(tracingLevel < 1)
{
if(!KnobForFrontend)
fprintf(trace,"Trace off called with tracing off\n");
}
else if(tracingLevel == 1)
{
if(tdata->lastBB && !inAlloc)
{
blockTracerExecuter(NULL, 0, tdata); // trace final block
}
shadowMemory.clear();
tracingLevel = 0;
tdata->tracing = false;
#ifdef THREADSAFE
PIN_MutexLock(&traceLock);
#endif
traceOutput(trace, tdata->instructionResults);
#ifdef THREADSAFE
PIN_MutexUnlock(&traceLock);
#endif
}
else
{
tracingLevel--;
}
#ifdef THREADSAFE
PIN_RWMutexUnlock(&traclingLevelLock);
PIN_ReleaseLock(&lock);
#endif
}
// Instrumentation for trace on calls
VOID traceFunctionEntry(CHAR * name, ADDRINT start, THREADID threadid)
{
traceOn(name, start, threadid);
}
// Instrumentation for trace off calls
VOID traceFunctionExit(CHAR * name, ADDRINT start, THREADID threadid)
{
traceOff(name,start,threadid);
}
// Instrumenation for start of main
VOID markMainStart(CHAR * name, ADDRINT start, THREADID threadid)
{
inMain = true;
if(KnobDebugTrace)
cerr << "Start Main" << endl;
}
// Instrumenation for start of main
VOID markMainEnd(CHAR * name, ADDRINT start, THREADID threadid)
{
if(KnobDebugTrace)
cerr << "End Main" << endl;
inMain = false;
}
// Instrumentation for array_mem calls
VOID arrayMem(ADDRINT start, size_t size)
{
if(KnobMallocPrinting)
{
fprintf(trace,"Memory Allocation Found start = %p size = %p\n", (void *) start, (void *) size);
}
#ifdef THREADSAFE
PIN_RWMutexReadLock(&traclingLevelLock);
#endif
shadowMemory.arrayMem(start, size, tracingLevel);
#ifdef THREADSAFE
PIN_RWMutexUnlock(&traclingLevelLock);
#endif
}
// Instrumentation for array_mem_clear calls
VOID arrayMemClear(ADDRINT start)
{
if(KnobMallocPrinting)
fprintf(trace,"Memory Allocation Found cleared = %p\n", (void *) start);
shadowMemory.arrayMemClear(start);
}
#ifdef LOOPSTACK
// Instumentation for loopstart calls
VOID loopStart(ADDRINT id)
{
loopStack.push_front((long long) id);
}
// Instumentation for loopsend calls
VOID loopEnd(ADDRINT id)
{
loopStack.pop_front();
}
#endif
// Code for instumenting durring image loading
VOID Image(IMG img, VOID *v)
{
#ifdef THREADSAFE
PIN_LockClient();
#endif
// Instrument the malloc() and free() functions.
// Find the malloc() function.
RTN mallocRtn = RTN_FindByName(img, MALLOC);
if (RTN_Valid(mallocRtn))
{
RTN_Open(mallocRtn);
RTN_InsertCall(mallocRtn, IPOINT_BEFORE, (AFUNPTR)MallocBefore,
IARG_ADDRINT, MALLOC,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_THREAD_ID,
IARG_END);
RTN_InsertCall(mallocRtn, IPOINT_AFTER, (AFUNPTR)MallocAfter,
IARG_FUNCRET_EXITPOINT_VALUE, IARG_THREAD_ID, IARG_END);
RTN_Close(mallocRtn);
}
// Find the free() function.
RTN freeRtn = RTN_FindByName(img, FREE);
if (RTN_Valid(freeRtn))
{
RTN_Open(freeRtn);
RTN_InsertCall(freeRtn, IPOINT_BEFORE, (AFUNPTR)FreeBefore,
IARG_ADDRINT, FREE,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_THREAD_ID,
IARG_END);
RTN_InsertCall(freeRtn, IPOINT_BEFORE, (AFUNPTR)FreeAfter,
IARG_END);
RTN_Close(freeRtn);
}
// Enable Tracing based on traceon
RTN traceRtn = RTN_FindByName(img, TRACE_ON);
if (RTN_Valid(traceRtn))
{
RTN_Open(traceRtn);
RTN_InsertCall(traceRtn, IPOINT_BEFORE, (AFUNPTR)traceOn,
IARG_ADDRINT, FREE,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_THREAD_ID,
IARG_END);
RTN_Close(traceRtn);
}
// End Tracing based on traceoff
traceRtn = RTN_FindByName(img, TRACE_OFF);
if (RTN_Valid(traceRtn))
{
RTN_Open(traceRtn);
RTN_InsertCall(traceRtn, IPOINT_AFTER, (AFUNPTR)traceOff,
IARG_ADDRINT, FREE,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_THREAD_ID,
IARG_END);
RTN_Close(traceRtn);
}
// Mark memmory as vector memory based on arrayMem calls.
traceRtn = RTN_FindByName(img, ARRAY_MEM);
if (RTN_Valid(traceRtn))
{
RTN_Open(traceRtn);
RTN_InsertCall(traceRtn, IPOINT_BEFORE, (AFUNPTR)arrayMem,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_FUNCARG_ENTRYPOINT_VALUE, 1,
IARG_END);