-
Notifications
You must be signed in to change notification settings - Fork 0
/
tnt_main.c
4106 lines (3623 loc) · 133 KB
/
tnt_main.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
/*--------------------------------------------------------------------*/
/*--- Taintgrind: The taint analysis Valgrind tool. tnt_main.c ---*/
/*--------------------------------------------------------------------*/
/*
This file is part of Taintgrind, the taint analysis Valgrind tool.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
*/
#include "pub_tool_basics.h"
#include "pub_tool_tooliface.h"
#include "pub_tool_vki.h" // keeps libcproc.h happy, syscall nums
#include "pub_tool_aspacemgr.h" // VG_(am_shadow_alloc)
#include "pub_tool_debuginfo.h" // VG_(get_fnname_w_offset), VG_(get_fnname)
#include "pub_tool_hashtable.h" // For tnt_include.h, VgHashtable
#include "pub_tool_libcassert.h" // tl_assert
#include "pub_tool_libcbase.h" // VG_STREQN
#include "pub_tool_libcprint.h" // VG_(message)
#include "pub_tool_libcproc.h" // VG_(getenv)
#include "pub_tool_replacemalloc.h" // VG_(replacement_malloc_process_cmd_line_option)
#include "pub_tool_machine.h" // VG_(get_IP)
#include "pub_tool_mallocfree.h" // VG_(out_of_memory_NORETURN)
#include "pub_tool_options.h" // VG_STR/BHEX/BINT_CLO
#include "pub_tool_oset.h" // OSet operations
#include "pub_tool_threadstate.h" // VG_(get_running_tid)
#include "pub_tool_xarray.h" // VG_(*XA)
#include "pub_tool_stacktrace.h" // VG_(get_and_pp_StackTrace)
#include "pub_tool_libcfile.h" // VG_(readlink)
#include "pub_tool_addrinfo.h" // VG_(describe_addr)
#include "pub_tool_stacktrace.h" // VG_(get_and_ppStackTrace)
#include "tnt_include.h"
#include "tnt_strings.h"
#include "tnt_structs.h"
//#include "tnt_asm.h"
/*------------------------------------------------------------*/
/*--- Fast-case knobs ---*/
/*------------------------------------------------------------*/
// Comment these out to disable the fast cases (don't just set them to zero).
#define PERF_FAST_LOADV 1
#define PERF_FAST_STOREV 1
#define PERF_FAST_SARP 1
/*---------- Taintgrind DEBUG statements---------*/
//#define DBG_MEM
//#define DBG_LOAD
//#define DBG_STORE
//#define DBG_COPY_ADDR_RANGE_STATE
/* --------------- Basic configuration --------------- */
/* Only change this. N_PRIMARY_MAP *must* be a power of 2. */
#if VG_WORDSIZE == 4
/* cover the entire address space */
# define N_PRIMARY_BITS 16
#else
/* Just handle the first 32G fast and the rest via auxiliary
primaries. If you change this, Memcheck will assert at startup.
See the definition of UNALIGNED_OR_HIGH for extensive comments. */
# define N_PRIMARY_BITS 19
#endif
/* Do not change this. */
#define N_PRIMARY_MAP ( ((UWord)1) << N_PRIMARY_BITS)
/* Do not change this. */
#define MAX_PRIMARY_ADDRESS (Addr)((((Addr)65536) * N_PRIMARY_MAP)-1)
// Taintgrind: UNDEFINED -> TAINTED, DEFINED -> UNTAINTED,
// PARTDEFINED -> PARTUNTAINTED
// These represent eight bits of memory.
#define VA_BITS2_NOACCESS 0x0 // 00b
#define VA_BITS2_TAINTED 0x1 // 01b
#define VA_BITS2_UNTAINTED 0x2 // 10b
#define VA_BITS2_PARTUNTAINTED 0x3 // 11b
// These represent 16 bits of memory.
#define VA_BITS4_NOACCESS 0x0 // 00_00b
#define VA_BITS4_TAINTED 0x5 // 01_01b
#define VA_BITS4_UNTAINTED 0xa // 10_10b
// These represent 32 bits of memory.
#define VA_BITS8_NOACCESS 0x00 // 00_00_00_00b
#define VA_BITS8_TAINTED 0x55 // 01_01_01_01b
#define VA_BITS8_UNTAINTED 0xaa // 10_10_10_10b
// These represent 64 bits of memory.
#define VA_BITS16_NOACCESS 0x0000 // 00_00_00_00b x 2
#define VA_BITS16_TAINTED 0x5555 // 01_01_01_01b x 2
#define VA_BITS16_UNTAINTED 0xaaaa // 10_10_10_10b x 2
#define SM_CHUNKS 16384
#define SM_OFF(aaa) (((aaa) & 0xffff) >> 2)
#define SM_OFF_16(aaa) (((aaa) & 0xffff) >> 3)
// Paranoia: it's critical for performance that the requested inlining
// occurs. So try extra hard.
#define INLINE inline __attribute__((always_inline))
static INLINE Addr start_of_this_sm ( Addr a ) {
return (a & (~SM_MASK));
}
static INLINE Bool is_start_of_sm ( Addr a ) {
return (start_of_this_sm(a) == a);
}
typedef
struct {
UChar vabits8[SM_CHUNKS];
}
SecMap;
// 3 distinguished secondary maps, one for no-access, one for
// accessible but undefined, and one for accessible and defined.
// Distinguished secondaries may never be modified.
#define SM_DIST_NOACCESS 0
#define SM_DIST_TAINTED 1
#define SM_DIST_UNTAINTED 2
static SecMap sm_distinguished[3];
static INLINE Bool is_distinguished_sm ( SecMap* sm ) {
return sm >= &sm_distinguished[0] && sm <= &sm_distinguished[2];
}
// -Start- Forward declarations for Taintgrind
void check_reg( UInt reg );
void infer_client_binary_name(UInt pc);
void bookkeeping_values(IRStmt *clone, UWord value);
void bookkeeping_taint(IRStmt *clone, UWord taint);
Int exit_early (IRStmt *clone, UWord taint);
Int exit_early_data (IRExpr *e, UWord taint);
void do_smt2(IRStmt *clone, UWord value, UWord taint);
//void print_asm(ULong pc);
int print_insn_type(ULong pc, IRStmt *clone, UWord size);
void print_info_flow(IRStmt *clone, UWord taint);
void print_info_flow_tmp(IRExpr *e, Int print_leading_space);
void print_info_flow_tmp_indirect(IRExpr *e);
UInt get_geti_puti_reg(IRRegArray *descr, IRExpr *ix, Int bias);
// -End- Forward declarations for Taintgrind
static void update_SM_counts(SecMap* oldSM, SecMap* newSM); //285
/* dist_sm points to one of our three distinguished secondaries. Make
a copy of it so that we can write to it.
*/
static SecMap* copy_for_writing ( SecMap* dist_sm )
{
SecMap* new_sm;
tl_assert(dist_sm == &sm_distinguished[0]
|| dist_sm == &sm_distinguished[1]
|| dist_sm == &sm_distinguished[2]);
new_sm = VG_(am_shadow_alloc)(sizeof(SecMap));
if (new_sm == NULL)
VG_(out_of_memory_NORETURN)( "memcheck:allocate new SecMap",
sizeof(SecMap) );
VG_(memcpy)(new_sm, dist_sm, sizeof(SecMap));
update_SM_counts(dist_sm, new_sm);
return new_sm;
}
/* --------------- Stats --------------- */
static Int n_issued_SMs = 0;
static Int n_deissued_SMs = 0;
static Int n_noaccess_SMs = N_PRIMARY_MAP; // start with many noaccess DSMs
static Int n_undefined_SMs = 0;
static Int n_defined_SMs = 0;
static Int n_non_DSM_SMs = 0;
static Int max_noaccess_SMs = 0;
static Int max_undefined_SMs = 0;
static Int max_defined_SMs = 0;
static Int max_non_DSM_SMs = 0;
/* # searches initiated in auxmap_L1, and # base cmps required */
static ULong n_auxmap_L1_searches = 0;
static ULong n_auxmap_L1_cmps = 0;
/* # of searches that missed in auxmap_L1 and therefore had to
be handed to auxmap_L2. And the number of nodes inserted. */
static ULong n_auxmap_L2_searches = 0;
static ULong n_auxmap_L2_nodes = 0;
//static Int n_sanity_cheap = 0;
//static Int n_sanity_expensive = 0;
static Int n_secVBit_nodes = 0;
static Int max_secVBit_nodes = 0;
static void update_SM_counts(SecMap* oldSM, SecMap* newSM)
{
if (oldSM == &sm_distinguished[SM_DIST_NOACCESS ]) n_noaccess_SMs --;
else if (oldSM == &sm_distinguished[SM_DIST_TAINTED]) n_undefined_SMs--;
else if (oldSM == &sm_distinguished[SM_DIST_UNTAINTED ]) n_defined_SMs --;
else { n_non_DSM_SMs --;
n_deissued_SMs ++; }
if (newSM == &sm_distinguished[SM_DIST_NOACCESS ]) n_noaccess_SMs ++;
else if (newSM == &sm_distinguished[SM_DIST_TAINTED]) n_undefined_SMs++;
else if (newSM == &sm_distinguished[SM_DIST_UNTAINTED ]) n_defined_SMs ++;
else { n_non_DSM_SMs ++;
n_issued_SMs ++; }
if (n_noaccess_SMs > max_noaccess_SMs ) max_noaccess_SMs = n_noaccess_SMs;
if (n_undefined_SMs > max_undefined_SMs) max_undefined_SMs = n_undefined_SMs;
if (n_defined_SMs > max_defined_SMs ) max_defined_SMs = n_defined_SMs;
if (n_non_DSM_SMs > max_non_DSM_SMs ) max_non_DSM_SMs = n_non_DSM_SMs;
}
/* --------------- Primary maps --------------- */
/* The main primary map. This covers some initial part of the address
space, addresses 0 .. (N_PRIMARY_MAP << 16)-1. The rest of it is
handled using the auxiliary primary map.
*/
static SecMap* primary_map[N_PRIMARY_MAP];
/* An entry in the auxiliary primary map. base must be a 64k-aligned
value, and sm points at the relevant secondary map. As with the
main primary map, the secondary may be either a real secondary, or
one of the three distinguished secondaries. DO NOT CHANGE THIS
LAYOUT: the first word has to be the key for OSet fast lookups.
*/
typedef
struct {
Addr base;
SecMap* sm;
}
AuxMapEnt;
/* Tunable parameter: How big is the L1 queue? */
#define N_AUXMAP_L1 24
/* Tunable parameter: How far along the L1 queue to insert
entries resulting from L2 lookups? */
#define AUXMAP_L1_INSERT_IX 12
static struct {
Addr base;
AuxMapEnt* ent; // pointer to the matching auxmap_L2 node
}
auxmap_L1[N_AUXMAP_L1];
static OSet* auxmap_L2 = NULL;
static void init_auxmap_L1_L2 ( void )
{
Int i;
for (i = 0; i < N_AUXMAP_L1; i++) {
auxmap_L1[i].base = 0;
auxmap_L1[i].ent = NULL;
}
tl_assert(0 == offsetof(AuxMapEnt,base));
tl_assert(sizeof(Addr) == sizeof(void*));
auxmap_L2 = VG_(OSetGen_Create)( /*keyOff*/ offsetof(AuxMapEnt,base),
/*fastCmp*/ NULL,
VG_(malloc), "mc.iaLL.1", VG_(free) );
}
/* Check representation invariants; if OK return NULL; else a
descriptive bit of text. Also return the number of
non-distinguished secondary maps referred to from the auxiliary
primary maps. */
//static HChar* check_auxmap_L1_L2_sanity ( Word* n_secmaps_found )
//{
// Word i, j;
/* On a 32-bit platform, the L2 and L1 tables should
both remain empty forever.
On a 64-bit platform:
In the L2 table:
all .base & 0xFFFF == 0
all .base > MAX_PRIMARY_ADDRESS
In the L1 table:
all .base & 0xFFFF == 0
all (.base > MAX_PRIMARY_ADDRESS
.base & 0xFFFF == 0
and .ent points to an AuxMapEnt with the same .base)
or
(.base == 0 and .ent == NULL)
*/
// *n_secmaps_found = 0;
// if (sizeof(void*) == 4) {
/* 32-bit platform */
// if (VG_(OSetGen_Size)(auxmap_L2) != 0)
// return "32-bit: auxmap_L2 is non-empty";
// for (i = 0; i < N_AUXMAP_L1; i++)
// if (auxmap_L1[i].base != 0 || auxmap_L1[i].ent != NULL)
// return "32-bit: auxmap_L1 is non-empty";
// } else {
/* 64-bit platform */
// UWord elems_seen = 0;
// AuxMapEnt *elem, *res;
// AuxMapEnt key;
/* L2 table */
// VG_(OSetGen_ResetIter)(auxmap_L2);
// while ( (elem = VG_(OSetGen_Next)(auxmap_L2)) ) {
// elems_seen++;
// if (0 != (elem->base & (Addr)0xFFFF))
// return "64-bit: nonzero .base & 0xFFFF in auxmap_L2";
// if (elem->base <= MAX_PRIMARY_ADDRESS)
// return "64-bit: .base <= MAX_PRIMARY_ADDRESS in auxmap_L2";
// if (elem->sm == NULL)
// return "64-bit: .sm in _L2 is NULL";
// if (!is_distinguished_sm(elem->sm))
// (*n_secmaps_found)++;
// }
// if (elems_seen != n_auxmap_L2_nodes)
// return "64-bit: disagreement on number of elems in _L2";
/* Check L1-L2 correspondence */
/* for (i = 0; i < N_AUXMAP_L1; i++) {
if (auxmap_L1[i].base == 0 && auxmap_L1[i].ent == NULL)
continue;
if (0 != (auxmap_L1[i].base & (Addr)0xFFFF))
return "64-bit: nonzero .base & 0xFFFF in auxmap_L1";
if (auxmap_L1[i].base <= MAX_PRIMARY_ADDRESS)
return "64-bit: .base <= MAX_PRIMARY_ADDRESS in auxmap_L1";
if (auxmap_L1[i].ent == NULL)
return "64-bit: .ent is NULL in auxmap_L1";
if (auxmap_L1[i].ent->base != auxmap_L1[i].base)
return "64-bit: _L1 and _L2 bases are inconsistent";*/
/* Look it up in auxmap_L2. */
/* key.base = auxmap_L1[i].base;
key.sm = 0;
res = VG_(OSetGen_Lookup)(auxmap_L2, &key);
if (res == NULL)
return "64-bit: _L1 .base not found in _L2";
if (res != auxmap_L1[i].ent)
return "64-bit: _L1 .ent disagrees with _L2 entry";
}*/
/* Check L1 contains no duplicates */
/* for (i = 0; i < N_AUXMAP_L1; i++) {
if (auxmap_L1[i].base == 0)
continue;
for (j = i+1; j < N_AUXMAP_L1; j++) {
if (auxmap_L1[j].base == 0)
continue;
if (auxmap_L1[j].base == auxmap_L1[i].base)
return "64-bit: duplicate _L1 .base entries";
}
}
}
return NULL;*/ /* ok */
//}
static void insert_into_auxmap_L1_at ( Word rank, AuxMapEnt* ent )
{
Word i;
tl_assert(ent);
tl_assert(rank >= 0 && rank < N_AUXMAP_L1);
for (i = N_AUXMAP_L1-1; i > rank; i--)
auxmap_L1[i] = auxmap_L1[i-1];
auxmap_L1[rank].base = ent->base;
auxmap_L1[rank].ent = ent;
}
static INLINE AuxMapEnt* maybe_find_in_auxmap ( Addr a )
{
AuxMapEnt key;
AuxMapEnt* res;
Word i;
tl_assert(a > MAX_PRIMARY_ADDRESS);
a &= ~(Addr)0xFFFF;
/* First search the front-cache, which is a self-organising
list containing the most popular entries. */
if (LIKELY(auxmap_L1[0].base == a))
return auxmap_L1[0].ent;
if (LIKELY(auxmap_L1[1].base == a)) {
Addr t_base = auxmap_L1[0].base;
AuxMapEnt* t_ent = auxmap_L1[0].ent;
auxmap_L1[0].base = auxmap_L1[1].base;
auxmap_L1[0].ent = auxmap_L1[1].ent;
auxmap_L1[1].base = t_base;
auxmap_L1[1].ent = t_ent;
return auxmap_L1[0].ent;
}
n_auxmap_L1_searches++;
for (i = 0; i < N_AUXMAP_L1; i++) {
if (auxmap_L1[i].base == a) {
break;
}
}
tl_assert(i >= 0 && i <= N_AUXMAP_L1);
n_auxmap_L1_cmps += (ULong)(i+1);
if (i < N_AUXMAP_L1) {
if (i > 0) {
Addr t_base = auxmap_L1[i-1].base;
AuxMapEnt* t_ent = auxmap_L1[i-1].ent;
auxmap_L1[i-1].base = auxmap_L1[i-0].base;
auxmap_L1[i-1].ent = auxmap_L1[i-0].ent;
auxmap_L1[i-0].base = t_base;
auxmap_L1[i-0].ent = t_ent;
i--;
}
return auxmap_L1[i].ent;
}
n_auxmap_L2_searches++;
/* First see if we already have it. */
key.base = a;
key.sm = 0;
res = VG_(OSetGen_Lookup)(auxmap_L2, &key);
if (res)
insert_into_auxmap_L1_at( AUXMAP_L1_INSERT_IX, res );
return res;
}
static AuxMapEnt* find_or_alloc_in_auxmap ( Addr a )
{
AuxMapEnt *nyu, *res;
/* First see if we already have it. */
res = maybe_find_in_auxmap( a );
if (LIKELY(res))
return res;
/* Ok, there's no entry in the secondary map, so we'll have
to allocate one. */
a &= ~(Addr)0xFFFF;
nyu = (AuxMapEnt*) VG_(OSetGen_AllocNode)( auxmap_L2, sizeof(AuxMapEnt) );
tl_assert(nyu);
nyu->base = a;
nyu->sm = &sm_distinguished[SM_DIST_NOACCESS];
VG_(OSetGen_Insert)( auxmap_L2, nyu );
insert_into_auxmap_L1_at( AUXMAP_L1_INSERT_IX, nyu );
n_auxmap_L2_nodes++;
return nyu;
}
/* --------------- SecMap fundamentals --------------- */ //586
// In all these, 'low' means it's definitely in the main primary map,
// 'high' means it's definitely in the auxiliary table.
static INLINE SecMap** get_secmap_low_ptr ( Addr a )
{
UWord pm_off = a >> 16;
//# if VG_DEBUG_MEMORY >= 1
tl_assert(pm_off < N_PRIMARY_MAP);
//# endif
return &primary_map[ pm_off ];
}
static INLINE SecMap** get_secmap_high_ptr ( Addr a )
{
AuxMapEnt* am = find_or_alloc_in_auxmap(a);
return &am->sm;
}
static SecMap** get_secmap_ptr ( Addr a )
{
return ( a <= MAX_PRIMARY_ADDRESS
? get_secmap_low_ptr(a)
: get_secmap_high_ptr(a));
}
static INLINE SecMap* get_secmap_for_reading_low ( Addr a )
{
return *get_secmap_low_ptr(a);
}
static INLINE SecMap* get_secmap_for_reading_high ( Addr a )
{
return *get_secmap_high_ptr(a);
}
static INLINE SecMap* get_secmap_for_writing_low(Addr a)
{
SecMap** p = get_secmap_low_ptr(a);
if (UNLIKELY(is_distinguished_sm(*p)))
*p = copy_for_writing(*p);
return *p;
}
static INLINE SecMap* get_secmap_for_writing_high ( Addr a )
{
SecMap** p = get_secmap_high_ptr(a);
if (UNLIKELY(is_distinguished_sm(*p)))
*p = copy_for_writing(*p);
return *p;
}
/* Produce the secmap for 'a', either from the primary map or by
ensuring there is an entry for it in the aux primary map. The
secmap may be a distinguished one as the caller will only want to
be able to read it.
*/
static INLINE SecMap* get_secmap_for_reading ( Addr a )
{
return ( a <= MAX_PRIMARY_ADDRESS
? get_secmap_for_reading_low (a)
: get_secmap_for_reading_high(a) );
}
/* Produce the secmap for 'a', either from the primary map or by
ensuring there is an entry for it in the aux primary map. The
secmap may not be a distinguished one, since the caller will want
to be able to write it. If it is a distinguished secondary, make a
writable copy of it, install it, and return the copy instead. (COW
semantics).
*/
static SecMap* get_secmap_for_writing ( Addr a )
{
return ( a <= MAX_PRIMARY_ADDRESS
? get_secmap_for_writing_low (a)
: get_secmap_for_writing_high(a) );
}
/* If 'a' has a SecMap, produce it. Else produce NULL. But don't
allocate one if one doesn't already exist. This is used by the
leak checker.
*/
/*static SecMap* maybe_get_secmap_for ( Addr a )
{
if (a <= MAX_PRIMARY_ADDRESS) {
return get_secmap_for_reading_low(a);
} else {
AuxMapEnt* am = maybe_find_in_auxmap(a);
return am ? am->sm : NULL;
}
}*/
/* --------------- Fundamental functions --------------- */
static INLINE
void insert_vabits2_into_vabits8 ( Addr a, UChar vabits2, UChar* vabits8 ) //682
{
UInt shift = (a & 3) << 1; // shift by 0, 2, 4, or 6
*vabits8 &= ~(0x3 << shift); // mask out the two old bits
*vabits8 |= (vabits2 << shift); // mask in the two new bits
}
static INLINE
void insert_vabits4_into_vabits8 ( Addr a, UChar vabits4, UChar* vabits8 )
{
UInt shift;
tl_assert(VG_IS_2_ALIGNED(a)); // Must be 2-aligned
shift = (a & 2) << 1; // shift by 0 or 4
*vabits8 &= ~(0xf << shift); // mask out the four old bits
*vabits8 |= (vabits4 << shift); // mask in the four new bits
}
static INLINE
UChar extract_vabits2_from_vabits8 ( Addr a, UChar vabits8 )
{
UInt shift = (a & 3) << 1; // shift by 0, 2, 4, or 6
vabits8 >>= shift; // shift the two bits to the bottom
return 0x3 & vabits8; // mask out the rest
}
static INLINE
UChar extract_vabits4_from_vabits8 ( Addr a, UChar vabits8 )
{
UInt shift;
tl_assert(VG_IS_2_ALIGNED(a)); // Must be 2-aligned
shift = (a & 2) << 1; // shift by 0 or 4
vabits8 >>= shift; // shift the four bits to the bottom
return 0xf & vabits8; // mask out the rest
}
// Note that these four are only used in slow cases. The fast cases do
// clever things like combine the auxmap check (in
// get_secmap_{read,writ}able) with alignment checks.
// *** WARNING! ***
// Any time this function is called, if it is possible that vabits2
// is equal to VA_BITS2_PARTUNTAINTED, then the corresponding entry in the
// sec-V-bits table must also be set!
static INLINE
void set_vabits2 ( Addr a, UChar vabits2 )
{
SecMap* sm = get_secmap_for_writing(a); // Taintgrind: only handle 32-bits
UWord sm_off = SM_OFF(a);
#ifdef DBG_MEM
// Taintgrind
// if (vabits2 == VA_BITS2_TAINTED)
VG_(printf)("set_vabits2 a:0x%08lx vabits2:0x%x sm->vabit8[sm_off]:0x%08x\n",
a, vabits2, (Int)&(sm->vabits8[sm_off]));
#endif
insert_vabits2_into_vabits8( a, vabits2, &(sm->vabits8[sm_off]) );
}
// Needed by TNT_(instrument)
//static INLINE
UChar get_vabits2 ( Addr a )
{
SecMap* sm = get_secmap_for_reading(a); // Taintgrind: only handle 32-bits
UWord sm_off = SM_OFF(a);
UChar vabits8 = sm->vabits8[sm_off];
#ifdef DBG_MEM
// Taintgrind
UChar result = extract_vabits2_from_vabits8(a, vabits8);
// if (vabits2 == VA_BITS2_TAINTED)
VG_(printf)("get_vabits2 a:0x%08lx vabits2:0x%x sm->vabit8[sm_off]:0x%08x\n",
a, result, (Int)&vabits8);
return result;
#endif
return extract_vabits2_from_vabits8(a, vabits8);
}
// *** WARNING! ***
// Any time this function is called, if it is possible that any of the
// 4 2-bit fields in vabits8 are equal to VA_BITS2_PARTUNTAINTED, then the
// corresponding entry(s) in the sec-V-bits table must also be set!
static INLINE
UChar get_vabits8_for_aligned_word32 ( Addr a )
{
SecMap* sm = get_secmap_for_reading(a);
UWord sm_off = SM_OFF(a);
UChar vabits8 = sm->vabits8[sm_off];
return vabits8;
}
static INLINE
void set_vabits8_for_aligned_word32 ( Addr a, UChar vabits8 )
{
SecMap* sm = get_secmap_for_writing(a);
UWord sm_off = SM_OFF(a);
sm->vabits8[sm_off] = vabits8;
}
// Forward declarations
static UWord get_sec_vbits8(Addr a);
static void set_sec_vbits8(Addr a, UWord vbits8);
// Returns False if there was an addressability error.
// Taintgrind: skip addressability check
static INLINE
Bool set_vbits8 ( Addr a, UChar vbits8 )
{
Bool ok = True;
UChar vabits2 = get_vabits2(a);
//if ( VA_BITS2_NOACCESS != vabits2 ) {
// Addressable. Convert in-register format to in-memory format.
// Also remove any existing sec V bit entry for the byte if no
// longer necessary.
if ( V_BITS8_UNTAINTED == vbits8 ) { vabits2 = VA_BITS2_UNTAINTED; }
else if ( V_BITS8_TAINTED == vbits8 ) { vabits2 = VA_BITS2_TAINTED; }
else { vabits2 = VA_BITS2_PARTUNTAINTED;
set_sec_vbits8(a, vbits8); }
set_vabits2(a, vabits2);
//} else {
// // Unaddressable! Do nothing -- when writing to unaddressable
// // memory it acts as a black hole, and the V bits can never be seen
// // again. So we don't have to write them at all.
// ok = False;
//}
return ok;
}
// Returns False if there was an addressability error. In that case, we put
// all defined bits into vbits8.
static INLINE
Bool get_vbits8 ( Addr a, UChar* vbits8 )
{
Bool ok = True;
UChar vabits2 = get_vabits2(a);
// Convert the in-memory format to in-register format.
if ( VA_BITS2_UNTAINTED == vabits2 ) { *vbits8 = V_BITS8_UNTAINTED; }
else if ( VA_BITS2_TAINTED == vabits2 ) { *vbits8 = V_BITS8_TAINTED; }
else if ( VA_BITS2_NOACCESS == vabits2 ) {
*vbits8 = V_BITS8_UNTAINTED; // Make V bits defined!
ok = False;
} else {
tl_assert( VA_BITS2_PARTUNTAINTED == vabits2 );
*vbits8 = get_sec_vbits8(a);
}
return ok;
}
/* --------------- Secondary V bit table ------------ */
static OSet* secVBitTable;
// Stats
static ULong sec_vbits_new_nodes = 0;
static ULong sec_vbits_updates = 0;
// This must be a power of two; this is checked in tnt_pre_clo_init().
// The size chosen here is a trade-off: if the nodes are bigger (ie. cover
// a larger address range) they take more space but we can get multiple
// partially-defined bytes in one if they are close to each other, reducing
// the number of total nodes. In practice sometimes they are clustered (eg.
// perf/bz2 repeatedly writes then reads more than 20,000 in a contiguous
// row), but often not. So we choose something intermediate.
#define BYTES_PER_SEC_VBIT_NODE 16
// We make the table bigger by a factor of STEPUP_GROWTH_FACTOR if
// more than this many nodes survive a GC.
#define STEPUP_SURVIVOR_PROPORTION 0.5
#define STEPUP_GROWTH_FACTOR 1.414213562
// If the above heuristic doesn't apply, then we may make the table
// slightly bigger, by a factor of DRIFTUP_GROWTH_FACTOR, if more than
// this many nodes survive a GC, _and_ the total table size does
// not exceed a fixed limit. The numbers are somewhat arbitrary, but
// work tolerably well on long Firefox runs. The scaleup ratio of 1.5%
// effectively although gradually reduces residency and increases time
// between GCs for programs with small numbers of PDBs. The 80000 limit
// effectively limits the table size to around 2MB for programs with
// small numbers of PDBs, whilst giving a reasonably long lifetime to
// entries, to try and reduce the costs resulting from deleting and
// re-adding of entries.
#define DRIFTUP_SURVIVOR_PROPORTION 0.15
#define DRIFTUP_GROWTH_FACTOR 1.015
#define DRIFTUP_MAX_SIZE 80000
// We GC the table when it gets this many nodes in it, ie. it's effectively
// the table size. It can change.
static Int secVBitLimit = 1000;
// The number of GCs done, used to age sec-V-bit nodes for eviction.
// Because it's unsigned, wrapping doesn't matter -- the right answer will
// come out anyway.
static UInt GCs_done = 0;
typedef
struct {
Addr a;
UChar vbits8[BYTES_PER_SEC_VBIT_NODE];
}
SecVBitNode;
static OSet* createSecVBitTable(void)
{
OSet* newSecVBitTable;
newSecVBitTable = VG_(OSetGen_Create_With_Pool)
( offsetof(SecVBitNode, a),
NULL, // use fast comparisons
VG_(malloc), "mc.cSVT.1 (sec VBit table)",
VG_(free),
1000,
sizeof(SecVBitNode));
return newSecVBitTable;
}
static void gcSecVBitTable(void)
{
OSet* secVBitTable2;
SecVBitNode* n;
Int i, n_nodes = 0, n_survivors = 0;
GCs_done++;
// Create the new table.
secVBitTable2 = createSecVBitTable();
// Traverse the table, moving fresh nodes into the new table.
VG_(OSetGen_ResetIter)(secVBitTable);
while ( (n = VG_(OSetGen_Next)(secVBitTable)) ) {
// Keep node if any of its bytes are non-stale. Using
// get_vabits2() for the lookup is not very efficient, but I don't
// think it matters.
for (i = 0; i < BYTES_PER_SEC_VBIT_NODE; i++) {
if (VA_BITS2_PARTUNTAINTED == get_vabits2(n->a + i)) {
// Found a non-stale byte, so keep =>
// Insert a copy of the node into the new table.
SecVBitNode* n2 =
VG_(OSetGen_AllocNode)(secVBitTable2, sizeof(SecVBitNode));
*n2 = *n;
VG_(OSetGen_Insert)(secVBitTable2, n2);
break;
}
}
}
// Get the before and after sizes.
n_nodes = VG_(OSetGen_Size)(secVBitTable);
n_survivors = VG_(OSetGen_Size)(secVBitTable2);
// Destroy the old table, and put the new one in its place.
VG_(OSetGen_Destroy)(secVBitTable);
secVBitTable = secVBitTable2;
if (VG_(clo_verbosity) > 1 && n_nodes != 0) {
VG_(message)(Vg_DebugMsg, "tnt_main.c: GC: %d nodes, %d survivors (%.1f%%)\n",
n_nodes, n_survivors, n_survivors * 100.0 / n_nodes);
}
// Increase table size if necessary.
if ((Double)n_survivors
> ((Double)secVBitLimit * STEPUP_SURVIVOR_PROPORTION)) {
secVBitLimit = (Int)((Double)secVBitLimit * (Double)STEPUP_GROWTH_FACTOR);
if (VG_(clo_verbosity) > 1)
VG_(message)(Vg_DebugMsg,
"tnt_main.c: GC: %d new table size (stepup)\n",
secVBitLimit);
}
else
if (secVBitLimit < DRIFTUP_MAX_SIZE
&& (Double)n_survivors
> ((Double)secVBitLimit * DRIFTUP_SURVIVOR_PROPORTION)) {
secVBitLimit = (Int)((Double)secVBitLimit * (Double)DRIFTUP_GROWTH_FACTOR);
if (VG_(clo_verbosity) > 1)
VG_(message)(Vg_DebugMsg,
"tnt_main.c GC: %d new table size (driftup)\n",
secVBitLimit);
}
}
static UWord get_sec_vbits8(Addr a)
{
Addr aAligned = VG_ROUNDDN(a, BYTES_PER_SEC_VBIT_NODE);
Int amod = a % BYTES_PER_SEC_VBIT_NODE;
SecVBitNode* n = VG_(OSetGen_Lookup)(secVBitTable, &aAligned);
UChar vbits8;
tl_assert2(n, "get_sec_vbits8: no node for address %p (%p)\n", aAligned, a);
// Shouldn't be fully defined or fully undefined -- those cases shouldn't
// make it to the secondary V bits table.
vbits8 = n->vbits8[amod];
tl_assert(V_BITS8_UNTAINTED != vbits8 && V_BITS8_TAINTED != vbits8);
return vbits8;
}
static void set_sec_vbits8(Addr a, UWord vbits8)
{
Addr aAligned = VG_ROUNDDN(a, BYTES_PER_SEC_VBIT_NODE);
Int i, amod = a % BYTES_PER_SEC_VBIT_NODE;
SecVBitNode* n = VG_(OSetGen_Lookup)(secVBitTable, &aAligned);
// Shouldn't be fully defined or fully undefined -- those cases shouldn't
// make it to the secondary V bits table.
tl_assert(V_BITS8_UNTAINTED != vbits8 && V_BITS8_TAINTED != vbits8);
if (n) {
n->vbits8[amod] = vbits8; // update
sec_vbits_updates++;
} else {
// Do a table GC if necessary. Nb: do this before creating and
// inserting the new node, to avoid erroneously GC'ing the new node.
if (secVBitLimit == VG_(OSetGen_Size)(secVBitTable)) {
gcSecVBitTable();
}
// New node: assign the specific byte, make the rest invalid (they
// should never be read as-is, but be cautious).
n = VG_(OSetGen_AllocNode)(secVBitTable, sizeof(SecVBitNode));
n->a = aAligned;
for (i = 0; i < BYTES_PER_SEC_VBIT_NODE; i++) {
n->vbits8[i] = V_BITS8_TAINTED;
}
n->vbits8[amod] = vbits8;
// Insert the new node.
VG_(OSetGen_Insert)(secVBitTable, n);
sec_vbits_new_nodes++;
n_secVBit_nodes = VG_(OSetGen_Size)(secVBitTable);
if (n_secVBit_nodes > max_secVBit_nodes)
max_secVBit_nodes = n_secVBit_nodes;
}
}
/* --------------- Endianness helpers --------------- */
/* Returns the offset in memory of the byteno-th most significant byte
in a wordszB-sized word, given the specified endianness. */
static INLINE UWord byte_offset_w ( UWord wordszB, Bool bigendian,
UWord byteno ) {
return bigendian ? (wordszB-1-byteno) : byteno;
}
/* --------------- Load/store slow cases. --------------- */
static
__attribute__((noinline))
void tnt_LOADV_128_or_256_slow ( /*OUT*/ULong* res,
Addr a, SizeT nBits, Bool bigendian )
{
//ULong pessim[4]; /* only used when p-l-ok=yes */
SSizeT szB = nBits / 8;
SSizeT szL = szB / 8; /* Size in Longs (64-bit units) */
SSizeT i, j; /* Must be signed. */
SizeT n_addrs_bad = 0;
Addr ai;
UChar vbits8;
Bool ok;
/* Code below assumes load size is a power of two and at least 64
bits. */
tl_assert((szB & (szB-1)) == 0 && szL > 0);
/* If this triggers, you probably just need to increase the size of
the pessim array. */
//tl_assert(szL <= sizeof(pessim) / sizeof(pessim[0]));
for (j = 0; j < szL; j++) {
//pessim[j] = V_BITS64_UNTAINTED;
res[j] = V_BITS64_TAINTED;
}
/* Make up a result V word, which contains the loaded data for
valid addresses and Defined for invalid addresses. Iterate over
the bytes in the word, from the most significant down to the
least. The vbits to return are calculated into vbits128. Also
compute the pessimising value to be used when
--partial-loads-ok=yes. n_addrs_bad is redundant (the relevant
info can be gleaned from the pessim array) but is used as a
cross-check. */
for (j = szL-1; j >= 0; j--) {
ULong vbits64 = V_BITS64_TAINTED;
//ULong pessim64 = V_BITS64_UNTAINTED;
UWord long_index = byte_offset_w(szL, bigendian, j);
for (i = 8-1; i >= 0; i--) {
PROF_EVENT(31, "tnt_LOADV_128_or_256_slow(loop)");
ai = a + 8*long_index + byte_offset_w(8, bigendian, i);
ok = get_vbits8(ai, &vbits8);
vbits64 <<= 8;
vbits64 |= vbits8;
if (!ok) n_addrs_bad++;
//pessim64 <<= 8;
//pessim64 |= (ok ? V_BITS8_UNTAINTED : V_BITS8_TAINTED);
}
res[long_index] = vbits64;
//pessim[long_index] = pessim64;
}
/* In the common case, all the addresses involved are valid, so we
just return the computed V bits and have done. */
//if (LIKELY(n_addrs_bad == 0))
return;
/* If there's no possibility of getting a partial-loads-ok
exemption, report the error and quit. */
//if (!MC_(clo_partial_loads_ok)) {
// MC_(record_address_error)( VG_(get_running_tid)(), a, szB, False );
// return;
//}
/* The partial-loads-ok excemption might apply. Find out if it
does. If so, don't report an addressing error, but do return
Undefined for the bytes that are out of range, so as to avoid
false negatives. If it doesn't apply, just report an addressing
error in the usual way. */
/* Some code steps along byte strings in aligned chunks
even when there is only a partially defined word at the end (eg,
optimised strlen). This is allowed by the memory model of
modern machines, since an aligned load cannot span two pages and
thus cannot "partially fault".
Therefore, a load from a partially-addressible place is allowed
if all of the following hold:
- the command-line flag is set [by default, it isn't]
- it's an aligned load
- at least one of the addresses in the word *is* valid
Since this suppresses the addressing error, we avoid false
negatives by marking bytes undefined when they come from an
invalid address.
*/
/* "at least one of the addresses is invalid" */
//ok = False;
//for (j = 0; j < szL; j++)
// ok |= pessim[j] != V_BITS8_TAINTED; //V_BITS8_UNTAINTED;
//if (0 == (a & (szB - 1)) && n_addrs_bad < szB) {