]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Merge r9299..r9303 (various leak-check test fixes) from the DARWIN branch.
authorNicholas Nethercote <njn@valgrind.org>
Mon, 2 Mar 2009 05:11:06 +0000 (05:11 +0000)
committerNicholas Nethercote <njn@valgrind.org>
Mon, 2 Mar 2009 05:11:06 +0000 (05:11 +0000)
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@9304

47 files changed:
NEWS
memcheck/docs/mc-manual.xml
memcheck/mc_include.h
memcheck/mc_leakcheck.c
memcheck/mc_main.c
memcheck/memcheck.h
memcheck/tests/Makefile.am
memcheck/tests/error_counts.c
memcheck/tests/error_counts.stdout.exp
memcheck/tests/filter_leak_check_size [deleted file]
memcheck/tests/filter_stderr
memcheck/tests/leak-0.c
memcheck/tests/leak-0.stderr.exp
memcheck/tests/leak-0.stderr.exp2 [deleted file]
memcheck/tests/leak-0.vgtest
memcheck/tests/leak-cycle.c
memcheck/tests/leak-cycle.stderr.exp
memcheck/tests/leak-cycle.stderr.exp2 [deleted file]
memcheck/tests/leak-cycle.stderr.exp64 [deleted file]
memcheck/tests/leak-cycle.vgtest
memcheck/tests/leak-regroot.c [deleted file]
memcheck/tests/leak-regroot.stderr.exp [deleted file]
memcheck/tests/leak-regroot.stderr.exp2 [deleted file]
memcheck/tests/leak-regroot.vgtest [deleted file]
memcheck/tests/leak-tree.stderr.exp
memcheck/tests/leak-tree.stderr.exp2
memcheck/tests/leak-tree.stderr.exp64
memcheck/tests/leak-tree.vgtest
memcheck/tests/leak.h [new file with mode: 0644]
memcheck/tests/leakotron.c
memcheck/tests/mempool.stderr.exp
memcheck/tests/mempool.stderr.exp64
memcheck/tests/nanoleak.stderr.exp
memcheck/tests/nanoleak.vgtest
memcheck/tests/nanoleak_supp.vgtest
memcheck/tests/pointer-trace.c
memcheck/tests/pointer-trace.stderr.exp
memcheck/tests/pointer-trace.stderr.exp2 [deleted file]
memcheck/tests/pointer-trace.stderr.exp3 [deleted file]
memcheck/tests/pointer-trace.stderr.exp64 [deleted file]
memcheck/tests/pointer-trace.vgtest
memcheck/tests/trivialleak.stderr.exp
memcheck/tests/trivialleak.stderr.exp2
memcheck/tests/trivialleak.vgtest
memcheck/tests/xml1.stderr.exp
memcheck/tests/xml1.stderr.exp2
memcheck/tests/xml1.stderr.exp64

diff --git a/NEWS b/NEWS
index 738b3102b8642637e446f4e1e3b24b8dc687b4ca..1388af2a53d2139694c9bb63583c8733308eb431 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,11 @@
 
 Release 3.5.0 (???)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+* A new Memcheck client request VALGRIND_COUNT_LEAK_BLOCKS has been added.
+  It is similar to VALGRIND_COUNT_LEAKS but counts blocks instead of bytes.
+  [XXX: consider adding VALGRIND_COUNT_LEAK_BYTES as a synonym and
+  deprecating VALGRIND_COUNT_LEAKS, which wasn't a good name to begin with]
+
 * The location of some install files has changed.  This should not affect
   most users.  Those who might be affected:
 
index 1b52137c49c6179d1afc1cb7c3e35ab4a1791dbe..1d9b806828316538c68f0cdf37e0b1369267ae07 100644 (file)
@@ -1177,6 +1177,13 @@ arguments.</para>
     <varname>VALGRIND_DO_LEAK_CHECK</varname>.</para>
   </listitem>
 
+  <listitem>
+    <para><varname>VALGRIND_COUNT_LEAK_BLOCKS</varname>: identical to
+    <varname>VALGRIND_COUNT_LEAKS</varname> except that it returns the
+    number of blocks rather than the number of bytes in each
+    category.</para>
+  </listitem>
+
   <listitem>
     <para><varname>VALGRIND_GET_VBITS</varname> and
     <varname>VALGRIND_SET_VBITS</varname>: allow you to get and set the
index 851929820c88f8bc32f61c46feba96195412d8eb..0f5f66afa929a261bd301ea15546ef11e41564ea 100644 (file)
@@ -249,6 +249,13 @@ extern SizeT MC_(bytes_dubious);
 extern SizeT MC_(bytes_reachable);
 extern SizeT MC_(bytes_suppressed);
 
+/* For VALGRIND_COUNT_LEAK_BLOCKS client request */
+extern SizeT MC_(blocks_leaked);
+extern SizeT MC_(blocks_indirect);
+extern SizeT MC_(blocks_dubious);
+extern SizeT MC_(blocks_reachable);
+extern SizeT MC_(blocks_suppressed);
+
 typedef
    enum {
       LC_Off,
index 9647bf9d877f2417bdaa5b93df8f399ba5c7ce58..b465c5244b42695bcb6b6dc7aed12bd23dce9c30 100644 (file)
@@ -211,6 +211,12 @@ SizeT MC_(bytes_dubious)    = 0;
 SizeT MC_(bytes_reachable)  = 0;
 SizeT MC_(bytes_suppressed) = 0;
 
+SizeT MC_(blocks_leaked)     = 0;
+SizeT MC_(blocks_indirect)   = 0;
+SizeT MC_(blocks_dubious)    = 0;
+SizeT MC_(blocks_reachable)  = 0;
+SizeT MC_(blocks_suppressed) = 0;
+
 static Int lc_compar(void* n1, void* n2)
 {
    MC_Chunk* mc1 = *(MC_Chunk**)n1;
@@ -399,12 +405,6 @@ static void lc_do_leakcheck(Int clique)
    }
 }
 
-static SizeT blocks_leaked;
-static SizeT blocks_indirect;
-static SizeT blocks_dubious;
-static SizeT blocks_reachable;
-static SizeT blocks_suppressed;
-
 static void full_report(ThreadId tid)
 {
    Int i;
@@ -515,24 +515,24 @@ static void full_report(ThreadId tid)
                                   print_record );
 
       if (is_suppressed) {
-         blocks_suppressed     += p_min->num_blocks;
-         MC_(bytes_suppressed) += p_min->total_bytes;
+         MC_(blocks_suppressed) += p_min->num_blocks;
+         MC_(bytes_suppressed)  += p_min->total_bytes;
 
       } else if (Unreached == p_min->loss_mode) {
-         blocks_leaked       += p_min->num_blocks;
-         MC_(bytes_leaked)   += p_min->total_bytes;
+         MC_(blocks_leaked) += p_min->num_blocks;
+         MC_(bytes_leaked)  += p_min->total_bytes;
 
       } else if (IndirectLeak == p_min->loss_mode) {
-         blocks_indirect     += p_min->num_blocks;
-         MC_(bytes_indirect) += p_min->total_bytes;
+         MC_(blocks_indirect) += p_min->num_blocks;
+         MC_(bytes_indirect)  += p_min->total_bytes;
 
-      } else if (Interior   == p_min->loss_mode) {
-         blocks_dubious     += p_min->num_blocks;
-         MC_(bytes_dubious) += p_min->total_bytes;
+      } else if (Interior == p_min->loss_mode) {
+         MC_(blocks_dubious) += p_min->num_blocks;
+         MC_(bytes_dubious)  += p_min->total_bytes;
 
-      } else if (Proper       == p_min->loss_mode) {
-         blocks_reachable     += p_min->num_blocks;
-         MC_(bytes_reachable) += p_min->total_bytes;
+      } else if (Proper == p_min->loss_mode) {
+         MC_(blocks_reachable) += p_min->num_blocks;
+         MC_(bytes_reachable)  += p_min->total_bytes;
 
       } else {
          VG_(tool_panic)("generic_detect_memory_leaks: unknown loss mode");
@@ -551,22 +551,22 @@ static void make_summary(void)
 
       switch(lc_markstack[i].state) {
       case Unreached:
-        blocks_leaked++;
+        MC_(blocks_leaked)++;
         MC_(bytes_leaked) += size;
         break;
 
       case Proper:
-        blocks_reachable++;
+        MC_(blocks_reachable)++;
         MC_(bytes_reachable) += size;
         break;
 
       case Interior:
-        blocks_dubious++;
+        MC_(blocks_dubious)++;
         MC_(bytes_dubious) += size;
         break;
         
       case IndirectLeak:       /* shouldn't happen */
-        blocks_indirect++;
+        MC_(blocks_indirect)++;
         MC_(bytes_indirect) += size;
         break;
       }
@@ -812,11 +812,11 @@ void MC_(do_detect_memory_leaks) (
    if (VG_(clo_verbosity) > 0 && !VG_(clo_xml))
       VG_(message)(Vg_UserMsg, "checked %'lu bytes.", lc_scanned);
 
-   blocks_leaked     = MC_(bytes_leaked)     = 0;
-   blocks_indirect   = MC_(bytes_indirect)   = 0;
-   blocks_dubious    = MC_(bytes_dubious)    = 0;
-   blocks_reachable  = MC_(bytes_reachable)  = 0;
-   blocks_suppressed = MC_(bytes_suppressed) = 0;
+   MC_(blocks_leaked)     = MC_(bytes_leaked)     = 0;
+   MC_(blocks_indirect)   = MC_(bytes_indirect)   = 0;
+   MC_(blocks_dubious)    = MC_(bytes_dubious)    = 0;
+   MC_(blocks_reachable)  = MC_(bytes_reachable)  = 0;
+   MC_(blocks_suppressed) = MC_(bytes_suppressed) = 0;
 
    if (mode == LC_Full)
       full_report(tid);
@@ -827,23 +827,23 @@ void MC_(do_detect_memory_leaks) (
       VG_(message)(Vg_UserMsg, "");
       VG_(message)(Vg_UserMsg, "LEAK SUMMARY:");
       VG_(message)(Vg_UserMsg, "   definitely lost: %'lu bytes in %'lu blocks.",
-                               MC_(bytes_leaked), blocks_leaked );
-      if (blocks_indirect > 0)
+                               MC_(bytes_leaked), MC_(blocks_leaked) );
+      if (MC_(blocks_indirect) > 0)
         VG_(message)(Vg_UserMsg, "   indirectly lost: %'lu bytes in %'lu blocks.",
-                     MC_(bytes_indirect), blocks_indirect );
+                     MC_(bytes_indirect), MC_(blocks_indirect) );
       VG_(message)(Vg_UserMsg, "     possibly lost: %'lu bytes in %'lu blocks.",
-                               MC_(bytes_dubious), blocks_dubious );
+                               MC_(bytes_dubious), MC_(blocks_dubious) );
       VG_(message)(Vg_UserMsg, "   still reachable: %'lu bytes in %'lu blocks.",
-                               MC_(bytes_reachable), blocks_reachable );
+                               MC_(bytes_reachable), MC_(blocks_reachable) );
       VG_(message)(Vg_UserMsg, "        suppressed: %'lu bytes in %'lu blocks.",
-                               MC_(bytes_suppressed), blocks_suppressed );
+                               MC_(bytes_suppressed), MC_(blocks_suppressed) );
       if (mode == LC_Summary 
-          && (blocks_leaked + blocks_indirect 
-              + blocks_dubious + blocks_reachable) > 0) {
+          && (MC_(blocks_leaked) + MC_(blocks_indirect) 
+              + MC_(blocks_dubious) + MC_(blocks_reachable)) > 0) {
          VG_(message)(Vg_UserMsg,
                       "Rerun with --leak-check=full to see details of leaked memory.");
       }
