-
Notifications
You must be signed in to change notification settings - Fork 535
/
ptdecode.c
1616 lines (1326 loc) · 45.5 KB
/
ptdecode.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
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
/*
WinAFL - Intel PT decoding
------------------------------------------------
Written and maintained by Ivan Fratric <[email protected]>
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "windows.h"
#include "intel-pt.h"
#include "pt_cpu.h"
#include "pt_cpuid.h"
#include "pt_opcodes.h"
#include "pt_retstack.h"
#include "pt_block_decoder.h"
#include "types.h"
#include "config.h"
#include "debug.h"
#include "winaflpt.h"
#include "ptdecode.h"
#define PPT_EXT 0xFF
uint32_t previous_offset;
uint64_t previous_ip;
extern address_range* coverage_ip_ranges;
extern size_t num_ip_ranges;
static address_range* current_range;
extern u8 *trace_bits;
#define MAX_TRACELET_SIZE 100 // just a hint, the tracelets could end up larger
#define MIN_TRACELET_SIZE 20 // just a hint, the tracelets could end up smaller
unsigned char opc_lut[] = {
0x02, 0x08, 0xff, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x06, 0x09, 0x12,
0x09, 0x07, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x0f, 0x09, 0x12, 0x09, 0x05, 0x09, 0x12,
0x09, 0x08, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x06, 0x09, 0x12,
0x09, 0x07, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x05, 0x09, 0x12,
0x09, 0x08, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x06, 0x09, 0x12,
0x09, 0x07, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x11, 0x09, 0x12, 0x09, 0x05, 0x09, 0x12,
0x09, 0x08, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x06, 0x09, 0x12,
0x09, 0x07, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x05, 0x09, 0x12,
0x09, 0x08, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x06, 0x09, 0x12,
0x09, 0x07, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x0b, 0x09, 0x12, 0x09, 0x05, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x08, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x06, 0x09, 0x12,
0x09, 0x07, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x05, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12,
0x09, 0x00, 0x09, 0x12, 0x09, 0x00, 0x09, 0x12
};
unsigned char ext_lut[] = {
0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x18, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x13, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x19, 0x0a, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x17, 0xff, 0x00, 0x00, 0x00, 0x00,
0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
unsigned char opc_size_lut[] = {
0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x08, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x03, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x03, 0x01, 0x01,
0x01, 0x03, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x03, 0x01, 0x01,
0x01, 0x05, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x05, 0x01, 0x01,
0x01, 0x05, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x02, 0x01, 0x01, 0x01, 0x05, 0x01, 0x01,
0x01, 0x07, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x07, 0x01, 0x01,
0x01, 0x07, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x07, 0x01, 0x01,
0x01, 0x07, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x07, 0x01, 0x01,
0x01, 0x07, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x02, 0x01, 0x01, 0x01, 0x07, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x09, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x09, 0x01, 0x01,
0x01, 0x09, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x09, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01
};
unsigned char ext_size_lut[] = {
0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x02, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x00, 0x00,
0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
unsigned char psb[16] = {
0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82,
0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82
};
static unsigned char psb_and_psbend[18] = {
0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82,
0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82,
0x02, 0x23
};
typedef struct decoder_state_t {
uint64_t query_ip;
uint64_t block_ip;
uint8_t mode;
} decoder_state;
typedef struct tracelet_cache_node_t {
uint64_t hash;
size_t size;
struct tracelet_cache_node_t *hash_prev;
struct tracelet_cache_node_t *hash_next;
struct tracelet_cache_node_t *lru_prev;
struct tracelet_cache_node_t *lru_next;
decoder_state state_prev;
decoder_state state_next;
uint8_t stack_removed;
uint8_t stack_added;
uint64_t *stack_prev;
uint64_t *stack_next;
uint32_t tracelet_size;
unsigned char * tracelet;
uint32_t map_update_size;
uint32_t *map_offsets;
uint8_t *map_updates;
} tracelet_cache_node;
struct tracelet_cahe_t {
tracelet_cache_node **hashtable;
tracelet_cache_node *lru_first;
tracelet_cache_node *lru_last;
size_t size;
size_t num_entries;
size_t max_size;
size_t max_entries;
};
static struct tracelet_cahe_t tracelet_cache;
struct coverage_cache_t {
uint32_t index_buffer[MAP_SIZE];
// need + 2 for edge coverage
uint32_t map_offsets[MAP_SIZE + 2];
uint8_t counters[MAP_SIZE + 2];
uint32_t size;
};
void tracelet_coverage_init(struct coverage_cache_t *coverage_cache) {
memset(coverage_cache->index_buffer, 0, MAP_SIZE * sizeof(coverage_cache->index_buffer[0]));
coverage_cache->size = 0;
}
void tracelet_coverage_clear(struct coverage_cache_t *coverage_cache, int coverage_kind) {
if (!coverage_cache->size) return;
uint32_t from = 0;
uint32_t to = coverage_cache->size;
if (coverage_kind == COVERAGE_EDGE) {
// the first and the last value have special meaning
// in the case of edge coverage
from++;
to--;
}
for (uint32_t i = from; i < to; i++) {
coverage_cache->index_buffer[coverage_cache->map_offsets[i]] = 0;
}
coverage_cache->size = 0;
}
void tracelet_coverage_add_bb(struct coverage_cache_t *coverage_cache, uint32_t offset) {
offset = offset % MAP_SIZE;
if (coverage_cache->index_buffer[offset]) {
coverage_cache->counters[coverage_cache->index_buffer[offset] - 1]++;
} else {
coverage_cache->index_buffer[offset] = coverage_cache->size + 1;
coverage_cache->map_offsets[coverage_cache->size] = offset;
coverage_cache->counters[coverage_cache->size] = 1;
coverage_cache->size++;
}
}
void tracelet_coverage_add_edge(struct coverage_cache_t *coverage_cache, uint32_t offset) {
uint32_t edge;
// don't touch the global previous_offset while building the cache
// we'll update everything once the cache gets replayed
uint32_t previous_offset;
if (!coverage_cache->size) {
// store the first offset as the first value
coverage_cache->map_offsets[0] = offset;
coverage_cache->counters[0] = 0;
coverage_cache->size = 2;
} else {
previous_offset = coverage_cache->map_offsets[coverage_cache->size - 1];
edge = (offset ^ previous_offset) % MAP_SIZE;
if (coverage_cache->index_buffer[edge]) {
coverage_cache->counters[coverage_cache->index_buffer[edge]]++;
} else {
coverage_cache->index_buffer[edge] = coverage_cache->size - 1;
coverage_cache->map_offsets[coverage_cache->size - 1] = edge;
coverage_cache->counters[coverage_cache->size - 1] = 1;
coverage_cache->size++;
}
}
// always store the previous offset as the last value
previous_offset = offset >> 1;
coverage_cache->map_offsets[coverage_cache->size - 1] = previous_offset;
coverage_cache->counters[coverage_cache->size - 1] = 0;
}
static inline uint64_t djb2(unsigned char *data, size_t size) {
uint64_t hash = 5381;
for (size_t i = 0; i < size; i++) {
hash = (hash << 5) + hash + data[i];
}
return hash;
}
void tracelet_cache_init(size_t max_entries, size_t max_size) {
tracelet_cache.max_entries = max_entries;
tracelet_cache.max_size = max_size;
tracelet_cache.hashtable = (tracelet_cache_node **)calloc(max_entries, sizeof(tracelet_cache_node *));
tracelet_cache.lru_first = NULL;
tracelet_cache.lru_last = NULL;
tracelet_cache.size = 0;
tracelet_cache.num_entries = 0;
}
// sets the node as the least recently used
void cache_node_touch(tracelet_cache_node *node) {
// printf("accessing %p in cache\n", node);
if (!node->lru_prev) return; //already at the beginning
else node->lru_prev->lru_next = node->lru_next;
if (node->lru_next) node->lru_next->lru_prev = node->lru_prev;
else tracelet_cache.lru_last = node->lru_prev;
node->lru_prev = NULL;
node->lru_next = tracelet_cache.lru_first;
if (node->lru_next) node->lru_next->lru_prev = node;
tracelet_cache.lru_first = node;
}
void cache_node_remove(tracelet_cache_node *node) {
// printf("removing %p from cache\n", node);
if (node->lru_prev) node->lru_prev->lru_next = node->lru_next;
else tracelet_cache.lru_first = node->lru_next;
if (node->lru_next) node->lru_next->lru_prev = node->lru_prev;
else tracelet_cache.lru_last = node->lru_prev;
if (node->hash_prev) node->hash_prev->hash_next = node->hash_next;
else tracelet_cache.hashtable[node->hash % tracelet_cache.max_entries] = node->hash_next;
if (node->hash_next) node->hash_next->hash_prev = node->hash_prev;
tracelet_cache.num_entries--;
tracelet_cache.size -= node->size;
free(node);
}
void cache_remove_lru() {
tracelet_cache_node *node = tracelet_cache.lru_last;
if (node) cache_node_remove(node);
}
void cache_node_add(tracelet_cache_node *node) {
// printf("adding %p to cache\n", node);
while (tracelet_cache.num_entries >= tracelet_cache.max_entries) cache_remove_lru();
while ((tracelet_cache.size + node->size) >= tracelet_cache.max_size) cache_remove_lru();
tracelet_cache_node *prev_first;
prev_first = tracelet_cache.hashtable[node->hash % tracelet_cache.max_entries];
tracelet_cache.hashtable[node->hash % tracelet_cache.max_entries] = node;
node->hash_prev = NULL;
node->hash_next = prev_first;
if (prev_first) prev_first->hash_prev = node;
prev_first = tracelet_cache.lru_first;
tracelet_cache.lru_first = node;
node->lru_prev = NULL;
node->lru_next = prev_first;
if (prev_first) prev_first->lru_prev = node;
else tracelet_cache.lru_last = node;
tracelet_cache.num_entries++;
tracelet_cache.size += node->size;
}
tracelet_cache_node *cache_find_node(uint64_t hash, decoder_state *state, unsigned char *tracelet, size_t tracelet_size, struct pt_retstack *retstack) {
tracelet_cache_node *node = tracelet_cache.hashtable[hash % tracelet_cache.max_entries];
while (node) {
if ((node->hash == hash) &&
(node->state_prev.block_ip == state->block_ip) &&
(node->state_prev.query_ip == state->query_ip) &&
(node->state_prev.mode == state->mode) &&
(node->tracelet_size == tracelet_size) &&
(memcmp(node->tracelet, tracelet, tracelet_size) == 0))
{
uint8_t top = retstack->top;
size_t i;
for (i = 0; i < node->stack_removed; i++) {
if (top == retstack->bottom) break;
top = (!top ? pt_retstack_size : top - 1);
if (retstack->stack[top] != node->stack_prev[i]) break;
}
if (i == node->stack_removed) return node; // finally
}
node = node->hash_next;
}
return NULL;
}
void dump_lut(unsigned char *lut, char *lutname) {
printf("unsigned char %s[] = {\n", lutname);
for (int i = 0; i<32; i++) {
printf("\t");
for (int j = 0; j<8; j++) {
printf("0x%02x", lut[i * 8 + j]);
if (j != 7) printf(", ");
}
if (i != 31) printf(",\n");
else printf("\n");
}
printf("};\n\n");
}
// function that was used to build the lookup tables for the packet decoder
void build_luts() {
for (int i = 0; i<256; i++) {
opc_lut[i] = ppt_invalid;
}
for (int i = 0; i<256; i++) {
ext_lut[i] = ppt_invalid;
}
for (int i = 0; i<256; i++) {
opc_size_lut[i] = 0;
ext_size_lut[i] = 0;
}
//ext packets
opc_lut[pt_opc_ext] = PPT_EXT;
opc_size_lut[pt_opc_ext] = 1; // not really important
//pad packet
opc_lut[pt_opc_pad] = ppt_pad;
opc_size_lut[pt_opc_pad] = 1;
//tip packet
for (int i = 0; i<8; i++) {
unsigned char opcode = (unsigned char)((i << 5) + 0xd);
if (i == 0) {
opc_lut[opcode] = ppt_tip;
opc_size_lut[opcode] = 1;
}
else if (i == 1) {
opc_lut[opcode] = ppt_tip;
opc_size_lut[opcode] = 1 + 2;
}
else if (i == 2) {
opc_lut[opcode] = ppt_tip;
opc_size_lut[opcode] = 1 + 4;
}
else if ((i == 3) || (i == 4)) {
opc_lut[opcode] = ppt_tip;
opc_size_lut[opcode] = 1 + 6;
}
else if (i == 6) {
opc_lut[opcode] = ppt_tip;
opc_size_lut[opcode] = 1 + 8;
}
}
//tip.pge packet
for (int i = 0; i<8; i++) {
unsigned char opcode = (unsigned char)((i << 5) + 0x11);
if (i == 0) {
opc_lut[opcode] = ppt_tip_pge;
opc_size_lut[opcode] = 1;
}
else if (i == 1) {
opc_lut[opcode] = ppt_tip_pge;
opc_size_lut[opcode] = 1 + 2;
}
else if (i == 2) {
opc_lut[opcode] = ppt_tip_pge;
opc_size_lut[opcode] = 1 + 4;
}
else if ((i == 3) || (i == 4)) {
opc_lut[opcode] = ppt_tip_pge;
opc_size_lut[opcode] = 1 + 6;
}
else if (i == 6) {
opc_lut[opcode] = ppt_tip_pge;
opc_size_lut[opcode] = 1 + 8;
}
}
//tip.pgd packet
for (int i = 0; i<8; i++) {
unsigned char opcode = (unsigned char)((i << 5) + 0x1);
if (i == 0) {
opc_lut[opcode] = ppt_tip_pgd;
opc_size_lut[opcode] = 1;
}
else if (i == 1) {
opc_lut[opcode] = ppt_tip_pgd;
opc_size_lut[opcode] = 1 + 2;
}
else if (i == 2) {
opc_lut[opcode] = ppt_tip_pgd;
opc_size_lut[opcode] = 1 + 4;
}
else if ((i == 3) || (i == 4)) {
opc_lut[opcode] = ppt_tip_pgd;
opc_size_lut[opcode] = 1 + 6;
}
else if (i == 6) {
opc_lut[opcode] = ppt_tip_pgd;
opc_size_lut[opcode] = 1 + 8;
}
}
//fup packet
for (int i = 0; i<8; i++) {
unsigned char opcode = (unsigned char)((i << 5) + 0x1d);
if (i == 0) {
opc_lut[opcode] = ppt_fup;
opc_size_lut[opcode] = 1;
}
else if (i == 1) {
opc_lut[opcode] = ppt_fup;
opc_size_lut[opcode] = 1 + 2;
}
else if (i == 2) {
opc_lut[opcode] = ppt_fup;
opc_size_lut[opcode] = 1 + 4;
}
else if ((i == 3) || (i == 4)) {
opc_lut[opcode] = ppt_fup;
opc_size_lut[opcode] = 1 + 6;
}
else if (i == 6) {
opc_lut[opcode] = ppt_fup;
opc_size_lut[opcode] = 1 + 8;
}
}
//mode packet
opc_lut[pt_opc_mode] = ppt_mode;
opc_size_lut[pt_opc_mode] = 2;
//tsc packet
opc_lut[pt_opc_tsc] = ppt_tsc;
opc_size_lut[pt_opc_tsc] = 8;
//mtc packet
opc_lut[pt_opc_mtc] = ppt_mtc;
opc_size_lut[pt_opc_mtc] = 2;
//cyc packet
for (int i = 0; i<64; i++) {
unsigned char opcode = (unsigned char)((i << 2) + 0x3);
opc_lut[opcode] = ppt_cyc;
opc_size_lut[opcode] = 1;
}
//tnt packets
for (int i = 1; i <= 6; i++) {
for (int bits = 0; bits<(1 << i); bits++) {
unsigned char opcode = (unsigned char)((1 << (i + 1)) + (bits << 1));
opc_lut[opcode] = ppt_tnt_8;
opc_size_lut[opcode] = 1;
}
}
//////extensions///////
//psb packet
ext_lut[pt_ext_psb] = ppt_psb;
ext_size_lut[pt_ext_psb] = 16;
//long tnt packet
ext_lut[pt_ext_tnt_64] = ppt_tnt_64;
ext_size_lut[pt_ext_tnt_64] = 8;
//pip packet
ext_lut[pt_ext_pip] = ppt_pip;
ext_size_lut[pt_ext_pip] = 8;
//ovf packet
ext_lut[pt_ext_ovf] = ppt_ovf;
ext_size_lut[pt_ext_ovf] = 2;
//psbend packet
ext_lut[pt_ext_psbend] = ppt_psbend;
ext_size_lut[pt_ext_psbend] = 2;
//cbr packet
ext_lut[pt_ext_cbr] = ppt_cbr;
ext_size_lut[pt_ext_cbr] = 4;
//tma packet
ext_lut[pt_ext_tma] = ppt_tma;
ext_size_lut[pt_ext_tma] = 7;
//stop packet
ext_lut[pt_ext_stop] = ppt_stop;
ext_size_lut[pt_ext_stop] = 2;
//vmcs packet
ext_lut[pt_ext_vmcs] = ppt_vmcs;
ext_size_lut[pt_ext_vmcs] = 7;
//exstop packet
ext_lut[pt_ext_exstop] = ppt_exstop;
ext_size_lut[pt_ext_exstop] = 2;
//exstop-ip packet
ext_lut[pt_ext_exstop_ip] = ppt_exstop;
ext_size_lut[pt_ext_exstop_ip] = 2;
//mwait packet
ext_lut[pt_ext_mwait] = ppt_mwait;
ext_size_lut[pt_ext_mwait] = 10;
//pwre packet
ext_lut[pt_ext_pwre] = ppt_pwre;
ext_size_lut[pt_ext_pwre] = 4;
//pwrx packet
ext_lut[pt_ext_pwrx] = ppt_pwrx;
ext_size_lut[pt_ext_pwrx] = 7;
//ptw packet
for (int i = 0; i<2; i++) {
for (int j = 0; j<2; j++) {
unsigned char opcode = (unsigned char)((i << 7) + (j << 5) + 0x12);
ext_lut[opcode] = ppt_ptw;
if (j == 0) {
ext_size_lut[opcode] = 6;
}
else if (j == 1) {
ext_size_lut[opcode] = 10;
}
}
}
//ext2
ext_lut[pt_ext_ext2] = PPT_EXT;
ext_size_lut[pt_ext_ext2] = 1; // not really important
dump_lut(opc_lut, "opc_lut");
dump_lut(ext_lut, "ext_lut");
dump_lut(opc_size_lut, "opc_size_lut");
dump_lut(ext_size_lut, "ext_size_lut");
}
// sign extend
inline static uint64_t sext(uint64_t val, uint8_t sign) {
uint64_t signbit, mask;
signbit = 1ull << (sign - 1);
mask = ~0ull << sign;
return val & signbit ? val | mask : val & ~mask;
}
// finds the next psb packet in the data buffer
bool findpsb(unsigned char **data, size_t *size) {
if (*size < 16) return false;
if (memcmp(*data, psb, sizeof(psb)) == 0) return true;
for (size_t i = 0; i < (*size - sizeof(psb) - 1); i++) {
if (((*data)[i] == psb[0]) && ((*data)[i+1] == psb[1])) {
if (memcmp((*data) + i, psb, sizeof(psb)) == 0) {
*data = *data + i;
*size = *size - i;
return true;
}
}
}
return false;
}
// checks if the IP address is in one of the modules we are interested in
// and updates the coverage map
inline static int update_coverage_map(uint64_t next_ip, int coverage_kind) {
uint32_t offset;
if (next_ip < current_range->start) {
do {
current_range--;
} while (next_ip < current_range->start);
} else if (next_ip > current_range->end) {
do {
current_range++;
} while (next_ip > current_range->end);
}
if (!current_range->collect) return 0;
// printf("ip: %p\n", (void*)next_ip);
offset = (uint32_t)(next_ip - current_range->start);
switch (coverage_kind) {
case COVERAGE_BB:
trace_bits[offset % MAP_SIZE]++;
break;
case COVERAGE_EDGE:
trace_bits[(offset ^ previous_offset) % MAP_SIZE]++;
previous_offset = offset >> 1;
break;
}
return 1;
}
// checks if the IP address is in one of the modules we are interested in
// and updates the coverage_cache datastructure
inline static int update_coverage_cache(struct coverage_cache_t *coverage_cache,
uint64_t next_ip, int coverage_kind)
{
uint32_t offset;
if (next_ip < current_range->start) {
do {
current_range--;
} while (next_ip < current_range->start);
}
else if (next_ip > current_range->end) {
do {
current_range++;
} while (next_ip > current_range->end);
}
if (!current_range->collect) return 0;
// printf("ip: %p\n", (void*)next_ip);
offset = (uint32_t)(next_ip - current_range->start);
switch (coverage_kind) {
case COVERAGE_BB:
tracelet_coverage_add_bb(coverage_cache, offset);
break;
case COVERAGE_EDGE:
tracelet_coverage_add_edge(coverage_cache, offset);
break;
}
return 1;
}
// gets the opcode and the size of the next packet in the trace buffer
static inline int get_next_opcode(unsigned char **data_p, size_t *size_p,
unsigned char *opcode_p, unsigned char *opcodesize_p)
{
unsigned char *data = *data_p;
size_t size = *size_p;
unsigned char opcode = opc_lut[*data];
unsigned char opcodesize = opc_size_lut[*data];
// handle extensions
if(opcode == PPT_EXT) {
if(size < 2) return 0;
opcode = ext_lut[*(data+1)];
opcodesize = ext_size_lut[*(data+1)];
// second-level extension
if(opcode == PPT_EXT) {
if(size < 3) return 0;
// currently there is only one possibility
if((*(data+2)) == 0x88) {
opcode = ppt_mnt;
opcodesize = 11;
} else {
opcode = ppt_invalid;
opcodesize = 0;
}
}
} else if(opcode == ppt_cyc) {
// special handling for cyc packets since
// they don't have a predetermined size
if(*data & 4) {
opcodesize = 2;
while(1) {
if(size < opcodesize) return 0;
if(!((*(data + (opcodesize - 1))) & 1)) break;
opcodesize++;
}
}
}
if (size < opcodesize) return 0;
*opcode_p = opcode;
*opcodesize_p = opcodesize;
return 1;
}
static inline uint64_t decode_ip(unsigned char *data) {
uint64_t next_ip;
switch ((*data) >> 5) {
case 0:
next_ip = previous_ip;
break;
case 1:
next_ip = (previous_ip & 0xFFFFFFFFFFFF0000ULL) | *((uint16_t *)(data + 1));
break;
case 2:
next_ip = (previous_ip & 0xFFFFFFFF00000000ULL) | *((uint32_t *)(data + 1));
break;
case 3:
next_ip = sext(*((uint32_t *)(data + 1)) | ((uint64_t)(*((uint16_t *)(data + 5))) << 32), 48);
break;
case 4:
next_ip = (previous_ip & 0xFFFF000000000000ULL) | *((uint32_t *)(data + 1)) | ((uint64_t)(*((uint16_t *)(data + 5))) << 32);
break;
case 6:
next_ip = *((uint64_t *)(data + 1));
break;
}
previous_ip = next_ip;
return next_ip;
}
// returns the type of the first packet or ppt_invalid
int get_next_tracelet(unsigned char **data, size_t *size,
unsigned char **tracelet_data, size_t *tracelet_size)
{
unsigned char opcode;
unsigned char opcodesize;
unsigned char previous_opcode = ppt_invalid;
int ret = ppt_tnt_8;
while (*size) {
if (!get_next_opcode(data, size, &opcode, &opcodesize))
return ppt_invalid;
if (opcode == ppt_invalid) return ppt_invalid;
// printf("packet type: %d\n", opcode);
switch (opcode) {
case ppt_tnt_8:
case ppt_tnt_64:
// merge tiny tracelets
if (*tracelet_size > MIN_TRACELET_SIZE) {
// always cut before tnt preceeded by non-tnt
if (previous_opcode != ppt_invalid &&
previous_opcode != ppt_tnt_8 &&
previous_opcode != ppt_tnt_64)
{
return ret;
}
// cut very long streams of tnt packets
if (*tracelet_size > MAX_TRACELET_SIZE) {
return ret;
}
}
memcpy(*tracelet_data, *data, opcodesize);
*tracelet_data += opcodesize;
*tracelet_size += opcodesize;
*size -= opcodesize;
*data += opcodesize;
previous_opcode = opcode;
break;
case ppt_psb:
// let the caller know there is a psb in this tracelet
ret = ppt_psb;
case ppt_psbend:
case ppt_fup:
case ppt_tip:
case ppt_tip_pge:
case ppt_tip_pgd:
case ppt_ovf:
case ppt_mode:
// just copy these packets
memcpy(*tracelet_data, *data, opcodesize);
*tracelet_data += opcodesize;
*tracelet_size += opcodesize;
*size -= opcodesize;
*data += opcodesize;
previous_opcode = opcode;
break;
default:
// skip over all other packets
*size -= opcodesize;
*data += opcodesize;
break;
}
}
return ret;
}
// checks if the trace starts with the expected IP address
int check_trace_start(unsigned char *data, size_t size, uint64_t expected_ip) {
unsigned char opcode;
unsigned char opcodesize;
previous_ip = 0;
while (size) {
if (!get_next_opcode(&data, &size, &opcode, &opcodesize)) return 0;
switch (opcode) {
case ppt_tip_pge:
if (decode_ip(data) == expected_ip) return 1;
else return 0;
case ppt_fup:
case ppt_tip:
case ppt_tnt_8:
case ppt_tnt_64:
case ppt_tip_pgd:
case ppt_invalid:
return 0;
default:
break;
}
size -= opcodesize;
data += opcodesize;
}
return 0;
}
// fast decoder that decodes only tip (and related packets)
// and skips over the reset
void decode_trace_tip_fast(unsigned char *data, size_t size, int coverage_kind) {
uint64_t next_ip;
unsigned char opcode;
unsigned char opcodesize;
previous_offset = 0;
previous_ip = 0;
current_range = &(coverage_ip_ranges[0]);
if (size < sizeof(psb)) return;
if (!findpsb(&data, &size)) {
FATAL("No sync packets in trace\n");