]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Memcheck: add a new flag, --show-mismatched-frees=no|yes [yes], to
authorJulian Seward <jseward@acm.org>
Fri, 22 Aug 2014 19:26:23 +0000 (19:26 +0000)
committerJulian Seward <jseward@acm.org>
Fri, 22 Aug 2014 19:26:23 +0000 (19:26 +0000)
optionally disable allocator/deallocator mismatch checking.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14336

NEWS
memcheck/docs/mc-manual.xml
memcheck/mc_include.h
memcheck/mc_main.c
memcheck/mc_malloc_wrappers.c

diff --git a/NEWS b/NEWS
index fab8508988853f1ee510088624184929c8b264fb..09103fe8921cf7243bf896ad296ad1c04b795ac8 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -20,6 +20,8 @@ Release 3.10.0 (?? ?????? 201?)
     ...) has several fields not initialised, an error is now reported for
     each field. Previously, an error was reported only for the first wrong
     field.
+  - new flag --show-mismatched-frees=no|yes [yes], to optionally disable
+    allocator/deallocator mismatch checking.
 
 * Helgrind:
   - Race condition error message with allocated blocks also show
index 887e82d53dfa431a14e440a6aa70d07dd1f89494..9ae7c67f28c32641d08aca698cbbe1146153eec1 100644 (file)
@@ -1090,6 +1090,44 @@ is <option>--errors-for-leak-kinds=definite,possible</option>
     </listitem>
   </varlistentry>
 
+  <varlistentry id="opt.show-mismatched-frees"
+                xreflabel="--show-mismatched-frees">
+    <term>
+      <option><![CDATA[--show-mismatched-frees=<yes|no> [default: yes] ]]></option>
+    </term>
+    <listitem>
+      <para>When enabled, Memcheck checks that heap blocks are
+      deallocated using a function that matches the allocating
+      function.  That is, it expects <varname>free</varname> to be
+      used to deallocate blocks allocated
+      by <varname>malloc</varname>, <varname>delete</varname> for
+      blocks allocated by <varname>new</varname>,
+      and <varname>delete[]</varname> for blocks allocated
+      by <varname>new[]</varname>.  If a mismatch is detected, an
+      error is reported.  This is in general important because in some
+      environments, freeing with a non-matching function can cause
+      crashes.</para>
+
+      <para>There is however a scenario where such mismatches cannot
+      be avoided.  That is when the user provides implementations of
+      <varname>new</varname>/<varname>new[]</varname> that
+      call <varname>malloc</varname> and
+      of <varname>delete</varname>/<varname>delete[]</varname> that
+      call <varname>free</varname>, and these functions are
+      asymmetrically inlined.  For example, imagine
+      that <varname>delete[]</varname> is inlined
+      but <varname>new[]</varname> is not.  The result is that
+      Memcheck "sees" all <varname>delete[]</varname> calls as direct
+      calls to <varname>free</varname>, even when the program source
+      contains no mismatched calls.</para>
+
+      <para>This causes a lot of confusing and irrelevant error
+      reports.  <varname>--show-mismatched-frees=no</varname> disables
+      these checks.  It is not generally advisable to disable them,
+      though, because you may miss real errors as a result.</para>
+    </listitem>
+  </varlistentry>
+
   <varlistentry id="opt.ignore-ranges" xreflabel="--ignore-ranges">
     <term>
       <option><![CDATA[--ignore-ranges=0xPP-0xQQ[,0xRR-0xSS] ]]></option>
index 9a5017b37bdc313545c7413294c733d373327519..258d3c6665630d782c97badd82af08c13ff9241f 100644 (file)
@@ -588,6 +588,9 @@ extern KeepStacktraces MC_(clo_keep_stacktraces);
 */
 extern Int MC_(clo_mc_level);
 
+/* Should we show mismatched frees?  Default: YES */
+extern Bool MC_(clo_show_mismatched_frees);
+
 
 /*------------------------------------------------------------*/
 /*--- Instrumentation                                      ---*/
index 970f3faa737843039dfec915f6c05859185a1a7f..9146064fb13de895fab23724d349abee08e42298 100644 (file)
@@ -5194,6 +5194,7 @@ Int           MC_(clo_malloc_fill)            = -1;
 Int           MC_(clo_free_fill)              = -1;
 KeepStacktraces MC_(clo_keep_stacktraces)     = KS_alloc_then_free;
 Int           MC_(clo_mc_level)               = 2;
+Bool          MC_(clo_show_mismatched_frees)  = True;
 
 static const HChar * MC_(parse_leak_heuristics_tokens) =
    "-,stdstring,length64,newarray,multipleinheritance";
@@ -5338,6 +5339,9 @@ static Bool mc_process_cmd_line_options(const HChar* arg)
    else if VG_XACT_CLO(arg, "--keep-stacktraces=none",
                        MC_(clo_keep_stacktraces), KS_none) {}
 
+   else if VG_BOOL_CLO(arg, "--show-mismatched-frees",
+                       MC_(clo_show_mismatched_frees)) {}
+
    else
       return VG_(replacement_malloc_process_cmd_line_option)(arg);
 
@@ -5384,6 +5388,7 @@ static void mc_print_usage(void)
 "    --free-fill=<hexnumber>          fill free'd areas with given value\n"
 "    --keep-stacktraces=alloc|free|alloc-and-free|alloc-then-free|none\n"
 "        stack trace(s) to keep for malloc'd/free'd areas       [alloc-then-free]\n"
+"    --show-mismatched-frees=no|yes   show frees that don't match the allocator? [yes]\n"
 , plo_default
    );
 }
index 6b152766eadf390c767513620963dd7b7685599a..d88890ba7b7f6074fdf0e57730531274c04eaba8 100644 (file)
@@ -456,6 +456,10 @@ void die_and_free_mem ( ThreadId tid, MC_Chunk* mc, SizeT rzB )
 static
 void record_freemismatch_error (ThreadId tid, MC_Chunk* mc)
 {
+   /* Only show such an error if the user hasn't disabled doing so. */
+   if (!MC_(clo_show_mismatched_frees))
+      return;
+
    /* MC_(record_freemismatch_error) reports errors for still
       allocated blocks but we are in the middle of freeing it.  To
       report the error correctly, we re-insert the chunk (making it