-      if (blocks_reachable > 0 && !MC_(clo_show_reachable) && mode == LC_Full) {
+      if (MC_(blocks_reachable) > 0 && !MC_(clo_show_reachable) && mode == LC_Full) {
          VG_(message)(Vg_UserMsg, 
            "Reachable blocks (those to which a pointer was found) are not shown.");
          VG_(message)(Vg_UserMsg, 
index 4c474d48ba7213fa32f0773fd05ddb5c5da6e182..3bec58ffc3268fe466b0fc3bddb279d285984cb5 100644 (file)
@@ -1056,9 +1056,9 @@ INLINE Bool MC_(in_ignored_range) ( Addr a )
 
 static Bool isHex ( UChar c )
 {
-  return ((c >= '0' && c <= '9')
-         || (c >= 'a' && c <= 'f')
-         || (c >= 'A' && c <= 'F'));
+  return ((c >= '0' && c <= '9') ||
+         (c >= 'a' && c <= 'f') ||
+         (c >= 'A' && c <= 'F'));
 }
 
 static UInt fromHex ( UChar c )
@@ -5005,7 +5005,23 @@ static Bool mc_handle_client_request ( ThreadId tid, UWord* arg, UWord* ret )
          *argp[4] = MC_(bytes_suppressed);
          // there is no argp[5]
          //*argp[5] = MC_(bytes_indirect);
-         // XXX need to make *argp[1-4] defined
+         // XXX need to make *argp[1-4] defined;  currently done in the
+         // VALGRIND_COUNT_LEAKS_MACRO by initialising them to zero.
+         *ret = 0;
+         return True;
+      }
+      case VG_USERREQ__COUNT_LEAK_BLOCKS: { /* count leaked blocks */
+         UWord** argp = (UWord**)arg;
+         // MC_(blocks_leaked) et al were set by the last leak check (or zero
+         // if no prior leak checks performed).
+         *argp[1] = MC_(blocks_leaked) + MC_(blocks_indirect);
+         *argp[2] = MC_(blocks_dubious);
+         *argp[3] = MC_(blocks_reachable);
+         *argp[4] = MC_(blocks_suppressed);
+         // there is no argp[5]
+         //*argp[5] = MC_(blocks_indirect);
+         // XXX need to make *argp[1-4] defined;  currently done in the
+         // VALGRIND_COUNT_LEAK_BLOCKS_MACRO by initialising them to zero.
          *ret = 0;
          return True;
       }
index dd56c9dfac16ef1be257c33c5c69b7dc37a88b49..f192f722d3b3705a5a38fcf93f77bb2a98973c5c 100644 (file)
@@ -93,6 +93,9 @@ typedef
 
       VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE,
 
+      /* Not next to VG_USERREQ__COUNT_LEAKS because it was added later. */
+      VG_USERREQ__COUNT_LEAK_BLOCKS,
+
       /* This is just for memcheck's internal use - don't use it */
       _VG_USERREQ__MEMCHECK_RECORD_OVERLAP_ERROR 
          = VG_USERREQ_TOOL_BASE('M','C') + 256
@@ -228,7 +231,7 @@ typedef
       specified, which works no matter what type 'leaked', 'dubious', etc
       are.  We also initialise '_qzz_leaked', etc because
       VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
-      initialised. */                                                    \
+      defined. */                                                        \
    {unsigned long _qzz_res;                                              \
     unsigned long _qzz_leaked    = 0, _qzz_dubious    = 0;               \
     unsigned long _qzz_reachable = 0, _qzz_suppressed = 0;               \
@@ -242,6 +245,28 @@ typedef
     suppressed = _qzz_suppressed;                                        \
    }
 
+/* Return number of leaked, dubious, reachable and suppressed bytes found by
+   all previous leak checks.  They must be lvalues.  */
+#define VALGRIND_COUNT_LEAK_BLOCKS(leaked, dubious, reachable, suppressed) \
+   /* For safety on 64-bit platforms we assign the results to private
+      unsigned long variables, then assign these to the lvalues the user
+      specified, which works no matter what type 'leaked', 'dubious', etc
+      are.  We also initialise '_qzz_leaked', etc because
+      VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
+      defined. */                                                        \
+   {unsigned long _qzz_res;                                              \
+    unsigned long _qzz_leaked    = 0, _qzz_dubious    = 0;               \
+    unsigned long _qzz_reachable = 0, _qzz_suppressed = 0;               \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                              \
+                               VG_USERREQ__COUNT_LEAK_BLOCKS,            \
+                               &_qzz_leaked, &_qzz_dubious,              \
+                               &_qzz_reachable, &_qzz_suppressed, 0);    \
+    leaked     = _qzz_leaked;                                            \
+    dubious    = _qzz_dubious;                                           \
+    reachable  = _qzz_reachable;                                         \
+    suppressed = _qzz_suppressed;                                        \
+   }
+
 
 /* Get the validity data for addresses [zza..zza+zznbytes-1] and copy it
    into the provided zzvbits array.  Return values:
index d88c071c517d12b52ddd4f72cce56b42fa60c5fb..b298cad9e6f3fe64321f2fefa3c9074c8bebfd5f 100644 (file)
@@ -23,7 +23,7 @@ endif
 
 DIST_SUBDIRS = x86 amd64 linux x86-linux .
 
-noinst_SCRIPTS = filter_allocs filter_leak_check_size \
+noinst_SCRIPTS = filter_allocs \
                 filter_stderr filter_xml \
                 filter_varinfo3
 
@@ -59,7 +59,7 @@ EXTRA_DIST = $(noinst_SCRIPTS) \
        inits.stderr.exp inits.vgtest \
        inline.stderr.exp inline.stdout.exp inline.vgtest \
        leak-0.vgtest leak-0.stderr.exp \
-       leak-cycle.vgtest leak-cycle.stderr.exp leak-cycle.stderr.exp64 \
+       leak-cycle.vgtest leak-cycle.stderr.exp \
        leak-pool-0.vgtest leak-pool-0.stderr.exp leak-pool-0.stderr.exp64 \
        leak-pool-1.vgtest leak-pool-1.stderr.exp leak-pool-1.stderr.exp64 \
        leak-pool-2.vgtest leak-pool-2.stderr.exp leak-pool-2.stderr.exp64 \
@@ -68,7 +68,6 @@ EXTRA_DIST = $(noinst_SCRIPTS) \
        leak-pool-5.vgtest leak-pool-5.stderr.exp leak-pool-5.stderr.exp64 \
        leak-tree.vgtest leak-tree.stderr.exp \
        leak-tree.stderr.exp2 leak-tree.stderr.exp64 \
-       leak-regroot.vgtest leak-regroot.stderr.exp \
        leakotron.vgtest leakotron.stdout.exp leakotron.stderr.exp \
        linux-syslog-syscall linux-syslog-syscall.stderr.exp \
        linux-syscalls-2007 linux-syscalls-2007.stderr.exp \
@@ -188,7 +187,7 @@ check_PROGRAMS = \
        doublefree error_counts errs1 exitprog execve execve2 erringfds \
        file_locking \
        fprw fwrite inits inline \
-       leak-0 leak-cycle leak-pool leak-tree leak-regroot leakotron \
+       leak-0 leak-cycle leak-pool leak-tree leakotron \
        linux-syslog-syscall \
        linux-syscalls-2007 \
        long_namespace_xml \
index 89c0c23b0b94a85c2226d38dc020eb3e2091e9ae..336be0c65667a40c9d8a98132f282cff35a1e2c6 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <assert.h>
 #include "../memcheck.h"
+#include "leak.h"
 
 int main(void)
 {
@@ -10,16 +11,13 @@ int main(void)
    int* reachable;
    int* dubious;
    int* leaked;
-   long n_reachable  = 0;
-   long n_dubious    = 0;
-   long n_leaked     = 0;
-   long n_suppressed = 0;
+   DECLARE_LEAK_COUNTERS;
 
    /* we require these longs to have same size as a machine word */
    assert(sizeof(long) == sizeof(void*));
 
    /* Error counting */
-   printf("errors: %d\n", VALGRIND_COUNT_ERRORS);
+   printf("errors: %d\n\n", VALGRIND_COUNT_ERRORS);
 
    if (x == 0) {
       y++;
@@ -27,14 +25,16 @@ int main(void)
       y--;
    }
 
-   printf("errors: %d\n", VALGRIND_COUNT_ERRORS);
+   printf("errors: %d\n\n", VALGRIND_COUNT_ERRORS);
+
+   // Get a baseline, after start-up and also after printf (because Darwin
+   // printf allocates memory the first time it's called!)
+   GET_INITIAL_LEAK_COUNTS;
 
    /* Leak checking */
-   VALGRIND_DO_LEAK_CHECK;
-   VALGRIND_COUNT_LEAKS(n_leaked, n_dubious, n_reachable, n_suppressed);
-   if (n_reachable == 24) n_reachable = 0; /* handle glibc differences */
-   printf("leaks: %ldB, %ldB, %ldB, %ldB\n",
-          n_leaked, n_dubious, n_reachable, n_suppressed);
+   GET_FINAL_LEAK_COUNTS;
+   PRINT_LEAK_COUNTS(stdout);
+   printf("\n");
 
    leaked = malloc(77);
    leaked = 0;
@@ -44,12 +44,9 @@ int main(void)
 
    reachable = malloc(99);
 
-   VALGRIND_DO_LEAK_CHECK;
-   VALGRIND_DO_LEAK_CHECK;
-   VALGRIND_COUNT_LEAKS(n_leaked, n_dubious, n_reachable, n_suppressed);
-   if (n_reachable == 123) n_reachable = 99; /* handle glibc differences */
-   printf("leaks: %ldB, %ldB, %ldB, %ldB\n",
-          n_leaked, n_dubious, n_reachable, n_suppressed);
+   GET_FINAL_LEAK_COUNTS;
+   PRINT_LEAK_COUNTS(stdout);
+   printf("\n");
 
    printf("errors: %d\n", VALGRIND_COUNT_ERRORS);
 
index ea11aad9b35126bd92e66da58ecfc7b568f95a27..66c8a6492685c504f8a01e2fc4c2fb8251340431 100644 (file)
@@ -1,5 +1,15 @@
 errors: 0
+
 errors: 1
-leaks: 0B, 0B, 0B, 0B
-leaks: 77B, 88B, 99B, 0B
+
+leaked:       0 bytes in  0 blocks
+dubious:      0 bytes in  0 blocks
+reachable:    0 bytes in  0 blocks
+suppressed:   0 bytes in  0 blocks
+
+leaked:      77 bytes in  1 blocks
+dubious:     88 bytes in  1 blocks
+reachable:   99 bytes in  1 blocks
+suppressed:   0 bytes in  0 blocks
+
 errors: 1
diff --git a/memcheck/tests/filter_leak_check_size b/memcheck/tests/filter_leak_check_size
deleted file mode 100755 (executable)
index ad9e2b1..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-#! /bin/sh
-
-./filter_stderr |
-sed "s/checked [0-9,]* bytes./checked ... bytes./"
index aeb8c41dba1e20f3e7dd936820079977197dea88..d3ae4860cfde9d28a7008864554031702785d929 100755 (executable)
@@ -31,5 +31,13 @@ perl -p -0 -e 's/(Syscall param[^\n<]*)\n(   (at|by)[^\n]*\n)*/$1\n   ...\n/gs'
 
 # Something similar for XML output.  Chops everything within
 # <stack>...</stack>.
-perl -p -0 -e 's/(<what>Syscall param[^\n]*)\n([^\n]*(stack|frame|ip|obj|fn)[^\n]*\n)*/$1\n/gs'
+perl -p -0 -e 's/(<what>Syscall param[^\n]*)\n([^\n]*(stack|frame|ip|obj|fn)[^\n]*\n)*/$1\n/gs' |
+
+# Leak check filtering.
+sed "s/checked [0-9,]* bytes./checked ... bytes./" |
+
+# More leak check filtering.  For systems that do extra libc allocations
+# (eg. Darwin) there may be extra (reachable, and thus not shown) loss
+# records.  So we filter out the loss record numbers.
+perl -p -e "s/lost in loss record \d+ of \d+/lost in loss record ... of .../" 
 
index 09eb1831f1db9f7c697baf7040bf6e24a91cd451..d563cb49a7e493a9b23a5f1b0024a34779129dd3 100644 (file)
@@ -1,15 +1,23 @@
+#include <stdio.h>
 #include <stdlib.h>
+#include "leak.h"
 #include "../memcheck.h"
 
-/* 
-   Live 0-sized blocks were being reported as leaked.
- */
+// Live 0-sized blocks were being reported as leaked.
+// Also, test that a pointer in a register is handled correctly.
 int main()
 {
-       void *volatile foo = malloc(0);
+       DECLARE_LEAK_COUNTERS;
 
-       //printf("&foo=%p foo=%p\n", &foo, foo);
-       VALGRIND_DO_LEAK_CHECK;
+       register char *foo;
+
+        GET_INITIAL_LEAK_COUNTS;
+
+       foo = malloc(0);
+
+       GET_FINAL_LEAK_COUNTS;
+
+       PRINT_LEAK_COUNTS(stderr);
 
        free(foo);
        return 0;
index adb0de3b4ef95e5d2b8e0041543b3ff4122d3a1a..ca8fab009d183a0efb4887923090e650d6f6f9e1 100644 (file)
@@ -1,17 +1,4 @@
-
-searching for pointers to 1 not-freed blocks.
-checked ... bytes.
-
-LEAK SUMMARY:
-   definitely lost: 0 bytes in 0 blocks.
-     possibly lost: 0 bytes in 0 blocks.
-   still reachable: 0 bytes in 1 blocks.
-        suppressed: 0 bytes in 0 blocks.
-Reachable blocks (those to which a pointer was found) are not shown.
-To see them, rerun with: --leak-check=full --show-reachable=yes
-
-ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
-malloc/free: in use at exit: 0 bytes in 0 blocks.
-malloc/free: 1 allocs, 1 frees, 0 bytes allocated.
-For a detailed leak analysis,  rerun with: --leak-check=yes
-For counts of detected errors, rerun with: -v
+leaked:       0 bytes in  0 blocks
+dubious:      0 bytes in  0 blocks
+reachable:    0 bytes in  1 blocks
+suppressed:   0 bytes in  0 blocks
diff --git a/memcheck/tests/leak-0.stderr.exp2 b/memcheck/tests/leak-0.stderr.exp2
deleted file mode 100644 (file)
index 23df1ed..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-
-searching for pointers to 2 not-freed blocks.
-checked ... bytes.
-
-LEAK SUMMARY:
-   definitely lost: 0 bytes in 0 blocks.
-   indirectly lost: 0 bytes in 0 blocks.
-   possibly lost:   0 bytes in 0 blocks.
-   still reachable: 24 bytes in 2 blocks.
-        suppressed: 0 bytes in 0 blocks.
-Reachable blocks (those to which a pointer was found) are not shown.
-To see them, rerun with: --leak-check=full --show-reachable=yes
-
-ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
-malloc/free: in use at exit: 0 bytes in 0 blocks.
-malloc/free: 2 allocs, 2 frees, 24 bytes allocated.
-For a detailed leak analysis,  rerun with: --leak-check=yes
-For counts of detected errors, rerun with: -v
index eff976fbdec7e54d9d11768966f0ac1eb0255520..fd67d70fa290da26db64d451d76d452dcc57a76b 100644 (file)
@@ -1,2 +1,2 @@
 prog: leak-0
-stderr_filter: filter_leak_check_size
+vgopts: -q
index 3e4dab1dd0cf61f0eeaf33b78126734aba5b2267..76dae77d5293554704db9c3459c6922d7059b48c 100644 (file)
@@ -1,14 +1,18 @@
+#include <stdio.h>
 #include <stdlib.h>
+#include "leak.h"
 #include "../memcheck.h"
 
 struct n {
        struct n *l;
        struct n *r;
+        // This ensures it's the same size on 32-bit and 64-bit platforms.
+        char padding[ 2 * (8 - sizeof(struct n*)) ];
 };
 
 struct n *mk(struct n *l, struct n *r)
 {
-       struct n *n = malloc(sizeof(*n));
+       struct n *n = malloc(sizeof(struct n));
        n->l = l;
        n->r = r;
 
@@ -27,19 +31,21 @@ static struct n *mkcycle()
        return a;
 }
 
+
 int main()
 {
+       DECLARE_LEAK_COUNTERS;
 
        struct n *volatile c1, *volatile c2;
 
+        GET_INITIAL_LEAK_COUNTS;
+
        /* two simple cycles */
        c1 = mkcycle();
        c2 = mkcycle();
 
        c1 = c2 = 0;
 
-       //VALGRIND_DO_LEAK_CHECK;
-
        /* one cycle linked to another */
        c1 = mkcycle();
        c2 = mkcycle();
@@ -53,8 +59,6 @@ int main()
 
        c1 = c2 = 0;
 
-       //VALGRIND_DO_LEAK_CHECK;
-
        /* two linked cycles */
        c1 = mkcycle();
        c2 = mkcycle();
@@ -64,7 +68,9 @@ int main()
 
        c1 = c2 = 0;
 
-       VALGRIND_DO_LEAK_CHECK;
+       GET_FINAL_LEAK_COUNTS;
+
+       PRINT_LEAK_COUNTS(stderr);
 
        return 0;
 }
index 0c24a861ef59bbc305971819b1e1a3eb7807fd87..5dc266443940255d81f331a924ca000fb8ec0dca 100644 (file)
@@ -1,43 +1,31 @@
+leaked:     288 bytes in 18 blocks
+dubious:      0 bytes in  0 blocks
+reachable:    0 bytes in  0 blocks
+suppressed:   0 bytes in  0 blocks
 
-searching for pointers to 18 not-freed blocks.
-checked ... bytes.
-
-24 (8 direct, 16 indirect) bytes in 1 blocks are definitely lost in loss record 15 of 18
+48 (16 direct, 32 indirect) bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
-   by 0x........: mk (leak-cycle.c:11)
-   by 0x........: mkcycle (leak-cycle.c:22)
-   by 0x........: main (leak-cycle.c:36)
+   by 0x........: mk (leak-cycle.c:15)
+   by 0x........: mkcycle (leak-cycle.c:26)
+   by 0x........: main (leak-cycle.c:44)
 
 
-24 (8 direct, 16 indirect) bytes in 1 blocks are definitely lost in loss record 16 of 18
+48 (16 direct, 32 indirect) bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
-   by 0x........: mk (leak-cycle.c:11)
-   by 0x........: mkcycle (leak-cycle.c:22)
-   by 0x........: main (leak-cycle.c:37)
-
-
-48 (8 direct, 40 indirect) bytes in 1 blocks are definitely lost in loss record 17 of 18
-   at 0x........: malloc (vg_replace_malloc.c:...)
-   by 0x........: mk (leak-cycle.c:11)
-   by 0x........: mkcycle (leak-cycle.c:22)
+   by 0x........: mk (leak-cycle.c:15)
+   by 0x........: mkcycle (leak-cycle.c:26)
    by 0x........: main (leak-cycle.c:45)
 
 
-48 (8 direct, 40 indirect) bytes in 1 blocks are definitely lost in loss record 18 of 18
+96 (16 direct, 80 indirect) bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
-   by 0x........: mk (leak-cycle.c:11)
-   by 0x........: mkcycle (leak-cycle.c:22)
-   by 0x........: main (leak-cycle.c:59)
+   by 0x........: mk (leak-cycle.c:15)
+   by 0x........: mkcycle (leak-cycle.c:26)
+   by 0x........: main (leak-cycle.c:51)
 
-LEAK SUMMARY:
-   definitely lost: 32 bytes in 4 blocks.
-   indirectly lost: 112 bytes in 14 blocks.
-     possibly lost: 0 bytes in 0 blocks.
-   still reachable: 0 bytes in 0 blocks.
-        suppressed: 0 bytes in 0 blocks.
 
-ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
-malloc/free: in use at exit: 144 bytes in 18 blocks.
-malloc/free: 18 allocs, 0 frees, 144 bytes allocated.
-For a detailed leak analysis,  rerun with: --leak-check=yes
-For counts of detected errors, rerun with: -v
+96 (16 direct, 80 indirect) bytes in 1 blocks are definitely lost in loss record ... of ...
+   at 0x........: malloc (vg_replace_malloc.c:...)
+   by 0x........: mk (leak-cycle.c:15)
+   by 0x........: mkcycle (leak-cycle.c:26)
+   by 0x........: main (leak-cycle.c:63)
diff --git a/memcheck/tests/leak-cycle.stderr.exp2 b/memcheck/tests/leak-cycle.stderr.exp2
deleted file mode 100644 (file)
index 21cd815..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-
-searching for pointers to 19 not-freed blocks.
-checked ... bytes.
-
-24 (8+16) bytes in 1 blocks are definitely lost in loss record 15 of 19
-   at 0x........: malloc (vg_replace_malloc.c:...)
-   by 0x........: mk (leak-cycle.c:11)
-   by 0x........: mkcycle (leak-cycle.c:22)
-   by 0x........: main (leak-cycle.c:36)
-
-
-24 (8+16) bytes in 1 blocks are definitely lost in loss record 16 of 19
-   at 0x........: malloc (vg_replace_malloc.c:...)
-   by 0x........: mk (leak-cycle.c:11)
-   by 0x........: mkcycle (leak-cycle.c:22)
-   by 0x........: main (leak-cycle.c:37)
-
-
-48 (8+40) bytes in 1 blocks are definitely lost in loss record 18 of 19
-   at 0x........: malloc (vg_replace_malloc.c:...)
-   by 0x........: mk (leak-cycle.c:11)
-   by 0x........: mkcycle (leak-cycle.c:22)
-   by 0x........: main (leak-cycle.c:45)
-
-
-48 (8+40) bytes in 1 blocks are definitely lost in loss record 19 of 19
-   at 0x........: malloc (vg_replace_malloc.c:...)
-   by 0x........: mk (leak-cycle.c:11)
-   by 0x........: mkcycle (leak-cycle.c:22)
-   by 0x........: main (leak-cycle.c:59)
-
-LEAK SUMMARY:
-   definitely lost: 32 bytes in 4 blocks.
-   indirectly lost: 112 bytes in 14 blocks.
-   possibly lost:   0 bytes in 0 blocks.
-   still reachable: 24 bytes in 1 blocks.
-        suppressed: 0 bytes in 0 blocks.
-Reachable blocks (those to which a pointer was found) are not shown.
-To see them, rerun with: --show-reachable=yes
-
-ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
-malloc/free: in use at exit: 144 bytes in 18 blocks.
-malloc/free: 19 allocs, 1 frees, 168 bytes allocated.
-For a detailed leak analysis,  rerun with: --leak-check=yes
-For counts of detected errors, rerun with: -v
diff --git a/memcheck/tests/leak-cycle.stderr.exp64 b/memcheck/tests/leak-cycle.stderr.exp64
deleted file mode 100644 (file)
index d9e63d8..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-
-searching for pointers to 18 not-freed blocks.
-checked ... bytes.
-
-48 (16 direct, 32 indirect) bytes in 1 blocks are definitely lost in loss record 15 of 18
-   at 0x........: malloc (vg_replace_malloc.c:...)
-   by 0x........: mk (leak-cycle.c:11)
-   by 0x........: mkcycle (leak-cycle.c:22)
-   by 0x........: main (leak-cycle.c:36)
-
-
-48 (16 direct, 32 indirect) bytes in 1 blocks are definitely lost in loss record 16 of 18
-   at 0x........: malloc (vg_replace_malloc.c:...)
-   by 0x........: mk (leak-cycle.c:11)
-   by 0x........: mkcycle (leak-cycle.c:22)
-   by 0x........: main (leak-cycle.c:37)
-
-
-96 (16 direct, 80 indirect) bytes in 1 blocks are definitely lost in loss record 17 of 18
-   at 0x........: malloc (vg_replace_malloc.c:...)
-   by 0x........: mk (leak-cycle.c:11)
-   by 0x........: mkcycle (leak-cycle.c:22)
-   by 0x........: main (leak-cycle.c:45)
-
-
-96 (16 direct, 80 indirect) bytes in 1 blocks are definitely lost in loss record 18 of 18
-   at 0x........: malloc (vg_replace_malloc.c:...)
-   by 0x........: mk (leak-cycle.c:11)
-   by 0x........: mkcycle (leak-cycle.c:22)
-   by 0x........: main (leak-cycle.c:59)
-
-LEAK SUMMARY:
-   definitely lost: 64 bytes in 4 blocks.
-   indirectly lost: 224 bytes in 14 blocks.
-     possibly lost: 0 bytes in 0 blocks.
-   still reachable: 0 bytes in 0 blocks.
-        suppressed: 0 bytes in 0 blocks.
-
-ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
-malloc/free: in use at exit: 288 bytes in 18 blocks.
-malloc/free: 18 allocs, 0 frees, 288 bytes allocated.
-For a detailed leak analysis,  rerun with: --leak-check=yes
-For counts of detected errors, rerun with: -v
index a39d328c133dd4a40b49d0d50239df084476fd77..34c23cde0553c0bc65e6375e9c93adfaa2ccb057 100644 (file)
@@ -1,3 +1,2 @@
 prog: leak-cycle
-vgopts: --leak-resolution=high
-stderr_filter: filter_leak_check_size
+vgopts: -q --leak-check=yes --leak-resolution=high
diff --git a/memcheck/tests/leak-regroot.c b/memcheck/tests/leak-regroot.c
deleted file mode 100644 (file)
index 05cd774..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-#include "../memcheck.h"
-#include <stdlib.h>
-
-int main()
-{
-       register char *foo /* asm("esi") */;
-
-       foo = malloc(10);       /* not leaked */
-
-       VALGRIND_DO_LEAK_CHECK;
-
-       free(foo);
-
-       return 0;
-}
diff --git a/memcheck/tests/leak-regroot.stderr.exp b/memcheck/tests/leak-regroot.stderr.exp
deleted file mode 100644 (file)
index dd10b70..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-
-searching for pointers to 1 not-freed blocks.
-checked ... bytes.
-
-LEAK SUMMARY:
-   definitely lost: 0 bytes in 0 blocks.
-     possibly lost: 0 bytes in 0 blocks.
-   still reachable: 10 bytes in 1 blocks.
-        suppressed: 0 bytes in 0 blocks.
-Reachable blocks (those to which a pointer was found) are not shown.
-To see them, rerun with: --leak-check=full --show-reachable=yes
-
-ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
-malloc/free: in use at exit: 0 bytes in 0 blocks.
-malloc/free: 1 allocs, 1 frees, 10 bytes allocated.
-For a detailed leak analysis,  rerun with: --leak-check=yes
-For counts of detected errors, rerun with: -v
diff --git a/memcheck/tests/leak-regroot.stderr.exp2 b/memcheck/tests/leak-regroot.stderr.exp2
deleted file mode 100644 (file)
index ee9db44..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-
-searching for pointers to 2 not-freed blocks.
-checked ... bytes.
-
-LEAK SUMMARY:
-   definitely lost: 0 bytes in 0 blocks.
-   indirectly lost: 0 bytes in 0 blocks.
-   possibly lost:   0 bytes in 0 blocks.
-   still reachable: 34 bytes in 2 blocks.
-        suppressed: 0 bytes in 0 blocks.
-Reachable blocks (those to which a pointer was found) are not shown.
-To see them, rerun with: --leak-check=full --show-reachable=yes
-
-ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
-malloc/free: in use at exit: 0 bytes in 0 blocks.
-malloc/free: 2 allocs, 2 frees, 34 bytes allocated.
-For a detailed leak analysis,  rerun with: --leak-check=yes
-For counts of detected errors, rerun with: -v
diff --git a/memcheck/tests/leak-regroot.vgtest b/memcheck/tests/leak-regroot.vgtest
deleted file mode 100644 (file)
index 522342b..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-prog: leak-regroot
-stderr_filter: filter_leak_check_size
index 651342035987f6ada27bd332f9a7742c9df29f00..2c3c28b7acc57e587e2111453d7766354abdab49 100644 (file)
@@ -2,7 +2,7 @@
 searching for pointers to 11 not-freed blocks.
 checked ... bytes.
 
-72 (8 direct, 64 indirect) bytes in 1 blocks are definitely lost in loss record 11 of 11
+72 (8 direct, 64 indirect) bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: mk (leak-tree.c:11)
    by 0x........: main (leak-tree.c:25)
@@ -19,19 +19,19 @@ searching for pointers to 14 not-freed blocks.
 checked ... bytes.
 
 
-8 bytes in 1 blocks are definitely lost in loss record 1 of 14
+8 bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: mk (leak-tree.c:11)
    by 0x........: main (leak-tree.c:39)
 
 
-88 (8 direct, 80 indirect) bytes in 1 blocks are definitely lost in loss record 13 of 14
+88 (8 direct, 80 indirect) bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: mk (leak-tree.c:11)
    by 0x........: main (leak-tree.c:25)
 
 
-16 (8 direct, 8 indirect) bytes in 1 blocks are definitely lost in loss record 14 of 14
+16 (8 direct, 8 indirect) bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: mk (leak-tree.c:11)
    by 0x........: main (leak-tree.c:38)
index cbe995bcb33715b54dfc8fdf4db1b680588f4d1a..f7013d663bf6c2a9fb704f076efbdb5461e45c2b 100644 (file)
@@ -2,7 +2,7 @@
 searching for pointers to 11 not-freed blocks.
 checked ... bytes.
 
-8 bytes in 1 blocks are definitely lost in loss record 1 of 11
+8 bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: mk (leak-tree.c:11)
    by 0x........: main (leak-tree.c:25)
@@ -18,19 +18,19 @@ searching for pointers to 14 not-freed blocks.
 checked ... bytes.
 
 
-8 bytes in 1 blocks are definitely lost in loss record 1 of 14
+8 bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: mk (leak-tree.c:11)
    by 0x........: main (leak-tree.c:39)
 
 
-8 bytes in 1 blocks are definitely lost in loss record 2 of 14
+8 bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: mk (leak-tree.c:11)
    by 0x........: main (leak-tree.c:38)
 
 
-40 (8 direct, 32 indirect) bytes in 1 blocks are definitely lost in loss record 14 of 14
+40 (8 direct, 32 indirect) bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: mk (leak-tree.c:11)
    by 0x........: main (leak-tree.c:25)
index 669612716ff6f8435adccace0e4bb870d2d58977..a0ba471ca17000698ca02dd97e74b90336a8fed6 100644 (file)
@@ -2,7 +2,7 @@
 searching for pointers to 11 not-freed blocks.
 checked ... bytes.
 
-80 (16 direct, 64 indirect) bytes in 1 blocks are definitely lost in loss record 11 of 11
+80 (16 direct, 64 indirect) bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: mk (leak-tree.c:11)
    by 0x........: main (leak-tree.c:25)
@@ -19,19 +19,19 @@ searching for pointers to 14 not-freed blocks.
 checked ... bytes.
 
 
-16 bytes in 1 blocks are definitely lost in loss record 1 of 14
+16 bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: mk (leak-tree.c:11)
    by 0x........: main (leak-tree.c:39)
 
 
-16 bytes in 1 blocks are definitely lost in loss record 2 of 14
+16 bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: mk (leak-tree.c:11)
    by 0x........: main (leak-tree.c:38)
 
 
-80 (16 direct, 64 indirect) bytes in 1 blocks are definitely lost in loss record 14 of 14
+80 (16 direct, 64 indirect) bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: mk (leak-tree.c:11)
    by 0x........: main (leak-tree.c:25)
index a575cd6afaf06800531037f5b7b1402ae6e3a5dc..7a43b94466fc1d6281d51d4c38e2562d84347667 100644 (file)
@@ -1,3 +1,2 @@
 prog: leak-tree
 vgopts: --leak-resolution=high
-stderr_filter: filter_leak_check_size
diff --git a/memcheck/tests/leak.h b/memcheck/tests/leak.h
new file mode 100644 (file)
index 0000000..ec94fe0
--- /dev/null
@@ -0,0 +1,43 @@
+// These counters are used to get a delta between leak counts at startup
+// (eg. due to libc) and later on.  Necessary to get reliable leak tests
+// across different platforms.
+#define DECLARE_LEAK_COUNTERS \
+   long L0_bytes = 0, L_bytes = 0, L0_blocks = 0, L_blocks = 0; \
+   long D0_bytes = 0, D_bytes = 0, D0_blocks = 0, D_blocks = 0; \
+   long R0_bytes = 0, R_bytes = 0, R0_blocks = 0, R_blocks = 0; \
+   long S0_bytes = 0, S_bytes = 0, S0_blocks = 0, S_blocks = 0
+
+// Set a baseline, in case allocations have already happened.
+#define GET_INITIAL_LEAK_COUNTS \
+   do { \
+      VALGRIND_DO_QUICK_LEAK_CHECK; \
+      VALGRIND_COUNT_LEAKS(      L0_bytes,  D0_bytes,  R0_bytes,  S0_bytes );\
+      VALGRIND_COUNT_LEAK_BLOCKS(L0_blocks, D0_blocks, R0_blocks, S0_blocks); \
+   } while (0)
+
+// Set a baseline, in case allocations have already happened.
+#define GET_FINAL_LEAK_COUNTS \
+   do { \
+      VALGRIND_DO_QUICK_LEAK_CHECK; \
+      VALGRIND_COUNT_LEAKS(      L_bytes,  D_bytes,  R_bytes,  S_bytes ); \
+      VALGRIND_COUNT_LEAK_BLOCKS(L_blocks, D_blocks, R_blocks, S_blocks); \
+      L_bytes -= L0_bytes;  L_blocks -= L0_blocks; \
+      D_bytes -= D0_bytes;  D_blocks -= D0_blocks; \
+      R_bytes -= R0_bytes;  R_blocks -= R0_blocks; \
+      S_bytes -= S0_bytes;  S_blocks -= S0_blocks; \
+   } while (0)
+
+// Print leak counts.  When used in conjunction with -q the normal counts
+// aren't shown, which is what we want.
+#define PRINT_LEAK_COUNTS(where) \
+   do { \
+      fprintf(where,"leaked:     %3ld bytes in %2ld blocks\n", \
+                     L_bytes,L_blocks); \
+      fprintf(where,"dubious:    %3ld bytes in %2ld blocks\n", \
+                     D_bytes,D_blocks); \
+      fprintf(where,"reachable:  %3ld bytes in %2ld blocks\n", \
+                     R_bytes,R_blocks); \
+      fprintf(where,"suppressed: %3ld bytes in %2ld blocks\n", \
+                     S_bytes,S_blocks); \
+   } while (0)
+
index 7d4fbdeb60b68d63a18d03796945ad6674a4babb..1b4b4f1405fea3e2cda1639d70b51ffac40868b1 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <assert.h>
 #include "../memcheck.h"
+#include "leak.h"
 
 /* 
    Incompetent memory management
@@ -10,7 +11,7 @@
    structure with refcounting.
  */
 
-static int bytes, blocks;
+static long bytes, blocks;
 
 struct node {
        struct node *l, *r;
@@ -95,63 +96,49 @@ static struct node *mk()
 int main()
 {
        int i;
-       long base_definite, base_dubious, base_reachable, base_suppressed;
-       long definite, dubious, reachable, suppressed;
-        int total;
+        long total;
+        DECLARE_LEAK_COUNTERS;
 
         /* we require these longs to have same size as a machine word */
         assert(sizeof(long) == sizeof(void*));
 
        /* get a baseline in case the runtime allocated some memory */
-       VALGRIND_DO_LEAK_CHECK;
-       base_definite = base_dubious = base_reachable = base_suppressed = 0;
-       VALGRIND_COUNT_LEAKS(base_definite, base_dubious, 
-                            base_reachable, base_suppressed);
+        GET_INITIAL_LEAK_COUNTS;
 
-       for(i = 0; i < ITER; i++) {
+       for (i = 0; i < ITER; i++) {
                mk();
 
                if ((i % (ITER/10)) == 0) {
                        if (0)
-                               printf("%d living blocks, %d bytes\n",
+                               printf("%ld living blocks, %ld bytes\n",
                                       blocks, bytes);
-                       VALGRIND_DO_LEAK_CHECK;
+                       //VALGRIND_DO_LEAK_CHECK;
                }
        }
        
        /* "free all memory" */
-       for(i = 0; i < N; i++)
+       for (i = 0; i < N; i++)
                assign(&nodes[i], NULL);
 
 
        if (0)
-               printf("FINISHED: %d living blocks, %d bytes\n",
+               printf("FINISHED: %ld living blocks, %ld bytes\n",
                       blocks, bytes);
 
-       VALGRIND_DO_LEAK_CHECK;
+        GET_FINAL_LEAK_COUNTS;
 
-       /* Shouldn't be necessary, but COUNT_LEAKS doesn't define its
-          result values */
-       definite = dubious = reachable = suppressed = 0;
-       VALGRIND_COUNT_LEAKS(definite, dubious, reachable, suppressed);
+       total = L_bytes + D_bytes + R_bytes + S_bytes;
 
-       definite   -= base_definite;
-       dubious    -= base_dubious;
-       reachable  -= base_reachable;
-       suppressed -= base_suppressed;
+        if (0) {
+               PRINT_LEAK_COUNTS(stderr);
+        }
 
-       total = definite+dubious+reachable+suppressed;
-
-       if (0)
-               printf("leaks: definite %d, dubious %d, reachable %d, suppressed %d = %d\n",
-                      (int)definite, (int)dubious, (int)reachable, (int)suppressed, total);
-
-       if (reachable != 0)
+       if (R_bytes != 0)
                printf("FAILED: I freed everything, "
-                      "but there's still %d bytes reachable\n", 
-                      (int)reachable);
+                      "but there's still %ld bytes (in %ld blocks) reachable\n", 
+                      R_bytes, R_blocks);
        else if (total != bytes)
-               printf("FAILED: I count %d bytes, leakcheck says %d\n",
+               printf("FAILED: I count %ld bytes, leakcheck says %ld\n",
                       bytes, total);
        else
                printf("PASS\n");
index a0367236dcdb907260c601feb2ae59360255df29..0d63fa9f253ac9f9517d02ad9e428f6fb2fdb284 100644 (file)
@@ -35,25 +35,25 @@ Invalid write of size 1
    by 0x........: main (mempool.c:148)
 
 
-10 bytes in 1 blocks are definitely lost in loss record 2 of 5
+10 bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: allocate (mempool.c:99)
    by 0x........: test (mempool.c:135)
    by 0x........: main (mempool.c:148)
 
 
-10 bytes in 1 blocks are definitely lost in loss record 3 of 5
+10 bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: allocate (mempool.c:99)
    by 0x........: test (mempool.c:115)
    by 0x........: main (mempool.c:148)
 
 
-20 bytes in 1 blocks are definitely lost in loss record 4 of 5
+20 bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: allocate (mempool.c:99)
    by 0x........: test (mempool.c:116)
    by 0x........: main (mempool.c:148)
 
 
-28 (20 direct, 8 indirect) bytes in 1 blocks are definitely lost in loss record 5 of 5
+28 (20 direct, 8 indirect) bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: make_pool (mempool.c:37)
    by 0x........: test (mempool.c:111)
index ad50a26634bdcbab2d0b397e9202359f233ec219..8fdfeac5da8ddb08c56f4efab60caf7fff483258 100644 (file)
@@ -35,25 +35,25 @@ Invalid write of size 1
    by 0x........: main (mempool.c:148)
 
 
-10 bytes in 1 blocks are definitely lost in loss record 1 of 5
+10 bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: allocate (mempool.c:99)
    by 0x........: test (mempool.c:135)
    by 0x........: main (mempool.c:148)
 
 
-10 bytes in 1 blocks are definitely lost in loss record 2 of 5
+10 bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: allocate (mempool.c:99)
    by 0x........: test (mempool.c:115)
    by 0x........: main (mempool.c:148)
 
 
-20 bytes in 1 blocks are definitely lost in loss record 4 of 5
+20 bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: allocate (mempool.c:99)
    by 0x........: test (mempool.c:116)
    by 0x........: main (mempool.c:148)
 
 
-48 (32 direct, 16 indirect) bytes in 1 blocks are definitely lost in loss record 5 of 5
+48 (32 direct, 16 indirect) bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: make_pool (mempool.c:37)
    by 0x........: test (mempool.c:111)
index 2988ee37c02d47c60d74b2e2c14a21c953e02d34..91683c16ae8ef9d087bb49498c3e3074bfee06e2 100644 (file)
@@ -1,4 +1,4 @@
 
-1,000 bytes in 1 blocks are definitely lost in loss record 1 of 1
+1,000 bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: main (nanoleak.c:6)
index d633a3626dff5c8fefb3d093194e2c0ae54d5af7..30053e67b4094cd89fc8faf8dc7f427b146e8873 100644 (file)
@@ -1,3 +1,2 @@
 vgopts: --leak-check=yes -q
 prog: nanoleak
-stderr_filter: filter_leak_check_size
index 57e8947012e9e901812dabaf5c704aa3bb198c31..e300df8959ddb038764e841d7ad720f9d5f983ec 100644 (file)
@@ -1,3 +1,2 @@
 vgopts: --leak-check=yes --suppressions=nanoleak.supp -q
 prog: nanoleak
-stderr_filter: filter_leak_check_size
index 047dcce4d172657254f8955b7979d2f4ef17805a..3380aef575c0c4410929410488134b053d4320ec 100644 (file)
@@ -1,5 +1,6 @@
 /* 
-   Make sure that leak-check's pointer tracing avoids traps
+   Make sure that leak-check's pointer tracing avoids traps, i.e. tricky
+   memory areas where it could crash if not careful.
  */
 
 #include <stdio.h>
@@ -45,14 +46,16 @@ int main()
        }
 
        ptrs = malloc(nptrs * sizeof(char *));
-       for(i = 0; i < nptrs; i++)
+       for (i = 0; i < nptrs; i++)
                ptrs[i] = (char *)((long)i << stepbits);
 
        /* lay some traps */
+        /* non-RWX memory, and MAP_NORESERVE if present */
        map = mmap(0, stepsize * 2, PROT_NONE, MAP_PRIVATE|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0);
        if (map == (char *)-1)
                perror("trap 1 failed");
 
+        /* write-only memory, and MAP_NORESERVE if supported */
        map = mmap(0, stepsize * 2, PROT_WRITE, MAP_PRIVATE|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0);
        if (map == (char *)-1)
                perror("trap 2 failed");
@@ -60,12 +63,12 @@ int main()
        /* non-zero mmap of a zero-length file -> SIGBUS */
        fd = open("./pointer-trace-test-file", O_RDWR | O_CREAT | O_EXCL, 0600);
        unlink("./pointer-trace-test-file");
-
        map = mmap(0, stepsize * 2, PROT_WRITE|PROT_READ, MAP_PRIVATE, fd, 0);
        if (map == (char *)-1)
                perror("trap 3 failed");
        //printf("trap 3 = %p-%p\n", map, map+stepsize*2);
 
+        /* unmapped memory that's marked as defined */
        map = mmap(0, 256*1024, PROT_NONE, MAP_PRIVATE|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0);
        if (map == (char *)-1)
                perror("trap 4 failed");
@@ -76,7 +79,11 @@ int main()
 
        VALGRIND_DO_LEAK_CHECK;
 
-       ptrs = 0;
+       free(ptrs);
+
+        // We deliberately make a leak, it'll be obvious if something went
+        // wrong because the message won't be printed.
+        ptrs = malloc(1000);
 
        return 0;
 }
index 45dd0381a48d8bfa1848c51d3e92d77daa5f5fa9..7633d533ff694edd8766812799476c51c1dcc74d 100644 (file)
@@ -1,28 +1,4 @@
 
-searching for pointers to 1 not-freed blocks.
-checked ... bytes.
-
-LEAK SUMMARY:
-   definitely lost: 0 bytes in 0 blocks.
-     possibly lost: 0 bytes in 0 blocks.
-   still reachable: 1,048,576 bytes in 1 blocks.
-        suppressed: 0 bytes in 0 blocks.
-Reachable blocks (those to which a pointer was found) are not shown.
-To see them, rerun with: --leak-check=full --show-reachable=yes
-
-ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
-malloc/free: in use at exit: 1,048,576 bytes in 1 blocks.
-malloc/free: 1 allocs, 0 frees, 1,048,576 bytes allocated.
-For counts of detected errors, rerun with: -v
-searching for pointers to 1 not-freed blocks.
-checked ... bytes.
-
-1,048,576 bytes in 1 blocks are definitely lost in loss record 1 of 1
+1,000 bytes in 1 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
-   by 0x........: main (pointer-trace.c:47)
-
-LEAK SUMMARY:
-   definitely lost: 1,048,576 bytes in 1 blocks.
-     possibly lost: 0 bytes in 0 blocks.
-   still reachable: 0 bytes in 0 blocks.
-        suppressed: 0 bytes in 0 blocks.
+   by 0x........: main (pointer-trace.c:86)
diff --git a/memcheck/tests/pointer-trace.stderr.exp2 b/memcheck/tests/pointer-trace.stderr.exp2
deleted file mode 100644 (file)
index 3f6ec1a..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-
-searching for pointers to 2 not-freed blocks.
-checked ... bytes.
-
-LEAK SUMMARY:
-   definitely lost: 0 bytes in 0 blocks.
-   indirectly lost: 0 bytes in 0 blocks.
-   possibly lost:   0 bytes in 0 blocks.
-   still reachable: 1,048,600 bytes in 2 blocks.
-        suppressed: 0 bytes in 0 blocks.
-Reachable blocks (those to which a pointer was found) are not shown.
-To see them, rerun with: --leak-check=full --show-reachable=yes
-
-ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
-malloc/free: in use at exit: 1,048,576 bytes in 1 blocks.
-malloc/free: 2 allocs, 1 frees, 1,048,600 bytes allocated.
-For counts of detected errors, rerun with: -v
-searching for pointers to 1 not-freed blocks.
-checked ... bytes.
-
-1,048,576 bytes in 1 blocks are definitely lost in loss record 1 of 1
-   at 0x........: malloc (vg_replace_malloc.c:...)
-   by 0x........: main (pointer-trace.c:24)
-
-LEAK SUMMARY:
-   definitely lost: 1,048,576 bytes in 1 blocks.
-   indirectly lost: 0 bytes in 0 blocks.
-   possibly lost:   0 bytes in 0 blocks.
-   still reachable: 0 bytes in 0 blocks.
-        suppressed: 0 bytes in 0 blocks.
-Reachable blocks (those to which a pointer was found) are not shown.
-To see them, rerun with: --leak-check=full --show-reachable=yes
diff --git a/memcheck/tests/pointer-trace.stderr.exp3 b/memcheck/tests/pointer-trace.stderr.exp3
deleted file mode 100644 (file)
index ce9dba3..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-
-searching for pointers to 1 not-freed blocks.
-checked ... bytes.
-
-LEAK SUMMARY:
-   definitely lost: 0 bytes in 0 blocks.
-     possibly lost: 0 bytes in 0 blocks.
-   still reachable: 1,048,576 bytes in 1 blocks.
-        suppressed: 0 bytes in 0 blocks.
-Reachable blocks (those to which a pointer was found) are not shown.
-To see them, rerun with: --leak-check=full --show-reachable=yes
-
-ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
-malloc/free: in use at exit: 1,048,576 bytes in 1 blocks.
-malloc/free: 1 allocs, 0 frees, 1,048,576 bytes allocated.
-For counts of detected errors, rerun with: -v
-searching for pointers to 1 not-freed blocks.
-checked ... bytes.
-
-1,048,576 bytes in 1 blocks are possibly lost in loss record 1 of 1
-   at 0x........: malloc (vg_replace_malloc.c:...)
-   by 0x........: main (pointer-trace.c:24)
-
-LEAK SUMMARY:
-   definitely lost: 0 bytes in 0 blocks.
-     possibly lost: 1,048,576 bytes in 1 blocks.
-   still reachable: 0 bytes in 0 blocks.
-        suppressed: 0 bytes in 0 blocks.
-Reachable blocks (those to which a pointer was found) are not shown.
-To see them, rerun with: --leak-check=full --show-reachable=yes
diff --git a/memcheck/tests/pointer-trace.stderr.exp64 b/memcheck/tests/pointer-trace.stderr.exp64
deleted file mode 100644 (file)
index 9eba36e..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-
-searching for pointers to 1 not-freed blocks.
-checked ... bytes.
-
-LEAK SUMMARY:
-   definitely lost: 0 bytes in 0 blocks.
-     possibly lost: 0 bytes in 0 blocks.
-   still reachable: 2,097,152 bytes in 1 blocks.
-        suppressed: 0 bytes in 0 blocks.
-Reachable blocks (those to which a pointer was found) are not shown.
-To see them, rerun with: --leak-check=full --show-reachable=yes
-
-ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
-malloc/free: in use at exit: 2,097,152 bytes in 1 blocks.
-malloc/free: 1 allocs, 0 frees, 2,097,152 bytes allocated.
-For counts of detected errors, rerun with: -v
-searching for pointers to 1 not-freed blocks.
-checked ... bytes.
-
-2,097,152 bytes in 1 blocks are definitely lost in loss record 1 of 1
-   at 0x........: malloc (vg_replace_malloc.c:...)
-   by 0x........: main (pointer-trace.c:24)
-
-LEAK SUMMARY:
-   definitely lost: 2,097,152 bytes in 1 blocks.
-     possibly lost: 0 bytes in 0 blocks.
-   still reachable: 0 bytes in 0 blocks.
-        suppressed: 0 bytes in 0 blocks.
-Reachable blocks (those to which a pointer was found) are not shown.
-To see them, rerun with: --leak-check=full --show-reachable=yes
index 81cae7d2ebfcd6894f471b93b4670124d9d6d29a..fa9228f1bb9cdff05f6d04f34c5c057ade81b8f9 100644 (file)
@@ -1,3 +1,2 @@
 prog: pointer-trace
-vgopts: --leak-check=yes
-stderr_filter: filter_leak_check_size
+vgopts: -q --leak-check=yes
index 0b1f57dcb716dcde7ce40b99209b0079e595d10a..002f716e205d66909ee3f01d5909cddd57d40f7a 100644 (file)
@@ -1,5 +1,5 @@
 
-1,000 bytes in 1,000 blocks are definitely lost in loss record 1 of 1
+1,000 bytes in 1,000 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: test (trivialleak.c:8)
    by 0x........: main (trivialleak.c:12)
index 6b061496a432a8d3028e7fa1c029850ad1a32a25..17507b7a3a32ccd6ee55f2275bc03e76aaa19346 100644 (file)
@@ -1,5 +1,5 @@
 
-999 bytes in 999 blocks are definitely lost in loss record 2 of 2
+999 bytes in 999 blocks are definitely lost in loss record ... of ...
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: test (trivialleak.c:8)
    by 0x........: main (trivialleak.c:12)
index 7d66a3c0d36f16b0faab2efb4e93a0958bef966b..58641ea33f2e8839ef8963da85b58bc0ec5b201b 100644 (file)
@@ -1,3 +1,2 @@
 vgopts: --leak-check=yes -q
 prog: trivialleak
-stderr_filter: filter_leak_check_size
index 453fed3afab4c24c7e5af99962e738abe8efdffa..27511bf3403026dc266dc06bc3e18d04b6f8768b 100644 (file)
   <unique>0x........</unique>
   <tid>...</tid>
   <kind>Leak_DefinitelyLost</kind>
-  <what>396 bytes in 1 blocks are definitely lost in loss record 1 of 1</what>
+  <what>396 bytes in 1 blocks are definitely lost in loss record ... of ...</what>
   <leakedbytes>396</leakedbytes>
   <leakedblocks>1</leakedblocks>
   <stack>
index 58a84f1f6fc0845ee220b2096017ab4cc6f3fce3..4b8aebf389dc9d6f07cb8dd00066f3f897ef94df 100644 (file)
   <unique>0x........</unique>
   <tid>...</tid>
   <kind>Leak_DefinitelyLost</kind>
-  <what>396 bytes in 1 blocks are definitely lost in loss record 1 of 1</what>
+  <what>396 bytes in 1 blocks are definitely lost in loss record ... of ...</what>
   <leakedbytes>396</leakedbytes>
   <leakedblocks>1</leakedblocks>
   <stack>
index 49e24a6b561bf003df912074f3df5578a8be4b70..78a8ec8bbf7b5c318ed57fb7ff244042c0679d35 100644 (file)
   <unique>0x........</unique>
   <tid>...</tid>
   <kind>Leak_DefinitelyLost</kind>
-  <what>396 bytes in 1 blocks are definitely lost in loss record 1 of 1</what>
+  <what>396 bytes in 1 blocks are definitely lost in loss record ... of ...</what>
   <leakedbytes>396</leakedbytes>
   <leakedblocks>1</leakedblocks>
   <stack>