From: Julian Seward Date: Fri, 22 Aug 2014 19:26:23 +0000 (+0000) Subject: Memcheck: add a new flag, --show-mismatched-frees=no|yes [yes], to X-Git-Tag: svn/VALGRIND_3_10_0~150 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7566a219b024abdcf21e71f0f71c30bdbc055b57;p=thirdparty%2Fvalgrind.git Memcheck: add a new flag, --show-mismatched-frees=no|yes [yes], to optionally disable allocator/deallocator mismatch checking. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14336 --- diff --git a/NEWS b/NEWS index fab8508988..09103fe892 100644 --- 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 diff --git a/memcheck/docs/mc-manual.xml b/memcheck/docs/mc-manual.xml index 887e82d53d..9ae7c67f28 100644 --- a/memcheck/docs/mc-manual.xml +++ b/memcheck/docs/mc-manual.xml @@ -1090,6 +1090,44 @@ is + + + + + + When enabled, Memcheck checks that heap blocks are + deallocated using a function that matches the allocating + function. That is, it expects free to be + used to deallocate blocks allocated + by malloc, delete for + blocks allocated by new, + and delete[] for blocks allocated + by new[]. 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. + + There is however a scenario where such mismatches cannot + be avoided. That is when the user provides implementations of + new/new[] that + call malloc and + of delete/delete[] that + call free, and these functions are + asymmetrically inlined. For example, imagine + that delete[] is inlined + but new[] is not. The result is that + Memcheck "sees" all delete[] calls as direct + calls to free, even when the program source + contains no mismatched calls. + + This causes a lot of confusing and irrelevant error + reports. --show-mismatched-frees=no disables + these checks. It is not generally advisable to disable them, + though, because you may miss real errors as a result. + + + diff --git a/memcheck/mc_include.h b/memcheck/mc_include.h index 9a5017b37b..258d3c6665 100644 --- a/memcheck/mc_include.h +++ b/memcheck/mc_include.h @@ -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 ---*/ diff --git a/memcheck/mc_main.c b/memcheck/mc_main.c index 970f3faa73..9146064fb1 100644 --- a/memcheck/mc_main.c +++ b/memcheck/mc_main.c @@ -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= 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 ); } diff --git a/memcheck/mc_malloc_wrappers.c b/memcheck/mc_malloc_wrappers.c index 6b152766ea..d88890ba7b 100644 --- a/memcheck/mc_malloc_wrappers.c +++ b/memcheck/mc_malloc_wrappers.c @@ -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