-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalgrind-memcheck.patch
More file actions
3211 lines (2992 loc) · 108 KB
/
valgrind-memcheck.patch
File metadata and controls
3211 lines (2992 loc) · 108 KB
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
From 82c55f6f22301d4cf29e8a74d96db47e906286a7 Mon Sep 17 00:00:00 2001
From: Mikael Jansson <mail@mikael.jansson.be>
Date: Sun, 22 Jul 2012 11:20:20 +0200
Subject: [PATCH 1/6] Valgrind malloc/free instrumentation.
Modified 'memcheck' for instrumenting free/malloc calls, printing out
size/id/address of allocated/freed blocks for later use with custom allocator.
translate.py, translate-2.py, translate-3.2.py does different passes at the
results for post-processing. It takes a fair amount of time and requires a
64-bit computer with lots of RAM and free disk space. Unfortunately, the order
(and when) to call the translate apps aren't documented. Check the source for
which files to apply them on.
---
valgrind/lackey/Makefile.am | 49 ++++++
valgrind/lackey/lk_main.c | 148 +++++++++++++++-
valgrind/memcheck/mc_include.h | 3 +
valgrind/memcheck/mc_malloc_wrappers.c | 24 +++
valgrind/memcheck/mc_translate.c | 301 ++++++++++++++++++++++++++++++++-
5 files changed, 514 insertions(+), 11 deletions(-)
diff --git a/valgrind/lackey/Makefile.am b/valgrind/lackey/Makefile.am
index 39ada21..1a4caa4 100644
--- a/valgrind/lackey/Makefile.am
+++ b/valgrind/lackey/Makefile.am
@@ -16,40 +16,89 @@ LACKEY_SOURCES_COMMON = lk_main.c
lackey_@VGCONF_ARCH_PRI@_@VGCONF_OS@_SOURCES = \
$(LACKEY_SOURCES_COMMON)
lackey_@VGCONF_ARCH_PRI@_@VGCONF_OS@_CPPFLAGS = \
$(AM_CPPFLAGS_@VGCONF_PLATFORM_PRI_CAPS@)
lackey_@VGCONF_ARCH_PRI@_@VGCONF_OS@_CFLAGS = \
$(AM_CFLAGS_@VGCONF_PLATFORM_PRI_CAPS@)
lackey_@VGCONF_ARCH_PRI@_@VGCONF_OS@_DEPENDENCIES = \
$(TOOL_DEPENDENCIES_@VGCONF_PLATFORM_PRI_CAPS@)
lackey_@VGCONF_ARCH_PRI@_@VGCONF_OS@_LDADD = \
$(TOOL_LDADD_@VGCONF_PLATFORM_PRI_CAPS@)
lackey_@VGCONF_ARCH_PRI@_@VGCONF_OS@_LDFLAGS = \
$(TOOL_LDFLAGS_@VGCONF_PLATFORM_PRI_CAPS@)
lackey_@VGCONF_ARCH_PRI@_@VGCONF_OS@_LINK = \
$(top_builddir)/coregrind/link_tool_exe_@VGCONF_OS@ \
@VALT_LOAD_ADDRESS_PRI@ \
$(LINK) \
$(lackey_@VGCONF_ARCH_PRI@_@VGCONF_OS@_CFLAGS) \
$(lackey_@VGCONF_ARCH_PRI@_@VGCONF_OS@_LDFLAGS)
if VGCONF_HAVE_PLATFORM_SEC
lackey_@VGCONF_ARCH_SEC@_@VGCONF_OS@_SOURCES = \
$(LACKEY_SOURCES_COMMON)
lackey_@VGCONF_ARCH_SEC@_@VGCONF_OS@_CPPFLAGS = \
$(AM_CPPFLAGS_@VGCONF_PLATFORM_SEC_CAPS@)
lackey_@VGCONF_ARCH_SEC@_@VGCONF_OS@_CFLAGS = \
$(AM_CFLAGS_@VGCONF_PLATFORM_SEC_CAPS@)
lackey_@VGCONF_ARCH_SEC@_@VGCONF_OS@_DEPENDENCIES = \
$(TOOL_DEPENDENCIES_@VGCONF_PLATFORM_SEC_CAPS@)
lackey_@VGCONF_ARCH_SEC@_@VGCONF_OS@_LDADD = \
$(TOOL_LDADD_@VGCONF_PLATFORM_SEC_CAPS@)
lackey_@VGCONF_ARCH_SEC@_@VGCONF_OS@_LDFLAGS = \
$(TOOL_LDFLAGS_@VGCONF_PLATFORM_SEC_CAPS@)
lackey_@VGCONF_ARCH_SEC@_@VGCONF_OS@_LINK = \
$(top_builddir)/coregrind/link_tool_exe_@VGCONF_OS@ \
@VALT_LOAD_ADDRESS_SEC@ \
$(LINK) \
$(lackey_@VGCONF_ARCH_SEC@_@VGCONF_OS@_CFLAGS) \
$(lackey_@VGCONF_ARCH_SEC@_@VGCONF_OS@_LDFLAGS)
endif
+
+#----------------------------------------------------------------------------
+# vgpreload_lackey-<platform>.so
+#----------------------------------------------------------------------------
+
+noinst_PROGRAMS += vgpreload_lackey-@VGCONF_ARCH_PRI@-@VGCONF_OS@.so
+if VGCONF_HAVE_PLATFORM_SEC
+noinst_PROGRAMS += vgpreload_lackey-@VGCONF_ARCH_SEC@-@VGCONF_OS@.so
+endif
+
+if VGCONF_OS_IS_DARWIN
+noinst_DSYMS = $(noinst_PROGRAMS)
+endif
+
+VGPRELOAD_LACKEY_SOURCES_COMMON =
+
+vgpreload_lackey_@VGCONF_ARCH_PRI@_@VGCONF_OS@_so_SOURCES = \
+ $(VGPRELOAD_LACKEY_SOURCES_COMMON)
+vgpreload_lackey_@VGCONF_ARCH_PRI@_@VGCONF_OS@_so_CPPFLAGS = \
+ $(AM_CPPFLAGS_@VGCONF_PLATFORM_PRI_CAPS@)
+vgpreload_lackey_@VGCONF_ARCH_PRI@_@VGCONF_OS@_so_CFLAGS = \
+ $(AM_CFLAGS_@VGCONF_PLATFORM_PRI_CAPS@) $(AM_CFLAGS_PIC) -O2
+vgpreload_lackey_@VGCONF_ARCH_PRI@_@VGCONF_OS@_so_DEPENDENCIES = \
+ $(LIBREPLACEMALLOC_@VGCONF_PLATFORM_PRI_CAPS@)
+vgpreload_lackey_@VGCONF_ARCH_PRI@_@VGCONF_OS@_so_LDFLAGS = \
+ $(PRELOAD_LDFLAGS_@VGCONF_PLATFORM_PRI_CAPS@) \
+ $(LIBREPLACEMALLOC_LDFLAGS_@VGCONF_PLATFORM_PRI_CAPS@)
+
+if VGCONF_HAVE_PLATFORM_SEC
+vgpreload_lackey_@VGCONF_ARCH_SEC@_@VGCONF_OS@_so_SOURCES = \
+ $(VGPRELOAD_LACKEY_SOURCES_COMMON)
+vgpreload_lackey_@VGCONF_ARCH_SEC@_@VGCONF_OS@_so_CPPFLAGS = \
+ $(AM_CPPFLAGS_@VGCONF_PLATFORM_SEC_CAPS@)
+vgpreload_lackey_@VGCONF_ARCH_SEC@_@VGCONF_OS@_so_CFLAGS = \
+ $(AM_CFLAGS_@VGCONF_PLATFORM_SEC_CAPS@) $(AM_CFLAGS_PIC) -O2
+vgpreload_lackey_@VGCONF_ARCH_SEC@_@VGCONF_OS@_so_DEPENDENCIES = \
+ $(LIBREPLACEMALLOC_@VGCONF_PLATFORM_SEC_CAPS@)
+vgpreload_lackey_@VGCONF_ARCH_SEC@_@VGCONF_OS@_so_LDFLAGS = \
+ $(PRELOAD_LDFLAGS_@VGCONF_PLATFORM_SEC_CAPS@) \
+ $(LIBREPLACEMALLOC_LDFLAGS_@VGCONF_PLATFORM_SEC_CAPS@)
+endif
+
+# mc_replace_strmem.c runs on the simulated CPU, and it often appears
+# in stack traces shown to the user. It is built with
+# -fno-omit-frame-pointer so as to guarantee robust backtraces on x86,
+# on which CFI based unwinding is not the "normal" case and so is
+# sometimes fragile.
+mc_replace_strmem.o: CFLAGS += -fno-omit-frame-pointer
+
diff --git a/valgrind/lackey/lk_main.c b/valgrind/lackey/lk_main.c
index 5d22d1b..ae24b7a 100644
--- a/valgrind/lackey/lk_main.c
+++ b/valgrind/lackey/lk_main.c
@@ -140,80 +140,88 @@
// uses the same basic technique for tracing memory accesses, but also groups
// events together for processing into twos and threes so that fewer C calls
// are made and things run faster.
//
// Specific Details about --trace-superblocks=yes
// ----------------------------------------------
// Valgrind splits code up into single entry, multiple exit blocks
// known as superblocks. By itself, --trace-superblocks=yes just
// prints a message as each superblock is run:
//
// SB 04013170
// SB 04013177
// SB 04013173
// SB 04013177
//
// The hex number is the address of the first instruction in the
// superblock. You can see the relationship more obviously if you use
// --trace-superblocks=yes and --trace-mem=yes together. Then a "SB"
// message at address X is immediately followed by an "instr:" message
// for that address, as the first instruction in the block is
// executed, for example:
//
// SB 04014073
// I 04014073,3
// L 7FEFFF7F8,8
// I 04014076,4
// I 0401407A,3
// I 0401407D,3
// I 04014080,3
// I 04014083,6
#include "pub_tool_basics.h"
#include "pub_tool_tooliface.h"
#include "pub_tool_libcassert.h"
#include "pub_tool_libcprint.h"
#include "pub_tool_debuginfo.h"
#include "pub_tool_libcbase.h"
#include "pub_tool_options.h"
#include "pub_tool_machine.h" // VG_(fnptr_to_fnentry)
+#include "pub_tool_mallocfree.h"
+#include "pub_tool_replacemalloc.h"
+
+#define HUMAN 1
+#define COMPUTER 2
+
+#define OUTPUT_FORMAT COMPUTER // HUMAN
+
/*------------------------------------------------------------*/
/*--- Command line options ---*/
/*------------------------------------------------------------*/
/* Command line options controlling instrumentation kinds, as described at
* the top of this file. */
static Bool clo_basic_counts = True;
static Bool clo_detailed_counts = False;
static Bool clo_trace_mem = False;
static Bool clo_trace_sbs = False;
/* The name of the function of which the number of calls (under
* --basic-counts=yes) is to be counted, with default. Override with command
* line option --fnname. */
static Char* clo_fnname = "main";
static Bool lk_process_cmd_line_option(Char* arg)
{
if VG_STR_CLO(arg, "--fnname", clo_fnname) {}
else if VG_BOOL_CLO(arg, "--basic-counts", clo_basic_counts) {}
else if VG_BOOL_CLO(arg, "--detailed-counts", clo_detailed_counts) {}
else if VG_BOOL_CLO(arg, "--trace-mem", clo_trace_mem) {}
else if VG_BOOL_CLO(arg, "--trace-superblocks", clo_trace_sbs) {}
else
return False;
tl_assert(clo_fnname);
tl_assert(clo_fnname[0]);
return True;
}
static void lk_print_usage(void)
{
VG_(printf)(
" --basic-counts=no|yes count instructions, jumps, etc. [yes]\n"
" --detailed-counts=no|yes count loads, stores and alu ops [no]\n"
" --trace-mem=no|yes trace all loads and stores [no]\n"
" --trace-superblocks=no|yes trace all superblock entries [no]\n"
" --fnname=<name> count calls to <name> (only used if\n"
@@ -405,99 +413,118 @@ typedef
IRAtom* addr;
Int size;
}
Event;
/* Up to this many unnotified events are allowed. Must be at least two,
so that reads and writes to the same address can be merged into a modify.
Beyond that, larger numbers just potentially induce more spilling due to
extending live ranges of address temporaries. */
#define N_EVENTS 4
/* Maintain an ordered list of memory events which are outstanding, in
the sense that no IR has yet been generated to do the relevant
helper calls. The SB is scanned top to bottom and memory events
are added to the end of the list, merging with the most recent
notified event where possible (Dw immediately following Dr and
having the same size and EA can be merged).
This merging is done so that for architectures which have
load-op-store instructions (x86, amd64), the instr is treated as if
it makes just one memory reference (a modify), rather than two (a
read followed by a write at the same address).
At various points the list will need to be flushed, that is, IR
generated from it. That must happen before any possible exit from
the block (the end, or an IRStmt_Exit). Flushing also takes place
when there is no space to add a new event.
If we require the simulation statistics to be up to date with
respect to possible memory exceptions, then the list would have to
be flushed before each memory reference. That's a pain so we don't
bother.
Flushing the list consists of walking it start to end and emitting
instrumentation IR for each event, in the order in which they
appear. */
static Event events[N_EVENTS];
static Int events_used = 0;
+// approximation to not print all loads
+static void *g_memory_start = (void *)0xffffffff;
+static void *g_memory_end = NULL;
+
static VG_REGPARM(2) void trace_instr(Addr addr, SizeT size)
{
- VG_(printf)("I %08lx,%lu\n", addr, size);
+ //VG_(printf)("I %08lx,%lu\n", addr, size);
}
static VG_REGPARM(2) void trace_load(Addr addr, SizeT size)
{
- VG_(printf)(" L %08lx,%lu\n", addr, size);
+ //if (addr >= (unsigned int)g_memory_start && addr+size < (unsigned int)g_memory_end)
+#if OUTPUT_FORMAT == HUMAN
+ VG_(printf)(" L %08lx,%lu\n", addr, size);
+#else
+ VG_(printf)("('load', 0x%lx, %lu)\n", addr, size);
+#endif
}
static VG_REGPARM(2) void trace_store(Addr addr, SizeT size)
{
- VG_(printf)(" S %08lx,%lu\n", addr, size);
+ //if (addr >= (unsigned int)g_memory_start && addr+size < (unsigned int)g_memory_end)
+#if OUTPUT_FORMAT == HUMAN
+ VG_(printf)(" S %08lx,%lu\n", addr, size);
+#else
+ VG_(printf)("('store', 0x%lx, %lu)\n", addr, size);
+#endif
}
static VG_REGPARM(2) void trace_modify(Addr addr, SizeT size)
{
- VG_(printf)(" M %08lx,%lu\n", addr, size);
+ //if (addr >= (unsigned int)g_memory_start && addr+size < (unsigned int)g_memory_end)
+#if OUTPUT_FORMAT == HUMAN
+ VG_(printf)(" M %08lx,%lu\n", addr, size);
+#else
+ VG_(printf)("('modify', 0x%lx, %lu)\n", addr, size);
+#endif
}
static void flushEvents(IRSB* sb)
{
Int i;
Char* helperName;
void* helperAddr;
IRExpr** argv;
IRDirty* di;
Event* ev;
for (i = 0; i < events_used; i++) {
ev = &events[i];
// Decide on helper fn to call and args to pass it.
switch (ev->ekind) {
case Event_Ir: helperName = "trace_instr";
helperAddr = trace_instr; break;
case Event_Dr: helperName = "trace_load";
helperAddr = trace_load; break;
case Event_Dw: helperName = "trace_store";
helperAddr = trace_store; break;
case Event_Dm: helperName = "trace_modify";
helperAddr = trace_modify; break;
default:
tl_assert(0);
}
// Add the helper.
argv = mkIRExprVec_2( ev->addr, mkIRExpr_HWord( ev->size ) );
di = unsafeIRDirty_0_N( /*regparms*/2,
helperName, VG_(fnptr_to_fnentry)( helperAddr ),
argv );
addStmtToIRSB( sb, IRStmt_Dirty(di) );
}
@@ -544,81 +571,85 @@ void addEvent_Dr ( IRSB* sb, IRAtom* daddr, Int dsize )
}
static
void addEvent_Dw ( IRSB* sb, IRAtom* daddr, Int dsize )
{
Event* lastEvt;
Event* evt;
tl_assert(clo_trace_mem);
tl_assert(isIRAtom(daddr));
tl_assert(dsize >= 1 && dsize <= MAX_DSIZE);
// Is it possible to merge this write with the preceding read?
lastEvt = &events[events_used-1];
if (events_used > 0
&& lastEvt->ekind == Event_Dr
&& lastEvt->size == dsize
&& eqIRAtom(lastEvt->addr, daddr))
{
lastEvt->ekind = Event_Dm;
return;
}
// No. Add as normal.
if (events_used == N_EVENTS)
flushEvents(sb);
tl_assert(events_used >= 0 && events_used < N_EVENTS);
evt = &events[events_used];
evt->ekind = Event_Dw;
evt->size = dsize;
evt->addr = daddr;
events_used++;
}
/*------------------------------------------------------------*/
/*--- Stuff for --trace-superblocks ---*/
/*------------------------------------------------------------*/
static void trace_superblock(Addr addr)
{
+#if OUTPUT_FORMAT == HUMAN
VG_(printf)("SB %08lx\n", addr);
+#else
+ VG_(printf)("('superblock', 0%lx)\n", addr);
+#endif
}
/*------------------------------------------------------------*/
/*--- Basic tool functions ---*/
/*------------------------------------------------------------*/
static void lk_post_clo_init(void)
{
Int op, tyIx;
if (clo_detailed_counts) {
for (op = 0; op < N_OPS; op++)
for (tyIx = 0; tyIx < N_TYPES; tyIx++)
detailCounts[op][tyIx] = 0;
}
}
static
IRSB* lk_instrument ( VgCallbackClosure* closure,
IRSB* sbIn,
VexGuestLayout* layout,
VexGuestExtents* vge,
IRType gWordTy, IRType hWordTy )
{
IRDirty* di;
Int i;
IRSB* sbOut;
Char fnname[100];
IRType type;
IRTypeEnv* tyenv = sbIn->tyenv;
Addr iaddr = 0, dst;
UInt ilen = 0;
Bool condition_inverted = False;
if (gWordTy != hWordTy) {
/* We don't currently support this case. */
VG_(tool_panic)("host/guest word size mismatch");
}
@@ -926,63 +957,172 @@ static void lk_fini(Int exitcode)
VG_(umsg)("\n");
VG_(umsg)("Jccs:\n");
VG_(umsg)(" total: %'llu\n", total_Jccs);
VG_(percentify)(taken_Jccs, (total_Jccs ? total_Jccs : 1),
percentify_decs, percentify_size, percentify_buf);
VG_(umsg)(" taken: %'llu (%s)\n",
taken_Jccs, percentify_buf);
VG_(umsg)("\n");
VG_(umsg)("Executed:\n");
VG_(umsg)(" SBs entered: %'llu\n", n_SBs_entered);
VG_(umsg)(" SBs completed: %'llu\n", n_SBs_completed);
VG_(umsg)(" guest instrs: %'llu\n", n_guest_instrs);
VG_(umsg)(" IRStmts: %'llu\n", n_IRStmts);
VG_(umsg)("\n");
VG_(umsg)("Ratios:\n");
tl_assert(n_SBs_entered); // Paranoia time.
VG_(umsg)(" guest instrs : SB entered = %'llu : 10\n",
10 * n_guest_instrs / n_SBs_entered);
VG_(umsg)(" IRStmts : SB entered = %'llu : 10\n",
10 * n_IRStmts / n_SBs_entered);
tl_assert(n_guest_instrs); // Paranoia time.
VG_(umsg)(" IRStmts : guest instr = %'llu : 10\n",
10 * n_IRStmts / n_guest_instrs);
}
if (clo_detailed_counts) {
VG_(umsg)("\n");
VG_(umsg)("IR-level counts by type:\n");
print_details();
}
if (clo_basic_counts) {
VG_(umsg)("\n");
VG_(umsg)("Exit code: %d\n", exitcode);
}
}
+static void *new_block(ThreadId tid, Addr p, SizeT szB, SizeT alignB, Bool is_zeroed)
+{
+ p = (Addr)VG_(cli_malloc)( alignB, szB );
+ if (!p)
+ return NULL;
+ if (is_zeroed) {
+ VG_(memset)((void*)p, 0, szB);
+ }
+
+ if (g_memory_start > (void *)p)
+ g_memory_start = (void *)p;
+ if (g_memory_end < (unsigned int)p + (unsigned int)szB)
+ g_memory_end = p + szB;
+
+#if OUTPUT_FORMAT == HUMAN
+ VG_(printf)("new_block(%u, align=%u) -> 0%x (start - end = 0%x to 0%x)\n", (unsigned int)szB,
+ (unsigned int)alignB, (void *)p, g_memory_start, g_memory_end);
+#else
+ VG_(printf)("('new', %lu, 0x%lx)\n", (unsigned int)szB, (void *)p);
+#endif
+ return (void *)p;
+}
+
+static void handle_free(ThreadId tid, Addr addr)
+{
+#if OUTPUT_FORMAT == HUMAN
+ VG_(printf)("handle_free(%p)\n", (void *)addr);
+#else
+ VG_(printf)("('free', 0x%lx)\n", addr);
+#endif
+ VG_(cli_free)((void *)addr);
+}
+
+
+static void* lk_malloc( ThreadId tid, SizeT n)
+{
+ return new_block(tid, /*Addr p*/0, n, VG_(clo_alignment), /*is_zeroed*/False);
+}
+
+static void* lk___builtin_new ( ThreadId tid, SizeT n )
+{
+ return new_block(tid, /*Addr p*/0, n, VG_(clo_alignment), /*is_zeroed*/False);
+}
+static void* lk___builtin_vec_new ( ThreadId tid, SizeT n )
+{
+ return new_block(tid, /*Addr p*/0, n, VG_(clo_alignment), /*is_zeroed*/False);
+}
+
+static void* lk_memalign ( ThreadId tid, SizeT alignB, SizeT n )
+{
+ //VG_(printf)("lk_memalign(%u, align=%u)\n", (unsigned int)n, (unsigned int)alignB);
+ return new_block(tid, /*Addr p*/0, n, alignB, /*is_zeroed*/False);
+}
+static void* lk_calloc ( ThreadId tid, SizeT nmemb, SizeT size1 )
+{
+ return new_block(tid, /*Addr p*/0, nmemb*size1, VG_(clo_alignment), /*is_zeroed*/True);
+}
+static void lk_free ( ThreadId tid, void* p )
+{
+ handle_free(tid, (Addr)p);
+}
+
+static void lk___builtin_delete ( ThreadId tid, void* p )
+{
+ handle_free(tid, (Addr)p);
+}
+
+static void lk___builtin_vec_delete ( ThreadId tid, void* p )
+{
+ handle_free(tid, (Addr)p);
+}
+
+static void* lk_realloc ( ThreadId tid, void* p_old, SizeT new_szB )
+{
+ return new_block(tid, /*Addr p*/(Addr)p_old, new_szB, VG_(clo_alignment), /*is_zeroed*/False);
+}
+
+static SizeT lk_malloc_usable_size ( ThreadId tid, void* p )
+{
+#if 0
+ MC_Chunk* mc = VG_(HT_lookup) ( MC_(malloc_list), (UWord)p );
+
+ // There may be slop, but pretend there isn't because only the asked-for
+ // area will be marked as addressable.
+ return ( mc ? mc->szB : 0 );
+ return 0;
+#endif
+
+ VG_(printf)("# lk_malloc_usable_size: %p\n", p);
+
+ return 0;
+}
+
+#define LK_MALLOC_REDZONE_SZB 16
+
static void lk_pre_clo_init(void)
{
VG_(details_name) ("Lackey");
VG_(details_version) (NULL);
VG_(details_description) ("an example Valgrind tool");
VG_(details_copyright_author)(
"Copyright (C) 2002-2011, and GNU GPL'd, by Nicholas Nethercote.");
VG_(details_bug_reports_to) (VG_BUGS_TO);
VG_(details_avg_translation_sizeB) ( 200 );
VG_(basic_tool_funcs) (lk_post_clo_init,
lk_instrument,
lk_fini);
VG_(needs_command_line_options)(lk_process_cmd_line_option,
lk_print_usage,
lk_print_debug_usage);
+
+ /*
+ VG_(needs_malloc_replacement) (lk_malloc,
+ lk___builtin_new,
+ lk___builtin_vec_new,
+ lk_memalign,
+ lk_calloc,
+ lk_free,
+ lk___builtin_delete,
+ lk___builtin_vec_delete,
+ lk_realloc,
+ NULL, //lk_malloc_usable_size,
+ LK_MALLOC_REDZONE_SZB );
+*/
}
VG_DETERMINE_INTERFACE_VERSION(lk_pre_clo_init)
/*--------------------------------------------------------------------*/
/*--- end lk_main.c ---*/
/*--------------------------------------------------------------------*/
diff --git a/valgrind/memcheck/mc_include.h b/valgrind/memcheck/mc_include.h
index 017868e..c9aee77 100644
--- a/valgrind/memcheck/mc_include.h
+++ b/valgrind/memcheck/mc_include.h
@@ -1,76 +1,79 @@
/*--------------------------------------------------------------------*/
/*--- A header file for all parts of the MemCheck tool. ---*/
/*--- mc_include.h ---*/
/*--------------------------------------------------------------------*/
/*
This file is part of MemCheck, a heavyweight Valgrind tool for
detecting memory errors.
Copyright (C) 2000-2011 Julian Seward
jseward@acm.org
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.
*/
#ifndef __MC_INCLUDE_H
#define __MC_INCLUDE_H
#define MC_(str) VGAPPEND(vgMemCheck_,str)
+#define HUMAN 0
+#define COMPUTER 1
+#define OUTPUT_FORMAT COMPUTER
/* This is a private header file for use only within the
memcheck/ directory. */
/*------------------------------------------------------------*/
/*--- Tracking the heap ---*/
/*------------------------------------------------------------*/
/* We want at least a 16B redzone on client heap blocks for Memcheck */
#define MC_MALLOC_REDZONE_SZB 16
/* For malloc()/new/new[] vs. free()/delete/delete[] mismatch checking. */
typedef
enum {
MC_AllocMalloc = 0,
MC_AllocNew = 1,
MC_AllocNewVec = 2,
MC_AllocCustom = 3
}
MC_AllocKind;
/* This describes a heap block. Nb: first two fields must match core's
* VgHashNode. */
typedef
struct _MC_Chunk {
struct _MC_Chunk* next;
Addr data; // Address of the actual block.
SizeT szB : (sizeof(SizeT)*8)-2; // Size requested; 30 or 62 bits.
MC_AllocKind allockind : 2; // Which operation did the allocation.
ExeContext* where; // Where it was allocated.
}
MC_Chunk;
/* Memory pool. Nb: first two fields must match core's VgHashNode. */
typedef
struct _MC_Mempool {
struct _MC_Mempool* next;
Addr pool; // pool identifier
SizeT rzB; // pool red-zone size
Bool is_zeroed; // allocations from this pool are zeroed
diff --git a/valgrind/memcheck/mc_malloc_wrappers.c b/valgrind/memcheck/mc_malloc_wrappers.c
index 7399c86..778d82c 100644
--- a/valgrind/memcheck/mc_malloc_wrappers.c
+++ b/valgrind/memcheck/mc_malloc_wrappers.c
@@ -21,80 +21,83 @@
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_execontext.h"
#include "pub_tool_poolalloc.h"
#include "pub_tool_hashtable.h"
#include "pub_tool_libcbase.h"
#include "pub_tool_libcassert.h"
#include "pub_tool_libcprint.h"
#include "pub_tool_mallocfree.h"
#include "pub_tool_options.h"
#include "pub_tool_replacemalloc.h"
#include "pub_tool_threadstate.h"
#include "pub_tool_tooliface.h" // Needed for mc_include.h
#include "pub_tool_stacktrace.h" // For VG_(get_and_pp_StackTrace)
#include "mc_include.h"
/*------------------------------------------------------------*/
/*--- Defns ---*/
/*------------------------------------------------------------*/
/* Stats ... */
static SizeT cmalloc_n_mallocs = 0;
static SizeT cmalloc_n_frees = 0;
static ULong cmalloc_bs_mallocd = 0;
/* For debug printing to do with mempools: what stack trace
depth to show. */
#define MEMPOOL_DEBUG_STACKTRACE_DEPTH 16
+// approximation to not print all loads
+void *g_memory_start = (void *)0xffffffff;
+void *g_memory_end = NULL;
/*------------------------------------------------------------*/
/*--- Tracking malloc'd and free'd blocks ---*/
/*------------------------------------------------------------*/
/* Record malloc'd blocks. */
VgHashTable MC_(malloc_list) = NULL;
/* Memory pools: a hash table of MC_Mempools. Search key is
MC_Mempool::pool. */
VgHashTable MC_(mempool_list) = NULL;
/* Pool allocator for MC_Chunk. */
PoolAlloc *MC_(chunk_poolalloc) = NULL;
static
MC_Chunk* create_MC_Chunk ( ExeContext* ec, Addr p, SizeT szB,
MC_AllocKind kind);
static inline
void delete_MC_Chunk (MC_Chunk* mc);
/* Records blocks after freeing. */
/* Blocks freed by the client are queued in one of two lists of
freed blocks not yet physically freed:
"big blocks" freed list.
"small blocks" freed list
The blocks with a size >= MC_(clo_freelist_big_blocks)
are linked in the big blocks freed list.
This allows a client to allocate and free big blocks
(e.g. bigger than VG_(clo_freelist_vol)) without losing
immediately all protection against dangling pointers.
position [0] is for big blocks, [1] is for small blocks. */
static MC_Chunk* freed_list_start[2] = {NULL, NULL};
static MC_Chunk* freed_list_end[2] = {NULL, NULL};
/* Put a shadow chunk on the freed blocks queue, possibly freeing up
some of the oldest blocks in the queue at the same time. */
static void add_to_freed_queue ( MC_Chunk* mc )
{
const Bool show = False;
const int l = (mc->szB >= MC_(clo_freelist_big_blocks) ? 0 : 1);
@@ -251,174 +254,195 @@ void* MC_(new_block) ( ThreadId tid,
Addr p, SizeT szB, SizeT alignB,
Bool is_zeroed, MC_AllocKind kind, VgHashTable table)
{
ExeContext* ec;
// Allocate and zero if necessary
if (p) {
tl_assert(MC_AllocCustom == kind);
} else {
tl_assert(MC_AllocCustom != kind);
p = (Addr)VG_(cli_malloc)( alignB, szB );
if (!p) {
return NULL;
}
if (is_zeroed) {
VG_(memset)((void*)p, 0, szB);
} else
if (MC_(clo_malloc_fill) != -1) {
tl_assert(MC_(clo_malloc_fill) >= 0x00 && MC_(clo_malloc_fill) <= 0xFF);
VG_(memset)((void*)p, MC_(clo_malloc_fill), szB);
}
}
// Only update stats if allocation succeeded.
cmalloc_n_mallocs ++;
cmalloc_bs_mallocd += (ULong)szB;
ec = VG_(record_ExeContext)(tid, 0/*first_ip_delta*/);
tl_assert(ec);
VG_(HT_add_node)( table, create_MC_Chunk(ec, p, szB, kind) );
if (is_zeroed)
MC_(make_mem_defined)( p, szB );
else {
UInt ecu = VG_(get_ECU_from_ExeContext)(ec);
tl_assert(VG_(is_plausible_ECU)(ecu));
MC_(make_mem_undefined_w_otag)( p, szB, ecu | MC_OKIND_HEAP );
}
+ //VG_(printf)("-> new block %p\n", p);
+
+ if (g_memory_start > (void *)p)
+ g_memory_start = (void *)p;
+ if (g_memory_end < (unsigned int)p + (unsigned int)szB)
+ g_memory_end = p + szB;
+
+#if OUTPUT_FORMAT == HUMAN
+ VG_(printf)("new_block(%u, align=%u) -> 0%x (start - end = 0%x to 0%x)\n", (unsigned int)szB,
+ (unsigned int)alignB, (void *)p, g_memory_start, g_memory_end);
+#else
+ VG_(printf)(">>> N %lu %lu\n", (unsigned int)p, (unsigned int)szB);
+#endif
return (void*)p;
}
void* MC_(malloc) ( ThreadId tid, SizeT n )
{
+ //VG_(printf)("MC_(malloc) size %u\n", n);
if (complain_about_silly_args(n, "malloc")) {
return NULL;
} else {
return MC_(new_block) ( tid, 0, n, VG_(clo_alignment),
/*is_zeroed*/False, MC_AllocMalloc, MC_(malloc_list));
}
}
void* MC_(__builtin_new) ( ThreadId tid, SizeT n )
{
if (complain_about_silly_args(n, "__builtin_new")) {
return NULL;
} else {
return MC_(new_block) ( tid, 0, n, VG_(clo_alignment),
/*is_zeroed*/False, MC_AllocNew, MC_(malloc_list));
}
}
void* MC_(__builtin_vec_new) ( ThreadId tid, SizeT n )
{
if (complain_about_silly_args(n, "__builtin_vec_new")) {
return NULL;
} else {
return MC_(new_block) ( tid, 0, n, VG_(clo_alignment),
/*is_zeroed*/False, MC_AllocNewVec, MC_(malloc_list));
}
}
void* MC_(memalign) ( ThreadId tid, SizeT alignB, SizeT n )
{
if (complain_about_silly_args(n, "memalign")) {
return NULL;
} else {
return MC_(new_block) ( tid, 0, n, alignB,
/*is_zeroed*/False, MC_AllocMalloc, MC_(malloc_list));
}
}
void* MC_(calloc) ( ThreadId tid, SizeT nmemb, SizeT size1 )
{
if (complain_about_silly_args2(nmemb, size1)) {
return NULL;
} else {
return MC_(new_block) ( tid, 0, nmemb*size1, VG_(clo_alignment),
/*is_zeroed*/True, MC_AllocMalloc, MC_(malloc_list));
}
}
static
void die_and_free_mem ( ThreadId tid, MC_Chunk* mc, SizeT rzB )
{
/* Note: we do not free fill the custom allocs produced
by MEMPOOL or by MALLOC/FREELIKE_BLOCK requests. */
if (MC_(clo_free_fill) != -1 && MC_AllocCustom != mc->allockind ) {
tl_assert(MC_(clo_free_fill) >= 0x00 && MC_(clo_free_fill) <= 0xFF);
VG_(memset)((void*)mc->data, MC_(clo_free_fill), mc->szB);
}
/* Note: make redzones noaccess again -- just in case user made them
accessible with a client request... */
MC_(make_mem_noaccess)( mc->data-rzB, mc->szB + 2*rzB );
/* Record where freed */
mc->where = VG_(record_ExeContext) ( tid, 0/*first_ip_delta*/ );
/* Put it out of harm's way for a while */
add_to_freed_queue ( mc );
/* If the free list volume is bigger than MC_(clo_freelist_vol),
we wait till the next block allocation to release blocks.
This increase the chance to discover dangling pointer usage,
even for big blocks being freed by the client. */
}
void MC_(handle_free) ( ThreadId tid, Addr p, UInt rzB, MC_AllocKind kind )
{
MC_Chunk* mc;
cmalloc_n_frees++;
+#if OUTPUT_FORMAT == HUMAN
+ VG_(printf)("hnewandle_free(%p)\n", (void *)p);
+#else
+ VG_(printf)(">>> F %lu %lu\n", (unsigned int)p, 0);
+#endif
+
mc = VG_(HT_remove) ( MC_(malloc_list), (UWord)p );
if (mc == NULL) {
MC_(record_free_error) ( tid, p );
} else {
/* check if it is a matching free() / delete / delete [] */
if (kind != mc->allockind) {
tl_assert(p == mc->data);
MC_(record_freemismatch_error) ( tid, mc );
}
die_and_free_mem ( tid, mc, rzB );
}
+ //VG_(printf)("-> handle free %p\n", p);
}
void MC_(free) ( ThreadId tid, void* p )
{
MC_(handle_free)(
tid, (Addr)p, MC_MALLOC_REDZONE_SZB, MC_AllocMalloc );
}
void MC_(__builtin_delete) ( ThreadId tid, void* p )
{
MC_(handle_free)(
tid, (Addr)p, MC_MALLOC_REDZONE_SZB, MC_AllocNew);
}
void MC_(__builtin_vec_delete) ( ThreadId tid, void* p )
{
MC_(handle_free)(
tid, (Addr)p, MC_MALLOC_REDZONE_SZB, MC_AllocNewVec);
}
void* MC_(realloc) ( ThreadId tid, void* p_old, SizeT new_szB )
{
MC_Chunk* mc;
void* p_new;
SizeT old_szB;
if (complain_about_silly_args(new_szB, "realloc"))
return NULL;
cmalloc_n_frees ++;
cmalloc_n_mallocs ++;
cmalloc_bs_mallocd += (ULong)new_szB;
/* Remove the old block */
mc = VG_(HT_remove) ( MC_(malloc_list), (UWord)p_old );
if (mc == NULL) {
MC_(record_free_error) ( tid, (Addr)p_old );
/* We return to the program regardless. */
return NULL;
}
diff --git a/valgrind/memcheck/mc_translate.c b/valgrind/memcheck/mc_translate.c
index cf35c71..2dcf9b8 100644
--- a/valgrind/memcheck/mc_translate.c
+++ b/valgrind/memcheck/mc_translate.c
@@ -4108,80 +4108,82 @@ IRExpr* zwidenToHostWord ( MCEnv* mce, IRAtom* vatom )
case Ity_I32:
return assignNew('V', mce, tyH, unop(Iop_32Uto64, vatom));
case Ity_I16:
return assignNew('V', mce, tyH, unop(Iop_32Uto64,
assignNew('V', mce, Ity_I32, unop(Iop_16Uto32, vatom))));
case Ity_I8:
return assignNew('V', mce, tyH, unop(Iop_32Uto64,
assignNew('V', mce, Ity_I32, unop(Iop_8Uto32, vatom))));
default:
goto unhandled;
}
} else {
goto unhandled;
}
unhandled:
VG_(printf)("\nty = "); ppIRType(ty); VG_(printf)("\n");
VG_(tool_panic)("zwidenToHostWord");
}
/* Generate a shadow store. addr is always the original address atom.
You can pass in either originals or V-bits for the data atom, but
obviously not both. guard :: Ity_I1 controls whether the store
really happens; NULL means it unconditionally does. Note that
guard itself is not checked for definedness; the caller of this
function must do that if necessary. */
static
void do_shadow_Store ( MCEnv* mce,
IREndness end,
IRAtom* addr, UInt bias,
IRAtom* data, IRAtom* vdata,
IRAtom* guard )
{
IROp mkAdd;
IRType ty, tyAddr;
void* helper = NULL;